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"
25 class MyEventReceiver : public IEventReceiver
28 // This is the one method that we have to implement
29 virtual bool OnEvent(const SEvent& event)
32 React to nothing here if a menu is active
34 if (noMenuActive() == false) {
35 #ifdef HAVE_TOUCHSCREENGUI
36 if (m_touchscreengui != 0) {
37 m_touchscreengui->Toggle(false);
40 return g_menumgr.preprocessEvent(event);
43 // Remember whether each key is down or up
44 if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
45 const KeyPress &keyCode = event.KeyInput;
46 if (keysListenedFor[keyCode]) {
47 if (event.KeyInput.PressedDown) {
48 keyIsDown.set(keyCode);
49 keyWasDown.set(keyCode);
51 keyIsDown.unset(keyCode);
57 #ifdef HAVE_TOUCHSCREENGUI
58 // case of touchscreengui we have to handle different events
59 if ((m_touchscreengui != 0) &&
60 (event.EventType == irr::EET_TOUCH_INPUT_EVENT)) {
61 m_touchscreengui->translateEvent(event);
65 // handle mouse events
66 if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
67 if (noMenuActive() == false) {
69 middle_active = false;
72 left_active = event.MouseInput.isLeftPressed();
73 middle_active = event.MouseInput.isMiddlePressed();
74 right_active = event.MouseInput.isRightPressed();
76 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
79 if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
82 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
85 if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
88 if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
89 mouse_wheel += event.MouseInput.Wheel;
92 } else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
93 static const LogLevel irr_loglev_conv[] = {
94 LL_VERBOSE, // ELL_DEBUG
95 LL_INFO, // ELL_INFORMATION
96 LL_WARNING, // ELL_WARNING
97 LL_ERROR, // ELL_ERROR
100 assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv));
101 g_logger.log(irr_loglev_conv[event.LogEvent.Level],
102 std::string("Irrlicht: ") + (const char*) event.LogEvent.Text);
105 /* always return false in order to continue processing events */
109 bool IsKeyDown(const KeyPress &keyCode) const
111 return keyIsDown[keyCode];
114 // Checks whether a key was down and resets the state
115 bool WasKeyDown(const KeyPress &keyCode)
117 bool b = keyWasDown[keyCode];
119 keyWasDown.unset(keyCode);
123 void listenForKey(const KeyPress &keyCode)
125 keysListenedFor.set(keyCode);
127 void dontListenForKeys()
129 keysListenedFor.clear();
145 rightclicked = false;
146 leftreleased = false;
147 rightreleased = false;
150 middle_active = false;
151 right_active = false;
159 #ifdef HAVE_TOUCHSCREENGUI
160 m_touchscreengui = NULL;
175 #ifdef HAVE_TOUCHSCREENGUI
176 TouchScreenGUI* m_touchscreengui;
180 // The current state of keys
182 // Whether a key has been pressed or not
184 // List of keys we listen for
185 // TODO perhaps the type of this is not really
186 // performant as KeyList is designed for few but
187 // often changing keys, and keysListenedFor is expected
188 // to change seldomly but contain lots of keys.
189 KeyList keysListenedFor;
194 Separated input handler
197 class RealInputHandler : public InputHandler
200 RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
202 m_receiver(receiver),
206 virtual bool isKeyDown(const KeyPress &keyCode)
208 return m_receiver->IsKeyDown(keyCode);
210 virtual bool wasKeyDown(const KeyPress &keyCode)
212 return m_receiver->WasKeyDown(keyCode);
214 virtual void listenForKey(const KeyPress &keyCode)
216 m_receiver->listenForKey(keyCode);
218 virtual void dontListenForKeys()
220 m_receiver->dontListenForKeys();
222 virtual v2s32 getMousePos()
224 if (m_device->getCursorControl()) {
225 return m_device->getCursorControl()->getPosition();
231 virtual void setMousePos(s32 x, s32 y)
233 if (m_device->getCursorControl()) {
234 m_device->getCursorControl()->setPosition(x, y);
237 m_mousepos = v2s32(x,y);
241 virtual bool getLeftState()
243 return m_receiver->left_active;
245 virtual bool getRightState()
247 return m_receiver->right_active;
250 virtual bool getLeftClicked()
252 return m_receiver->leftclicked;
254 virtual bool getRightClicked()
256 return m_receiver->rightclicked;
258 virtual void resetLeftClicked()
260 m_receiver->leftclicked = false;
262 virtual void resetRightClicked()
264 m_receiver->rightclicked = false;
267 virtual bool getLeftReleased()
269 return m_receiver->leftreleased;
271 virtual bool getRightReleased()
273 return m_receiver->rightreleased;
275 virtual void resetLeftReleased()
277 m_receiver->leftreleased = false;
279 virtual void resetRightReleased()
281 m_receiver->rightreleased = false;
284 virtual s32 getMouseWheel()
286 return m_receiver->getMouseWheel();
291 m_receiver->clearInput();
294 IrrlichtDevice *m_device;
295 MyEventReceiver *m_receiver;
299 class RandomInputHandler : public InputHandler
307 rightclicked = false;
308 leftreleased = false;
309 rightreleased = false;
312 virtual bool isKeyDown(const KeyPress &keyCode)
314 return keydown[keyCode];
316 virtual bool wasKeyDown(const KeyPress &keyCode)
320 virtual v2s32 getMousePos()
324 virtual void setMousePos(s32 x, s32 y)
326 mousepos = v2s32(x, y);
329 virtual bool getLeftState()
333 virtual bool getRightState()
338 virtual bool getLeftClicked()
342 virtual bool getRightClicked()
346 virtual void resetLeftClicked()
350 virtual void resetRightClicked()
352 rightclicked = false;
355 virtual bool getLeftReleased()
359 virtual bool getRightReleased()
361 return rightreleased;
363 virtual void resetLeftReleased()
365 leftreleased = false;
367 virtual void resetRightReleased()
369 rightreleased = false;
372 virtual s32 getMouseWheel()
377 virtual void step(float dtime)
380 static float counter1 = 0;
382 if (counter1 < 0.0) {
383 counter1 = 0.1 * Rand(1, 40);
384 keydown.toggle(getKeySetting("keymap_jump"));
388 static float counter1 = 0;
390 if (counter1 < 0.0) {
391 counter1 = 0.1 * Rand(1, 40);
392 keydown.toggle(getKeySetting("keymap_special1"));
396 static float counter1 = 0;
398 if (counter1 < 0.0) {
399 counter1 = 0.1 * Rand(1, 40);
400 keydown.toggle(getKeySetting("keymap_forward"));
404 static float counter1 = 0;
406 if (counter1 < 0.0) {
407 counter1 = 0.1 * Rand(1, 40);
408 keydown.toggle(getKeySetting("keymap_left"));
412 static float counter1 = 0;
414 if (counter1 < 0.0) {
415 counter1 = 0.1 * Rand(1, 20);
416 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
420 static float counter1 = 0;
422 if (counter1 < 0.0) {
423 counter1 = 0.1 * Rand(1, 30);
424 leftdown = !leftdown;
432 static float counter1 = 0;
434 if (counter1 < 0.0) {
435 counter1 = 0.1 * Rand(1, 15);
436 rightdown = !rightdown;
440 rightreleased = true;
443 mousepos += mousespeed;
446 s32 Rand(s32 min, s32 max)
448 return (myrand()%(max-min+1))+min;