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 void emitAction(bool action, video::IVideoDriver *driver,
72 IEventReceiver *receiver, ISimpleTextureSource *tsrc);
73};
74
75
77{
78public:
79 TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
82
83 void translateEvent(const SEvent &event);
85
86 double getYawChange()
87 {
88 double res = m_camera_yaw_change;
90 return res;
91 }
92
93 double getPitchChange() {
94 double res = m_camera_pitch_change;
96 return res;
97 }
98
107 line3d<f32> getShootline() { return m_shootline; }
108
111
112 void step(float dtime);
113 inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
114
115 void setVisible(bool visible);
116 void hide();
117 void show();
118
119 void resetHotbarRects();
120 void registerHotbarRect(u16 index, const recti &rect);
121 std::optional<u16> getHotbarSelection();
122
124 IGUIStaticText *getStatusText() { return m_status_text.get(); }
125
127 void applyLayout(const ButtonLayout &layout);
128
129private:
130 IrrlichtDevice *m_device = nullptr;
131 IGUIEnvironment *m_guienv = nullptr;
132 IEventReceiver *m_receiver = nullptr;
138 bool m_visible = true;
139
140 std::unordered_map<u16, recti> m_hotbar_rects;
141 std::optional<u16> m_hotbar_selection = std::nullopt;
142
143 // value in degree
146
152 line3d<f32> m_shootline;
153
154 bool m_has_move_id = false;
155 size_t m_move_id;
158 // m_move_pos stays valid even after m_move_id has been released.
160 // This is needed so that we don't miss if m_has_move_id is true for less
161 // than one client step, i.e. press and release happen in the same step.
162 bool m_had_move_id = false;
164
165 bool m_has_joystick_id = false;
168 float m_joystick_direction = 0.0f; // assume forward
169 float m_joystick_speed = 0.0f; // no movement
171 bool m_fixed_joystick = false;
173 bool m_draw_crosshair = false;
174 std::shared_ptr<IGUIImage> m_joystick_btn_off;
175 std::shared_ptr<IGUIImage> m_joystick_btn_bg;
176 std::shared_ptr<IGUIImage> m_joystick_btn_center;
177
178 std::vector<button_info> m_buttons;
179 std::shared_ptr<IGUIImage> m_overflow_btn;
180
181 bool m_overflow_open = false;
182 std::shared_ptr<IGUIStaticText> m_overflow_bg;
183 std::vector<button_info> m_overflow_buttons;
184 std::vector<std::shared_ptr<IGUIStaticText>> m_overflow_button_titles;
185 std::vector<recti> m_overflow_button_rects;
186
187 std::shared_ptr<IGUIStaticText> m_status_text;
188
189 void toggleOverflowMenu();
190 void updateVisibility();
191 void releaseAll();
192
193 // initialize a button
195 void addButton(std::vector<button_info> &buttons,
196 touch_gui_button_id id, const std::string &image,
197 const recti &rect, bool visible);
198 void addToggleButton(std::vector<button_info> &buttons,
200 const std::string &image_1, const std::string &image_2,
201 const recti &rect, bool visible);
202
204 const recti &rect, bool visible);
205
206 // handle pressing hotbar items
207 bool isHotbarButton(const SEvent &event);
208
209 // handle release event
210 void handleReleaseEvent(size_t pointer_id);
211
212 // apply joystick status
213 void applyJoystickStatus();
214
215 // map to store the IDs and original positions of currently pressed pointers
216 std::unordered_map<size_t, v2s32> m_pointer_downpos;
217 // map to store the IDs and positions of currently pressed pointers
218 std::unordered_map<size_t, v2s32> m_pointer_pos;
219
221 void emitMouseEvent(EMOUSE_INPUT_EVENT type);
224
225 bool m_dig_pressed = false;
227
228 bool m_place_pressed = false;
230
232};
233
Definition texturesource.h:25
Definition touchcontrols.h:77
bool m_has_joystick_id
Definition touchcontrols.h:165
void registerHotbarRect(u16 index, const recti &rect)
Definition touchcontrols.cpp:679
void addButton(std::vector< button_info > &buttons, touch_gui_button_id id, const std::string &image, const recti &rect, bool visible)
Definition touchcontrols.cpp:338
bool m_dig_pressed
Definition touchcontrols.h:225
DISABLE_CLASS_COPY(TouchControls)
void applyContextControls(const TouchInteractionMode &mode)
Definition touchcontrols.cpp:776
IGUIImage * makeButtonDirect(touch_gui_button_id id, const recti &rect, bool visible)
Definition touchcontrols.cpp:361
v2s32 m_move_pos
Definition touchcontrols.h:159
void show()
Definition touchcontrols.cpp:746
bool m_visible
Definition touchcontrols.h:138
void updateVisibility()
Definition touchcontrols.cpp:705
TapState m_tap_state
Definition touchcontrols.h:223
double m_camera_pitch_change
Definition touchcontrols.h:145
~TouchControls()
Definition touchcontrols.cpp:322
bool mayAddButton(touch_gui_button_id id)
Definition touchcontrols.cpp:327
u64 m_move_downtime
Definition touchcontrols.h:157
IGUIStaticText * getStatusText()
Definition touchcontrols.h:124
u64 m_place_pressed_until
Definition touchcontrols.h:229
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:351
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:211
bool m_fixed_joystick
Definition touchcontrols.h:171
v2u32 m_screensize
Definition touchcontrols.h:134
void resetHotbarRects()
Definition touchcontrols.cpp:674
std::optional< u16 > getHotbarSelection()
Definition touchcontrols.cpp:387
bool m_overflow_open
Definition touchcontrols.h:181
double getYawChange()
Definition touchcontrols.h:86
line3d< f32 > m_shootline
A line starting at the camera and pointing towards the selected object.
Definition touchcontrols.h:152
std::vector< button_info > m_overflow_buttons
Definition touchcontrols.h:183
float m_joystick_speed
Definition touchcontrols.h:169
u16 m_long_tap_delay
Definition touchcontrols.h:137
std::shared_ptr< IGUIStaticText > m_overflow_bg
Definition touchcontrols.h:182
std::vector< recti > m_overflow_button_rects
Definition touchcontrols.h:185
v2s32 getPointerPos()
Definition touchcontrols.cpp:751
ISimpleTextureSource * m_texturesource
Definition touchcontrols.h:133
void releaseAll()
Definition touchcontrols.cpp:724
std::vector< std::shared_ptr< IGUIStaticText > > m_overflow_button_titles
Definition touchcontrols.h:184
bool m_move_prevent_short_tap
Definition touchcontrols.h:163
void emitMouseEvent(EMOUSE_INPUT_EVENT type)
Definition touchcontrols.cpp:760
std::unordered_map< size_t, v2s32 > m_pointer_pos
Definition touchcontrols.h:218
size_t m_joystick_id
Definition touchcontrols.h:166
ButtonLayout getLayout()
Definition touchcontrols.h:126
void applyLayout(const ButtonLayout &layout)
Definition touchcontrols.cpp:227
TouchInteractionMode m_last_mode
Definition touchcontrols.h:222
std::shared_ptr< IGUIImage > m_joystick_btn_center
Definition touchcontrols.h:176
IGUIEnvironment * m_guienv
Definition touchcontrols.h:131
std::optional< u16 > m_hotbar_selection
Definition touchcontrols.h:141
IEventReceiver * m_receiver
Definition touchcontrols.h:132
float getJoystickSpeed()
Definition touchcontrols.h:110
std::shared_ptr< IGUIImage > m_overflow_btn
Definition touchcontrols.h:179
void setUseCrosshair(bool use_crosshair)
Definition touchcontrols.h:113
void translateEvent(const SEvent &event)
Definition touchcontrols.cpp:441
s32 m_button_size
Definition touchcontrols.h:135
void setVisible(bool visible)
Definition touchcontrols.cpp:684
std::shared_ptr< IGUIImage > m_joystick_btn_bg
Definition touchcontrols.h:175
std::unordered_map< size_t, v2s32 > m_pointer_downpos
Definition touchcontrols.h:216
bool isHotbarButton(const SEvent &event)
Definition touchcontrols.cpp:372
IrrlichtDevice * m_device
Definition touchcontrols.h:130
void handleReleaseEvent(size_t pointer_id)
Definition touchcontrols.cpp:394
bool m_draw_crosshair
Definition touchcontrols.h:173
void applyJoystickStatus()
Definition touchcontrols.cpp:614
void toggleOverflowMenu()
Definition touchcontrols.cpp:697
bool m_had_move_id
Definition touchcontrols.h:162
size_t m_move_id
Definition touchcontrols.h:155
std::shared_ptr< IGUIStaticText > m_status_text
Definition touchcontrols.h:187
double m_touchscreen_threshold
Definition touchcontrols.h:136
std::shared_ptr< IGUIImage > m_joystick_btn_off
Definition touchcontrols.h:174
line3d< f32 > getShootline()
Returns a line which describes what the player is pointing at.
Definition touchcontrols.h:107
u64 m_dig_pressed_until
Definition touchcontrols.h:226
std::vector< button_info > m_buttons
Definition touchcontrols.h:178
void hide()
Definition touchcontrols.cpp:741
ButtonLayout m_layout
Definition touchcontrols.h:231
std::unordered_map< u16, recti > m_hotbar_rects
Definition touchcontrols.h:140
float m_joystick_direction
Definition touchcontrols.h:168
float getJoystickDirection()
Definition touchcontrols.h:109
bool isStatusTextOverriden()
Definition touchcontrols.h:123
bool m_joystick_status_aux1
Definition touchcontrols.h:170
bool m_move_has_really_moved
Definition touchcontrols.h:156
void step(float dtime)
Definition touchcontrols.cpp:630
double getPitchChange()
Definition touchcontrols.h:93
double m_camera_yaw_change
Definition touchcontrols.h:144
bool m_place_pressed
Definition touchcontrols.h:228
bool m_joystick_has_really_moved
Definition touchcontrols.h:167
bool m_joystick_triggers_aux1
Definition touchcontrols.h:172
bool m_has_move_id
Definition touchcontrols.h:154
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
void emitAction(bool action, video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:42
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