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"
35 /****************************************************************************
36 Fast key cache for main game loop
37 ****************************************************************************/
39 /* This is faster than using getKeySetting with the tradeoff that functions
40 * using it must make sure that it's initialised before using it and there is
41 * no error handling (for example bounds checking). This is really intended for
42 * use only in the main running loop of the client (the_game()) where the faster
43 * (up to 10x faster) key lookup is an asset. Other parts of the codebase
44 * (e.g. formspecs) should continue using getKeySetting().
53 populate_nonchanging();
58 // Keys that are not settings dependent
59 void populate_nonchanging();
61 KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
62 InputHandler *handler;
65 class KeyList : private std::list<KeyPress>
67 typedef std::list<KeyPress> super;
68 typedef super::iterator iterator;
69 typedef super::const_iterator const_iterator;
71 virtual const_iterator find(const KeyPress &key) const
73 const_iterator f(begin());
74 const_iterator e(end());
86 virtual iterator find(const KeyPress &key)
102 void clear() { super::clear(); }
104 void set(const KeyPress &key)
106 if (find(key) == end())
110 void unset(const KeyPress &key)
112 iterator p(find(key));
118 void toggle(const KeyPress &key)
120 iterator p(this->find(key));
128 bool operator[](const KeyPress &key) const { return find(key) != end(); }
131 class MyEventReceiver : public IEventReceiver
134 // This is the one method that we have to implement
135 virtual bool OnEvent(const SEvent &event);
137 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
139 // Checks whether a key was down and resets the state
140 bool WasKeyDown(const KeyPress &keyCode)
142 bool b = keyWasDown[keyCode];
144 keyWasDown.unset(keyCode);
148 // Checks whether a key was just pressed. State will be cleared
149 // in the subsequent iteration of Game::processPlayerInteraction
150 bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
152 // Checks whether a key was just released. State will be cleared
153 // in the subsequent iteration of Game::processPlayerInteraction
154 bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
156 void listenForKey(const KeyPress &keyCode)
158 keysListenedFor.set(keyCode);
160 void dontListenForKeys()
162 keysListenedFor.clear();
176 keyWasPressed.clear();
177 keyWasReleased.clear();
182 void clearWasKeyPressed()
184 keyWasPressed.clear();
187 void clearWasKeyReleased()
189 keyWasReleased.clear();
194 #ifdef HAVE_TOUCHSCREENGUI
195 m_touchscreengui = NULL;
199 JoystickController *joystick = nullptr;
201 #ifdef HAVE_TOUCHSCREENGUI
202 TouchScreenGUI *m_touchscreengui;
207 // The current state of keys
210 // Like keyIsDown but only reset when that key is read
213 // Whether a key has just been pressed
214 KeyList keyWasPressed;
216 // Whether a key has just been released
217 KeyList keyWasReleased;
219 // List of keys we listen for
220 // TODO perhaps the type of this is not really
221 // performant as KeyList is designed for few but
222 // often changing keys, and keysListenedFor is expected
223 // to change seldomly but contain lots of keys.
224 KeyList keysListenedFor;
232 keycache.handler = this;
236 virtual ~InputHandler() = default;
238 virtual bool isRandom() const
243 virtual bool isKeyDown(GameKeyType k) = 0;
244 virtual void setKeypress(const KeyPress &keyCode) = 0;
245 virtual void unsetKeypress(const KeyPress &keyCode) = 0;
246 virtual bool wasKeyDown(GameKeyType k) = 0;
247 virtual bool wasKeyPressed(GameKeyType k) = 0;
248 virtual bool wasKeyReleased(GameKeyType k) = 0;
249 virtual bool cancelPressed() = 0;
251 virtual float getMovementSpeed() = 0;
252 virtual float getMovementDirection() = 0;
254 virtual void clearWasKeyPressed() {}
255 virtual void clearWasKeyReleased() {}
257 virtual void listenForKey(const KeyPress &keyCode) {}
258 virtual void dontListenForKeys() {}
260 virtual v2s32 getMousePos() = 0;
261 virtual void setMousePos(s32 x, s32 y) = 0;
263 virtual s32 getMouseWheel() = 0;
265 virtual void step(float dtime) {}
267 virtual void clear() {}
269 JoystickController joystick;
273 Separated input handler
276 class RealInputHandler : public InputHandler
279 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
281 m_receiver->joystick = &joystick;
284 virtual ~RealInputHandler()
286 m_receiver->joystick = nullptr;
289 virtual bool isKeyDown(GameKeyType k)
291 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
293 virtual void setKeypress(const KeyPress &keyCode)
295 m_receiver->keyIsDown.set(keyCode);
296 m_receiver->keyWasDown.set(keyCode);
298 virtual void unsetKeypress(const KeyPress &keyCode)
300 m_receiver->keyIsDown.unset(keyCode);
302 virtual bool wasKeyDown(GameKeyType k)
304 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
306 virtual bool wasKeyPressed(GameKeyType k)
308 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
310 virtual bool wasKeyReleased(GameKeyType k)
312 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
315 virtual float getMovementSpeed()
317 bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
318 b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
319 l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
320 r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
321 if (f || b || l || r)
323 // if contradictory keys pressed, stay still
324 if (f && b && l && r)
326 else if (f && b && !l && !r)
328 else if (!f && !b && l && r)
330 return 1.0f; // If there is a keyboard event, assume maximum speed
332 return joystick.getMovementSpeed();
335 virtual float getMovementDirection()
339 /* Check keyboard for input */
340 if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
342 if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
344 if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
346 if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
349 if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
352 return joystick.getMovementDirection();
355 virtual bool cancelPressed()
357 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
360 virtual void clearWasKeyPressed()
362 m_receiver->clearWasKeyPressed();
364 virtual void clearWasKeyReleased()
366 m_receiver->clearWasKeyReleased();
369 virtual void listenForKey(const KeyPress &keyCode)
371 m_receiver->listenForKey(keyCode);
373 virtual void dontListenForKeys()
375 m_receiver->dontListenForKeys();
378 virtual v2s32 getMousePos()
380 auto control = RenderingEngine::get_raw_device()->getCursorControl();
382 return control->getPosition();
388 virtual void setMousePos(s32 x, s32 y)
390 auto control = RenderingEngine::get_raw_device()->getCursorControl();
392 control->setPosition(x, y);
394 m_mousepos = v2s32(x, y);
398 virtual s32 getMouseWheel()
400 return m_receiver->getMouseWheel();
406 m_receiver->clearInput();
410 MyEventReceiver *m_receiver = nullptr;
414 class RandomInputHandler : public InputHandler
417 RandomInputHandler() = default;
419 bool isRandom() const
424 virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
425 virtual void setKeypress(const KeyPress &keyCode)
427 keydown.set(keyCode);
429 virtual void unsetKeypress(const KeyPress &keyCode)
431 keydown.unset(keyCode);
433 virtual bool wasKeyDown(GameKeyType k) { return false; }
434 virtual bool wasKeyPressed(GameKeyType k) { return false; }
435 virtual bool wasKeyReleased(GameKeyType k) { return false; }
436 virtual bool cancelPressed() { return false; }
437 virtual float getMovementSpeed() { return movementSpeed; }
438 virtual float getMovementDirection() { return movementDirection; }
439 virtual v2s32 getMousePos() { return mousepos; }
440 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
442 virtual s32 getMouseWheel() { return 0; }
444 virtual void step(float dtime);
446 s32 Rand(s32 min, s32 max);
453 float movementDirection;