]> git.lizzy.rs Git - minetest.git/blob - src/touchscreengui.h
Optimize headers (part 2) (#6272)
[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         line3d<f32> getShootline() { return m_shootline; }
158
159         void step(float dtime);
160         void resetHud();
161         void registerHudItem(int index, const rect<s32> &rect);
162         void Toggle(bool visible);
163
164         void hide();
165         void show();
166
167 private:
168         IrrlichtDevice *m_device;
169         IGUIEnvironment *m_guienv;
170         IEventReceiver *m_receiver;
171         ISimpleTextureSource *m_texturesource;
172         v2u32 m_screensize;
173         std::map<int, rect<s32>> m_hud_rects;
174         std::map<int, irr::EKEY_CODE> m_hud_ids;
175         bool m_visible; // is the gui visible
176
177         /* value in degree */
178         double m_camera_yaw_change = 0.0;
179         double m_camera_pitch = 0.0;
180
181         line3d<f32> m_shootline;
182
183         int m_move_id = -1;
184         bool m_move_has_really_moved = false;
185         s64 m_move_downtime = 0;
186         bool m_move_sent_as_mouse_event = false;
187         v2s32 m_move_downlocation = v2s32(-10000, -10000);
188
189         button_info m_buttons[after_last_element_id];
190
191         /* gui button detection */
192         touch_gui_button_id getButtonID(s32 x, s32 y);
193
194         /* gui button by eventID */
195         touch_gui_button_id getButtonID(int eventID);
196
197         /* check if a button has changed */
198         void handleChangedButton(const SEvent &event);
199
200         /* initialize a button */
201         void initButton(touch_gui_button_id id, rect<s32> button_rect,
202                         std::wstring caption, bool immediate_release,
203                         float repeat_delay = BUTTON_REPEAT_DELAY);
204
205         struct id_status
206         {
207                 int id;
208                 int X;
209                 int Y;
210         };
211
212         /* vector to store known ids and their initial touch positions*/
213         std::vector<id_status> m_known_ids;
214
215         /* handle a button event */
216         void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
217
218         /* handle pressed hud buttons */
219         bool isHUDButton(const SEvent &event);
220
221         /* handle released hud buttons */
222         bool isReleaseHUDButton(int eventID);
223
224         /* handle double taps */
225         bool doubleTapDetection();
226
227         /* handle release event */
228         void handleReleaseEvent(int evt_id);
229
230         /* get size of regular gui control button */
231         int getGuiButtonSize();
232
233         /* doubleclick detection variables */
234         struct key_event
235         {
236                 unsigned int down_time;
237                 s32 x;
238                 s32 y;
239         };
240
241         /* array for saving last known position of a pointer */
242         v2s32 m_pointerpos[MAX_TOUCH_COUNT];
243
244         /* array for doubletap detection */
245         key_event m_key_events[2];
246
247         /* settings bar */
248         AutoHideButtonBar m_settingsbar;
249
250         /* rare controls bar */
251         AutoHideButtonBar m_rarecontrolsbar;
252 };
253 extern TouchScreenGUI *g_touchscreengui;