Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/InputManager.cc @ 1070

Last change on this file since 1070 was 1066, checked in by rgrieder, 16 years ago
  • TestConsole should work now
File size: 7.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30 @file
31 @brief Implementation of a little Input handler that distributes everything
32        coming from OIS.
33 */
34
35#include "InputManager.h"
36#include "CoreIncludes.h"
37#include "Debug.h"
38#include "InputEventListener.h"
39#include "InputHandler.h"
40#include "InputBuffer.h"
41
42namespace orxonox
43{
44  /**
45    @brief Constructor only resets the pointer values to 0.
46  */
47  InputManager::InputManager() :
48      inputSystem_(0), keyboard_(0), mouse_(0),
49      currentMode_(IM_UNINIT), setMode_(IM_UNINIT),
50      handlerGUI_(0), handlerBuffer_(0), handlerGame_(0)
51  {
52  }
53
54  /**
55    @brief Destructor only called at the end of the program
56  */
57  InputManager::~InputManager()
58  {
59    this->destroy();
60  }
61
62  /**
63    @brief The one instance of the InputManager is stored in this function.
64    @return A reference to the only instance of the InputManager
65  */
66  InputManager& InputManager::getSingleton()
67  {
68    static InputManager theOnlyInstance;
69    return theOnlyInstance;
70  }
71
72  /**
73    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
74           assigns the key bindings.
75    @param windowHnd The window handle of the render window
76    @param windowWidth The width of the render window
77    @param windowHeight The height of the render window
78  */
79  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
80  {
81    if (!this->inputSystem_)
82    {
83      // Setup basic variables
84      OIS::ParamList paramList;
85      std::ostringstream windowHndStr;
86
87      // Fill parameter list
88      windowHndStr << (unsigned int)windowHnd;
89      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
90
91#if defined OIS_LINUX_PLATFORM
92      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
93#endif
94
95      try
96      {
97        // Create inputsystem
98        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
99        COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl;
100
101        // create a keyboard. If none are available the exception is caught.
102        keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
103        COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl;
104
105        // create a mouse. If none are available the exception is caught.
106        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
107        COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl;
108
109        // Set mouse region
110        this->setWindowExtents(windowWidth, windowHeight);
111      }
112      catch (OIS::Exception ex)
113      {
114        // something went wrong with the initialisation
115        COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl;
116        this->inputSystem_ = 0;
117        return false;
118      }
119    }
120
121    // create the handlers
122    this->handlerGUI_ = new InputHandlerGUI();
123    this->handlerGame_ = new InputHandlerGame();
124    //this->handlerBuffer_ = new InputBuffer();
125    this->handlerGame_->loadBindings();
126
127    /*COUT(ORX_DEBUG) << "*** InputManager: Loading key bindings..." << std::endl;
128    // load the key bindings
129    InputEvent empty = {0, false, 0, 0, 0};
130    for (int i = 0; i < this->numberOfKeys_; i++)
131      this->bindingsKeyPressed_[i] = empty;
132
133    //assign 'abort' to the escape key
134    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
135    COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;*/
136
137    return true;
138  }
139
140  /**
141    @brief Destroys all the created input devices and handlers.
142  */
143  void InputManager::destroy()
144  {
145    COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl;
146    if (this->mouse_)
147      this->inputSystem_->destroyInputObject(mouse_);
148    if (this->keyboard_)
149      this->inputSystem_->destroyInputObject(keyboard_);
150    if (this->inputSystem_)
151      OIS::InputManager::destroyInputSystem(this->inputSystem_);
152
153    this->mouse_         = 0;
154    this->keyboard_      = 0;
155    this->inputSystem_   = 0;
156
157    if (this->handlerBuffer_)
158      delete this->handlerBuffer_;
159    if (this->handlerGame_)
160      delete this->handlerGame_;
161    if (this->handlerGUI_)
162      delete this->handlerGUI_;
163
164    this->handlerBuffer_ = 0;
165    this->handlerGame_   = 0;
166    this->handlerGUI_    = 0;
167
168    COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl;
169  }
170
171  /**
172    @brief Updates the InputManager
173    @param dt Delta time
174  */
175  void InputManager::tick(float dt)
176  {
177    // reset the game if it has changed
178    if (this->currentMode_ != this->setMode_)
179    {
180      switch (this->setMode_)
181      {
182      case IM_GUI:
183        this->mouse_->setEventCallback(this->handlerGUI_);
184        this->keyboard_->setEventCallback(this->handlerGUI_);
185        break;
186      case IM_INGAME:
187        this->mouse_->setEventCallback(this->handlerGame_);
188        this->keyboard_->setEventCallback(this->handlerGame_);
189        break;
190      case IM_KEYBOARD:
191        this->mouse_->setEventCallback(this->handlerGame_);
192        this->keyboard_->setEventCallback(this->handlerBuffer_);
193        break;
194      case IM_UNINIT:
195        this->mouse_->setEventCallback(0);
196        this->keyboard_->setEventCallback(0);
197        break;
198      }
199      this->currentMode_ = this->setMode_;
200    }
201
202    // capture all the input. That calls the event handlers.
203    if (mouse_)
204      mouse_->capture();
205
206    if (keyboard_)
207      keyboard_->capture();
208  }
209
210  /**
211    @brief Adjusts the mouse window metrics.
212    This method has to be called every time the size of the window changes.
213    @param width The new width of the render window
214    @param height the new height of the render window
215  */
216  void InputManager::setWindowExtents(int width, int height)
217  {
218    // Set mouse region (if window resizes, we should alter this to reflect as well)
219    const OIS::MouseState &mouseState = mouse_->getMouseState();
220    mouseState.width  = width;
221    mouseState.height = height;
222  }
223
224  /**
225    @brief Sets the input mode to either GUI, inGame or Buffer
226    @param mode The new input mode
227    @remark Only has an affect if the mode actually changes
228  */
229  void InputManager::setInputMode(InputMode mode)
230  {
231    this->setMode_ = mode;
232  }
233
234  /**
235    @brief Returns the current input handling method
236    @return The current input mode.
237  */
238  InputMode InputManager::getInputMode()
239  {
240    return this->currentMode_;
241  }
242
243  void InputManager::feedInputBuffer(InputBuffer* buffer)
244  {
245    this->handlerBuffer_ = buffer;
246  }
247
248
249}
Note: See TracBrowser for help on using the repository browser.