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 _InputManager_H__ |
---|
30 | #define _InputManager_H__ |
---|
31 | |
---|
32 | #include "InputPrereqs.h" |
---|
33 | |
---|
34 | #include <map> |
---|
35 | #include <string> |
---|
36 | #include <vector> |
---|
37 | #include <boost/function.hpp> |
---|
38 | |
---|
39 | |
---|
40 | #include "util/Singleton.h" |
---|
41 | #include "util/tribool.h" |
---|
42 | #include "core/WindowEventListener.h" |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | // tolua_begin |
---|
47 | namespace orxonox |
---|
48 | { |
---|
49 | /** |
---|
50 | @brief |
---|
51 | Manages the input devices (mouse, keyboard, joy sticks) and the input states. |
---|
52 | |
---|
53 | Every input device has its own wrapper class which does the actually input event |
---|
54 | distribution. The InputManager only creates reloads (on request) those devices. |
---|
55 | |
---|
56 | The other functionality concerns handling InputStates. They act as a layer |
---|
57 | between InputHandlers (like the KeyBinder or the GUIManager) and InputDevices. |
---|
58 | InputStates are memory managed by the IputManager. You cannot create or destroy |
---|
59 | them on your own. Therefore all states get destroyed with the InputManager d'tor. |
---|
60 | @note |
---|
61 | - The actual lists containing all the InputStates for a specific device are stored |
---|
62 | in the InputDevices themselves. |
---|
63 | - The devices_ vector always has at least two elements: Keyboard (first) and mouse. |
---|
64 | You best access them internally with InputDeviceEnumerator::Keyboard/Mouse |
---|
65 | The first joy stick is accessed with InputDeviceEnumerator::FirstJoyStick. |
---|
66 | - Keyboard construction is mandatory , mouse and joy sticks are not. |
---|
67 | If the OIS::InputManager or the Keyboard fail, an exception is thrown. |
---|
68 | */ |
---|
69 | class _CoreExport InputManager |
---|
70 | // tolua_end |
---|
71 | : public Singleton<InputManager>, public WindowEventListener |
---|
72 | { // tolua_export |
---|
73 | friend class Singleton<InputManager>; |
---|
74 | public: |
---|
75 | //! Represents internal states of the InputManager. |
---|
76 | enum State |
---|
77 | { |
---|
78 | Nothing = 0x00, |
---|
79 | Bad = 0x02, |
---|
80 | Calibrating = 0x04, |
---|
81 | }; |
---|
82 | |
---|
83 | /** |
---|
84 | @brief |
---|
85 | Loads the devices and initialises the KeyDetector and the Calibrator. |
---|
86 | |
---|
87 | If either the OIS input system and/or the keyboard could not be created, |
---|
88 | the constructor fails with an std::exception. |
---|
89 | */ |
---|
90 | InputManager(); |
---|
91 | //! Destroys all devices AND all input states! |
---|
92 | ~InputManager(); |
---|
93 | void setConfigValues(); |
---|
94 | |
---|
95 | /** |
---|
96 | @brief |
---|
97 | Updates the devices (which distribute the input events) and the input states. |
---|
98 | |
---|
99 | Any InpuStates changes (destroy, enter, leave) and happens here. If a reload request |
---|
100 | was submitted while updating, the request will be postponed until the next update call. |
---|
101 | */ |
---|
102 | void preUpdate(const Clock& time); |
---|
103 | //! Clears all input device buffers. This usually only includes the pressed button list. |
---|
104 | void clearBuffers(); |
---|
105 | //! Starts joy stick calibration. |
---|
106 | void calibrate(); |
---|
107 | /** |
---|
108 | @brief |
---|
109 | Reloads all the input devices. Use this method to initialise new joy sticks. |
---|
110 | @note |
---|
111 | Only reloads immediately if the call stack doesn't include the preUpdate() method. |
---|
112 | */ |
---|
113 | void reload(); |
---|
114 | |
---|
115 | //------------------------------- |
---|
116 | // Input States |
---|
117 | //------------------------------- |
---|
118 | /** |
---|
119 | @brief |
---|
120 | Creates a new InputState that gets managed by the InputManager. |
---|
121 | @remarks |
---|
122 | The InputManager will take care of the state completely. That also |
---|
123 | means it gets deleted when the InputManager is destroyed! |
---|
124 | @param name |
---|
125 | Unique name of the InputState when referenced as string |
---|
126 | @param bAlwaysGetsInput |
---|
127 | InputState always gets the input when activated |
---|
128 | @param bTransparent |
---|
129 | InputState will not prevent underlaying state from receiving input |
---|
130 | @param priority |
---|
131 | Priority matters when multiple states are active. You can specify any |
---|
132 | number, but 1 - 99 is preferred (99 means high priority). |
---|
133 | */ |
---|
134 | InputState* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic); |
---|
135 | /** |
---|
136 | @brief |
---|
137 | Returns a pointer to a InputState referenced by name. |
---|
138 | @return |
---|
139 | Returns NULL if state was not found. |
---|
140 | */ |
---|
141 | InputState* getState(const std::string& name); |
---|
142 | /** |
---|
143 | @brief |
---|
144 | Activates a specific input state. |
---|
145 | It might not actually be activated if the priority is too low! |
---|
146 | @return |
---|
147 | False if name was not found, true otherwise. |
---|
148 | */ |
---|
149 | bool enterState(const std::string& name); // tolua_export |
---|
150 | /** |
---|
151 | @brief |
---|
152 | Deactivates a specific input state. |
---|
153 | @return |
---|
154 | False if name was not found, true otherwise. |
---|
155 | */ |
---|
156 | bool leaveState(const std::string& name); // tolua_export |
---|
157 | /** |
---|
158 | @brief |
---|
159 | Removes and destroys an input state. |
---|
160 | @return |
---|
161 | True if removal was successful, false if name was not found. |
---|
162 | @remarks |
---|
163 | - You can't remove the internal states "empty", "calibrator" and "detector". |
---|
164 | - The removal process is being postponed if InputManager::preUpdate() is currently running. |
---|
165 | */ |
---|
166 | bool destroyState(const std::string& name); // tolua_export |
---|
167 | /** |
---|
168 | @brief |
---|
169 | Changes the mouse mode of an input state. |
---|
170 | @return |
---|
171 | True if the call was successful, fals if the name was not found |
---|
172 | */ |
---|
173 | bool setMouseExclusive(const std::string& name, tribool value); // tolua_export |
---|
174 | |
---|
175 | //------------------------------- |
---|
176 | // Various getters and setters |
---|
177 | //------------------------------- |
---|
178 | //! Returns the number of joy stick that have been created since the c'tor or last call to reload(). |
---|
179 | unsigned int getJoyStickQuantity() const |
---|
180 | { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; } |
---|
181 | //! Returns a pointer to the OIS InputManager. Only you if you know what you're doing! |
---|
182 | OIS::InputManager* getOISInputManager() { return this->oisInputManager_; } |
---|
183 | //! Returns the position of the cursor as std::pair of ints |
---|
184 | std::pair<int, int> getMousePosition() const; |
---|
185 | //! Tells whether the mouse is used exclusively to the game |
---|
186 | bool isMouseExclusive() const { return this->exclusiveMouse_; } // tolua_export |
---|
187 | |
---|
188 | //------------------------------- |
---|
189 | // Function call caching |
---|
190 | //------------------------------- |
---|
191 | void pushCall(const boost::function<void ()>& function) |
---|
192 | { this->callBuffer_.push_back(function); } |
---|
193 | |
---|
194 | static InputManager& getInstance() { return Singleton<InputManager>::getInstance(); } // tolua_export |
---|
195 | |
---|
196 | private: // functions |
---|
197 | // don't mess with a Singleton |
---|
198 | InputManager(const InputManager&); |
---|
199 | |
---|
200 | // Internal methods |
---|
201 | void loadDevices(); |
---|
202 | void loadWiiMote(); |
---|
203 | void loadMouse(); |
---|
204 | void loadJoySticks(); |
---|
205 | void destroyDevices(); |
---|
206 | |
---|
207 | void stopCalibration(); |
---|
208 | void reloadInternal(); |
---|
209 | |
---|
210 | void destroyStateInternal(InputState* state); |
---|
211 | void updateActiveStates(); |
---|
212 | |
---|
213 | // From WindowEventListener |
---|
214 | void windowFocusChanged(bool bFocus); |
---|
215 | |
---|
216 | private: // variables |
---|
217 | State internalState_; //!< Current internal state |
---|
218 | OIS::InputManager* oisInputManager_; //!< OIS input manager |
---|
219 | std::vector<InputDevice*> devices_; //!< List of all input devices (keyboard, mouse, joy sticks) |
---|
220 | bool exclusiveMouse_; //!< Currently applied mouse mode |
---|
221 | |
---|
222 | // some internally handled states and handlers |
---|
223 | InputState* emptyState_; //!< Lowest priority states (makes handling easier) |
---|
224 | //! InputBuffer that reacts to the Enter key when calibrating the joy sticks |
---|
225 | InputBuffer* calibratorCallbackHandler_; |
---|
226 | |
---|
227 | std::map<std::string, InputState*> statesByName_; //!< Contains all the created input states by name |
---|
228 | std::map<int, InputState*> activeStates_; //!< Contains all active input states by priority (std::map is sorted!) |
---|
229 | std::vector<InputState*> activeStatesTicked_; //!< Like activeStates_, but only contains the ones that currently receive events |
---|
230 | |
---|
231 | std::vector<boost::function<void ()> > callBuffer_; //!< Caches all calls from InputStates to be executed afterwards (see preUpdate) |
---|
232 | |
---|
233 | static InputManager* singletonPtr_s; //!< Pointer reference to the singleton |
---|
234 | }; // tolua_export |
---|
235 | } // tolua_export |
---|
236 | |
---|
237 | #endif /* _InputManager_H__ */ |
---|