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