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) { keysListenedFor.set(keyCode); }
157 void dontListenForKeys() { keysListenedFor.clear(); }
170 keyWasPressed.clear();
171 keyWasReleased.clear();
176 void clearWasKeyPressed()
178 keyWasPressed.clear();
181 void clearWasKeyReleased()
183 keyWasReleased.clear();
188 #ifdef HAVE_TOUCHSCREENGUI
189 m_touchscreengui = NULL;
195 JoystickController *joystick = nullptr;
197 #ifdef HAVE_TOUCHSCREENGUI
198 TouchScreenGUI *m_touchscreengui;
201 // The current state of keys
204 // Like keyIsDown but only reset when that key is read
207 // Whether a key has just been pressed
208 KeyList keyWasPressed;
210 // Whether a key has just been released
211 KeyList keyWasReleased;
213 // List of keys we listen for
214 // TODO perhaps the type of this is not really
215 // performant as KeyList is designed for few but
216 // often changing keys, and keysListenedFor is expected
217 // to change seldomly but contain lots of keys.
218 KeyList keysListenedFor;
226 keycache.handler = this;
230 virtual ~InputHandler() = default;
232 virtual bool isRandom() const
237 virtual bool isKeyDown(GameKeyType k) = 0;
238 virtual void setKeypress(const KeyPress &keyCode) = 0;
239 virtual void unsetKeypress(const KeyPress &keyCode) = 0;
240 virtual bool wasKeyDown(GameKeyType k) = 0;
241 virtual bool wasKeyPressed(GameKeyType k) = 0;
242 virtual bool wasKeyReleased(GameKeyType k) = 0;
243 virtual bool cancelPressed() = 0;
245 virtual float getMovementSpeed() = 0;
246 virtual float getMovementDirection() = 0;
248 virtual void clearWasKeyPressed() {}
249 virtual void clearWasKeyReleased() {}
251 virtual void listenForKey(const KeyPress &keyCode) {}
252 virtual void dontListenForKeys() {}
254 virtual v2s32 getMousePos() = 0;
255 virtual void setMousePos(s32 x, s32 y) = 0;
257 virtual s32 getMouseWheel() = 0;
259 virtual void step(float dtime) {}
261 virtual void clear() {}
263 JoystickController joystick;
267 Separated input handler
270 class RealInputHandler : public InputHandler
273 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
275 m_receiver->joystick = &joystick;
277 virtual bool isKeyDown(GameKeyType k)
279 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
281 virtual void setKeypress(const KeyPress &keyCode)
283 m_receiver->keyIsDown.set(keyCode);
284 m_receiver->keyWasDown.set(keyCode);
286 virtual void unsetKeypress(const KeyPress &keyCode)
288 m_receiver->keyIsDown.unset(keyCode);
290 virtual bool wasKeyDown(GameKeyType k)
292 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
294 virtual bool wasKeyPressed(GameKeyType k)
296 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
298 virtual bool wasKeyReleased(GameKeyType k)
300 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
302 virtual float getMovementSpeed()
304 bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
305 b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
306 l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
307 r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
308 if (f || b || l || r)
310 // if contradictory keys pressed, stay still
311 if (f && b && l && r)
313 else if (f && b && !l && !r)
315 else if (!f && !b && l && r)
317 return 1.0f; // If there is a keyboard event, assume maximum speed
319 return joystick.getMovementSpeed();
321 virtual float getMovementDirection()
325 /* Check keyboard for input */
326 if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
328 if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
330 if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
332 if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
335 if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
338 return joystick.getMovementDirection();
340 virtual bool cancelPressed()
342 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
344 virtual void clearWasKeyPressed()
346 m_receiver->clearWasKeyPressed();
348 virtual void clearWasKeyReleased()
350 m_receiver->clearWasKeyReleased();
352 virtual void listenForKey(const KeyPress &keyCode)
354 m_receiver->listenForKey(keyCode);
356 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
357 virtual v2s32 getMousePos()
359 if (RenderingEngine::get_raw_device()->getCursorControl()) {
360 return RenderingEngine::get_raw_device()
368 virtual void setMousePos(s32 x, s32 y)
370 if (RenderingEngine::get_raw_device()->getCursorControl()) {
371 RenderingEngine::get_raw_device()
375 m_mousepos = v2s32(x, y);
379 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
384 m_receiver->clearInput();
388 MyEventReceiver *m_receiver = nullptr;
392 class RandomInputHandler : public InputHandler
395 RandomInputHandler() = default;
397 bool isRandom() const
402 virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
403 virtual void setKeypress(const KeyPress &keyCode)
405 keydown.set(keyCode);
407 virtual void unsetKeypress(const KeyPress &keyCode)
409 keydown.unset(keyCode);
411 virtual bool wasKeyDown(GameKeyType k) { return false; }
412 virtual bool wasKeyPressed(GameKeyType k) { return false; }
413 virtual bool wasKeyReleased(GameKeyType k) { return false; }
414 virtual bool cancelPressed() { return false; }
415 virtual float getMovementSpeed() { return movementSpeed; }
416 virtual float getMovementDirection() { return movementDirection; }
417 virtual v2s32 getMousePos() { return mousepos; }
418 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
420 virtual s32 getMouseWheel() { return 0; }
422 virtual void step(float dtime);
424 s32 Rand(s32 min, s32 max);
431 float movementDirection;