Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 929 was 929, checked in by rgrieder, 16 years ago
  • removed getRoot() from GaphicsEngine —> added getRenderWindow() —> added 3 function to control render loop
  • rearranged the sequence of methods in Orxonox.cc to make it a little bit more logical
  • added deletion code in Orxonox.cc destructor
  • fixed a bug in AudioManger destructor
  • fixed a bug in InputHandler destroy()
File size: 6.2 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      mouse_(0), keyboard_(0), inputSystem_(0),
48      uninitialized_(true)
49  {
50    //RegisterObject(InputHandler);
51  }
52
53  /**
54    @brief Destructor only called at the end of the program
55  */
56  InputHandler::~InputHandler()
57  {
58    this->destroy();
59  }
60
61  /**
62    @brief The one instance of the InputHandler is stored in this function.
63    @return The pointer to the only instance of the InputHandler
64  */
65  InputHandler *InputHandler::getSingleton()
66  {
67    static InputHandler theOnlyInstance;
68    return &theOnlyInstance;
69  }
70
71  /**
72    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
73           assigns the key bindings.
74    @param windowHnd The window handle of the render window
75    @param windowWidth The width of the render window
76    @param windowHeight The height of the render window
77  */
78  void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight)
79  {
80    if (this->uninitialized_ || !this->inputSystem_)
81    {
82      // Setup basic variables
83      OIS::ParamList paramList;
84      std::ostringstream windowHndStr;
85
86      // Fill parameter list
87      windowHndStr << (unsigned int)windowHnd;
88      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
89
90#if defined OIS_LINUX_PLATFORM
91      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
92#endif
93
94      // Create inputsystem
95      inputSystem_ = OIS::InputManager::createInputSystem(paramList);
96
97      // If possible create a buffered keyboard
98      if (inputSystem_->numKeyboards() > 0)
99      {
100        keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
101        keyboard_->setEventCallback(this);
102      }
103
104      // If possible create a buffered mouse
105      if (inputSystem_->numMice() > 0 )
106      {
107        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
108        mouse_->setEventCallback(this);
109
110        // Set mouse region
111        this->setWindowExtents(windowWidth, windowHeight);
112      }
113
114      uninitialized_ = false;
115    }
116
117    // load the key bindings
118    InputEvent empty = {0, false, 0, 0, 0};
119    for (int i = 0; i < this->numberOfKeys_; i++)
120      this->bindingsKeyPressed_[i] = empty;
121
122    //assign 'abort' to the escape key
123    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
124  }
125
126  /**
127    @brief Destroys all the created input devices.
128  */
129  void InputHandler::destroy()
130  {
131    if (!this->inputSystem_)
132    {
133      this->inputSystem_->destroyInputObject(this->mouse_);
134      this->inputSystem_->destroyInputObject(this->keyboard_);
135      OIS::InputManager::destroyInputSystem(this->inputSystem_);
136    }
137
138    this->uninitialized_ = true;
139  }
140
141  /**
142    @brief Updates the InputHandler
143    @param dt Delta time
144  */
145  void InputHandler::tick(float dt)
146  {
147    // capture all the input. That calls the event handlers.
148    if (mouse_)
149      mouse_->capture();
150
151    if (keyboard_)
152      keyboard_->capture();
153  }
154
155  /**
156    @brief Adjusts the mouse window metrics.
157    This method has to be called every time the size of the window changes.
158    @param width The new width of the render window
159    @param height the new height of the render window
160  */
161  void InputHandler::setWindowExtents(int width, int height)
162  {
163    // Set mouse region (if window resizes, we should alter this to reflect as well)
164    const OIS::MouseState &mouseState = mouse_->getMouseState();
165    mouseState.width  = width;
166    mouseState.height = height;
167  }
168
169  /**
170    @brief Calls all the objects from classes that derive from InputEventListener.
171    @param evt The input event that occured.
172  */
173  inline void InputHandler::callListeners(orxonox::InputEvent &evt)
174  {
175    for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; )
176    {
177      if (it->bActive_)
178        (it++)->eventOccured(evt);
179      else
180        it++;
181    }
182  }
183
184  /**
185    @brief Event handler for the keyPressed Event.
186    @param e Event information
187  */
188  bool InputHandler::keyPressed(const OIS::KeyEvent &e)
189  {
190    callListeners(this->bindingsKeyPressed_[(int)e.key]);
191    return true;
192  }
193
194  /**
195    @brief Event handler for the keyReleased Event.
196    @param e Event information
197  */
198  bool InputHandler::keyReleased(const OIS::KeyEvent &e)
199  {
200    return true;
201  }
202
203  /**
204    @brief Event handler for the mouseMoved Event.
205    @param e Event information
206  */
207  bool InputHandler::mouseMoved(const OIS::MouseEvent &e)
208  {
209    return true;
210  }
211
212  /**
213    @brief Event handler for the mousePressed Event.
214    @param e Event information
215    @param id The ID of the mouse button
216  */
217  bool InputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
218  {
219    return true;
220  }
221
222  /**
223    @brief Event handler for the mouseReleased Event.
224    @param e Event information
225    @param id The ID of the mouse button
226  */
227  bool InputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
228  {
229    return true;
230  }
231
232}
Note: See TracBrowser for help on using the repository browser.