Luanti 5.15.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 "irrlichttypes.h"
9#include "IEventReceiver.h"
10
11#include <memory>
12#include <optional>
13#include <unordered_map>
14#include <vector>
15
16#include "itemdef.h"
17#include "touchscreenlayout.h"
18#include "util/basic_macros.h"
19#include "client/keycode.h"
20
21class IrrlichtDevice;
22namespace gui
23{
24 class IGUIEnvironment;
25 class IGUIImage;
26 class IGUIStaticText;
27}
28
30
31using namespace core;
32using namespace gui;
33
34
35enum class TapState
36{
37 None,
39 LongTap,
40};
41
42
43#define BUTTON_REPEAT_DELAY 0.5f
44#define BUTTON_REPEAT_INTERVAL 0.333f
45
46// Our simulated clicks last some milliseconds so that server-side mods have a
47// chance to detect them via l_get_player_control.
48// If you tap faster than this value, the simulated clicks are of course shorter.
49#define SIMULATED_CLICK_DURATION_MS 50
50
51
53{
57 std::vector<size_t> pointer_ids;
58 std::shared_ptr<IGUIImage> gui_button = nullptr;
59
60 enum {
65 std::string toggle_textures[2];
66};
67
68
70{
71public:
72 TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
75
76 void translateEvent(const SEvent &event);
78
79 double getYawChange()
80 {
81 double res = m_camera_yaw_change;
83 return res;
84 }
85
86 double getPitchChange() {
87 double res = m_camera_pitch_change;
89 return res;
90 }
91
93
105 line3d<f32> getShootline() { return m_shootline; }
106
109
110 void step(float dtime);
111
112 void setVisible(bool visible);
113 void hide();
114 void show();
115
116 void resetHotbarRects();
117 void registerHotbarRect(u16 index, const recti &rect);
118 std::optional<u16> getHotbarSelection();
119
121 IGUIStaticText *getStatusText() { return m_status_text.get(); }
122
123private:
124 IrrlichtDevice *m_device = nullptr;
125 IGUIEnvironment *m_guienv = nullptr;
126 IEventReceiver *m_receiver = nullptr;
128 bool m_visible = true;
129
130 // changes to these two values are handled in TouchControls::step
133
134 // cached settings
140
141 static void settingChangedCallback(const std::string &name, void *data);
142 void readSettings();
143
145 void applyLayout(const ButtonLayout &layout);
146
147 std::unordered_map<u16, recti> m_hotbar_rects;
148 std::optional<u16> m_hotbar_selection = std::nullopt;
149
150 // value in degree
153
161 line3d<f32> m_shootline;
162
163 bool m_has_move_id = false;
164 size_t m_move_id;
167 // m_move_pos stays valid even after the m_move_id pointer has been
168 // released and m_pointer_pos[m_move_id] has been erased
169 // (or even overwritten by a new pointer reusing the same id).
171 // This is needed so that we don't miss if m_has_move_id is true for less
172 // than one client step, i.e. press and release happen in the same step.
173 bool m_had_move_id = false;
175
176 bool m_has_joystick_id = false;
179 float m_joystick_direction = 0.0f; // assume forward
180 float m_joystick_speed = 0.0f; // no movement
182 std::shared_ptr<IGUIImage> m_joystick_btn_off;
183 std::shared_ptr<IGUIImage> m_joystick_btn_bg;
184 std::shared_ptr<IGUIImage> m_joystick_btn_center;
185
186 std::vector<button_info> m_buttons;
187 std::shared_ptr<IGUIImage> m_overflow_btn;
188
189 bool m_overflow_open = false;
190 std::shared_ptr<IGUIStaticText> m_overflow_bg;
191 std::vector<button_info> m_overflow_buttons;
192 std::vector<std::shared_ptr<IGUIStaticText>> m_overflow_button_titles;
193 std::vector<recti> m_overflow_button_rects;
194
195 std::shared_ptr<IGUIStaticText> m_status_text;
196
197 // Note: TouchControls intentionally uses IGUIImage instead of IGUIButton
198 // for its buttons. We only want static image display, not interactivity,
199 // from Irrlicht.
200
201 void emitKeyboardEvent(KeyPress keycode, bool pressed);
202
203 void loadButtonTexture(IGUIImage *gui_button, const std::string &path);
204 void buttonEmitAction(button_info &btn, bool action);
205
206 bool buttonsHandlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element);
207 bool buttonsHandleRelease(std::vector<button_info> &buttons, size_t pointer_id);
208 bool buttonsStep(std::vector<button_info> &buttons, float dtime);
209
210 void toggleOverflowMenu();
211 void updateVisibility();
212 void releaseAll();
213
214 // initialize a button
216 void addButton(std::vector<button_info> &buttons,
217 touch_gui_button_id id, const std::string &image,
218 const recti &rect, bool visible);
219 void addToggleButton(std::vector<button_info> &buttons,
221 const std::string &image_1, const std::string &image_2,
222 const recti &rect, bool visible);
223
225 const recti &rect, bool visible);
226
227 // handle pressing hotbar items
228 bool isHotbarButton(const SEvent &event);
229
230 // handle release event
231 void handleReleaseEvent(size_t pointer_id);
232
233 // apply joystick status
234 void applyJoystickStatus();
235
236 // map to store the IDs and original positions of currently pressed pointers
237 std::unordered_map<size_t, v2s32> m_pointer_downpos;
238 // map to store the IDs and positions of currently pressed pointers
239 std::unordered_map<size_t, v2s32> m_pointer_pos;
240
241 // The following are not used if m_interaction_style == BUTTONS_CROSSHAIR
242
245
246 bool m_dig_pressed = false;
248
249 bool m_place_pressed = false;
251};
252
Definition texturesource.h:34
Definition keycode.h:17
Definition touchcontrols.h:70
bool m_has_joystick_id
Definition touchcontrols.h:176
void registerHotbarRect(u16 index, const recti &rect)
Definition touchcontrols.cpp:711
void addButton(std::vector< button_info > &buttons, touch_gui_button_id id, const std::string &image, const recti &rect, bool visible)
Definition touchcontrols.cpp:379
bool m_dig_pressed
Definition touchcontrols.h:246
DISABLE_CLASS_COPY(TouchControls)
void applyContextControls(const TouchInteractionMode &mode)
Definition touchcontrols.cpp:783
IGUIImage * makeButtonDirect(touch_gui_button_id id, const recti &rect, bool visible)
Definition touchcontrols.cpp:402
v2s32 m_move_pos
Definition touchcontrols.h:170
void show()
Definition touchcontrols.cpp:778
bool m_visible
Definition touchcontrols.h:128
void updateVisibility()
Definition touchcontrols.cpp:737
TapState m_tap_state
Definition touchcontrols.h:244
double m_camera_pitch_change
Definition touchcontrols.h:152
~TouchControls()
Definition touchcontrols.cpp:365
bool mayAddButton(touch_gui_button_id id)
Definition touchcontrols.cpp:371
u64 m_move_downtime
Definition touchcontrols.h:166
IGUIStaticText * getStatusText()
Definition touchcontrols.h:121
u64 m_place_pressed_until
Definition touchcontrols.h:250
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)
Definition touchcontrols.cpp:392
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:227
bool m_fixed_joystick
Definition touchcontrols.h:138
v2u32 m_screensize
Definition touchcontrols.h:131
void readSettings()
Definition touchcontrols.cpp:251
void resetHotbarRects()
Definition touchcontrols.cpp:706
bool buttonsStep(std::vector< button_info > &buttons, float dtime)
Definition touchcontrols.cpp:119
std::optional< u16 > getHotbarSelection()
Definition touchcontrols.cpp:427
bool m_overflow_open
Definition touchcontrols.h:189
double getYawChange()
Definition touchcontrols.h:79
line3d< f32 > m_shootline
A line starting at the camera and pointing towards the selected object.
Definition touchcontrols.h:161
std::vector< button_info > m_overflow_buttons
Definition touchcontrols.h:191
float m_joystick_speed
Definition touchcontrols.h:180
u16 m_long_tap_delay
Definition touchcontrols.h:137
std::shared_ptr< IGUIStaticText > m_overflow_bg
Definition touchcontrols.h:190
std::vector< recti > m_overflow_button_rects
Definition touchcontrols.h:193
ISimpleTextureSource * m_texturesource
Definition touchcontrols.h:127
void releaseAll()
Definition touchcontrols.cpp:756
std::vector< std::shared_ptr< IGUIStaticText > > m_overflow_button_titles
Definition touchcontrols.h:192
bool m_move_prevent_short_tap
Definition touchcontrols.h:174
void buttonEmitAction(button_info &btn, bool action)
Definition touchcontrols.cpp:53
std::unordered_map< size_t, v2s32 > m_pointer_pos
Definition touchcontrols.h:239
size_t m_joystick_id
Definition touchcontrols.h:177
void applyLayout(const ButtonLayout &layout)
Definition touchcontrols.cpp:270
TouchInteractionMode m_last_mode
Definition touchcontrols.h:243
std::shared_ptr< IGUIImage > m_joystick_btn_center
Definition touchcontrols.h:184
IGUIEnvironment * m_guienv
Definition touchcontrols.h:125
bool buttonsHandlePress(std::vector< button_info > &buttons, size_t pointer_id, IGUIElement *element)
Definition touchcontrols.cpp:72
std::optional< u16 > m_hotbar_selection
Definition touchcontrols.h:148
void loadButtonTexture(IGUIImage *gui_button, const std::string &path)
Definition touchcontrols.cpp:44
IEventReceiver * m_receiver
Definition touchcontrols.h:126
float getJoystickSpeed()
Definition touchcontrols.h:108
static void settingChangedCallback(const std::string &name, void *data)
Definition touchcontrols.cpp:246
std::shared_ptr< IGUIImage > m_overflow_btn
Definition touchcontrols.h:187
void translateEvent(const SEvent &event)
Definition touchcontrols.cpp:480
s32 m_button_size
Definition touchcontrols.h:132
void setVisible(bool visible)
Definition touchcontrols.cpp:716
std::shared_ptr< IGUIImage > m_joystick_btn_bg
Definition touchcontrols.h:183
std::unordered_map< size_t, v2s32 > m_pointer_downpos
Definition touchcontrols.h:237
bool isHotbarButton(const SEvent &event)
Definition touchcontrols.cpp:412
IrrlichtDevice * m_device
Definition touchcontrols.h:124
void handleReleaseEvent(size_t pointer_id)
Definition touchcontrols.cpp:434
void applyJoystickStatus()
Definition touchcontrols.cpp:652
void toggleOverflowMenu()
Definition touchcontrols.cpp:729
bool m_had_move_id
Definition touchcontrols.h:173
size_t m_move_id
Definition touchcontrols.h:164
std::shared_ptr< IGUIStaticText > m_status_text
Definition touchcontrols.h:195
double m_touchscreen_threshold
Definition touchcontrols.h:136
std::shared_ptr< IGUIImage > m_joystick_btn_off
Definition touchcontrols.h:182
line3d< f32 > getShootline()
Returns a line which describes what the player is pointing at.
Definition touchcontrols.h:105
u64 m_dig_pressed_until
Definition touchcontrols.h:247
std::vector< button_info > m_buttons
Definition touchcontrols.h:186
void hide()
Definition touchcontrols.cpp:773
bool isShootlineAvailable()
Definition touchcontrols.h:92
ButtonLayout m_layout
Definition touchcontrols.h:144
std::unordered_map< u16, recti > m_hotbar_rects
Definition touchcontrols.h:147
float m_joystick_direction
Definition touchcontrols.h:179
float getJoystickDirection()
Definition touchcontrols.h:107
bool isStatusTextOverriden()
Definition touchcontrols.h:120
void emitKeyboardEvent(KeyPress keycode, bool pressed)
Definition touchcontrols.cpp:31
bool m_joystick_status_aux1
Definition touchcontrols.h:181
bool buttonsHandleRelease(std::vector< button_info > &buttons, size_t pointer_id)
Definition touchcontrols.cpp:98
bool m_move_has_really_moved
Definition touchcontrols.h:165
void step(float dtime)
Definition touchcontrols.cpp:662
double getPitchChange()
Definition touchcontrols.h:86
double m_camera_yaw_change
Definition touchcontrols.h:151
bool m_place_pressed
Definition touchcontrols.h:249
bool m_joystick_has_really_moved
Definition touchcontrols.h:178
bool m_joystick_triggers_aux1
Definition touchcontrols.h:139
TouchInteractionStyle m_interaction_style
Definition touchcontrols.h:135
bool m_has_move_id
Definition touchcontrols.h:163
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
core::vector2d< u32 > v2u32
Definition irr_v2d.h:14
TouchInteractionMode
Definition itemdef.h:38
@ TouchInteractionMode_END
Definition itemdef.h:42
Definition printing.h:10
Definition fontengine.h:16
Definition touchscreenlayout.h:85
Definition touchcontrols.h:53
std::string toggle_textures[2]
Definition touchcontrols.h:65
enum button_info::@20 toggleable
std::shared_ptr< IGUIImage > gui_button
Definition touchcontrols.h:58
@ SECOND_TEXTURE
Definition touchcontrols.h:63
@ NOT_TOGGLEABLE
Definition touchcontrols.h:61
@ FIRST_TEXTURE
Definition touchcontrols.h:62
KeyPress keypress
Definition touchcontrols.h:56
std::vector< size_t > pointer_ids
Definition touchcontrols.h:57
float repeat_counter
Definition touchcontrols.h:55
touch_gui_button_id id
Definition touchcontrols.h:54
TouchControls * g_touchcontrols
Definition touchcontrols.cpp:29
TapState
Definition touchcontrols.h:36
TouchInteractionStyle
Definition touchscreenlayout.h:25
@ TAP
Definition touchscreenlayout.h:26
touch_gui_button_id
Definition touchscreenlayout.h:33