Translated using Weblate (Lithuanian)
[oweals/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 BUTTON_REPEAT_DELAY 0.2f
84 #define SETTINGS_BAR_Y_OFFSET 5
85 #define RARE_CONTROLS_BAR_Y_OFFSET 5
86
87 // Very slow button repeat frequency
88 #define SLOW_BUTTON_REPEAT 1.0f
89
90 extern const char **button_imagenames;
91 extern const char **joystick_imagenames;
92
93 struct button_info
94 {
95         float repeatcounter;
96         float repeatdelay;
97         irr::EKEY_CODE keycode;
98         std::vector<size_t> ids;
99         IGUIButton *guibutton = nullptr;
100         bool immediate_release;
101
102         // 0: false, 1: (true) first texture, 2: (true) second texture
103         int togglable = 0;
104         std::vector<const char *> textures;
105 };
106
107 class AutoHideButtonBar
108 {
109 public:
110         AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
111
112         void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
113                         const v2s32 &UpperLeft, const v2s32 &LowerRight,
114                         autohide_button_bar_dir dir, float timeout);
115
116         ~AutoHideButtonBar();
117
118         // add button to be shown
119         void addButton(touch_gui_button_id id, const wchar_t *caption,
120                         const char *btn_image);
121
122         // add toggle button to be shown
123         void addToggleButton(touch_gui_button_id id, const wchar_t *caption,
124                         const char *btn_image_1, const char *btn_image_2);
125
126         // detect settings bar button events
127         bool isButton(const SEvent &event);
128
129         // step handler
130         void step(float dtime);
131
132         // deactivate button bar
133         void deactivate();
134
135         // hide the whole buttonbar
136         void hide();
137
138         // unhide the buttonbar
139         void show();
140
141 private:
142         ISimpleTextureSource *m_texturesource = nullptr;
143         irr::video::IVideoDriver *m_driver;
144         IGUIEnvironment *m_guienv;
145         IEventReceiver *m_receiver;
146         button_info m_starter;
147         std::vector<button_info *> m_buttons;
148
149         v2s32 m_upper_left;
150         v2s32 m_lower_right;
151
152         // show settings bar
153         bool m_active = false;
154
155         bool m_visible = true;
156
157         // settings bar timeout
158         float m_timeout = 0.0f;
159         float m_timeout_value = 3.0f;
160         bool m_initialized = false;
161         autohide_button_bar_dir m_dir = AHBB_Dir_Right_Left;
162 };
163
164 class TouchScreenGUI
165 {
166 public:
167         TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
168         ~TouchScreenGUI();
169
170         void translateEvent(const SEvent &event);
171
172         void init(ISimpleTextureSource *tsrc);
173
174         double getYawChange()
175         {
176                 double res = m_camera_yaw_change;
177                 m_camera_yaw_change = 0;
178                 return res;
179         }
180
181         double getPitch() { return m_camera_pitch; }
182
183         /*
184          * Returns a line which describes what the player is pointing at.
185          * The starting point and looking direction are significant,
186          * the line should be scaled to match its length to the actual distance
187          * the player can reach.
188          * The line starts at the camera and ends on the camera's far plane.
189          * The coordinates do not contain the camera offset.
190          */
191         line3d<f32> getShootline() { return m_shootline; }
192
193         void step(float dtime);
194         void resetHud();
195         void registerHudItem(int index, const rect<s32> &rect);
196         void Toggle(bool visible);
197
198         void hide();
199         void show();
200
201 private:
202         IrrlichtDevice *m_device;
203         IGUIEnvironment *m_guienv;
204         IEventReceiver *m_receiver;
205         ISimpleTextureSource *m_texturesource;
206         v2u32 m_screensize;
207         s32 button_size;
208         double m_touchscreen_threshold;
209         std::map<int, rect<s32>> m_hud_rects;
210         std::map<size_t, irr::EKEY_CODE> m_hud_ids;
211         bool m_visible; // is the gui visible
212
213         // value in degree
214         double m_camera_yaw_change = 0.0;
215         double m_camera_pitch = 0.0;
216
217         // forward, backward, left, right
218         touch_gui_button_id m_joystick_names[5] = {
219                         forward_id, backward_id, left_id, right_id, special1_id};
220         bool m_joystick_status[5] = {false, false, false, false, false};
221
222         /*
223          * A line starting at the camera and pointing towards the
224          * selected object.
225          * The line ends on the camera's far plane.
226          * The coordinates do not contain the camera offset.
227          */
228         line3d<f32> m_shootline;
229
230         int m_move_id = -1;
231         bool m_move_has_really_moved = false;
232         u64 m_move_downtime = 0;
233         bool m_move_sent_as_mouse_event = false;
234         v2s32 m_move_downlocation = v2s32(-10000, -10000);
235
236         int m_joystick_id = -1;
237         bool m_joystick_has_really_moved = false;
238         bool m_fixed_joystick = false;
239         bool m_joystick_triggers_special1 = false;
240         button_info *m_joystick_btn_off = nullptr;
241         button_info *m_joystick_btn_bg = nullptr;
242         button_info *m_joystick_btn_center = nullptr;
243
244         button_info m_buttons[after_last_element_id];
245
246         // gui button detection
247         touch_gui_button_id getButtonID(s32 x, s32 y);
248
249         // gui button by eventID
250         touch_gui_button_id getButtonID(size_t eventID);
251
252         // check if a button has changed
253         void handleChangedButton(const SEvent &event);
254
255         // initialize a button
256         void initButton(touch_gui_button_id id, const rect<s32> &button_rect,
257                         const std::wstring &caption, bool immediate_release,
258                         float repeat_delay = BUTTON_REPEAT_DELAY);
259
260         // initialize a joystick button
261         button_info *initJoystickButton(touch_gui_button_id id,
262                         const rect<s32> &button_rect, int texture_id,
263                         bool visible = true);
264
265         struct id_status
266         {
267                 size_t 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, size_t eventID, bool action);
277
278         // handle pressed hud buttons
279         bool isHUDButton(const SEvent &event);
280
281         // handle double taps
282         bool doubleTapDetection();
283
284         // handle release event
285         void handleReleaseEvent(size_t evt_id);
286
287         // apply joystick status
288         void applyJoystickStatus();
289
290         // double-click detection variables
291         struct key_event
292         {
293                 u64 down_time;
294                 s32 x;
295                 s32 y;
296         };
297
298         // array for saving last known position of a pointer
299         std::map<size_t, v2s32> m_pointerpos;
300
301         // array for double tap detection
302         key_event m_key_events[2];
303
304         // settings bar
305         AutoHideButtonBar m_settingsbar;
306
307         // rare controls bar
308         AutoHideButtonBar m_rarecontrolsbar;
309 };
310
311 extern TouchScreenGUI *g_touchscreengui;