Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/InputManager.cc @ 1398

Last change on this file since 1398 was 1398, checked in by rgrieder, 16 years ago
  • removed the mouseHandler in SpaceShip
File size: 40.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      //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
101      //paramList.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
102//#if defined OIS_LINUX_PLATFORM
103//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
104//#endif
105
106      try
107      {
108        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
109        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
110      }
111      catch (OIS::Exception ex)
112      {
113        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
114            << "OIS message: \"" << ex.eText << "\"" << std::endl;
115        inputSystem_ = 0;
116        return false;
117      }
118
119      if (createKeyboard)
120        _initialiseKeyboard();
121
122      if (createMouse)
123        _initialiseMouse();
124
125      if (createJoySticks)
126        _initialiseJoySticks();
127
128      // Set mouse/joystick region
129      if (mouse_)
130      {
131        //// hack the mouse position
132        //((OIS::MouseState&)mouse_->getMouseState()).X.abs = windowWidth/2;
133        //((OIS::MouseState&)mouse_->getMouseState()).Y.abs = windowHeight/2;
134        setWindowExtents(windowWidth, windowHeight);
135      }
136
137      state_ = IS_NONE;
138      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
139    }
140    else
141    {
142      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
143    }
144
145    // InputManager holds the input buffer --> create one and add it.
146    addKeyHandler(new InputBuffer(), "buffer");
147
148    KeyBinder* binder = new KeyBinder();
149    binder->loadBindings();
150    addKeyHandler(binder, "keybinder");
151    addMouseHandler(binder, "keybinder");
152    addJoyStickHandler(binder, "keybinder");
153
154    // Read all the key bindings and assign them
155    //if (!_loadBindings())
156    //  return false;
157
158    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
159    return true;
160  }
161
162  /**
163    @brief Creates a keyboard and sets the event handler.
164    @return False if keyboard stays uninitialised, true otherwise.
165  */
166  bool InputManager::_initialiseKeyboard()
167  {
168    if (keyboard_ != 0)
169    {
170      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
171      return true;
172    }
173    try
174    {
175      if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
176      {
177        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
178        // register our listener in OIS.
179        keyboard_->setEventCallback(this);
180        // note: OIS will not detect keys that have already been down when the keyboard was created.
181        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
182        return true;
183      }
184      else
185      {
186        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
187        return false;
188      }
189    }
190    catch (OIS::Exception ex)
191    {
192      // TODO: Test this output regarding formatting
193      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
194          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
195      keyboard_ = 0;
196      return false;
197    }
198  }
199
200  /**
201    @brief Creates a mouse and sets the event handler.
202    @return False if mouse stays uninitialised, true otherwise.
203  */
204  bool InputManager::_initialiseMouse()
205  {
206    if (mouse_ != 0)
207    {
208      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
209      return true;
210    }
211    try
212    {
213      if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
214      {
215        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
216        // register our listener in OIS.
217        mouse_->setEventCallback(this);
218        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
219        return true;
220      }
221      else
222      {
223        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
224        return false;
225      }
226    }
227    catch (OIS::Exception ex)
228    {
229      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
230          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
231      mouse_ = 0;
232      return false;
233    }
234  }
235
236  /**
237    @brief Creates all joy sticks and sets the event handler.
238    @return False joy stick stay uninitialised, true otherwise.
239  */
240  bool InputManager::_initialiseJoySticks()
241  {
242    if (joySticksSize_ > 0)
243    {
244      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
245      return true;
246    }
247    bool success = false;
248    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
249    {
250      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
251      {
252        try
253        {
254          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
255          joySticks_.push_back(stig);
256          // register our listener in OIS.
257          stig->setEventCallback(this);
258          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
259          success = true;
260        }
261        catch (OIS::Exception ex)
262        {
263          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
264              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
265        }
266      }
267    }
268    else
269    {
270      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
271      return false;
272    }
273    joySticksSize_ = joySticks_.size();
274    activeJoyStickHandlers_.resize(joySticksSize_);
275    joyStickButtonsDown_.resize(joySticksSize_);
276    povStates_.resize(joySticksSize_);
277    sliderStates_.resize(joySticksSize_);
278    return success;
279  }
280
281  /**
282    @brief Destroys all the created input devices and sets the InputManager to construction state.
283  */
284  void InputManager::_destroy()
285  {
286    if (state_ != IS_UNINIT)
287    {
288      CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
289
290      if (keyHandlers_.find("buffer") != keyHandlers_.end())
291        delete keyHandlers_["buffer"];
292
293      if (keyHandlers_.find("keybinder") != keyHandlers_.end())
294        delete keyHandlers_["keybinder"];
295
296      keyHandlers_.clear();
297      mouseHandlers_.clear();
298      joyStickHandlers_.clear();
299
300      _destroyKeyboard();
301      _destroyMouse();
302      _destroyJoySticks();
303
304      // inputSystem_ can never be 0, or else the code is mistaken
305      OIS::InputManager::destroyInputSystem(inputSystem_);
306      inputSystem_ = 0;
307
308      state_ = IS_UNINIT;
309      CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
310    }
311  }
312
313  /**
314    @brief Destroys the keyboard and sets it to 0.
315  */
316  void InputManager::_destroyKeyboard()
317  {
318    if (keyboard_)
319      // inputSystem_ can never be 0, or else the code is mistaken
320      inputSystem_->destroyInputObject(keyboard_);
321    keyboard_ = 0;
322    activeKeyHandlers_.clear();
323    keysDown_.clear();
324    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
325  }
326
327  /**
328    @brief Destroys the mouse and sets it to 0.
329  */
330  void InputManager::_destroyMouse()
331  {
332    if (mouse_)
333      // inputSystem_ can never be 0, or else the code is mistaken
334      inputSystem_->destroyInputObject(mouse_);
335    mouse_ = 0;
336    activeMouseHandlers_.clear();
337    mouseButtonsDown_.clear();
338    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
339  }
340
341  /**
342    @brief Destroys all the joy sticks and resizes the lists to 0.
343  */
344  void InputManager::_destroyJoySticks()
345  {
346    if (joySticksSize_ > 0)
347    {
348      // note: inputSystem_ can never be 0, or else the code is mistaken
349      for (unsigned int i = 0; i < joySticksSize_; i++)
350        if (joySticks_[i] != 0)
351          inputSystem_->destroyInputObject(joySticks_[i]);
352
353      joySticks_.clear();
354      joySticksSize_ = 0;
355      activeJoyStickHandlers_.clear();
356      joyStickButtonsDown_.clear();
357    }
358    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
359  }
360
361  void InputManager::_updateTickables()
362  {
363    // we can use a set to have a list of unique pointers (an object can implement all 3 handlers)
364    std::set<InputTickable*> tempSet;
365    for (unsigned int iHandler = 0; iHandler < activeKeyHandlers_.size(); iHandler++)
366      tempSet.insert(activeKeyHandlers_[iHandler]);
367    for (unsigned int iHandler = 0; iHandler < activeMouseHandlers_.size(); iHandler++)
368      tempSet.insert(activeMouseHandlers_[iHandler]);
369    for (unsigned int iJoyStick  = 0; iJoyStick < joySticksSize_; iJoyStick++)
370      for (unsigned int iHandler = 0; iHandler  < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
371        tempSet.insert(activeJoyStickHandlers_[iJoyStick][iHandler]);
372
373    // copy the content of the set back to the actual vector
374    activeHandlers_.clear();
375    for (std::set<InputTickable*>::const_iterator itHandler = tempSet.begin(); itHandler != tempSet.end(); itHandler++)
376      activeHandlers_.push_back(*itHandler);
377  }
378
379
380  // #################################
381  // ### Private Interface Methods ###
382  // #################################
383  // #################################
384
385  /**
386    @brief Updates the InputManager. Tick is called by Orxonox.
387    @param dt Delta time
388  */
389  void InputManager::tick(float dt)
390  {
391    if (state_ == IS_UNINIT)
392      return;
393
394    // reset the game if it has changed
395    if (state_ != stateRequest_)
396    {
397      if (stateRequest_ != IS_CUSTOM)
398      {
399        activeKeyHandlers_.clear();
400        activeMouseHandlers_.clear();
401        for (unsigned int i = 0; i < joySticksSize_; i++)
402          activeJoyStickHandlers_[i].clear();
403
404        switch (stateRequest_)
405        {
406        case IS_NORMAL:
407          // normal play mode
408          // note: we assume that the handlers exist since otherwise, something's wrong anyway.
409          enableKeyHandler("keybinder");
410          enableMouseHandler("keybinder");
411          enableJoyStickHandler("keybinder", 0);
412          break;
413
414        case IS_GUI:
415          // TODO: do stuff
416          break;
417
418        case IS_CONSOLE:
419          enableMouseHandler("keybinder");
420          enableJoyStickHandler("keybinder", 0);
421          enableKeyHandler("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    //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000)
644    //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;}
645    // use the device to identify which one called the method
646    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
647    unsigned int iJoyStick = 0;
648    while (joySticks_[iJoyStick] != joyStick)
649      iJoyStick++;
650
651    // keep in mind that the first 8 axes are reserved for the sliders
652    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
653      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis + 8, arg.state.mAxes[axis].abs);
654
655    return true;
656  }
657
658  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
659  {
660    //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000)
661    //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;}
662    //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl;
663    // use the device to identify which one called the method
664    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
665    unsigned int iJoyStick = 0;
666    while (joySticks_[iJoyStick] != joyStick)
667      iJoyStick++;
668
669    if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX)
670    {
671      // slider X axis changed
672      sliderStates_[iJoyStick].sliderStates[id].x = arg.state.mSliders[id].abX;
673      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
674        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, id * 2, arg.state.mSliders[id].abX);
675    }
676    else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY)
677    {
678      // slider Y axis changed
679      sliderStates_[iJoyStick].sliderStates[id].y = arg.state.mSliders[id].abY;
680      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
681        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY);
682    }
683
684    return true;
685  }
686
687  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
688  {
689    // use the device to identify which one called the method
690    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
691    unsigned int iJoyStick = 0;
692    while (joySticks_[iJoyStick] != joyStick)
693      iJoyStick++;
694
695    // translate the POV into 8 simple buttons
696    int lastState = povStates_[iJoyStick][id];
697    if (lastState & OIS::Pov::North)
698      buttonReleased(arg, 32 + id * 4 + 0);
699    if (lastState & OIS::Pov::South)
700      buttonReleased(arg, 32 + id * 4 + 1);
701    if (lastState & OIS::Pov::East)
702      buttonReleased(arg, 32 + id * 4 + 2);
703    if (lastState & OIS::Pov::West)
704      buttonReleased(arg, 32 + id * 4 + 3);
705   
706    povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction;
707    int currentState = povStates_[iJoyStick][id];
708    if (currentState & OIS::Pov::North)
709      buttonPressed(arg, 32 + id * 4 + 0);
710    if (currentState & OIS::Pov::South)
711      buttonPressed(arg, 32 + id * 4 + 1);
712    if (currentState & OIS::Pov::East)
713      buttonPressed(arg, 32 + id * 4 + 2);
714    if (currentState & OIS::Pov::West)
715      buttonPressed(arg, 32 + id * 4 + 3);
716
717    return true;
718  }
719
720  /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
721  {
722    // use the device to identify which one called the method
723    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
724    unsigned int iJoyStick = 0;
725    while (joySticks_[iJoyStick] != joyStick)
726      iJoyStick++;
727
728    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
729      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
730
731    return true;
732  }*/
733
734
735  // ################################
736  // ### Static Interface Methods ###
737  // ################################
738  // ################################
739
740  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
741    bool createKeyboard, bool createMouse, bool createJoySticks)
742  {
743    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
744          createKeyboard, createMouse, createJoySticks);
745  }
746
747  bool InputManager::initialiseKeyboard()
748  {
749    return _getSingleton()._initialiseKeyboard();
750  }
751
752  bool InputManager::initialiseMouse()
753  {
754    return _getSingleton()._initialiseMouse();
755  }
756
757  bool InputManager::initialiseJoySticks()
758  {
759    return _getSingleton()._initialiseJoySticks();
760  }
761
762  int InputManager::numberOfKeyboards()
763  {
764    if (_getSingleton().keyboard_ != 0)
765      return 1;
766    else
767      return 0;
768  }
769
770  int InputManager::numberOfMice()
771  {
772    if (_getSingleton().mouse_ != 0)
773      return 1;
774    else
775      return 0;
776  }
777
778  int InputManager::numberOfJoySticks()
779  {
780    return _getSingleton().joySticksSize_;
781  }
782
783  bool InputManager::isKeyDown(KeyCode::Enum key)
784  {
785    if (_getSingleton().keyboard_)
786      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
787    else
788      return false;
789  }
790
791  bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
792  {
793    if (_getSingleton().keyboard_)
794      return isModifierDown(modifier);
795    else
796      return false;
797  }
798
799  /*const MouseState InputManager::getMouseState()
800  {
801    if (_getSingleton().mouse_)
802      return _getSingleton().mouse_->getMouseState();
803    else
804      return MouseState();
805  }*/
806
807  /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
808  {
809    if (ID < _getSingleton().joySticksSize_)
810      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
811    else
812      return JoyStickState();
813  }*/
814
815
816  void InputManager::destroy()
817  {
818    _getSingleton()._destroy();
819  }
820
821  void InputManager::destroyKeyboard()
822  {
823    return _getSingleton()._destroyKeyboard();
824  }
825
826  void InputManager::destroyMouse()
827  {
828    return _getSingleton()._destroyMouse();
829  }
830
831  void InputManager::destroyJoySticks()
832  {
833    return _getSingleton()._destroyJoySticks();
834  }
835
836
837  /**
838    @brief Adjusts the mouse window metrics.
839    This method has to be called every time the size of the window changes.
840    @param width The new width of the render window
841    @param height the new height of the render window
842  */
843  void InputManager::setWindowExtents(const int width, const int height)
844  {
845    if (_getSingleton().mouse_)
846    {
847      // Set mouse region (if window resizes, we should alter this to reflect as well)
848      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
849      mouseState.width  = width;
850      mouseState.height = height;
851    }
852  }
853
854  /**
855    @brief Sets the input mode to either GUI, inGame or Buffer
856    @param mode The new input mode
857    @remark Only has an affect if the mode actually changes
858  */
859  void InputManager::setInputState(const InputState state)
860  {
861    _getSingleton().stateRequest_ = state;
862  }
863
864  /**
865    @brief Returns the current input handling method
866    @return The current input mode.
867  */
868  InputManager::InputState InputManager::getInputState()
869  {
870    return _getSingleton().state_;
871  }
872
873
874  // ###### KeyHandler ######
875
876  /**
877    @brief Adds a new key handler.
878    @param handler Pointer to the handler object.
879    @param name Unique name of the handler.
880    @return True if added, false if name already existed.
881  */
882  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
883  {
884    if (!handler)
885      return false;
886    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
887    {
888      _getSingleton().keyHandlers_[name] = handler;
889      return true;
890    }
891    else
892      return false;
893  }
894
895  /**
896    @brief Removes a Key handler from the list.
897    @param name Unique name of the handler.
898    @return True if removal was successful, false if name was not found.
899  */
900  bool InputManager::removeKeyHandler(const std::string &name)
901  {
902    disableKeyHandler(name);
903    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
904    if (it != _getSingleton().keyHandlers_.end())
905    {
906      _getSingleton().keyHandlers_.erase(it);
907      return true;
908    }
909    else
910      return false;
911  }
912
913  /**
914    @brief Returns the pointer to a handler.
915    @param name Unique name of the handler.
916    @return Pointer to the instance, 0 if name was not found.
917  */
918  KeyHandler* InputManager::getKeyHandler(const std::string& name)
919  {
920    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
921    if (it != _getSingleton().keyHandlers_.end())
922    {
923      return (*it).second;
924    }
925    else
926      return 0;
927  }
928
929  /**
930    @brief Enables a specific key handler that has already been added.
931    @param name Unique name of the handler.
932    @return False if name was not found, true otherwise.
933  */
934  bool InputManager::enableKeyHandler(const std::string& name)
935  {
936    // get pointer from the map with all stored handlers
937    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
938    if (mapIt == _getSingleton().keyHandlers_.end())
939      return false;
940    // see whether the handler already is in the list
941    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
942          it != _getSingleton().activeKeyHandlers_.end(); it++)
943    {
944      if ((*it) == (*mapIt).second)
945      {
946        return true;
947      }
948    }
949    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
950    _getSingleton().stateRequest_ = IS_CUSTOM;
951    _getSingleton()._updateTickables();
952    return true;
953  }
954
955  /**
956    @brief Disables a specific key handler.
957    @param name Unique name of the handler.
958    @return False if name was not found, true otherwise.
959  */
960  bool InputManager::disableKeyHandler(const std::string &name)
961  {
962    // get pointer from the map with all stored handlers
963    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
964    if (mapIt == _getSingleton().keyHandlers_.end())
965      return false;
966    // look for the handler in the list
967    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
968          it != _getSingleton().activeKeyHandlers_.end(); it++)
969    {
970      if ((*it) == (*mapIt).second)
971      {
972        _getSingleton().activeKeyHandlers_.erase(it);
973        _getSingleton().stateRequest_ = IS_CUSTOM;
974        _getSingleton()._updateTickables();
975        return true;
976      }
977    }
978    return true;
979  }
980
981  /**
982    @brief Checks whether a key handler is active
983    @param name Unique name of the handler.
984    @return False if key handler is not active or doesn't exist, true otherwise.
985  */
986  bool InputManager::isKeyHandlerActive(const std::string& name)
987  {
988    // get pointer from the map with all stored handlers
989    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
990    if (mapIt == _getSingleton().keyHandlers_.end())
991      return false;
992    // see whether the handler already is in the list
993    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
994          it != _getSingleton().activeKeyHandlers_.end(); it++)
995    {
996      if ((*it) == (*mapIt).second)
997        return true;
998    }
999    return false;
1000  }
1001
1002
1003  // ###### MouseHandler ######
1004  /**
1005    @brief Adds a new mouse handler.
1006    @param handler Pointer to the handler object.
1007    @param name Unique name of the handler.
1008    @return True if added, false if name already existed.
1009  */
1010  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
1011  {
1012    if (!handler)
1013      return false;
1014    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
1015    {
1016      _getSingleton().mouseHandlers_[name] = handler;
1017      return true;
1018    }
1019    else
1020      return false;
1021  }
1022
1023  /**
1024    @brief Removes a Mouse handler from the list.
1025    @param name Unique name of the handler.
1026    @return True if removal was successful, false if name was not found.
1027  */
1028  bool InputManager::removeMouseHandler(const std::string &name)
1029  {
1030    disableMouseHandler(name);
1031    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1032    if (it != _getSingleton().mouseHandlers_.end())
1033    {
1034      _getSingleton().mouseHandlers_.erase(it);
1035      return true;
1036    }
1037    else
1038      return false;
1039  }
1040
1041  /**
1042    @brief Returns the pointer to a handler.
1043    @param name Unique name of the handler.
1044    @return Pointer to the instance, 0 if name was not found.
1045  */
1046  MouseHandler* InputManager::getMouseHandler(const std::string& name)
1047  {
1048    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1049    if (it != _getSingleton().mouseHandlers_.end())
1050    {
1051      return (*it).second;
1052    }
1053    else
1054      return 0;
1055  }
1056
1057  /**
1058    @brief Enables a specific mouse handler that has already been added.
1059    @param name Unique name of the handler.
1060    @return False if name was not found, true otherwise.
1061  */
1062  bool InputManager::enableMouseHandler(const std::string& name)
1063  {
1064    // get pointer from the map with all stored handlers
1065    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1066    if (mapIt == _getSingleton().mouseHandlers_.end())
1067      return false;
1068    // see whether the handler already is in the list
1069    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1070          it != _getSingleton().activeMouseHandlers_.end(); it++)
1071    {
1072      if ((*it) == (*mapIt).second)
1073      {
1074        return true;
1075      }
1076    }
1077    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1078    _getSingleton().stateRequest_ = IS_CUSTOM;
1079    _getSingleton()._updateTickables();
1080    return true;
1081  }
1082
1083  /**
1084    @brief Disables a specific mouse handler.
1085    @param name Unique name of the handler.
1086    @return False if name was not found, true otherwise.
1087  */
1088  bool InputManager::disableMouseHandler(const std::string &name)
1089  {
1090    // get pointer from the map with all stored handlers
1091    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1092    if (mapIt == _getSingleton().mouseHandlers_.end())
1093      return false;
1094    // look for the handler in the list
1095    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1096          it != _getSingleton().activeMouseHandlers_.end(); it++)
1097    {
1098      if ((*it) == (*mapIt).second)
1099      {
1100        _getSingleton().activeMouseHandlers_.erase(it);
1101        _getSingleton().stateRequest_ = IS_CUSTOM;
1102        _getSingleton()._updateTickables();
1103        return true;
1104      }
1105    }
1106    return true;
1107  }
1108
1109  /**
1110    @brief Checks whether a mouse handler is active
1111    @param name Unique name of the handler.
1112    @return False if key handler is not active or doesn't exist, true otherwise.
1113  */
1114  bool InputManager::isMouseHandlerActive(const std::string& name)
1115  {
1116    // get pointer from the map with all stored handlers
1117    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1118    if (mapIt == _getSingleton().mouseHandlers_.end())
1119      return false;
1120    // see whether the handler already is in the list
1121    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1122          it != _getSingleton().activeMouseHandlers_.end(); it++)
1123    {
1124      if ((*it) == (*mapIt).second)
1125        return true;
1126    }
1127    return false;
1128  }
1129
1130
1131  // ###### JoyStickHandler ######
1132
1133  /**
1134    @brief Adds a new joy stick handler.
1135    @param handler Pointer to the handler object.
1136    @param name Unique name of the handler.
1137    @return True if added, false if name already existed.
1138  */
1139  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1140  {
1141    if (!handler)
1142      return false;
1143    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1144    {
1145      _getSingleton().joyStickHandlers_[name] = handler;
1146      return true;
1147    }
1148    else
1149      return false;
1150  }
1151
1152  /**
1153    @brief Removes a JoyStick handler from the list.
1154    @param name Unique name of the handler.
1155    @return True if removal was successful, false if name was not found.
1156  */
1157  bool InputManager::removeJoyStickHandler(const std::string &name)
1158  {
1159    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1160          itstick != _getSingleton().joySticks_.end(); itstick++)
1161      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1162
1163    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1164    if (it != _getSingleton().joyStickHandlers_.end())
1165    {
1166      _getSingleton().joyStickHandlers_.erase(it);
1167      return true;
1168    }
1169    else
1170      return false;
1171  }
1172
1173  /**
1174    @brief Returns the pointer to a handler.
1175    @param name Unique name of the handler.
1176    @return Pointer to the instance, 0 if name was not found.
1177  */
1178  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1179  {
1180    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1181    if (it != _getSingleton().joyStickHandlers_.end())
1182    {
1183      return (*it).second;
1184    }
1185    else
1186      return 0;
1187  }
1188
1189  /**
1190    @brief Enables a specific joy stick handler that has already been added.
1191    @param name Unique name of the handler.
1192    @return False if name or id was not found, true otherwise.
1193  */
1194  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1195  {
1196    // get handler pointer from the map with all stored handlers
1197    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1198    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1199      return false;
1200
1201    // check for existence of the ID
1202    if (ID >= _getSingleton().joySticksSize_)
1203      return false;
1204
1205    // see whether the handler already is in the list
1206    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1207          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1208    {
1209      if ((*it) == (*handlerIt).second)
1210      {
1211        return true;
1212      }
1213    }
1214    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1215    _getSingleton().stateRequest_ = IS_CUSTOM;
1216    _getSingleton()._updateTickables();
1217    return true;
1218  }
1219
1220  /**
1221    @brief Disables a specific joy stick handler.
1222    @param name Unique name of the handler.
1223    @return False if name or id was not found, true otherwise.
1224  */
1225  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1226  {
1227    // get handler pointer from the map with all stored handlers
1228    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1229    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1230      return false;
1231
1232    // check for existence of the ID
1233    if (ID >= _getSingleton().joySticksSize_)
1234      return false;
1235
1236    // look for the handler in the list
1237    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1238          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1239    {
1240      if ((*it) == (*handlerIt).second)
1241      {
1242        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1243        _getSingleton().stateRequest_ = IS_CUSTOM;
1244        _getSingleton()._updateTickables();
1245        return true;
1246      }
1247    }
1248    return true;
1249  }
1250
1251  /**
1252    @brief Checks whether a joy stick handler is active
1253    @param name Unique name of the handler.
1254    @return False if key handler is not active or doesn't exist, true otherwise.
1255  */
1256  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1257  {
1258    // get handler pointer from the map with all stored handlers
1259    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1260    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1261      return false;
1262
1263    // check for existence of the ID
1264    if (ID >= _getSingleton().joySticksSize_)
1265      return false;
1266
1267    // see whether the handler already is in the list
1268    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1269          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1270    {
1271      if ((*it) == (*handlerIt).second)
1272        return true;
1273    }
1274    return false;
1275  }
1276
1277}
Note: See TracBrowser for help on using the repository browser.