3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irrlichttypes_extrabloated.h"
23 #include "joystick_controller.h"
26 #include "renderingengine.h"
28 #ifdef HAVE_TOUCHSCREENGUI
29 #include "gui/touchscreengui.h"
34 /****************************************************************************
35 Fast key cache for main game loop
36 ****************************************************************************/
38 /* This is faster than using getKeySetting with the tradeoff that functions
39 * using it must make sure that it's initialised before using it and there is
40 * no error handling (for example bounds checking). This is really intended for
41 * use only in the main running loop of the client (the_game()) where the faster
42 * (up to 10x faster) key lookup is an asset. Other parts of the codebase
43 * (e.g. formspecs) should continue using getKeySetting().
52 populate_nonchanging();
57 // Keys that are not settings dependent
58 void populate_nonchanging();
60 KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
61 InputHandler *handler;
64 class KeyList : private std::list<KeyPress>
66 typedef std::list<KeyPress> super;
67 typedef super::iterator iterator;
68 typedef super::const_iterator const_iterator;
70 virtual const_iterator find(const KeyPress &key) const
72 const_iterator f(begin());
73 const_iterator e(end());
85 virtual iterator find(const KeyPress &key)
101 void clear() { super::clear(); }
103 void set(const KeyPress &key)
105 if (find(key) == end())
109 void unset(const KeyPress &key)
111 iterator p(find(key));
117 void toggle(const KeyPress &key)
119 iterator p(this->find(key));
127 bool operator[](const KeyPress &key) const { return find(key) != end(); }
130 class MyEventReceiver : public IEventReceiver
133 // This is the one method that we have to implement
134 virtual bool OnEvent(const SEvent &event);
136 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
138 // Checks whether a key was down and resets the state
139 bool WasKeyDown(const KeyPress &keyCode)
141 bool b = keyWasDown[keyCode];
143 keyWasDown.unset(keyCode);
147 void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
148 void dontListenForKeys() { keysListenedFor.clear(); }
163 rightclicked = false;
164 leftreleased = false;
165 rightreleased = false;
168 middle_active = false;
169 right_active = false;
176 #ifdef HAVE_TOUCHSCREENGUI
177 m_touchscreengui = NULL;
181 bool leftclicked = false;
182 bool rightclicked = false;
183 bool leftreleased = false;
184 bool rightreleased = false;
186 bool left_active = false;
187 bool middle_active = false;
188 bool right_active = false;
192 JoystickController *joystick = nullptr;
194 #ifdef HAVE_TOUCHSCREENGUI
195 TouchScreenGUI *m_touchscreengui;
199 // The current state of keys
201 // Whether a key has been pressed or not
203 // List of keys we listen for
204 // TODO perhaps the type of this is not really
205 // performant as KeyList is designed for few but
206 // often changing keys, and keysListenedFor is expected
207 // to change seldomly but contain lots of keys.
208 KeyList keysListenedFor;
216 keycache.handler = this;
220 virtual ~InputHandler() = default;
222 virtual bool isKeyDown(GameKeyType k) = 0;
223 virtual bool wasKeyDown(GameKeyType k) = 0;
224 virtual bool cancelPressed() = 0;
226 virtual void listenForKey(const KeyPress &keyCode) {}
227 virtual void dontListenForKeys() {}
229 virtual v2s32 getMousePos() = 0;
230 virtual void setMousePos(s32 x, s32 y) = 0;
232 virtual bool getLeftState() = 0;
233 virtual bool getRightState() = 0;
235 virtual bool getLeftClicked() = 0;
236 virtual bool getRightClicked() = 0;
237 virtual void resetLeftClicked() = 0;
238 virtual void resetRightClicked() = 0;
240 virtual bool getLeftReleased() = 0;
241 virtual bool getRightReleased() = 0;
242 virtual void resetLeftReleased() = 0;
243 virtual void resetRightReleased() = 0;
245 virtual s32 getMouseWheel() = 0;
247 virtual void step(float dtime) {}
249 virtual void clear() {}
251 JoystickController joystick;
255 Separated input handler
258 class RealInputHandler : public InputHandler
261 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
263 m_receiver->joystick = &joystick;
265 virtual bool isKeyDown(GameKeyType k)
267 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
269 virtual bool wasKeyDown(GameKeyType k)
271 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
273 virtual bool cancelPressed()
275 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
277 virtual void listenForKey(const KeyPress &keyCode)
279 m_receiver->listenForKey(keyCode);
281 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
282 virtual v2s32 getMousePos()
284 if (RenderingEngine::get_raw_device()->getCursorControl()) {
285 return RenderingEngine::get_raw_device()
293 virtual void setMousePos(s32 x, s32 y)
295 if (RenderingEngine::get_raw_device()->getCursorControl()) {
296 RenderingEngine::get_raw_device()
300 m_mousepos = v2s32(x, y);
304 virtual bool getLeftState()
306 return m_receiver->left_active || joystick.isKeyDown(KeyType::MOUSE_L);
308 virtual bool getRightState()
310 return m_receiver->right_active || joystick.isKeyDown(KeyType::MOUSE_R);
313 virtual bool getLeftClicked()
315 return m_receiver->leftclicked ||
316 joystick.getWasKeyDown(KeyType::MOUSE_L);
318 virtual bool getRightClicked()
320 return m_receiver->rightclicked ||
321 joystick.getWasKeyDown(KeyType::MOUSE_R);
324 virtual void resetLeftClicked()
326 m_receiver->leftclicked = false;
327 joystick.clearWasKeyDown(KeyType::MOUSE_L);
329 virtual void resetRightClicked()
331 m_receiver->rightclicked = false;
332 joystick.clearWasKeyDown(KeyType::MOUSE_R);
335 virtual bool getLeftReleased()
337 return m_receiver->leftreleased ||
338 joystick.wasKeyReleased(KeyType::MOUSE_L);
340 virtual bool getRightReleased()
342 return m_receiver->rightreleased ||
343 joystick.wasKeyReleased(KeyType::MOUSE_R);
346 virtual void resetLeftReleased()
348 m_receiver->leftreleased = false;
349 joystick.clearWasKeyReleased(KeyType::MOUSE_L);
351 virtual void resetRightReleased()
353 m_receiver->rightreleased = false;
354 joystick.clearWasKeyReleased(KeyType::MOUSE_R);
357 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
362 m_receiver->clearInput();
366 MyEventReceiver *m_receiver = nullptr;
370 class RandomInputHandler : public InputHandler
373 RandomInputHandler() = default;
375 virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
376 virtual bool wasKeyDown(GameKeyType k) { return false; }
377 virtual bool cancelPressed() { return false; }
378 virtual v2s32 getMousePos() { return mousepos; }
379 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
381 virtual bool getLeftState() { return leftdown; }
382 virtual bool getRightState() { return rightdown; }
384 virtual bool getLeftClicked() { return leftclicked; }
385 virtual bool getRightClicked() { return rightclicked; }
386 virtual void resetLeftClicked() { leftclicked = false; }
387 virtual void resetRightClicked() { rightclicked = false; }
389 virtual bool getLeftReleased() { return leftreleased; }
390 virtual bool getRightReleased() { return rightreleased; }
391 virtual void resetLeftReleased() { leftreleased = false; }
392 virtual void resetRightReleased() { rightreleased = false; }
394 virtual s32 getMouseWheel() { return 0; }
396 virtual void step(float dtime);
398 s32 Rand(s32 min, s32 max);
404 bool leftdown = false;
405 bool rightdown = false;
406 bool leftclicked = false;
407 bool rightclicked = false;
408 bool leftreleased = false;
409 bool rightreleased = false;