Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1189 was 1182, checked in by rgrieder, 17 years ago
  • InputManager works so far, but has to be largely extended
File size: 11.2 KB
RevLine 
[918]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[918]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:
[934]25 *      ...
[918]26 *
27 */
[973]28
[918]29/**
30 @file
31 @brief Implementation of a little Input handler that distributes everything
32        coming from OIS.
33 */
34
[1062]35#include "InputManager.h"
36#include "CoreIncludes.h"
37#include "Debug.h"
[1066]38#include "InputBuffer.h"
[1084]39#include "ConsoleCommand.h"
[1182]40#include "util/Convert.h"
[918]41
42namespace orxonox
43{
[919]44  /**
45    @brief Constructor only resets the pointer values to 0.
46  */
[973]47  InputManager::InputManager() :
[1034]48      inputSystem_(0), keyboard_(0), mouse_(0),
[1182]49      state_(IS_UNINIT), stateRequest_(IS_UNINIT)
[918]50  {
[1089]51    RegisterObject(InputManager);
[918]52  }
53
[919]54  /**
[1182]55    @brief The one instance of the InputManager is stored in this function.
56    @return A reference to the only instance of the InputManager
[929]57  */
[1182]58  InputManager& InputManager::_getSingleton()
[929]59  {
[1182]60    static InputManager theOnlyInstance;
61    return theOnlyInstance;
[929]62  }
63
64  /**
[1182]65    @brief Destructor only called at the end of the program
[919]66  */
[1182]67  InputManager::~InputManager()
[919]68  {
[1182]69    this->_destroy();
[919]70  }
71
72  /**
[922]73    @brief Creates the OIS::InputMananger, the keyboard and the mouse and
74           assigns the key bindings.
[919]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  */
[1182]79  bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight)
[919]80  {
[1182]81    if (!inputSystem_)
[918]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
[1182]91//#if defined OIS_LINUX_PLATFORM
92//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
93//#endif
[928]94
[934]95      try
[918]96      {
[934]97        // Create inputsystem
98        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
[973]99        COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl;
[918]100
[940]101        // create a keyboard. If none are available the exception is caught.
102        keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
[973]103        COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl;
[918]104
[1182]105        //TODO: check for already pressed keys
106
[940]107        // create a mouse. If none are available the exception is caught.
108        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
[973]109        COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl;
[934]110
[940]111        // Set mouse region
[1182]112        setWindowExtents(windowWidth, windowHeight);
113
114        this->state_ = IS_NONE;
[918]115      }
[934]116      catch (OIS::Exception ex)
117      {
118        // something went wrong with the initialisation
[944]119        COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl;
[1182]120        inputSystem_ = 0;
[934]121        return false;
122      }
[918]123    }
[922]124
[1182]125    keyboard_->setEventCallback(this);
126    mouse_->setEventCallback(this);
[1022]127
[1182]128    addKeyListener(new InputBuffer(), "buffer");
[922]129
[1182]130    _loadBindings();
[934]131
[1182]132    COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;
133
[934]134    return true;
[918]135  }
136
[1182]137  void InputManager::_loadBindings()
138  {
139    for (int i = 0; i < numberOfKeys_s; i++)
140    {
141      // simply write the key number (i) in the string
142      this->bindingsKeyPress_[i] = "";
143      this->bindingsKeyRelease_[i] = "";
144    }
145    this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole";
146    this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
147    this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
148  }
149
[919]150  /**
[1035]151    @brief Destroys all the created input devices and handlers.
[928]152  */
[1182]153  void InputManager::_destroy()
[928]154  {
[1035]155    COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl;
[1182]156    if (mouse_)
157      inputSystem_->destroyInputObject(mouse_);
158    if (keyboard_)
159      inputSystem_->destroyInputObject(keyboard_);
160    if (inputSystem_)
161      OIS::InputManager::destroyInputSystem(inputSystem_);
[928]162
[1182]163    mouse_         = 0;
164    keyboard_      = 0;
165    inputSystem_   = 0;
[1035]166
[1182]167    OIS::KeyListener* buffer = keyListeners_["buffer"];
168    if (buffer)
169    {
170      this->removeKeyListener("buffer");
171      delete buffer;
172      buffer = 0;
173    }
[1035]174
[973]175    COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl;
[928]176  }
177
178  /**
[973]179    @brief Updates the InputManager
[919]180    @param dt Delta time
181  */
[973]182  void InputManager::tick(float dt)
[918]183  {
[1182]184    if (state_ == IS_UNINIT)
185      return;
186
[1022]187    // reset the game if it has changed
[1182]188    if (state_ != stateRequest_)
[1022]189    {
[1182]190      if (stateRequest_ != IS_CUSTOM)
191        _setDefaultState();
192
193      switch (stateRequest_)
[1022]194      {
[1182]195      case IS_NORMAL:
[1022]196        break;
[1182]197      case IS_GUI:
198        //FIXME: do stuff
[1022]199        break;
[1182]200      case IS_CONSOLE:
201        if (this->keyListeners_.find("buffer") != this->keyListeners_.end())
202          this->activeKeyListeners_.push_back(this->keyListeners_["buffer"]);
203        this->bDefaultKeyInput = false;
[1022]204        break;
[1182]205      case IS_CUSTOM:
206        // don't do anything
[1022]207        break;
208      }
[1182]209      state_ = stateRequest_;
[1022]210    }
211
[918]212    // capture all the input. That calls the event handlers.
213    if (mouse_)
214      mouse_->capture();
215    if (keyboard_)
216      keyboard_->capture();
217  }
218
[1182]219  void InputManager::_setDefaultState()
220  {
221    this->activeJoyStickListeners_.clear();
222    this->activeKeyListeners_.clear();
223    this->activeMouseListeners_.clear();
224    this->bDefaultKeyInput      = true;
225    this->bDefaultMouseInput    = true;
226    this->bDefaultJoyStickInput = true;
227  }
228
229
[919]230  /**
[1182]231    @brief Event handler for the keyPressed Event.
232    @param e Event information
233  */
234  bool InputManager::keyPressed(const OIS::KeyEvent &e)
235  {
236    this->keysDown_.push_back(e.key);
237
238    if (this->bDefaultKeyInput)
239    {
240      // find the appropriate key binding
241      std::string cmdStr = bindingsKeyPress_[int(e.key)];
242      if (cmdStr != "")
243      {
244        CommandExecutor::execute(cmdStr);
245        COUT(3) << "Executing command: " << cmdStr << std::endl;
246      }
247    }
248    else
249    {
250      for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++)
251        (*it)->keyPressed(e);
252    }
253    return true;
254  }
255
256  /**
257    @brief Event handler for the keyReleased Event.
258    @param e Event information
259  */
260  bool InputManager::keyReleased(const OIS::KeyEvent &e)
261  {
262    // remove the key from the keysDown_ list
263    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
264    {
265      if (*it == e.key)
266      {
267        keysDown_.erase(it);
268        break;
269      }
270    }
271
272    if (this->bDefaultKeyInput)
273    {
274      // find the appropriate key binding
275      std::string cmdStr = bindingsKeyRelease_[int(e.key)];
276      if (cmdStr != "")
277      {
278        CommandExecutor::execute(cmdStr);
279        COUT(3) << "Executing command: " << cmdStr << std::endl;
280      }
281    }
282    else
283    {
284      for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++)
285        (*it)->keyReleased(e);
286    }
287    return true;
288  }
289
290  /**
291    @brief Event handler for the mouseMoved Event.
292    @param e Event information
293  */
294  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
295  {
296    return true;
297  }
298
299  /**
300    @brief Event handler for the mousePressed Event.
301    @param e Event information
302    @param id The ID of the mouse button
303  */
304  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
305  {
306    return true;
307  }
308
309  /**
310    @brief Event handler for the mouseReleased Event.
311    @param e Event information
312    @param id The ID of the mouse button
313  */
314  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
315  {
316    return true;
317  }
318
319  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
320  {
321    return true;
322  }
323
324  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
325  {
326    return true;
327  }
328
329  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
330  {
331    return true;
332  }
333
334  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
335  {
336    return true;
337  }
338
339  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
340  {
341    return true;
342  }
343
344
345
346  /**
[919]347    @brief Adjusts the mouse window metrics.
348    This method has to be called every time the size of the window changes.
349    @param width The new width of the render window
350    @param height the new height of the render window
351  */
[973]352  void InputManager::setWindowExtents(int width, int height)
[918]353  {
354    // Set mouse region (if window resizes, we should alter this to reflect as well)
[1182]355    const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
[918]356    mouseState.width  = width;
357    mouseState.height = height;
358  }
359
[919]360  /**
[1022]361    @brief Sets the input mode to either GUI, inGame or Buffer
362    @param mode The new input mode
363    @remark Only has an affect if the mode actually changes
[922]364  */
[1182]365  void InputManager::setInputState(const InputState state)
[922]366  {
[1182]367    _getSingleton().stateRequest_ = state;
[922]368  }
369
370  /**
[1022]371    @brief Returns the current input handling method
372    @return The current input mode.
[919]373  */
[1182]374  InputManager::InputState InputManager::getInputState()
[918]375  {
[1182]376    return _getSingleton().state_;
[918]377  }
378
[1182]379  void InputManager::destroy()
[1066]380  {
[1182]381    _getSingleton()._destroy();
[1066]382  }
383
[1182]384  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
385  {
386    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight);
387  }
[1066]388
[1182]389  bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name)
390  {
391    if (_getSingleton().keyListeners_.find(name) == _getSingleton().keyListeners_.end())
392    {
393      _getSingleton().keyListeners_[name] = listener;
394      return true;
395    }
396    else
397      return false;
398  }
399
400  bool InputManager::removeKeyListener(const std::string &name)
401  {
402    std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name);
403    if (it != _getSingleton().keyListeners_.end())
404    {
405      _getSingleton().keyListeners_.erase(it);
406      return true;
407    }
408    else
409      return false;
410  }
411
412  OIS::KeyListener* InputManager::getKeyListener(const std::string& name)
413  {
414    std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name);
415    if (it != _getSingleton().keyListeners_.end())
416    {
417      return (*it).second;
418    }
419    else
420      return 0;
421  }
422
[918]423}
Note: See TracBrowser for help on using the repository browser.