Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1391 was 1391, checked in by rgrieder, 16 years ago
  • fixed some bugs
File size: 40.8 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    //if (arg.state.mAxes[axis].abs > 10000 || arg.state.mAxes[axis].abs < -10000)
646    //{ CCOUT(3) << "axis " << axis << " moved" << arg.state.mAxes[axis].abs << std::endl;}
647    // use the device to identify which one called the method
648    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
649    unsigned int iJoyStick = 0;
650    while (joySticks_[iJoyStick] != joyStick)
651      iJoyStick++;
652
653    // keep in mind that the first 8 axes are reserved for the sliders
654    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
655      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, axis + 8, arg.state.mAxes[axis].abs);
656
657    return true;
658  }
659
660  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
661  {
662    //if (arg.state.mSliders[id].abX > 10000 || arg.state.mSliders[id].abX < -10000)
663    //{CCOUT(3) << "slider " << id << " moved" << arg.state.mSliders[id].abX << std::endl;}
664    //CCOUT(3) << arg.state.mSliders[id].abX << "\t |" << arg.state.mSliders[id].abY << std::endl;
665    // use the device to identify which one called the method
666    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
667    unsigned int iJoyStick = 0;
668    while (joySticks_[iJoyStick] != joyStick)
669      iJoyStick++;
670
671    if (sliderStates_[iJoyStick].sliderStates[id].x != arg.state.mSliders[id].abX)
672    {
673      // slider X axis changed
674      sliderStates_[iJoyStick].sliderStates[id].x = arg.state.mSliders[id].abX;
675      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
676        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, id * 2, arg.state.mSliders[id].abX);
677    }
678    else if (sliderStates_[iJoyStick].sliderStates[id].y != arg.state.mSliders[id].abY)
679    {
680      // slider Y axis changed
681      sliderStates_[iJoyStick].sliderStates[id].y = arg.state.mSliders[id].abY;
682      for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
683        activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickAxisMoved(iJoyStick, id * 2 + 1, arg.state.mSliders[id].abY);
684    }
685
686    return true;
687  }
688
689  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
690  {
691    // use the device to identify which one called the method
692    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
693    unsigned int iJoyStick = 0;
694    while (joySticks_[iJoyStick] != joyStick)
695      iJoyStick++;
696
697    // translate the POV into 8 simple buttons
698    int lastState = povStates_[iJoyStick][id];
699    if (lastState & OIS::Pov::North)
700      buttonReleased(arg, 32 + id * 4 + 0);
701    if (lastState & OIS::Pov::South)
702      buttonReleased(arg, 32 + id * 4 + 1);
703    if (lastState & OIS::Pov::East)
704      buttonReleased(arg, 32 + id * 4 + 2);
705    if (lastState & OIS::Pov::West)
706      buttonReleased(arg, 32 + id * 4 + 3);
707   
708    povStates_[iJoyStick].povStates[id] = arg.state.mPOV[id].direction;
709    int currentState = povStates_[iJoyStick][id];
710    if (currentState & OIS::Pov::North)
711      buttonPressed(arg, 32 + id * 4 + 0);
712    if (currentState & OIS::Pov::South)
713      buttonPressed(arg, 32 + id * 4 + 1);
714    if (currentState & OIS::Pov::East)
715      buttonPressed(arg, 32 + id * 4 + 2);
716    if (currentState & OIS::Pov::West)
717      buttonPressed(arg, 32 + id * 4 + 3);
718
719    return true;
720  }
721
722  /*bool InputManager::vector3Moved(const OIS::JoyStickEvent &arg, int id)
723  {
724    // use the device to identify which one called the method
725    OIS::JoyStick* joyStick = (OIS::JoyStick*)arg.device;
726    unsigned int iJoyStick = 0;
727    while (joySticks_[iJoyStick] != joyStick)
728      iJoyStick++;
729
730    for (unsigned int iHandler = 0; iHandler < activeJoyStickHandlers_[iJoyStick].size(); iHandler++)
731      activeJoyStickHandlers_[iJoyStick][iHandler]->joyStickVector3Moved(JoyStickState(arg.state, iJoyStick), id);
732
733    return true;
734  }*/
735
736
737  // ################################
738  // ### Static Interface Methods ###
739  // ################################
740  // ################################
741
742  bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
743    bool createKeyboard, bool createMouse, bool createJoySticks)
744  {
745    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
746          createKeyboard, createMouse, createJoySticks);
747  }
748
749  bool InputManager::initialiseKeyboard()
750  {
751    return _getSingleton()._initialiseKeyboard();
752  }
753
754  bool InputManager::initialiseMouse()
755  {
756    return _getSingleton()._initialiseMouse();
757  }
758
759  bool InputManager::initialiseJoySticks()
760  {
761    return _getSingleton()._initialiseJoySticks();
762  }
763
764  int InputManager::numberOfKeyboards()
765  {
766    if (_getSingleton().keyboard_ != 0)
767      return 1;
768    else
769      return 0;
770  }
771
772  int InputManager::numberOfMice()
773  {
774    if (_getSingleton().mouse_ != 0)
775      return 1;
776    else
777      return 0;
778  }
779
780  int InputManager::numberOfJoySticks()
781  {
782    return _getSingleton().joySticksSize_;
783  }
784
785  bool InputManager::isKeyDown(KeyCode::Enum key)
786  {
787    if (_getSingleton().keyboard_)
788      return _getSingleton().keyboard_->isKeyDown((OIS::KeyCode)key);
789    else
790      return false;
791  }
792
793  bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
794  {
795    if (_getSingleton().keyboard_)
796      return isModifierDown(modifier);
797    else
798      return false;
799  }
800
801  /*const MouseState InputManager::getMouseState()
802  {
803    if (_getSingleton().mouse_)
804      return _getSingleton().mouse_->getMouseState();
805    else
806      return MouseState();
807  }*/
808
809  /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
810  {
811    if (ID < _getSingleton().joySticksSize_)
812      return JoyStickState(_getSingleton().joySticks_[ID]->getJoyStickState(), ID);
813    else
814      return JoyStickState();
815  }*/
816
817
818  void InputManager::destroy()
819  {
820    _getSingleton()._destroy();
821  }
822
823  void InputManager::destroyKeyboard()
824  {
825    return _getSingleton()._destroyKeyboard();
826  }
827
828  void InputManager::destroyMouse()
829  {
830    return _getSingleton()._destroyMouse();
831  }
832
833  void InputManager::destroyJoySticks()
834  {
835    return _getSingleton()._destroyJoySticks();
836  }
837
838
839  /**
840    @brief Adjusts the mouse window metrics.
841    This method has to be called every time the size of the window changes.
842    @param width The new width of the render window
843    @param height the new height of the render window
844  */
845  void InputManager::setWindowExtents(const int width, const int height)
846  {
847    if (_getSingleton().mouse_)
848    {
849      // Set mouse region (if window resizes, we should alter this to reflect as well)
850      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
851      mouseState.width  = width;
852      mouseState.height = height;
853    }
854  }
855
856  /**
857    @brief Sets the input mode to either GUI, inGame or Buffer
858    @param mode The new input mode
859    @remark Only has an affect if the mode actually changes
860  */
861  void InputManager::setInputState(const InputState state)
862  {
863    _getSingleton().stateRequest_ = state;
864  }
865
866  /**
867    @brief Returns the current input handling method
868    @return The current input mode.
869  */
870  InputManager::InputState InputManager::getInputState()
871  {
872    return _getSingleton().state_;
873  }
874
875
876  // ###### KeyHandler ######
877
878  /**
879    @brief Adds a new key handler.
880    @param handler Pointer to the handler object.
881    @param name Unique name of the handler.
882    @return True if added, false if name already existed.
883  */
884  bool InputManager::addKeyHandler(KeyHandler* handler, const std::string& name)
885  {
886    if (!handler)
887      return false;
888    if (_getSingleton().keyHandlers_.find(name) == _getSingleton().keyHandlers_.end())
889    {
890      _getSingleton().keyHandlers_[name] = handler;
891      return true;
892    }
893    else
894      return false;
895  }
896
897  /**
898    @brief Removes a Key handler from the list.
899    @param name Unique name of the handler.
900    @return True if removal was successful, false if name was not found.
901  */
902  bool InputManager::removeKeyHandler(const std::string &name)
903  {
904    disableKeyHandler(name);
905    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
906    if (it != _getSingleton().keyHandlers_.end())
907    {
908      _getSingleton().keyHandlers_.erase(it);
909      return true;
910    }
911    else
912      return false;
913  }
914
915  /**
916    @brief Returns the pointer to a handler.
917    @param name Unique name of the handler.
918    @return Pointer to the instance, 0 if name was not found.
919  */
920  KeyHandler* InputManager::getKeyHandler(const std::string& name)
921  {
922    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().keyHandlers_.find(name);
923    if (it != _getSingleton().keyHandlers_.end())
924    {
925      return (*it).second;
926    }
927    else
928      return 0;
929  }
930
931  /**
932    @brief Enables a specific key handler that has already been added.
933    @param name Unique name of the handler.
934    @return False if name was not found, true otherwise.
935  */
936  bool InputManager::enableKeyHandler(const std::string& name)
937  {
938    // get pointer from the map with all stored handlers
939    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
940    if (mapIt == _getSingleton().keyHandlers_.end())
941      return false;
942    // see whether the handler already is in the list
943    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
944          it != _getSingleton().activeKeyHandlers_.end(); it++)
945    {
946      if ((*it) == (*mapIt).second)
947      {
948        return true;
949      }
950    }
951    _getSingleton().activeKeyHandlers_.push_back((*mapIt).second);
952    _getSingleton().stateRequest_ = IS_CUSTOM;
953    _getSingleton()._updateTickables();
954    return true;
955  }
956
957  /**
958    @brief Disables a specific key handler.
959    @param name Unique name of the handler.
960    @return False if name was not found, true otherwise.
961  */
962  bool InputManager::disableKeyHandler(const std::string &name)
963  {
964    // get pointer from the map with all stored handlers
965    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
966    if (mapIt == _getSingleton().keyHandlers_.end())
967      return false;
968    // look for the handler in the list
969    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
970          it != _getSingleton().activeKeyHandlers_.end(); it++)
971    {
972      if ((*it) == (*mapIt).second)
973      {
974        _getSingleton().activeKeyHandlers_.erase(it);
975        _getSingleton().stateRequest_ = IS_CUSTOM;
976        _getSingleton()._updateTickables();
977        return true;
978      }
979    }
980    return true;
981  }
982
983  /**
984    @brief Checks whether a key handler is active
985    @param name Unique name of the handler.
986    @return False if key handler is not active or doesn't exist, true otherwise.
987  */
988  bool InputManager::isKeyHandlerActive(const std::string& name)
989  {
990    // get pointer from the map with all stored handlers
991    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().keyHandlers_.find(name);
992    if (mapIt == _getSingleton().keyHandlers_.end())
993      return false;
994    // see whether the handler already is in the list
995    for (std::vector<KeyHandler*>::iterator it = _getSingleton().activeKeyHandlers_.begin();
996          it != _getSingleton().activeKeyHandlers_.end(); it++)
997    {
998      if ((*it) == (*mapIt).second)
999        return true;
1000    }
1001    return false;
1002  }
1003
1004
1005  // ###### MouseHandler ######
1006  /**
1007    @brief Adds a new mouse handler.
1008    @param handler Pointer to the handler object.
1009    @param name Unique name of the handler.
1010    @return True if added, false if name already existed.
1011  */
1012  bool InputManager::addMouseHandler(MouseHandler* handler, const std::string& name)
1013  {
1014    if (!handler)
1015      return false;
1016    if (_getSingleton().mouseHandlers_.find(name) == _getSingleton().mouseHandlers_.end())
1017    {
1018      _getSingleton().mouseHandlers_[name] = handler;
1019      return true;
1020    }
1021    else
1022      return false;
1023  }
1024
1025  /**
1026    @brief Removes a Mouse handler from the list.
1027    @param name Unique name of the handler.
1028    @return True if removal was successful, false if name was not found.
1029  */
1030  bool InputManager::removeMouseHandler(const std::string &name)
1031  {
1032    disableMouseHandler(name);
1033    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1034    if (it != _getSingleton().mouseHandlers_.end())
1035    {
1036      _getSingleton().mouseHandlers_.erase(it);
1037      return true;
1038    }
1039    else
1040      return false;
1041  }
1042
1043  /**
1044    @brief Returns the pointer to a handler.
1045    @param name Unique name of the handler.
1046    @return Pointer to the instance, 0 if name was not found.
1047  */
1048  MouseHandler* InputManager::getMouseHandler(const std::string& name)
1049  {
1050    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().mouseHandlers_.find(name);
1051    if (it != _getSingleton().mouseHandlers_.end())
1052    {
1053      return (*it).second;
1054    }
1055    else
1056      return 0;
1057  }
1058
1059  /**
1060    @brief Enables a specific mouse handler that has already been added.
1061    @param name Unique name of the handler.
1062    @return False if name was not found, true otherwise.
1063  */
1064  bool InputManager::enableMouseHandler(const std::string& name)
1065  {
1066    // get pointer from the map with all stored handlers
1067    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1068    if (mapIt == _getSingleton().mouseHandlers_.end())
1069      return false;
1070    // see whether the handler already is in the list
1071    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1072          it != _getSingleton().activeMouseHandlers_.end(); it++)
1073    {
1074      if ((*it) == (*mapIt).second)
1075      {
1076        return true;
1077      }
1078    }
1079    _getSingleton().activeMouseHandlers_.push_back((*mapIt).second);
1080    _getSingleton().stateRequest_ = IS_CUSTOM;
1081    _getSingleton()._updateTickables();
1082    return true;
1083  }
1084
1085  /**
1086    @brief Disables a specific mouse handler.
1087    @param name Unique name of the handler.
1088    @return False if name was not found, true otherwise.
1089  */
1090  bool InputManager::disableMouseHandler(const std::string &name)
1091  {
1092    // get pointer from the map with all stored handlers
1093    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1094    if (mapIt == _getSingleton().mouseHandlers_.end())
1095      return false;
1096    // look for the handler in the list
1097    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1098          it != _getSingleton().activeMouseHandlers_.end(); it++)
1099    {
1100      if ((*it) == (*mapIt).second)
1101      {
1102        _getSingleton().activeMouseHandlers_.erase(it);
1103        _getSingleton().stateRequest_ = IS_CUSTOM;
1104        _getSingleton()._updateTickables();
1105        return true;
1106      }
1107    }
1108    return true;
1109  }
1110
1111  /**
1112    @brief Checks whether a mouse handler is active
1113    @param name Unique name of the handler.
1114    @return False if key handler is not active or doesn't exist, true otherwise.
1115  */
1116  bool InputManager::isMouseHandlerActive(const std::string& name)
1117  {
1118    // get pointer from the map with all stored handlers
1119    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().mouseHandlers_.find(name);
1120    if (mapIt == _getSingleton().mouseHandlers_.end())
1121      return false;
1122    // see whether the handler already is in the list
1123    for (std::vector<MouseHandler*>::iterator it = _getSingleton().activeMouseHandlers_.begin();
1124          it != _getSingleton().activeMouseHandlers_.end(); it++)
1125    {
1126      if ((*it) == (*mapIt).second)
1127        return true;
1128    }
1129    return false;
1130  }
1131
1132
1133  // ###### JoyStickHandler ######
1134
1135  /**
1136    @brief Adds a new joy stick handler.
1137    @param handler Pointer to the handler object.
1138    @param name Unique name of the handler.
1139    @return True if added, false if name already existed.
1140  */
1141  bool InputManager::addJoyStickHandler(JoyStickHandler* handler, const std::string& name)
1142  {
1143    if (!handler)
1144      return false;
1145    if (_getSingleton().joyStickHandlers_.find(name) == _getSingleton().joyStickHandlers_.end())
1146    {
1147      _getSingleton().joyStickHandlers_[name] = handler;
1148      return true;
1149    }
1150    else
1151      return false;
1152  }
1153
1154  /**
1155    @brief Removes a JoyStick handler from the list.
1156    @param name Unique name of the handler.
1157    @return True if removal was successful, false if name was not found.
1158  */
1159  bool InputManager::removeJoyStickHandler(const std::string &name)
1160  {
1161    for (std::vector<OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin();
1162          itstick != _getSingleton().joySticks_.end(); itstick++)
1163      disableJoyStickHandler(name, itstick - _getSingleton().joySticks_.begin());
1164
1165    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1166    if (it != _getSingleton().joyStickHandlers_.end())
1167    {
1168      _getSingleton().joyStickHandlers_.erase(it);
1169      return true;
1170    }
1171    else
1172      return false;
1173  }
1174
1175  /**
1176    @brief Returns the pointer to a handler.
1177    @param name Unique name of the handler.
1178    @return Pointer to the instance, 0 if name was not found.
1179  */
1180  JoyStickHandler* InputManager::getJoyStickHandler(const std::string& name)
1181  {
1182    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().joyStickHandlers_.find(name);
1183    if (it != _getSingleton().joyStickHandlers_.end())
1184    {
1185      return (*it).second;
1186    }
1187    else
1188      return 0;
1189  }
1190
1191  /**
1192    @brief Enables a specific joy stick handler that has already been added.
1193    @param name Unique name of the handler.
1194    @return False if name or id was not found, true otherwise.
1195  */
1196  bool InputManager::enableJoyStickHandler(const std::string& name, unsigned int ID)
1197  {
1198    // get handler pointer from the map with all stored handlers
1199    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1200    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1201      return false;
1202
1203    // check for existence of the ID
1204    if (ID >= _getSingleton().joySticksSize_)
1205      return false;
1206
1207    // see whether the handler already is in the list
1208    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1209          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1210    {
1211      if ((*it) == (*handlerIt).second)
1212      {
1213        return true;
1214      }
1215    }
1216    _getSingleton().activeJoyStickHandlers_[ID].push_back((*handlerIt).second);
1217    _getSingleton().stateRequest_ = IS_CUSTOM;
1218    _getSingleton()._updateTickables();
1219    return true;
1220  }
1221
1222  /**
1223    @brief Disables a specific joy stick handler.
1224    @param name Unique name of the handler.
1225    @return False if name or id was not found, true otherwise.
1226  */
1227  bool InputManager::disableJoyStickHandler(const std::string &name, unsigned int ID)
1228  {
1229    // get handler pointer from the map with all stored handlers
1230    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1231    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1232      return false;
1233
1234    // check for existence of the ID
1235    if (ID >= _getSingleton().joySticksSize_)
1236      return false;
1237
1238    // look for the handler in the list
1239    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1240          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1241    {
1242      if ((*it) == (*handlerIt).second)
1243      {
1244        _getSingleton().activeJoyStickHandlers_[ID].erase(it);
1245        _getSingleton().stateRequest_ = IS_CUSTOM;
1246        _getSingleton()._updateTickables();
1247        return true;
1248      }
1249    }
1250    return true;
1251  }
1252
1253  /**
1254    @brief Checks whether a joy stick handler is active
1255    @param name Unique name of the handler.
1256    @return False if key handler is not active or doesn't exist, true otherwise.
1257  */
1258  bool InputManager::isJoyStickHandlerActive(const std::string& name, unsigned int ID)
1259  {
1260    // get handler pointer from the map with all stored handlers
1261    std::map<std::string, JoyStickHandler*>::const_iterator handlerIt = _getSingleton().joyStickHandlers_.find(name);
1262    if (handlerIt == _getSingleton().joyStickHandlers_.end())
1263      return false;
1264
1265    // check for existence of the ID
1266    if (ID >= _getSingleton().joySticksSize_)
1267      return false;
1268
1269    // see whether the handler already is in the list
1270    for (std::vector<JoyStickHandler*>::iterator it = _getSingleton().activeJoyStickHandlers_[ID].begin();
1271          it != _getSingleton().activeJoyStickHandlers_[ID].end(); it++)
1272    {
1273      if ((*it) == (*handlerIt).second)
1274        return true;
1275    }
1276    return false;
1277  }
1278
1279}
Note: See TracBrowser for help on using the repository browser.