Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 29, 2008, 11:13:11 PM (17 years ago)
Author:
rgrieder
Message:
  • InputManager fully functional (most parts tested), if there wasn't that selfish SpaceShip who claims all the mouse input…
  • InputHandler still loads hard coded key bindings, but works fine otherwise
  • I've tried to give full multiple joy stick support. Couldn't yet test that however. And more than one Joystick still doesn't make sense as long as we don't have split view ;)
Location:
code/branches/input/src/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/input/src/core/CMakeLists.txt

    r1196 r1203  
    55  Identifier.cc
    66  IdentifierDistributor.cc
    7 #  InputHandler.cc
     7  InputHandler.cc
    88  InputManager.cc
    99#  InputEventListener.cc
  • code/branches/input/src/core/InputBuffer.h

    r1195 r1203  
    3636
    3737#include "ois/OISKeyboard.h"
     38#include "InputHandler.h"
    3839
    3940namespace orxonox
     
    4243    {};
    4344
    44     class _CoreExport InputBuffer : public OIS::KeyListener
     45    class _CoreExport InputBuffer : public KeyHandler
    4546    {
    4647        struct InputBufferListenerTuple
     
    110111            bool keyPressed(const OIS::KeyEvent &e);
    111112            bool keyReleased(const OIS::KeyEvent &e);
     113            bool keyHeld(const OIS::KeyEvent &e) { return true; }
    112114
    113115            OIS::Keyboard* keyboard_;
  • code/branches/input/src/core/InputHandler.cc

    r1182 r1203  
    3535#include "Debug.h"
    3636#include "util/Convert.h"
    37 #include "InputEventListener.h"
    38 #include "InputEvent.h"
    39 #include "InputManager.h"
    4037#include "core/CommandExecutor.h"
    4138
     
    4340{
    4441  // ###############################
    45   // ###    InputHandlerGame     ###
     42  // ######     KeyBinder     ######
    4643  // ###############################
    4744
    4845  /**
    49     @brief standard constructor
    50   */
    51   InputHandlerGame::InputHandlerGame()
    52   {
     46    @brief Constructor that does as little as necessary.
     47  */
     48  KeyBinder::KeyBinder()
     49  {
     50    clearBindings();
    5351  }
    5452
     
    5654    @brief Destructor
    5755  */
    58   InputHandlerGame::~InputHandlerGame()
    59   {
    60   }
    61 
    62   /**
    63     @brief Loads the key bindings from the ini file.
    64     Currently, this is just a simple test routine that fills the list with numbers.
    65   */
    66   bool InputHandlerGame::loadBindings()
     56  KeyBinder::~KeyBinder()
     57  {
     58  }
     59
     60  /**
     61    @brief Overwrites all bindings with ""
     62  */
     63  void KeyBinder::clearBindings()
    6764  {
    6865    for (int i = 0; i < numberOfKeys_s; i++)
    6966    {
    70       // simply write the key number (i) in the string
    71       this->bindingsKeyPress_[i] = "";
    72       this->bindingsKeyRelease_[i] = "";
    73     }
    74     this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "setInputMode " + getConvertedValue<int, std::string>(IM_KEYBOARD);
    75     this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
    76     this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
    77     return true;
    78   }
     67      bindingsKeyPress_  [i] = "";
     68      bindingsKeyRelease_[i] = "";
     69      bindingsKeyHold_   [i] = "";
     70    }
     71    for (int i = 0; i < numberOfMouseButtons_s; i++)
     72    {
     73      bindingsMouseButtonPress_  [i] = "";
     74      bindingsMouseButtonRelease_[i] = "";
     75      bindingsMouseButtonHold_   [i] = "";
     76    }
     77    for (int i = 0; i < numberOfJoyStickButtons_s; i++)
     78    {
     79      bindingsJoyStickButtonPress_  [i] = "";
     80      bindingsJoyStickButtonRelease_[i] = "";
     81      bindingsJoyStickButtonHold_   [i] = "";
     82    }
     83  }
     84
     85  /**
     86    @brief Loads the key and button bindings.
     87    @return True if loading succeeded.
     88  */
     89  bool KeyBinder::loadBindings()
     90  {
     91    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings..." << std::endl;
     92
     93    // clear all the bindings at first.
     94    clearBindings();
     95
     96    // TODO: Insert the code to load the bindings from file.
     97    bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole";
     98    bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
     99    bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
     100
     101    COUT(ORX_DEBUG) << "KeyBinder: Loading key bindings done." << std::endl;
     102    return true;
     103  }
     104
    79105
    80106  /**
     
    82108    @param e Event information
    83109  */
    84   bool InputHandlerGame::keyPressed(const OIS::KeyEvent &e)
    85   {
    86     this->keysDown_.push_back(e.key);
     110  bool KeyBinder::keyPressed(const OIS::KeyEvent &e)
     111  {
    87112    // find the appropriate key binding
    88113    std::string cmdStr = bindingsKeyPress_[int(e.key)];
     
    92117      COUT(3) << "Executing command: " << cmdStr << std::endl;
    93118    }
     119   
    94120    return true;
    95121  }
     
    99125    @param e Event information
    100126  */
    101   bool InputHandlerGame::keyReleased(const OIS::KeyEvent &e)
    102   {
    103     // remove the key from the keysDown_ list
    104     for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
    105     {
    106       if (*it == e.key)
    107       {
    108         keysDown_.erase(it);
    109         break;
    110       }
    111     }
    112 
     127  bool KeyBinder::keyReleased(const OIS::KeyEvent &e)
     128  {
    113129    // find the appropriate key binding
    114130    std::string cmdStr = bindingsKeyRelease_[int(e.key)];
     
    118134      COUT(3) << "Executing command: " << cmdStr << std::endl;
    119135    }
     136
     137    return true;
     138  }
     139
     140  /**
     141    @brief Event handler for the keyHeld Event.
     142    @param e Event information
     143  */
     144  bool KeyBinder::keyHeld(const OIS::KeyEvent &e)
     145  {
     146    // find the appropriate key binding
     147    std::string cmdStr = bindingsKeyHold_[int(e.key)];
     148    if (cmdStr != "")
     149    {
     150      CommandExecutor::execute(cmdStr);
     151      COUT(3) << "Executing command: " << cmdStr << std::endl;
     152    }
     153
    120154    return true;
    121155  }
     
    125159    @param e Event information
    126160  */
    127   bool InputHandlerGame::mouseMoved(const OIS::MouseEvent &e)
     161  bool KeyBinder::mouseMoved(const OIS::MouseEvent &e)
    128162  {
    129163    return true;
     
    135169    @param id The ID of the mouse button
    136170  */
    137   bool InputHandlerGame::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    138   {
     171  bool KeyBinder::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     172  {
     173    // find the appropriate key binding
     174    std::string cmdStr = bindingsMouseButtonPress_[int(id)];
     175    if (cmdStr != "")
     176    {
     177      CommandExecutor::execute(cmdStr);
     178      COUT(3) << "Executing command: " << cmdStr << std::endl;
     179    }
     180
    139181    return true;
    140182  }
     
    145187    @param id The ID of the mouse button
    146188  */
    147   bool InputHandlerGame::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    148   {
    149     return true;
    150   }
    151 
    152   /**
    153     @brief Tick method to do additional calculations.
    154     @param dt Delta time.
    155   */
    156   void InputHandlerGame::tick(float dt)
    157   {
    158     // iterate through all the pressed keys
    159     for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
    160     {
    161       // find the appropriate key binding
    162       std::string cmdStr = bindingsKeyHold_[*it];
    163       if (cmdStr != "")
    164       {
    165         CommandExecutor::execute(cmdStr);
    166         COUT(3) << "Executing command: " << cmdStr << std::endl;
    167       }
    168     }
    169   }
    170 
    171   /**
    172     @brief Calls all the objects from classes that derive from InputEventListener.
    173     @param evt The input event that occured.
    174   */
    175   inline void InputHandlerGame::callListeners(orxonox::InputEvent &evt)
    176   {
    177     for (Iterator<InputEventListener> it = ObjectList<InputEventListener>::start(); it; )
    178     {
    179       if (it->bActive_)
    180         (it++)->eventOccured(evt);
    181       else
    182         it++;
    183     }
    184   }
     189  bool KeyBinder::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     190  {
     191    // find the appropriate key binding
     192    std::string cmdStr = bindingsMouseButtonRelease_[int(id)];
     193    if (cmdStr != "")
     194    {
     195      CommandExecutor::execute(cmdStr);
     196      COUT(3) << "Executing command: " << cmdStr << std::endl;
     197    }
     198
     199    return true;
     200  }
     201
     202  /**
     203    @brief Event handler for the mouseHeld Event.
     204    @param e Event information
     205    @param id The ID of the mouse button
     206  */
     207  bool KeyBinder::mouseHeld(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     208  {
     209    // find the appropriate key binding
     210    std::string cmdStr = bindingsMouseButtonHold_[int(id)];
     211    if (cmdStr != "")
     212    {
     213      CommandExecutor::execute(cmdStr);
     214      COUT(3) << "Executing command: " << cmdStr << std::endl;
     215    }
     216
     217    return true;
     218  }
     219
     220  bool KeyBinder::buttonPressed(const OIS::JoyStickEvent &arg, int button)
     221  {
     222    // find the appropriate key binding
     223    std::string cmdStr = bindingsJoyStickButtonPress_[button];
     224    if (cmdStr != "")
     225    {
     226      CommandExecutor::execute(cmdStr);
     227      COUT(3) << "Executing command: " << cmdStr << std::endl;
     228    }
     229
     230    return true;
     231  }
     232
     233  bool KeyBinder::buttonReleased(const OIS::JoyStickEvent &arg, int button)
     234  {
     235    // find the appropriate key binding
     236    std::string cmdStr = bindingsJoyStickButtonRelease_[button];
     237    if (cmdStr != "")
     238    {
     239      CommandExecutor::execute(cmdStr);
     240      COUT(3) << "Executing command: " << cmdStr << std::endl;
     241    }
     242
     243    return true;
     244  }
     245
     246  bool KeyBinder::buttonHeld(const OIS::JoyStickEvent &arg, int button)
     247  {
     248    // find the appropriate key binding
     249    std::string cmdStr = bindingsJoyStickButtonHold_[button];
     250    if (cmdStr != "")
     251    {
     252      CommandExecutor::execute(cmdStr);
     253      COUT(3) << "Executing command: " << cmdStr << std::endl;
     254    }
     255
     256    return true;
     257  }
     258
     259  bool KeyBinder::axisMoved(const OIS::JoyStickEvent &arg, int axis)
     260  {
     261    return true;
     262  }
     263
     264  bool KeyBinder::sliderMoved(const OIS::JoyStickEvent &arg, int id)
     265  {
     266    return true;
     267  }
     268
     269  bool KeyBinder::povMoved(const OIS::JoyStickEvent &arg, int id)
     270  {
     271    return true;
     272  }
     273
    185274
    186275
    187276  // ###############################
    188   // ###     InputHandlerGUI     ###
     277  // ###     GUIInputHandler     ###
    189278  // ###############################
    190279
    191   /**
    192     @brief standard constructor
    193   */
    194   InputHandlerGUI::InputHandlerGUI()
    195   {
    196   }
    197 
    198   /**
    199     @brief Destructor
    200   */
    201   InputHandlerGUI::~InputHandlerGUI()
    202   {
    203   }
    204 
    205   /**
    206     @brief Event handler for the keyPressed Event.
    207     @param e Event information
    208   */
    209   bool InputHandlerGUI::keyPressed(const OIS::KeyEvent &e)
    210   {
    211                 //CEGUI::System::getSingleton().injectKeyDown( arg.key );
    212                 //CEGUI::System::getSingleton().injectChar( arg.text );
    213     return true;
    214   }
    215 
    216   /**
    217     @brief Event handler for the keyReleased Event.
    218     @param e Event information
    219   */
    220   bool InputHandlerGUI::keyReleased(const OIS::KeyEvent &e)
    221   {
    222                 //CEGUI::System::getSingleton().injectKeyUp( arg.key );
    223     return true;
    224   }
    225 
    226   /**
    227     @brief Event handler for the mouseMoved Event.
    228     @param e Event information
    229   */
    230   bool InputHandlerGUI::mouseMoved(const OIS::MouseEvent &e)
    231   {
    232                 //CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
    233     return true;
    234   }
    235 
    236   /**
    237     @brief Event handler for the mousePressed Event.
    238     @param e Event information
    239     @param id The ID of the mouse button
    240   */
    241   bool InputHandlerGUI::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    242   {
    243                 //CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
    244     return true;
    245   }
    246 
    247   /**
    248     @brief Event handler for the mouseReleased Event.
    249     @param e Event information
    250     @param id The ID of the mouse button
    251   */
    252   bool InputHandlerGUI::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    253   {
    254                 //CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
    255     return true;
    256   }
    257 
    258   /**
    259     @brief Tick method to do additional calculations.
    260     @param dt Delta time.
    261   */
    262   void InputHandlerGUI::tick(float dt)
    263   {
    264    
    265   }
     280  ///**
     281  //  @brief standard constructor
     282  //*/
     283  //GUIInputHandler::GUIInputHandler()
     284  //{
     285  //}
     286
     287  ///**
     288  //  @brief Destructor
     289  //*/
     290  //GUIInputHandler::~GUIInputHandler()
     291  //{
     292  //}
     293
     294  ///**
     295  //  @brief Event handler for the keyPressed Event.
     296  //  @param e Event information
     297  //*/
     298  //bool GUIInputHandler::keyPressed(const OIS::KeyEvent &e)
     299  //{
     300                ////CEGUI::System::getSingleton().injectKeyDown( arg.key );
     301                ////CEGUI::System::getSingleton().injectChar( arg.text );
     302  //  return true;
     303  //}
     304
     305  ///**
     306  //  @brief Event handler for the keyReleased Event.
     307  //  @param e Event information
     308  //*/
     309  //bool GUIInputHandler::keyReleased(const OIS::KeyEvent &e)
     310  //{
     311                ////CEGUI::System::getSingleton().injectKeyUp( arg.key );
     312  //  return true;
     313  //}
     314
     315  ///**
     316  //  @brief Event handler for the mouseMoved Event.
     317  //  @param e Event information
     318  //*/
     319  //bool GUIInputHandler::mouseMoved(const OIS::MouseEvent &e)
     320  //{
     321                ////CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
     322  //  return true;
     323  //}
     324
     325  ///**
     326  //  @brief Event handler for the mousePressed Event.
     327  //  @param e Event information
     328  //  @param id The ID of the mouse button
     329  //*/
     330  //bool GUIInputHandler::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     331  //{
     332                ////CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
     333  //  return true;
     334  //}
     335
     336  ///**
     337  //  @brief Event handler for the mouseReleased Event.
     338  //  @param e Event information
     339  //  @param id The ID of the mouse button
     340  //*/
     341  //bool GUIInputHandler::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     342  //{
     343                ////CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
     344  //  return true;
     345  //}
    266346
    267347}
  • code/branches/input/src/core/InputHandler.h

    r1182 r1203  
    3838
    3939#include <string>
    40 #include <list>
    41 #include <OIS/OIS.h>
    42 
    43 #include "InputEvent.h"
     40#include "ois/OIS.h"
    4441
    4542namespace orxonox
     
    5653  }
    5754
    58   class _CoreExport BaseInputHandler
    59       : public OIS::KeyListener, public OIS::MouseListener
    60   {
    61     virtual void tick(float dt) = 0;
    62   };
    63    
    6455  /**
    65     @brief Captures mouse and keyboard input while in the actual game mode.
    66     Manages the key bindings.
     56    @brief Interface class used for key input listeners.
    6757  */
    68   class _CoreExport InputHandlerGame : public BaseInputHandler
     58  class _CoreExport KeyHandler : public OIS::KeyListener
    6959  {
    7060  public:
    71     InputHandlerGame ();
    72     ~InputHandlerGame();
     61    virtual bool keyHeld(const OIS::KeyEvent &arg) = 0;
     62  };
     63
     64  /**
     65    @brief Interface class used for mouse input listeners.
     66  */
     67  class _CoreExport MouseHandler : public OIS::MouseListener
     68  {
     69  public:
     70    virtual bool mouseHeld(const OIS::MouseEvent &arg, OIS::MouseButtonID id) = 0;
     71  };
     72
     73  /**
     74    @brief Interface class used for joy stick input listeners.
     75  */
     76  class _CoreExport JoyStickHandler : public OIS::JoyStickListener
     77  {
     78  public:
     79    virtual bool buttonHeld(const OIS::JoyStickEvent &arg, int button) = 0;
     80  };
     81 
     82
     83  /**
     84    @brief Captures mouse, keyboard and joy stick input while in the actual game mode.
     85           Manages the key bindings.
     86  */
     87  class _CoreExport KeyBinder : public KeyHandler, public MouseHandler, public JoyStickHandler
     88  {
     89  public:
     90    KeyBinder ();
     91    ~KeyBinder();
    7392
    7493    bool loadBindings();
     94    void clearBindings();
    7595
    76   private:
    77     // input events
    78                 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id);
    79                 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
    80     bool mouseMoved   (const OIS::MouseEvent &arg);
     96  private: // functions
    8197                bool keyPressed   (const OIS::KeyEvent   &arg);
    8298                bool keyReleased  (const OIS::KeyEvent   &arg);
     99                bool keyHeld      (const OIS::KeyEvent   &arg);
    83100
    84     void tick(float dt);
     101    bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id);
     102                bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
     103                bool mouseHeld    (const OIS::MouseEvent &arg, OIS::MouseButtonID id);
     104    bool mouseMoved   (const OIS::MouseEvent &arg);
    85105
    86     // temporary hack
    87     void callListeners(InputEvent &evt);
     106                bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
     107                bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
     108                bool buttonHeld    (const OIS::JoyStickEvent &arg, int button);
     109                bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
     110                bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
     111                bool povMoved      (const OIS::JoyStickEvent &arg, int id);
    88112
    89     //! Stores all the keys that are down
    90     std::list<OIS::KeyCode> keysDown_;
    91 
     113  private: // variables
    92114    /** denotes the maximum number of different keys there are in OIS.
    93         256 should be ok since the highest number in the enum is 237. */
     115        256 should be ok since the highest number in the OIS enum is 237. */
    94116    static const int numberOfKeys_s = 256;
    95117    //! Array of input events for every pressed key
    96     std::string bindingsKeyPress_[numberOfKeys_s];
     118    std::string bindingsKeyPress_  [numberOfKeys_s];
    97119    //! Array of input events for every released key
    98120    std::string bindingsKeyRelease_[numberOfKeys_s];
    99     //! Array of input events for every holding key
    100     std::string bindingsKeyHold_[numberOfKeys_s];
     121    //!Array of input events for every held key
     122    std::string bindingsKeyHold_   [numberOfKeys_s];
    101123
    102124    /** denotes the maximum number of different buttons there are in OIS.
    103         16 should be ok since the highest number in the enum is 7. */
    104     static const int numberOfButtons_s = 16;
    105     //! Array of input events for every pressed key
    106     std::string bindingsButtonPressed_[numberOfButtons_s];
    107     //! Array of input events for every released key
    108     std::string bindingsButtonReleased_[numberOfButtons_s];
     125        16 should be ok since the highest number in the OIS enum is 7. */
     126    static const int numberOfMouseButtons_s = 16;
     127    //! Array of input events for every pressed mouse button
     128    std::string bindingsMouseButtonPress_  [numberOfMouseButtons_s];
     129    //! Array of input events for every released mouse button
     130    std::string bindingsMouseButtonRelease_[numberOfMouseButtons_s];
     131    //! Array of input events for every held mouse button
     132    std::string bindingsMouseButtonHold_   [numberOfMouseButtons_s];
     133
     134    /** denotes the maximum number of different buttons there are in OIS.
     135        32 should be ok. */
     136    static const int numberOfJoyStickButtons_s = 32;
     137    //! Array of input events for every pressed joy stick button
     138    std::string bindingsJoyStickButtonPress_  [numberOfJoyStickButtons_s];
     139    //! Array of input events for every released joy stick button
     140    std::string bindingsJoyStickButtonRelease_[numberOfJoyStickButtons_s];
     141    //! Array of input events for every held joy stick button
     142    std::string bindingsJoyStickButtonHold_   [numberOfJoyStickButtons_s];
    109143
    110144  };
     
    115149    GUI.
    116150  */
    117   class _CoreExport InputHandlerGUI : public BaseInputHandler
    118   {
    119   public:
    120     InputHandlerGUI ();
    121     ~InputHandlerGUI();
     151  //class _CoreExport GUIInputHandler : public KeyHandler, public MouseHandler, public JoyStickHandler
     152  //{
     153  //public:
     154  //  GUIInputHandler ();
     155  //  ~GUIInputHandler();
    122156
    123     void tick(float dt);
     157  //private:
     158  //  // input events
     159                //bool keyPressed   (const OIS::KeyEvent   &arg);
     160                //bool keyReleased  (const OIS::KeyEvent   &arg);
     161                //bool keyHeld      (const OIS::KeyEvent   &arg);
    124162
    125   private:
    126     // input events
    127                 bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id);
    128                 bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
    129     bool mouseMoved   (const OIS::MouseEvent &arg);
    130                 bool keyPressed   (const OIS::KeyEvent   &arg);
    131                 bool keyReleased  (const OIS::KeyEvent   &arg);
    132   };
     163  //  bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id);
     164                //bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id);
     165                //bool mouseHeld    (const OIS::MouseEvent &arg, OIS::MouseButtonID id);
     166  //  bool mouseMoved   (const OIS::MouseEvent &arg);
     167
     168                //bool buttonPressed (const OIS::JoyStickEvent &arg, int button);
     169                //bool buttonReleased(const OIS::JoyStickEvent &arg, int button);
     170                //bool buttonHeld    (const OIS::JoyStickEvent &arg, int button);
     171                //bool axisMoved     (const OIS::JoyStickEvent &arg, int axis);
     172                //bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
     173                //bool povMoved      (const OIS::JoyStickEvent &arg, int id);
     174  //};
    133175
    134176}
  • code/branches/input/src/core/InputManager.cc

    r1195 r1203  
    4545  // ###    Internal Methods     ###
    4646  // ###############################
     47  // ###############################
    4748
    4849  /**
     
    5455      state_(IS_UNINIT), stateRequest_(IS_UNINIT)
    5556  {
    56     // overwrite every key binding with ""
    57     _clearBindings();
    58     _setNumberOfJoysticks(0);
    59 
    6057    RegisterObject(InputManager);
    6158  }
     
    7673  InputManager::~InputManager()
    7774  {
    78     this->_destroy();
     75    _destroy();
    7976  }
    8077
     
    129126      setWindowExtents(windowWidth, windowHeight);
    130127
    131       this->state_ = IS_NONE;
     128      state_ = IS_NONE;
    132129      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
    133130    }
     
    140137    addKeyListener(new InputBuffer(), "buffer");
    141138
     139    KeyBinder* binder = new KeyBinder();
     140    binder->loadBindings();
     141    addKeyListener(binder, "keybinder");
     142    addMouseListener(binder, "keybinder");
     143
    142144    // Read all the key bindings and assign them
    143     if (!_loadBindings())
    144       return false;
     145    //if (!_loadBindings())
     146    //  return false;
    145147
    146148    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
     
    150152  /**
    151153    @brief Creates a keyboard and sets the event handler.
    152   */
    153   void InputManager::_initialiseKeyboard()
    154   {
     154    @return False if keyboard stays uninitialised, true otherwise.
     155  */
     156  bool InputManager::_initialiseKeyboard()
     157  {
     158    if (keyboard_ != 0)
     159    {
     160      CCOUT(2) << "Warning: Keyboard already initialised, skipping." << std::endl;
     161      return true;
     162    }
    155163    try
    156164    {
     
    162170        // note: OIS will not detect keys that have already been down when the keyboard was created.
    163171        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
     172        return true;
    164173      }
    165174      else
     175      {
    166176        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
     177        return false;
     178      }
    167179    }
    168180    catch (OIS::Exception ex)
    169181    {
     182      // TODO: Test this output regarding formatting
    170183      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
    171184          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
    172185      keyboard_ = 0;
     186      return false;
    173187    }
    174188  }
     
    176190  /**
    177191    @brief Creates a mouse and sets the event handler.
    178   */
    179   void InputManager::_initialiseMouse()
    180   {
     192    @return False if mouse stays uninitialised, true otherwise.
     193  */
     194  bool InputManager::_initialiseMouse()
     195  {
     196    if (mouse_ != 0)
     197    {
     198      CCOUT(2) << "Warning: Mouse already initialised, skipping." << std::endl;
     199      return true;
     200    }
    181201    try
    182202    {
     
    187207        mouse_->setEventCallback(this);
    188208        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
     209        return true;
    189210      }
    190211      else
     212      {
    191213        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
     214        return false;
     215      }
    192216    }
    193217    catch (OIS::Exception ex)
     
    196220          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
    197221      mouse_ = 0;
     222      return false;
    198223    }
    199224  }
     
    201226  /**
    202227    @brief Creates all joy sticks and sets the event handler.
    203   */
    204   void InputManager::_initialiseJoySticks()
    205   {
     228    @return False joy stick stay uninitialised, true otherwise.
     229  */
     230  bool InputManager::_initialiseJoySticks()
     231  {
     232    if (joySticks_.size() > 0)
     233    {
     234      CCOUT(2) << "Warning: Joy sticks already initialised, skipping." << std::endl;
     235      return true;
     236    }
     237    bool success = false;
    206238    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
    207239    {
    208       _setNumberOfJoysticks(inputSystem_->getNumberOfDevices(OIS::OISJoyStick));
    209       for (std::vector<OIS::JoyStick*>::iterator it = joySticks_.begin(); it != joySticks_.end(); it++)
     240      for (int i = 0; i < inputSystem_->getNumberOfDevices(OIS::OISJoyStick); i++)
    210241      {
    211242        try
    212243        {
    213           *it = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
     244          OIS::JoyStick* stig = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
     245          joySticks_[stig->getID()] = stig;
    214246          // register our listener in OIS.
    215           (*it)->setEventCallback(this);
    216           CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << (*it)->getID() << std::endl;
     247          stig->setEventCallback(this);
     248          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << stig->getID() << std::endl;
     249          success = true;
    217250        }
    218251        catch (OIS::Exception ex)
    219252        {
    220           CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy stick with ID" << (*it)->getID() << "\n"
     253          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy number" << i << "\n"
    221254              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
    222           (*it) = 0;
    223255        }
    224256      }
    225257    }
    226258    else
     259    {
    227260      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
    228   }
    229 
    230   /**
    231     @brief Resizes all lists related to joy sticks and sets joy stick bindings to "".
    232     @param size Number of joy sticks available.
    233   */
    234   void InputManager::_setNumberOfJoysticks(int size)
    235   {
    236     this->bindingsJoyStickButtonHold_   .resize(size);
    237     this->bindingsJoyStickButtonPress_  .resize(size);
    238     this->bindingsJoyStickButtonRelease_.resize(size);
    239     this->bJoyStickButtonBindingsActive_.resize(size);
    240     this->joyStickButtonsDown_          .resize(size);
    241     this->joySticks_                    .resize(size);
    242     for (int j = 0; j < size; j++)
    243     {
    244       bindingsJoyStickButtonPress_  [j].resize(numberOfJoyStickButtons_s);
    245       bindingsJoyStickButtonRelease_[j].resize(numberOfJoyStickButtons_s);
    246       bindingsJoyStickButtonHold_   [j].resize(numberOfJoyStickButtons_s);
    247       for (int i = 0; i < numberOfJoyStickButtons_s; i++)
    248       {
    249         this->bindingsJoyStickButtonPress_  [j][i] = "";
    250         this->bindingsJoyStickButtonRelease_[j][i] = "";
    251         this->bindingsJoyStickButtonHold_   [j][i] = "";
    252       }
    253     }
    254   }
    255 
    256   /**
    257     @brief Loads the key and button bindings.
    258   */
    259   bool InputManager::_loadBindings()
    260   {
    261     CCOUT(ORX_DEBUG) << "Loading key bindings..." << std::endl;
    262 
    263     // clear all the bindings at first.
    264     _clearBindings();
    265 
    266     // TODO: Insert the code to load the bindings from file.
    267     this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole";
    268     this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
    269     this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
    270 
    271     CCOUT(ORX_DEBUG) << "Loading key bindings done." << std::endl;
    272     return true;
    273   }
    274 
    275   /**
    276     @brief Overwrites all bindings with ""
    277   */
    278   void InputManager::_clearBindings()
    279   {
    280     for (int i = 0; i < numberOfKeys_s; i++)
    281     {
    282       this->bindingsKeyPress_  [i] = "";
    283       this->bindingsKeyRelease_[i] = "";
    284       this->bindingsKeyHold_   [i] = "";
    285     }
    286     for (int i = 0; i < numberOfMouseButtons_s; i++)
    287     {
    288       this->bindingsMouseButtonPress_  [i] = "";
    289       this->bindingsMouseButtonRelease_[i] = "";
    290       this->bindingsMouseButtonHold_   [i] = "";
    291     }
    292     for (unsigned int j = 0; j < joySticks_.size(); j++)
    293     {
    294       for (int i = 0; i < numberOfJoyStickButtons_s; i++)
    295       {
    296         this->bindingsJoyStickButtonPress_  [j][i] = "";
    297         this->bindingsJoyStickButtonRelease_[j][i] = "";
    298         this->bindingsJoyStickButtonHold_   [j][i] = "";
    299       }
    300     }
     261      return false;
     262    }
     263    return success;
    301264  }
    302265
     
    310273    if (state_ != IS_UNINIT)
    311274    {
    312       this->listenersKeyActive_.clear();
    313       this->listenersMouseActive_.clear();
    314       this->listenersJoySticksActive_.clear();
    315       this->listenersKey_.clear();
    316       this->listenersMouse_.clear();
    317       this->listenersJoySticks_.clear();
    318 
    319       this->keysDown_.clear();
    320       this->mouseButtonsDown_.clear();
    321 
    322       _clearBindings();
    323 
    324       if (keyboard_)
    325         inputSystem_->destroyInputObject(keyboard_);
    326       keyboard_ = 0;
    327 
    328       if (mouse_)
    329         inputSystem_->destroyInputObject(mouse_);
    330       mouse_ = 0;
    331 
    332       if (joySticks_.size() > 0)
    333       {
    334         for (unsigned int i = 0; i < joySticks_.size(); i++)
    335         {
    336           if (joySticks_[i] != 0)
    337             inputSystem_->destroyInputObject(joySticks_[i]);
    338         }
    339         _setNumberOfJoysticks(0);
    340       }
    341 
    342       if (inputSystem_)
    343         OIS::InputManager::destroyInputSystem(inputSystem_);
    344       inputSystem_ = 0;
    345 
    346275      if (listenersKey_.find("buffer") != listenersKey_.end())
    347276        delete listenersKey_["buffer"];
    348277
    349       this->state_ = IS_UNINIT;
     278      if (listenersKey_.find("keybinder") != listenersKey_.end())
     279        delete listenersKey_["keybinder"];
     280
     281      listenersKeyActive_.clear();
     282      listenersMouseActive_.clear();
     283      listenersJoySticksActive_.clear();
     284      listenersKey_.clear();
     285      listenersMouse_.clear();
     286      listenersJoySticks_.clear();
     287
     288      keysDown_.clear();
     289      mouseButtonsDown_.clear();
     290      joySticksButtonsDown_.clear();
     291
     292      _destroyKeyboard();
     293      _destroyMouse();
     294      _destroyJoySticks();
     295
     296      // inputSystem_ can never be 0, or else the code is mistaken
     297      OIS::InputManager::destroyInputSystem(inputSystem_);
     298      inputSystem_ = 0;
     299
     300      state_ = IS_UNINIT;
    350301    }
    351302    else
     
    355306  }
    356307
    357 
    358   // ###############################
    359   // ###    Interface Methods    ###
    360   // ###############################
     308  /**
     309    @brief Destroys the keyboard and sets it to 0.
     310  */
     311  void InputManager::_destroyKeyboard()
     312  {
     313    if (keyboard_)
     314      // inputSystem_ can never be 0, or else the code is mistaken
     315      inputSystem_->destroyInputObject(keyboard_);
     316    keyboard_ = 0;
     317    CCOUT(ORX_DEBUG) << "Keyboard destroyed." << std::endl;
     318  }
     319
     320  /**
     321    @brief Destroys the mouse and sets it to 0.
     322  */
     323  void InputManager::_destroyMouse()
     324  {
     325    if (mouse_)
     326      // inputSystem_ can never be 0, or else the code is mistaken
     327      inputSystem_->destroyInputObject(mouse_);
     328    mouse_ = 0;
     329    CCOUT(ORX_DEBUG) << "Mouse destroyed." << std::endl;
     330  }
     331
     332  /**
     333    @brief Destroys all the joy sticks and resizes the lists to 0.
     334  */
     335  void InputManager::_destroyJoySticks()
     336  {
     337    if (joySticks_.size() > 0)
     338    {
     339      // note: inputSystem_ can never be 0, or else the code is mistaken
     340      for (std::map<int, OIS::JoyStick*>::iterator itstick = joySticks_.begin(); itstick != joySticks_.end(); itstick++)
     341      {
     342        if ((*itstick).second != 0)
     343          inputSystem_->destroyInputObject((*itstick).second);
     344      }
     345      joySticks_.clear();
     346    }
     347    CCOUT(ORX_DEBUG) << "Joy sticks destroyed." << std::endl;
     348  }
     349
     350
     351  // #################################
     352  // ### Private Interface Methods ###
     353  // #################################
     354  // #################################
    361355
    362356  /**
     
    372366    if (state_ != stateRequest_)
    373367    {
    374       switch (stateRequest_)
    375       {
    376       case IS_NORMAL:
    377         this->listenersKeyActive_.clear();
    378         this->listenersMouseActive_.clear();
    379         this->listenersJoySticksActive_.clear();
    380         this->bKeyBindingsActive_            = true;
    381         this->bMouseButtonBindingsActive_    = true;
    382         for (unsigned int i = 0; i < joySticks_.size(); i++)
    383           this->bJoyStickButtonBindingsActive_[i] = true;
    384         break;
    385 
    386       case IS_GUI:
    387         // FIXME: do stuff
    388         break;
    389 
    390       case IS_CONSOLE:
    391         this->listenersKeyActive_.clear();
    392         this->listenersMouseActive_.clear();
    393         this->listenersJoySticksActive_.clear();
    394         this->bKeyBindingsActive_            = false;
    395         this->bMouseButtonBindingsActive_    = true;
    396         for (unsigned int i = 0; i < joySticks_.size(); i++)
    397           this->bJoyStickButtonBindingsActive_[i] = true;
    398         if (listenersKey_.find("buffer") != listenersKey_.end())
    399           listenersKeyActive_.push_back(listenersKey_["buffer"]);
    400         else
     368      if (stateRequest_ != IS_CUSTOM)
     369      {
     370        listenersKeyActive_.clear();
     371        listenersMouseActive_.clear();
     372        listenersJoySticksActive_.clear();
     373
     374        switch (stateRequest_)
    401375        {
    402           // someone fiddled with the InputBuffer
    403           CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl;
    404           this->bKeyBindingsActive_ = true;
     376        case IS_NORMAL:
     377          // normal play mode
     378          if (listenersKey_.find("keybinder") != listenersKey_.end())
     379            listenersKeyActive_.push_back(listenersKey_["keybinder"]);
     380          if (listenersMouse_.find("keybinder") != listenersMouse_.end())
     381            listenersMouseActive_.push_back(listenersMouse_["keybinder"]);
     382          if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end())
     383          {
     384            for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin();
     385                  it != joySticks_.end(); it++)
     386              listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]);
     387          }
     388          break;
     389
     390        case IS_GUI:
     391          // FIXME: do stuff
     392          break;
     393
     394        case IS_CONSOLE:
     395          if (listenersMouse_.find("keybinder") != listenersMouse_.end())
     396            listenersMouseActive_.push_back(listenersMouse_["keybinder"]);
     397          if (listenersJoySticks_.find("keybinder") != listenersJoySticks_.end())
     398          {
     399            for (std::map<int, OIS::JoyStick*>::const_iterator it = joySticks_.begin();
     400                  it != joySticks_.end(); it++)
     401              listenersJoySticksActive_[(*it).first].push_back(listenersJoySticks_["keybinder"]);
     402          }
     403
     404          if (listenersKey_.find("buffer") != listenersKey_.end())
     405            listenersKeyActive_.push_back(listenersKey_["buffer"]);
     406          else
     407          {
     408            // someone fiddled with the InputBuffer
     409            CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl;
     410            if (listenersKey_.find("keybinder") != listenersKey_.end())
     411              listenersKeyActive_.push_back(listenersKey_["keybinder"]);
     412            else
     413              // this is bad
     414              CCOUT(2) << "Error: Cannot reactivate key binder: not found!" << std::endl;
     415          }
     416          break;
     417
     418        default:
     419          break;
    405420        }
    406         break;
    407 
    408       case IS_NONE:
    409         this->listenersKeyActive_.clear();
    410         this->listenersMouseActive_.clear();
    411         this->listenersJoySticksActive_.clear();
    412         this->bKeyBindingsActive_            = false;
    413         this->bMouseButtonBindingsActive_    = false;
    414         for (unsigned int i = 0; i < joySticks_.size(); i++)
    415           this->bJoyStickButtonBindingsActive_[i] = false;
    416         break;
    417 
    418       case IS_CUSTOM:
    419         // don't do anything
    420         break;
    421 
    422       case IS_UNINIT:
    423         // this can't happen
    424         break;
    425       }
    426       state_ = stateRequest_;
     421        state_ = stateRequest_;
     422      }
    427423    }
    428424
     
    432428    if (keyboard_)
    433429      keyboard_->capture();
    434   }
    435 
    436   /*void InputManager::_setDefaultState()
    437   {
    438     this->listenersKeyActive_.clear();
    439     this->listenersMouseActive_.clear();
    440     this->listenersJoyStickActive_.clear();
    441     this->bKeyBindingsActive_            = true;
    442     this->bMouseButtonBindingsActive_    = true;
    443     this->bJoyStickButtonBindingsActive_ = true;
    444   }*/
     430
     431
     432    // call all the listeners for the held key events
     433    for (std::list<OIS::KeyCode>::const_iterator itKey = keysDown_.begin();
     434          itKey != keysDown_.end(); itKey++)
     435    {
     436      OIS::KeyEvent keyArg(keyboard_, *itKey, 0);
     437      for (std::list<KeyHandler*>::const_iterator itKeyHandler = listenersKeyActive_.begin();
     438            itKeyHandler != listenersKeyActive_.end(); itKeyHandler++)
     439      {
     440        (*itKeyHandler)->keyHeld(keyArg);
     441      }
     442    }
     443
     444    // call all the listeners for the held mouse button events
     445    for (std::list<OIS::MouseButtonID>::const_iterator itMouseButton = mouseButtonsDown_.begin();
     446          itMouseButton != mouseButtonsDown_.end(); itMouseButton++)
     447    {
     448      OIS::MouseEvent mouseButtonArg(mouse_, mouse_->getMouseState());
     449      for (std::list<MouseHandler*>::const_iterator itMouseHandler = listenersMouseActive_.begin();
     450            itMouseHandler != listenersMouseActive_.end(); itMouseHandler++)
     451      {
     452        (*itMouseHandler)->mouseHeld(mouseButtonArg, *itMouseButton);
     453      }
     454    }
     455
     456    // call all the listeners for the held joy stick button events
     457    for (std::map<int, std::list <int> >::const_iterator itJoyStick = joySticksButtonsDown_.begin();
     458          itJoyStick != joySticksButtonsDown_.end(); itJoyStick++)
     459    {
     460      int id = (*itJoyStick).first;
     461      for (std::list<int>::const_iterator itJoyStickButton = (*itJoyStick).second.begin();
     462            itJoyStickButton != (*itJoyStick).second.end(); itJoyStickButton++)
     463      {
     464        OIS::JoyStickEvent joyStickButtonArg(joySticks_[id], joySticks_[id]->getJoyStickState());
     465        for (std::list<JoyStickHandler*>::const_iterator itJoyStickHandler = listenersJoySticksActive_[id].begin();
     466              itJoyStickHandler != listenersJoySticksActive_[id].end(); itJoyStickHandler++)
     467        {
     468          (*itJoyStickHandler)->buttonHeld(joyStickButtonArg, *itJoyStickButton);
     469        }
     470      }
     471    }
     472  }
    445473
    446474
     
    451479  bool InputManager::keyPressed(const OIS::KeyEvent &e)
    452480  {
    453     this->keysDown_.push_back(e.key);
    454 
    455     if (this->bKeyBindingsActive_)
    456     {
    457       // find the appropriate key binding
    458       std::string cmdStr = bindingsKeyPress_[int(e.key)];
    459       if (cmdStr != "")
    460       {
    461         CommandExecutor::execute(cmdStr);
    462         COUT(3) << "Executing command: " << cmdStr << std::endl;
    463       }
    464     }
    465     else
    466     {
    467       for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
    468         (*it)->keyPressed(e);
    469     }
     481    // check whether the key is already in the list (can happen when focus was lost)
     482    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
     483    {
     484      if (*it == e.key)
     485      {
     486        keysDown_.erase(it);
     487        break;
     488      }
     489    }
     490    keysDown_.push_back(e.key);
     491
     492    for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
     493      (*it)->keyPressed(e);
     494
    470495    return true;
    471496  }
     
    487512    }
    488513
    489     if (this->bKeyBindingsActive_)
    490     {
    491       // find the appropriate key binding
    492       std::string cmdStr = bindingsKeyRelease_[int(e.key)];
    493       if (cmdStr != "")
    494       {
    495         CommandExecutor::execute(cmdStr);
    496         COUT(3) << "Executing command: " << cmdStr << std::endl;
    497       }
    498     }
    499     else
    500     {
    501       for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
    502         (*it)->keyReleased(e);
    503     }
     514    for (std::list<KeyHandler*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
     515      (*it)->keyReleased(e);
     516
    504517    return true;
    505518  }
     
    511524  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
    512525  {
     526    for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++)
     527      (*it)->mouseMoved(e);
     528
    513529    return true;
    514530  }
     
    521537  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    522538  {
     539    // check whether the button is already in the list (can happen when focus was lost)
     540    for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++)
     541    {
     542      if (*it == id)
     543      {
     544        mouseButtonsDown_.erase(it);
     545        break;
     546      }
     547    }
     548    mouseButtonsDown_.push_back(id);
     549
     550    for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++)
     551      (*it)->mousePressed(e, id);
     552
    523553    return true;
    524554  }
     
    531561  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
    532562  {
     563    // remove the button from the keysDown_ list
     564    for (std::list<OIS::MouseButtonID>::iterator it = mouseButtonsDown_.begin(); it != mouseButtonsDown_.end(); it++)
     565    {
     566      if (*it == id)
     567      {
     568        mouseButtonsDown_.erase(it);
     569        break;
     570      }
     571    }
     572
     573    for (std::list<MouseHandler*>::const_iterator it = listenersMouseActive_.begin(); it != listenersMouseActive_.end(); it++)
     574      (*it)->mouseReleased(e, id);
     575
    533576    return true;
    534577  }
     
    536579  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
    537580  {
     581    int devID = arg.device->getID();
     582
     583    // check whether the button is already in the list (can happen when focus was lost)
     584    std::list<int>& buttonsDownList = joySticksButtonsDown_[devID];
     585    for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++)
     586    {
     587      if (*it == button)
     588      {
     589        buttonsDownList.erase(it);
     590        break;
     591      }
     592    }
     593    joySticksButtonsDown_[devID].push_back(button);
     594
     595    std::list<JoyStickHandler*>::iterator end = listenersJoySticksActive_[devID].end();
     596    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
     597      (*it)->buttonPressed(arg, button);
     598
    538599    return true;
    539600  }
     
    541602  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
    542603  {
     604    int devID = arg.device->getID();
     605
     606    // remove the button from the joySticksButtonsDown_ list
     607    std::list<int>& buttonsDownList = joySticksButtonsDown_[devID];
     608    for (std::list<int>::iterator it = buttonsDownList.begin(); it != buttonsDownList.end(); it++)
     609    {
     610      if (*it == button)
     611      {
     612        buttonsDownList.erase(it);
     613        break;
     614      }
     615    }
     616
     617    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
     618    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
     619      (*it)->buttonReleased(arg, button);
     620
    543621    return true;
    544622  }
     
    546624  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
    547625  {
     626    int devID = arg.device->getID();
     627    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
     628    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
     629      (*it)->axisMoved(arg, axis);
     630
    548631    return true;
    549632  }
     
    551634  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
    552635  {
     636    int devID = arg.device->getID();
     637    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
     638    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
     639      (*it)->sliderMoved(arg, id);
     640
    553641    return true;
    554642  }
     
    556644  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
    557645  {
     646    int devID = arg.device->getID();
     647    std::list<JoyStickHandler*>::const_iterator end = listenersJoySticksActive_[devID].end();
     648    for (std::list<JoyStickHandler*>::const_iterator it = listenersJoySticksActive_[devID].begin(); it != end; it++)
     649      (*it)->povMoved(arg, id);
     650
    558651    return true;
    559652  }
     
    563656  // ### Static Interface Methods ###
    564657  // ################################
     658  // ################################
     659
     660  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight,
     661    bool createKeyboard, bool createMouse, bool createJoySticks)
     662  {
     663    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
     664          createKeyboard, createMouse, createJoySticks);
     665  }
     666
     667  bool InputManager::initialiseKeyboard()
     668  {
     669    return _getSingleton()._initialiseKeyboard();
     670  }
     671
     672  bool InputManager::initialiseMouse()
     673  {
     674    return _getSingleton()._initialiseMouse();
     675  }
     676
     677  bool InputManager::initialiseJoySticks()
     678  {
     679    return _getSingleton()._initialiseJoySticks();
     680  }
     681
     682
     683  void InputManager::destroy()
     684  {
     685    _getSingleton()._destroy();
     686  }
     687
     688  void InputManager::destroyKeyboard()
     689  {
     690    return _getSingleton()._destroyKeyboard();
     691  }
     692
     693  void InputManager::destroyMouse()
     694  {
     695    return _getSingleton()._destroyMouse();
     696  }
     697
     698  void InputManager::destroyJoySticks()
     699  {
     700    return _getSingleton()._destroyJoySticks();
     701  }
     702
    565703
    566704  /**
     
    600738  }
    601739
    602   void InputManager::destroy()
    603   {
    604     _getSingleton()._destroy();
    605   }
    606 
    607   bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight,
    608     bool createKeyboard, bool createMouse, bool createJoySticks)
    609   {
    610     return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
    611           createKeyboard, createMouse, createJoySticks);
    612   }
    613 
    614   /*bool InputManager::initialiseKeyboard()
    615   {
    616   }
    617 
    618   bool InputManager::initialiseMouse()
    619   {
    620   }
    621 
    622   bool InputManager::initialiseJoySticks()
    623   {
    624   }*/
    625 
    626   bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name)
     740
     741  // ###### KeyHandler ######
     742
     743  /**
     744    @brief Adds a new key listener.
     745    @param listener Pointer to the listener object.
     746    @param name Unique name of the listener.
     747    @return True if added, false if name already existed.
     748  */
     749  bool InputManager::addKeyListener(KeyHandler* listener, const std::string& name)
    627750  {
    628751    if (_getSingleton().listenersKey_.find(name) == _getSingleton().listenersKey_.end())
     
    635758  }
    636759
     760  /**
     761    @brief Removes a Key Listener from the list.
     762    @param name Unique name of the listener.
     763    @return True if removal was successful, false if name was not found.
     764  */
    637765  bool InputManager::removeKeyListener(const std::string &name)
    638766  {
    639     std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name);
     767    disableKeyListener(name);
     768    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name);
    640769    if (it != _getSingleton().listenersKey_.end())
    641770    {
     
    647776  }
    648777
    649   OIS::KeyListener* InputManager::getKeyListener(const std::string& name)
    650   {
    651     std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name);
     778  /**
     779    @brief Returns the pointer to a listener.
     780    @param name Unique name of the listener.
     781    @return Pointer to the instance, 0 if name was not found.
     782  */
     783  KeyHandler* InputManager::getKeyListener(const std::string& name)
     784  {
     785    std::map<std::string, KeyHandler*>::iterator it = _getSingleton().listenersKey_.find(name);
    652786    if (it != _getSingleton().listenersKey_.end())
    653787    {
     
    658792  }
    659793
     794  /**
     795    @brief Enables a specific key listener that has already been added.
     796    @param name Unique name of the listener.
     797    @return False if name was not found, true otherwise.
     798  */
     799  bool InputManager::enableKeyListener(const std::string& name)
     800  {
     801    // get pointer from the map with all stored listeners
     802    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name);
     803    if (mapIt == _getSingleton().listenersKey_.end())
     804      return false;
     805    // see whether the listener is already in the list
     806    for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin();
     807          it != _getSingleton().listenersKeyActive_.end(); it++)
     808    {
     809      if ((*it) == (*mapIt).second)
     810        return true;
     811    }
     812    _getSingleton().listenersKeyActive_.push_back((*mapIt).second);
     813    return true;
     814  }
     815
     816  /**
     817    @brief Disables a specific key listener.
     818    @param name Unique name of the listener.
     819    @return False if name was not found, true otherwise.
     820  */
     821  bool InputManager::disableKeyListener(const std::string &name)
     822  {
     823    // get pointer from the map with all stored listeners
     824    std::map<std::string, KeyHandler*>::const_iterator mapIt = _getSingleton().listenersKey_.find(name);
     825    if (mapIt == _getSingleton().listenersKey_.end())
     826      return false;
     827    // look for the listener in the list
     828    for (std::list<KeyHandler*>::iterator it = _getSingleton().listenersKeyActive_.begin();
     829          it != _getSingleton().listenersKeyActive_.end(); it++)
     830    {
     831      if ((*it) == (*mapIt).second)
     832      {
     833        _getSingleton().listenersKeyActive_.erase(it);
     834        return true;
     835      }
     836    }
     837    return true;
     838  }
     839
     840
     841  // ###### MouseHandler ######
     842  /**
     843    @brief Adds a new mouse listener.
     844    @param listener Pointer to the listener object.
     845    @param name Unique name of the listener.
     846    @return True if added, false if name already existed.
     847  */
     848  bool InputManager::addMouseListener(MouseHandler* listener, const std::string& name)
     849  {
     850    if (_getSingleton().listenersMouse_.find(name) == _getSingleton().listenersMouse_.end())
     851    {
     852      _getSingleton().listenersMouse_[name] = listener;
     853      return true;
     854    }
     855    else
     856      return false;
     857  }
     858
     859  /**
     860    @brief Removes a Mouse Listener from the list.
     861    @param name Unique name of the listener.
     862    @return True if removal was successful, false if name was not found.
     863  */
     864  bool InputManager::removeMouseListener(const std::string &name)
     865  {
     866    disableMouseListener(name);
     867    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name);
     868    if (it != _getSingleton().listenersMouse_.end())
     869    {
     870      _getSingleton().listenersMouse_.erase(it);
     871      return true;
     872    }
     873    else
     874      return false;
     875  }
     876
     877  /**
     878    @brief Returns the pointer to a listener.
     879    @param name Unique name of the listener.
     880    @return Pointer to the instance, 0 if name was not found.
     881  */
     882  MouseHandler* InputManager::getMouseListener(const std::string& name)
     883  {
     884    std::map<std::string, MouseHandler*>::iterator it = _getSingleton().listenersMouse_.find(name);
     885    if (it != _getSingleton().listenersMouse_.end())
     886    {
     887      return (*it).second;
     888    }
     889    else
     890      return 0;
     891  }
     892
     893  /**
     894    @brief Enables a specific mouse listener that has already been added.
     895    @param name Unique name of the listener.
     896    @return False if name was not found, true otherwise.
     897  */
     898  bool InputManager::enableMouseListener(const std::string& name)
     899  {
     900    // get pointer from the map with all stored listeners
     901    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name);
     902    if (mapIt == _getSingleton().listenersMouse_.end())
     903      return false;
     904    // see whether the listener is already in the list
     905    for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin();
     906          it != _getSingleton().listenersMouseActive_.end(); it++)
     907    {
     908      if ((*it) == (*mapIt).second)
     909        return true;
     910    }
     911    _getSingleton().listenersMouseActive_.push_back((*mapIt).second);
     912    return true;
     913  }
     914
     915  /**
     916    @brief Disables a specific mouse listener.
     917    @param name Unique name of the listener.
     918    @return False if name was not found, true otherwise.
     919  */
     920  bool InputManager::disableMouseListener(const std::string &name)
     921  {
     922    // get pointer from the map with all stored listeners
     923    std::map<std::string, MouseHandler*>::const_iterator mapIt = _getSingleton().listenersMouse_.find(name);
     924    if (mapIt == _getSingleton().listenersMouse_.end())
     925      return false;
     926    // look for the listener in the list
     927    for (std::list<MouseHandler*>::iterator it = _getSingleton().listenersMouseActive_.begin();
     928          it != _getSingleton().listenersMouseActive_.end(); it++)
     929    {
     930      if ((*it) == (*mapIt).second)
     931      {
     932        _getSingleton().listenersMouseActive_.erase(it);
     933        return true;
     934      }
     935    }
     936    return true;
     937  }
     938
     939
     940  // ###### JoyStickHandler ######
     941
     942  /**
     943    @brief Adds a new joy stick listener.
     944    @param listener Pointer to the listener object.
     945    @param name Unique name of the listener.
     946    @return True if added, false if name already existed.
     947  */
     948  bool InputManager::addJoyStickListener(JoyStickHandler* listener, const std::string& name)
     949  {
     950    if (_getSingleton().listenersJoySticks_.find(name) == _getSingleton().listenersJoySticks_.end())
     951    {
     952      _getSingleton().listenersJoySticks_[name] = listener;
     953      return true;
     954    }
     955    else
     956      return false;
     957  }
     958
     959  /**
     960    @brief Removes a JoyStick Listener from the list.
     961    @param name Unique name of the listener.
     962    @return True if removal was successful, false if name was not found.
     963  */
     964  bool InputManager::removeJoyStickListener(const std::string &name)
     965  {
     966    for (std::map<int, OIS::JoyStick*>::iterator itstick = _getSingleton().joySticks_.begin(); itstick != _getSingleton().joySticks_.end(); itstick++)
     967      disableJoyStickListener(name, (*itstick).first);
     968
     969    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name);
     970    if (it != _getSingleton().listenersJoySticks_.end())
     971    {
     972      _getSingleton().listenersJoySticks_.erase(it);
     973      return true;
     974    }
     975    else
     976      return false;
     977  }
     978
     979  /**
     980    @brief Returns the pointer to a listener.
     981    @param name Unique name of the listener.
     982    @return Pointer to the instance, 0 if name was not found.
     983  */
     984  JoyStickHandler* InputManager::getJoyStickListener(const std::string& name)
     985  {
     986    std::map<std::string, JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticks_.find(name);
     987    if (it != _getSingleton().listenersJoySticks_.end())
     988    {
     989      return (*it).second;
     990    }
     991    else
     992      return 0;
     993  }
     994
     995  /**
     996    @brief Enables a specific joy stick listener that has already been added.
     997    @param name Unique name of the listener.
     998    @return False if name or id was not found, true otherwise.
     999  */
     1000  bool InputManager::enableJoyStickListener(const std::string& name, const int id)
     1001  {
     1002    // get pointer from the map with all stored listeners
     1003    std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name);
     1004    if (listenerIt == _getSingleton().listenersJoySticks_.end())
     1005      return false;
     1006
     1007    // check for existance of the ID
     1008    std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id);
     1009    if (joyStickIt == _getSingleton().joySticks_.end())
     1010      return false;
     1011
     1012    // see whether the listener is already in the list
     1013    for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin();
     1014          it != _getSingleton().listenersJoySticksActive_[id].end(); it++)
     1015    {
     1016      if ((*it) == (*listenerIt).second)
     1017        return true;
     1018    }
     1019    _getSingleton().listenersJoySticksActive_[id].push_back((*listenerIt).second);
     1020    return true;
     1021  }
     1022
     1023  /**
     1024    @brief Disables a specific joy stick listener.
     1025    @param name Unique name of the listener.
     1026    @return False if name or id was not found, true otherwise.
     1027  */
     1028  bool InputManager::disableJoyStickListener(const std::string &name, int id)
     1029  {
     1030    // get pointer from the map with all stored listeners
     1031    std::map<std::string, JoyStickHandler*>::const_iterator listenerIt = _getSingleton().listenersJoySticks_.find(name);
     1032    if (listenerIt == _getSingleton().listenersJoySticks_.end())
     1033      return false;
     1034
     1035    // check for existance of the ID
     1036    std::map<int, OIS::JoyStick*>::const_iterator joyStickIt = _getSingleton().joySticks_.find(id);
     1037    if (joyStickIt == _getSingleton().joySticks_.end())
     1038      return false;
     1039
     1040    // look for the listener in the list
     1041    for (std::list<JoyStickHandler*>::iterator it = _getSingleton().listenersJoySticksActive_[id].begin();
     1042          it != _getSingleton().listenersJoySticksActive_[id].end(); it++)
     1043    {
     1044      if ((*it) == (*listenerIt).second)
     1045      {
     1046        _getSingleton().listenersJoySticksActive_[id].erase(it);
     1047        return true;
     1048      }
     1049    }
     1050    return true;
     1051  }
     1052
    6601053}
  • code/branches/input/src/core/InputManager.h

    r1195 r1203  
    4343#include "ois/OIS.h"
    4444#include "Tickable.h"
    45 #include "InputEvent.h"
     45//#include "InputEvent.h"
     46#include "InputHandler.h"
    4647
    4748namespace orxonox
    4849{
     50  class Mouse : public OIS::Mouse
     51  {
     52  };
     53
    4954  /**
    5055    @brief Captures and distributes mouse and keyboard input.
     
    7075    };
    7176
    72   public: // functions
    73     // METHODS OF THE STATIC INTERFACE
    74 
     77  public: // static functions
    7578    static bool initialise(size_t windowHnd, int windowWidth, int windowHeight,
    7679          bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
     80    static bool initialiseKeyboard();
     81    static bool initialiseMouse();
     82    static bool initialiseJoySticks();
     83
    7784    static void destroy();
     85    static void destroyKeyboard();
     86    static void destroyMouse();
     87    static void destroyJoySticks();
    7888
    7989    static void setWindowExtents(int width, int height);
     
    8595    static void setJoyStickButtonBindingState(bool bActive);
    8696
    87     static bool addKeyListener(OIS::KeyListener* listener, const std::string& name);
     97    static bool addKeyListener(KeyHandler* listener, const std::string& name);
    8898    static bool removeKeyListener  (const std::string& name);
    8999    static bool enableKeyListener  (const std::string& name);
    90100    static bool disableKeyListener (const std::string& name);
    91101    static bool isKeyListenerActive(const std::string& name);
    92     static OIS::KeyListener* getKeyListener(const std::string& name);
     102    static KeyHandler* getKeyListener(const std::string& name);
    93103
    94     static bool addMouseListener(OIS::MouseListener* listener, const std::string& name);
     104    static bool addMouseListener(MouseHandler* listener, const std::string& name);
    95105    static bool removeMouseListener  (const std::string& name);
    96106    static bool enableMouseListener  (const std::string& name);
    97107    static bool disableMouseListener (const std::string& name);
    98108    static bool isMouseListenerActive(const std::string& name);
    99     static OIS::MouseListener* getMouseListener(const std::string& name);
     109    static MouseHandler* getMouseListener(const std::string& name);
    100110
    101     static bool addJoyStickListener(OIS::JoyStickListener* listener, const std::string& name);
     111    static bool addJoyStickListener(JoyStickHandler* listener, const std::string& name);
    102112    static bool removeJoyStickListener  (const std::string& name);
    103     static bool enableJoyStickListener  (const std::string& name);
    104     static bool disableJoyStickListener (const std::string& name);
     113    static bool enableJoyStickListener  (const std::string& name, const int id);
     114    static bool disableJoyStickListener (const std::string& name, const int id);
    105115    static bool isJoyStickListenerActive(const std::string& name);
    106     static OIS::JoyStickListener* getJoyStickListener(const std::string& name);
     116    static JoyStickHandler* getJoyStickListener(const std::string& name);
    107117
    108118    // Temporary solutions. Will be removed soon!
     
    118128    // Intenal methods
    119129    bool _initialise(size_t, int, int, bool, bool, bool);
    120     void _initialiseKeyboard();
    121     void _initialiseMouse();
    122     void _initialiseJoySticks();
     130    bool _initialiseKeyboard();
     131    bool _initialiseMouse();
     132    bool _initialiseJoySticks();
     133
    123134    void _destroy();
    124     //void _setDefaultState();
    125     bool _loadBindings();
    126     void _clearBindings();
    127     void _setNumberOfJoysticks(int size);
     135    void _destroyKeyboard();
     136    void _destroyMouse();
     137    void _destroyJoySticks();
     138
     139    //void _setNumberOfJoysticks(int size);
    128140
    129141    void tick(float dt);
     
    145157
    146158  private: // variables
    147     OIS::InputManager* inputSystem_;    //!< OIS input manager
    148     OIS::Keyboard*     keyboard_;       //!< OIS mouse
    149     OIS::Mouse*        mouse_;          //!< OIS keyboard
    150     std::vector<OIS::JoyStick*> joySticks_;       //!< OIS joy sticks
     159    OIS::InputManager* inputSystem_;            //!< OIS input manager
     160    OIS::Keyboard*     keyboard_;               //!< OIS mouse
     161    OIS::Mouse*        mouse_;                  //!< OIS keyboard
     162    std::map<int, OIS::JoyStick*> joySticks_;   //!< OIS joy sticks
    151163
    152164    InputState state_;
    153165    InputState stateRequest_;
    154166
    155     bool bKeyBindingsActive_;
    156     bool bMouseButtonBindingsActive_;
    157     std::vector<bool> bJoyStickButtonBindingsActive_;
     167    std::map<std::string, KeyHandler*>      listenersKey_;
     168    std::map<std::string, MouseHandler*>    listenersMouse_;
     169    std::map<std::string, JoyStickHandler*> listenersJoySticks_;
    158170
    159     std::map<std::string, OIS::KeyListener*>      listenersKey_;
    160     std::map<std::string, OIS::MouseListener*>    listenersMouse_;
    161     std::map<std::string, OIS::JoyStickListener*> listenersJoySticks_;
    162 
    163     std::list<OIS::KeyListener*>      listenersKeyActive_;
    164     std::list<OIS::MouseListener*>    listenersMouseActive_;
    165     std::list<OIS::JoyStickListener*> listenersJoySticksActive_;
     171    std::list<KeyHandler*>      listenersKeyActive_;
     172    std::list<MouseHandler*>    listenersMouseActive_;
     173    std::map< int, std::list<JoyStickHandler*> > listenersJoySticksActive_;
    166174
    167175    std::list<OIS::KeyCode>         keysDown_;
    168176    std::list<OIS::MouseButtonID>   mouseButtonsDown_;
    169     std::vector< std::list<int> >   joyStickButtonsDown_;
    170 
    171 
    172     /** denotes the maximum number of different keys there are in OIS.
    173         256 should be ok since the highest number in the OIS enum is 237. */
    174     static const int numberOfKeys_s = 256;
    175     //! Array of input events for every pressed key
    176     std::string bindingsKeyPress_  [numberOfKeys_s];
    177     //! Array of input events for every released key
    178     std::string bindingsKeyRelease_[numberOfKeys_s];
    179     //! Array of input events for every held key
    180     std::string bindingsKeyHold_   [numberOfKeys_s];
    181 
    182     /** denotes the maximum number of different buttons there are in OIS.
    183         16 should be ok since the highest number in the OIS enum is 7. */
    184     static const int numberOfMouseButtons_s = 16;
    185     //! Array of input events for every pressed mouse button
    186     std::string bindingsMouseButtonPress_  [numberOfMouseButtons_s];
    187     //! Array of input events for every released mouse button
    188     std::string bindingsMouseButtonRelease_[numberOfMouseButtons_s];
    189     //! Array of input events for every held mouse button
    190     std::string bindingsMouseButtonHold_   [numberOfMouseButtons_s];
    191 
    192     /** denotes the maximum number of different buttons there are in OIS.
    193         32 should be ok. */
    194     static const int numberOfJoyStickButtons_s = 32;
    195     std::vector< std::vector< std::string > > bindingsJoyStickButtonPress_;
    196     std::vector< std::vector< std::string > > bindingsJoyStickButtonRelease_;
    197     std::vector< std::vector< std::string > > bindingsJoyStickButtonHold_;
     177    std::map< int, std::list<int> > joySticksButtonsDown_;
    198178
    199179  };
Note: See TracChangeset for help on using the changeset viewer.