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"
32 class KeyList : private std::list<KeyPress>
34 typedef std::list<KeyPress> super;
35 typedef super::iterator iterator;
36 typedef super::const_iterator const_iterator;
38 virtual const_iterator find(const KeyPress &key) const
40 const_iterator f(begin());
41 const_iterator e(end());
53 virtual iterator find(const KeyPress &key)
69 void clear() { super::clear(); }
71 void set(const KeyPress &key)
73 if (find(key) == end())
77 void unset(const KeyPress &key)
79 iterator p(find(key));
85 void toggle(const KeyPress &key)
87 iterator p(this->find(key));
95 bool operator[](const KeyPress &key) const { return find(key) != end(); }
98 class MyEventReceiver : public IEventReceiver
101 // This is the one method that we have to implement
102 virtual bool OnEvent(const SEvent &event);
104 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
106 // Checks whether a key was down and resets the state
107 bool WasKeyDown(const KeyPress &keyCode)
109 bool b = keyWasDown[keyCode];
111 keyWasDown.unset(keyCode);
115 void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
116 void dontListenForKeys() { keysListenedFor.clear(); }
131 rightclicked = false;
132 leftreleased = false;
133 rightreleased = false;
136 middle_active = false;
137 right_active = false;
144 #ifdef HAVE_TOUCHSCREENGUI
145 m_touchscreengui = NULL;
149 bool leftclicked = false;
150 bool rightclicked = false;
151 bool leftreleased = false;
152 bool rightreleased = false;
154 bool left_active = false;
155 bool middle_active = false;
156 bool right_active = false;
160 JoystickController *joystick = nullptr;
162 #ifdef HAVE_TOUCHSCREENGUI
163 TouchScreenGUI *m_touchscreengui;
167 // The current state of keys
169 // Whether a key has been pressed or not
171 // List of keys we listen for
172 // TODO perhaps the type of this is not really
173 // performant as KeyList is designed for few but
174 // often changing keys, and keysListenedFor is expected
175 // to change seldomly but contain lots of keys.
176 KeyList keysListenedFor;
182 InputHandler() = default;
184 virtual ~InputHandler() = default;
186 virtual bool isKeyDown(const KeyPress &keyCode) = 0;
187 virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
189 virtual void listenForKey(const KeyPress &keyCode) {}
190 virtual void dontListenForKeys() {}
192 virtual v2s32 getMousePos() = 0;
193 virtual void setMousePos(s32 x, s32 y) = 0;
195 virtual bool getLeftState() = 0;
196 virtual bool getRightState() = 0;
198 virtual bool getLeftClicked() = 0;
199 virtual bool getRightClicked() = 0;
200 virtual void resetLeftClicked() = 0;
201 virtual void resetRightClicked() = 0;
203 virtual bool getLeftReleased() = 0;
204 virtual bool getRightReleased() = 0;
205 virtual void resetLeftReleased() = 0;
206 virtual void resetRightReleased() = 0;
208 virtual s32 getMouseWheel() = 0;
210 virtual void step(float dtime) {}
212 virtual void clear() {}
214 JoystickController joystick;
217 Separated input handler
220 class RealInputHandler : public InputHandler
223 RealInputHandler(MyEventReceiver *receiver) : m_receiver(receiver)
225 m_receiver->joystick = &joystick;
227 virtual bool isKeyDown(const KeyPress &keyCode)
229 return m_receiver->IsKeyDown(keyCode);
231 virtual bool wasKeyDown(const KeyPress &keyCode)
233 return m_receiver->WasKeyDown(keyCode);
235 virtual void listenForKey(const KeyPress &keyCode)
237 m_receiver->listenForKey(keyCode);
239 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
240 virtual v2s32 getMousePos()
242 if (RenderingEngine::get_raw_device()->getCursorControl()) {
243 return RenderingEngine::get_raw_device()
251 virtual void setMousePos(s32 x, s32 y)
253 if (RenderingEngine::get_raw_device()->getCursorControl()) {
254 RenderingEngine::get_raw_device()
258 m_mousepos = v2s32(x, y);
262 virtual bool getLeftState()
264 return m_receiver->left_active || joystick.isKeyDown(KeyType::MOUSE_L);
266 virtual bool getRightState()
268 return m_receiver->right_active || joystick.isKeyDown(KeyType::MOUSE_R);
271 virtual bool getLeftClicked()
273 return m_receiver->leftclicked || joystick.getWasKeyDown(KeyType::MOUSE_L);
275 virtual bool getRightClicked()
277 return m_receiver->rightclicked || joystick.getWasKeyDown(KeyType::MOUSE_R);
280 virtual void resetLeftClicked()
282 m_receiver->leftclicked = false;
283 joystick.clearWasKeyDown(KeyType::MOUSE_L);
285 virtual void resetRightClicked() {
286 m_receiver->rightclicked = false;
287 joystick.clearWasKeyDown(KeyType::MOUSE_R);
290 virtual bool getLeftReleased()
292 return m_receiver->leftreleased || joystick.wasKeyReleased(KeyType::MOUSE_L);
294 virtual bool getRightReleased()
296 return m_receiver->rightreleased || joystick.wasKeyReleased(KeyType::MOUSE_R);
299 virtual void resetLeftReleased()
301 m_receiver->leftreleased = false;
302 joystick.clearWasKeyReleased(KeyType::MOUSE_L);
304 virtual void resetRightReleased()
306 m_receiver->rightreleased = false;
307 joystick.clearWasKeyReleased(KeyType::MOUSE_R);
310 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
315 m_receiver->clearInput();
319 MyEventReceiver *m_receiver = nullptr;
323 class RandomInputHandler : public InputHandler
326 RandomInputHandler() = default;
328 virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
329 virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
330 virtual v2s32 getMousePos() { return mousepos; }
331 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
333 virtual bool getLeftState() { return leftdown; }
334 virtual bool getRightState() { return rightdown; }
336 virtual bool getLeftClicked() { return leftclicked; }
337 virtual bool getRightClicked() { return rightclicked; }
338 virtual void resetLeftClicked() { leftclicked = false; }
339 virtual void resetRightClicked() { rightclicked = false; }
341 virtual bool getLeftReleased() { return leftreleased; }
342 virtual bool getRightReleased() { return rightreleased; }
343 virtual void resetLeftReleased() { leftreleased = false; }
344 virtual void resetRightReleased() { rightreleased = false; }
346 virtual s32 getMouseWheel() { return 0; }
348 virtual void step(float dtime)
351 static float counter1 = 0;
353 if (counter1 < 0.0) {
354 counter1 = 0.1 * Rand(1, 40);
355 keydown.toggle(getKeySetting("keymap_jump"));
359 static float counter1 = 0;
361 if (counter1 < 0.0) {
362 counter1 = 0.1 * Rand(1, 40);
363 keydown.toggle(getKeySetting("keymap_special1"));
367 static float counter1 = 0;
369 if (counter1 < 0.0) {
370 counter1 = 0.1 * Rand(1, 40);
371 keydown.toggle(getKeySetting("keymap_forward"));
375 static float counter1 = 0;
377 if (counter1 < 0.0) {
378 counter1 = 0.1 * Rand(1, 40);
379 keydown.toggle(getKeySetting("keymap_left"));
383 static float counter1 = 0;
385 if (counter1 < 0.0) {
386 counter1 = 0.1 * Rand(1, 20);
387 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
391 static float counter1 = 0;
393 if (counter1 < 0.0) {
394 counter1 = 0.1 * Rand(1, 30);
395 leftdown = !leftdown;
403 static float counter1 = 0;
405 if (counter1 < 0.0) {
406 counter1 = 0.1 * Rand(1, 15);
407 rightdown = !rightdown;
411 rightreleased = true;
414 mousepos += mousespeed;
417 s32 Rand(s32 min, s32 max);
423 bool leftdown = false;
424 bool rightdown = false;
425 bool leftclicked = false;
426 bool rightclicked = false;
427 bool leftreleased = false;
428 bool rightreleased = false;