Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
touchcontrols.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2014 sapier
4// Copyright (C) 2024 grorp, Gregor Parzefall
5
6#pragma once
7
8#include "IGUIStaticText.h"
9#include "irrlichttypes.h"
10#include <IEventReceiver.h>
11#include <IGUIImage.h>
12#include <IGUIEnvironment.h>
13
14#include <memory>
15#include <optional>
16#include <unordered_map>
17#include <vector>
18
19#include "itemdef.h"
20#include "client/game.h"
21#include "util/basic_macros.h"
23
24namespace irr
25{
26 class IrrlichtDevice;
27}
28
29using namespace irr::core;
30using namespace irr::gui;
31
32
33// We cannot use irr_ptr for Irrlicht GUI elements we own.
34// Option 1: Pass IGUIElement* returned by IGUIEnvironment::add* into irr_ptr
35// constructor.
36// -> We steal the reference owned by IGUIEnvironment and drop it later,
37// causing the IGUIElement to be deleted while IGUIEnvironment still
38// references it.
39// Option 2: Pass IGUIElement* returned by IGUIEnvironment::add* into irr_ptr::grab.
40// -> We add another reference and drop it later, but since IGUIEnvironment
41// still references the IGUIElement, it is never deleted.
42// To make IGUIEnvironment drop its reference to the IGUIElement, we have to call
43// IGUIElement::remove, so that's what we'll do.
44template <typename T>
45std::shared_ptr<T> grab_gui_element(T *element)
46{
47 static_assert(std::is_base_of_v<IGUIElement, T>,
48 "grab_gui_element only works for IGUIElement");
49 return std::shared_ptr<T>(element, [](T *e) {
50 e->remove();
51 });
52}
53
54
55enum class TapState
56{
57 None,
59 LongTap,
60};
61
91
92
93#define BUTTON_REPEAT_DELAY 0.5f
94#define BUTTON_REPEAT_INTERVAL 0.333f
95
96// Our simulated clicks last some milliseconds so that server-side mods have a
97// chance to detect them via l_get_player_control.
98// If you tap faster than this value, the simulated clicks are of course shorter.
99#define SIMULATED_CLICK_DURATION_MS 50
100
101
103{
105 EKEY_CODE keycode;
106 std::vector<size_t> pointer_ids;
107 std::shared_ptr<IGUIImage> gui_button = nullptr;
108
109 enum {
114 std::string toggle_textures[2];
115
116 void emitAction(bool action, video::IVideoDriver *driver,
117 IEventReceiver *receiver, ISimpleTextureSource *tsrc);
118};
119
120
122{
123public:
124 TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
127
128 void translateEvent(const SEvent &event);
130
132 {
133 double res = m_camera_yaw_change;
135 return res;
136 }
137
138 double getPitchChange() {
139 double res = m_camera_pitch_change;
141 return res;
142 }
143
152 line3d<f32> getShootline() { return m_shootline; }
153
156
157 void step(float dtime);
158 inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
159
160 void setVisible(bool visible);
161 void hide();
162 void show();
163
164 void resetHotbarRects();
165 void registerHotbarRect(u16 index, const recti &rect);
166 std::optional<u16> getHotbarSelection();
167
169 IGUIStaticText *getStatusText() { return m_status_text.get(); }
170
171private:
172 IrrlichtDevice *m_device = nullptr;
173 IGUIEnvironment *m_guienv = nullptr;
174 IEventReceiver *m_receiver = nullptr;
180 bool m_visible = true;
181
182 std::unordered_map<u16, recti> m_hotbar_rects;
183 std::optional<u16> m_hotbar_selection = std::nullopt;
184
185 // value in degree
188
194 line3d<f32> m_shootline;
195
196 bool m_has_move_id = false;
197 size_t m_move_id;
200 // m_move_pos stays valid even after m_move_id has been released.
202 // This is needed so that we don't miss if m_has_move_id is true for less
203 // than one client step, i.e. press and release happen in the same step.
204 bool m_had_move_id = false;
206
207 bool m_has_joystick_id = false;
210 float m_joystick_direction = 0.0f; // assume forward
211 float m_joystick_speed = 0.0f; // no movement
213 bool m_fixed_joystick = false;
215 bool m_draw_crosshair = false;
216 std::shared_ptr<IGUIImage> m_joystick_btn_off;
217 std::shared_ptr<IGUIImage> m_joystick_btn_bg;
218 std::shared_ptr<IGUIImage> m_joystick_btn_center;
219
220 std::vector<button_info> m_buttons;
221 std::shared_ptr<IGUIImage> m_overflow_btn;
222
223 bool m_overflow_open = false;
224 std::shared_ptr<IGUIStaticText> m_overflow_bg;
225 std::vector<button_info> m_overflow_buttons;
226 std::vector<std::shared_ptr<IGUIStaticText>> m_overflow_button_titles;
227 std::vector<recti> m_overflow_button_rects;
228
229 std::shared_ptr<IGUIStaticText> m_status_text;
230
231 void toggleOverflowMenu();
232 void updateVisibility();
233 void releaseAll();
234
235 // initialize a button
236 void addButton(std::vector<button_info> &buttons,
237 touch_gui_button_id id, const std::string &image,
238 const recti &rect, bool visible=true);
239 void addToggleButton(std::vector<button_info> &buttons,
241 const std::string &image_1, const std::string &image_2,
242 const recti &rect, bool visible=true);
243
245 const recti &rect, bool visible);
246
247 // handle pressing hotbar items
248 bool isHotbarButton(const SEvent &event);
249
250 // handle release event
251 void handleReleaseEvent(size_t pointer_id);
252
253 // apply joystick status
254 void applyJoystickStatus();
255
256 // map to store the IDs and original positions of currently pressed pointers
257 std::unordered_map<size_t, v2s32> m_pointer_downpos;
258 // map to store the IDs and positions of currently pressed pointers
259 std::unordered_map<size_t, v2s32> m_pointer_pos;
260
262 void emitMouseEvent(EMOUSE_INPUT_EVENT type);
265
266 bool m_dig_pressed = false;
268
269 bool m_place_pressed = false;
271};
272
Definition texturesource.h:25
Definition touchcontrols.h:122
bool m_has_joystick_id
Definition touchcontrols.h:207
void registerHotbarRect(u16 index, const recti &rect)
Definition touchcontrols.cpp:742
bool m_dig_pressed
Definition touchcontrols.h:266
DISABLE_CLASS_COPY(TouchControls)
void applyContextControls(const TouchInteractionMode &mode)
Definition touchcontrols.cpp:839
IGUIImage * makeButtonDirect(touch_gui_button_id id, const recti &rect, bool visible)
Definition touchcontrols.cpp:433
v2s32 m_move_pos
Definition touchcontrols.h:201
void show()
Definition touchcontrols.cpp:809
bool m_visible
Definition touchcontrols.h:180
void updateVisibility()
Definition touchcontrols.cpp:768
TapState m_tap_state
Definition touchcontrols.h:264
double m_camera_pitch_change
Definition touchcontrols.h:187
~TouchControls()
Definition touchcontrols.cpp:405
u64 m_move_downtime
Definition touchcontrols.h:199
IGUIStaticText * getStatusText()
Definition touchcontrols.h:169
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 touchcontrols.cpp:423
u64 m_place_pressed_until
Definition touchcontrols.h:270
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:261
bool m_fixed_joystick
Definition touchcontrols.h:213
v2u32 m_screensize
Definition touchcontrols.h:176
void resetHotbarRects()
Definition touchcontrols.cpp:737
std::optional< u16 > getHotbarSelection()
Definition touchcontrols.cpp:459
bool m_overflow_open
Definition touchcontrols.h:223
double getYawChange()
Definition touchcontrols.h:131
line3d< f32 > m_shootline
A line starting at the camera and pointing towards the selected object.
Definition touchcontrols.h:194
std::vector< button_info > m_overflow_buttons
Definition touchcontrols.h:225
float m_joystick_speed
Definition touchcontrols.h:211
u16 m_long_tap_delay
Definition touchcontrols.h:179
std::shared_ptr< IGUIStaticText > m_overflow_bg
Definition touchcontrols.h:224
std::vector< recti > m_overflow_button_rects
Definition touchcontrols.h:227
v2s32 getPointerPos()
Definition touchcontrols.cpp:814
ISimpleTextureSource * m_texturesource
Definition touchcontrols.h:175
void releaseAll()
Definition touchcontrols.cpp:787
std::vector< std::shared_ptr< IGUIStaticText > > m_overflow_button_titles
Definition touchcontrols.h:226
bool m_move_prevent_short_tap
Definition touchcontrols.h:205
void emitMouseEvent(EMOUSE_INPUT_EVENT type)
Definition touchcontrols.cpp:823
void addButton(std::vector< button_info > &buttons, touch_gui_button_id id, const std::string &image, const recti &rect, bool visible=true)
Definition touchcontrols.cpp:410
std::unordered_map< size_t, v2s32 > m_pointer_pos
Definition touchcontrols.h:259
size_t m_joystick_id
Definition touchcontrols.h:208
TouchInteractionMode m_last_mode
Definition touchcontrols.h:263
std::shared_ptr< IGUIImage > m_joystick_btn_center
Definition touchcontrols.h:218
IGUIEnvironment * m_guienv
Definition touchcontrols.h:173
std::optional< u16 > m_hotbar_selection
Definition touchcontrols.h:183
IEventReceiver * m_receiver
Definition touchcontrols.h:174
float getJoystickSpeed()
Definition touchcontrols.h:155
std::shared_ptr< IGUIImage > m_overflow_btn
Definition touchcontrols.h:221
void setUseCrosshair(bool use_crosshair)
Definition touchcontrols.h:158
void translateEvent(const SEvent &event)
Definition touchcontrols.cpp:513
s32 m_button_size
Definition touchcontrols.h:177
void setVisible(bool visible)
Definition touchcontrols.cpp:747
std::shared_ptr< IGUIImage > m_joystick_btn_bg
Definition touchcontrols.h:217
std::unordered_map< size_t, v2s32 > m_pointer_downpos
Definition touchcontrols.h:257
bool isHotbarButton(const SEvent &event)
Definition touchcontrols.cpp:444
IrrlichtDevice * m_device
Definition touchcontrols.h:172
void handleReleaseEvent(size_t pointer_id)
Definition touchcontrols.cpp:466
bool m_draw_crosshair
Definition touchcontrols.h:215
void applyJoystickStatus()
Definition touchcontrols.cpp:686
void toggleOverflowMenu()
Definition touchcontrols.cpp:760
bool m_had_move_id
Definition touchcontrols.h:204
size_t m_move_id
Definition touchcontrols.h:197
std::shared_ptr< IGUIStaticText > m_status_text
Definition touchcontrols.h:229
double m_touchscreen_threshold
Definition touchcontrols.h:178
std::shared_ptr< IGUIImage > m_joystick_btn_off
Definition touchcontrols.h:216
line3d< f32 > getShootline()
Returns a line which describes what the player is pointing at.
Definition touchcontrols.h:152
u64 m_dig_pressed_until
Definition touchcontrols.h:267
std::vector< button_info > m_buttons
Definition touchcontrols.h:220
void hide()
Definition touchcontrols.cpp:804
std::unordered_map< u16, recti > m_hotbar_rects
Definition touchcontrols.h:182
float m_joystick_direction
Definition touchcontrols.h:210
float getJoystickDirection()
Definition touchcontrols.h:154
bool isStatusTextOverriden()
Definition touchcontrols.h:168
bool m_joystick_status_aux1
Definition touchcontrols.h:212
bool m_move_has_really_moved
Definition touchcontrols.h:198
void step(float dtime)
Definition touchcontrols.cpp:702
double getPitchChange()
Definition touchcontrols.h:138
double m_camera_yaw_change
Definition touchcontrols.h:186
bool m_place_pressed
Definition touchcontrols.h:269
bool m_joystick_has_really_moved
Definition touchcontrols.h:209
bool m_joystick_triggers_aux1
Definition touchcontrols.h:214
bool m_has_move_id
Definition touchcontrols.h:196
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
core::vector2d< u32 > v2u32
Definition irr_v2d.h:14
TouchInteractionMode
Definition itemdef.h:43
@ TouchInteractionMode_END
Definition itemdef.h:47
Definition printing.h:10
Definition fontengine.h:14
Definition clientmap.h:30
Definition touchcontrols.h:103
std::string toggle_textures[2]
Definition touchcontrols.h:114
@ SECOND_TEXTURE
Definition touchcontrols.h:112
@ NOT_TOGGLEABLE
Definition touchcontrols.h:110
@ FIRST_TEXTURE
Definition touchcontrols.h:111
std::shared_ptr< IGUIImage > gui_button
Definition touchcontrols.h:107
EKEY_CODE keycode
Definition touchcontrols.h:105
void emitAction(bool action, video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:92
std::vector< size_t > pointer_ids
Definition touchcontrols.h:106
enum button_info::@19 toggleable
float repeat_counter
Definition touchcontrols.h:104
TouchControls * g_touchcontrols
Definition touchcontrols.cpp:27
touch_gui_button_id
Definition touchcontrols.h:63
@ joystick_center_id
Definition touchcontrols.h:89
@ fast_id
Definition touchcontrols.h:73
@ chat_id
Definition touchcontrols.h:81
@ inventory_id
Definition touchcontrols.h:82
@ debug_id
Definition touchcontrols.h:74
@ range_id
Definition touchcontrols.h:76
@ zoom_id
Definition touchcontrols.h:66
@ exit_id
Definition touchcontrols.h:84
@ aux1_id
Definition touchcontrols.h:67
@ fly_id
Definition touchcontrols.h:71
@ minimap_id
Definition touchcontrols.h:77
@ camera_id
Definition touchcontrols.h:75
@ drop_id
Definition touchcontrols.h:83
@ jump_id
Definition touchcontrols.h:64
@ joystick_off_id
Definition touchcontrols.h:87
@ toggle_chat_id
Definition touchcontrols.h:78
@ joystick_bg_id
Definition touchcontrols.h:88
@ sneak_id
Definition touchcontrols.h:65
@ noclip_id
Definition touchcontrols.h:72
@ overflow_id
Definition touchcontrols.h:68
std::shared_ptr< T > grab_gui_element(T *element)
Definition touchcontrols.h:45
TapState
Definition touchcontrols.h:56