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().
51 populate_nonchanging();
56 // Keys that are not settings dependent
57 void populate_nonchanging();
59 KeyPress key[KeyType::INTERNAL_ENUM_COUNT];
60 InputHandler *handler;
63 class KeyList : private std::list<KeyPress>
65 typedef std::list<KeyPress> super;
66 typedef super::iterator iterator;
67 typedef super::const_iterator const_iterator;
69 virtual const_iterator find(const KeyPress &key) const
71 const_iterator f(begin());
72 const_iterator e(end());
84 virtual iterator find(const KeyPress &key)
100 void clear() { super::clear(); }
102 void set(const KeyPress &key)
104 if (find(key) == end())
108 void unset(const KeyPress &key)
110 iterator p(find(key));
116 void toggle(const KeyPress &key)
118 iterator p(this->find(key));
126 bool operator[](const KeyPress &key) const { return find(key) != end(); }
129 class MyEventReceiver : public IEventReceiver
132 // This is the one method that we have to implement
133 virtual bool OnEvent(const SEvent &event);
135 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
137 // Checks whether a key was down and resets the state
138 bool WasKeyDown(const KeyPress &keyCode)
140 bool b = keyWasDown[keyCode];
142 keyWasDown.unset(keyCode);
146 void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
147 void dontListenForKeys() { keysListenedFor.clear(); }
162 rightclicked = false;
163 leftreleased = false;
164 rightreleased = false;
167 middle_active = false;
168 right_active = false;
175 #ifdef HAVE_TOUCHSCREENGUI
176 m_touchscreengui = NULL;
180 bool leftclicked = false;
181 bool rightclicked = false;
182 bool leftreleased = false;
183 bool rightreleased = false;
185 bool left_active = false;
186 bool middle_active = false;
187 bool right_active = false;
191 JoystickController *joystick = nullptr;
193 #ifdef HAVE_TOUCHSCREENGUI
194 TouchScreenGUI *m_touchscreengui;
198 // The current state of keys
200 // Whether a key has been pressed or not
202 // List of keys we listen for
203 // TODO perhaps the type of this is not really
204 // performant as KeyList is designed for few but
205 // often changing keys, and keysListenedFor is expected
206 // to change seldomly but contain lots of keys.
207 KeyList keysListenedFor;
213 InputHandler() = default;
215 virtual ~InputHandler() = default;
217 virtual bool isKeyDown(const KeyPress &keyCode) = 0;
218 virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
220 virtual void listenForKey(const KeyPress &keyCode) {}
221 virtual void dontListenForKeys() {}
223 virtual v2s32 getMousePos() = 0;
224 virtual void setMousePos(s32 x, s32 y) = 0;
226 virtual bool getLeftState() = 0;
227 virtual bool getRightState() = 0;
229 virtual bool getLeftClicked() = 0;
230 virtual bool getRightClicked() = 0;
231 virtual void resetLeftClicked() = 0;
232 virtual void resetRightClicked() = 0;
234 virtual bool getLeftReleased() = 0;
235 virtual bool getRightReleased() = 0;
236 virtual void resetLeftReleased() = 0;
237 virtual void resetRightReleased() = 0;
239 virtual s32 getMouseWheel() = 0;
241 virtual void step(float dtime) {}
243 virtual void clear() {}
245 JoystickController joystick;
248 Separated input handler
251 class RealInputHandler : public InputHandler
254 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
256 m_receiver->joystick = &joystick;
258 virtual bool isKeyDown(const KeyPress &keyCode)
260 return m_receiver->IsKeyDown(keyCode);
262 virtual bool wasKeyDown(const KeyPress &keyCode)
264 return m_receiver->WasKeyDown(keyCode);
266 virtual void listenForKey(const KeyPress &keyCode)
268 m_receiver->listenForKey(keyCode);
270 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
271 virtual v2s32 getMousePos()
273 if (RenderingEngine::get_raw_device()->getCursorControl()) {
274 return RenderingEngine::get_raw_device()
282 virtual void setMousePos(s32 x, s32 y)
284 if (RenderingEngine::get_raw_device()->getCursorControl()) {
285 RenderingEngine::get_raw_device()
289 m_mousepos = v2s32(x, y);
293 virtual bool getLeftState()
295 return m_receiver->left_active || joystick.isKeyDown(KeyType::MOUSE_L);
297 virtual bool getRightState()
299 return m_receiver->right_active || joystick.isKeyDown(KeyType::MOUSE_R);
302 virtual bool getLeftClicked()
304 return m_receiver->leftclicked || joystick.getWasKeyDown(KeyType::MOUSE_L);
306 virtual bool getRightClicked()
308 return m_receiver->rightclicked || joystick.getWasKeyDown(KeyType::MOUSE_R);
311 virtual void resetLeftClicked()
313 m_receiver->leftclicked = false;
314 joystick.clearWasKeyDown(KeyType::MOUSE_L);
316 virtual void resetRightClicked() {
317 m_receiver->rightclicked = false;
318 joystick.clearWasKeyDown(KeyType::MOUSE_R);
321 virtual bool getLeftReleased()
323 return m_receiver->leftreleased || joystick.wasKeyReleased(KeyType::MOUSE_L);
325 virtual bool getRightReleased()
327 return m_receiver->rightreleased || joystick.wasKeyReleased(KeyType::MOUSE_R);
330 virtual void resetLeftReleased()
332 m_receiver->leftreleased = false;
333 joystick.clearWasKeyReleased(KeyType::MOUSE_L);
335 virtual void resetRightReleased()
337 m_receiver->rightreleased = false;
338 joystick.clearWasKeyReleased(KeyType::MOUSE_R);
341 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
346 m_receiver->clearInput();
350 MyEventReceiver *m_receiver = nullptr;
354 class RandomInputHandler : public InputHandler
357 RandomInputHandler() = default;
359 virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
360 virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
361 virtual v2s32 getMousePos() { return mousepos; }
362 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
364 virtual bool getLeftState() { return leftdown; }
365 virtual bool getRightState() { return rightdown; }
367 virtual bool getLeftClicked() { return leftclicked; }
368 virtual bool getRightClicked() { return rightclicked; }
369 virtual void resetLeftClicked() { leftclicked = false; }
370 virtual void resetRightClicked() { rightclicked = false; }
372 virtual bool getLeftReleased() { return leftreleased; }
373 virtual bool getRightReleased() { return rightreleased; }
374 virtual void resetLeftReleased() { leftreleased = false; }
375 virtual void resetRightReleased() { rightreleased = false; }
377 virtual s32 getMouseWheel() { return 0; }
379 virtual void step(float dtime)
382 static float counter1 = 0;
384 if (counter1 < 0.0) {
385 counter1 = 0.1 * Rand(1, 40);
386 keydown.toggle(getKeySetting("keymap_jump"));
390 static float counter1 = 0;
392 if (counter1 < 0.0) {
393 counter1 = 0.1 * Rand(1, 40);
394 keydown.toggle(getKeySetting("keymap_special1"));
398 static float counter1 = 0;
400 if (counter1 < 0.0) {
401 counter1 = 0.1 * Rand(1, 40);
402 keydown.toggle(getKeySetting("keymap_forward"));
406 static float counter1 = 0;
408 if (counter1 < 0.0) {
409 counter1 = 0.1 * Rand(1, 40);
410 keydown.toggle(getKeySetting("keymap_left"));
414 static float counter1 = 0;
416 if (counter1 < 0.0) {
417 counter1 = 0.1 * Rand(1, 20);
418 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
422 static float counter1 = 0;
424 if (counter1 < 0.0) {
425 counter1 = 0.1 * Rand(1, 30);
426 leftdown = !leftdown;
434 static float counter1 = 0;
436 if (counter1 < 0.0) {
437 counter1 = 0.1 * Rand(1, 15);
438 rightdown = !rightdown;
442 rightreleased = true;
445 mousepos += mousespeed;
448 s32 Rand(s32 min, s32 max);
454 bool leftdown = false;
455 bool rightdown = false;
456 bool leftclicked = false;
457 bool rightclicked = false;
458 bool leftreleased = false;
459 bool rightreleased = false;