Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/orxonox/core/InputManager.cc @ 975

Last change on this file since 975 was 973, checked in by rgrieder, 17 years ago
  • not really done a lot, but svn create patch doesn't work with renamed files
File size: 7.3 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 *      ...
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 "core/CoreIncludes.h"
35#include "core/Debug.h"
36#include "InputEventListener.h"
37#include "InputManager.h"
38
39namespace orxonox
40{
41  /**
42    @brief The reference to the singleton
43  */
44  InputManager* InputManager::singletonRef_s = 0;
45
46  /**
47    @brief Constructor only resets the pointer values to 0.
48  */
49  InputManager::InputManager() :
50      mouse_(0), keyboard_(0), inputSystem_(0)
51  {
52  }
53
54  /**
55    @brief Destructor only called at the end of the program
56  */
57  InputManager::~InputManager()
58  {
59    this->destroyDevices();
60  }
61
62  /**
63    @brief The one instance of the InputManager is stored in this function.
64    @return The pointer to the only instance of the InputManager
65  */
66  InputManager *InputManager::getSingleton()
67  {
68    if (!singletonRef_s)
69      singletonRef_s = new InputManager();
70    return singletonRef_s;
71  }
72
73  /**
74    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
75           assigns the key bindings.
76    @param windowHnd The window handle of the render window
77    @param windowWidth The width of the render window
78    @param windowHeight The height of the render window
79  */
80  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
81  {
82    if (!this->inputSystem_)
83    {
84      // Setup basic variables
85      OIS::ParamList paramList;
86      std::ostringstream windowHndStr;
87
88      // Fill parameter list
89      windowHndStr << (unsigned int)windowHnd;
90      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
91
92#if defined OIS_LINUX_PLATFORM
93      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
94#endif
95
96      try
97      {
98        // Create inputsystem
99        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
100        COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl;
101
102        // create a keyboard. If none are available the exception is caught.
103        keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
104        keyboard_->setEventCallback(this);
105        COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl;
106
107        // create a mouse. If none are available the exception is caught.
108        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
109        mouse_->setEventCallback(this);
110        COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl;
111
112        // Set mouse region
113        this->setWindowExtents(windowWidth, windowHeight);
114      }
115      catch (OIS::Exception ex)
116      {
117        // something went wrong with the initialisation
118        COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl;
119        this->inputSystem_ = 0;
120        return false;
121      }
122    }
123
124    COUT(ORX_DEBUG) << "*** InputManager: Loading key bindings..." << std::endl;
125    // load the key bindings
126    InputEvent empty = {0, false, 0, 0, 0};
127    for (int i = 0; i < this->numberOfKeys_; i++)
128      this->bindingsKeyPressed_[i] = empty;
129
130    //assign 'abort' to the escape key
131    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
132    COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;
133
134    return true;
135  }
136
137  /**
138    @brief Destroys all the created input devices.
139  */
140  void InputManager::destroyDevices()
141  {
142    COUT(ORX_DEBUG) << "*** InputManager: Destroying InputManager..." << std::endl;
143    if (this->mouse_)
144      this->inputSystem_->destroyInputObject(mouse_);
145    if (this->keyboard_)
146      this->inputSystem_->destroyInputObject(keyboard_);
147    if (this->inputSystem_)
148      OIS::InputManager::destroyInputSystem(this->inputSystem_);
149
150    this->mouse_         = 0;
151    this->keyboard_      = 0;
152    this->inputSystem_   = 0;
153    COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl;
154  }
155
156  /**
157    @brief Destroys the singleton.
158  */
159  void InputManager::destroySingleton()
160  {
161    if (singletonRef_s)
162      delete singletonRef_s;
163    singletonRef_s = 0;
164  }
165
166  /**
167    @brief Updates the InputManager
168    @param dt Delta time
169  */
170  void InputManager::tick(float dt)
171  {
172    //this->mouse_->setEventCallback(this);
173    // capture all the input. That calls the event handlers.
174    if (mouse_)
175      mouse_->capture();
176
177    if (keyboard_)
178      keyboard_->capture();
179  }
180
181  /**
182    @brief Adjusts the mouse window metrics.
183    This method has to be called every time the size of the window changes.
184    @param width The new width of the render window
185    @param height the new height of the render window
186  */
187  void InputManager::setWindowExtents(int width, int height)
188  {
189    // Set mouse region (if window resizes, we should alter this to reflect as well)
190    const OIS::MouseState &mouseState = mouse_->getMouseState();
191    mouseState.width  = width;
192    mouseState.height = height;
193  }
194
195  /**
196    @brief Calls all the objects from classes that derive from InputEventListener.
197    @param evt The input event that occured.
198  */
199  inline void InputManager::callListeners(orxonox::InputEvent &evt)
200  {
201    for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; )
202    {
203      if (it->bActive_)
204        (it++)->eventOccured(evt);
205      else
206        it++;
207    }
208  }
209
210  /**
211    @brief Event handler for the keyPressed Event.
212    @param e Event information
213  */
214  bool InputManager::keyPressed(const OIS::KeyEvent &e)
215  {
216    callListeners(this->bindingsKeyPressed_[(int)e.key]);
217    return true;
218  }
219
220  /**
221    @brief Event handler for the keyReleased Event.
222    @param e Event information
223  */
224  bool InputManager::keyReleased(const OIS::KeyEvent &e)
225  {
226    return true;
227  }
228
229  /**
230    @brief Event handler for the mouseMoved Event.
231    @param e Event information
232  */
233  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
234  {
235    return true;
236  }
237
238  /**
239    @brief Event handler for the mousePressed Event.
240    @param e Event information
241    @param id The ID of the mouse button
242  */
243  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
244  {
245    COUT(1) << "asdf" << std::endl;
246    return true;
247  }
248
249  /**
250    @brief Event handler for the mouseReleased Event.
251    @param e Event information
252    @param id The ID of the mouse button
253  */
254  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
255  {
256    return true;
257  }
258
259}
Note: See TracBrowser for help on using the repository browser.