Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/InputManager.cc @ 1396

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