C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/InputManager.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 Copyright (C) 2006 Michael Harnetty
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 -----------------------------------------------------------------------------
00019 */
00020 
00021 #include "InputManager.h"
00022 #include "OgreStringConverter.h"
00023 #include "GameApp.h"
00024 
00025 namespace GameEngine {
00026 
00027         char const * const KeyPressedEvent::eventName = "KeyPressed";
00028         char const * const KeyReleasedEvent::eventName = "KeyReleased";
00029         char const * const MouseMovedEvent::eventName = "MouseMoved";
00030         char const * const MouseButtonPressedEvent::eventName = "MouseButtonPressed";
00031         char const * const MouseButtonReleasedEvent::eventName = "MouseButtonReleased";
00032 
00033         InputManager::InputManager(GameApp* application) 
00034                 :       mApplication(application), mInputManager(NULL), mKeyboard(NULL), mMouse(NULL)
00035         {
00036 
00037 
00038         }
00039 
00040         InputManager::~InputManager()
00041         {
00042                 if(mInputManager){
00043                         if(mMouse){
00044                                 mInputManager->destroyInputObject(mMouse);
00045                                 mMouse = NULL;
00046                         }
00047 
00048                         if(mKeyboard){
00049                                 mInputManager->destroyInputObject(mKeyboard);
00050                                 mKeyboard = NULL;
00051                         }
00052 
00053                         mInputManager->destroyInputSystem();
00054                         mInputManager = NULL;
00055                 }
00056         }
00057 
00058 
00059         bool InputManager::init()
00060         {
00061                 size_t windowHnd;
00062                 
00063                 // Get our window
00064                 #if defined OIS_WIN32_PLATFORM
00065                         mApplication->getRenderWindow()->getCustomAttribute("HWND", &windowHnd);
00066                 #elif defined OIS_LINUX_PLATFORM
00067                         mApplication->getRenderWindow()->getCustomAttribute("GLXWINDOW", &windowHnd);
00068                 #endif
00069 
00070                 
00071                 // Setup parameters and then create OIS input manager
00072                 std::ostringstream wnd;
00073                 wnd << windowHnd; 
00074                 OIS::ParamList pl;
00075                 pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));
00076 
00077                 #if defined OIS_WIN32_PLATFORM
00078                         pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" ))); // Only receive input when the window is in focus
00079                         pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_EXCLUSIVE"))); // Share mouse with others (means windows mouse pointer is displayed)
00080 
00081                 #elif defined OIS_LINUX_PLATFORM
00082                         //For this demo, show mouse and do not grab (confine to window)
00083                         pl.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
00084                 #endif
00085 
00086                 mInputManager = OIS::InputManager::createInputSystem(pl);
00087 
00088                 bool bufferInput = true;
00089 
00090                 // Create keyboard
00091                 mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, bufferInput));
00092                 mKeyboard->setEventCallback(this);
00093 
00094                 // Create mouse
00095                 mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, bufferInput));
00096                 mMouse->setEventCallback(this);
00097 
00098                 // Get the window metrics and set the mouse state to the correct window size
00099                 unsigned int width, height, depth;
00100         int left, top;
00101                 mApplication->getRenderWindow()->getMetrics(width, height, depth, left, top);
00102 
00103                 const OIS::MouseState &mouseState = mMouse->getMouseState();
00104                 mouseState.width = width;
00105                 mouseState.height = height;
00106 
00107                 // All done!
00108                 Process::init();
00109 
00110                 return isInitialised();
00111         }
00112 
00113 
00114         void InputManager::update(const double& dt)
00115         {
00116                 Process::update(dt);
00117 
00118                 mKeyboard->capture();
00119                 mMouse->capture();
00120         }
00121 
00122 
00123         CEGUI::MouseButton InputManager::convertOisButtonToCegui(OIS::MouseButtonID buttonId) const
00124         {
00125                 switch (buttonId)
00126                 {
00127                         case OIS::MB_Left:
00128                                 return CEGUI::LeftButton;
00129                         case OIS::MB_Right:
00130                                 return CEGUI::RightButton;
00131                         case OIS::MB_Middle:
00132                                 return CEGUI::MiddleButton;
00133                         case OIS::MB_Button3:
00134                                 return CEGUI::X1Button;
00135                         default:
00136                                 return CEGUI::X2Button;
00137                 }
00138         }
00139 
00140 
00141 
00142         bool InputManager::keyPressed (const OIS::KeyEvent& arg)
00143         { 
00144                 boost::shared_ptr<Event> keyPressedEvent( new KeyPressedEvent(arg) );
00145                 EventManager::getSingletonPtr()->queueEvent( keyPressedEvent );
00146                 return true; 
00147         }
00148 
00149 
00150         bool InputManager::keyReleased (const OIS::KeyEvent& arg)
00151         { 
00152                 boost::shared_ptr<Event> keyReleasedEvent( new KeyReleasedEvent(arg) );
00153                 EventManager::getSingletonPtr()->queueEvent( keyReleasedEvent );
00154                 return true;
00155         }
00156 
00157         bool InputManager::mouseMoved (const OIS::MouseEvent &arg)
00158         { 
00159                 boost::shared_ptr<Event> mouseMovedEvent( new MouseMovedEvent(arg) );
00160                 EventManager::getSingletonPtr()->queueEvent( mouseMovedEvent );
00161                 return true; 
00162         }
00163 
00164         bool InputManager::mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00165         { 
00166                 boost::shared_ptr<Event> mousePressedEvent( new MouseButtonPressedEvent(arg, id) );
00167                 EventManager::getSingletonPtr()->queueEvent( mousePressedEvent );
00168                 return true; 
00169         }
00170 
00171         bool InputManager::mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00172         {
00173                 boost::shared_ptr<Event> mouseReleasedEvent( new MouseButtonReleasedEvent(arg, id) );
00174                 EventManager::getSingletonPtr()->queueEvent( mouseReleasedEvent );
00175                 return true; 
00176         }
00177 
00178 }

Generated on Thu Apr 19 09:47:02 2007 for GameEngine by  doxygen 1.5.2