]> git.lizzy.rs Git - minetest.git/blob - src/client/inputhandler.cpp
Modernize client code (#6250)
[minetest.git] / src / client / inputhandler.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "util/numeric.h"
22 #include "inputhandler.h"
23 #include "mainmenumanager.h"
24
25 bool MyEventReceiver::OnEvent(const SEvent &event)
26 {
27         /*
28                 React to nothing here if a menu is active
29         */
30         if (isMenuActive()) {
31 #ifdef HAVE_TOUCHSCREENGUI
32                 if (m_touchscreengui) {
33                         m_touchscreengui->Toggle(false);
34                 }
35 #endif
36                 return g_menumgr.preprocessEvent(event);
37         }
38
39         // Remember whether each key is down or up
40         if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
41                 const KeyPress &keyCode = event.KeyInput;
42                 if (keysListenedFor[keyCode]) {
43                         if (event.KeyInput.PressedDown) {
44                                 keyIsDown.set(keyCode);
45                                 keyWasDown.set(keyCode);
46                         } else {
47                                 keyIsDown.unset(keyCode);
48                         }
49                         return true;
50                 }
51         }
52
53 #ifdef HAVE_TOUCHSCREENGUI
54         // case of touchscreengui we have to handle different events
55         if (m_touchscreengui && event.EventType == irr::EET_TOUCH_INPUT_EVENT) {
56                 m_touchscreengui->translateEvent(event);
57                 return true;
58         }
59 #endif
60
61         if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
62                 /* TODO add a check like:
63                 if (event.JoystickEvent != joystick_we_listen_for)
64                         return false;
65                 */
66                 return joystick->handleEvent(event.JoystickEvent);
67         }
68         // handle mouse events
69         if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
70                 if (isMenuActive()) {
71                         left_active = false;
72                         middle_active = false;
73                         right_active = false;
74                 } else {
75                         left_active = event.MouseInput.isLeftPressed();
76                         middle_active = event.MouseInput.isMiddlePressed();
77                         right_active = event.MouseInput.isRightPressed();
78
79                         if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
80                                 leftclicked = true;
81                         }
82                         if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) {
83                                 rightclicked = true;
84                         }
85                         if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
86                                 leftreleased = true;
87                         }
88                         if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) {
89                                 rightreleased = true;
90                         }
91                         if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
92                                 mouse_wheel += event.MouseInput.Wheel;
93                         }
94                 }
95         } else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
96                 static const LogLevel irr_loglev_conv[] = {
97                                 LL_VERBOSE, // ELL_DEBUG
98                                 LL_INFO,    // ELL_INFORMATION
99                                 LL_WARNING, // ELL_WARNING
100                                 LL_ERROR,   // ELL_ERROR
101                                 LL_NONE,    // ELL_NONE
102                 };
103                 assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv));
104                 g_logger.log(irr_loglev_conv[event.LogEvent.Level],
105                                 std::string("Irrlicht: ") + event.LogEvent.Text);
106                 return true;
107         }
108         /* always return false in order to continue processing events */
109         return false;
110 }
111
112 /*
113  * RandomInputHandler
114  */
115 s32 RandomInputHandler::Rand(s32 min, s32 max)
116 {
117         return (myrand() % (max - min + 1)) + min;
118 }