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.
20 #ifndef INPUT_HANDLER_H
21 #define INPUT_HANDLER_H
23 #include "irrlichttypes_extrabloated.h"
24 #include "joystick_controller.h"
27 #include "renderingengine.h"
29 #ifdef HAVE_TOUCHSCREENGUI
30 #include "touchscreengui.h"
33 class KeyList : private std::list<KeyPress>
35 typedef std::list<KeyPress> super;
36 typedef super::iterator iterator;
37 typedef super::const_iterator const_iterator;
39 virtual const_iterator find(const KeyPress &key) const
41 const_iterator f(begin());
42 const_iterator e(end());
54 virtual iterator find(const KeyPress &key)
70 void clear() { super::clear(); }
72 void set(const KeyPress &key)
74 if (find(key) == end())
78 void unset(const KeyPress &key)
80 iterator p(find(key));
86 void toggle(const KeyPress &key)
88 iterator p(this->find(key));
96 bool operator[](const KeyPress &key) const { return find(key) != end(); }
99 class MyEventReceiver : public IEventReceiver
102 // This is the one method that we have to implement
103 virtual bool OnEvent(const SEvent &event);
105 bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; }
107 // Checks whether a key was down and resets the state
108 bool WasKeyDown(const KeyPress &keyCode)
110 bool b = keyWasDown[keyCode];
112 keyWasDown.unset(keyCode);
116 void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
117 void dontListenForKeys() { keysListenedFor.clear(); }
132 rightclicked = false;
133 leftreleased = false;
134 rightreleased = false;
137 middle_active = false;
138 right_active = false;
145 #ifdef HAVE_TOUCHSCREENGUI
146 m_touchscreengui = NULL;
150 bool leftclicked = false;
151 bool rightclicked = false;
152 bool leftreleased = false;
153 bool rightreleased = false;
155 bool left_active = false;
156 bool middle_active = false;
157 bool right_active = false;
161 JoystickController *joystick = nullptr;
163 #ifdef HAVE_TOUCHSCREENGUI
164 TouchScreenGUI *m_touchscreengui;
168 // The current state of keys
170 // Whether a key has been pressed or not
172 // List of keys we listen for
173 // TODO perhaps the type of this is not really
174 // performant as KeyList is designed for few but
175 // often changing keys, and keysListenedFor is expected
176 // to change seldomly but contain lots of keys.
177 KeyList keysListenedFor;
184 virtual ~InputHandler() {}
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()
250 virtual void setMousePos(s32 x, s32 y)
252 if (RenderingEngine::get_raw_device()->getCursorControl()) {
253 RenderingEngine::get_raw_device()
257 m_mousepos = v2s32(x, y);
261 virtual bool getLeftState() { return m_receiver->left_active; }
262 virtual bool getRightState() { return m_receiver->right_active; }
264 virtual bool getLeftClicked() { return m_receiver->leftclicked; }
265 virtual bool getRightClicked() { return m_receiver->rightclicked; }
266 virtual void resetLeftClicked() { m_receiver->leftclicked = false; }
267 virtual void resetRightClicked() { m_receiver->rightclicked = false; }
269 virtual bool getLeftReleased() { return m_receiver->leftreleased; }
270 virtual bool getRightReleased() { return m_receiver->rightreleased; }
271 virtual void resetLeftReleased() { m_receiver->leftreleased = false; }
272 virtual void resetRightReleased() { m_receiver->rightreleased = false; }
274 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
279 m_receiver->clearInput();
283 MyEventReceiver *m_receiver = nullptr;
287 class RandomInputHandler : public InputHandler
290 RandomInputHandler() {}
291 virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
292 virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
293 virtual v2s32 getMousePos() { return mousepos; }
294 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
296 virtual bool getLeftState() { return leftdown; }
297 virtual bool getRightState() { return rightdown; }
299 virtual bool getLeftClicked() { return leftclicked; }
300 virtual bool getRightClicked() { return rightclicked; }
301 virtual void resetLeftClicked() { leftclicked = false; }
302 virtual void resetRightClicked() { rightclicked = false; }
304 virtual bool getLeftReleased() { return leftreleased; }
305 virtual bool getRightReleased() { return rightreleased; }
306 virtual void resetLeftReleased() { leftreleased = false; }
307 virtual void resetRightReleased() { rightreleased = false; }
309 virtual s32 getMouseWheel() { return 0; }
311 virtual void step(float dtime)
314 static float counter1 = 0;
316 if (counter1 < 0.0) {
317 counter1 = 0.1 * Rand(1, 40);
318 keydown.toggle(getKeySetting("keymap_jump"));
322 static float counter1 = 0;
324 if (counter1 < 0.0) {
325 counter1 = 0.1 * Rand(1, 40);
326 keydown.toggle(getKeySetting("keymap_special1"));
330 static float counter1 = 0;
332 if (counter1 < 0.0) {
333 counter1 = 0.1 * Rand(1, 40);
334 keydown.toggle(getKeySetting("keymap_forward"));
338 static float counter1 = 0;
340 if (counter1 < 0.0) {
341 counter1 = 0.1 * Rand(1, 40);
342 keydown.toggle(getKeySetting("keymap_left"));
346 static float counter1 = 0;
348 if (counter1 < 0.0) {
349 counter1 = 0.1 * Rand(1, 20);
350 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
354 static float counter1 = 0;
356 if (counter1 < 0.0) {
357 counter1 = 0.1 * Rand(1, 30);
358 leftdown = !leftdown;
366 static float counter1 = 0;
368 if (counter1 < 0.0) {
369 counter1 = 0.1 * Rand(1, 15);
370 rightdown = !rightdown;
374 rightreleased = true;
377 mousepos += mousespeed;
380 s32 Rand(s32 min, s32 max);
386 bool leftdown = false;
387 bool rightdown = false;
388 bool leftclicked = false;
389 bool rightclicked = false;
390 bool leftreleased = false;
391 bool rightreleased = false;