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 Implementation of a little Input handler that distributes everything |
---|
32 | coming from OIS. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifndef _InputManager_H__ |
---|
36 | #define _InputManager_H__ |
---|
37 | |
---|
38 | #include "CorePrereqs.h" |
---|
39 | |
---|
40 | #include <map> |
---|
41 | #include <vector> |
---|
42 | |
---|
43 | #include "ois/OIS.h" |
---|
44 | #include "util/Math.h" |
---|
45 | #include "Tickable.h" |
---|
46 | #include "InputInterfaces.h" |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | /** |
---|
51 | * Helper class to realise a vector<int[4]> |
---|
52 | */ |
---|
53 | class POVStates |
---|
54 | { |
---|
55 | public: |
---|
56 | int operator[](unsigned int index) { return povStates[index]; } |
---|
57 | int povStates[4]; |
---|
58 | }; |
---|
59 | |
---|
60 | /** |
---|
61 | * Helper class to realise a vector< {int[4], int[4]} > |
---|
62 | */ |
---|
63 | class SliderStates |
---|
64 | { |
---|
65 | public: |
---|
66 | IntVector2 sliderStates[4]; |
---|
67 | }; |
---|
68 | |
---|
69 | /** |
---|
70 | @brief Captures and distributes mouse and keyboard input. |
---|
71 | */ |
---|
72 | class _CoreExport InputManager |
---|
73 | : public TickableReal, |
---|
74 | public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener |
---|
75 | { |
---|
76 | public: // enumerations |
---|
77 | /** |
---|
78 | @brief Designates the way input is handled and redirected. |
---|
79 | */ |
---|
80 | enum InputState |
---|
81 | { |
---|
82 | IS_UNINIT, //!< InputManager has not yet been initialised. |
---|
83 | IS_NONE, //!< Input is discarded. |
---|
84 | IS_NORMAL, //!< Normal play state. Key and button bindings are active. |
---|
85 | IS_GUI, //!< All OIS input events are passed to CEGUI. |
---|
86 | IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. |
---|
87 | IS_CUSTOM //!< Any possible configuration. |
---|
88 | }; |
---|
89 | |
---|
90 | public: // static functions |
---|
91 | static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, |
---|
92 | bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); |
---|
93 | static bool initialiseKeyboard(); |
---|
94 | static bool initialiseMouse(); |
---|
95 | static bool initialiseJoySticks(); |
---|
96 | static int numberOfKeyboards(); |
---|
97 | static int numberOfMice(); |
---|
98 | static int numberOfJoySticks(); |
---|
99 | |
---|
100 | static void destroy(); |
---|
101 | static void destroyKeyboard(); |
---|
102 | static void destroyMouse(); |
---|
103 | static void destroyJoySticks(); |
---|
104 | |
---|
105 | static bool isModifierDown(KeyboardModifier::Enum modifier); |
---|
106 | static bool isKeyDown(KeyCode::Enum key); |
---|
107 | //static const MouseState getMouseState(); |
---|
108 | //static const JoyStickState getJoyStickState(unsigned int ID); |
---|
109 | |
---|
110 | static void setWindowExtents(const int width, const int height); |
---|
111 | |
---|
112 | static void setInputState(const InputState state); |
---|
113 | static InputState getInputState(); |
---|
114 | |
---|
115 | static bool addKeyHandler (KeyHandler* handler, const std::string& name); |
---|
116 | static bool removeKeyHandler (const std::string& name); |
---|
117 | static KeyHandler* getKeyHandler (const std::string& name); |
---|
118 | static bool enableKeyHandler (const std::string& name); |
---|
119 | static bool disableKeyHandler (const std::string& name); |
---|
120 | static bool isKeyHandlerActive (const std::string& name); |
---|
121 | |
---|
122 | static bool addMouseHandler (MouseHandler* handler, const std::string& name); |
---|
123 | static bool removeMouseHandler (const std::string& name); |
---|
124 | static MouseHandler* getMouseHandler (const std::string& name); |
---|
125 | static bool enableMouseHandler (const std::string& name); |
---|
126 | static bool disableMouseHandler (const std::string& name); |
---|
127 | static bool isMouseHandlerActive (const std::string& name); |
---|
128 | |
---|
129 | static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); |
---|
130 | static bool removeJoyStickHandler (const std::string& name); |
---|
131 | static JoyStickHandler* getJoyStickHandler(const std::string& name); |
---|
132 | static bool enableJoyStickHandler (const std::string& name, unsigned int id); |
---|
133 | static bool disableJoyStickHandler (const std::string& name, unsigned int id); |
---|
134 | static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); |
---|
135 | |
---|
136 | private: // functions |
---|
137 | // don't mess with a Singleton |
---|
138 | InputManager (); |
---|
139 | InputManager (const InputManager&); |
---|
140 | ~InputManager(); |
---|
141 | |
---|
142 | // Intenal methods |
---|
143 | bool _initialise(const size_t, int, int, bool, bool, bool); |
---|
144 | bool _initialiseKeyboard(); |
---|
145 | bool _initialiseMouse(); |
---|
146 | bool _initialiseJoySticks(); |
---|
147 | |
---|
148 | void _destroy(); |
---|
149 | void _destroyKeyboard(); |
---|
150 | void _destroyMouse(); |
---|
151 | void _destroyJoySticks(); |
---|
152 | |
---|
153 | void _updateTickables(); |
---|
154 | |
---|
155 | void tick(float dt); |
---|
156 | |
---|
157 | // input events |
---|
158 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
159 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
160 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
161 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
162 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
163 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
164 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
165 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
166 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
167 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
168 | //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); |
---|
169 | |
---|
170 | static InputManager& _getSingleton(); |
---|
171 | static InputManager* _getSingletonPtr() { return &_getSingleton(); } |
---|
172 | |
---|
173 | private: // variables |
---|
174 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
175 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
176 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
177 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
178 | unsigned int joySticksSize_; |
---|
179 | |
---|
180 | InputState state_; |
---|
181 | InputState stateRequest_; |
---|
182 | unsigned int keyboardModifiers_; |
---|
183 | |
---|
184 | //! Keeps track of the joy stick POV states |
---|
185 | std::vector<POVStates> povStates_; |
---|
186 | //! Keeps track of the possibly two slider axes |
---|
187 | std::vector<SliderStates> sliderStates_; |
---|
188 | |
---|
189 | std::map<std::string, KeyHandler*> keyHandlers_; |
---|
190 | std::map<std::string, MouseHandler*> mouseHandlers_; |
---|
191 | std::map<std::string, JoyStickHandler*> joyStickHandlers_; |
---|
192 | |
---|
193 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
194 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
195 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
196 | std::vector<InputTickable*> activeHandlers_; |
---|
197 | |
---|
198 | std::vector<Key> keysDown_; |
---|
199 | std::vector<MouseButton::Enum> mouseButtonsDown_; |
---|
200 | std::vector<std::vector<int> > joyStickButtonsDown_; |
---|
201 | |
---|
202 | }; |
---|
203 | |
---|
204 | } |
---|
205 | |
---|
206 | #endif /* _InputManager_H__ */ |
---|