1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _InputState_H__ |
---|
30 | #define _InputState_H__ |
---|
31 | |
---|
32 | #include "InputPrereqs.h" |
---|
33 | |
---|
34 | #include <cassert> |
---|
35 | #include <string> |
---|
36 | #include <vector> |
---|
37 | |
---|
38 | #include "util/OrxEnum.h" |
---|
39 | #include "InputHandler.h" |
---|
40 | #include "JoyStickQuantityListener.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | struct InputStatePriority : OrxEnum<InputStatePriority> |
---|
45 | { |
---|
46 | OrxEnumConstructors(InputStatePriority); |
---|
47 | |
---|
48 | static const int Empty = -1; |
---|
49 | static const int Dynamic = 0; |
---|
50 | |
---|
51 | static const int HighPriority = 1000; |
---|
52 | static const int Console = HighPriority + 0; |
---|
53 | static const int Calibrator = HighPriority + 1; |
---|
54 | static const int Detector = HighPriority + 2; |
---|
55 | }; |
---|
56 | |
---|
57 | class _CoreExport InputState : public JoyStickQuantityListener |
---|
58 | { |
---|
59 | friend class InputManager; |
---|
60 | |
---|
61 | static const InputDeviceEnumerator::Value keyboardIndex_s = InputDeviceEnumerator::Keyboard; |
---|
62 | static const InputDeviceEnumerator::Value mouseIndex_s = InputDeviceEnumerator::Mouse; |
---|
63 | static const InputDeviceEnumerator::Value firstJoyStickIndex_s = InputDeviceEnumerator::FirstJoyStick; |
---|
64 | |
---|
65 | public: |
---|
66 | void setKeyHandler (InputHandler* handler) |
---|
67 | { handlers_[keyboardIndex_s] = handler; bExpired_ = true; } |
---|
68 | void setMouseHandler (InputHandler* handler) |
---|
69 | { handlers_[mouseIndex_s] = handler; bExpired_ = true; } |
---|
70 | bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick); |
---|
71 | bool setJoyStickHandler(InputHandler* handler); |
---|
72 | bool setHandler (InputHandler* handler); |
---|
73 | |
---|
74 | const std::string& getName() const { return name_; } |
---|
75 | int getPriority() const { return priority_; } |
---|
76 | |
---|
77 | bool isInputDeviceEnabled(unsigned int device); |
---|
78 | |
---|
79 | bool hasExpired() { return this->bExpired_; } |
---|
80 | void resetExpiration() { bExpired_ = false; } |
---|
81 | |
---|
82 | void update(float dt, unsigned int device); |
---|
83 | void update(float dt); |
---|
84 | |
---|
85 | template <typename EventType, class Traits> |
---|
86 | void buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button); |
---|
87 | |
---|
88 | void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); |
---|
89 | void mouseScrolled(int abs, int rel); |
---|
90 | void joyStickAxisMoved(unsigned int device, unsigned int axis, float value); |
---|
91 | |
---|
92 | // Functors |
---|
93 | void entered(); |
---|
94 | void left(); |
---|
95 | void setEnterFunctor(Functor* functor) { this->enterFunctor_ = functor; } |
---|
96 | void setLeaveFunctor(Functor* functor) { this->leaveFunctor_ = functor; } |
---|
97 | |
---|
98 | private: |
---|
99 | InputState(); |
---|
100 | ~InputState() { } |
---|
101 | |
---|
102 | void JoyStickQuantityChanged(unsigned int n); |
---|
103 | |
---|
104 | void setName(const std::string& name) { name_ = name; } |
---|
105 | void setPriority(int priority) { priority_ = priority; } |
---|
106 | |
---|
107 | std::string name_; |
---|
108 | int priority_; |
---|
109 | bool bAlwaysGetsInput_; |
---|
110 | bool bTransparent_; |
---|
111 | bool bExpired_; |
---|
112 | std::vector<InputHandler*> handlers_; |
---|
113 | InputHandler* joyStickHandlerAll_; |
---|
114 | Functor* enterFunctor_; |
---|
115 | Functor* leaveFunctor_; |
---|
116 | }; |
---|
117 | |
---|
118 | inline void InputState::update(float dt) |
---|
119 | { |
---|
120 | for (unsigned int i = 0; i < handlers_.size(); ++i) |
---|
121 | if (handlers_[i] != NULL) |
---|
122 | handlers_[i]->allDevicesUpdated(dt); |
---|
123 | } |
---|
124 | |
---|
125 | inline void InputState::update(float dt, unsigned int device) |
---|
126 | { |
---|
127 | switch (device) |
---|
128 | { |
---|
129 | case InputDeviceEnumerator::Keyboard: |
---|
130 | if (handlers_[keyboardIndex_s] != NULL) |
---|
131 | handlers_[keyboardIndex_s]->keyboardUpdated(dt); |
---|
132 | break; |
---|
133 | |
---|
134 | case InputDeviceEnumerator::Mouse: |
---|
135 | if (handlers_[mouseIndex_s] != NULL) |
---|
136 | handlers_[mouseIndex_s]->mouseUpdated(dt); |
---|
137 | break; |
---|
138 | |
---|
139 | default: // joy sticks |
---|
140 | if (handlers_[device] != NULL) |
---|
141 | handlers_[device]->joyStickUpdated(device - firstJoyStickIndex_s, dt); |
---|
142 | break; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | template <typename EventType, class Traits> |
---|
147 | inline void InputState::buttonEvent(unsigned int device, const typename Traits::ButtonTypeParam button) |
---|
148 | { |
---|
149 | assert(device < handlers_.size()); |
---|
150 | if (handlers_[device] != NULL) |
---|
151 | handlers_[device]->buttonEvent(device, button, EventType()); |
---|
152 | } |
---|
153 | |
---|
154 | inline void InputState::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
155 | { |
---|
156 | if (handlers_[mouseIndex_s] != NULL) |
---|
157 | handlers_[mouseIndex_s]->mouseMoved(abs, rel, clippingSize); |
---|
158 | } |
---|
159 | |
---|
160 | inline void InputState::mouseScrolled(int abs, int rel) |
---|
161 | { |
---|
162 | if (handlers_[mouseIndex_s] != NULL) |
---|
163 | handlers_[mouseIndex_s]->mouseScrolled(abs, rel); |
---|
164 | } |
---|
165 | |
---|
166 | inline void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value) |
---|
167 | { |
---|
168 | assert(device < handlers_.size()); |
---|
169 | if (handlers_[device] != NULL) |
---|
170 | handlers_[device]->axisMoved(device - firstJoyStickIndex_s, axis, value); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | #endif /* _InputState_H__ */ |
---|