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"
25 #include "renderingengine.h"
26 #include <unordered_set>
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 MyEventReceiver : public IEventReceiver
67 // This is the one method that we have to implement
68 virtual bool OnEvent(const SEvent &event);
70 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown.count(keyCode); }
72 // Checks whether a key was down and resets the state
73 bool WasKeyDown(const KeyPress &keyCode)
75 bool b = keyWasDown.count(keyCode);
77 keyWasDown.erase(keyCode);
81 // Checks whether a key was just pressed. State will be cleared
82 // in the subsequent iteration of Game::processPlayerInteraction
83 bool WasKeyPressed(const KeyPress &keycode) const { return keyWasPressed.count(keycode); }
85 // Checks whether a key was just released. State will be cleared
86 // in the subsequent iteration of Game::processPlayerInteraction
87 bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased.count(keycode); }
89 void listenForKey(const KeyPress &keyCode) { keysListenedFor.insert(keyCode); }
90 void dontListenForKeys() { keysListenedFor.clear(); }
103 keyWasPressed.clear();
104 keyWasReleased.clear();
109 void clearWasKeyPressed()
111 keyWasPressed.clear();
114 void clearWasKeyReleased()
116 keyWasReleased.clear();
121 #ifdef HAVE_TOUCHSCREENGUI
122 m_touchscreengui = NULL;
128 JoystickController *joystick = nullptr;
130 #ifdef HAVE_TOUCHSCREENGUI
131 TouchScreenGUI *m_touchscreengui;
135 //! The current state of keys
136 std::unordered_set<KeyPress> keyIsDown;
138 //! Whether a key was down
139 std::unordered_set<KeyPress> keyWasDown;
141 //! Whether a key has just been pressed
142 std::unordered_set<KeyPress> keyWasPressed;
144 //! Whether a key has just been released
145 std::unordered_set<KeyPress> keyWasReleased;
147 //! List of keys we listen for
148 std::unordered_set<KeyPress> keysListenedFor;
156 keycache.handler = this;
160 virtual ~InputHandler() = default;
162 virtual bool isRandom() const
167 virtual bool isKeyDown(GameKeyType k) = 0;
168 virtual bool wasKeyDown(GameKeyType k) = 0;
169 virtual bool wasKeyPressed(GameKeyType k) = 0;
170 virtual bool wasKeyReleased(GameKeyType k) = 0;
171 virtual bool cancelPressed() = 0;
173 virtual void clearWasKeyPressed() {}
174 virtual void clearWasKeyReleased() {}
176 virtual void listenForKey(const KeyPress &keyCode) {}
177 virtual void dontListenForKeys() {}
179 virtual v2s32 getMousePos() = 0;
180 virtual void setMousePos(s32 x, s32 y) = 0;
182 virtual s32 getMouseWheel() = 0;
184 virtual void step(float dtime) {}
186 virtual void clear() {}
188 JoystickController joystick;
192 Separated input handler
195 class RealInputHandler : public InputHandler
198 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
200 m_receiver->joystick = &joystick;
202 virtual bool isKeyDown(GameKeyType k)
204 return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
206 virtual bool wasKeyDown(GameKeyType k)
208 return m_receiver->WasKeyDown(keycache.key[k]) || joystick.wasKeyDown(k);
210 virtual bool wasKeyPressed(GameKeyType k)
212 return m_receiver->WasKeyPressed(keycache.key[k]) || joystick.wasKeyReleased(k);
214 virtual bool wasKeyReleased(GameKeyType k)
216 return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
218 virtual bool cancelPressed()
220 return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
222 virtual void clearWasKeyPressed()
224 m_receiver->clearWasKeyPressed();
226 virtual void clearWasKeyReleased()
228 m_receiver->clearWasKeyReleased();
230 virtual void listenForKey(const KeyPress &keyCode)
232 m_receiver->listenForKey(keyCode);
234 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
235 virtual v2s32 getMousePos()
237 if (RenderingEngine::get_raw_device()->getCursorControl()) {
238 return RenderingEngine::get_raw_device()
246 virtual void setMousePos(s32 x, s32 y)
248 if (RenderingEngine::get_raw_device()->getCursorControl()) {
249 RenderingEngine::get_raw_device()
253 m_mousepos = v2s32(x, y);
257 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
262 m_receiver->clearInput();
266 MyEventReceiver *m_receiver = nullptr;
270 class RandomInputHandler : public InputHandler
273 RandomInputHandler() = default;
275 bool isRandom() const
280 virtual bool isKeyDown(GameKeyType k) { return keydown.count(keycache.key[k]); }
281 virtual bool wasKeyDown(GameKeyType k) { return false; }
282 virtual bool wasKeyPressed(GameKeyType k) { return false; }
283 virtual bool wasKeyReleased(GameKeyType k) { return false; }
284 virtual bool cancelPressed() { return false; }
285 virtual v2s32 getMousePos() { return mousepos; }
286 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
288 virtual s32 getMouseWheel() { return 0; }
290 virtual void step(float dtime);
292 s32 Rand(s32 min, s32 max);
295 std::unordered_set<KeyPress> keydown;