Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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