Make use of safe file writing in auth handler (fixes #6576)
[oweals/minetest.git] / src / 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
25 #include <map>
26 #include <vector>
27
28 #include "client/tile.h"
29 #include "game.h"
30
31 using namespace irr;
32 using namespace irr::core;
33 using namespace irr::gui;
34
35 typedef enum {
36         forward_id = 0,
37         backward_id,
38         left_id,
39         right_id,
40         jump_id,
41         crunch_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         chat_id,
52         inventory_id,
53         drop_id
54 } touch_gui_button_id;
55
56 typedef enum {
57         AHBB_Dir_Top_Bottom,
58         AHBB_Dir_Bottom_Top,
59         AHBB_Dir_Left_Right,
60         AHBB_Dir_Right_Left
61 } autohide_button_bar_dir;
62
63 #define MIN_DIG_TIME_MS 500
64 #define MAX_TOUCH_COUNT 64
65 #define BUTTON_REPEAT_DELAY 0.2f
66
67 #define SETTINGS_BAR_Y_OFFSET 6.5
68 #define RARE_CONTROLS_BAR_Y_OFFSET 4
69
70 extern const char **touchgui_button_imagenames;
71
72 struct button_info
73 {
74         float repeatcounter;
75         float repeatdelay;
76         irr::EKEY_CODE keycode;
77         std::vector<int> ids;
78         IGUIButton *guibutton = nullptr;
79         bool immediate_release;
80 };
81
82 class AutoHideButtonBar
83 {
84 public:
85         AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
86
87         void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
88                         v2s32 UpperLeft, v2s32 LowerRight, autohide_button_bar_dir dir,
89                         float timeout);
90
91         ~AutoHideButtonBar();
92
93         /* add button to be shown */
94         void addButton(touch_gui_button_id id, const wchar_t *caption,
95                         const char *btn_image);
96
97         /* detect settings bar button events */
98         bool isButton(const SEvent &event);
99
100         /* handle released hud buttons */
101         bool isReleaseButton(int eventID);
102
103         /* step handler */
104         void step(float dtime);
105
106         /* deactivate button bar */
107         void deactivate();
108
109         /* hide the whole buttonbar */
110         void hide();
111
112         /* unhide the buttonbar */
113         void show();
114
115 private:
116         ISimpleTextureSource *m_texturesource = nullptr;
117         irr::video::IVideoDriver *m_driver;
118         IGUIEnvironment *m_guienv;
119         IEventReceiver *m_receiver;
120         button_info m_starter;
121         std::vector<button_info *> m_buttons;
122
123         v2s32 m_upper_left;
124         v2s32 m_lower_right;
125
126         /* show settings bar */
127         bool m_active = false;
128
129         bool m_visible = true;
130
131         /* settings bar timeout */
132         float m_timeout = 0.0f;
133         float m_timeout_value = 3.0f;
134         bool m_initialized = false;
135         autohide_button_bar_dir m_dir = AHBB_Dir_Right_Left;
136 };
137
138 class TouchScreenGUI
139 {
140 public:
141         TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
142         ~TouchScreenGUI();
143
144         void translateEvent(const SEvent &event);
145
146         void init(ISimpleTextureSource *tsrc);
147
148         double getYawChange()
149         {
150                 double res = m_camera_yaw_change;
151                 m_camera_yaw_change = 0;
152                 return res;
153         }
154
155         double getPitch() { return m_camera_pitch; }
156
157         /*!
158          * Returns a line which describes what the player is pointing at.
159          * The starting point and looking direction are significant,
160          * the line should be scaled to match its length to the actual distance
161          * the player can reach.
162          * The line starts at the camera and ends on the camera's far plane.
163          * The coordinates do not contain the camera offset.
164          */
165         line3d<f32> getShootline() { return m_shootline; }
166
167         void step(float dtime);
168         void resetHud();
169         void registerHudItem(int index, const rect<s32> &rect);
170         void Toggle(bool visible);
171
172         void hide();
173         void show();
174
175 private:
176         IrrlichtDevice *m_device;
177         IGUIEnvironment *m_guienv;
178         IEventReceiver *m_receiver;
179         ISimpleTextureSource *m_texturesource;
180         v2u32 m_screensize;
181         std::map<int, rect<s32>> m_hud_rects;
182         std::map<int, irr::EKEY_CODE> m_hud_ids;
183         bool m_visible; // is the gui visible
184
185         /* value in degree */
186         double m_camera_yaw_change = 0.0;
187         double m_camera_pitch = 0.0;
188
189         /*!
190          * A line starting at the camera and pointing towards the
191          * selected object.
192          * The line ends on the camera's far plane.
193          * The coordinates do not contain the camera offset.
194          */
195         line3d<f32> m_shootline;
196
197         int m_move_id = -1;
198         bool m_move_has_really_moved = false;
199         s64 m_move_downtime = 0;
200         bool m_move_sent_as_mouse_event = false;
201         v2s32 m_move_downlocation = v2s32(-10000, -10000);
202
203         button_info m_buttons[after_last_element_id];
204
205         /* gui button detection */
206         touch_gui_button_id getButtonID(s32 x, s32 y);
207
208         /* gui button by eventID */
209         touch_gui_button_id getButtonID(int eventID);
210
211         /* check if a button has changed */
212         void handleChangedButton(const SEvent &event);
213
214         /* initialize a button */
215         void initButton(touch_gui_button_id id, rect<s32> button_rect,
216                         std::wstring caption, bool immediate_release,
217                         float repeat_delay = BUTTON_REPEAT_DELAY);
218
219         struct id_status
220         {
221                 int id;
222                 int X;
223                 int Y;
224         };
225
226         /* vector to store known ids and their initial touch positions*/
227         std::vector<id_status> m_known_ids;
228
229         /* handle a button event */
230         void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
231
232         /* handle pressed hud buttons */
233         bool isHUDButton(const SEvent &event);
234
235         /* handle released hud buttons */
236         bool isReleaseHUDButton(int eventID);
237
238         /* handle double taps */
239         bool doubleTapDetection();
240
241         /* handle release event */
242         void handleReleaseEvent(int evt_id);
243
244         /* get size of regular gui control button */
245         int getGuiButtonSize();
246
247         /* doubleclick detection variables */
248         struct key_event
249         {
250                 unsigned int down_time;
251                 s32 x;
252                 s32 y;
253         };
254
255         /* array for saving last known position of a pointer */
256         v2s32 m_pointerpos[MAX_TOUCH_COUNT];
257
258         /* array for doubletap detection */
259         key_event m_key_events[2];
260
261         /* settings bar */
262         AutoHideButtonBar m_settingsbar;
263
264         /* rare controls bar */
265         AutoHideButtonBar m_rarecontrolsbar;
266 };
267 extern TouchScreenGUI *g_touchscreengui;