1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Reto Grieder |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | @file |
---|
30 | @brief Implementation of a little Input handler that distributes everything |
---|
31 | coming from OIS. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "core/CoreIncludes.h" |
---|
35 | #include "core/Debug.h" |
---|
36 | #include "InputEventListener.h" |
---|
37 | #include "InputHandler.h" |
---|
38 | #include "InputManager.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | /** |
---|
43 | @brief The reference to the singleton |
---|
44 | */ |
---|
45 | //InputManager* InputManager::singletonRef_s = 0; |
---|
46 | |
---|
47 | /** |
---|
48 | @brief Constructor only resets the pointer values to 0. |
---|
49 | */ |
---|
50 | InputManager::InputManager() : |
---|
51 | inputSystem_(0), keyboard_(0), mouse_(0), |
---|
52 | currentMode_(IM_UNINIT), setMode_(IM_UNINIT), |
---|
53 | handlerGUI_(0), handlerBuffer_(0), handlerGame_(0) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | @brief Destructor only called at the end of the program |
---|
59 | */ |
---|
60 | InputManager::~InputManager() |
---|
61 | { |
---|
62 | this->destroy(); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | @brief The one instance of the InputManager is stored in this function. |
---|
67 | @return A reference to the only instance of the InputManager |
---|
68 | */ |
---|
69 | InputManager& InputManager::getSingleton() |
---|
70 | { |
---|
71 | static InputManager theOnlyInstance; |
---|
72 | return theOnlyInstance; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | @brief Creates the OIS::InputMananger, the keyboard and the mouse and |
---|
77 | assigns the key bindings. |
---|
78 | @param windowHnd The window handle of the render window |
---|
79 | @param windowWidth The width of the render window |
---|
80 | @param windowHeight The height of the render window |
---|
81 | */ |
---|
82 | bool InputManager::initialise(size_t windowHnd, int windowWidth, int windowHeight) |
---|
83 | { |
---|
84 | if (!this->inputSystem_) |
---|
85 | { |
---|
86 | // Setup basic variables |
---|
87 | OIS::ParamList paramList; |
---|
88 | std::ostringstream windowHndStr; |
---|
89 | |
---|
90 | // Fill parameter list |
---|
91 | windowHndStr << (unsigned int)windowHnd; |
---|
92 | paramList.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); |
---|
93 | |
---|
94 | #if defined OIS_LINUX_PLATFORM |
---|
95 | paramList.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true"))); |
---|
96 | #endif |
---|
97 | |
---|
98 | try |
---|
99 | { |
---|
100 | // Create inputsystem |
---|
101 | inputSystem_ = OIS::InputManager::createInputSystem(paramList); |
---|
102 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS input system" << std::endl; |
---|
103 | |
---|
104 | // create a keyboard. If none are available the exception is caught. |
---|
105 | keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true)); |
---|
106 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS mouse" << std::endl; |
---|
107 | |
---|
108 | // create a mouse. If none are available the exception is caught. |
---|
109 | mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true)); |
---|
110 | COUT(ORX_DEBUG) << "*** InputManager: Created OIS keyboard" << std::endl; |
---|
111 | |
---|
112 | // Set mouse region |
---|
113 | this->setWindowExtents(windowWidth, windowHeight); |
---|
114 | } |
---|
115 | catch (OIS::Exception ex) |
---|
116 | { |
---|
117 | // something went wrong with the initialisation |
---|
118 | COUT(ORX_ERROR) << "Error: Failed creating an input system/keyboard/mouse. Message: \"" << ex.eText << "\"" << std::endl; |
---|
119 | this->inputSystem_ = 0; |
---|
120 | return false; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | // create the handlers |
---|
125 | this->handlerGUI_ = new InputHandlerGUI(); |
---|
126 | this->handlerGame_ = new InputHandlerGame(); |
---|
127 | this->handlerGame_->loadBindings(); |
---|
128 | |
---|
129 | /*COUT(ORX_DEBUG) << "*** InputManager: Loading key bindings..." << std::endl; |
---|
130 | // load the key bindings |
---|
131 | InputEvent empty = {0, false, 0, 0, 0}; |
---|
132 | for (int i = 0; i < this->numberOfKeys_; i++) |
---|
133 | this->bindingsKeyPressed_[i] = empty; |
---|
134 | |
---|
135 | //assign 'abort' to the escape key |
---|
136 | this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1; |
---|
137 | COUT(ORX_DEBUG) << "*** InputManager: Loading done." << std::endl;*/ |
---|
138 | |
---|
139 | return true; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | @brief Destroys all the created input devices and handlers. |
---|
144 | */ |
---|
145 | void InputManager::destroy() |
---|
146 | { |
---|
147 | COUT(ORX_DEBUG) << "*** InputManager: Destroying ..." << std::endl; |
---|
148 | if (this->mouse_) |
---|
149 | this->inputSystem_->destroyInputObject(mouse_); |
---|
150 | if (this->keyboard_) |
---|
151 | this->inputSystem_->destroyInputObject(keyboard_); |
---|
152 | if (this->inputSystem_) |
---|
153 | OIS::InputManager::destroyInputSystem(this->inputSystem_); |
---|
154 | |
---|
155 | this->mouse_ = 0; |
---|
156 | this->keyboard_ = 0; |
---|
157 | this->inputSystem_ = 0; |
---|
158 | |
---|
159 | if (this->handlerBuffer_) |
---|
160 | delete this->handlerBuffer_; |
---|
161 | if (this->handlerGame_) |
---|
162 | delete this->handlerGame_; |
---|
163 | if (this->handlerGUI_) |
---|
164 | delete this->handlerGUI_; |
---|
165 | |
---|
166 | this->handlerBuffer_ = 0; |
---|
167 | this->handlerGame_ = 0; |
---|
168 | this->handlerGUI_ = 0; |
---|
169 | |
---|
170 | COUT(ORX_DEBUG) << "*** InputManager: Destroying done." << std::endl; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | @brief Updates the InputManager |
---|
175 | @param dt Delta time |
---|
176 | */ |
---|
177 | void InputManager::tick(float dt) |
---|
178 | { |
---|
179 | // reset the game if it has changed |
---|
180 | if (this->currentMode_ != this->setMode_) |
---|
181 | { |
---|
182 | switch (this->setMode_) |
---|
183 | { |
---|
184 | case IM_GUI: |
---|
185 | this->mouse_->setEventCallback(this->handlerGUI_); |
---|
186 | this->keyboard_->setEventCallback(this->handlerGUI_); |
---|
187 | break; |
---|
188 | case IM_INGAME: |
---|
189 | this->mouse_->setEventCallback(this->handlerGame_); |
---|
190 | this->keyboard_->setEventCallback(this->handlerGame_); |
---|
191 | break; |
---|
192 | case IM_KEYBOARD: |
---|
193 | this->mouse_->setEventCallback(this->handlerGame_); |
---|
194 | this->keyboard_->setEventCallback(this->handlerBuffer_); |
---|
195 | break; |
---|
196 | case IM_UNINIT: |
---|
197 | this->mouse_->setEventCallback(0); |
---|
198 | this->keyboard_->setEventCallback(0); |
---|
199 | break; |
---|
200 | } |
---|
201 | this->currentMode_ = this->setMode_; |
---|
202 | } |
---|
203 | |
---|
204 | // capture all the input. That calls the event handlers. |
---|
205 | if (mouse_) |
---|
206 | mouse_->capture(); |
---|
207 | |
---|
208 | if (keyboard_) |
---|
209 | keyboard_->capture(); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | @brief Adjusts the mouse window metrics. |
---|
214 | This method has to be called every time the size of the window changes. |
---|
215 | @param width The new width of the render window |
---|
216 | @param height the new height of the render window |
---|
217 | */ |
---|
218 | void InputManager::setWindowExtents(int width, int height) |
---|
219 | { |
---|
220 | // Set mouse region (if window resizes, we should alter this to reflect as well) |
---|
221 | const OIS::MouseState &mouseState = mouse_->getMouseState(); |
---|
222 | mouseState.width = width; |
---|
223 | mouseState.height = height; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | @brief Sets the input mode to either GUI, inGame or Buffer |
---|
228 | @param mode The new input mode |
---|
229 | @remark Only has an affect if the mode actually changes |
---|
230 | */ |
---|
231 | void InputManager::setInputMode(InputMode mode) |
---|
232 | { |
---|
233 | this->setMode_ = mode; |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | @brief Returns the current input handling method |
---|
238 | @return The current input mode. |
---|
239 | */ |
---|
240 | InputMode InputManager::getInputMode() |
---|
241 | { |
---|
242 | return this->currentMode_; |
---|
243 | } |
---|
244 | |
---|
245 | } |
---|