Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 27, 2008, 1:50:28 PM (17 years ago)
Author:
rgrieder
Message:
  • OIS initialise should be complete, but need to use cmake to determine version number..
  • merged trunk back
File:
1 edited

Legend:

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

    r1182 r1193  
    2828
    2929/**
    30  @file
    31  @brief Implementation of a little Input handler that distributes everything
    32         coming from OIS.
     30  @file
     31  @brief Implementation of the InputManager that captures all the input from OIS
     32         and redirects it to listeners if necessary.
    3333 */
    3434
     
    4242namespace orxonox
    4343{
    44   /**
    45     @brief Constructor only resets the pointer values to 0.
     44  // ###############################
     45  // ###    Internal Methods     ###
     46  // ###############################
     47
     48  /**
     49    @brief Constructor only sets member fields to initial zero values
     50           and registers the class in the class hierarchy.
    4651  */
    4752  InputManager::InputManager() :
     
    4954      state_(IS_UNINIT), stateRequest_(IS_UNINIT)
    5055  {
     56    // overwrite every key binding with ""
     57    _clearBindings();
     58    _setNumberOfJoysticks(0);
     59
    5160    RegisterObject(InputManager);
    5261  }
     
    6372
    6473  /**
    65     @brief Destructor only called at the end of the program
     74    @brief Destructor only called at the end of the program, after main.
    6675  */
    6776  InputManager::~InputManager()
     
    7180
    7281  /**
    73     @brief Creates the OIS::InputMananger, the keyboard and the mouse and
    74            assigns the key bindings.
     82    @brief Creates the OIS::InputMananger, the keyboard, the mouse and
     83           the joysticks and assigns the key bindings.
    7584    @param windowHnd The window handle of the render window
    7685    @param windowWidth The width of the render window
    7786    @param windowHeight The height of the render window
    7887  */
    79   bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight)
    80   {
    81     if (!inputSystem_)
    82     {
    83       // Setup basic variables
     88  bool InputManager::_initialise(size_t windowHnd, int windowWidth, int windowHeight,
     89        bool createKeyboard, bool createMouse, bool createJoySticks)
     90  {
     91    if (state_ == IS_UNINIT)
     92    {
     93      CCOUT(ORX_DEBUG) << "Initialising OIS components..." << std::endl;
     94
    8495      OIS::ParamList paramList;
    8596      std::ostringstream windowHndStr;
     
    95106      try
    96107      {
    97         // Create inputsystem
    98108        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
    99         COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl;
    100 
    101         // create a keyboard. If none are available the exception is caught.
    102         keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
    103         COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl;
    104 
    105         //TODO: check for already pressed keys
    106 
    107         // create a mouse. If none are available the exception is caught.
    108         mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
    109         COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl;
    110 
    111         // Set mouse region
    112         setWindowExtents(windowWidth, windowHeight);
    113 
    114         this->state_ = IS_NONE;
     109        CCOUT(ORX_DEBUG) << "Created OIS input system" << std::endl;
    115110      }
    116111      catch (OIS::Exception ex)
    117112      {
    118         // something went wrong with the initialisation
    119         COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl;
     113        CCOUT(ORX_ERROR) << "Error: Failed creating an OIS input system."
     114            << "OIS message: \"" << ex.eText << "\"" << std::endl;
    120115        inputSystem_ = 0;
    121116        return false;
    122117      }
    123     }
    124 
    125     keyboard_->setEventCallback(this);
    126     mouse_->setEventCallback(this);
    127 
     118
     119      if (createKeyboard)
     120        _initialiseKeyboard();
     121
     122      if (createMouse)
     123        _initialiseMouse();
     124
     125      if (createJoySticks)
     126        _initialiseJoySticks();
     127
     128      // Set mouse/joystick region
     129      setWindowExtents(windowWidth, windowHeight);
     130
     131      this->state_ = IS_NONE;
     132      CCOUT(ORX_DEBUG) << "Initialising OIS components done." << std::endl;
     133    }
     134    else
     135    {
     136      CCOUT(ORX_WARNING) << "Warning: OIS compoments already initialised, skipping" << std::endl;
     137    }
     138
     139    // InputManager holds the input buffer --> create one and add it.
    128140    addKeyListener(new InputBuffer(), "buffer");
    129141
    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     }
     142    // Read all the key bindings and assign them
     143    if (!_loadBindings())
     144      return false;
     145
     146    CCOUT(ORX_DEBUG) << "Initialising complete." << std::endl;
     147    return true;
     148  }
     149
     150  /**
     151    @brief Creates a keyboard and sets the event handler.
     152  */
     153  void InputManager::_initialiseKeyboard()
     154  {
     155    try
     156    {
     157#if (OIS_VERSION >> 8) == 0x0100
     158    if (inputSystem_->numKeyboards() > 0)
     159#elif (OIS_VERSION >> 8) == 0x0102
     160    if (inputSystem_->getNumberOfDevices(OIS::OISKeyboard) > 0)
     161#endif
     162      {
     163        keyboard_ = (OIS::Keyboard*)inputSystem_->createInputObject(OIS::OISKeyboard, true);
     164        // register our listener in OIS.
     165        keyboard_->setEventCallback(this);
     166        // note: OIS will not detect keys that have already been down when the keyboard was created.
     167        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
     168      }
     169      else
     170        CCOUT(ORX_WARNING) << "Warning: No keyboard found!" << std::endl;
     171    }
     172    catch (OIS::Exception ex)
     173    {
     174      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS keyboard\n"
     175          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
     176      keyboard_ = 0;
     177    }
     178  }
     179
     180  /**
     181    @brief Creates a mouse and sets the event handler.
     182  */
     183  void InputManager::_initialiseMouse()
     184  {
     185    try
     186    {
     187#if (OIS_VERSION >> 8) == 0x0100
     188    if (inputSystem_->numMice() > 0)
     189#elif (OIS_VERSION >> 8) == 0x0102
     190    if (inputSystem_->getNumberOfDevices(OIS::OISMouse) > 0)
     191#endif
     192      {
     193        mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
     194        // register our listener in OIS.
     195        mouse_->setEventCallback(this);
     196        CCOUT(ORX_DEBUG) << "Created OIS keyboard" << std::endl;
     197      }
     198      else
     199        CCOUT(ORX_WARNING) << "Warning: No mouse found!" << std::endl;
     200    }
     201    catch (OIS::Exception ex)
     202    {
     203      CCOUT(ORX_WARNING) << "Warning: Failed to create an OIS mouse\n"
     204          << "OIS error message: \"" << ex.eText << "\"" << std::endl;
     205      mouse_ = 0;
     206    }
     207  }
     208
     209  /**
     210    @brief Creates all joy sticks and sets the event handler.
     211  */
     212  void InputManager::_initialiseJoySticks()
     213  {
     214#if (OIS_VERSION >> 8) == 0x0100
     215    if (inputSystem_->numJoySticks() > 0)
     216    {
     217      _setNumberOfJoysticks(inputSystem_->numJoySticks());
     218#elif (OIS_VERSION >> 8) == 0x0102
     219    if (inputSystem_->getNumberOfDevices(OIS::OISJoyStick) > 0)
     220    {
     221      _setNumberOfJoysticks(inputSystem_->getNumberOfDevices(OIS::OISJoyStick));
     222#endif
     223      for (std::vector<OIS::JoyStick*>::iterator it = joySticks_.begin(); it != joySticks_.end(); it++)
     224      {
     225        try
     226        {
     227          *it = static_cast<OIS::JoyStick*>(inputSystem_->createInputObject(OIS::OISJoyStick, true));
     228          // register our listener in OIS.
     229          (*it)->setEventCallback(this);
     230          CCOUT(ORX_DEBUG) << "Created OIS joy stick with ID " << (*it)->getID() << std::endl;
     231        }
     232        catch (OIS::Exception ex)
     233        {
     234          CCOUT(ORX_WARNING) << "Warning: Failed to create OIS joy stick with ID" << (*it)->getID() << "\n"
     235              << "OIS error message: \"" << ex.eText << "\"" << std::endl;
     236          (*it) = 0;
     237        }
     238      }
     239    }
     240    else
     241      CCOUT(ORX_WARNING) << "Warning: Joy stick support requested, but no joy stick was found" << std::endl;
     242  }
     243
     244  /**
     245    @brief Resizes all lists related to joy sticks and sets joy stick bindings to "".
     246    @param size Number of joy sticks available.
     247  */
     248  void InputManager::_setNumberOfJoysticks(int size)
     249  {
     250    this->bindingsJoyStickButtonHold_   .resize(size);
     251    this->bindingsJoyStickButtonPress_  .resize(size);
     252    this->bindingsJoyStickButtonRelease_.resize(size);
     253    this->bJoyStickButtonBindingsActive_.resize(size);
     254    this->joyStickButtonsDown_          .resize(size);
     255    this->joySticks_                    .resize(size);
     256    for (int j = 0; j < size; j++)
     257    {
     258      bindingsJoyStickButtonPress_  [j].resize(numberOfJoyStickButtons_s);
     259      bindingsJoyStickButtonRelease_[j].resize(numberOfJoyStickButtons_s);
     260      bindingsJoyStickButtonHold_   [j].resize(numberOfJoyStickButtons_s);
     261      for (int i = 0; i < numberOfJoyStickButtons_s; i++)
     262      {
     263        this->bindingsJoyStickButtonPress_  [j][i] = "";
     264        this->bindingsJoyStickButtonRelease_[j][i] = "";
     265        this->bindingsJoyStickButtonHold_   [j][i] = "";
     266      }
     267    }
     268  }
     269
     270  /**
     271    @brief Loads the key and button bindings.
     272  */
     273  bool InputManager::_loadBindings()
     274  {
     275    CCOUT(ORX_DEBUG) << "Loading key bindings..." << std::endl;
     276
     277    // clear all the bindings at first.
     278    _clearBindings();
     279
     280    // TODO: Insert the code to load the bindings from file.
    145281    this->bindingsKeyPress_[OIS::KC_NUMPADENTER] = "activateConsole";
    146282    this->bindingsKeyPress_[OIS::KC_ESCAPE] = "exit";
    147283    this->bindingsKeyHold_[OIS::KC_U] = "exec disco.txt";
    148   }
    149 
    150   /**
    151     @brief Destroys all the created input devices and handlers.
     284
     285    CCOUT(ORX_DEBUG) << "Loading key bindings done." << std::endl;
     286    return true;
     287  }
     288
     289  /**
     290    @brief Overwrites all bindings with ""
     291  */
     292  void InputManager::_clearBindings()
     293  {
     294    for (int i = 0; i < numberOfKeys_s; i++)
     295    {
     296      this->bindingsKeyPress_  [i] = "";
     297      this->bindingsKeyRelease_[i] = "";
     298      this->bindingsKeyHold_   [i] = "";
     299    }
     300    for (int i = 0; i < numberOfMouseButtons_s; i++)
     301    {
     302      this->bindingsMouseButtonPress_  [i] = "";
     303      this->bindingsMouseButtonRelease_[i] = "";
     304      this->bindingsMouseButtonHold_   [i] = "";
     305    }
     306    for (unsigned int j = 0; j < joySticks_.size(); j++)
     307    {
     308      for (int i = 0; i < numberOfJoyStickButtons_s; i++)
     309      {
     310        this->bindingsJoyStickButtonPress_  [j][i] = "";
     311        this->bindingsJoyStickButtonRelease_[j][i] = "";
     312        this->bindingsJoyStickButtonHold_   [j][i] = "";
     313      }
     314    }
     315  }
     316
     317  /**
     318    @brief Destroys all the created input devices and sets the InputManager to construction state.
    152319  */
    153320  void InputManager::_destroy()
    154321  {
    155     COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl;
    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     }
    174 
    175     COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl;
    176   }
     322    CCOUT(ORX_DEBUG) << "Destroying ..." << std::endl;
     323
     324    if (state_ != IS_UNINIT)
     325    {
     326      this->listenersKeyActive_.clear();
     327      this->listenersMouseActive_.clear();
     328      this->listenersJoySticksActive_.clear();
     329      this->listenersKey_.clear();
     330      this->listenersMouse_.clear();
     331      this->listenersJoySticks_.clear();
     332
     333      this->keysDown_.clear();
     334      this->mouseButtonsDown_.clear();
     335
     336      _clearBindings();
     337
     338      if (keyboard_)
     339        inputSystem_->destroyInputObject(keyboard_);
     340      keyboard_ = 0;
     341
     342      if (mouse_)
     343        inputSystem_->destroyInputObject(mouse_);
     344      mouse_ = 0;
     345
     346      if (joySticks_.size() > 0)
     347      {
     348        for (unsigned int i = 0; i < joySticks_.size(); i++)
     349        {
     350          if (joySticks_[i] != 0)
     351            inputSystem_->destroyInputObject(joySticks_[i]);
     352        }
     353        _setNumberOfJoysticks(0);
     354      }
     355
     356      if (inputSystem_)
     357        OIS::InputManager::destroyInputSystem(inputSystem_);
     358      inputSystem_ = 0;
     359
     360      if (listenersKey_.find("buffer") != listenersKey_.end())
     361        delete listenersKey_["buffer"];
     362
     363      this->state_ = IS_UNINIT;
     364    }
     365    else
     366      CCOUT(ORX_WARNING) << "Warning: Cannot be destroyed, since not initialised." << std::endl;
     367
     368    CCOUT(ORX_DEBUG) << "Destroying done." << std::endl;
     369  }
     370
     371
     372  // ###############################
     373  // ###  Public Member Methods  ###
     374  // ###############################
    177375
    178376  /**
     
    188386    if (state_ != stateRequest_)
    189387    {
    190       if (stateRequest_ != IS_CUSTOM)
    191         _setDefaultState();
    192 
    193388      switch (stateRequest_)
    194389      {
    195390      case IS_NORMAL:
     391        this->listenersKeyActive_.clear();
     392        this->listenersMouseActive_.clear();
     393        this->listenersJoySticksActive_.clear();
     394        this->bKeyBindingsActive_            = true;
     395        this->bMouseButtonBindingsActive_    = true;
     396        //this->bJoySticksButtonBindingsActive_ = true;
    196397        break;
     398
    197399      case IS_GUI:
    198         //FIXME: do stuff
     400        // FIXME: do stuff
    199401        break;
     402
    200403      case IS_CONSOLE:
    201         if (this->keyListeners_.find("buffer") != this->keyListeners_.end())
    202           this->activeKeyListeners_.push_back(this->keyListeners_["buffer"]);
    203         this->bDefaultKeyInput = false;
     404        this->listenersKeyActive_.clear();
     405        this->listenersMouseActive_.clear();
     406        //this->listenersJoyStickActive_.clear();
     407        this->bKeyBindingsActive_            = false;
     408        this->bMouseButtonBindingsActive_    = true;
     409        //this->bJoyStickButtonBindingsActive_ = true;
     410        if (listenersKey_.find("buffer") != listenersKey_.end())
     411          listenersKeyActive_.push_back(listenersKey_["buffer"]);
     412        else
     413        {
     414          // someone fiddled with the InputBuffer
     415          CCOUT(2) << "Error: Cannot redirect input to console, InputBuffer instance missing." << std::endl;
     416          this->bKeyBindingsActive_ = true;
     417        }
    204418        break;
     419
     420      case IS_NONE:
     421        this->listenersKeyActive_.clear();
     422        this->listenersMouseActive_.clear();
     423        //this->listenersJoyStickActive_.clear();
     424        this->bKeyBindingsActive_            = false;
     425        this->bMouseButtonBindingsActive_    = false;
     426        //this->bJoyStickButtonBindingsActive_ = false;
     427        break;
     428
    205429      case IS_CUSTOM:
    206430        // don't do anything
     
    210434    }
    211435
    212     // capture all the input. That calls the event handlers.
     436    // Capture all the input. This calls the event handlers in InputManager.
    213437    if (mouse_)
    214438      mouse_->capture();
     
    217441  }
    218442
    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   }
     443  /*void InputManager::_setDefaultState()
     444  {
     445    this->listenersKeyActive_.clear();
     446    this->listenersMouseActive_.clear();
     447    this->listenersJoyStickActive_.clear();
     448    this->bKeyBindingsActive_            = true;
     449    this->bMouseButtonBindingsActive_    = true;
     450    this->bJoyStickButtonBindingsActive_ = true;
     451  }*/
    228452
    229453
     
    236460    this->keysDown_.push_back(e.key);
    237461
    238     if (this->bDefaultKeyInput)
     462    if (this->bKeyBindingsActive_)
    239463    {
    240464      // find the appropriate key binding
     
    248472    else
    249473    {
    250       for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++)
     474      for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
    251475        (*it)->keyPressed(e);
    252476    }
     
    270494    }
    271495
    272     if (this->bDefaultKeyInput)
     496    if (this->bKeyBindingsActive_)
    273497    {
    274498      // find the appropriate key binding
     
    282506    else
    283507    {
    284       for (std::list<OIS::KeyListener*>::const_iterator it = activeKeyListeners_.begin(); it != activeKeyListeners_.end(); it++)
     508      for (std::list<OIS::KeyListener*>::const_iterator it = listenersKeyActive_.begin(); it != listenersKeyActive_.end(); it++)
    285509        (*it)->keyReleased(e);
    286510    }
     
    343567
    344568
     569  // ################################
     570  // ### Static Interface Methods ###
     571  // ################################
    345572
    346573  /**
     
    352579  void InputManager::setWindowExtents(int width, int height)
    353580  {
    354     // Set mouse region (if window resizes, we should alter this to reflect as well)
    355     const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
    356     mouseState.width  = width;
    357     mouseState.height = height;
     581    if (_getSingleton().mouse_)
     582    {
     583      // Set mouse region (if window resizes, we should alter this to reflect as well)
     584      const OIS::MouseState &mouseState = _getSingleton().mouse_->getMouseState();
     585      mouseState.width  = width;
     586      mouseState.height = height;
     587    }
    358588  }
    359589
     
    382612  }
    383613
    384   bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight)
    385   {
    386     return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight);
     614  bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight,
     615    bool createKeyboard, bool createMouse, bool createJoySticks)
     616  {
     617    return _getSingleton()._initialise(windowHnd, windowWidth, windowHeight,
     618          createKeyboard, createMouse, createJoySticks);
    387619  }
    388620
    389621  bool InputManager::addKeyListener(OIS::KeyListener *listener, const std::string& name)
    390622  {
    391     if (_getSingleton().keyListeners_.find(name) == _getSingleton().keyListeners_.end())
    392     {
    393       _getSingleton().keyListeners_[name] = listener;
     623    if (_getSingleton().listenersKey_.find(name) == _getSingleton().listenersKey_.end())
     624    {
     625      _getSingleton().listenersKey_[name] = listener;
    394626      return true;
    395627    }
     
    400632  bool InputManager::removeKeyListener(const std::string &name)
    401633  {
    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);
     634    std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name);
     635    if (it != _getSingleton().listenersKey_.end())
     636    {
     637      _getSingleton().listenersKey_.erase(it);
    406638      return true;
    407639    }
     
    412644  OIS::KeyListener* InputManager::getKeyListener(const std::string& name)
    413645  {
    414     std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().keyListeners_.find(name);
    415     if (it != _getSingleton().keyListeners_.end())
     646    std::map<std::string, OIS::KeyListener*>::iterator it = _getSingleton().listenersKey_.find(name);
     647    if (it != _getSingleton().listenersKey_.end())
    416648    {
    417649      return (*it).second;
Note: See TracChangeset for help on using the changeset viewer.