Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/InputManager.cc @ 1024

Last change on this file since 1024 was 1024, checked in by rgrieder, 16 years ago

merged input back into trunk
note: I have created an asylum with obsolete code, please check the files

File size: 7.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 *      ...
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 "InputHandler.h"
38#include "InputManager.h"
39
40namespace orxonox
41{
42  /**
43    @brief The reference to the singleton
44  */
45  InputManager* InputManager::singletonRef_s = 0;
46
47  /**
48    @brief Constructor only resets the pointer values to 0.
49  */
50  InputManager::InputManager() :
51      mouse_(0), keyboard_(0), inputSystem_(0),
52      currentMode_(IM_UNINIT), setMode_(IM_UNINIT),
53      handlerGUI_(0), handlerGame_(0), handlerBuffer_(0)
54  {
55  }
56
57  /**
58    @brief Destructor only called at the end of the program
59  */
60  InputManager::~InputManager()
61  {
62    this->destroyDevices();
63  }
64
65  /**
66    @brief The one instance of the InputManager is stored in this function.
67    @return The pointer to the only instance of the InputManager
68  */
69  InputManager *InputManager::getSingleton()
70  {
71    if (!singletonRef_s)
72      singletonRef_s = new InputManager();
73    return singletonRef_s;
74  }
75
76  /**
77    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
78           assigns the key bindings.
79    @param windowHnd The window handle of the render window
80    @param windowWidth The width of the render window
81    @param windowHeight The height of the render window
82  */
83  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
84  {
85    if (!this->inputSystem_)
86    {
87      // Setup basic variables
88      OIS::ParamList paramList;
89      std::ostringstream windowHndStr;
90
91      // Fill parameter list
92      windowHndStr << (unsigned int)windowHnd;
93      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
94
95#if defined OIS_LINUX_PLATFORM
96      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
97#endif
98
99      try
100      {
101        // Create inputsystem
102        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
103        COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl;
104
105        // create a keyboard. If none are available the exception is caught.
106        keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
107        COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl;
108
109        // create a mouse. If none are available the exception is caught.
110        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
111        COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl;
112
113        // Set mouse region
114        this->setWindowExtents(windowWidth, windowHeight);
115      }
116      catch (OIS::Exception ex)
117      {
118        // something went wrong with the initialisation
119        COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl;
120        this->inputSystem_ = 0;
121        return false;
122      }
123    }
124
125    // create the handlers
126    this->handlerGUI_ = new InputHandlerGUI();
127    this->handlerGame_ = new InputHandlerGame();
128    this->handlerGame_->loadBindings();
129
130    /*COUT(ORX_DEBUG) << "*** InputManager: Loading key bindings..." << std::endl;
131    // load the key bindings
132    InputEvent empty = {0, false, 0, 0, 0};
133    for (int i = 0; i < this->numberOfKeys_; i++)
134      this->bindingsKeyPressed_[i] = empty;
135
136    //assign 'abort' to the escape key
137    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
138    COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;*/
139
140    return true;
141  }
142
143  /**
144    @brief Destroys all the created input devices.
145  */
146  void InputManager::destroyDevices()
147  {
148    COUT(ORX_DEBUG) << "*** InputManager: Destroying InputManager..." << std::endl;
149    if (this->mouse_)
150      this->inputSystem_->destroyInputObject(mouse_);
151    if (this->keyboard_)
152      this->inputSystem_->destroyInputObject(keyboard_);
153    if (this->inputSystem_)
154      OIS::InputManager::destroyInputSystem(this->inputSystem_);
155
156    this->mouse_         = 0;
157    this->keyboard_      = 0;
158    this->inputSystem_   = 0;
159    COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl;
160  }
161
162  /**
163    @brief Destroys the singleton.
164  */
165  void InputManager::destroySingleton()
166  {
167    if (singletonRef_s)
168      delete singletonRef_s;
169    singletonRef_s = 0;
170  }
171
172  /**
173    @brief Updates the InputManager
174    @param dt Delta time
175  */
176  void InputManager::tick(float dt)
177  {
178    // reset the game if it has changed
179    if (this->currentMode_ != this->setMode_)
180    {
181      switch (this->setMode_)
182      {
183      case IM_GUI:
184        this->mouse_->setEventCallback(this->handlerGUI_);
185        this->keyboard_->setEventCallback(this->handlerGUI_);
186        break;
187      case IM_INGAME:
188        this->mouse_->setEventCallback(this->handlerGame_);
189        this->keyboard_->setEventCallback(this->handlerGame_);
190        break;
191      case IM_KEYBOARD:
192        this->mouse_->setEventCallback(this->handlerGame_);
193        this->keyboard_->setEventCallback(this->handlerBuffer_);
194        break;
195      case IM_UNINIT:
196        this->mouse_->setEventCallback(0);
197        this->keyboard_->setEventCallback(0);
198        break;
199      }
200      this->currentMode_ = this->setMode_;
201    }
202
203    // capture all the input. That calls the event handlers.
204    if (mouse_)
205      mouse_->capture();
206
207    if (keyboard_)
208      keyboard_->capture();
209  }
210
211  /**
212    @brief Adjusts the mouse window metrics.
213    This method has to be called every time the size of the window changes.
214    @param width The new width of the render window
215    @param height the new height of the render window
216  */
217  void InputManager::setWindowExtents(int width, int height)
218  {
219    // Set mouse region (if window resizes, we should alter this to reflect as well)
220    const OIS::MouseState &mouseState = mouse_->getMouseState();
221    mouseState.width  = width;
222    mouseState.height = height;
223  }
224
225  /**
226    @brief Sets the input mode to either GUI, inGame or Buffer
227    @param mode The new input mode
228    @remark Only has an affect if the mode actually changes
229  */
230  void InputManager::setInputMode(InputMode mode)
231  {
232    this->setMode_ = mode;
233  }
234
235  /**
236    @brief Returns the current input handling method
237    @return The current input mode.
238  */
239  InputMode InputManager::getInputMode()
240  {
241    return this->currentMode_;
242  }
243
244}
Note: See TracBrowser for help on using the repository browser.