Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

many minor changes, but it's more of a 'svn save' since I'm gonna start rewriting parts of the keybinder

File size: 37.7 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 the InputManager that captures all the input from OIS
32         and redirects it to handlers.
33 */
34
35#include "InputManager.h"
36#include "CoreIncludes.h"
37#include "Debug.h"
38#include "InputBuffer.h"
39#include "InputHandler.h"
40
41namespace orxonox
42{
43  // ###############################
44  // ###    Internal Methods     ###
45  // ###############################
46  // ###############################
47
48  /**
49    @brief Constructor only sets member fields to initial zero values
50           and registers the class in the class hierarchy.
51  */
52  InputManager::InputManager() :
53      inputSystem_(0), keyboard_(0), mouse_(0),
54      joySticksSize_(0),
55      state_(IS_UNINIT), stateRequest_(IS_UNINIT),
56      keyboardModifiers_(0)
57  {
58    RegisterObject(InputManager);
59  }
60
61  /**
62    @brief The one instance of the InputManager is stored in this function.
63    @return A reference to the only instance of the InputManager
64  */
65  InputManager& InputManager::_getSingleton()
66  {
67    static InputManager theOnlyInstance;
68    return theOnlyInstance;
69  }
70
71  /**
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.
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  */
86  bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight,
87        bool createKeyboard, bool createMouse, bool createJoySticks)
88  {
89    if (state_ == IS_UNINIT)
90    {
91      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
92
93      OIS::ParamList paramList;
94      std::ostringstream windowHndStr;
95
96      // Fill parameter list
97      windowHndStr << (unsigned int)windowHnd;
98      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
99
100//#if defined OIS_LINUX_PLATFORM
101//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
102//#endif
103
104      try
105      {
106        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
107        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
108      }
109      catch (OIS::Exception ex)
110      {
111        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
112            << "OIS message: \"" << ex.eText << "\"" << std::endl;
113        inputSystem_ = 0;
114        return false;
115      }
116
117      if (createKeyboard)
118        _initialiseKeyboard();
119
120      if (createMouse)
121        _initialiseMouse();
122
123      if (createJoySticks)
124        _initialiseJoySticks();
125
126      // Set mouse/joystick region
127      setWindowExtents(windowWidth, windowHeight);
128
129      state_ = IS_NONE;
130      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
131    }
132    else
133    {
134      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
135    }
136
137    // InputManager holds the input buffer --> create one and add it.
138    addKeyHandler(new InputBuffer(), "buffer");
139
140    KeyBinder* binder = new KeyBinder();
141    binder->loadBindings();
142    addKeyHandler(binder, "keybinder");
143    addMouseHandler(binder, "keybinder");
144    addJoyStickHandler(binder, "keybinder");
145
146    // Read all the key bindings and assign them
147    //if (!_loadBindings())
148    //  return false;
149
150    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
151    return true;
152  }
153
154  /**
155    @brief Creates a keyboard and sets the event handler.
156    @return False if keyboard stays uninitialised, true otherwise.
157  */
158  bool InputManager::_initialiseKeyboard()
159  {
160    if (keyboard_ != 0)
161    {
162      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
163      return true;
164    }
165    try
166    {
167      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
168      {
169        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
170        // register our listener in OIS.
171        keyboard_->setEventCallback(this);
172        // note: OIS will not detect keys that have already been down when the keyboard was created.
173        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
174        return true;
175      }
176      else
177      {
178        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
179        return false;
180      }
181    }
182    catch (OIS::Exception ex)
183    {
184      // TODO: Test this output regarding formatting
185      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
186          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
187      keyboard_ = 0;
188      return false;
189    }
190  }
191
192  /**
193    @brief Creates a mouse and sets the event handler.
194    @return False if mouse stays uninitialised, true otherwise.
195  */
196  bool InputManager::_initialiseMouse()
197  {
198    if (mouse_ != 0)
199    {
200      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
201      return true;
202    }
203    try
204    {
205      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
206      {
207        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
208        // register our listener in OIS.
209        mouse_->setEventCallback(this);
210        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
211        return true;
212      }
213      else
214      {
215        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
216        return false;
217      }
218    }
219    catch (OIS::Exception ex)
220    {
221      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
222          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
223      mouse_ = 0;
224      return false;
225    }
226  }
227
228  /**
229    @brief Creates all joy sticks and sets the event handler.
230    @return False joy stick stay uninitialised, true otherwise.
231  */
232  bool InputManager::_initialiseJoySticks()
233  {
234    if (joySticksSize_ > 0)
235    {
236      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
237      return true;
238    }
239    bool success = false;
240    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
241    {
242      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
243      {
244        try
245        {
246          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
247          joySticks_.push_back(stig);
248          // register our listener in OIS.
249          stig->setEventCallback(this);
250          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
251          success = true;
252        }
253        catch (OIS::Exception ex)
254        {
255          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
256              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
257        }
258      }
259    }
260    else
261    {
262      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
263      return false;
264    }
265    joySticksSize_ = joySticks_.size();
266    activeJoyStickHandlers_.resize(joySticksSize_);
267    joyStickButtonsDown_.resize(joySticksSize_);
268    return success;
269  }
270
271  /**
272    @brief Destroys all the created input devices and sets the InputManager to construction state.
273  */
274  void InputManager::_destroy()
275  {
276    CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
277
278    if (state_ != IS_UNINIT)
279    {
280      if (keyHandlers_.find("buffer") != keyHandlers_.end())
281        delete keyHandlers_["buffer"];
282
283      if (keyHandlers_.find("keybinder") != keyHandlers_.end())
284        delete keyHandlers_["keybinder"];
285
286      keyHandlers_.clear();
287      mouseHandlers_.clear();
288      joyStickHandlers_.clear();
289
290      _destroyKeyboard();
291      _destroyMouse();
292      _destroyJoySticks();
293
294      // inputSystem_ can never be 0, or else the code is mistaken
295      OIS::InputManager::destroyInputSystem(inputSystem_);
296      inputSystem_ = 0;
297
298      state_ = IS_UNINIT;
299    }
300    else
301      CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl;
302
303    CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
304  }
305
306  /**
307    @brief Destroys the keyboard and sets it to 0.
308  */
309  void InputManager::_destroyKeyboard()
310  {
311    if (keyboard_)
312      // inputSystem_ can never be 0, or else the code is mistaken
313      inputSystem_->destroyInputObject(keyboard_);
314    keyboard_ = 0;
315    activeKeyHandlers_.clear();
316    keysDown_.clear();
317    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
318  }
319
320  /**
321    @brief Destroys the mouse and sets it to 0.
322  */
323  void InputManager::_destroyMouse()
324  {
325    if (mouse_)
326      // inputSystem_ can never be 0, or else the code is mistaken
327      inputSystem_->destroyInputObject(mouse_);
328    mouse_ = 0;
329    activeMouseHandlers_.clear();
330    mouseButtonsDown_.clear();
331    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
332  }
333
334  /**
335    @brief Destroys all the joy sticks and resizes the lists to 0.
336  */
337  void InputManager::_destroyJoySticks()
338  {
339    if (joySticksSize_ > 0)
340    {
341      // note: inputSystem_ can never be 0, or else the code is mistaken
342      for (unsigned int i = 0; i < joySticksSize_; i++)
343        if (joySticks_[i] != 0)
344          inputSystem_->destroyInputObject(joySticks_[i]);
345
346      joySticks_.clear();
347      joySticksSize_ = 0;
348      activeJoyStickHandlers_.clear();
349      joyStickButtonsDown_.clear();
350    }
351    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
352  }
353
354
355  // #################################
356  // ### Private Interface Methods ###
357  // #################################
358  // #################################
359
360  /**
361    @brief Updates the InputManager. Tick is called by Orxonox.
362    @param dt Delta time
363  */
364  void InputManager::tick(float dt)
365  {
366    if (state_ == IS_UNINIT)
367      return;
368
369    // reset the game if it has changed
370    if (state_ != stateRequest_)
371    {
372      if (stateRequest_ != IS_CUSTOM)
373      {
374        activeKeyHandlers_.clear();
375        activeMouseHandlers_.clear();
376        for (unsigned int i = 0; i < joySticksSize_; i++)
377          activeJoyStickHandlers_[i].clear();
378
379        switch (stateRequest_)
380        {
381        case IS_NORMAL:
382          // normal play mode
383          // note: we assume that the handlers exist since otherwise, something's wrong anyway.
384          activeKeyHandlers_.push_back(keyHandlers_["keybinder"]);
385          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
386          activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
387          for (unsigned int i = 0; i < joySticksSize_; i++)
388            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
389          break;
390
391        case IS_GUI:
392          // FIXME: do stuff
393          break;
394
395        case IS_CONSOLE:
396          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
397          activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
398          for (unsigned int i = 0; i < joySticksSize_; i++)
399            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
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();
416    for (unsigned  int i = 0; i < joySticksSize_; i++)
417      joySticks_[i]->capture();
418
419
420    // call all the handlers for the held key events
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_));
424
425    // call all the handlers for the held mouse button events
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]);
429
430    // call all the handlers for the held joy stick button events
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]);
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)
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));
452
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
461    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
462      activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
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
474    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
475    {
476      if (keysDown_[iKey].key == (KeyCode::Enum)e.key)
477      {
478        keysDown_.erase(keysDown_.begin() + iKey);
479        break;
480      }
481    }
482
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
491    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
492      activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
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  {
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    }
512
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
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)
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);
536
537    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
538      activeMouseHandlers_[i]->mouseButtonPressed(e.state, (MouseButton::Enum)id);
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
551    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
552    {
553      if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id)
554      {
555        mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton);
556        break;
557      }
558    }
559
560    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
561      activeMouseHandlers_[i]->mouseButtonReleased(e.state, (MouseButton::Enum)id);
562
563    return true;
564  }
565
566
567  // ###### Joy Stick Events ######
568
569  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
570  {
571    // use the device to identify which one called the method
572    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
573    unsigned int iJoyStick = 0;
574    while (joySticks_[iJoyStick] != joyStick)
575      iJoyStick++;
576
577    // check whether the button already is in the list (can happen when focus was lost)
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);
584
585    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
586      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(JoyStickState(arg.state, iJoyStick), button);
587
588    return true;
589  }
590
591  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
592  {
593    // use the device to identify which one called the method
594    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
595    unsigned int iJoyStick = 0;
596    while (joySticks_[iJoyStick] != joyStick)
597      iJoyStick++;
598
599    // remove the button from the joyStickButtonsDown_ list
600    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
601    for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
602    {
603      if (buttonsDown[iButton] == button)
604      {
605        buttonsDown.erase(buttonsDown.begin() + iButton);
606        break;
607      }
608    }
609
610    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
611      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(JoyStickState(arg.state, iJoyStick), button);
612
613    return true;
614  }
615
616  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
617  {
618    // use the device to identify which one called the method
619    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
620    unsigned int iJoyStick = 0;
621    while (joySticks_[iJoyStick] != joyStick)
622      iJoyStick++;
623
624    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
625      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(JoyStickState(arg.state, iJoyStick), axis);
626
627    return true;
628  }
629
630  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
631  {
632    // use the device to identify which one called the method
633    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
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);
640
641    return true;
642  }
643
644  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
645  {
646    // use the device to identify which one called the method
647    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
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);
654
655    return true;
656  }
657
658  bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
659  {
660    // use the device to identify which one called the method
661    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
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);
668
669    return true;
670  }
671
672
673  // ################################
674  // ### Static Interface Methods ###
675  // ################################
676  // ################################
677
678  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
679    bool createKeyboard, bool createMouse, bool createJoySticks)
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  {
718    return _getSingleton().joySticksSize_;
719  }
720
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  }
728
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
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
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  */
781  void InputManager::setWindowExtents(const int width, const int height)
782  {
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    }
790  }
791
792  /**
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
796  */
797  void InputManager::setInputState(const InputState state)
798  {
799    _getSingleton().stateRequest_ = state;
800  }
801
802  /**
803    @brief Returns the current input handling method
804    @return The current input mode.
805  */
806  InputManager::InputState InputManager::getInputState()
807  {
808    return _getSingleton().state_;
809  }
810
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)
821  {
822    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
823    {
824      _getSingleton().keyHandlers_[name] = handler;
825      return true;
826    }
827    else
828      return false;
829  }
830
831  /**
832    @brief Removes a Key handler from the list.
833    @param name Unique name of the handler.
834    @return True if removal was successful, false if name was not found.
835  */
836  bool InputManager::removeKeyHandler(const std::string &name)
837  {
838    disableKeyHandler(name);
839    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
840    if (it != _getSingleton().keyHandlers_.end())
841    {
842      _getSingleton().keyHandlers_.erase(it);
843      return true;
844    }
845    else
846      return false;
847  }
848
849  /**
850    @brief Returns the pointer to a handler.
851    @param name Unique name of the handler.
852    @return Pointer to the instance, 0 if name was not found.
853  */
854  KeyHandler* InputManager::getKeyHandler(const std::string& name)
855  {
856    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
857    if (it != _getSingleton().keyHandlers_.end())
858    {
859      return (*it).second;
860    }
861    else
862      return 0;
863  }
864
865  /**
866    @brief Enables a specific key handler that has already been added.
867    @param name Unique name of the handler.
868    @return False if name was not found, true otherwise.
869  */
870  bool InputManager::enableKeyHandler(const std::string& name)
871  {
872    // get pointer from the map with all stored handlers
873    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
874    if (mapIt == _getSingleton().keyHandlers_.end())
875      return false;
876    // see whether the handler already is in the list
877    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
878          it != _getSingleton().activeKeyHandlers_.end(); it++)
879    {
880      if ((*it) == (*mapIt).second)
881      {
882        _getSingleton().stateRequest_ = IS_CUSTOM;
883        return true;
884      }
885    }
886    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
887    _getSingleton().stateRequest_ = IS_CUSTOM;
888    return true;
889  }
890
891  /**
892    @brief Disables a specific key handler.
893    @param name Unique name of the handler.
894    @return False if name was not found, true otherwise.
895  */
896  bool InputManager::disableKeyHandler(const std::string &name)
897  {
898    // get pointer from the map with all stored handlers
899    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
900    if (mapIt == _getSingleton().keyHandlers_.end())
901      return false;
902    // look for the handler in the list
903    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
904          it != _getSingleton().activeKeyHandlers_.end(); it++)
905    {
906      if ((*it) == (*mapIt).second)
907      {
908        _getSingleton().activeKeyHandlers_.erase(it);
909        _getSingleton().stateRequest_ = IS_CUSTOM;
910        return true;
911      }
912    }
913    _getSingleton().stateRequest_ = IS_CUSTOM;
914    return true;
915  }
916
917  /**
918    @brief Checks whether a key handler is active
919    @param name Unique name of the handler.
920    @return False if key handler is not active or doesn't exist, true otherwise.
921  */
922  bool InputManager::isKeyHandlerActive(const std::string& name)
923  {
924    // get pointer from the map with all stored handlers
925    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
926    if (mapIt == _getSingleton().keyHandlers_.end())
927      return false;
928    // see whether the handler already is in the list
929    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
930          it != _getSingleton().activeKeyHandlers_.end(); it++)
931    {
932      if ((*it) == (*mapIt).second)
933        return true;
934    }
935    return false;
936  }
937
938
939  // ###### MouseHandler ######
940  /**
941    @brief Adds a new mouse handler.
942    @param handler Pointer to the handler object.
943    @param name Unique name of the handler.
944    @return True if added, false if name already existed.
945  */
946  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
947  {
948    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
949    {
950      _getSingleton().mouseHandlers_[name] = handler;
951      return true;
952    }
953    else
954      return false;
955  }
956
957  /**
958    @brief Removes a Mouse handler from the list.
959    @param name Unique name of the handler.
960    @return True if removal was successful, false if name was not found.
961  */
962  bool InputManager::removeMouseHandler(const std::string &name)
963  {
964    disableMouseHandler(name);
965    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
966    if (it != _getSingleton().mouseHandlers_.end())
967    {
968      _getSingleton().mouseHandlers_.erase(it);
969      return true;
970    }
971    else
972      return false;
973  }
974
975  /**
976    @brief Returns the pointer to a handler.
977    @param name Unique name of the handler.
978    @return Pointer to the instance, 0 if name was not found.
979  */
980  MouseHandler* InputManager::getMouseHandler(const std::string& name)
981  {
982    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
983    if (it != _getSingleton().mouseHandlers_.end())
984    {
985      return (*it).second;
986    }
987    else
988      return 0;
989  }
990
991  /**
992    @brief Enables a specific mouse handler that has already been added.
993    @param name Unique name of the handler.
994    @return False if name was not found, true otherwise.
995  */
996  bool InputManager::enableMouseHandler(const std::string& name)
997  {
998    // get pointer from the map with all stored handlers
999    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1000    if (mapIt == _getSingleton().mouseHandlers_.end())
1001      return false;
1002    // see whether the handler already is in the list
1003    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1004          it != _getSingleton().activeMouseHandlers_.end(); it++)
1005    {
1006      if ((*it) == (*mapIt).second)
1007      {
1008        _getSingleton().stateRequest_ = IS_CUSTOM;
1009        return true;
1010      }
1011    }
1012    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1013    _getSingleton().stateRequest_ = IS_CUSTOM;
1014    return true;
1015  }
1016
1017  /**
1018    @brief Disables a specific mouse handler.
1019    @param name Unique name of the handler.
1020    @return False if name was not found, true otherwise.
1021  */
1022  bool InputManager::disableMouseHandler(const std::string &name)
1023  {
1024    // get pointer from the map with all stored handlers
1025    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1026    if (mapIt == _getSingleton().mouseHandlers_.end())
1027      return false;
1028    // look for the handler in the list
1029    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1030          it != _getSingleton().activeMouseHandlers_.end(); it++)
1031    {
1032      if ((*it) == (*mapIt).second)
1033      {
1034        _getSingleton().activeMouseHandlers_.erase(it);
1035        _getSingleton().stateRequest_ = IS_CUSTOM;
1036        return true;
1037      }
1038    }
1039    _getSingleton().stateRequest_ = IS_CUSTOM;
1040    return true;
1041  }
1042
1043  /**
1044    @brief Checks whether a mouse handler is active
1045    @param name Unique name of the handler.
1046    @return False if key handler is not active or doesn't exist, true otherwise.
1047  */
1048  bool InputManager::isMouseHandlerActive(const std::string& name)
1049  {
1050    // get pointer from the map with all stored handlers
1051    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1052    if (mapIt == _getSingleton().mouseHandlers_.end())
1053      return false;
1054    // see whether the handler already is in the list
1055    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1056          it != _getSingleton().activeMouseHandlers_.end(); it++)
1057    {
1058      if ((*it) == (*mapIt).second)
1059        return true;
1060    }
1061    return false;
1062  }
1063
1064
1065  // ###### JoyStickHandler ######
1066
1067  /**
1068    @brief Adds a new joy stick handler.
1069    @param handler Pointer to the handler object.
1070    @param name Unique name of the handler.
1071    @return True if added, false if name already existed.
1072  */
1073  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1074  {
1075    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1076    {
1077      _getSingleton().joyStickHandlers_[name] = handler;
1078      return true;
1079    }
1080    else
1081      return false;
1082  }
1083
1084  /**
1085    @brief Removes a JoyStick handler from the list.
1086    @param name Unique name of the handler.
1087    @return True if removal was successful, false if name was not found.
1088  */
1089  bool InputManager::removeJoyStickHandler(const std::string &name)
1090  {
1091    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1092          itstick != _getSingleton().joySticks_.end(); itstick++)
1093      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1094
1095    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1096    if (it != _getSingleton().joyStickHandlers_.end())
1097    {
1098      _getSingleton().joyStickHandlers_.erase(it);
1099      return true;
1100    }
1101    else
1102      return false;
1103  }
1104
1105  /**
1106    @brief Returns the pointer to a handler.
1107    @param name Unique name of the handler.
1108    @return Pointer to the instance, 0 if name was not found.
1109  */
1110  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1111  {
1112    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1113    if (it != _getSingleton().joyStickHandlers_.end())
1114    {
1115      return (*it).second;
1116    }
1117    else
1118      return 0;
1119  }
1120
1121  /**
1122    @brief Enables a specific joy stick handler that has already been added.
1123    @param name Unique name of the handler.
1124    @return False if name or id was not found, true otherwise.
1125  */
1126  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1127  {
1128    // get handler pointer from the map with all stored handlers
1129    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1130    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1131      return false;
1132
1133    // check for existence of the ID
1134    if (ID >= _getSingleton().joySticksSize_)
1135      return false;
1136
1137    // see whether the handler already is in the list
1138    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1139          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1140    {
1141      if ((*it) == (*handlerIt).second)
1142      {
1143        _getSingleton().stateRequest_ = IS_CUSTOM;
1144        return true;
1145      }
1146    }
1147    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1148    _getSingleton().stateRequest_ = IS_CUSTOM;
1149    return true;
1150  }
1151
1152  /**
1153    @brief Disables a specific joy stick handler.
1154    @param name Unique name of the handler.
1155    @return False if name or id was not found, true otherwise.
1156  */
1157  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1158  {
1159    // get handler pointer from the map with all stored handlers
1160    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1161    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1162      return false;
1163
1164    // check for existence of the ID
1165    if (ID >= _getSingleton().joySticksSize_)
1166      return false;
1167
1168    // look for the handler in the list
1169    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1170          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1171    {
1172      if ((*it) == (*handlerIt).second)
1173      {
1174        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1175        _getSingleton().stateRequest_ = IS_CUSTOM;
1176        return true;
1177      }
1178    }
1179    return true;
1180  }
1181
1182  /**
1183    @brief Checks whether a joy stick handler is active
1184    @param name Unique name of the handler.
1185    @return False if key handler is not active or doesn't exist, true otherwise.
1186  */
1187  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1188  {
1189    // get handler pointer from the map with all stored handlers
1190    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1191    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1192      return false;
1193
1194    // check for existence of the ID
1195    if (ID >= _getSingleton().joySticksSize_)
1196      return false;
1197
1198    // see whether the handler already is in the list
1199    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1200          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1201    {
1202      if ((*it) == (*handlerIt).second)
1203        return true;
1204    }
1205    return false;
1206  }
1207
1208}
Note: See TracBrowser for help on using the repository browser.