Minetest 5.10.0-dev
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Loading...
Searching...
No Matches
touchscreengui.h
Go to the documentation of this file.
1/*
2Copyright (C) 2014 sapier
3Copyright (C) 2024 grorp, Gregor Parzefall
4 <gregor.parzefall@posteo.de>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU Lesser General Public License as published by
8the Free Software Foundation; either version 2.1 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General Public License along
17with this program; if not, write to the Free Software Foundation, Inc.,
1851 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21#pragma once
22
23#include "IGUIStaticText.h"
24#include "irrlichttypes.h"
25#include <IEventReceiver.h>
26#include <IGUIImage.h>
27#include <IGUIEnvironment.h>
28#include <IrrlichtDevice.h>
29
30#include <memory>
31#include <optional>
32#include <unordered_map>
33#include <vector>
34
35#include "itemdef.h"
36#include "client/game.h"
37
38using namespace irr;
39using namespace irr::core;
40using namespace irr::gui;
41
42
43// We cannot use irr_ptr for Irrlicht GUI elements we own.
44// Option 1: Pass IGUIElement* returned by IGUIEnvironment::add* into irr_ptr
45// constructor.
46// -> We steal the reference owned by IGUIEnvironment and drop it later,
47// causing the IGUIElement to be deleted while IGUIEnvironment still
48// references it.
49// Option 2: Pass IGUIElement* returned by IGUIEnvironment::add* into irr_ptr::grab.
50// -> We add another reference and drop it later, but since IGUIEnvironment
51// still references the IGUIElement, it is never deleted.
52// To make IGUIEnvironment drop its reference to the IGUIElement, we have to call
53// IGUIElement::remove, so that's what we'll do.
54template <typename T>
55std::shared_ptr<T> grab_gui_element(T *element)
56{
57 static_assert(std::is_base_of_v<IGUIElement, T>,
58 "grab_gui_element only works for IGUIElement");
59 return std::shared_ptr<T>(element, [](T *e) {
60 e->remove();
61 });
62}
63
64
65enum class TapState
66{
67 None,
69 LongTap,
70};
71
101
102
103#define BUTTON_REPEAT_DELAY 0.5f
104#define BUTTON_REPEAT_INTERVAL 0.333f
105
106// Our simulated clicks last some milliseconds so that server-side mods have a
107// chance to detect them via l_get_player_control.
108// If you tap faster than this value, the simulated clicks are of course shorter.
109#define SIMULATED_CLICK_DURATION_MS 50
110
111
113{
115 EKEY_CODE keycode;
116 std::vector<size_t> pointer_ids;
117 std::shared_ptr<IGUIImage> gui_button = nullptr;
118
119 enum {
124 std::string toggle_textures[2];
125
126 void emitAction(bool action, video::IVideoDriver *driver,
127 IEventReceiver *receiver, ISimpleTextureSource *tsrc);
128};
129
130
132{
133public:
134 TouchScreenGUI(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
135
136 void translateEvent(const SEvent &event);
138
140 {
141 double res = m_camera_yaw_change;
143 return res;
144 }
145
146 double getPitchChange() {
147 double res = m_camera_pitch_change;
149 return res;
150 }
151
160 line3d<f32> getShootline() { return m_shootline; }
161
164
165 void step(float dtime);
166 inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
167
168 void setVisible(bool visible);
169 void hide();
170 void show();
171
172 void resetHotbarRects();
173 void registerHotbarRect(u16 index, const recti &rect);
174 std::optional<u16> getHotbarSelection();
175
176private:
177 IrrlichtDevice *m_device = nullptr;
178 IGUIEnvironment *m_guienv = nullptr;
179 IEventReceiver *m_receiver = nullptr;
185 bool m_visible = true; // is the whole touch screen gui visible
186
187 std::unordered_map<u16, recti> m_hotbar_rects;
188 std::optional<u16> m_hotbar_selection = std::nullopt;
189
190 // value in degree
193
199 line3d<f32> m_shootline;
200
201 bool m_has_move_id = false;
202 size_t m_move_id;
205 // m_move_pos stays valid even after m_move_id has been released.
207 // This is needed so that we don't miss if m_has_move_id is true for less
208 // than one client step, i.e. press and release happen in the same step.
209 bool m_had_move_id = false;
211
212 bool m_has_joystick_id = false;
215 float m_joystick_direction = 0.0f; // assume forward
216 float m_joystick_speed = 0.0f; // no movement
218 bool m_fixed_joystick = false;
220 bool m_draw_crosshair = false;
221 std::shared_ptr<IGUIImage> m_joystick_btn_off;
222 std::shared_ptr<IGUIImage> m_joystick_btn_bg;
223 std::shared_ptr<IGUIImage> m_joystick_btn_center;
224
225 std::vector<button_info> m_buttons;
226 std::shared_ptr<IGUIImage> m_overflow_btn;
227
228 bool m_overflow_open = false;
229 std::shared_ptr<IGUIStaticText> m_overflow_bg;
230 std::vector<button_info> m_overflow_buttons;
231 std::vector<std::shared_ptr<IGUIStaticText>> m_overflow_button_titles;
232 std::vector<recti> m_overflow_button_rects;
233
234 void toggleOverflowMenu();
235 void updateVisibility();
236 void releaseAll();
237
238 // initialize a button
239 void addButton(std::vector<button_info> &buttons,
240 touch_gui_button_id id, const std::string &image,
241 const recti &rect, bool visible=true);
242 void addToggleButton(std::vector<button_info> &buttons,
244 const std::string &image_1, const std::string &image_2,
245 const recti &rect, bool visible=true);
246
248 const recti &rect, bool visible);
249
250 // handle pressing hotbar items
251 bool isHotbarButton(const SEvent &event);
252
253 // handle release event
254 void handleReleaseEvent(size_t pointer_id);
255
256 // apply joystick status
257 void applyJoystickStatus();
258
259 // map to store the IDs and original positions of currently pressed pointers
260 std::unordered_map<size_t, v2s32> m_pointer_downpos;
261 // map to store the IDs and positions of currently pressed pointers
262 std::unordered_map<size_t, v2s32> m_pointer_pos;
263
265 void emitMouseEvent(EMOUSE_INPUT_EVENT type);
268
269 bool m_dig_pressed = false;
271
272 bool m_place_pressed = false;
274};
275
Definition texturesource.h:34
Definition touchscreengui.h:132
std::shared_ptr< IGUIImage > m_overflow_btn
Definition touchscreengui.h:226
double getYawChange()
Definition touchscreengui.h:139
std::shared_ptr< IGUIImage > m_joystick_btn_center
Definition touchscreengui.h:223
std::shared_ptr< IGUIStaticText > m_overflow_bg
Definition touchscreengui.h:229
TouchInteractionMode m_last_mode
Definition touchscreengui.h:266
v2s32 m_move_pos
Definition touchscreengui.h:206
bool m_had_move_id
Definition touchscreengui.h:209
bool isHotbarButton(const SEvent &event)
Definition touchscreengui.cpp:450
double m_camera_yaw_change
Definition touchscreengui.h:191
void applyJoystickStatus()
Definition touchscreengui.cpp:705
void handleReleaseEvent(size_t pointer_id)
Definition touchscreengui.cpp:472
TapState m_tap_state
Definition touchscreengui.h:267
bool m_has_move_id
Definition touchscreengui.h:201
bool m_draw_crosshair
Definition touchscreengui.h:220
size_t m_joystick_id
Definition touchscreengui.h:213
double m_touchscreen_threshold
Definition touchscreengui.h:183
bool m_overflow_open
Definition touchscreengui.h:228
double m_camera_pitch_change
Definition touchscreengui.h:192
u64 m_move_downtime
Definition touchscreengui.h:204
void step(float dtime)
Definition touchscreengui.cpp:721
void show()
Definition touchscreengui.cpp:829
ISimpleTextureSource * m_texturesource
Definition touchscreengui.h:180
line3d< f32 > getShootline()
Returns a line which describes what the player is pointing at.
Definition touchscreengui.h:160
bool m_fixed_joystick
Definition touchscreengui.h:218
void addToggleButton(std::vector< button_info > &buttons, touch_gui_button_id id, const std::string &image_1, const std::string &image_2, const recti &rect, bool visible=true)
Definition touchscreengui.cpp:429
IEventReceiver * m_receiver
Definition touchscreengui.h:179
float getMovementSpeed()
Definition touchscreengui.h:163
std::vector< button_info > m_buttons
Definition touchscreengui.h:225
void addButton(std::vector< button_info > &buttons, touch_gui_button_id id, const std::string &image, const recti &rect, bool visible=true)
Definition touchscreengui.cpp:416
void hide()
Definition touchscreengui.cpp:824
void emitMouseEvent(EMOUSE_INPUT_EVENT type)
Definition touchscreengui.cpp:843
void releaseAll()
Definition touchscreengui.cpp:807
std::unordered_map< u16, recti > m_hotbar_rects
Definition touchscreengui.h:187
void resetHotbarRects()
Definition touchscreengui.cpp:760
std::shared_ptr< IGUIImage > m_joystick_btn_bg
Definition touchscreengui.h:222
bool m_place_pressed
Definition touchscreengui.h:272
void applyContextControls(const TouchInteractionMode &mode)
Definition touchscreengui.cpp:858
IrrlichtDevice * m_device
Definition touchscreengui.h:177
void updateVisibility()
Definition touchscreengui.cpp:791
IGUIEnvironment * m_guienv
Definition touchscreengui.h:178
void setVisible(bool visible)
Definition touchscreengui.cpp:770
float m_joystick_direction
Definition touchscreengui.h:215
void setUseCrosshair(bool use_crosshair)
Definition touchscreengui.h:166
bool m_move_prevent_short_tap
Definition touchscreengui.h:210
std::unordered_map< size_t, v2s32 > m_pointer_downpos
Definition touchscreengui.h:260
std::shared_ptr< IGUIImage > m_joystick_btn_off
Definition touchscreengui.h:221
v2u32 m_screensize
Definition touchscreengui.h:181
void translateEvent(const SEvent &event)
Definition touchscreengui.cpp:524
TouchScreenGUI(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
Definition touchscreengui.cpp:276
double getPitchChange()
Definition touchscreengui.h:146
IGUIImage * makeButtonDirect(touch_gui_button_id id, const recti &rect, bool visible)
Definition touchscreengui.cpp:439
bool m_joystick_has_really_moved
Definition touchscreengui.h:214
void toggleOverflowMenu()
Definition touchscreengui.cpp:784
v2s32 getPointerPos()
Definition touchscreengui.cpp:834
bool m_visible
Definition touchscreengui.h:185
std::vector< std::shared_ptr< IGUIStaticText > > m_overflow_button_titles
Definition touchscreengui.h:231
bool m_joystick_triggers_aux1
Definition touchscreengui.h:219
std::vector< recti > m_overflow_button_rects
Definition touchscreengui.h:232
std::optional< u16 > m_hotbar_selection
Definition touchscreengui.h:188
bool m_move_has_really_moved
Definition touchscreengui.h:203
bool m_joystick_status_aux1
Definition touchscreengui.h:217
void registerHotbarRect(u16 index, const recti &rect)
Definition touchscreengui.cpp:765
u16 m_long_tap_delay
Definition touchscreengui.h:184
size_t m_move_id
Definition touchscreengui.h:202
std::unordered_map< size_t, v2s32 > m_pointer_pos
Definition touchscreengui.h:262
float getMovementDirection()
Definition touchscreengui.h:162
std::optional< u16 > getHotbarSelection()
Definition touchscreengui.cpp:465
line3d< f32 > m_shootline
A line starting at the camera and pointing towards the selected object.
Definition touchscreengui.h:199
bool m_dig_pressed
Definition touchscreengui.h:269
s32 m_button_size
Definition touchscreengui.h:182
bool m_has_joystick_id
Definition touchscreengui.h:212
u64 m_place_pressed_until
Definition touchscreengui.h:273
std::vector< button_info > m_overflow_buttons
Definition touchscreengui.h:230
float m_joystick_speed
Definition touchscreengui.h:216
u64 m_dig_pressed_until
Definition touchscreengui.h:270
core::vector2d< s32 > v2s32
Definition irr_v2d.h:28
core::vector2d< u32 > v2u32
Definition irr_v2d.h:29
TouchInteractionMode
Definition itemdef.h:58
@ TouchInteractionMode_END
Definition itemdef.h:62
Definition printing.h:25
Definition guiSkin.cpp:18
Definition shader.h:61
Definition touchscreengui.h:113
std::string toggle_textures[2]
Definition touchscreengui.h:124
enum button_info::@20 toggleable
std::shared_ptr< IGUIImage > gui_button
Definition touchscreengui.h:117
EKEY_CODE keycode
Definition touchscreengui.h:115
void emitAction(bool action, video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
Definition touchscreengui.cpp:107
@ SECOND_TEXTURE
Definition touchscreengui.h:122
@ NOT_TOGGLEABLE
Definition touchscreengui.h:120
@ FIRST_TEXTURE
Definition touchscreengui.h:121
std::vector< size_t > pointer_ids
Definition touchscreengui.h:116
float repeat_counter
Definition touchscreengui.h:114
touch_gui_button_id
Definition touchscreengui.h:73
@ joystick_center_id
Definition touchscreengui.h:99
@ fast_id
Definition touchscreengui.h:83
@ chat_id
Definition touchscreengui.h:91
@ inventory_id
Definition touchscreengui.h:92
@ debug_id
Definition touchscreengui.h:84
@ range_id
Definition touchscreengui.h:86
@ zoom_id
Definition touchscreengui.h:76
@ exit_id
Definition touchscreengui.h:94
@ aux1_id
Definition touchscreengui.h:77
@ fly_id
Definition touchscreengui.h:81
@ minimap_id
Definition touchscreengui.h:87
@ camera_id
Definition touchscreengui.h:85
@ drop_id
Definition touchscreengui.h:93
@ jump_id
Definition touchscreengui.h:74
@ joystick_off_id
Definition touchscreengui.h:97
@ toggle_chat_id
Definition touchscreengui.h:88
@ joystick_bg_id
Definition touchscreengui.h:98
@ sneak_id
Definition touchscreengui.h:75
@ noclip_id
Definition touchscreengui.h:82
@ overflow_id
Definition touchscreengui.h:78
TouchScreenGUI * g_touchscreengui
Definition touchscreengui.cpp:42
std::shared_ptr< T > grab_gui_element(T *element)
Definition touchscreengui.h:55
TapState
Definition touchscreengui.h:66