Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1304 was 1293, checked in by scheusso, 17 years ago

merged changes from input & camera & network branch into trunk

File size: 37.8 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/**
[1219]30  @file
31  @brief Implementation of the InputManager that captures all the input from OIS
[1293]32         and redirects it to handlers.
[918]33 */
34
[1062]35#include "InputManager.h"
36#include "CoreIncludes.h"
37#include "Debug.h"
[1219]38#include "InputBuffer.h"
[1022]39#include "InputHandler.h"
[918]40
41namespace orxonox
42{
[1219]43  // ###############################
44  // ###    Internal Methods     ###
45  // ###############################
46  // ###############################
[1084]47
[919]48  /**
[1219]49    @brief Constructor only sets member fields to initial zero values
50           and registers the class in the class hierarchy.
[919]51  */
[973]52  InputManager::InputManager() :
[1034]53      inputSystem_(0), keyboard_(0), mouse_(0),
[1293]54      joySticksSize_(0),
55      state_(IS_UNINIT), stateRequest_(IS_UNINIT),
56      keyboardModifiers_(0)
[918]57  {
[1089]58    RegisterObject(InputManager);
[929]59  }
60
61  /**
[973]62    @brief The one instance of the InputManager is stored in this function.
[1035]63    @return A reference to the only instance of the InputManager
[919]64  */
[1219]65  InputManager& InputManager::_getSingleton()
[919]66  {
[1035]67    static InputManager theOnlyInstance;
68    return theOnlyInstance;
[919]69  }
70
71  /**
[1219]72    @brief Destructor only called at the end of the program, after main.
73  */
74  InputManager::~InputManager()
75  {
76    _destroy();
77  }
78
79  /**
80    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
81           the joysticks and assigns the key bindings.
[919]82    @param windowHnd The window handle of the render window
83    @param windowWidth The width of the render window
84    @param windowHeight The height of the render window
85  */
[1293]86  bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight,
87        bool createKeyboard, bool createMouse, bool createJoySticks)
[919]88  {
[1219]89    if (state_ == IS_UNINIT)
[918]90    {
[1293]91      CCOUT(3) << "Initialising Input System..." << std::endl;
[1219]92      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
93
[918]94      OIS::ParamList paramList;
95      std::ostringstream windowHndStr;
96
97      // Fill parameter list
98      windowHndStr << (unsigned int)windowHnd;
99      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
100
[1219]101//#if defined OIS_LINUX_PLATFORM
102//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
103//#endif
[928]104
[934]105      try
[918]106      {
[934]107        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
[1219]108        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
[918]109      }
[934]110      catch (OIS::Exception ex)
111      {
[1219]112        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
113            << "OIS message: \"" << ex.eText << "\"" << std::endl;
114        inputSystem_ = 0;
[934]115        return false;
116      }
[1219]117
118      if (createKeyboard)
119        _initialiseKeyboard();
120
121      if (createMouse)
122        _initialiseMouse();
123
124      if (createJoySticks)
125        _initialiseJoySticks();
126
127      // Set mouse/joystick region
128      setWindowExtents(windowWidth, windowHeight);
129
130      state_ = IS_NONE;
131      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
[918]132    }
[1219]133    else
134    {
135      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
136    }
[922]137
[1219]138    // InputManager holds the input buffer --> create one and add it.
139    addKeyHandler(new InputBuffer(), "buffer");
[1022]140
[1219]141    KeyBinder* binder = new KeyBinder();
142    binder->loadBindings();
143    addKeyHandler(binder, "keybinder");
144    addMouseHandler(binder, "keybinder");
[1293]145    addJoyStickHandler(binder, "keybinder");
[922]146
[1219]147    // Read all the key bindings and assign them
148    //if (!_loadBindings())
149    //  return false;
[934]150
[1219]151    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
[934]152    return true;
[918]153  }
154
[919]155  /**
[1219]156    @brief Creates a keyboard and sets the event handler.
157    @return False if keyboard stays uninitialised, true otherwise.
[928]158  */
[1219]159  bool InputManager::_initialiseKeyboard()
[928]160  {
[1219]161    if (keyboard_ != 0)
162    {
163      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
164      return true;
165    }
166    try
167    {
168      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
169      {
170        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
171        // register our listener in OIS.
172        keyboard_->setEventCallback(this);
173        // note: OIS will not detect keys that have already been down when the keyboard was created.
174        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
175        return true;
176      }
177      else
178      {
179        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
180        return false;
181      }
182    }
183    catch (OIS::Exception ex)
184    {
185      // TODO: Test this output regarding formatting
186      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
187          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
188      keyboard_ = 0;
189      return false;
190    }
191  }
[928]192
[1219]193  /**
194    @brief Creates a mouse and sets the event handler.
195    @return False if mouse stays uninitialised, true otherwise.
196  */
197  bool InputManager::_initialiseMouse()
198  {
199    if (mouse_ != 0)
200    {
201      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
202      return true;
203    }
204    try
205    {
206      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
207      {
208        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
209        // register our listener in OIS.
210        mouse_->setEventCallback(this);
211        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
212        return true;
213      }
214      else
215      {
216        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
217        return false;
218      }
219    }
220    catch (OIS::Exception ex)
221    {
222      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
223          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
224      mouse_ = 0;
225      return false;
226    }
227  }
[1035]228
[1219]229  /**
230    @brief Creates all joy sticks and sets the event handler.
231    @return False joy stick stay uninitialised, true otherwise.
232  */
233  bool InputManager::_initialiseJoySticks()
234  {
[1293]235    if (joySticksSize_ > 0)
[1219]236    {
237      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
238      return true;
239    }
240    bool success = false;
241    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
242    {
243      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
244      {
245        try
246        {
247          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
248          joySticks_.push_back(stig);
249          // register our listener in OIS.
250          stig->setEventCallback(this);
251          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
252          success = true;
253        }
254        catch (OIS::Exception ex)
255        {
256          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
257              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
258        }
259      }
260    }
261    else
262    {
263      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
264      return false;
265    }
[1293]266    joySticksSize_ = joySticks_.size();
267    activeJoyStickHandlers_.resize(joySticksSize_);
268    joyStickButtonsDown_.resize(joySticksSize_);
[1219]269    return success;
270  }
[1035]271
[1219]272  /**
273    @brief Destroys all the created input devices and sets the InputManager to construction state.
274  */
275  void InputManager::_destroy()
276  {
277    if (state_ != IS_UNINIT)
278    {
[1293]279      CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
280
[1219]281      if (keyHandlers_.find("buffer") != keyHandlers_.end())
282        delete keyHandlers_["buffer"];
283
284      if (keyHandlers_.find("keybinder") != keyHandlers_.end())
285        delete keyHandlers_["keybinder"];
286
287      keyHandlers_.clear();
288      mouseHandlers_.clear();
289      joyStickHandlers_.clear();
290
291      _destroyKeyboard();
292      _destroyMouse();
293      _destroyJoySticks();
294
295      // inputSystem_ can never be 0, or else the code is mistaken
296      OIS::InputManager::destroyInputSystem(inputSystem_);
297      inputSystem_ = 0;
298
299      state_ = IS_UNINIT;
[1293]300      CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
[1219]301    }
[928]302  }
303
304  /**
[1219]305    @brief Destroys the keyboard and sets it to 0.
306  */
307  void InputManager::_destroyKeyboard()
308  {
309    if (keyboard_)
310      // inputSystem_ can never be 0, or else the code is mistaken
311      inputSystem_->destroyInputObject(keyboard_);
312    keyboard_ = 0;
313    activeKeyHandlers_.clear();
314    keysDown_.clear();
315    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
316  }
317
318  /**
319    @brief Destroys the mouse and sets it to 0.
320  */
321  void InputManager::_destroyMouse()
322  {
323    if (mouse_)
324      // inputSystem_ can never be 0, or else the code is mistaken
325      inputSystem_->destroyInputObject(mouse_);
326    mouse_ = 0;
327    activeMouseHandlers_.clear();
328    mouseButtonsDown_.clear();
329    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
330  }
331
332  /**
333    @brief Destroys all the joy sticks and resizes the lists to 0.
334  */
335  void InputManager::_destroyJoySticks()
336  {
[1293]337    if (joySticksSize_ > 0)
[1219]338    {
339      // note: inputSystem_ can never be 0, or else the code is mistaken
[1293]340      for (unsigned int i = 0; i < joySticksSize_; i++)
[1219]341        if (joySticks_[i] != 0)
342          inputSystem_->destroyInputObject(joySticks_[i]);
343
344      joySticks_.clear();
[1293]345      joySticksSize_ = 0;
[1219]346      activeJoyStickHandlers_.clear();
347      joyStickButtonsDown_.clear();
348    }
349    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
350  }
351
352
353  // #################################
354  // ### Private Interface Methods ###
355  // #################################
356  // #################################
357
358  /**
359    @brief Updates the InputManager. Tick is called by Orxonox.
[919]360    @param dt Delta time
361  */
[973]362  void InputManager::tick(float dt)
[918]363  {
[1219]364    if (state_ == IS_UNINIT)
365      return;
366
[1022]367    // reset the game if it has changed
[1219]368    if (state_ != stateRequest_)
[1022]369    {
[1219]370      if (stateRequest_ != IS_CUSTOM)
[1022]371      {
[1219]372        activeKeyHandlers_.clear();
373        activeMouseHandlers_.clear();
[1293]374        for (unsigned int i = 0; i < joySticksSize_; i++)
375          activeJoyStickHandlers_[i].clear();
[1219]376
377        switch (stateRequest_)
378        {
379        case IS_NORMAL:
380          // normal play mode
381          // note: we assume that the handlers exist since otherwise, something's wrong anyway.
382          activeKeyHandlers_.push_back(keyHandlers_["keybinder"]);
383          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
[1293]384          if (getMouseHandler("SpaceShip"))
385            activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
386          for (unsigned int i = 0; i < joySticksSize_; i++)
387            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
[1219]388          break;
389
390        case IS_GUI:
391          // FIXME: do stuff
392          break;
393
394        case IS_CONSOLE:
395          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
[1293]396          if (getMouseHandler("SpaceShip"))
397            activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
398          for (unsigned int i = 0; i < joySticksSize_; i++)
399            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
[1219]400
401          activeKeyHandlers_.push_back(keyHandlers_["buffer"]);
402          break;
403
404        default:
405          break;
406        }
407        state_ = stateRequest_;
408      }
409    }
410
411    // Capture all the input. This calls the event handlers in InputManager.
412    if (mouse_)
413      mouse_->capture();
414    if (keyboard_)
415      keyboard_->capture();
[1293]416    for (unsigned  int i = 0; i < joySticksSize_; i++)
417      joySticks_[i]->capture();
[1219]418
419
420    // call all the handlers for the held key events
[1293]421    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
422      for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
423        activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_));
[1219]424
425    // call all the handlers for the held mouse button events
[1293]426    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
427      for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
428        activeMouseHandlers_[iHandler]->mouseButtonHeld(mouse_->getMouseState(), mouseButtonsDown_[iButton]);
[1219]429
430    // call all the handlers for the held joy stick button events
[1293]431    for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
432      for (unsigned int iButton   = 0; iButton   < joyStickButtonsDown_[iJoyStick].size(); iButton++)
433        for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
434          activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld(
435              JoyStickState(joySticks_[iJoyStick]->getJoyStickState(), iJoyStick), joyStickButtonsDown_[iJoyStick][iButton]);
[1219]436  }
437
438  // ###### Key Events ######
439
440  /**
441    @brief Event handler for the keyPressed Event.
442    @param e Event information
443  */
444  bool InputManager::keyPressed(const OIS::KeyEvent &e)
445  {
446    // check whether the key already is in the list (can happen when focus was lost)
[1293]447    unsigned int iKey = 0;
448    while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key)
449      iKey++;
450    if (iKey == keysDown_.size())
451      keysDown_.push_back(Key(e));
[1219]452
[1293]453    // update modifiers
454    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
455      keyboardModifiers_ |= KeyboardModifier::Alt;   // alt key
456    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
457      keyboardModifiers_ |= KeyboardModifier::Ctrl;  // ctrl key
458    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
459      keyboardModifiers_ |= KeyboardModifier::Shift; // shift key
460
[1219]461    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
[1293]462      activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
[1219]463
464    return true;
465  }
466
467  /**
468    @brief Event handler for the keyReleased Event.
469    @param e Event information
470  */
471  bool InputManager::keyReleased(const OIS::KeyEvent &e)
472  {
473    // remove the key from the keysDown_ list
[1293]474    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
[1219]475    {
[1293]476      if (keysDown_[iKey].key == (KeyCode::Enum)e.key)
[1219]477      {
[1293]478        keysDown_.erase(keysDown_.begin() + iKey);
[1022]479        break;
[1219]480      }
481    }
482
[1293]483    // update modifiers
484    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
485      keyboardModifiers_ &= ~KeyboardModifier::Alt;   // alt key
486    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
487      keyboardModifiers_ &= ~KeyboardModifier::Ctrl;  // ctrl key
488    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
489      keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key
490
[1219]491    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
[1293]492      activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
[1219]493
494    return true;
495  }
496
497
498  // ###### Mouse Events ######
499
500  /**
501    @brief Event handler for the mouseMoved Event.
502    @param e Event information
503  */
504  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
505  {
[1293]506    // check for actual moved event
507    if (e.state.X.rel != 0 || e.state.Y.rel != 0)
508    {
509      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
510        activeMouseHandlers_[i]->mouseMoved(e.state);
511    }
[1219]512
[1293]513    // check for mouse scrolled event
514    if (e.state.Z.rel != 0)
515    {
516      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
517        activeMouseHandlers_[i]->mouseScrolled(e.state);
518    }
519
[1219]520    return true;
521  }
522
523  /**
524    @brief Event handler for the mousePressed Event.
525    @param e Event information
526    @param id The ID of the mouse button
527  */
528  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
529  {
530    // check whether the button already is in the list (can happen when focus was lost)
[1293]531    unsigned int iButton = 0;
532    while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id)
533      iButton++;
534    if (iButton == mouseButtonsDown_.size())
535      mouseButtonsDown_.push_back((MouseButton::Enum)id);
[1219]536
537    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
[1293]538      activeMouseHandlers_[i]->mouseButtonPressed(e.state, (MouseButton::Enum)id);
[1219]539
540    return true;
541  }
542
543  /**
544    @brief Event handler for the mouseReleased Event.
545    @param e Event information
546    @param id The ID of the mouse button
547  */
548  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
549  {
550    // remove the button from the keysDown_ list
[1293]551    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
[1219]552    {
[1293]553      if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id)
[1219]554      {
[1293]555        mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton);
[1022]556        break;
557      }
558    }
559
[1219]560    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
[1293]561      activeMouseHandlers_[i]->mouseButtonReleased(e.state, (MouseButton::Enum)id);
[918]562
[1219]563    return true;
[918]564  }
565
[1219]566
567  // ###### Joy Stick Events ######
568
569  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
570  {
[1293]571    // use the device to identify which one called the method
[1219]572    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
[1293]573    unsigned int iJoyStick = 0;
574    while (joySticks_[iJoyStick] != joyStick)
575      iJoyStick++;
[1219]576
577    // check whether the button already is in the list (can happen when focus was lost)
[1293]578    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
579    unsigned int iButton = 0;
580    while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
581      iButton++;
582    if (iButton == buttonsDown.size())
583      buttonsDown.push_back(button);
[1219]584
[1293]585    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
586      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(JoyStickState(arg.state, iJoyStick), button);
[1219]587
588    return true;
589  }
590
591  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
592  {
[1293]593    // use the device to identify which one called the method
[1219]594    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
[1293]595    unsigned int iJoyStick = 0;
596    while (joySticks_[iJoyStick] != joyStick)
597      iJoyStick++;
[1219]598
599    // remove the button from the joyStickButtonsDown_ list
[1293]600    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
601    for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
[1219]602    {
[1293]603      if (buttonsDown[iButton] == button)
[1219]604      {
[1293]605        buttonsDown.erase(buttonsDown.begin() + iButton);
[1219]606        break;
607      }
608    }
609
[1293]610    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
611      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(JoyStickState(arg.state, iJoyStick), button);
[1219]612
613    return true;
614  }
615
616  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
617  {
[1293]618    // use the device to identify which one called the method
[1219]619    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
[1293]620    unsigned int iJoyStick = 0;
621    while (joySticks_[iJoyStick] != joyStick)
622      iJoyStick++;
[1219]623
[1293]624    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
625      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(JoyStickState(arg.state, iJoyStick), axis);
626
[1219]627    return true;
628  }
629
630  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
631  {
[1293]632    // use the device to identify which one called the method
[1219]633    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
[1293]634    unsigned int iJoyStick = 0;
635    while (joySticks_[iJoyStick] != joyStick)
636      iJoyStick++;
637
638    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
639      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickSliderMoved(JoyStickState(arg.state, iJoyStick), id);
[1219]640
641    return true;
642  }
643
644  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
645  {
[1293]646    // use the device to identify which one called the method
[1219]647    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
[1293]648    unsigned int iJoyStick = 0;
649    while (joySticks_[iJoyStick] != joyStick)
650      iJoyStick++;
651
652    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
653      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickPovMoved(JoyStickState(arg.state, iJoyStick), id);
[1219]654
655    return true;
656  }
657
658  bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
659  {
[1293]660    // use the device to identify which one called the method
[1219]661    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
[1293]662    unsigned int iJoyStick = 0;
663    while (joySticks_[iJoyStick] != joyStick)
664      iJoyStick++;
665
666    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
667      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
[1219]668
669    return true;
670  }
671
672
673  // ################################
674  // ### Static Interface Methods ###
675  // ################################
676  // ################################
677
[1293]678  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
679    bool createKeyboard, bool createMouse, bool createJoySticks)
[1219]680  {
681    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
682          createKeyboard, createMouse, createJoySticks);
683  }
684
685  bool InputManager::initialiseKeyboard()
686  {
687    return _getSingleton()._initialiseKeyboard();
688  }
689
690  bool InputManager::initialiseMouse()
691  {
692    return _getSingleton()._initialiseMouse();
693  }
694
695  bool InputManager::initialiseJoySticks()
696  {
697    return _getSingleton()._initialiseJoySticks();
698  }
699
700  int InputManager::numberOfKeyboards()
701  {
702    if (_getSingleton().keyboard_ != 0)
703      return 1;
704    else
705      return 0;
706  }
707
708  int InputManager::numberOfMice()
709  {
710    if (_getSingleton().mouse_ != 0)
711      return 1;
712    else
713      return 0;
714  }
715
716  int InputManager::numberOfJoySticks()
717  {
[1293]718    return _getSingleton().joySticksSize_;
[1219]719  }
720
[1293]721  bool InputManager::isKeyDown(KeyCode::Enum key)
722  {
723    if (_getSingleton().keyboard_)
724      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
725    else
726      return false;
727  }
[1219]728
[1293]729  bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
730  {
731    if (_getSingleton().keyboard_)
732      return isModifierDown(modifier);
733    else
734      return false;
735  }
736
737  const MouseState InputManager::getMouseState()
738  {
739    if (_getSingleton().mouse_)
740      return _getSingleton().mouse_->getMouseState();
741    else
742      return MouseState();
743  }
744
745  const JoyStickState InputManager::getJoyStickState(unsigned int ID)
746  {
747    if (ID < _getSingleton().joySticksSize_)
748      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
749    else
750      return JoyStickState();
751  }
752
753
[1219]754  void InputManager::destroy()
755  {
756    _getSingleton()._destroy();
757  }
758
759  void InputManager::destroyKeyboard()
760  {
761    return _getSingleton()._destroyKeyboard();
762  }
763
764  void InputManager::destroyMouse()
765  {
766    return _getSingleton()._destroyMouse();
767  }
768
769  void InputManager::destroyJoySticks()
770  {
771    return _getSingleton()._destroyJoySticks();
772  }
773
774
[919]775  /**
776    @brief Adjusts the mouse window metrics.
777    This method has to be called every time the size of the window changes.
778    @param width The new width of the render window
779    @param height the new height of the render window
780  */
[1219]781  void InputManager::setWindowExtents(const int width, const int height)
[918]782  {
[1219]783    if (_getSingleton().mouse_)
784    {
785      // Set mouse region (if window resizes, we should alter this to reflect as well)
786      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
787      mouseState.width  = width;
788      mouseState.height = height;
789    }
[918]790  }
791
[919]792  /**
[1022]793    @brief Sets the input mode to either GUI, inGame or Buffer
794    @param mode The new input mode
795    @remark Only has an affect if the mode actually changes
[922]796  */
[1219]797  void InputManager::setInputState(const InputState state)
[922]798  {
[1219]799    _getSingleton().stateRequest_ = state;
[922]800  }
801
802  /**
[1022]803    @brief Returns the current input handling method
804    @return The current input mode.
[919]805  */
[1219]806  InputManager::InputState InputManager::getInputState()
[918]807  {
[1219]808    return _getSingleton().state_;
[918]809  }
810
[1219]811
812  // ###### KeyHandler ######
813
814  /**
815    @brief Adds a new key handler.
816    @param handler Pointer to the handler object.
817    @param name Unique name of the handler.
818    @return True if added, false if name already existed.
819  */
820  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
[1066]821  {
[1293]822    if (!handler)
823      return false;
[1219]824    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
825    {
826      _getSingleton().keyHandlers_[name] = handler;
827      return true;
828    }
829    else
830      return false;
[1066]831  }
832
[1219]833  /**
834    @brief Removes a Key handler from the list.
835    @param name Unique name of the handler.
836    @return True if removal was successful, false if name was not found.
837  */
838  bool InputManager::removeKeyHandler(const std::string &name)
839  {
840    disableKeyHandler(name);
841    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
842    if (it != _getSingleton().keyHandlers_.end())
843    {
844      _getSingleton().keyHandlers_.erase(it);
845      return true;
846    }
847    else
848      return false;
849  }
[1066]850
[1219]851  /**
852    @brief Returns the pointer to a handler.
853    @param name Unique name of the handler.
854    @return Pointer to the instance, 0 if name was not found.
855  */
856  KeyHandler* InputManager::getKeyHandler(const std::string& name)
857  {
858    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
859    if (it != _getSingleton().keyHandlers_.end())
860    {
861      return (*it).second;
862    }
863    else
864      return 0;
865  }
866
867  /**
868    @brief Enables a specific key handler that has already been added.
869    @param name Unique name of the handler.
870    @return False if name was not found, true otherwise.
871  */
872  bool InputManager::enableKeyHandler(const std::string& name)
873  {
874    // get pointer from the map with all stored handlers
875    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
876    if (mapIt == _getSingleton().keyHandlers_.end())
877      return false;
878    // see whether the handler already is in the list
879    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
880          it != _getSingleton().activeKeyHandlers_.end(); it++)
881    {
882      if ((*it) == (*mapIt).second)
883      {
884        _getSingleton().stateRequest_ = IS_CUSTOM;
885        return true;
886      }
887    }
888    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
889    _getSingleton().stateRequest_ = IS_CUSTOM;
890    return true;
891  }
892
893  /**
894    @brief Disables a specific key handler.
895    @param name Unique name of the handler.
896    @return False if name was not found, true otherwise.
897  */
898  bool InputManager::disableKeyHandler(const std::string &name)
899  {
900    // get pointer from the map with all stored handlers
901    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
902    if (mapIt == _getSingleton().keyHandlers_.end())
903      return false;
904    // look for the handler in the list
905    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
906          it != _getSingleton().activeKeyHandlers_.end(); it++)
907    {
908      if ((*it) == (*mapIt).second)
909      {
910        _getSingleton().activeKeyHandlers_.erase(it);
911        _getSingleton().stateRequest_ = IS_CUSTOM;
912        return true;
913      }
914    }
915    _getSingleton().stateRequest_ = IS_CUSTOM;
916    return true;
917  }
918
919  /**
920    @brief Checks whether a key handler is active
921    @param name Unique name of the handler.
922    @return False if key handler is not active or doesn't exist, true otherwise.
923  */
924  bool InputManager::isKeyHandlerActive(const std::string& name)
925  {
926    // get pointer from the map with all stored handlers
927    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
928    if (mapIt == _getSingleton().keyHandlers_.end())
929      return false;
930    // see whether the handler already is in the list
931    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
932          it != _getSingleton().activeKeyHandlers_.end(); it++)
933    {
934      if ((*it) == (*mapIt).second)
935        return true;
936    }
937    return false;
938  }
939
940
941  // ###### MouseHandler ######
942  /**
943    @brief Adds a new mouse handler.
944    @param handler Pointer to the handler object.
945    @param name Unique name of the handler.
946    @return True if added, false if name already existed.
947  */
948  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
949  {
[1293]950    if (!handler)
951      return false;
[1219]952    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
953    {
954      _getSingleton().mouseHandlers_[name] = handler;
955      return true;
956    }
957    else
958      return false;
959  }
960
961  /**
962    @brief Removes a Mouse handler from the list.
963    @param name Unique name of the handler.
964    @return True if removal was successful, false if name was not found.
965  */
966  bool InputManager::removeMouseHandler(const std::string &name)
967  {
968    disableMouseHandler(name);
969    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
970    if (it != _getSingleton().mouseHandlers_.end())
971    {
972      _getSingleton().mouseHandlers_.erase(it);
973      return true;
974    }
975    else
976      return false;
977  }
978
979  /**
980    @brief Returns the pointer to a handler.
981    @param name Unique name of the handler.
982    @return Pointer to the instance, 0 if name was not found.
983  */
984  MouseHandler* InputManager::getMouseHandler(const std::string& name)
985  {
986    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
987    if (it != _getSingleton().mouseHandlers_.end())
988    {
989      return (*it).second;
990    }
991    else
992      return 0;
993  }
994
995  /**
996    @brief Enables a specific mouse handler that has already been added.
997    @param name Unique name of the handler.
998    @return False if name was not found, true otherwise.
999  */
1000  bool InputManager::enableMouseHandler(const std::string& name)
1001  {
1002    // get pointer from the map with all stored handlers
1003    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1004    if (mapIt == _getSingleton().mouseHandlers_.end())
1005      return false;
1006    // see whether the handler already is in the list
1007    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1008          it != _getSingleton().activeMouseHandlers_.end(); it++)
1009    {
1010      if ((*it) == (*mapIt).second)
1011      {
1012        _getSingleton().stateRequest_ = IS_CUSTOM;
1013        return true;
1014      }
1015    }
1016    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1017    _getSingleton().stateRequest_ = IS_CUSTOM;
1018    return true;
1019  }
1020
1021  /**
1022    @brief Disables a specific mouse handler.
1023    @param name Unique name of the handler.
1024    @return False if name was not found, true otherwise.
1025  */
1026  bool InputManager::disableMouseHandler(const std::string &name)
1027  {
1028    // get pointer from the map with all stored handlers
1029    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1030    if (mapIt == _getSingleton().mouseHandlers_.end())
1031      return false;
1032    // look for the handler in the list
1033    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1034          it != _getSingleton().activeMouseHandlers_.end(); it++)
1035    {
1036      if ((*it) == (*mapIt).second)
1037      {
1038        _getSingleton().activeMouseHandlers_.erase(it);
1039        _getSingleton().stateRequest_ = IS_CUSTOM;
1040        return true;
1041      }
1042    }
1043    _getSingleton().stateRequest_ = IS_CUSTOM;
1044    return true;
1045  }
1046
1047  /**
1048    @brief Checks whether a mouse handler is active
1049    @param name Unique name of the handler.
1050    @return False if key handler is not active or doesn't exist, true otherwise.
1051  */
1052  bool InputManager::isMouseHandlerActive(const std::string& name)
1053  {
1054    // get pointer from the map with all stored handlers
1055    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1056    if (mapIt == _getSingleton().mouseHandlers_.end())
1057      return false;
1058    // see whether the handler already is in the list
1059    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1060          it != _getSingleton().activeMouseHandlers_.end(); it++)
1061    {
1062      if ((*it) == (*mapIt).second)
1063        return true;
1064    }
1065    return false;
1066  }
1067
1068
1069  // ###### JoyStickHandler ######
1070
1071  /**
1072    @brief Adds a new joy stick handler.
1073    @param handler Pointer to the handler object.
1074    @param name Unique name of the handler.
1075    @return True if added, false if name already existed.
1076  */
1077  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1078  {
[1293]1079    if (!handler)
1080      return false;
[1219]1081    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1082    {
1083      _getSingleton().joyStickHandlers_[name] = handler;
1084      return true;
1085    }
1086    else
1087      return false;
1088  }
1089
1090  /**
1091    @brief Removes a JoyStick handler from the list.
1092    @param name Unique name of the handler.
1093    @return True if removal was successful, false if name was not found.
1094  */
1095  bool InputManager::removeJoyStickHandler(const std::string &name)
1096  {
1097    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1098          itstick != _getSingleton().joySticks_.end(); itstick++)
1099      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1100
1101    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1102    if (it != _getSingleton().joyStickHandlers_.end())
1103    {
1104      _getSingleton().joyStickHandlers_.erase(it);
1105      return true;
1106    }
1107    else
1108      return false;
1109  }
1110
1111  /**
1112    @brief Returns the pointer to a handler.
1113    @param name Unique name of the handler.
1114    @return Pointer to the instance, 0 if name was not found.
1115  */
1116  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1117  {
1118    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1119    if (it != _getSingleton().joyStickHandlers_.end())
1120    {
1121      return (*it).second;
1122    }
1123    else
1124      return 0;
1125  }
1126
1127  /**
1128    @brief Enables a specific joy stick handler that has already been added.
1129    @param name Unique name of the handler.
1130    @return False if name or id was not found, true otherwise.
1131  */
[1293]1132  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
[1219]1133  {
1134    // get handler pointer from the map with all stored handlers
1135    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1136    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1137      return false;
1138
1139    // check for existence of the ID
[1293]1140    if (ID >= _getSingleton().joySticksSize_)
[1219]1141      return false;
1142
1143    // see whether the handler already is in the list
[1293]1144    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1145          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
[1219]1146    {
1147      if ((*it) == (*handlerIt).second)
1148      {
1149        _getSingleton().stateRequest_ = IS_CUSTOM;
1150        return true;
1151      }
1152    }
[1293]1153    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
[1219]1154    _getSingleton().stateRequest_ = IS_CUSTOM;
1155    return true;
1156  }
1157
1158  /**
1159    @brief Disables a specific joy stick handler.
1160    @param name Unique name of the handler.
1161    @return False if name or id was not found, true otherwise.
1162  */
[1293]1163  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
[1219]1164  {
1165    // get handler pointer from the map with all stored handlers
1166    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1167    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1168      return false;
1169
1170    // check for existence of the ID
[1293]1171    if (ID >= _getSingleton().joySticksSize_)
[1219]1172      return false;
1173
1174    // look for the handler in the list
[1293]1175    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1176          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
[1219]1177    {
1178      if ((*it) == (*handlerIt).second)
1179      {
[1293]1180        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
[1219]1181        _getSingleton().stateRequest_ = IS_CUSTOM;
1182        return true;
1183      }
1184    }
1185    return true;
1186  }
1187
1188  /**
1189    @brief Checks whether a joy stick handler is active
1190    @param name Unique name of the handler.
1191    @return False if key handler is not active or doesn't exist, true otherwise.
1192  */
[1293]1193  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
[1219]1194  {
1195    // get handler pointer from the map with all stored handlers
1196    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1197    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1198      return false;
1199
1200    // check for existence of the ID
[1293]1201    if (ID >= _getSingleton().joySticksSize_)
[1219]1202      return false;
1203
1204    // see whether the handler already is in the list
[1293]1205    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1206          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
[1219]1207    {
1208      if ((*it) == (*handlerIt).second)
1209        return true;
1210    }
1211    return false;
1212  }
1213
[918]1214}
Note: See TracBrowser for help on using the repository browser.