Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Basically, almost everything about the input management is written, but I wasn't yet able to test things.

File size: 40.5 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    povStates_.resize(joySticksSize_);
270    sliderStates_.resize(joySticksSize_);
271    return success;
272  }
273
274  /**
275    @brief Destroys all the created input devices and sets the InputManager to construction state.
276  */
277  void InputManager::_destroy()
278  {
279    if (state_ != IS_UNINIT)
280    {
281      CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
282
283      if (keyHandlers_.find("buffer") != keyHandlers_.end())
284        delete keyHandlers_["buffer"];
285
286      if (keyHandlers_.find("keybinder") != keyHandlers_.end())
287        delete keyHandlers_["keybinder"];
288
289      keyHandlers_.clear();
290      mouseHandlers_.clear();
291      joyStickHandlers_.clear();
292
293      _destroyKeyboard();
294      _destroyMouse();
295      _destroyJoySticks();
296
297      // inputSystem_ can never be 0, or else the code is mistaken
298      OIS::InputManager::destroyInputSystem(inputSystem_);
299      inputSystem_ = 0;
300
301      state_ = IS_UNINIT;
302      CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
303    }
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  void InputManager::_updateTickables()
355  {
356    // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
357    std::set<InputTickable*> tempSet;
358    for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
359      tempSet.insert(activeKeyHandlers_[iHandler]);
360    for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
361      tempSet.insert(activeMouseHandlers_[iHandler]);
362    for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
363      for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
364        tempSet.insert(activeJoyStickHandlers_[iJoyStick][iHandler]);
365
366    // copy the content of the set back to the actual vector
367    activeHandlers_.clear();
368    for (std::set<InputTickable*>::const_iterator itHandler = tempSet.begin(); itHandler != tempSet.end(); itHandler++)
369      activeHandlers_.push_back(*itHandler);
370  }
371
372
373  // #################################
374  // ### Private Interface Methods ###
375  // #################################
376  // #################################
377
378  /**
379    @brief Updates the InputManager. Tick is called by Orxonox.
380    @param dt Delta time
381  */
382  void InputManager::tick(float dt)
383  {
384    if (state_ == IS_UNINIT)
385      return;
386
387    // reset the game if it has changed
388    if (state_ != stateRequest_)
389    {
390      if (stateRequest_ != IS_CUSTOM)
391      {
392        activeKeyHandlers_.clear();
393        activeMouseHandlers_.clear();
394        for (unsigned int i = 0; i < joySticksSize_; i++)
395          activeJoyStickHandlers_[i].clear();
396
397        switch (stateRequest_)
398        {
399        case IS_NORMAL:
400          // normal play mode
401          // note: we assume that the handlers exist since otherwise, something's wrong anyway.
402          activeKeyHandlers_.push_back(keyHandlers_["keybinder"]);
403          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
404          if (getMouseHandler("SpaceShip"))
405            activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
406          for (unsigned int i = 0; i < joySticksSize_; i++)
407            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
408          break;
409
410        case IS_GUI:
411          // TODO: do stuff
412          break;
413
414        case IS_CONSOLE:
415          activeMouseHandlers_.push_back(mouseHandlers_["keybinder"]);
416          if (getMouseHandler("SpaceShip"))
417            activeMouseHandlers_.push_back(mouseHandlers_["SpaceShip"]);
418          for (unsigned int i = 0; i < joySticksSize_; i++)
419            activeJoyStickHandlers_[i].push_back(joyStickHandlers_["keybinder"]);
420
421          activeKeyHandlers_.push_back(keyHandlers_["buffer"]);
422          break;
423
424        default:
425          break;
426        }
427        state_ = stateRequest_;
428      }
429    }
430
431    // Capture all the input. This calls the event handlers in InputManager.
432    if (mouse_)
433      mouse_->capture();
434    if (keyboard_)
435      keyboard_->capture();
436    for (unsigned  int i = 0; i < joySticksSize_; i++)
437      joySticks_[i]->capture();
438
439
440    // call all the handlers for the held key events
441    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
442      for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
443        activeKeyHandlers_[iHandler]->keyHeld(KeyEvent(keysDown_[iKey], keyboardModifiers_));
444
445    // call all the handlers for the held mouse button events
446    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
447      for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
448        activeMouseHandlers_[iHandler]->mouseButtonHeld(mouseButtonsDown_[iButton]);
449
450    // call all the handlers for the held joy stick button events
451    for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
452      for (unsigned int iButton   = 0; iButton   < joyStickButtonsDown_[iJoyStick].size(); iButton++)
453        for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
454          activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonHeld(iJoyStick, joyStickButtonsDown_[iJoyStick][iButton]);
455
456
457    // call the ticks
458    for (unsigned int iHandler = 0; iHandler < activeHandlers_.size(); iHandler++)
459      activeHandlers_[iHandler]->tick(dt);
460  }
461
462  // ###### Key Events ######
463
464  /**
465    @brief Event handler for the keyPressed Event.
466    @param e Event information
467  */
468  bool InputManager::keyPressed(const OIS::KeyEvent &e)
469  {
470    // check whether the key already is in the list (can happen when focus was lost)
471    unsigned int iKey = 0;
472    while (iKey < keysDown_.size() && keysDown_[iKey].key != (KeyCode::Enum)e.key)
473      iKey++;
474    if (iKey == keysDown_.size())
475      keysDown_.push_back(Key(e));
476
477    // update modifiers
478    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
479      keyboardModifiers_ |= KeyboardModifier::Alt;   // alt key
480    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
481      keyboardModifiers_ |= KeyboardModifier::Ctrl;  // ctrl key
482    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
483      keyboardModifiers_ |= KeyboardModifier::Shift; // shift key
484
485    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
486      activeKeyHandlers_[i]->keyPressed(KeyEvent(e, keyboardModifiers_));
487
488    return true;
489  }
490
491  /**
492    @brief Event handler for the keyReleased Event.
493    @param e Event information
494  */
495  bool InputManager::keyReleased(const OIS::KeyEvent &e)
496  {
497    // remove the key from the keysDown_ list
498    for (unsigned int iKey = 0; iKey < keysDown_.size(); iKey++)
499    {
500      if (keysDown_[iKey].key == (KeyCode::Enum)e.key)
501      {
502        keysDown_.erase(keysDown_.begin() + iKey);
503        break;
504      }
505    }
506
507    // update modifiers
508    if(e.key == OIS::KC_RMENU    || e.key == OIS::KC_LMENU)
509      keyboardModifiers_ &= ~KeyboardModifier::Alt;   // alt key
510    if(e.key == OIS::KC_RCONTROL || e.key == OIS::KC_LCONTROL)
511      keyboardModifiers_ &= ~KeyboardModifier::Ctrl;  // ctrl key
512    if(e.key == OIS::KC_RSHIFT   || e.key == OIS::KC_LSHIFT)
513      keyboardModifiers_ &= ~KeyboardModifier::Shift; // shift key
514
515    for (unsigned int i = 0; i < activeKeyHandlers_.size(); i++)
516      activeKeyHandlers_[i]->keyReleased(KeyEvent(e, keyboardModifiers_));
517
518    return true;
519  }
520
521
522  // ###### Mouse Events ######
523
524  /**
525    @brief Event handler for the mouseMoved Event.
526    @param e Event information
527  */
528  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
529  {
530    // check for actual moved event
531    if (e.state.X.rel != 0 || e.state.Y.rel != 0)
532    {
533      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
534        activeMouseHandlers_[i]->mouseMoved(IntVector2(e.state.X.abs, e.state.Y.abs),
535            IntVector2(e.state.X.rel, e.state.Y.rel), IntVector2(e.state.width, e.state.height));
536    }
537
538    // check for mouse scrolled event
539    if (e.state.Z.rel != 0)
540    {
541      for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
542        activeMouseHandlers_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel);
543    }
544
545    return true;
546  }
547
548  /**
549    @brief Event handler for the mousePressed Event.
550    @param e Event information
551    @param id The ID of the mouse button
552  */
553  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
554  {
555    // check whether the button already is in the list (can happen when focus was lost)
556    unsigned int iButton = 0;
557    while (iButton < mouseButtonsDown_.size() && mouseButtonsDown_[iButton] != (MouseButton::Enum)id)
558      iButton++;
559    if (iButton == mouseButtonsDown_.size())
560      mouseButtonsDown_.push_back((MouseButton::Enum)id);
561
562    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
563      activeMouseHandlers_[i]->mouseButtonPressed((MouseButton::Enum)id);
564
565    return true;
566  }
567
568  /**
569    @brief Event handler for the mouseReleased Event.
570    @param e Event information
571    @param id The ID of the mouse button
572  */
573  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
574  {
575    // remove the button from the keysDown_ list
576    for (unsigned int iButton = 0; iButton < mouseButtonsDown_.size(); iButton++)
577    {
578      if (mouseButtonsDown_[iButton] == (MouseButton::Enum)id)
579      {
580        mouseButtonsDown_.erase(mouseButtonsDown_.begin() + iButton);
581        break;
582      }
583    }
584
585    for (unsigned int i = 0; i < activeMouseHandlers_.size(); i++)
586      activeMouseHandlers_[i]->mouseButtonReleased((MouseButton::Enum)id);
587
588    return true;
589  }
590
591
592  // ###### Joy Stick Events ######
593
594  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
595  {
596    // use the device to identify which one called the method
597    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
598    unsigned int iJoyStick = 0;
599    while (joySticks_[iJoyStick] != joyStick)
600      iJoyStick++;
601
602    // check whether the button already is in the list (can happen when focus was lost)
603    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
604    unsigned int iButton = 0;
605    while (iButton < buttonsDown.size() && buttonsDown[iButton] != button)
606      iButton++;
607    if (iButton == buttonsDown.size())
608      buttonsDown.push_back(button);
609
610    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
611      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonPressed(iJoyStick, button);
612
613    return true;
614  }
615
616  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
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    // remove the button from the joyStickButtonsDown_ list
625    std::vector<int> buttonsDown = joyStickButtonsDown_[iJoyStick];
626    for (unsigned int iButton = 0; iButton < buttonsDown.size(); iButton++)
627    {
628      if (buttonsDown[iButton] == button)
629      {
630        buttonsDown.erase(buttonsDown.begin() + iButton);
631        break;
632      }
633    }
634
635    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
636      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickButtonReleased(iJoyStick, button);
637
638    return true;
639  }
640
641  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
642  {
643    //CCOUT(3) << arg.state.mAxes[axis].abs << std::endl;
644    // use the device to identify which one called the method
645    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
646    unsigned int iJoyStick = 0;
647    while (joySticks_[iJoyStick] != joyStick)
648      iJoyStick++;
649
650    // keep in mind that the first 8 axes are reserved for the sliders
651    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
652      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis + 8, arg.state.mAxes[axis].abs);
653
654    return true;
655  }
656
657  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
658  {
659    //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl;
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    if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX)
667    {
668      // slider X axis changed
669      sliderStates_[iJoyStick].sliderStates[id].x = arg.state.mSliders[id].abX;
670      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
671        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, id * 2, arg.state.mSliders[id].abX);
672    }
673    else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY)
674    {
675      // slider Y axis changed
676      sliderStates_[iJoyStick].sliderStates[id].y = arg.state.mSliders[id].abY;
677      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
678        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY);
679    }
680
681    return true;
682  }
683
684  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
685  {
686    // use the device to identify which one called the method
687    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
688    unsigned int iJoyStick = 0;
689    while (joySticks_[iJoyStick] != joyStick)
690      iJoyStick++;
691
692    // translate the POV into 8 simple buttons
693    int lastState = povStates_[iJoyStick][id];
694    if (lastState & OIS::Pov::North)
695      buttonReleased(arg, 32 + id * 4 + 0);
696    if (lastState & OIS::Pov::South)
697      buttonReleased(arg, 32 + id * 4 + 1);
698    if (lastState & OIS::Pov::East)
699      buttonReleased(arg, 32 + id * 4 + 2);
700    if (lastState & OIS::Pov::West)
701      buttonReleased(arg, 32 + id * 4 + 3);
702   
703    povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction;
704    int currentState = povStates_[iJoyStick][id];
705    if (currentState & OIS::Pov::North)
706      buttonPressed(arg, 32 + id * 4 + 0);
707    if (currentState & OIS::Pov::South)
708      buttonPressed(arg, 32 + id * 4 + 1);
709    if (currentState & OIS::Pov::East)
710      buttonPressed(arg, 32 + id * 4 + 2);
711    if (currentState & OIS::Pov::West)
712      buttonPressed(arg, 32 + id * 4 + 3);
713
714    return true;
715  }
716
717  /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
718  {
719    // use the device to identify which one called the method
720    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
721    unsigned int iJoyStick = 0;
722    while (joySticks_[iJoyStick] != joyStick)
723      iJoyStick++;
724
725    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
726      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
727
728    return true;
729  }*/
730
731
732  // ################################
733  // ### Static Interface Methods ###
734  // ################################
735  // ################################
736
737  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
738    bool createKeyboard, bool createMouse, bool createJoySticks)
739  {
740    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
741          createKeyboard, createMouse, createJoySticks);
742  }
743
744  bool InputManager::initialiseKeyboard()
745  {
746    return _getSingleton()._initialiseKeyboard();
747  }
748
749  bool InputManager::initialiseMouse()
750  {
751    return _getSingleton()._initialiseMouse();
752  }
753
754  bool InputManager::initialiseJoySticks()
755  {
756    return _getSingleton()._initialiseJoySticks();
757  }
758
759  int InputManager::numberOfKeyboards()
760  {
761    if (_getSingleton().keyboard_ != 0)
762      return 1;
763    else
764      return 0;
765  }
766
767  int InputManager::numberOfMice()
768  {
769    if (_getSingleton().mouse_ != 0)
770      return 1;
771    else
772      return 0;
773  }
774
775  int InputManager::numberOfJoySticks()
776  {
777    return _getSingleton().joySticksSize_;
778  }
779
780  bool InputManager::isKeyDown(KeyCode::Enum key)
781  {
782    if (_getSingleton().keyboard_)
783      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
784    else
785      return false;
786  }
787
788  bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
789  {
790    if (_getSingleton().keyboard_)
791      return isModifierDown(modifier);
792    else
793      return false;
794  }
795
796  /*const MouseState InputManager::getMouseState()
797  {
798    if (_getSingleton().mouse_)
799      return _getSingleton().mouse_->getMouseState();
800    else
801      return MouseState();
802  }*/
803
804  /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
805  {
806    if (ID < _getSingleton().joySticksSize_)
807      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
808    else
809      return JoyStickState();
810  }*/
811
812
813  void InputManager::destroy()
814  {
815    _getSingleton()._destroy();
816  }
817
818  void InputManager::destroyKeyboard()
819  {
820    return _getSingleton()._destroyKeyboard();
821  }
822
823  void InputManager::destroyMouse()
824  {
825    return _getSingleton()._destroyMouse();
826  }
827
828  void InputManager::destroyJoySticks()
829  {
830    return _getSingleton()._destroyJoySticks();
831  }
832
833
834  /**
835    @brief Adjusts the mouse window metrics.
836    This method has to be called every time the size of the window changes.
837    @param width The new width of the render window
838    @param height the new height of the render window
839  */
840  void InputManager::setWindowExtents(const int width, const int height)
841  {
842    if (_getSingleton().mouse_)
843    {
844      // Set mouse region (if window resizes, we should alter this to reflect as well)
845      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
846      mouseState.width  = width;
847      mouseState.height = height;
848    }
849  }
850
851  /**
852    @brief Sets the input mode to either GUI, inGame or Buffer
853    @param mode The new input mode
854    @remark Only has an affect if the mode actually changes
855  */
856  void InputManager::setInputState(const InputState state)
857  {
858    _getSingleton().stateRequest_ = state;
859  }
860
861  /**
862    @brief Returns the current input handling method
863    @return The current input mode.
864  */
865  InputManager::InputState InputManager::getInputState()
866  {
867    return _getSingleton().state_;
868  }
869
870
871  // ###### KeyHandler ######
872
873  /**
874    @brief Adds a new key handler.
875    @param handler Pointer to the handler object.
876    @param name Unique name of the handler.
877    @return True if added, false if name already existed.
878  */
879  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
880  {
881    if (!handler)
882      return false;
883    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
884    {
885      _getSingleton().keyHandlers_[name] = handler;
886      return true;
887    }
888    else
889      return false;
890  }
891
892  /**
893    @brief Removes a Key handler from the list.
894    @param name Unique name of the handler.
895    @return True if removal was successful, false if name was not found.
896  */
897  bool InputManager::removeKeyHandler(const std::string &name)
898  {
899    disableKeyHandler(name);
900    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
901    if (it != _getSingleton().keyHandlers_.end())
902    {
903      _getSingleton().keyHandlers_.erase(it);
904      return true;
905    }
906    else
907      return false;
908  }
909
910  /**
911    @brief Returns the pointer to a handler.
912    @param name Unique name of the handler.
913    @return Pointer to the instance, 0 if name was not found.
914  */
915  KeyHandler* InputManager::getKeyHandler(const std::string& name)
916  {
917    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
918    if (it != _getSingleton().keyHandlers_.end())
919    {
920      return (*it).second;
921    }
922    else
923      return 0;
924  }
925
926  /**
927    @brief Enables a specific key handler that has already been added.
928    @param name Unique name of the handler.
929    @return False if name was not found, true otherwise.
930  */
931  bool InputManager::enableKeyHandler(const std::string& name)
932  {
933    // get pointer from the map with all stored handlers
934    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
935    if (mapIt == _getSingleton().keyHandlers_.end())
936      return false;
937    // see whether the handler already is in the list
938    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
939          it != _getSingleton().activeKeyHandlers_.end(); it++)
940    {
941      if ((*it) == (*mapIt).second)
942      {
943        return true;
944      }
945    }
946    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
947    _getSingleton().stateRequest_ = IS_CUSTOM;
948    _getSingleton()._updateTickables();
949    return true;
950  }
951
952  /**
953    @brief Disables a specific key handler.
954    @param name Unique name of the handler.
955    @return False if name was not found, true otherwise.
956  */
957  bool InputManager::disableKeyHandler(const std::string &name)
958  {
959    // get pointer from the map with all stored handlers
960    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
961    if (mapIt == _getSingleton().keyHandlers_.end())
962      return false;
963    // look for the handler in the list
964    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
965          it != _getSingleton().activeKeyHandlers_.end(); it++)
966    {
967      if ((*it) == (*mapIt).second)
968      {
969        _getSingleton().activeKeyHandlers_.erase(it);
970        _getSingleton().stateRequest_ = IS_CUSTOM;
971        _getSingleton()._updateTickables();
972        return true;
973      }
974    }
975    return true;
976  }
977
978  /**
979    @brief Checks whether a key handler is active
980    @param name Unique name of the handler.
981    @return False if key handler is not active or doesn't exist, true otherwise.
982  */
983  bool InputManager::isKeyHandlerActive(const std::string& name)
984  {
985    // get pointer from the map with all stored handlers
986    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
987    if (mapIt == _getSingleton().keyHandlers_.end())
988      return false;
989    // see whether the handler already is in the list
990    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
991          it != _getSingleton().activeKeyHandlers_.end(); it++)
992    {
993      if ((*it) == (*mapIt).second)
994        return true;
995    }
996    return false;
997  }
998
999
1000  // ###### MouseHandler ######
1001  /**
1002    @brief Adds a new mouse handler.
1003    @param handler Pointer to the handler object.
1004    @param name Unique name of the handler.
1005    @return True if added, false if name already existed.
1006  */
1007  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
1008  {
1009    if (!handler)
1010      return false;
1011    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
1012    {
1013      _getSingleton().mouseHandlers_[name] = handler;
1014      return true;
1015    }
1016    else
1017      return false;
1018  }
1019
1020  /**
1021    @brief Removes a Mouse handler from the list.
1022    @param name Unique name of the handler.
1023    @return True if removal was successful, false if name was not found.
1024  */
1025  bool InputManager::removeMouseHandler(const std::string &name)
1026  {
1027    disableMouseHandler(name);
1028    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1029    if (it != _getSingleton().mouseHandlers_.end())
1030    {
1031      _getSingleton().mouseHandlers_.erase(it);
1032      return true;
1033    }
1034    else
1035      return false;
1036  }
1037
1038  /**
1039    @brief Returns the pointer to a handler.
1040    @param name Unique name of the handler.
1041    @return Pointer to the instance, 0 if name was not found.
1042  */
1043  MouseHandler* InputManager::getMouseHandler(const std::string& name)
1044  {
1045    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1046    if (it != _getSingleton().mouseHandlers_.end())
1047    {
1048      return (*it).second;
1049    }
1050    else
1051      return 0;
1052  }
1053
1054  /**
1055    @brief Enables a specific mouse handler that has already been added.
1056    @param name Unique name of the handler.
1057    @return False if name was not found, true otherwise.
1058  */
1059  bool InputManager::enableMouseHandler(const std::string& name)
1060  {
1061    // get pointer from the map with all stored handlers
1062    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1063    if (mapIt == _getSingleton().mouseHandlers_.end())
1064      return false;
1065    // see whether the handler already is in the list
1066    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1067          it != _getSingleton().activeMouseHandlers_.end(); it++)
1068    {
1069      if ((*it) == (*mapIt).second)
1070      {
1071        return true;
1072      }
1073    }
1074    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1075    _getSingleton().stateRequest_ = IS_CUSTOM;
1076    _getSingleton()._updateTickables();
1077    return true;
1078  }
1079
1080  /**
1081    @brief Disables a specific mouse handler.
1082    @param name Unique name of the handler.
1083    @return False if name was not found, true otherwise.
1084  */
1085  bool InputManager::disableMouseHandler(const std::string &name)
1086  {
1087    // get pointer from the map with all stored handlers
1088    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1089    if (mapIt == _getSingleton().mouseHandlers_.end())
1090      return false;
1091    // look for the handler in the list
1092    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1093          it != _getSingleton().activeMouseHandlers_.end(); it++)
1094    {
1095      if ((*it) == (*mapIt).second)
1096      {
1097        _getSingleton().activeMouseHandlers_.erase(it);
1098        _getSingleton().stateRequest_ = IS_CUSTOM;
1099        _getSingleton()._updateTickables();
1100        return true;
1101      }
1102    }
1103    return true;
1104  }
1105
1106  /**
1107    @brief Checks whether a mouse handler is active
1108    @param name Unique name of the handler.
1109    @return False if key handler is not active or doesn't exist, true otherwise.
1110  */
1111  bool InputManager::isMouseHandlerActive(const std::string& name)
1112  {
1113    // get pointer from the map with all stored handlers
1114    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1115    if (mapIt == _getSingleton().mouseHandlers_.end())
1116      return false;
1117    // see whether the handler already is in the list
1118    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1119          it != _getSingleton().activeMouseHandlers_.end(); it++)
1120    {
1121      if ((*it) == (*mapIt).second)
1122        return true;
1123    }
1124    return false;
1125  }
1126
1127
1128  // ###### JoyStickHandler ######
1129
1130  /**
1131    @brief Adds a new joy stick handler.
1132    @param handler Pointer to the handler object.
1133    @param name Unique name of the handler.
1134    @return True if added, false if name already existed.
1135  */
1136  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1137  {
1138    if (!handler)
1139      return false;
1140    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1141    {
1142      _getSingleton().joyStickHandlers_[name] = handler;
1143      return true;
1144    }
1145    else
1146      return false;
1147  }
1148
1149  /**
1150    @brief Removes a JoyStick handler from the list.
1151    @param name Unique name of the handler.
1152    @return True if removal was successful, false if name was not found.
1153  */
1154  bool InputManager::removeJoyStickHandler(const std::string &name)
1155  {
1156    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1157          itstick != _getSingleton().joySticks_.end(); itstick++)
1158      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1159
1160    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1161    if (it != _getSingleton().joyStickHandlers_.end())
1162    {
1163      _getSingleton().joyStickHandlers_.erase(it);
1164      return true;
1165    }
1166    else
1167      return false;
1168  }
1169
1170  /**
1171    @brief Returns the pointer to a handler.
1172    @param name Unique name of the handler.
1173    @return Pointer to the instance, 0 if name was not found.
1174  */
1175  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1176  {
1177    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1178    if (it != _getSingleton().joyStickHandlers_.end())
1179    {
1180      return (*it).second;
1181    }
1182    else
1183      return 0;
1184  }
1185
1186  /**
1187    @brief Enables a specific joy stick handler that has already been added.
1188    @param name Unique name of the handler.
1189    @return False if name or id was not found, true otherwise.
1190  */
1191  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1192  {
1193    // get handler pointer from the map with all stored handlers
1194    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1195    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1196      return false;
1197
1198    // check for existence of the ID
1199    if (ID >= _getSingleton().joySticksSize_)
1200      return false;
1201
1202    // see whether the handler already is in the list
1203    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1204          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1205    {
1206      if ((*it) == (*handlerIt).second)
1207      {
1208        return true;
1209      }
1210    }
1211    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1212    _getSingleton().stateRequest_ = IS_CUSTOM;
1213    _getSingleton()._updateTickables();
1214    return true;
1215  }
1216
1217  /**
1218    @brief Disables a specific joy stick handler.
1219    @param name Unique name of the handler.
1220    @return False if name or id was not found, true otherwise.
1221  */
1222  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1223  {
1224    // get handler pointer from the map with all stored handlers
1225    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1226    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1227      return false;
1228
1229    // check for existence of the ID
1230    if (ID >= _getSingleton().joySticksSize_)
1231      return false;
1232
1233    // look for the handler in the list
1234    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1235          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1236    {
1237      if ((*it) == (*handlerIt).second)
1238      {
1239        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1240        _getSingleton().stateRequest_ = IS_CUSTOM;
1241        _getSingleton()._updateTickables();
1242        return true;
1243      }
1244    }
1245    return true;
1246  }
1247
1248  /**
1249    @brief Checks whether a joy stick handler is active
1250    @param name Unique name of the handler.
1251    @return False if key handler is not active or doesn't exist, true otherwise.
1252  */
1253  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1254  {
1255    // get handler pointer from the map with all stored handlers
1256    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1257    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1258      return false;
1259
1260    // check for existence of the ID
1261    if (ID >= _getSingleton().joySticksSize_)
1262      return false;
1263
1264    // see whether the handler already is in the list
1265    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1266          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1267    {
1268      if ((*it) == (*handlerIt).second)
1269        return true;
1270    }
1271    return false;
1272  }
1273
1274}
Note: See TracBrowser for help on using the repository browser.