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