|
From: <xs...@us...> - 2006-05-23 23:04:37
|
Revision: 60 Author: xsdwx Date: 2006-05-23 16:04:31 -0700 (Tue, 23 May 2006) ViewCVS: http://svn.sourceforge.net/perceptioncrash/?rev=60&view=rev Log Message: ----------- -Changed the InputController interface and implementation around. -Entities should now store a pointer to what button they need input from. -REMOVED update() from InputControllers. -renamed removeInputController to destroyInputController to match createInputController. -Added some hard-coded input buttons to GameManager.cpp Modified Paths: -------------- trunk/perceptioncrash/src/GameManager.cpp trunk/perceptioncrash/src/InputController.hpp trunk/perceptioncrash/src/InputManager.cpp trunk/perceptioncrash/src/InputManager.hpp trunk/perceptioncrash/src/KeyboardMouseInputController.cpp trunk/perceptioncrash/src/KeyboardMouseInputController.hpp Modified: trunk/perceptioncrash/src/GameManager.cpp =================================================================== --- trunk/perceptioncrash/src/GameManager.cpp 2006-05-22 23:39:47 UTC (rev 59) +++ trunk/perceptioncrash/src/GameManager.cpp 2006-05-23 23:04:31 UTC (rev 60) @@ -5,6 +5,9 @@ #include "InputManager.hpp" #include "GameState.hpp" +// TODO Temporary! See message below +#include "KeyboardMouseInputController.hpp" + template<> GameManager* Ogre::Singleton<GameManager>::ms_Singleton = 0; GameManager::GameManager() @@ -44,10 +47,18 @@ mInputManager = new InputManager(mRoot->getAutoCreatedWindow()); mInputController = mInputManager->createController("default"); mInputController->addEventListener(this); - //mInputManager->getEventProcessor()->addKeyListener(this); - //mInputManager->getEventProcessor()->addMouseMotionListener(this); + // TODO Temporary! This will be configured normally with XML configuration file + KeyboardMouseInputController* kbnm = dynamic_cast<KeyboardMouseInputController*>(mInputController); + if ( kbnm ) + { + kbnm->createKeyboardInputButton("select", Ogre::KC_SPACE); + kbnm->createKeyboardInputButton("pause", Ogre::KC_P); + kbnm->createKeyboardInputButton("back", Ogre::KC_ESCAPE); + kbnm->createKeyboardInputButton("move_down", Ogre::KC_DOWN); + } + mDebugPanelOverlay = Ogre::OverlayManager::getSingleton().getByName("Core/DebugOverlay"); changeState(state); @@ -208,9 +219,6 @@ bool GameManager::frameStarted(const Ogre::FrameEvent& evt) { - // update controllers - mInputManager->updateAllControllers(); - // call frameStarted of current state return mStates.back()->frameStarted(evt); } Modified: trunk/perceptioncrash/src/InputController.hpp =================================================================== --- trunk/perceptioncrash/src/InputController.hpp 2006-05-22 23:39:47 UTC (rev 59) +++ trunk/perceptioncrash/src/InputController.hpp 2006-05-23 23:04:31 UTC (rev 60) @@ -6,16 +6,17 @@ class InputControllerListener; class InputControllerEvent; +class InputButton; class InputController { public: virtual ~InputController() { } - + typedef std::string Button; - - virtual bool isButtonDown(Button button) = 0; + virtual InputButton* getInputButton(const Button& button) = 0; + virtual void addEventListener(InputControllerListener* l) { /// @todo Check to make sure 'l' isn't already registered @@ -29,11 +30,7 @@ icl->erase(std::remove(icl->begin(), icl->end(), l)); } - // For controllers that need to update there state every frame - virtual void update() { } - protected: - std::map<Button, bool> mButtonStates; std::vector<InputControllerListener*> mInputControllerListeners; }; @@ -48,15 +45,27 @@ class InputControllerEvent { public: - InputControllerEvent(InputController::Button& button) + InputControllerEvent(const InputController::Button& button) { mButton = button; } - InputController::Button getButton() + const InputController::Button& getButton() const { return mButton; } private: InputController::Button mButton; }; +class InputButton { + public: + InputButton() : mDown(false) { } + bool isDown() const + { return mDown; } + void setDown(bool down) + { mDown = down; } + + protected: + bool mDown; +}; + #endif /* PC_INPUT_CONTROLLER_HPP */ Modified: trunk/perceptioncrash/src/InputManager.cpp =================================================================== --- trunk/perceptioncrash/src/InputManager.cpp 2006-05-22 23:39:47 UTC (rev 59) +++ trunk/perceptioncrash/src/InputManager.cpp 2006-05-23 23:04:31 UTC (rev 60) @@ -49,7 +49,7 @@ } void -InputManager::removeController(std::string name) +InputManager::destroyController(std::string name) { std::map<std::string, InputController*>::iterator iter = mControllers.find(name); if ( iter != mControllers.end() ) @@ -69,13 +69,3 @@ throw std::runtime_error("Controller '" + name + "' does not exist"); } - -void -InputManager::updateAllControllers() -{ - std::map<std::string, InputController*>::iterator i; - for (i = mControllers.begin(); i != mControllers.end(); ++i) - { - i->second->update(); - } -} Modified: trunk/perceptioncrash/src/InputManager.hpp =================================================================== --- trunk/perceptioncrash/src/InputManager.hpp 2006-05-22 23:39:47 UTC (rev 59) +++ trunk/perceptioncrash/src/InputManager.hpp 2006-05-23 23:04:31 UTC (rev 60) @@ -21,9 +21,8 @@ { return mEventProcessor; } InputController* createController(std::string name); - void removeController(std::string name); + void destroyController(std::string name); InputController* getController(std::string name); - void updateAllControllers(); private: Ogre::EventProcessor* mEventProcessor; Modified: trunk/perceptioncrash/src/KeyboardMouseInputController.cpp =================================================================== --- trunk/perceptioncrash/src/KeyboardMouseInputController.cpp 2006-05-22 23:39:47 UTC (rev 59) +++ trunk/perceptioncrash/src/KeyboardMouseInputController.cpp 2006-05-23 23:04:31 UTC (rev 60) @@ -26,17 +26,62 @@ KeyboardMouseInputController::~KeyboardMouseInputController() { + std::map<Button, KeyboardInputButton*>::iterator i; + for ( i = mKeyboardButtons.begin(); i != mKeyboardButtons.end(); ++i ) + { + delete i->second; + } + + std::map<Button, MouseInputButton*>::iterator j; + for ( j = mMouseButtons.begin(); j != mMouseButtons.end(); ++j ) + { + delete j->second; + } + InputManager* inputManager = InputManager::getSingletonPtr(); inputManager->getEventProcessor()->removeKeyListener(this); inputManager->getEventProcessor()->removeMouseMotionListener(this); } -bool -KeyboardMouseInputController::isButtonDown(Button button) +InputButton* +KeyboardMouseInputController::getInputButton(const Button& button) { - return mButtonStates[button]; + if ( mKeyboardButtons.find(button) != mKeyboardButtons.end() ) + { + return mKeyboardButtons[button]; + } + else if ( mMouseButtons.find(button) != mMouseButtons.end() ) + { + return mMouseButtons[button]; + } + + throw std::runtime_error("InputButton '" + button + "' does not exist"); } +InputButton* +KeyboardMouseInputController::createKeyboardInputButton(const Button& button, int keycode) +{ + if ( mMouseButtons.find(button) == mMouseButtons.end() && + mKeyboardButtons.find(button) == mKeyboardButtons.end() ) + { + return mKeyboardButtons[button] = new KeyboardInputButton(keycode); + } + + throw std::runtime_error("InputButton '" + button + "' already exists"); +} + +InputButton* +KeyboardMouseInputController::createMouseInputButton(const Button& button, int buttoncode) +{ + if ( mMouseButtons.find(button) == mMouseButtons.end() && + mKeyboardButtons.find(button) == mKeyboardButtons.end() ) + { + return mMouseButtons[button] = new MouseInputButton(buttoncode); + } + + throw std::runtime_error("InputButton '" + button + "' already exists"); +} + void KeyboardMouseInputController::keyClicked(Ogre::KeyEvent* e) { @@ -46,9 +91,9 @@ KeyboardMouseInputController::keyPressed(Ogre::KeyEvent* e) { /// @todo less code repetition in these event functions - - std::vector<Button> pressed; + /*std::vector<Button> pressed; + // Build a list of pushed buttons (single keys can map to more than one // button) and update button states for (std::multimap<int, Button>::iterator i = mKeyboardMap.begin(); @@ -69,13 +114,29 @@ { (*j)->buttonPressed(&event); } + }*/ + + std::map<Button, KeyboardInputButton*>::iterator i; + for (i = mKeyboardButtons.begin(); i != mKeyboardButtons.end(); ++i) + { + if ( e->getKey() == i->second->getKeyCode() ) + { + i->second->setDown(true); + + InputControllerEvent event(i->first); + std::vector<InputControllerListener*>::iterator j; + for (j = mInputControllerListeners.begin(); j != mInputControllerListeners.end(); j++) + { + (*j)->buttonPressed(&event); + } + } } } void KeyboardMouseInputController::keyReleased(Ogre::KeyEvent* e) { - std::vector<Button> released; + /*std::vector<Button> released; // Build a list of pushed buttons (single keys can map to more than one // button) and update button states @@ -97,6 +158,22 @@ { (*j)->buttonReleased(&event); } + }*/ + + std::map<Button, KeyboardInputButton*>::iterator i; + for (i = mKeyboardButtons.begin(); i != mKeyboardButtons.end(); ++i) + { + if ( e->getKey() == i->second->getKeyCode() ) + { + i->second->setDown(false); + + InputControllerEvent event(i->first); + std::vector<InputControllerListener*>::iterator j; + for (j = mInputControllerListeners.begin(); j != mInputControllerListeners.end(); j++) + { + (*j)->buttonReleased(&event); + } + } } } @@ -118,7 +195,7 @@ void KeyboardMouseInputController::mousePressed(Ogre::MouseEvent* e) { - std::vector<Button> pressed; + /*std::vector<Button> pressed; // Build a list of pushed buttons (a single mouse button can map to more // than one button) and update button states @@ -140,13 +217,29 @@ { (*j)->buttonPressed(&event); } + }*/ + + std::map<Button, MouseInputButton*>::iterator i; + for (i = mMouseButtons.begin(); i != mMouseButtons.end(); ++i) + { + if ( e->getButtonID() == i->second->getButtonCode() ) + { + i->second->setDown(true); + + InputControllerEvent event(i->first); + std::vector<InputControllerListener*>::iterator j; + for (j = mInputControllerListeners.begin(); j != mInputControllerListeners.end(); j++) + { + (*j)->buttonPressed(&event); + } + } } } void KeyboardMouseInputController::mouseReleased(Ogre::MouseEvent* e) { - std::vector<Button> released; + /*std::vector<Button> released; // Build a list of pushed buttons (a single mouse button can map to more // than one button) and update button states @@ -168,6 +261,22 @@ { (*j)->buttonReleased(&event); } + }*/ + + std::map<Button, MouseInputButton*>::iterator i; + for (i = mMouseButtons.begin(); i != mMouseButtons.end(); ++i) + { + if ( e->getButtonID() == i->second->getButtonCode() ) + { + i->second->setDown(false); + + InputControllerEvent event(i->first); + std::vector<InputControllerListener*>::iterator j; + for (j = mInputControllerListeners.begin(); j != mInputControllerListeners.end(); j++) + { + (*j)->buttonReleased(&event); + } + } } } Modified: trunk/perceptioncrash/src/KeyboardMouseInputController.hpp =================================================================== --- trunk/perceptioncrash/src/KeyboardMouseInputController.hpp 2006-05-22 23:39:47 UTC (rev 59) +++ trunk/perceptioncrash/src/KeyboardMouseInputController.hpp 2006-05-23 23:04:31 UTC (rev 60) @@ -8,6 +8,9 @@ #include "InputController.hpp" #include "Factory.hpp" +class KeyboardInputButton; +class MouseInputButton; + class KeyboardMouseInputController : public InputController, public Ogre::KeyListener, public Ogre::MouseListener, @@ -17,9 +20,11 @@ KeyboardMouseInputController(); KeyboardMouseInputController(TiXmlElement* node); ~KeyboardMouseInputController(); - - bool isButtonDown(Button button); + InputButton* getInputButton(const Button& button); + InputButton* createKeyboardInputButton(const Button& button, int keycode); + InputButton* createMouseInputButton(const Button& button, int buttoncode); + protected: /** Ogre::KeyListener keyClicked event */ void keyClicked(Ogre::KeyEvent* e); @@ -47,9 +52,36 @@ void mouseDragMoved(Ogre::MouseEvent* e); private: - std::multimap<int, Button> mKeyboardMap; - std::multimap<int, Button> mMouseMap; + std::map<Button, KeyboardInputButton*> mKeyboardButtons; + std::map<Button, MouseInputButton*> mMouseButtons; }; +class KeyboardInputButton : public InputButton +{ + public: + KeyboardInputButton(int keycode) : mKeyCode(keycode) { } + void changeKey(int key) + { mKeyCode = key; } + int getKeyCode() const + { return mKeyCode; } + + private: + int mKeyCode; +}; + +class MouseInputButton : public InputButton +{ + public: + MouseInputButton(int buttoncode) : mButtonCode(buttoncode) { } + + void changeButtonCode(int button) + { mButtonCode = button; } + int getButtonCode() const + { return mButtonCode; } + + private: + int mButtonCode; +}; + #endif /* PC_KEYBOARD_MOUSE_INPUT_CONTROLLER_HPP */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |