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 // Checks whether a key was just pressed. State will be cleared
148 // in the subsequent iteration of Game::processPlayerInteraction
149 bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed[keycode]; }
151 // Checks whether a key was just released. State will be cleared
152 // in the subsequent iteration of Game::processPlayerInteraction
153 bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
155 void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
156 void dontListenForKeys() { keysListenedFor.clear(); }
169 keyWasPressed.clear();
170 keyWasReleased.clear();
175 void clearWasKeyPressed()
177 keyWasPressed.clear();
180 void clearWasKeyReleased()
182 keyWasReleased.clear();
187 #ifdef HAVE_TOUCHSCREENGUI
188 m_touchscreengui = NULL;
194 JoystickController *joystick = nullptr;
196 #ifdef HAVE_TOUCHSCREENGUI
197 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 bool wasKeyDown(GameKeyType k) = 0;
239 virtual bool wasKeyPressed(GameKeyType k) = 0;
240 virtual bool wasKeyReleased(GameKeyType k) = 0;
241 virtual bool cancelPressed() = 0;
243 virtual float getMovementSpeed() = 0;
244 virtual float getMovementDirection() = 0;
246 virtual void clearWasKeyPressed() {}
247 virtual void clearWasKeyReleased() {}
249 virtual void listenForKey(const KeyPress &keyCode) {}
250 virtual void dontListenForKeys() {}
252 virtual v2s32 getMousePos() = 0;
253 virtual void setMousePos(s32 x, s32 y) = 0;
255 virtual s32 getMouseWheel() = 0;
257 virtual void step(float dtime) {}
259 virtual void clear() {}
261 JoystickController joystick;
265 Separated input handler
268 class RealInputHandler : public InputHandler
271 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
273 m_receiver->joystick = &joystick;
275 virtual bool isKeyDown(GameKeyType k)
277 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
279 virtual bool wasKeyDown(GameKeyType k)
281 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
283 virtual bool wasKeyPressed(GameKeyType k)
285 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyPressed(k);
287 virtual bool wasKeyReleased(GameKeyType k)
289 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
291 virtual float getMovementSpeed()
293 bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
294 b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
295 l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
296 r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
297 if (f || b || l || r)
299 // if contradictory keys pressed, stay still
300 if (f && b && l && r)
302 else if (f && b && !l && !r)
304 else if (!f && !b && l && r)
306 return 1.0f; // If there is a keyboard event, assume maximum speed
308 return joystick.getMovementSpeed();
310 virtual float getMovementDirection()
314 /* Check keyboard for input */
315 if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
317 if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
319 if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
321 if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
324 if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
327 return joystick.getMovementDirection();
329 virtual bool cancelPressed()
331 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
333 virtual void clearWasKeyPressed()
335 m_receiver->clearWasKeyPressed();
337 virtual void clearWasKeyReleased()
339 m_receiver->clearWasKeyReleased();
341 virtual void listenForKey(const KeyPress &keyCode)
343 m_receiver->listenForKey(keyCode);
345 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
346 virtual v2s32 getMousePos()
348 if (RenderingEngine::get_raw_device()->getCursorControl()) {
349 return RenderingEngine::get_raw_device()
357 virtual void setMousePos(s32 x, s32 y)
359 if (RenderingEngine::get_raw_device()->getCursorControl()) {
360 RenderingEngine::get_raw_device()
364 m_mousepos = v2s32(x, y);
368 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
373 m_receiver->clearInput();
377 MyEventReceiver *m_receiver = nullptr;
381 class RandomInputHandler : public InputHandler
384 RandomInputHandler() = default;
386 bool isRandom() const
391 virtual bool isKeyDown(GameKeyType k) { return keydown[keycache.key[k]]; }
392 virtual bool wasKeyDown(GameKeyType k) { return false; }
393 virtual bool wasKeyPressed(GameKeyType k) { return false; }
394 virtual bool wasKeyReleased(GameKeyType k) { return false; }
395 virtual bool cancelPressed() { return false; }
396 virtual float getMovementSpeed() { return movementSpeed; }
397 virtual float getMovementDirection() { return movementDirection; }
398 virtual v2s32 getMousePos() { return mousepos; }
399 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
401 virtual s32 getMouseWheel() { return 0; }
403 virtual void step(float dtime);
405 s32 Rand(s32 min, s32 max);
412 float movementDirection;