Luanti 5.11.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
20namespace irr
21{
22 class IrrlichtDevice;
23 namespace gui
24 {
25 class IGUIEnvironment;
26 class IGUIImage;
27 class IGUIStaticText;
28 }
29 namespace video
30 {
31 class IVideoDriver;
32 }
33}
35
36using namespace irr::core;
37using namespace irr::gui;
38
39
40enum class TapState
41{
42 None,
44 LongTap,
45};
46
47
48#define BUTTON_REPEAT_DELAY 0.5f
49#define BUTTON_REPEAT_INTERVAL 0.333f
50
51// Our simulated clicks last some milliseconds so that server-side mods have a
52// chance to detect them via l_get_player_control.
53// If you tap faster than this value, the simulated clicks are of course shorter.
54#define SIMULATED_CLICK_DURATION_MS 50
55
56
58{
60 EKEY_CODE keycode;
61 std::vector<size_t> pointer_ids;
62 std::shared_ptr<IGUIImage> gui_button = nullptr;
63
64 enum {
69 std::string toggle_textures[2];
70};
71
72
74{
75public:
76 TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
79
80 void translateEvent(const SEvent &event);
82
83 double getYawChange()
84 {
85 double res = m_camera_yaw_change;
87 return res;
88 }
89
90 double getPitchChange() {
91 double res = m_camera_pitch_change;
93 return res;
94 }
95
104 line3d<f32> getShootline() { return m_shootline; }
105
108
109 void step(float dtime);
110 inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
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
124 void applyLayout(const ButtonLayout &layout);
125
126private:
127 IrrlichtDevice *m_device = nullptr;
128 IGUIEnvironment *m_guienv = nullptr;
129 IEventReceiver *m_receiver = nullptr;
135 bool m_visible = true;
136
137 std::unordered_map<u16, recti> m_hotbar_rects;
138 std::optional<u16> m_hotbar_selection = std::nullopt;
139
140 // value in degree
143
149 line3d<f32> m_shootline;
150
151 bool m_has_move_id = false;
152 size_t m_move_id;
155 // m_move_pos stays valid even after m_move_id has been released.
157 // This is needed so that we don't miss if m_has_move_id is true for less
158 // than one client step, i.e. press and release happen in the same step.
159 bool m_had_move_id = false;
161
162 bool m_has_joystick_id = false;
165 float m_joystick_direction = 0.0f; // assume forward
166 float m_joystick_speed = 0.0f; // no movement
168 bool m_fixed_joystick = false;
170 bool m_draw_crosshair = false;
171 std::shared_ptr<IGUIImage> m_joystick_btn_off;
172 std::shared_ptr<IGUIImage> m_joystick_btn_bg;
173 std::shared_ptr<IGUIImage> m_joystick_btn_center;
174
175 std::vector<button_info> m_buttons;
176 std::shared_ptr<IGUIImage> m_overflow_btn;
177
178 bool m_overflow_open = false;
179 std::shared_ptr<IGUIStaticText> m_overflow_bg;
180 std::vector<button_info> m_overflow_buttons;
181 std::vector<std::shared_ptr<IGUIStaticText>> m_overflow_button_titles;
182 std::vector<recti> m_overflow_button_rects;
183
184 std::shared_ptr<IGUIStaticText> m_status_text;
185
186 // Note: TouchControls intentionally uses IGUIImage instead of IGUIButton
187 // for its buttons. We only want static image display, not interactivity,
188 // from Irrlicht.
189
190 void emitKeyboardEvent(EKEY_CODE keycode, bool pressed);
191
192 void loadButtonTexture(IGUIImage *gui_button, const std::string &path);
193 void buttonEmitAction(button_info &btn, bool action);
194
195 bool buttonsHandlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element);
196 bool buttonsHandleRelease(std::vector<button_info> &buttons, size_t pointer_id);
197 bool buttonsStep(std::vector<button_info> &buttons, float dtime);
198
199 void toggleOverflowMenu();
200 void updateVisibility();
201 void releaseAll();
202
203 // initialize a button
205 void addButton(std::vector<button_info> &buttons,
206 touch_gui_button_id id, const std::string &image,
207 const recti &rect, bool visible);
208 void addToggleButton(std::vector<button_info> &buttons,
210 const std::string &image_1, const std::string &image_2,
211 const recti &rect, bool visible);
212
214 const recti &rect, bool visible);
215
216 // handle pressing hotbar items
217 bool isHotbarButton(const SEvent &event);
218
219 // handle release event
220 void handleReleaseEvent(size_t pointer_id);
221
222 // apply joystick status
223 void applyJoystickStatus();
224
225 // map to store the IDs and original positions of currently pressed pointers
226 std::unordered_map<size_t, v2s32> m_pointer_downpos;
227 // map to store the IDs and positions of currently pressed pointers
228 std::unordered_map<size_t, v2s32> m_pointer_pos;
229
231 void emitMouseEvent(EMOUSE_INPUT_EVENT type);
234
235 bool m_dig_pressed = false;
237
238 bool m_place_pressed = false;
240
242};
243
Definition texturesource.h:25
Definition touchcontrols.h:74
bool m_has_joystick_id
Definition touchcontrols.h:162
void registerHotbarRect(u16 index, const recti &rect)
Definition touchcontrols.cpp:660
void addButton(std::vector< button_info > &buttons, touch_gui_button_id id, const std::string &image, const recti &rect, bool visible)
Definition touchcontrols.cpp:331
bool m_dig_pressed
Definition touchcontrols.h:235
DISABLE_CLASS_COPY(TouchControls)
void applyContextControls(const TouchInteractionMode &mode)
Definition touchcontrols.cpp:757
IGUIImage * makeButtonDirect(touch_gui_button_id id, const recti &rect, bool visible)
Definition touchcontrols.cpp:353
v2s32 m_move_pos
Definition touchcontrols.h:156
void show()
Definition touchcontrols.cpp:727
bool m_visible
Definition touchcontrols.h:135
void updateVisibility()
Definition touchcontrols.cpp:686
TapState m_tap_state
Definition touchcontrols.h:233
double m_camera_pitch_change
Definition touchcontrols.h:142
~TouchControls()
Definition touchcontrols.cpp:315
bool mayAddButton(touch_gui_button_id id)
Definition touchcontrols.cpp:320
u64 m_move_downtime
Definition touchcontrols.h:154
IGUIStaticText * getStatusText()
Definition touchcontrols.h:121
u64 m_place_pressed_until
Definition touchcontrols.h:239
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:343
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:204
bool m_fixed_joystick
Definition touchcontrols.h:168
v2u32 m_screensize
Definition touchcontrols.h:131
void resetHotbarRects()
Definition touchcontrols.cpp:655
bool buttonsStep(std::vector< button_info > &buttons, float dtime)
Definition touchcontrols.cpp:113
std::optional< u16 > getHotbarSelection()
Definition touchcontrols.cpp:378
bool m_overflow_open
Definition touchcontrols.h:178
double getYawChange()
Definition touchcontrols.h:83
line3d< f32 > m_shootline
A line starting at the camera and pointing towards the selected object.
Definition touchcontrols.h:149
std::vector< button_info > m_overflow_buttons
Definition touchcontrols.h:180
float m_joystick_speed
Definition touchcontrols.h:166
u16 m_long_tap_delay
Definition touchcontrols.h:134
std::shared_ptr< IGUIStaticText > m_overflow_bg
Definition touchcontrols.h:179
std::vector< recti > m_overflow_button_rects
Definition touchcontrols.h:182
v2s32 getPointerPos()
Definition touchcontrols.cpp:732
ISimpleTextureSource * m_texturesource
Definition touchcontrols.h:130
void releaseAll()
Definition touchcontrols.cpp:705
std::vector< std::shared_ptr< IGUIStaticText > > m_overflow_button_titles
Definition touchcontrols.h:181
bool m_move_prevent_short_tap
Definition touchcontrols.h:160
void emitMouseEvent(EMOUSE_INPUT_EVENT type)
Definition touchcontrols.cpp:741
void buttonEmitAction(button_info &btn, bool action)
Definition touchcontrols.cpp:53
std::unordered_map< size_t, v2s32 > m_pointer_pos
Definition touchcontrols.h:228
size_t m_joystick_id
Definition touchcontrols.h:163
ButtonLayout getLayout()
Definition touchcontrols.h:123
void applyLayout(const ButtonLayout &layout)
Definition touchcontrols.cpp:220
TouchInteractionMode m_last_mode
Definition touchcontrols.h:232
std::shared_ptr< IGUIImage > m_joystick_btn_center
Definition touchcontrols.h:173
IGUIEnvironment * m_guienv
Definition touchcontrols.h:128
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:138
void loadButtonTexture(IGUIImage *gui_button, const std::string &path)
Definition touchcontrols.cpp:44
IEventReceiver * m_receiver
Definition touchcontrols.h:129
float getJoystickSpeed()
Definition touchcontrols.h:107
std::shared_ptr< IGUIImage > m_overflow_btn
Definition touchcontrols.h:176
void setUseCrosshair(bool use_crosshair)
Definition touchcontrols.h:110
void translateEvent(const SEvent &event)
Definition touchcontrols.cpp:430
s32 m_button_size
Definition touchcontrols.h:132
void setVisible(bool visible)
Definition touchcontrols.cpp:665
std::shared_ptr< IGUIImage > m_joystick_btn_bg
Definition touchcontrols.h:172
std::unordered_map< size_t, v2s32 > m_pointer_downpos
Definition touchcontrols.h:226
bool isHotbarButton(const SEvent &event)
Definition touchcontrols.cpp:363
IrrlichtDevice * m_device
Definition touchcontrols.h:127
void handleReleaseEvent(size_t pointer_id)
Definition touchcontrols.cpp:385
bool m_draw_crosshair
Definition touchcontrols.h:170
void applyJoystickStatus()
Definition touchcontrols.cpp:601
void toggleOverflowMenu()
Definition touchcontrols.cpp:678
bool m_had_move_id
Definition touchcontrols.h:159
size_t m_move_id
Definition touchcontrols.h:152
std::shared_ptr< IGUIStaticText > m_status_text
Definition touchcontrols.h:184
double m_touchscreen_threshold
Definition touchcontrols.h:133
std::shared_ptr< IGUIImage > m_joystick_btn_off
Definition touchcontrols.h:171
line3d< f32 > getShootline()
Returns a line which describes what the player is pointing at.
Definition touchcontrols.h:104
void emitKeyboardEvent(EKEY_CODE keycode, bool pressed)
Definition touchcontrols.cpp:32
u64 m_dig_pressed_until
Definition touchcontrols.h:236
std::vector< button_info > m_buttons
Definition touchcontrols.h:175
void hide()
Definition touchcontrols.cpp:722
ButtonLayout m_layout
Definition touchcontrols.h:241
std::unordered_map< u16, recti > m_hotbar_rects
Definition touchcontrols.h:137
float m_joystick_direction
Definition touchcontrols.h:165
float getJoystickDirection()
Definition touchcontrols.h:106
bool isStatusTextOverriden()
Definition touchcontrols.h:120
bool m_joystick_status_aux1
Definition touchcontrols.h:167
bool buttonsHandleRelease(std::vector< button_info > &buttons, size_t pointer_id)
Definition touchcontrols.cpp:95
bool m_move_has_really_moved
Definition touchcontrols.h:153
void step(float dtime)
Definition touchcontrols.cpp:611
double getPitchChange()
Definition touchcontrols.h:90
double m_camera_yaw_change
Definition touchcontrols.h:141
bool m_place_pressed
Definition touchcontrols.h:238
bool m_joystick_has_really_moved
Definition touchcontrols.h:164
bool m_joystick_triggers_aux1
Definition touchcontrols.h:169
bool m_has_move_id
Definition touchcontrols.h:151
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 touchscreenlayout.h:73
Definition touchcontrols.h:58
std::string toggle_textures[2]
Definition touchcontrols.h:69
@ SECOND_TEXTURE
Definition touchcontrols.h:67
@ NOT_TOGGLEABLE
Definition touchcontrols.h:65
@ FIRST_TEXTURE
Definition touchcontrols.h:66
std::shared_ptr< IGUIImage > gui_button
Definition touchcontrols.h:62
EKEY_CODE keycode
Definition touchcontrols.h:60
std::vector< size_t > pointer_ids
Definition touchcontrols.h:61
enum button_info::@19 toggleable
float repeat_counter
Definition touchcontrols.h:59
TouchControls * g_touchcontrols
Definition touchcontrols.cpp:30
TapState
Definition touchcontrols.h:41
touch_gui_button_id
Definition touchscreenlayout.h:24