Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
touchcontrols.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
29#include <memory>
30#include <optional>
31#include <unordered_map>
32#include <vector>
33
34#include "itemdef.h"
35#include "client/game.h"
36
37namespace irr
38{
39 class IrrlichtDevice;
40}
41
42using namespace irr;
43using namespace irr::core;
44using namespace irr::gui;
45
46
47// We cannot use irr_ptr for Irrlicht GUI elements we own.
48// Option 1: Pass IGUIElement* returned by IGUIEnvironment::add* into irr_ptr
49// constructor.
50// -> We steal the reference owned by IGUIEnvironment and drop it later,
51// causing the IGUIElement to be deleted while IGUIEnvironment still
52// references it.
53// Option 2: Pass IGUIElement* returned by IGUIEnvironment::add* into irr_ptr::grab.
54// -> We add another reference and drop it later, but since IGUIEnvironment
55// still references the IGUIElement, it is never deleted.
56// To make IGUIEnvironment drop its reference to the IGUIElement, we have to call
57// IGUIElement::remove, so that's what we'll do.
58template <typename T>
59std::shared_ptr<T> grab_gui_element(T *element)
60{
61 static_assert(std::is_base_of_v<IGUIElement, T>,
62 "grab_gui_element only works for IGUIElement");
63 return std::shared_ptr<T>(element, [](T *e) {
64 e->remove();
65 });
66}
67
68
69enum class TapState
70{
71 None,
73 LongTap,
74};
75
105
106
107#define BUTTON_REPEAT_DELAY 0.5f
108#define BUTTON_REPEAT_INTERVAL 0.333f
109
110// Our simulated clicks last some milliseconds so that server-side mods have a
111// chance to detect them via l_get_player_control.
112// If you tap faster than this value, the simulated clicks are of course shorter.
113#define SIMULATED_CLICK_DURATION_MS 50
114
115
117{
119 EKEY_CODE keycode;
120 std::vector<size_t> pointer_ids;
121 std::shared_ptr<IGUIImage> gui_button = nullptr;
122
123 enum {
128 std::string toggle_textures[2];
129
130 void emitAction(bool action, video::IVideoDriver *driver,
131 IEventReceiver *receiver, ISimpleTextureSource *tsrc);
132};
133
134
136{
137public:
138 TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
139
140 void translateEvent(const SEvent &event);
142
144 {
145 double res = m_camera_yaw_change;
147 return res;
148 }
149
150 double getPitchChange() {
151 double res = m_camera_pitch_change;
153 return res;
154 }
155
164 line3d<f32> getShootline() { return m_shootline; }
165
168
169 void step(float dtime);
170 inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
171
172 void setVisible(bool visible);
173 void hide();
174 void show();
175
176 void resetHotbarRects();
177 void registerHotbarRect(u16 index, const recti &rect);
178 std::optional<u16> getHotbarSelection();
179
180private:
181 IrrlichtDevice *m_device = nullptr;
182 IGUIEnvironment *m_guienv = nullptr;
183 IEventReceiver *m_receiver = nullptr;
189 bool m_visible = true;
190
191 std::unordered_map<u16, recti> m_hotbar_rects;
192 std::optional<u16> m_hotbar_selection = std::nullopt;
193
194 // value in degree
197
203 line3d<f32> m_shootline;
204
205 bool m_has_move_id = false;
206 size_t m_move_id;
209 // m_move_pos stays valid even after m_move_id has been released.
211 // This is needed so that we don't miss if m_has_move_id is true for less
212 // than one client step, i.e. press and release happen in the same step.
213 bool m_had_move_id = false;
215
216 bool m_has_joystick_id = false;
219 float m_joystick_direction = 0.0f; // assume forward
220 float m_joystick_speed = 0.0f; // no movement
222 bool m_fixed_joystick = false;
224 bool m_draw_crosshair = false;
225 std::shared_ptr<IGUIImage> m_joystick_btn_off;
226 std::shared_ptr<IGUIImage> m_joystick_btn_bg;
227 std::shared_ptr<IGUIImage> m_joystick_btn_center;
228
229 std::vector<button_info> m_buttons;
230 std::shared_ptr<IGUIImage> m_overflow_btn;
231
232 bool m_overflow_open = false;
233 std::shared_ptr<IGUIStaticText> m_overflow_bg;
234 std::vector<button_info> m_overflow_buttons;
235 std::vector<std::shared_ptr<IGUIStaticText>> m_overflow_button_titles;
236 std::vector<recti> m_overflow_button_rects;
237
238 void toggleOverflowMenu();
239 void updateVisibility();
240 void releaseAll();
241
242 // initialize a button
243 void addButton(std::vector<button_info> &buttons,
244 touch_gui_button_id id, const std::string &image,
245 const recti &rect, bool visible=true);
246 void addToggleButton(std::vector<button_info> &buttons,
248 const std::string &image_1, const std::string &image_2,
249 const recti &rect, bool visible=true);
250
252 const recti &rect, bool visible);
253
254 // handle pressing hotbar items
255 bool isHotbarButton(const SEvent &event);
256
257 // handle release event
258 void handleReleaseEvent(size_t pointer_id);
259
260 // apply joystick status
261 void applyJoystickStatus();
262
263 // map to store the IDs and original positions of currently pressed pointers
264 std::unordered_map<size_t, v2s32> m_pointer_downpos;
265 // map to store the IDs and positions of currently pressed pointers
266 std::unordered_map<size_t, v2s32> m_pointer_pos;
267
269 void emitMouseEvent(EMOUSE_INPUT_EVENT type);
272
273 bool m_dig_pressed = false;
275
276 bool m_place_pressed = false;
278};
279
Definition texturesource.h:34
Definition touchcontrols.h:136
bool m_has_joystick_id
Definition touchcontrols.h:216
void registerHotbarRect(u16 index, const recti &rect)
Definition touchcontrols.cpp:766
bool m_dig_pressed
Definition touchcontrols.h:273
void applyContextControls(const TouchInteractionMode &mode)
Definition touchcontrols.cpp:859
IGUIImage * makeButtonDirect(touch_gui_button_id id, const recti &rect, bool visible)
Definition touchcontrols.cpp:440
v2s32 m_move_pos
Definition touchcontrols.h:210
void show()
Definition touchcontrols.cpp:830
bool m_visible
Definition touchcontrols.h:189
void updateVisibility()
Definition touchcontrols.cpp:792
TapState m_tap_state
Definition touchcontrols.h:271
double m_camera_pitch_change
Definition touchcontrols.h:196
u64 m_move_downtime
Definition touchcontrols.h:208
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:430
u64 m_place_pressed_until
Definition touchcontrols.h:277
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:277
bool m_fixed_joystick
Definition touchcontrols.h:222
v2u32 m_screensize
Definition touchcontrols.h:185
void resetHotbarRects()
Definition touchcontrols.cpp:761
std::optional< u16 > getHotbarSelection()
Definition touchcontrols.cpp:466
bool m_overflow_open
Definition touchcontrols.h:232
double getYawChange()
Definition touchcontrols.h:143
line3d< f32 > m_shootline
A line starting at the camera and pointing towards the selected object.
Definition touchcontrols.h:203
std::vector< button_info > m_overflow_buttons
Definition touchcontrols.h:234
float m_joystick_speed
Definition touchcontrols.h:220
u16 m_long_tap_delay
Definition touchcontrols.h:188
std::shared_ptr< IGUIStaticText > m_overflow_bg
Definition touchcontrols.h:233
std::vector< recti > m_overflow_button_rects
Definition touchcontrols.h:236
v2s32 getPointerPos()
Definition touchcontrols.cpp:835
ISimpleTextureSource * m_texturesource
Definition touchcontrols.h:184
float getMovementSpeed()
Definition touchcontrols.h:167
void releaseAll()
Definition touchcontrols.cpp:808
std::vector< std::shared_ptr< IGUIStaticText > > m_overflow_button_titles
Definition touchcontrols.h:235
bool m_move_prevent_short_tap
Definition touchcontrols.h:214
void emitMouseEvent(EMOUSE_INPUT_EVENT type)
Definition touchcontrols.cpp:844
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:417
std::unordered_map< size_t, v2s32 > m_pointer_pos
Definition touchcontrols.h:266
size_t m_joystick_id
Definition touchcontrols.h:217
float getMovementDirection()
Definition touchcontrols.h:166
TouchInteractionMode m_last_mode
Definition touchcontrols.h:270
std::shared_ptr< IGUIImage > m_joystick_btn_center
Definition touchcontrols.h:227
IGUIEnvironment * m_guienv
Definition touchcontrols.h:182
std::optional< u16 > m_hotbar_selection
Definition touchcontrols.h:192
IEventReceiver * m_receiver
Definition touchcontrols.h:183
std::shared_ptr< IGUIImage > m_overflow_btn
Definition touchcontrols.h:230
void setUseCrosshair(bool use_crosshair)
Definition touchcontrols.h:170
void translateEvent(const SEvent &event)
Definition touchcontrols.cpp:525
s32 m_button_size
Definition touchcontrols.h:186
void setVisible(bool visible)
Definition touchcontrols.cpp:771
std::shared_ptr< IGUIImage > m_joystick_btn_bg
Definition touchcontrols.h:226
std::unordered_map< size_t, v2s32 > m_pointer_downpos
Definition touchcontrols.h:264
bool isHotbarButton(const SEvent &event)
Definition touchcontrols.cpp:451
IrrlichtDevice * m_device
Definition touchcontrols.h:181
void handleReleaseEvent(size_t pointer_id)
Definition touchcontrols.cpp:473
bool m_draw_crosshair
Definition touchcontrols.h:224
void applyJoystickStatus()
Definition touchcontrols.cpp:706
void toggleOverflowMenu()
Definition touchcontrols.cpp:785
bool m_had_move_id
Definition touchcontrols.h:213
size_t m_move_id
Definition touchcontrols.h:206
double m_touchscreen_threshold
Definition touchcontrols.h:187
std::shared_ptr< IGUIImage > m_joystick_btn_off
Definition touchcontrols.h:225
line3d< f32 > getShootline()
Returns a line which describes what the player is pointing at.
Definition touchcontrols.h:164
u64 m_dig_pressed_until
Definition touchcontrols.h:274
std::vector< button_info > m_buttons
Definition touchcontrols.h:229
void hide()
Definition touchcontrols.cpp:825
std::unordered_map< u16, recti > m_hotbar_rects
Definition touchcontrols.h:191
float m_joystick_direction
Definition touchcontrols.h:219
bool m_joystick_status_aux1
Definition touchcontrols.h:221
bool m_move_has_really_moved
Definition touchcontrols.h:207
void step(float dtime)
Definition touchcontrols.cpp:722
double getPitchChange()
Definition touchcontrols.h:150
double m_camera_yaw_change
Definition touchcontrols.h:195
bool m_place_pressed
Definition touchcontrols.h:276
bool m_joystick_has_really_moved
Definition touchcontrols.h:218
bool m_joystick_triggers_aux1
Definition touchcontrols.h:223
bool m_has_move_id
Definition touchcontrols.h:205
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 core.h:25
Definition touchcontrols.h:117
std::string toggle_textures[2]
Definition touchcontrols.h:128
enum button_info::@20 toggleable
std::shared_ptr< IGUIImage > gui_button
Definition touchcontrols.h:121
EKEY_CODE keycode
Definition touchcontrols.h:119
void emitAction(bool action, video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
Definition touchcontrols.cpp:108
@ SECOND_TEXTURE
Definition touchcontrols.h:126
@ NOT_TOGGLEABLE
Definition touchcontrols.h:124
@ FIRST_TEXTURE
Definition touchcontrols.h:125
std::vector< size_t > pointer_ids
Definition touchcontrols.h:120
float repeat_counter
Definition touchcontrols.h:118
TouchControls * g_touchcontrols
Definition touchcontrols.cpp:43
touch_gui_button_id
Definition touchcontrols.h:77
@ joystick_center_id
Definition touchcontrols.h:103
@ fast_id
Definition touchcontrols.h:87
@ chat_id
Definition touchcontrols.h:95
@ inventory_id
Definition touchcontrols.h:96
@ debug_id
Definition touchcontrols.h:88
@ range_id
Definition touchcontrols.h:90
@ zoom_id
Definition touchcontrols.h:80
@ exit_id
Definition touchcontrols.h:98
@ aux1_id
Definition touchcontrols.h:81
@ fly_id
Definition touchcontrols.h:85
@ minimap_id
Definition touchcontrols.h:91
@ camera_id
Definition touchcontrols.h:89
@ drop_id
Definition touchcontrols.h:97
@ jump_id
Definition touchcontrols.h:78
@ joystick_off_id
Definition touchcontrols.h:101
@ toggle_chat_id
Definition touchcontrols.h:92
@ joystick_bg_id
Definition touchcontrols.h:102
@ sneak_id
Definition touchcontrols.h:79
@ noclip_id
Definition touchcontrols.h:86
@ overflow_id
Definition touchcontrols.h:82
std::shared_ptr< T > grab_gui_element(T *element)
Definition touchcontrols.h:59
TapState
Definition touchcontrols.h:70