]> git.lizzy.rs Git - minetest.git/blob - src/client/inputhandler.h
Use minetest logging facilities for irrlicht log output
[minetest.git] / src / client / inputhandler.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef __INPUT_HANDLER_H__
21 #define __INPUT_HANDLER_H__
22
23 #include "irrlichttypes_extrabloated.h"
24
25 class MyEventReceiver : public IEventReceiver
26 {
27 public:
28         // This is the one method that we have to implement
29         virtual bool OnEvent(const SEvent& event)
30         {
31                 /*
32                         React to nothing here if a menu is active
33                 */
34                 if (noMenuActive() == false) {
35 #ifdef HAVE_TOUCHSCREENGUI
36                         if (m_touchscreengui != 0) {
37                                 m_touchscreengui->Toggle(false);
38                         }
39 #endif
40                         return g_menumgr.preprocessEvent(event);
41                 }
42
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);
48                         } else {
49                                 keyIsDown.unset(event.KeyInput);
50                         }
51                 }
52
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);
58                         return true;
59                 }
60 #endif
61                 // handle mouse events
62                 if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
63                         if (noMenuActive() == false) {
64                                 left_active = false;
65                                 middle_active = false;
66                                 right_active = false;
67                         } else {
68                                 left_active = event.MouseInput.isLeftPressed();
69                                 middle_active = event.MouseInput.isMiddlePressed();
70                                 right_active = event.MouseInput.isRightPressed();
71
72                                 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
73                                         leftclicked = true;
74                                 }
75                                 if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
76                                         rightclicked = true;
77                                 }
78                                 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
79                                         leftreleased = true;
80                                 }
81                                 if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
82                                         rightreleased = true;
83                                 }
84                                 if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
85                                         mouse_wheel += event.MouseInput.Wheel;
86                                 }
87                         }
88                 } else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
89                         static const enum LogMessageLevel irr_loglev_conv[] = {
90                                 LMT_VERBOSE, // ELL_DEBUG
91                                 LMT_INFO, // ELL_INFORMATION
92                                 LMT_ACTION, // ELL_WARNING
93                                 LMT_ERROR, // ELL_ERROR
94                                 LMT_ERROR, // ELL_NONE
95                         };
96                         assert(event.LogEvent.Level < sizeof(irr_loglev_conv));
97                         log_printline(irr_loglev_conv[event.LogEvent.Level],
98                                 std::string("Irrlicht: ")
99                                 + (const char*) event.LogEvent.Text);
100                         return true;
101                 }
102                 /* always return false in order to continue processing events */
103                 return false;
104         }
105
106         bool IsKeyDown(const KeyPress &keyCode) const
107         {
108                 return keyIsDown[keyCode];
109         }
110
111         // Checks whether a key was down and resets the state
112         bool WasKeyDown(const KeyPress &keyCode)
113         {
114                 bool b = keyWasDown[keyCode];
115                 if (b)
116                         keyWasDown.unset(keyCode);
117                 return b;
118         }
119
120         s32 getMouseWheel()
121         {
122                 s32 a = mouse_wheel;
123                 mouse_wheel = 0;
124                 return a;
125         }
126
127         void clearInput()
128         {
129                 keyIsDown.clear();
130                 keyWasDown.clear();
131
132                 leftclicked = false;
133                 rightclicked = false;
134                 leftreleased = false;
135                 rightreleased = false;
136
137                 left_active = false;
138                 middle_active = false;
139                 right_active = false;
140
141                 mouse_wheel = 0;
142         }
143
144         MyEventReceiver()
145         {
146                 clearInput();
147 #ifdef HAVE_TOUCHSCREENGUI
148                 m_touchscreengui = NULL;
149 #endif
150         }
151
152         bool leftclicked;
153         bool rightclicked;
154         bool leftreleased;
155         bool rightreleased;
156
157         bool left_active;
158         bool middle_active;
159         bool right_active;
160
161         s32 mouse_wheel;
162
163 #ifdef HAVE_TOUCHSCREENGUI
164         TouchScreenGUI* m_touchscreengui;
165 #endif
166
167 private:
168         // The current state of keys
169         KeyList keyIsDown;
170         // Whether a key has been pressed or not
171         KeyList keyWasDown;
172 };
173
174
175 /*
176         Separated input handler
177 */
178
179 class RealInputHandler : public InputHandler
180 {
181 public:
182         RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
183                 m_device(device),
184                 m_receiver(receiver),
185                 m_mousepos(0,0)
186         {
187         }
188         virtual bool isKeyDown(const KeyPress &keyCode)
189         {
190                 return m_receiver->IsKeyDown(keyCode);
191         }
192         virtual bool wasKeyDown(const KeyPress &keyCode)
193         {
194                 return m_receiver->WasKeyDown(keyCode);
195         }
196         virtual v2s32 getMousePos()
197         {
198                 if (m_device->getCursorControl()) {
199                         return m_device->getCursorControl()->getPosition();
200                 }
201                 else {
202                         return m_mousepos;
203                 }
204         }
205         virtual void setMousePos(s32 x, s32 y)
206         {
207                 if (m_device->getCursorControl()) {
208                         m_device->getCursorControl()->setPosition(x, y);
209                 }
210                 else {
211                         m_mousepos = v2s32(x,y);
212                 }
213         }
214
215         virtual bool getLeftState()
216         {
217                 return m_receiver->left_active;
218         }
219         virtual bool getRightState()
220         {
221                 return m_receiver->right_active;
222         }
223
224         virtual bool getLeftClicked()
225         {
226                 return m_receiver->leftclicked;
227         }
228         virtual bool getRightClicked()
229         {
230                 return m_receiver->rightclicked;
231         }
232         virtual void resetLeftClicked()
233         {
234                 m_receiver->leftclicked = false;
235         }
236         virtual void resetRightClicked()
237         {
238                 m_receiver->rightclicked = false;
239         }
240
241         virtual bool getLeftReleased()
242         {
243                 return m_receiver->leftreleased;
244         }
245         virtual bool getRightReleased()
246         {
247                 return m_receiver->rightreleased;
248         }
249         virtual void resetLeftReleased()
250         {
251                 m_receiver->leftreleased = false;
252         }
253         virtual void resetRightReleased()
254         {
255                 m_receiver->rightreleased = false;
256         }
257
258         virtual s32 getMouseWheel()
259         {
260                 return m_receiver->getMouseWheel();
261         }
262
263         void clear()
264         {
265                 m_receiver->clearInput();
266         }
267 private:
268         IrrlichtDevice  *m_device;
269         MyEventReceiver *m_receiver;
270         v2s32           m_mousepos;
271 };
272
273 class RandomInputHandler : public InputHandler
274 {
275 public:
276         RandomInputHandler()
277         {
278                 leftdown = false;
279                 rightdown = false;
280                 leftclicked = false;
281                 rightclicked = false;
282                 leftreleased = false;
283                 rightreleased = false;
284                 keydown.clear();
285         }
286         virtual bool isKeyDown(const KeyPress &keyCode)
287         {
288                 return keydown[keyCode];
289         }
290         virtual bool wasKeyDown(const KeyPress &keyCode)
291         {
292                 return false;
293         }
294         virtual v2s32 getMousePos()
295         {
296                 return mousepos;
297         }
298         virtual void setMousePos(s32 x, s32 y)
299         {
300                 mousepos = v2s32(x, y);
301         }
302
303         virtual bool getLeftState()
304         {
305                 return leftdown;
306         }
307         virtual bool getRightState()
308         {
309                 return rightdown;
310         }
311
312         virtual bool getLeftClicked()
313         {
314                 return leftclicked;
315         }
316         virtual bool getRightClicked()
317         {
318                 return rightclicked;
319         }
320         virtual void resetLeftClicked()
321         {
322                 leftclicked = false;
323         }
324         virtual void resetRightClicked()
325         {
326                 rightclicked = false;
327         }
328
329         virtual bool getLeftReleased()
330         {
331                 return leftreleased;
332         }
333         virtual bool getRightReleased()
334         {
335                 return rightreleased;
336         }
337         virtual void resetLeftReleased()
338         {
339                 leftreleased = false;
340         }
341         virtual void resetRightReleased()
342         {
343                 rightreleased = false;
344         }
345
346         virtual s32 getMouseWheel()
347         {
348                 return 0;
349         }
350
351         virtual void step(float dtime)
352         {
353                 {
354                         static float counter1 = 0;
355                         counter1 -= dtime;
356                         if (counter1 < 0.0) {
357                                 counter1 = 0.1 * Rand(1, 40);
358                                 keydown.toggle(getKeySetting("keymap_jump"));
359                         }
360                 }
361                 {
362                         static float counter1 = 0;
363                         counter1 -= dtime;
364                         if (counter1 < 0.0) {
365                                 counter1 = 0.1 * Rand(1, 40);
366                                 keydown.toggle(getKeySetting("keymap_special1"));
367                         }
368                 }
369                 {
370                         static float counter1 = 0;
371                         counter1 -= dtime;
372                         if (counter1 < 0.0) {
373                                 counter1 = 0.1 * Rand(1, 40);
374                                 keydown.toggle(getKeySetting("keymap_forward"));
375                         }
376                 }
377                 {
378                         static float counter1 = 0;
379                         counter1 -= dtime;
380                         if (counter1 < 0.0) {
381                                 counter1 = 0.1 * Rand(1, 40);
382                                 keydown.toggle(getKeySetting("keymap_left"));
383                         }
384                 }
385                 {
386                         static float counter1 = 0;
387                         counter1 -= dtime;
388                         if (counter1 < 0.0) {
389                                 counter1 = 0.1 * Rand(1, 20);
390                                 mousespeed = v2s32(Rand(-20, 20), Rand(-15, 20));
391                         }
392                 }
393                 {
394                         static float counter1 = 0;
395                         counter1 -= dtime;
396                         if (counter1 < 0.0) {
397                                 counter1 = 0.1 * Rand(1, 30);
398                                 leftdown = !leftdown;
399                                 if (leftdown)
400                                         leftclicked = true;
401                                 if (!leftdown)
402                                         leftreleased = true;
403                         }
404                 }
405                 {
406                         static float counter1 = 0;
407                         counter1 -= dtime;
408                         if (counter1 < 0.0) {
409                                 counter1 = 0.1 * Rand(1, 15);
410                                 rightdown = !rightdown;
411                                 if (rightdown)
412                                         rightclicked = true;
413                                 if (!rightdown)
414                                         rightreleased = true;
415                         }
416                 }
417                 mousepos += mousespeed;
418         }
419
420         s32 Rand(s32 min, s32 max)
421         {
422                 return (myrand()%(max-min+1))+min;
423         }
424 private:
425         KeyList keydown;
426         v2s32 mousepos;
427         v2s32 mousespeed;
428         bool leftdown;
429         bool rightdown;
430         bool leftclicked;
431         bool rightclicked;
432         bool leftreleased;
433         bool rightreleased;
434 };
435
436 #endif