Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
touchscreenlayout.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2024 grorp, Gregor Parzefall <grorp@posteo.de>
4
5#pragma once
6
7#include "irr_ptr.h"
9#include "rect.h"
10#include "util/enum_string.h"
11#include <iostream>
12#include <unordered_map>
13
15namespace gui
16{
17 class IGUIStaticText;
18}
19namespace video
20{
21 class ITexture;
22}
23
30extern const struct EnumString es_TouchInteractionStyle[];
31
66
67extern const char *button_names[];
68extern const char *button_titles[];
69extern const char *button_image_names[];
70
71struct ButtonMeta {
72 // Position, specified as a percentage of the screensize in the range [0,1].
73 // The editor currently writes the values 0, 0.5 and 1.
75 // Offset, multiplied by the global button size before it is applied.
76 // Together, position and offset define the position of the button's center.
78
79 // Returns the button's effective center position in pixels.
80 v2s32 getPos(v2u32 screensize, s32 button_size) const;
81 // Sets the button's effective center position in pixels.
82 void setPos(v2s32 pos, v2u32 screensize, s32 button_size);
83};
84
86 using ButtonMap = std::unordered_map<touch_gui_button_id, ButtonMeta>;
87
88 static bool isButtonValid(touch_gui_button_id id);
91 static s32 getButtonSize(v2u32 screensize);
92
93 // Returns the default layout.
94 // Note: Indirectly depends on settings.
96 // Reads the layout from the "touch_layout" setting. Falls back to loadDefault
97 // if the setting is absent or invalid.
98 // Note: Indirectly depends on additional settings.
100
101 static video::ITexture *getTexture(touch_gui_button_id btn, ISimpleTextureSource *tsrc);
102 static void clearTextureCache();
103
105
106 // Contains disallowed buttons that have been loaded.
107 // These are only preserved to be saved again later.
108 // This exists to prevent data loss: If you edit the layout while some button
109 // is temporarily disallowed, this prevents that button's custom position
110 // from being lost. See isButtonAllowed.
111 // This may result in overlapping buttons when the buttons are allowed again
112 // (could be fixed by resolving collisions in postProcessLoaded).
114
115 core::recti getRect(touch_gui_button_id btn,
116 v2u32 screensize, s32 button_size, ISimpleTextureSource *tsrc);
117
118 std::vector<touch_gui_button_id> getMissingButtons();
119
120 void serializeJson(std::ostream &os) const;
121
122private:
123 static const ButtonMap default_data;
124 static ButtonMap deserializeJson(std::istream &is);
125 static ButtonLayout postProcessLoaded(const ButtonMap &map);
126
127 static std::unordered_map<touch_gui_button_id, irr_ptr<video::ITexture>> texture_cache;
128};
129
130void layout_button_grid(v2u32 screensize, ISimpleTextureSource *tsrc,
131 const std::vector<touch_gui_button_id> &buttons,
132 // pos refers to the center of the button.
133 const std::function<void(touch_gui_button_id btn, v2s32 pos, core::recti rect)> &callback);
134
135void make_button_grid_title(gui::IGUIStaticText *text,
136 touch_gui_button_id btn,v2s32 pos, core::recti rect);
Definition texturesource.h:34
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector2d< u32 > v2u32
Definition irr_v2d.h:14
Definition fontengine.h:16
Definition clientmap.h:36
Definition touchscreenlayout.h:85
static bool isButtonValid(touch_gui_button_id id)
Definition touchscreenlayout.cpp:142
static ButtonLayout loadFromSettings()
Definition touchscreenlayout.cpp:220
static void clearTextureCache()
Definition touchscreenlayout.cpp:254
void serializeJson(std::ostream &os) const
Definition touchscreenlayout.cpp:282
static ButtonLayout postProcessLoaded(const ButtonMap &map)
Definition touchscreenlayout.cpp:202
static s32 getButtonSize(v2u32 screensize)
Definition touchscreenlayout.cpp:164
static video::ITexture * getTexture(touch_gui_button_id btn, ISimpleTextureSource *tsrc)
Definition touchscreenlayout.cpp:238
static ButtonMap deserializeJson(std::istream &is)
Definition touchscreenlayout.cpp:312
std::unordered_map< touch_gui_button_id, ButtonMeta > ButtonMap
Definition touchscreenlayout.h:86
static const ButtonMap default_data
Definition touchscreenlayout.h:171
core::recti getRect(touch_gui_button_id btn, v2u32 screensize, s32 button_size, ISimpleTextureSource *tsrc)
Definition touchscreenlayout.cpp:259
static bool isButtonAllowed(touch_gui_button_id id)
Definition touchscreenlayout.cpp:150
ButtonMap layout
Definition touchscreenlayout.h:104
static std::unordered_map< touch_gui_button_id, irr_ptr< video::ITexture > > texture_cache
Definition touchscreenlayout.h:127
static ButtonLayout loadDefault()
Definition touchscreenlayout.cpp:215
static bool isButtonRequired(touch_gui_button_id id)
Definition touchscreenlayout.cpp:159
ButtonMap preserved_disallowed
Definition touchscreenlayout.h:113
std::vector< touch_gui_button_id > getMissingButtons()
Definition touchscreenlayout.cpp:271
Definition touchscreenlayout.h:71
v2f position
Definition touchscreenlayout.h:74
void setPos(v2s32 pos, v2u32 screensize, s32 button_size)
Definition touchscreenlayout.cpp:120
v2s32 getPos(v2u32 screensize, s32 button_size) const
Definition touchscreenlayout.cpp:114
v2f offset
Definition touchscreenlayout.h:77
Definition enum_string.h:11
TouchInteractionStyle
Definition touchscreenlayout.h:25
@ TAP
Definition touchscreenlayout.h:26
@ TAP_CROSSHAIR
Definition touchscreenlayout.h:27
@ BUTTONS_CROSSHAIR
Definition touchscreenlayout.h:28
const char * button_titles[]
Definition touchscreenlayout.cpp:55
const char * button_image_names[]
Definition touchscreenlayout.cpp:84
const char * button_names[]
Definition touchscreenlayout.cpp:25
const struct EnumString es_TouchInteractionStyle[]
Definition touchscreenlayout.cpp:17
touch_gui_button_id
Definition touchscreenlayout.h:33
@ joystick_center_id
Definition touchscreenlayout.h:62
@ fast_id
Definition touchscreenlayout.h:51
@ chat_id
Definition touchscreenlayout.h:44
@ place_id
Definition touchscreenlayout.h:35
@ inventory_id
Definition touchscreenlayout.h:45
@ debug_id
Definition touchscreenlayout.h:53
@ range_id
Definition touchscreenlayout.h:55
@ zoom_id
Definition touchscreenlayout.h:39
@ exit_id
Definition touchscreenlayout.h:47
@ aux1_id
Definition touchscreenlayout.h:40
@ fly_id
Definition touchscreenlayout.h:50
@ minimap_id
Definition touchscreenlayout.h:56
@ camera_id
Definition touchscreenlayout.h:54
@ touch_gui_button_id_END
Definition touchscreenlayout.h:64
@ drop_id
Definition touchscreenlayout.h:46
@ jump_id
Definition touchscreenlayout.h:37
@ joystick_off_id
Definition touchscreenlayout.h:60
@ toggle_chat_id
Definition touchscreenlayout.h:57
@ joystick_bg_id
Definition touchscreenlayout.h:61
@ dig_id
Definition touchscreenlayout.h:34
@ sneak_id
Definition touchscreenlayout.h:38
@ noclip_id
Definition touchscreenlayout.h:52
@ overflow_id
Definition touchscreenlayout.h:41
void layout_button_grid(v2u32 screensize, ISimpleTextureSource *tsrc, const std::vector< touch_gui_button_id > &buttons, const std::function< void(touch_gui_button_id btn, v2s32 pos, core::recti rect)> &callback)
Definition touchscreenlayout.cpp:367
void make_button_grid_title(gui::IGUIStaticText *text, touch_gui_button_id btn, v2s32 pos, core::recti rect)
Definition touchscreenlayout.cpp:405