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