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.
21 #include <IEventReceiver.h>
22 #include <IGUIButton.h>
23 #include <IGUIEnvironment.h>
28 #include "client/tile.h"
32 using namespace irr::core;
33 using namespace irr::gui;
42 after_last_element_id,
44 rare_controls_starter_id,
54 } touch_gui_button_id;
61 } autohide_button_bar_dir;
63 #define MIN_DIG_TIME_MS 500
64 #define MAX_TOUCH_COUNT 64
65 #define BUTTON_REPEAT_DELAY 0.2f
67 #define SETTINGS_BAR_Y_OFFSET 6.5
68 #define RARE_CONTROLS_BAR_Y_OFFSET 4
70 extern const char **touchgui_button_imagenames;
76 irr::EKEY_CODE keycode;
78 IGUIButton *guibutton = nullptr;
79 bool immediate_release;
82 class AutoHideButtonBar
85 AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
87 void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
88 v2s32 UpperLeft, v2s32 LowerRight, autohide_button_bar_dir dir,
93 /* add button to be shown */
94 void addButton(touch_gui_button_id id, const wchar_t *caption,
95 const char *btn_image);
97 /* detect settings bar button events */
98 bool isButton(const SEvent &event);
100 /* handle released hud buttons */
101 bool isReleaseButton(int eventID);
104 void step(float dtime);
106 /* deactivate button bar */
109 /* hide the whole buttonbar */
112 /* unhide the buttonbar */
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;
126 /* show settings bar */
127 bool m_active = false;
129 bool m_visible = true;
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;
141 TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
144 void translateEvent(const SEvent &event);
146 void init(ISimpleTextureSource *tsrc);
148 double getYawChange()
150 double res = m_camera_yaw_change;
151 m_camera_yaw_change = 0;
155 double getPitch() { return m_camera_pitch; }
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.
165 line3d<f32> getShootline() { return m_shootline; }
167 void step(float dtime);
169 void registerHudItem(int index, const rect<s32> &rect);
170 void Toggle(bool visible);
176 IrrlichtDevice *m_device;
177 IGUIEnvironment *m_guienv;
178 IEventReceiver *m_receiver;
179 ISimpleTextureSource *m_texturesource;
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
185 /* value in degree */
186 double m_camera_yaw_change = 0.0;
187 double m_camera_pitch = 0.0;
190 * A line starting at the camera and pointing towards the
192 * The line ends on the camera's far plane.
193 * The coordinates do not contain the camera offset.
195 line3d<f32> m_shootline;
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);
203 button_info m_buttons[after_last_element_id];
205 /* gui button detection */
206 touch_gui_button_id getButtonID(s32 x, s32 y);
208 /* gui button by eventID */
209 touch_gui_button_id getButtonID(int eventID);
211 /* check if a button has changed */
212 void handleChangedButton(const SEvent &event);
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);
226 /* vector to store known ids and their initial touch positions*/
227 std::vector<id_status> m_known_ids;
229 /* handle a button event */
230 void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
232 /* handle pressed hud buttons */
233 bool isHUDButton(const SEvent &event);
235 /* handle released hud buttons */
236 bool isReleaseHUDButton(int eventID);
238 /* handle double taps */
239 bool doubleTapDetection();
241 /* handle release event */
242 void handleReleaseEvent(int evt_id);
244 /* get size of regular gui control button */
245 int getGuiButtonSize();
247 /* doubleclick detection variables */
250 unsigned int down_time;
255 /* array for saving last known position of a pointer */
256 v2s32 m_pointerpos[MAX_TOUCH_COUNT];
258 /* array for doubletap detection */
259 key_event m_key_events[2];
262 AutoHideButtonBar m_settingsbar;
264 /* rare controls bar */
265 AutoHideButtonBar m_rarecontrolsbar;
267 extern TouchScreenGUI *g_touchscreengui;