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 if (event.KeyInput.PressedDown) {
46 keyIsDown.set(event.KeyInput);
47 keyWasDown.set(event.KeyInput);
49 keyIsDown.unset(event.KeyInput);
53 #ifdef HAVE_TOUCHSCREENGUI
54 // case of touchscreengui we have to handle different events
55 if ((m_touchscreengui != 0) &&
56 (event.EventType == irr::EET_TOUCH_INPUT_EVENT)) {
57 m_touchscreengui->translateEvent(event);
61 // handle mouse events
62 if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
63 if (noMenuActive() == false) {
65 middle_active = false;
68 left_active = event.MouseInput.isLeftPressed();
69 middle_active = event.MouseInput.isMiddlePressed();
70 right_active = event.MouseInput.isRightPressed();
72 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
75 if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
78 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
81 if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
84 if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
85 mouse_wheel += event.MouseInput.Wheel;
89 if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
90 dstream << std::string("Irrlicht log: ") + std::string(event.LogEvent.Text)
94 /* always return false in order to continue processing events */
98 bool IsKeyDown(const KeyPress &keyCode) const
100 return keyIsDown[keyCode];
103 // Checks whether a key was down and resets the state
104 bool WasKeyDown(const KeyPress &keyCode)
106 bool b = keyWasDown[keyCode];
108 keyWasDown.unset(keyCode);
125 rightclicked = false;
126 leftreleased = false;
127 rightreleased = false;
130 middle_active = false;
131 right_active = false;
139 #ifdef HAVE_TOUCHSCREENGUI
140 m_touchscreengui = NULL;
155 #ifdef HAVE_TOUCHSCREENGUI
156 TouchScreenGUI* m_touchscreengui;
160 // The current state of keys
162 // Whether a key has been pressed or not
168 Separated input handler
171 class RealInputHandler : public InputHandler
174 RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
176 m_receiver(receiver),
180 virtual bool isKeyDown(const KeyPress &keyCode)
182 return m_receiver->IsKeyDown(keyCode);
184 virtual bool wasKeyDown(const KeyPress &keyCode)
186 return m_receiver->WasKeyDown(keyCode);
188 virtual v2s32 getMousePos()
190 if (m_device->getCursorControl()) {
191 return m_device->getCursorControl()->getPosition();
197 virtual void setMousePos(s32 x, s32 y)
199 if (m_device->getCursorControl()) {
200 m_device->getCursorControl()->setPosition(x, y);
203 m_mousepos = v2s32(x,y);
207 virtual bool getLeftState()
209 return m_receiver->left_active;
211 virtual bool getRightState()
213 return m_receiver->right_active;
216 virtual bool getLeftClicked()
218 return m_receiver->leftclicked;
220 virtual bool getRightClicked()
222 return m_receiver->rightclicked;
224 virtual void resetLeftClicked()
226 m_receiver->leftclicked = false;
228 virtual void resetRightClicked()
230 m_receiver->rightclicked = false;
233 virtual bool getLeftReleased()
235 return m_receiver->leftreleased;
237 virtual bool getRightReleased()
239 return m_receiver->rightreleased;
241 virtual void resetLeftReleased()
243 m_receiver->leftreleased = false;
245 virtual void resetRightReleased()
247 m_receiver->rightreleased = false;
250 virtual s32 getMouseWheel()
252 return m_receiver->getMouseWheel();
257 m_receiver->clearInput();
260 IrrlichtDevice *m_device;
261 MyEventReceiver *m_receiver;
265 class RandomInputHandler : public InputHandler
273 rightclicked = false;
274 leftreleased = false;
275 rightreleased = false;
278 virtual bool isKeyDown(const KeyPress &keyCode)
280 return keydown[keyCode];
282 virtual bool wasKeyDown(const KeyPress &keyCode)
286 virtual v2s32 getMousePos()
290 virtual void setMousePos(s32 x, s32 y)
292 mousepos = v2s32(x, y);
295 virtual bool getLeftState()
299 virtual bool getRightState()
304 virtual bool getLeftClicked()
308 virtual bool getRightClicked()
312 virtual void resetLeftClicked()
316 virtual void resetRightClicked()
318 rightclicked = false;
321 virtual bool getLeftReleased()
325 virtual bool getRightReleased()
327 return rightreleased;
329 virtual void resetLeftReleased()
331 leftreleased = false;
333 virtual void resetRightReleased()
335 rightreleased = false;
338 virtual s32 getMouseWheel()
343 virtual void step(float dtime)
346 static float counter1 = 0;
348 if (counter1 < 0.0) {
349 counter1 = 0.1 * Rand(1, 40);
350 keydown.toggle(getKeySetting("keymap_jump"));
354 static float counter1 = 0;
356 if (counter1 < 0.0) {
357 counter1 = 0.1 * Rand(1, 40);
358 keydown.toggle(getKeySetting("keymap_special1"));
362 static float counter1 = 0;
364 if (counter1 < 0.0) {
365 counter1 = 0.1 * Rand(1, 40);
366 keydown.toggle(getKeySetting("keymap_forward"));
370 static float counter1 = 0;
372 if (counter1 < 0.0) {
373 counter1 = 0.1 * Rand(1, 40);
374 keydown.toggle(getKeySetting("keymap_left"));
378 static float counter1 = 0;
380 if (counter1 < 0.0) {
381 counter1 = 0.1 * Rand(1, 20);
382 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
386 static float counter1 = 0;
388 if (counter1 < 0.0) {
389 counter1 = 0.1 * Rand(1, 30);
390 leftdown = !leftdown;
398 static float counter1 = 0;
400 if (counter1 < 0.0) {
401 counter1 = 0.1 * Rand(1, 15);
402 rightdown = !rightdown;
406 rightreleased = true;
409 mousepos += mousespeed;
412 s32 Rand(s32 min, s32 max)
414 return (myrand()%(max-min+1))+min;