2 Copyright (C) 2014 sapier
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.
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.
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.
19 #ifndef TOUCHSCREENGUI_HEADER
20 #define TOUCHSCREENGUI_HEADER
22 #include <IEventReceiver.h>
23 #include <IGUIButton.h>
24 #include <IGUIEnvironment.h>
29 #include "client/tile.h"
33 using namespace irr::core;
34 using namespace irr::gui;
43 after_last_element_id,
45 rare_controls_starter_id,
55 } touch_gui_button_id;
62 } autohide_button_bar_dir;
64 #define MIN_DIG_TIME_MS 500
65 #define MAX_TOUCH_COUNT 64
66 #define BUTTON_REPEAT_DELAY 0.2f
68 #define SETTINGS_BAR_Y_OFFSET 6.5
69 #define RARE_CONTROLS_BAR_Y_OFFSET 4
71 extern const char **touchgui_button_imagenames;
77 irr::EKEY_CODE keycode;
79 IGUIButton *guibutton = nullptr;
80 bool immediate_release;
83 class AutoHideButtonBar
86 AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
88 void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
89 v2s32 UpperLeft, v2s32 LowerRight, autohide_button_bar_dir dir,
94 /* add button to be shown */
95 void addButton(touch_gui_button_id id, const wchar_t *caption,
96 const char *btn_image);
98 /* detect settings bar button events */
99 bool isButton(const SEvent &event);
101 /* handle released hud buttons */
102 bool isReleaseButton(int eventID);
105 void step(float dtime);
107 /* deactivate button bar */
110 /* hide the whole buttonbar */
113 /* unhide the buttonbar */
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;
127 /* show settings bar */
128 bool m_active = false;
130 bool m_visible = true;
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;
142 TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
145 void translateEvent(const SEvent &event);
147 void init(ISimpleTextureSource *tsrc);
149 double getYawChange()
151 double res = m_camera_yaw_change;
152 m_camera_yaw_change = 0;
156 double getPitch() { return m_camera_pitch; }
158 line3d<f32> getShootline() { return m_shootline; }
160 void step(float dtime);
162 void registerHudItem(int index, const rect<s32> &rect);
163 void Toggle(bool visible);
169 IrrlichtDevice *m_device;
170 IGUIEnvironment *m_guienv;
171 IEventReceiver *m_receiver;
172 ISimpleTextureSource *m_texturesource;
174 std::map<int, rect<s32>> m_hud_rects;
175 std::map<int, irr::EKEY_CODE> m_hud_ids;
176 bool m_visible; // is the gui visible
178 /* value in degree */
179 double m_camera_yaw_change = 0.0;
180 double m_camera_pitch = 0.0;
182 line3d<f32> m_shootline;
185 bool m_move_has_really_moved = false;
186 s64 m_move_downtime = 0;
187 bool m_move_sent_as_mouse_event = false;
188 v2s32 m_move_downlocation = v2s32(-10000, -10000);
190 button_info m_buttons[after_last_element_id];
192 /* gui button detection */
193 touch_gui_button_id getButtonID(s32 x, s32 y);
195 /* gui button by eventID */
196 touch_gui_button_id getButtonID(int eventID);
198 /* check if a button has changed */
199 void handleChangedButton(const SEvent &event);
201 /* initialize a button */
202 void initButton(touch_gui_button_id id, rect<s32> button_rect,
203 std::wstring caption, bool immediate_release,
204 float repeat_delay = BUTTON_REPEAT_DELAY);
213 /* vector to store known ids and their initial touch positions*/
214 std::vector<id_status> m_known_ids;
216 /* handle a button event */
217 void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
219 /* handle pressed hud buttons */
220 bool isHUDButton(const SEvent &event);
222 /* handle released hud buttons */
223 bool isReleaseHUDButton(int eventID);
225 /* handle double taps */
226 bool doubleTapDetection();
228 /* handle release event */
229 void handleReleaseEvent(int evt_id);
231 /* get size of regular gui control button */
232 int getGuiButtonSize();
234 /* doubleclick detection variables */
237 unsigned int down_time;
242 /* array for saving last known position of a pointer */
243 v2s32 m_pointerpos[MAX_TOUCH_COUNT];
245 /* array for doubletap detection */
246 key_event m_key_events[2];
249 AutoHideButtonBar m_settingsbar;
251 /* rare controls bar */
252 AutoHideButtonBar m_rarecontrolsbar;
254 extern TouchScreenGUI *g_touchscreengui;