Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/InputHandler.cc @ 928

Last change on this file since 928 was 928, checked in by rgrieder, 16 years ago
  • added destroy code for the InputHandler
File size: 6.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Reto Grieder
23 *   Co-authors:
24 *      Some guy writing the example code from Ogre
25 *
26 */
27
28/**
29 @file
30 @brief Implementation of a little Input handler that distributes everything
31        coming from OIS.
32 */
33
34#include "OrxonoxStableHeaders.h"
35
36#include "core/CoreIncludes.h"
37#include "Orxonox.h"
38#include "InputEventListener.h"
39#include "InputHandler.h"
40
41namespace orxonox
42{
43  /**
44    @brief Constructor only resets the pointer values to 0.
45  */
46  InputHandler::InputHandler()
47  {
48    //RegisterObject(InputHandler);
49    this->mouse_       = 0;
50    this->keyboard_    = 0;
51    this->inputSystem_ = 0;
52  }
53
54  /**
55    @brief The one instance of the InputHandler is stored in this function.
56    @return The pointer to the only instance of the InputHandler
57  */
58  InputHandler *InputHandler::getSingleton()
59  {
60    static InputHandler theOnlyInstance;
61    return &theOnlyInstance;
62  }
63
64  /**
65    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
66           assigns the key bindings.
67    @param windowHnd The window handle of the render window
68    @param windowWidth The width of the render window
69    @param windowHeight The height of the render window
70  */
71  void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight)
72  {
73    if (!inputSystem_)
74    {
75      // Setup basic variables
76      OIS::ParamList paramList;
77      std::ostringstream windowHndStr;
78
79      // Fill parameter list
80      windowHndStr << (unsigned int)windowHnd;
81      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
82
83#if defined OIS_LINUX_PLATFORM
84      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
85#endif
86
87      // Create inputsystem
88      inputSystem_ = OIS::InputManager::createInputSystem(paramList);
89
90      // If possible create a buffered keyboard
91      if (inputSystem_->numKeyboards() > 0)
92      {
93        keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
94        keyboard_->setEventCallback(this);
95      }
96
97      // If possible create a buffered mouse
98      if (inputSystem_->numMice() > 0 )
99      {
100        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
101        mouse_->setEventCallback(this);
102
103        // Set mouse region
104        this->setWindowExtents(windowWidth, windowHeight);
105      }
106    }
107
108    // load the key bindings
109    InputEvent empty = {0, false, 0, 0, 0};
110    for (int i = 0; i < this->numberOfKeys_; i++)
111      this->bindingsKeyPressed_[i] = empty;
112
113    //assign 'abort' to the escape key
114    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
115  }
116
117  /**
118    @brief Destroys all the created input devices.
119  */
120  void InputHandler::destroy()
121  {
122    this->inputSystem_->destroyInputObject(this->mouse_);
123    this->inputSystem_->destroyInputObject(this->keyboard_);
124    OIS::InputManager::destroyInputSystem(this->inputSystem_);
125
126    //TODO: If the InputHandler has been destroyed, how does it know?
127  }
128
129  /**
130    @brief Updates the InputHandler
131    @param dt Delta time
132  */
133  void InputHandler::tick(float dt)
134  {
135    // capture all the input. That calls the event handlers.
136    if (mouse_)
137      mouse_->capture();
138
139    if (keyboard_)
140      keyboard_->capture();
141  }
142
143  /**
144    @brief Adjusts the mouse window metrics.
145    This method has to be called every time the size of the window changes.
146    @param width The new width of the render window
147    @param height the new height of the render window
148  */
149  void InputHandler::setWindowExtents(int width, int height)
150  {
151    // Set mouse region (if window resizes, we should alter this to reflect as well)
152    const OIS::MouseState &mouseState = mouse_->getMouseState();
153    mouseState.width  = width;
154    mouseState.height = height;
155  }
156
157  /**
158    @brief Calls all the objects from classes that derive from InputEventListener.
159    @param evt The input event that occured.
160  */
161  inline void InputHandler::callListeners(orxonox::InputEvent &evt)
162  {
163    for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; )
164    {
165      if (it->bActive_)
166        (it++)->eventOccured(evt);
167      else
168        it++;
169    }
170  }
171
172  /**
173    @brief Event handler for the keyPressed Event.
174    @param e Event information
175  */
176  bool InputHandler::keyPressed(const OIS::KeyEvent &e)
177  {
178    callListeners(this->bindingsKeyPressed_[(int)e.key]);
179    return true;
180  }
181
182  /**
183    @brief Event handler for the keyReleased Event.
184    @param e Event information
185  */
186  bool InputHandler::keyReleased(const OIS::KeyEvent &e)
187  {
188    return true;
189  }
190
191  /**
192    @brief Event handler for the mouseMoved Event.
193    @param e Event information
194  */
195  bool InputHandler::mouseMoved(const OIS::MouseEvent &e)
196  {
197    return true;
198  }
199
200  /**
201    @brief Event handler for the mousePressed Event.
202    @param e Event information
203    @param id The ID of the mouse button
204  */
205  bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
206  {
207    return true;
208  }
209
210  /**
211    @brief Event handler for the mouseReleased Event.
212    @param e Event information
213    @param id The ID of the mouse button
214  */
215  bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
216  {
217    return true;
218  }
219
220}
Note: See TracBrowser for help on using the repository browser.