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 | /** |
---|
30 | @file |
---|
31 | @brief |
---|
32 | Implementation of a little Input handler that distributes everything |
---|
33 | coming from OIS. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifndef _InputManager_H__ |
---|
37 | #define _InputManager_H__ |
---|
38 | |
---|
39 | #include "InputPrereqs.h" |
---|
40 | |
---|
41 | #include <map> |
---|
42 | #include <set> |
---|
43 | #include <string> |
---|
44 | #include <vector> |
---|
45 | |
---|
46 | #include "util/Math.h" |
---|
47 | #include "util/OrxEnum.h" |
---|
48 | #include "core/OrxonoxClass.h" |
---|
49 | |
---|
50 | namespace orxonox |
---|
51 | { |
---|
52 | struct InputStatePriority : OrxEnum<InputStatePriority> |
---|
53 | { |
---|
54 | OrxEnumConstructors(InputStatePriority); |
---|
55 | |
---|
56 | static const int Empty = -1; |
---|
57 | static const int Dynamic = 0; |
---|
58 | |
---|
59 | static const int HighPriority = 1000; |
---|
60 | static const int Console = HighPriority + 0; |
---|
61 | static const int Calibrator = HighPriority + 1; |
---|
62 | static const int Detector = HighPriority + 2; |
---|
63 | }; |
---|
64 | |
---|
65 | /** |
---|
66 | @brief |
---|
67 | Captures and distributes mouse and keyboard input. |
---|
68 | */ |
---|
69 | class _CoreExport InputManager : public OrxonoxClass |
---|
70 | { |
---|
71 | // --> setConfigValues is private |
---|
72 | friend class ClassIdentifier<InputManager>; |
---|
73 | |
---|
74 | public: |
---|
75 | enum InputManagerState |
---|
76 | { |
---|
77 | Uninitialised = 0x00, |
---|
78 | OISReady = 0x01, |
---|
79 | InternalsReady = 0x02, |
---|
80 | Ticking = 0x04, |
---|
81 | Calibrating = 0x08, |
---|
82 | ReloadRequest = 0x10, |
---|
83 | }; |
---|
84 | |
---|
85 | InputManager (size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight); |
---|
86 | ~InputManager(); |
---|
87 | |
---|
88 | void initialise(size_t windowHnd, unsigned int windowWidth, unsigned int windowHeight); |
---|
89 | |
---|
90 | void reloadInputSystem(); |
---|
91 | |
---|
92 | void clearBuffers(); |
---|
93 | |
---|
94 | void setWindowExtents(const int width, const int height); |
---|
95 | void setKeyDetectorCallback(const std::string& command); |
---|
96 | |
---|
97 | InputState* createInputState(const std::string& name, bool bAlwaysGetsInput = false, bool bTransparent = false, InputStatePriority priority = InputStatePriority::Dynamic); |
---|
98 | |
---|
99 | InputState* getState (const std::string& name); |
---|
100 | InputState* getCurrentState(); |
---|
101 | bool requestDestroyState (const std::string& name); |
---|
102 | bool requestEnterState (const std::string& name); |
---|
103 | bool requestLeaveState (const std::string& name); |
---|
104 | |
---|
105 | OIS::InputManager* getInputSystem() { return this->inputSystem_; } |
---|
106 | bool checkJoyStickID(const std::string& idString) const; |
---|
107 | unsigned int getJoyStickQuantity() const |
---|
108 | { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; } |
---|
109 | |
---|
110 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
111 | // HACK! |
---|
112 | static void grabMouse(); |
---|
113 | static void ungrabMouse(); |
---|
114 | #endif |
---|
115 | |
---|
116 | void update(const Clock& time); |
---|
117 | |
---|
118 | static InputManager& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
119 | static InputManager* getInstancePtr() { return singletonRef_s; } |
---|
120 | |
---|
121 | // console commands |
---|
122 | static void calibrate(); |
---|
123 | static void reload(); |
---|
124 | |
---|
125 | private: // functions |
---|
126 | // don't mess with a Singleton |
---|
127 | InputManager(const InputManager&); |
---|
128 | |
---|
129 | // Intenal methods |
---|
130 | void _initialiseKeyboard(); |
---|
131 | void _initialiseMouse(unsigned int windowWidth, unsigned int windowHeight); |
---|
132 | void _initialiseJoySticks(); |
---|
133 | |
---|
134 | void _startCalibration(); |
---|
135 | void _stopCalibration(); |
---|
136 | |
---|
137 | void _destroyState(InputState* state); |
---|
138 | |
---|
139 | void _reload(); |
---|
140 | |
---|
141 | void _updateActiveStates(); |
---|
142 | bool _configureInputState(InputState* state, const std::string& name, bool bAlwaysGetsInput, bool bTransparent, int priority); |
---|
143 | |
---|
144 | void setConfigValues(); |
---|
145 | |
---|
146 | private: // variables |
---|
147 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
148 | std::vector<InputDevice*> devices_; //!< List of all input devices (keyboard, mouse, joy sticks) |
---|
149 | size_t windowHnd_; //!< Render window handle |
---|
150 | InputManagerState internalState_; //!< Current internal state |
---|
151 | |
---|
152 | // some internally handled states and handlers |
---|
153 | InputState* stateEmpty_; |
---|
154 | KeyDetector* keyDetector_; //!< KeyDetector instance |
---|
155 | InputBuffer* calibratorCallbackBuffer_; |
---|
156 | |
---|
157 | std::map<std::string, InputState*> inputStatesByName_; |
---|
158 | |
---|
159 | std::set<InputState*> stateEnterRequests_; //!< Request to enter a new state |
---|
160 | std::set<InputState*> stateLeaveRequests_; //!< Request to leave a running state |
---|
161 | std::set<InputState*> stateDestroyRequests_; //!< Request to destroy a state |
---|
162 | |
---|
163 | std::map<int, InputState*> activeStates_; |
---|
164 | std::vector<InputState*> activeStatesTicked_; |
---|
165 | |
---|
166 | static InputManager* singletonRef_s; |
---|
167 | }; |
---|
168 | } |
---|
169 | |
---|
170 | #endif /* _InputManager_H__ */ |
---|