Changeset 3274 for code/branches/core4/src/core/input/InputState.h
- Timestamp:
- Jul 12, 2009, 4:12:04 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core4/src/core/input/InputState.h
r3270 r3274 27 27 */ 28 28 29 /**30 @file31 @brief32 */33 34 29 #ifndef _InputState_H__ 35 30 #define _InputState_H__ 36 31 37 #include " core/CorePrereqs.h"32 #include "InputPrereqs.h" 38 33 34 #include <cassert> 39 35 #include <string> 40 36 #include <vector> 41 #include "InputInterfaces.h" 42 #include "JoyStickDeviceNumberListener.h" 37 38 #include "InputHandler.h" 39 #include "JoyStickQuantityListener.h" 43 40 44 41 namespace orxonox 45 42 { 46 class _CoreExport InputState : public JoyStick DeviceNumberListener43 class _CoreExport InputState : public JoyStickQuantityListener 47 44 { 48 45 friend class InputManager; 49 46 47 static const InputDeviceEnumerator::Value keyboardIndex_s = InputDeviceEnumerator::Keyboard; 48 static const InputDeviceEnumerator::Value mouseIndex_s = InputDeviceEnumerator::Mouse; 49 static const InputDeviceEnumerator::Value firstJoyStickIndex_s = InputDeviceEnumerator::FirstJoyStick; 50 50 51 public: 52 void setKeyHandler (InputHandler* handler) 53 { handlers_[keyboardIndex_s] = handler; bExpired_ = true; } 54 void setMouseHandler (InputHandler* handler) 55 { handlers_[mouseIndex_s] = handler; bExpired_ = true; } 56 bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick); 57 bool setJoyStickHandler(InputHandler* handler); 58 bool setHandler (InputHandler* handler); 59 51 60 const std::string& getName() const { return name_; } 52 61 int getPriority() const { return priority_; } 53 62 54 bool isInputDeviceEnabled(unsigned int device) 55 { 56 if (device < bInputDeviceEnabled_.size()) 57 return bInputDeviceEnabled_[device]; 58 else 59 return false; 60 } 63 bool isInputDeviceEnabled(unsigned int device); 61 64 62 bool ha ndlersChanged() { return this->bHandlersChanged_; }63 void reset HandlersChanged() { bHandlersChanged_ = false; }65 bool hasExpired() { return this->bExpired_; } 66 void resetExpiration() { bExpired_ = false; } 64 67 65 v irtual void onEnter() = 0;66 v irtual void onLeave() = 0;68 void update(float dt, unsigned int device); 69 void update(float dt); 67 70 68 virtual void registerOnEnter(Executor* executor) { executorOnEnter_ = executor; } 69 virtual void unRegisterOnEnter() { executorOnEnter_ = 0; } 70 virtual void registerOnLeave(Executor* executor) { executorOnLeave_ = executor; } 71 virtual void unRegisterOnLeave() { executorOnLeave_ = 0; } 71 template <typename EventType, class Traits> 72 void buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button); 72 73 73 virtual void updateInput(float dt, unsigned int device) = 0; 74 virtual void updateInput(float dt) = 0; 74 void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); 75 void mouseScrolled(int abs, int rel); 76 void joyStickAxisMoved(unsigned int device, unsigned int axis, float value); 75 77 76 virtual void keyPressed (const KeyEvent& evt) = 0; 77 virtual void keyReleased(const KeyEvent& evt) = 0; 78 virtual void keyHeld (const KeyEvent& evt) = 0; 79 80 virtual void mouseButtonPressed (MouseButtonCode::ByEnum id) = 0; 81 virtual void mouseButtonReleased(MouseButtonCode::ByEnum id) = 0; 82 virtual void mouseButtonHeld (MouseButtonCode::ByEnum id) = 0; 83 virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) = 0; 84 virtual void mouseScrolled (int abs, int rel) = 0; 85 86 virtual void joyStickButtonPressed (unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0; 87 virtual void joyStickButtonReleased(unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0; 88 virtual void joyStickButtonHeld (unsigned int joyStickID, JoyStickButtonCode::ByEnum id) = 0; 89 virtual void joyStickAxisMoved (unsigned int joyStickID, unsigned int axis, float value) = 0; 90 91 protected: 92 InputState() 93 : bHandlersChanged_(false) 94 , executorOnEnter_(0) 95 , executorOnLeave_(0) 96 , priority_(0) 97 , bAlwaysGetsInput_(false) 98 , bTransparent_(false) 99 { } 100 virtual ~InputState() { } 101 102 virtual void numberOfJoySticksChanged(unsigned int n) = 0; 103 void setInputDeviceEnabled(unsigned int device, bool bEnabled) 104 { 105 if (device < bInputDeviceEnabled_.size()) 106 bInputDeviceEnabled_[device] = bEnabled; 107 } 108 109 bool bHandlersChanged_; 110 Executor* executorOnEnter_; 111 Executor* executorOnLeave_; 78 // Functors 79 void entered(); 80 void left(); 81 void setEnterFunctor(Functor* functor) { this->enterFunctor_ = functor; } 82 void setLeaveFunctor(Functor* functor) { this->leaveFunctor_ = functor; } 112 83 113 84 private: 114 void JoyStickDeviceNumberChanged(unsigned int n) 85 InputState(); 86 ~InputState() { } 87 88 void JoyStickQuantityChanged(unsigned int n); 89 90 void setName(const std::string& name) { name_ = name; } 91 void setPriority(int priority) { priority_ = priority; } 92 93 std::string name_; 94 int priority_; 95 bool bAlwaysGetsInput_; 96 bool bTransparent_; 97 bool bExpired_; 98 std::vector<InputHandler*> handlers_; 99 InputHandler* joyStickHandlerAll_; 100 Functor* enterFunctor_; 101 Functor* leaveFunctor_; 102 }; 103 104 inline void InputState::update(float dt) 105 { 106 for (unsigned int i = 0; i < handlers_.size(); ++i) 107 if (handlers_[i] != NULL) 108 handlers_[i]->allDevicesUpdated(dt); 109 } 110 111 inline void InputState::update(float dt, unsigned int device) 112 { 113 switch (device) 115 114 { 116 bInputDeviceEnabled_.resize(n + 2); 117 numberOfJoySticksChanged(n); 115 case InputDeviceEnumerator::Keyboard: 116 if (handlers_[keyboardIndex_s] != NULL) 117 handlers_[keyboardIndex_s]->keyboardUpdated(dt); 118 break; 119 120 case InputDeviceEnumerator::Mouse: 121 if (handlers_[mouseIndex_s] != NULL) 122 handlers_[mouseIndex_s]->mouseUpdated(dt); 123 break; 124 125 default: // joy sticks 126 if (handlers_[device] != NULL) 127 handlers_[device]->joyStickUpdated(device - firstJoyStickIndex_s, dt); 128 break; 118 129 } 119 void setName(const std::string& name) { name_ = name; } 120 void setPriority(int priority) { priority_ = priority; } 130 } 121 131 122 std::string name_; 123 int priority_; 124 std::vector<bool> bInputDeviceEnabled_; 125 bool bAlwaysGetsInput_; 126 bool bTransparent_; 127 }; 132 template <typename EventType, class Traits> 133 inline void InputState::buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button) 134 { 135 assert(device < handlers_.size()); 136 if (handlers_[device] != NULL) 137 handlers_[device]->buttonEvent(device, button, EventType()); 138 } 139 140 inline void InputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) 141 { 142 if (handlers_[mouseIndex_s] != NULL) 143 handlers_[mouseIndex_s]->mouseMoved(abs, rel, clippingSize); 144 } 145 146 inline void InputState::mouseScrolled(int abs, int rel) 147 { 148 if (handlers_[mouseIndex_s] != NULL) 149 handlers_[mouseIndex_s]->mouseScrolled(abs, rel); 150 } 151 152 inline void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value) 153 { 154 assert(device < handlers_.size()); 155 if (handlers_[device] != NULL) 156 handlers_[device]->axisMoved(device - firstJoyStickIndex_s, axis, value); 157 } 128 158 } 129 159
Note: See TracChangeset
for help on using the changeset viewer.