Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/core/InputManager.cc @ 1272

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

merged input branch into merge branch

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