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 "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() { return m_receiver->left_active; }
263 virtual bool getRightState() { return m_receiver->right_active; }
265 virtual bool getLeftClicked() { return m_receiver->leftclicked; }
266 virtual bool getRightClicked() { return m_receiver->rightclicked; }
267 virtual void resetLeftClicked() { m_receiver->leftclicked = false; }
268 virtual void resetRightClicked() { m_receiver->rightclicked = false; }
270 virtual bool getLeftReleased() { return m_receiver->leftreleased; }
271 virtual bool getRightReleased() { return m_receiver->rightreleased; }
272 virtual void resetLeftReleased() { m_receiver->leftreleased = false; }
273 virtual void resetRightReleased() { m_receiver->rightreleased = false; }
275 virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
280 m_receiver->clearInput();
284 MyEventReceiver *m_receiver = nullptr;
288 class RandomInputHandler : public InputHandler
291 RandomInputHandler() = default;
293 virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
294 virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
295 virtual v2s32 getMousePos() { return mousepos; }
296 virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
298 virtual bool getLeftState() { return leftdown; }
299 virtual bool getRightState() { return rightdown; }
301 virtual bool getLeftClicked() { return leftclicked; }
302 virtual bool getRightClicked() { return rightclicked; }
303 virtual void resetLeftClicked() { leftclicked = false; }
304 virtual void resetRightClicked() { rightclicked = false; }
306 virtual bool getLeftReleased() { return leftreleased; }
307 virtual bool getRightReleased() { return rightreleased; }
308 virtual void resetLeftReleased() { leftreleased = false; }
309 virtual void resetRightReleased() { rightreleased = false; }
311 virtual s32 getMouseWheel() { return 0; }
313 virtual void step(float dtime)
316 static float counter1 = 0;
318 if (counter1 < 0.0) {
319 counter1 = 0.1 * Rand(1, 40);
320 keydown.toggle(getKeySetting("keymap_jump"));
324 static float counter1 = 0;
326 if (counter1 < 0.0) {
327 counter1 = 0.1 * Rand(1, 40);
328 keydown.toggle(getKeySetting("keymap_special1"));
332 static float counter1 = 0;
334 if (counter1 < 0.0) {
335 counter1 = 0.1 * Rand(1, 40);
336 keydown.toggle(getKeySetting("keymap_forward"));
340 static float counter1 = 0;
342 if (counter1 < 0.0) {
343 counter1 = 0.1 * Rand(1, 40);
344 keydown.toggle(getKeySetting("keymap_left"));
348 static float counter1 = 0;
350 if (counter1 < 0.0) {
351 counter1 = 0.1 * Rand(1, 20);
352 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
356 static float counter1 = 0;
358 if (counter1 < 0.0) {
359 counter1 = 0.1 * Rand(1, 30);
360 leftdown = !leftdown;
368 static float counter1 = 0;
370 if (counter1 < 0.0) {
371 counter1 = 0.1 * Rand(1, 15);
372 rightdown = !rightdown;
376 rightreleased = true;
379 mousepos += mousespeed;
382 s32 Rand(s32 min, s32 max);
388 bool leftdown = false;
389 bool rightdown = false;
390 bool leftclicked = false;
391 bool rightclicked = false;
392 bool leftreleased = false;
393 bool rightreleased = false;