]> git.lizzy.rs Git - dragonfireclient.git/blob - src/touchscreengui.h
Handle particle spawners in env and delete expired ids
[dragonfireclient.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 #ifndef TOUCHSCREENGUI_HEADER
20 #define TOUCHSCREENGUI_HEADER
21
22 #include <IGUIEnvironment.h>
23 #include <IGUIButton.h>
24 #include <IEventReceiver.h>
25
26 #include <vector>
27 #include <map>
28
29 #include "game.h"
30 #include "client/tile.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         float            repeatcounter;
75         float            repeatdelay;
76         irr::EKEY_CODE   keycode;
77         std::vector<int> ids;
78         IGUIButton*      guibutton;
79         bool             immediate_release;
80 };
81
82 class AutoHideButtonBar
83 {
84 public:
85
86         AutoHideButtonBar( IrrlichtDevice *device, IEventReceiver* receiver );
87
88         void init(ISimpleTextureSource* tsrc, const char* starter_img,
89                         int button_id, v2s32 UpperLeft, v2s32 LowerRight,
90                         autohide_button_bar_dir dir, 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;
118         irr::video::IVideoDriver* m_driver;
119         IGUIEnvironment*          m_guienv;
120         IEventReceiver*           m_receiver;
121         v2u32                     m_screensize;
122         button_info               m_starter;
123         std::vector<button_info*> m_buttons;
124
125         v2s32                     m_upper_left;
126         v2s32                     m_lower_right;
127
128         /* show settings bar */
129         bool                      m_active;
130
131         bool                      m_visible;
132
133         /* settings bar timeout */
134         float                     m_timeout;
135         float                     m_timeout_value;
136         bool                      m_initialized;
137         autohide_button_bar_dir   m_dir;
138 };
139
140 class TouchScreenGUI
141 {
142 public:
143         TouchScreenGUI(IrrlichtDevice *device, IEventReceiver* receiver);
144         ~TouchScreenGUI();
145
146         void translateEvent(const SEvent &event);
147
148         void init(ISimpleTextureSource* tsrc);
149
150         double getYaw() { return m_camera_yaw; }
151         double getPitch() { return m_camera_pitch; }
152         line3d<f32> getShootline() { return m_shootline; }
153
154         void step(float dtime);
155         void resetHud();
156         void registerHudItem(int index, const rect<s32> &rect);
157         void Toggle(bool visible);
158
159         void hide();
160         void show();
161
162 private:
163         IrrlichtDevice*         m_device;
164         IGUIEnvironment*        m_guienv;
165         IEventReceiver*         m_receiver;
166         ISimpleTextureSource*   m_texturesource;
167         v2u32                   m_screensize;
168         std::map<int,rect<s32> > m_hud_rects;
169         std::map<int,irr::EKEY_CODE> m_hud_ids;
170         bool                    m_visible; // is the gui visible
171
172         /* value in degree */
173         double                  m_camera_yaw;
174         double                  m_camera_pitch;
175
176         line3d<f32>             m_shootline;
177
178         rect<s32>               m_control_pad_rect;
179
180         int                     m_move_id;
181         bool                    m_move_has_really_moved;
182         s32                     m_move_downtime;
183         bool                    m_move_sent_as_mouse_event;
184         v2s32                   m_move_downlocation;
185
186         button_info m_buttons[after_last_element_id];
187
188         /* gui button detection */
189         touch_gui_button_id getButtonID(s32 x, s32 y);
190
191         /* gui button by eventID */
192         touch_gui_button_id getButtonID(int eventID);
193
194         /* check if a button has changed */
195         void handleChangedButton(const SEvent &event);
196
197         /* initialize a button */
198         void initButton(touch_gui_button_id id, rect<s32> button_rect,
199                         std::wstring caption, bool immediate_release,
200                         float repeat_delay = BUTTON_REPEAT_DELAY);
201
202         /* load texture */
203         void loadButtonTexture(button_info* btn, const char* path, rect<s32> button_rect);
204
205         struct id_status{
206                 int id;
207                 int X;
208                 int Y;
209         };
210
211         /* vector to store known ids and their initial touch positions*/
212         std::vector<id_status> m_known_ids;
213
214         /* handle a button event */
215         void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
216
217         /* handle pressed hud buttons */
218         bool isHUDButton(const SEvent &event);
219
220         /* handle released hud buttons */
221         bool isReleaseHUDButton(int eventID);
222
223         /* handle double taps */
224         bool doubleTapDetection();
225
226         /* handle release event */
227         void handleReleaseEvent(int evt_id);
228
229         /* get size of regular gui control button */
230         int getGuiButtonSize();
231
232         /* doubleclick detection variables */
233         struct key_event {
234                 unsigned int down_time;
235                 s32 x;
236                 s32 y;
237         };
238
239         /* array for saving last known position of a pointer */
240         v2s32 m_pointerpos[MAX_TOUCH_COUNT];
241
242         /* array for doubletap detection */
243         key_event m_key_events[2];
244
245         /* settings bar */
246         AutoHideButtonBar m_settingsbar;
247
248         /* rare controls bar */
249         AutoHideButtonBar m_rarecontrolsbar;
250 };
251 extern TouchScreenGUI *g_touchscreengui;
252 #endif