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"
26 class MyEventReceiver : public IEventReceiver
29 // This is the one method that we have to implement
30 virtual bool OnEvent(const SEvent& event)
33 React to nothing here if a menu is active
35 if (noMenuActive() == false) {
36 #ifdef HAVE_TOUCHSCREENGUI
37 if (m_touchscreengui != 0) {
38 m_touchscreengui->Toggle(false);
41 return g_menumgr.preprocessEvent(event);
44 // Remember whether each key is down or up
45 if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
46 const KeyPress &keyCode = event.KeyInput;
47 if (keysListenedFor[keyCode]) {
48 if (event.KeyInput.PressedDown) {
49 keyIsDown.set(keyCode);
50 keyWasDown.set(keyCode);
52 keyIsDown.unset(keyCode);
58 #ifdef HAVE_TOUCHSCREENGUI
59 // case of touchscreengui we have to handle different events
60 if ((m_touchscreengui != 0) &&
61 (event.EventType == irr::EET_TOUCH_INPUT_EVENT)) {
62 m_touchscreengui->translateEvent(event);
67 if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
68 /* TODO add a check like:
69 if (event.JoystickEvent != joystick_we_listen_for)
72 return joystick->handleEvent(event.JoystickEvent);
74 // handle mouse events
75 if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
76 if (noMenuActive() == false) {
78 middle_active = false;
81 left_active = event.MouseInput.isLeftPressed();
82 middle_active = event.MouseInput.isMiddlePressed();
83 right_active = event.MouseInput.isRightPressed();
85 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
88 if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
91 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
94 if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
97 if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
98 mouse_wheel += event.MouseInput.Wheel;
101 } else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
102 static const LogLevel irr_loglev_conv[] = {
103 LL_VERBOSE, // ELL_DEBUG
104 LL_INFO, // ELL_INFORMATION
105 LL_WARNING, // ELL_WARNING
106 LL_ERROR, // ELL_ERROR
109 assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv));
110 g_logger.log(irr_loglev_conv[event.LogEvent.Level],
111 std::string("Irrlicht: ") + (const char*) event.LogEvent.Text);
114 /* always return false in order to continue processing events */
118 bool IsKeyDown(const KeyPress &keyCode) const
120 return keyIsDown[keyCode];
123 // Checks whether a key was down and resets the state
124 bool WasKeyDown(const KeyPress &keyCode)
126 bool b = keyWasDown[keyCode];
128 keyWasDown.unset(keyCode);
132 void listenForKey(const KeyPress &keyCode)
134 keysListenedFor.set(keyCode);
136 void dontListenForKeys()
138 keysListenedFor.clear();
154 rightclicked = false;
155 leftreleased = false;
156 rightreleased = false;
159 middle_active = false;
160 right_active = false;
168 #ifdef HAVE_TOUCHSCREENGUI
169 m_touchscreengui = NULL;
184 JoystickController *joystick;
186 #ifdef HAVE_TOUCHSCREENGUI
187 TouchScreenGUI* m_touchscreengui;
191 // The current state of keys
193 // Whether a key has been pressed or not
195 // List of keys we listen for
196 // TODO perhaps the type of this is not really
197 // performant as KeyList is designed for few but
198 // often changing keys, and keysListenedFor is expected
199 // to change seldomly but contain lots of keys.
200 KeyList keysListenedFor;
205 Separated input handler
208 class RealInputHandler : public InputHandler
211 RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
213 m_receiver(receiver),
216 m_receiver->joystick = &joystick;
218 virtual bool isKeyDown(const KeyPress &keyCode)
220 return m_receiver->IsKeyDown(keyCode);
222 virtual bool wasKeyDown(const KeyPress &keyCode)
224 return m_receiver->WasKeyDown(keyCode);
226 virtual void listenForKey(const KeyPress &keyCode)
228 m_receiver->listenForKey(keyCode);
230 virtual void dontListenForKeys()
232 m_receiver->dontListenForKeys();
234 virtual v2s32 getMousePos()
236 if (m_device->getCursorControl()) {
237 return m_device->getCursorControl()->getPosition();
243 virtual void setMousePos(s32 x, s32 y)
245 if (m_device->getCursorControl()) {
246 m_device->getCursorControl()->setPosition(x, y);
249 m_mousepos = v2s32(x,y);
253 virtual bool getLeftState()
255 return m_receiver->left_active;
257 virtual bool getRightState()
259 return m_receiver->right_active;
262 virtual bool getLeftClicked()
264 return m_receiver->leftclicked;
266 virtual bool getRightClicked()
268 return m_receiver->rightclicked;
270 virtual void resetLeftClicked()
272 m_receiver->leftclicked = false;
274 virtual void resetRightClicked()
276 m_receiver->rightclicked = false;
279 virtual bool getLeftReleased()
281 return m_receiver->leftreleased;
283 virtual bool getRightReleased()
285 return m_receiver->rightreleased;
287 virtual void resetLeftReleased()
289 m_receiver->leftreleased = false;
291 virtual void resetRightReleased()
293 m_receiver->rightreleased = false;
296 virtual s32 getMouseWheel()
298 return m_receiver->getMouseWheel();
304 m_receiver->clearInput();
307 IrrlichtDevice *m_device;
308 MyEventReceiver *m_receiver;
312 class RandomInputHandler : public InputHandler
320 rightclicked = false;
321 leftreleased = false;
322 rightreleased = false;
325 virtual bool isKeyDown(const KeyPress &keyCode)
327 return keydown[keyCode];
329 virtual bool wasKeyDown(const KeyPress &keyCode)
333 virtual v2s32 getMousePos()
337 virtual void setMousePos(s32 x, s32 y)
339 mousepos = v2s32(x, y);
342 virtual bool getLeftState()
346 virtual bool getRightState()
351 virtual bool getLeftClicked()
355 virtual bool getRightClicked()
359 virtual void resetLeftClicked()
363 virtual void resetRightClicked()
365 rightclicked = false;
368 virtual bool getLeftReleased()
372 virtual bool getRightReleased()
374 return rightreleased;
376 virtual void resetLeftReleased()
378 leftreleased = false;
380 virtual void resetRightReleased()
382 rightreleased = false;
385 virtual s32 getMouseWheel()
390 virtual void step(float dtime)
393 static float counter1 = 0;
395 if (counter1 < 0.0) {
396 counter1 = 0.1 * Rand(1, 40);
397 keydown.toggle(getKeySetting("keymap_jump"));
401 static float counter1 = 0;
403 if (counter1 < 0.0) {
404 counter1 = 0.1 * Rand(1, 40);
405 keydown.toggle(getKeySetting("keymap_special1"));
409 static float counter1 = 0;
411 if (counter1 < 0.0) {
412 counter1 = 0.1 * Rand(1, 40);
413 keydown.toggle(getKeySetting("keymap_forward"));
417 static float counter1 = 0;
419 if (counter1 < 0.0) {
420 counter1 = 0.1 * Rand(1, 40);
421 keydown.toggle(getKeySetting("keymap_left"));
425 static float counter1 = 0;
427 if (counter1 < 0.0) {
428 counter1 = 0.1 * Rand(1, 20);
429 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
433 static float counter1 = 0;
435 if (counter1 < 0.0) {
436 counter1 = 0.1 * Rand(1, 30);
437 leftdown = !leftdown;
445 static float counter1 = 0;
447 if (counter1 < 0.0) {
448 counter1 = 0.1 * Rand(1, 15);
449 rightdown = !rightdown;
453 rightreleased = true;
456 mousepos += mousespeed;
459 s32 Rand(s32 min, s32 max)
461 return (myrand()%(max-min+1))+min;