]> git.lizzy.rs Git - minetest.git/blob - src/gui/touchscreengui.h
LINT fixes since recent tooling update
[minetest.git] / src / gui / touchscreengui.h
1 /*
2 Copyright (C) 2014 sapier
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #pragma once
20
21 #include <IEventReceiver.h>
22 #include <IGUIButton.h>
23 #include <IGUIEnvironment.h>
24 #include <IrrlichtDevice.h>
25
26 #include <map>
27 #include <vector>
28
29 #include "client/tile.h"
30 #include "client/game.h"
31
32 using namespace irr;
33 using namespace irr::core;
34 using namespace irr::gui;
35
36 typedef enum
37 {
38         jump_id = 0,
39         crunch_id,
40         zoom_id,
41         special1_id,
42         after_last_element_id,
43         settings_starter_id,
44         rare_controls_starter_id,
45         fly_id,
46         noclip_id,
47         fast_id,
48         debug_id,
49         camera_id,
50         range_id,
51         minimap_id,
52         toggle_chat_id,
53         chat_id,
54         inventory_id,
55         drop_id,
56         forward_id,
57         backward_id,
58         left_id,
59         right_id,
60         joystick_off_id,
61         joystick_bg_id,
62         joystick_center_id
63 } touch_gui_button_id;
64
65 typedef enum
66 {
67         j_forward = 0,
68         j_backward,
69         j_left,
70         j_right,
71         j_special1
72 } touch_gui_joystick_move_id;
73
74 typedef enum
75 {
76         AHBB_Dir_Top_Bottom,
77         AHBB_Dir_Bottom_Top,
78         AHBB_Dir_Left_Right,
79         AHBB_Dir_Right_Left
80 } autohide_button_bar_dir;
81
82 #define MIN_DIG_TIME_MS 500
83 #define MAX_TOUCH_COUNT 64
84 #define BUTTON_REPEAT_DELAY 0.2f
85
86 #define SETTINGS_BAR_Y_OFFSET 5
87 #define RARE_CONTROLS_BAR_Y_OFFSET 5
88
89 extern const char **touchgui_button_imagenames;
90 extern const char **touchgui_joystick_imagenames;
91
92 struct button_info
93 {
94         float repeatcounter;
95         float repeatdelay;
96         irr::EKEY_CODE keycode;
97         std::vector<int> ids;
98         IGUIButton *guibutton = nullptr;
99         bool immediate_release;
100
101         // 0: false, 1: (true) first texture, 2: (true) second texture
102         int togglable = 0;
103         std::vector<const char *> textures;
104 };
105
106 class AutoHideButtonBar
107 {
108 public:
109         AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
110
111         void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
112                         v2s32 UpperLeft, v2s32 LowerRight, autohide_button_bar_dir dir,
113                         float timeout);
114
115         ~AutoHideButtonBar();
116
117         // add button to be shown
118         void addButton(touch_gui_button_id id, const wchar_t *caption,
119                         const char *btn_image);
120
121         // add toggle button to be shown
122         void addToggleButton(touch_gui_button_id id, const wchar_t *caption,
123                         const char *btn_image_1, const char *btn_image_2);
124
125         // detect settings bar button events
126         bool isButton(const SEvent &event);
127
128         // handle released hud buttons
129         bool isReleaseButton(int eventID);
130
131         // step handler
132         void step(float dtime);
133
134         // deactivate button bar
135         void deactivate();
136
137         // hide the whole buttonbar
138         void hide();
139
140         // unhide the buttonbar
141         void show();
142
143 private:
144         ISimpleTextureSource *m_texturesource = nullptr;
145         irr::video::IVideoDriver *m_driver;
146         IGUIEnvironment *m_guienv;
147         IEventReceiver *m_receiver;
148         button_info m_starter;
149         std::vector<button_info *> m_buttons;
150
151         v2s32 m_upper_left;
152         v2s32 m_lower_right;
153
154         // show settings bar
155         bool m_active = false;
156
157         bool m_visible = true;
158
159         // settings bar timeout
160         float m_timeout = 0.0f;
161         float m_timeout_value = 3.0f;
162         bool m_initialized = false;
163         autohide_button_bar_dir m_dir = AHBB_Dir_Right_Left;
164 };
165
166 class TouchScreenGUI
167 {
168 public:
169         TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
170         ~TouchScreenGUI();
171
172         void translateEvent(const SEvent &event);
173
174         void init(ISimpleTextureSource *tsrc);
175
176         double getYawChange()
177         {
178                 double res = m_camera_yaw_change;
179                 m_camera_yaw_change = 0;
180                 return res;
181         }
182
183         double getPitch() { return m_camera_pitch; }
184
185         /*!
186          * Returns a line which describes what the player is pointing at.
187          * The starting point and looking direction are significant,
188          * the line should be scaled to match its length to the actual distance
189          * the player can reach.
190          * The line starts at the camera and ends on the camera's far plane.
191          * The coordinates do not contain the camera offset.
192          */
193         line3d<f32> getShootline() { return m_shootline; }
194
195         void step(float dtime);
196         void resetHud();
197         void registerHudItem(int index, const rect<s32> &rect);
198         void Toggle(bool visible);
199
200         void hide();
201         void show();
202
203 private:
204         IrrlichtDevice *m_device;
205         IGUIEnvironment *m_guienv;
206         IEventReceiver *m_receiver;
207         ISimpleTextureSource *m_texturesource;
208         v2u32 m_screensize;
209         double m_touchscreen_threshold;
210         std::map<int, rect<s32>> m_hud_rects;
211         std::map<int, irr::EKEY_CODE> m_hud_ids;
212         bool m_visible; // is the gui visible
213
214         // value in degree
215         double m_camera_yaw_change = 0.0;
216         double m_camera_pitch = 0.0;
217
218         // forward, backward, left, right
219         touch_gui_button_id m_joystick_names[5] = {
220                         forward_id, backward_id, left_id, right_id, special1_id};
221         bool m_joystick_status[5] = {false, false, false, false, false};
222
223         /*!
224          * A line starting at the camera and pointing towards the
225          * selected object.
226          * The line ends on the camera's far plane.
227          * The coordinates do not contain the camera offset.
228          */
229         line3d<f32> m_shootline;
230
231         int m_move_id = -1;
232         bool m_move_has_really_moved = false;
233         s64 m_move_downtime = 0;
234         bool m_move_sent_as_mouse_event = false;
235         v2s32 m_move_downlocation = v2s32(-10000, -10000);
236
237         int m_joystick_id = -1;
238         bool m_joystick_has_really_moved = false;
239         bool m_fixed_joystick = false;
240         bool m_joystick_triggers_special1 = false;
241         button_info *m_joystick_btn_off = nullptr;
242         button_info *m_joystick_btn_bg = nullptr;
243         button_info *m_joystick_btn_center = nullptr;
244
245         button_info m_buttons[after_last_element_id];
246
247         // gui button detection
248         touch_gui_button_id getButtonID(s32 x, s32 y);
249
250         // gui button by eventID
251         touch_gui_button_id getButtonID(int eventID);
252
253         // check if a button has changed
254         void handleChangedButton(const SEvent &event);
255
256         // initialize a button
257         void initButton(touch_gui_button_id id, rect<s32> button_rect,
258                         std::wstring caption, bool immediate_release,
259                         float repeat_delay = BUTTON_REPEAT_DELAY);
260
261         // initialize a joystick button
262         button_info *initJoystickButton(touch_gui_button_id id, rect<s32> button_rect,
263                         int texture_id, bool visible = true);
264
265         struct id_status
266         {
267                 int id;
268                 int X;
269                 int Y;
270         };
271
272         // vector to store known ids and their initial touch positions
273         std::vector<id_status> m_known_ids;
274
275         // handle a button event
276         void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
277
278         // handle pressed hud buttons
279         bool isHUDButton(const SEvent &event);
280
281         // handle released hud buttons
282         bool isReleaseHUDButton(int eventID);
283
284         // handle double taps
285         bool doubleTapDetection();
286
287         // handle release event
288         void handleReleaseEvent(int evt_id);
289
290         // apply joystick status
291         void applyJoystickStatus();
292
293         // get size of regular gui control button
294         int getGuiButtonSize();
295
296         // doubleclick detection variables
297         struct key_event
298         {
299                 unsigned int down_time;
300                 s32 x;
301                 s32 y;
302         };
303
304         // array for saving last known position of a pointer
305         v2s32 m_pointerpos[MAX_TOUCH_COUNT];
306
307         // array for doubletap detection
308         key_event m_key_events[2];
309
310         // settings bar
311         AutoHideButtonBar m_settingsbar;
312
313         // rare controls bar
314         AutoHideButtonBar m_rarecontrolsbar;
315 };
316 extern TouchScreenGUI *g_touchscreengui;