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"
28 #ifdef HAVE_TOUCHSCREENGUI
29 #include "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;
145 #ifdef HAVE_TOUCHSCREENGUI
146 m_touchscreengui = NULL;
161 JoystickController *joystick;
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(IrrlichtDevice *device, MyEventReceiver *receiver)
224 : m_device(device), m_receiver(receiver), m_mousepos(0, 0)
226 m_receiver->joystick = &joystick;
228 virtual bool isKeyDown(const KeyPress &keyCode)
230 return m_receiver->IsKeyDown(keyCode);
232 virtual bool wasKeyDown(const KeyPress &keyCode)
234 return m_receiver->WasKeyDown(keyCode);
236 virtual void listenForKey(const KeyPress &keyCode)
238 m_receiver->listenForKey(keyCode);
240 virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
241 virtual v2s32 getMousePos()
243 if (m_device->getCursorControl()) {
244 return m_device->getCursorControl()->getPosition();
249 virtual void setMousePos(s32 x, s32 y)
251 if (m_device->getCursorControl()) {
252 m_device->getCursorControl()->setPosition(x, y);
254 m_mousepos = v2s32(x, y);
258 virtual bool getLeftState() { return m_receiver->left_active; }
259 virtual bool getRightState() { return m_receiver->right_active; }
261 virtual bool getLeftClicked() { return m_receiver->leftclicked; }
262 virtual bool getRightClicked() { return m_receiver->rightclicked; }
263 virtual void resetLeftClicked() { m_receiver->leftclicked = false; }
264 virtual void resetRightClicked() { m_receiver->rightclicked = false; }
266 virtual bool getLeftReleased() { return m_receiver->leftreleased; }
267 virtual bool getRightReleased() { return m_receiver->rightreleased; }
268 virtual void resetLeftReleased() { m_receiver->leftreleased = false; }
269 virtual void resetRightReleased() { m_receiver->rightreleased = false; }
271 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
276 m_receiver->clearInput();
280 IrrlichtDevice *m_device;
281 MyEventReceiver *m_receiver;
285 class RandomInputHandler : public InputHandler
293 rightclicked = false;
294 leftreleased = false;
295 rightreleased = false;
298 virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
299 virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
300 virtual v2s32 getMousePos() { return mousepos; }
301 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
303 virtual bool getLeftState() { return leftdown; }
304 virtual bool getRightState() { return rightdown; }
306 virtual bool getLeftClicked() { return leftclicked; }
307 virtual bool getRightClicked() { return rightclicked; }
308 virtual void resetLeftClicked() { leftclicked = false; }
309 virtual void resetRightClicked() { rightclicked = false; }
311 virtual bool getLeftReleased() { return leftreleased; }
312 virtual bool getRightReleased() { return rightreleased; }
313 virtual void resetLeftReleased() { leftreleased = false; }
314 virtual void resetRightReleased() { rightreleased = false; }
316 virtual s32 getMouseWheel() { return 0; }
318 virtual void step(float dtime)
321 static float counter1 = 0;
323 if (counter1 < 0.0) {
324 counter1 = 0.1 * Rand(1, 40);
325 keydown.toggle(getKeySetting("keymap_jump"));
329 static float counter1 = 0;
331 if (counter1 < 0.0) {
332 counter1 = 0.1 * Rand(1, 40);
333 keydown.toggle(getKeySetting("keymap_special1"));
337 static float counter1 = 0;
339 if (counter1 < 0.0) {
340 counter1 = 0.1 * Rand(1, 40);
341 keydown.toggle(getKeySetting("keymap_forward"));
345 static float counter1 = 0;
347 if (counter1 < 0.0) {
348 counter1 = 0.1 * Rand(1, 40);
349 keydown.toggle(getKeySetting("keymap_left"));
353 static float counter1 = 0;
355 if (counter1 < 0.0) {
356 counter1 = 0.1 * Rand(1, 20);
357 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
361 static float counter1 = 0;
363 if (counter1 < 0.0) {
364 counter1 = 0.1 * Rand(1, 30);
365 leftdown = !leftdown;
373 static float counter1 = 0;
375 if (counter1 < 0.0) {
376 counter1 = 0.1 * Rand(1, 15);
377 rightdown = !rightdown;
381 rightreleased = true;
384 mousepos += mousespeed;
387 s32 Rand(s32 min, s32 max);