Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 24, 2008, 9:40:23 PM (16 years ago)
Author:
rgrieder
Message:
  • InputManager works so far, but has to be largely extended
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/input/src/core/InputManager.cc

    r1089 r1182  
    3636#include "CoreIncludes.h"
    3737#include "Debug.h"
    38 #include "InputEventListener.h"
    39 #include "InputHandler.h"
    4038#include "InputBuffer.h"
    4139#include "ConsoleCommand.h"
     40#include "util/Convert.h"
    4241
    4342namespace orxonox
    4443{
    45   ConsoleCommand(InputManager, setInputMode, AccessLevel::Admin, true).setDefaultValue(0, IM_INGAME);
    46 
    4744  /**
    4845    @brief Constructor only resets the pointer values to 0.
     
    5047  InputManager::InputManager() :
    5148      inputSystem_(0), keyboard_(0), mouse_(0),
    52       currentMode_(IM_UNINIT), setMode_(IM_UNINIT),
    53       handlerGUI_(0), handlerBuffer_(0), handlerGame_(0)
     49      state_(IS_UNINIT), stateRequest_(IS_UNINIT)
    5450  {
    5551    RegisterObject(InputManager);
    56   }
    57 
    58   /**
    59     @brief Destructor only called at the end of the program
    60   */
    61   InputManager::~InputManager()
    62   {
    63     this->destroy();
    6452  }
    6553
     
    6856    @return A reference to the only instance of the InputManager
    6957  */
    70   InputManager& InputManager::getSingleton()
     58  InputManager& InputManager::_getSingleton()
    7159  {
    7260    static InputManager theOnlyInstance;
    7361    return theOnlyInstance;
     62  }
     63
     64  /**
     65    @brief Destructor only called at the end of the program
     66  */
     67  InputManager::~InputManager()
     68  {
     69    this->_destroy();
    7470  }
    7571
     
    8177    @param windowHeight The height of the render window
    8278  */
    83   bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
    84   {
    85     if (!this->inputSystem_)
     79  bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight)
     80  {
     81    if (!inputSystem_)
    8682    {
    8783      // Setup basic variables
     
    9389      paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    9490
    95 #if defined OIS_LINUX_PLATFORM
    96       paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
    97 #endif
     91//#if defined OIS_LINUX_PLATFORM
     92//      paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
     93//#endif
    9894
    9995      try
     
    107103        COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl;
    108104
     105        //TODO: check for already pressed keys
     106
    109107        // create a mouse. If none are available the exception is caught.
    110108        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
     
    112110
    113111        // Set mouse region
    114         this->setWindowExtents(windowWidth, windowHeight);
     112        setWindowExtents(windowWidth, windowHeight);
     113
     114        this->state_ = IS_NONE;
    115115      }
    116116      catch (OIS::Exception ex)
     
    118118        // something went wrong with the initialisation
    119119        COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl;
    120         this->inputSystem_ = 0;
     120        inputSystem_ = 0;
    121121        return false;
    122122      }
    123123    }
    124124
    125     // create the handlers
    126     this->handlerGUI_ = new InputHandlerGUI();
    127     this->handlerGame_ = new InputHandlerGame();
    128     //this->handlerBuffer_ = new InputBuffer();
    129     this->handlerGame_->loadBindings();
    130 
    131     /*COUT(ORX_DEBUG) << "*** InputManager: Loading key bindings..." << std::endl;
    132     // load the key bindings
    133     InputEvent empty = {0, false, 0, 0, 0};
    134     for (int i = 0; i < this->numberOfKeys_; i++)
    135       this->bindingsKeyPressed_[i] = empty;
    136 
    137     //assign 'abort' to the escape key
    138     this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
    139     COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;*/
    140 
    141     return true;
     125    keyboard_->setEventCallback(this);
     126    mouse_->setEventCallback(this);
     127
     128    addKeyListener(new InputBuffer(), "buffer");
     129
     130    _loadBindings();
     131
     132    COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;
     133
     134    return true;
     135  }
     136
     137  void InputManager::_loadBindings()
     138  {
     139    for (int i = 0; i < numberOfKeys_s; i++)
     140    {
     141      // simply write the key number (i) in the string
     142      this->bindingsKeyPress_[i] = "";
     143      this->bindingsKeyRelease_[i] = "";
     144    }
     145    this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole";
     146    this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
     147    this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
    142148  }
    143149
     
    145151    @brief Destroys all the created input devices and handlers.
    146152  */
    147   void InputManager::destroy()
     153  void InputManager::_destroy()
    148154  {
    149155    COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl;
    150     if (this->mouse_)
    151       this->inputSystem_->destroyInputObject(mouse_);
    152     if (this->keyboard_)
    153       this->inputSystem_->destroyInputObject(keyboard_);
    154     if (this->inputSystem_)
    155       OIS::InputManager::destroyInputSystem(this->inputSystem_);
    156 
    157     this->mouse_         = 0;
    158     this->keyboard_      = 0;
    159     this->inputSystem_   = 0;
    160 
    161     if (this->handlerBuffer_)
    162       delete this->handlerBuffer_;
    163     if (this->handlerGame_)
    164       delete this->handlerGame_;
    165     if (this->handlerGUI_)
    166       delete this->handlerGUI_;
    167 
    168     this->handlerBuffer_ = 0;
    169     this->handlerGame_   = 0;
    170     this->handlerGUI_    = 0;
     156    if (mouse_)
     157      inputSystem_->destroyInputObject(mouse_);
     158    if (keyboard_)
     159      inputSystem_->destroyInputObject(keyboard_);
     160    if (inputSystem_)
     161      OIS::InputManager::destroyInputSystem(inputSystem_);
     162
     163    mouse_         = 0;
     164    keyboard_      = 0;
     165    inputSystem_   = 0;
     166
     167    OIS::KeyListener* buffer = keyListeners_["buffer"];
     168    if (buffer)
     169    {
     170      this->removeKeyListener("buffer");
     171      delete buffer;
     172      buffer = 0;
     173    }
    171174
    172175    COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl;
     
    179182  void InputManager::tick(float dt)
    180183  {
     184    if (state_ == IS_UNINIT)
     185      return;
     186
    181187    // reset the game if it has changed
    182     if (this->currentMode_ != this->setMode_)
    183     {
    184       switch (this->setMode_)
    185       {
    186       case IM_GUI:
    187         this->mouse_->setEventCallback(this->handlerGUI_);
    188         this->keyboard_->setEventCallback(this->handlerGUI_);
     188    if (state_ != stateRequest_)
     189    {
     190      if (stateRequest_ != IS_CUSTOM)
     191        _setDefaultState();
     192
     193      switch (stateRequest_)
     194      {
     195      case IS_NORMAL:
    189196        break;
    190       case IM_INGAME:
    191         this->mouse_->setEventCallback(this->handlerGame_);
    192         this->keyboard_->setEventCallback(this->handlerGame_);
     197      case IS_GUI:
     198        //FIXME: do stuff
    193199        break;
    194       case IM_KEYBOARD:
    195         this->mouse_->setEventCallback(this->handlerGame_);
    196         this->keyboard_->setEventCallback(this->handlerBuffer_);
     200      case IS_CONSOLE:
     201        if (this->keyListeners_.find("buffer") != this->keyListeners_.end())
     202          this->activeKeyListeners_.push_back(this->keyListeners_["buffer"]);
     203        this->bDefaultKeyInput = false;
    197204        break;
    198       case IM_UNINIT:
    199         this->mouse_->setEventCallback(0);
    200         this->keyboard_->setEventCallback(0);
     205      case IS_CUSTOM:
     206        // don't do anything
    201207        break;
    202208      }
    203       this->currentMode_ = this->setMode_;
     209      state_ = stateRequest_;
    204210    }
    205211
     
    207213    if (mouse_)
    208214      mouse_->capture();
    209 
    210215    if (keyboard_)
    211216      keyboard_->capture();
    212217  }
     218
     219  void InputManager::_setDefaultState()
     220  {
     221    this->activeJoyStickListeners_.clear();
     222    this->activeKeyListeners_.clear();
     223    this->activeMouseListeners_.clear();
     224    this->bDefaultKeyInput      = true;
     225    this->bDefaultMouseInput    = true;
     226    this->bDefaultJoyStickInput = true;
     227  }
     228
     229
     230  /**
     231    @brief Event handler for the keyPressed Event.
     232    @param e Event information
     233  */
     234  bool InputManager::keyPressed(const OIS::KeyEvent &e)
     235  {
     236    this->keysDown_.push_back(e.key);
     237
     238    if (this->bDefaultKeyInput)
     239    {
     240      // find the appropriate key binding
     241      std::string cmdStr = bindingsKeyPress_[int(e.key)];
     242      if (cmdStr != "")
     243      {
     244        CommandExecutor::execute(cmdStr);
     245        COUT(3) << "Executing command: " << cmdStr << std::endl;
     246      }
     247    }
     248    else
     249    {
     250      for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++)
     251        (*it)->keyPressed(e);
     252    }
     253    return true;
     254  }
     255
     256  /**
     257    @brief Event handler for the keyReleased Event.
     258    @param e Event information
     259  */
     260  bool InputManager::keyReleased(const OIS::KeyEvent &e)
     261  {
     262    // remove the key from the keysDown_ list
     263    for (std::list<OIS::KeyCode>::iterator it = keysDown_.begin(); it != keysDown_.end(); it++)
     264    {
     265      if (*it == e.key)
     266      {
     267        keysDown_.erase(it);
     268        break;
     269      }
     270    }
     271
     272    if (this->bDefaultKeyInput)
     273    {
     274      // find the appropriate key binding
     275      std::string cmdStr = bindingsKeyRelease_[int(e.key)];
     276      if (cmdStr != "")
     277      {
     278        CommandExecutor::execute(cmdStr);
     279        COUT(3) << "Executing command: " << cmdStr << std::endl;
     280      }
     281    }
     282    else
     283    {
     284      for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++)
     285        (*it)->keyReleased(e);
     286    }
     287    return true;
     288  }
     289
     290  /**
     291    @brief Event handler for the mouseMoved Event.
     292    @param e Event information
     293  */
     294  bool InputManager::mouseMoved(const OIS::MouseEvent &e)
     295  {
     296    return true;
     297  }
     298
     299  /**
     300    @brief Event handler for the mousePressed Event.
     301    @param e Event information
     302    @param id The ID of the mouse button
     303  */
     304  bool InputManager::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     305  {
     306    return true;
     307  }
     308
     309  /**
     310    @brief Event handler for the mouseReleased Event.
     311    @param e Event information
     312    @param id The ID of the mouse button
     313  */
     314  bool InputManager::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
     315  {
     316    return true;
     317  }
     318
     319  bool InputManager::buttonPressed(const OIS::JoyStickEvent &arg, int button)
     320  {
     321    return true;
     322  }
     323
     324  bool InputManager::buttonReleased(const OIS::JoyStickEvent &arg, int button)
     325  {
     326    return true;
     327  }
     328
     329  bool InputManager::axisMoved(const OIS::JoyStickEvent &arg, int axis)
     330  {
     331    return true;
     332  }
     333
     334  bool InputManager::sliderMoved(const OIS::JoyStickEvent &arg, int id)
     335  {
     336    return true;
     337  }
     338
     339  bool InputManager::povMoved(const OIS::JoyStickEvent &arg, int id)
     340  {
     341    return true;
     342  }
     343
     344
    213345
    214346  /**
     
    221353  {
    222354    // Set mouse region (if window resizes, we should alter this to reflect as well)
    223     const OIS::MouseState &mouseState = mouse_->getMouseState();
     355    const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
    224356    mouseState.width  = width;
    225357    mouseState.height = height;
     
    231363    @remark Only has an affect if the mode actually changes
    232364  */
    233   void InputManager::setInputMode(int mode)
    234   {
    235     if (mode > 0 && mode < 4)
    236       getSingleton().setMode_ = (InputMode)mode;
     365  void InputManager::setInputState(const InputState state)
     366  {
     367    _getSingleton().stateRequest_ = state;
    237368  }
    238369
     
    241372    @return The current input mode.
    242373  */
    243   InputMode InputManager::getInputMode()
    244   {
    245     return this->currentMode_;
    246   }
    247 
    248   void InputManager::feedInputBuffer(InputBuffer* buffer)
    249   {
    250     this->handlerBuffer_ = buffer;
    251   }
    252 
     374  InputManager::InputState InputManager::getInputState()
     375  {
     376    return _getSingleton().state_;
     377  }
     378
     379  void InputManager::destroy()
     380  {
     381    _getSingleton()._destroy();
     382  }
     383
     384  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
     385  {
     386    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight);
     387  }
     388
     389  bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name)
     390  {
     391    if (_getSingleton().keyListeners_.find(name) == _getSingleton().keyListeners_.end())
     392    {
     393      _getSingleton().keyListeners_[name] = listener;
     394      return true;
     395    }
     396    else
     397      return false;
     398  }
     399
     400  bool InputManager::removeKeyListener(const std::string &name)
     401  {
     402    std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name);
     403    if (it != _getSingleton().keyListeners_.end())
     404    {
     405      _getSingleton().keyListeners_.erase(it);
     406      return true;
     407    }
     408    else
     409      return false;
     410  }
     411
     412  OIS::KeyListener* InputManager::getKeyListener(const std::string& name)
     413  {
     414    std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name);
     415    if (it != _getSingleton().keyListeners_.end())
     416    {
     417      return (*it).second;
     418    }
     419    else
     420      return 0;
     421  }
    253422
    254423}
Note: See TracChangeset for help on using the changeset viewer.