Skip to content

Commit

Permalink
Implement an editor to customize the touchscreen controls (luanti-org…
Browse files Browse the repository at this point in the history
…#14933)

- The editor is accessible via the pause menu and the settings menu.
- Buttons can be moved via drag & drop.
- Buttons can be added/removed. The grid menu added by luanti-org#14918 is used to show
  all buttons not included in the layout.
- Custom layouts are responsive and adapt to changed screen size / DPI /
  hud_scaling.
- The layout is saved as JSON in the "touch_layout" setting.
  • Loading branch information
grorp authored Nov 24, 2024
1 parent 4faa16f commit 6a1d22b
Show file tree
Hide file tree
Showing 18 changed files with 1,124 additions and 197 deletions.
18 changes: 18 additions & 0 deletions builtin/mainmenu/settings/dlg_settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ local function load()
end,
}

local touchscreen_layout = {
query_text = "Touchscreen layout",
requires = {
touchscreen = true,
},
get_formspec = function(self, avail_w)
local btn_w = math.min(avail_w, 6)
return ("button[0,0;%f,0.8;btn_touch_layout;%s]"):format(btn_w, fgettext("Touchscreen layout")), 0.8
end,
on_submit = function(self, fields)
if fields.btn_touch_layout then
core.show_touchscreen_layout()
end
end,
}

add_page({
id = "accessibility",
title = fgettext_ne("Accessibility"),
Expand Down Expand Up @@ -151,6 +167,8 @@ local function load()
load_settingtypes()

table.insert(page_by_id.controls_keyboard_and_mouse.content, 1, change_keys)
-- insert after "touch_controls"
table.insert(page_by_id.controls_touchscreen.content, 2, touchscreen_layout)
do
local content = page_by_id.graphics_and_audio_effects.content
local idx = table.indexof(content, "enable_dynamic_shadows")
Expand Down
1 change: 1 addition & 0 deletions doc/menu_lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ GUI
* `core.set_clouds(<true/false>)`
* `core.set_topleft_text(text)`
* `core.show_keys_menu()`
* `core.show_touchscreen_layout()`
* `core.show_path_select_dialog(formname, caption, is_file_select)`
* shows a path select dialog
* `formname` is base name of dialog response returned in fields
Expand Down
21 changes: 19 additions & 2 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "client/event_manager.h"
#include "fontengine.h"
#include "gui/touchcontrols.h"
#include "gui/touchscreeneditor.h"
#include "itemdef.h"
#include "log.h"
#include "log_internal.h"
Expand Down Expand Up @@ -144,6 +145,11 @@ struct LocalFormspecHandler : public TextDest
return;
}

if (fields.find("btn_touchscreen_layout") != fields.end()) {
g_gamecallback->touchscreenLayout();
return;
}

if (fields.find("btn_exit_menu") != fields.end()) {
g_gamecallback->disconnect();
return;
Expand Down Expand Up @@ -1844,6 +1850,12 @@ inline bool Game::handleCallbacks()
g_gamecallback->keyconfig_requested = false;
}

if (g_gamecallback->touchscreenlayout_requested) {
(new GUITouchscreenLayout(guienv, guiroot, -1,
&g_menumgr, texture_src))->drop();
g_gamecallback->touchscreenlayout_requested = false;
}

if (!g_gamecallback->show_open_url_dialog.empty()) {
(void)make_irr<GUIOpenURLMenu>(guienv, guiroot, -1,
&g_menumgr, texture_src, g_gamecallback->show_open_url_dialog);
Expand Down Expand Up @@ -4510,9 +4522,14 @@ void Game::showPauseMenu()
<< strgettext("Sound Volume") << "]";
}
#endif
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_key_config;"
<< strgettext("Controls") << "]";
#endif
if (g_touchcontrols) {
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_touchscreen_layout;"
<< strgettext("Touchscreen Layout") << "]";
} else {
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_key_config;"
<< strgettext("Controls") << "]";
}
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_menu;"
<< strgettext("Exit to Menu") << "]";
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_os;"
Expand Down
1 change: 1 addition & 0 deletions src/client/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "wieldmesh.h"
#include "client/renderingengine.h"
#include "client/minimap.h"
#include "client/texturesource.h"
#include "gui/touchcontrols.h"
#include "util/enriched_string.h"
#include "irrlicht_changes/CGUITTFont.h"
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ void set_default_settings()
settings->setDefault("keymap_sneak", "KEY_SHIFT");
#endif

settings->setDefault("touch_layout", "");
settings->setDefault("touchscreen_sensitivity", "0.2");
settings->setDefault("touchscreen_threshold", "20");
settings->setDefault("touch_long_tap_delay", "400");
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/modalMenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp
${CMAKE_CURRENT_SOURCE_DIR}/touchcontrols.cpp
${CMAKE_CURRENT_SOURCE_DIR}/touchscreenlayout.cpp
${CMAKE_CURRENT_SOURCE_DIR}/touchscreeneditor.cpp
PARENT_SCOPE
)
7 changes: 7 additions & 0 deletions src/gui/mainmenumanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class IGameCallback
virtual void changeVolume() = 0;
virtual void showOpenURLDialog(const std::string &url) = 0;
virtual void signalKeyConfigChange() = 0;
virtual void touchscreenLayout() = 0;
};

extern gui::IGUIEnvironment *guienv;
Expand Down Expand Up @@ -133,6 +134,11 @@ class MainGameCallback : public IGameCallback
keyconfig_changed = true;
}

void touchscreenLayout() override
{
touchscreenlayout_requested = true;
}

void showOpenURLDialog(const std::string &url) override
{
show_open_url_dialog = url;
Expand All @@ -142,6 +148,7 @@ class MainGameCallback : public IGameCallback
bool changepassword_requested = false;
bool changevolume_requested = false;
bool keyconfig_requested = false;
bool touchscreenlayout_requested = false;
bool shutdown_requested = false;
bool keyconfig_changed = false;
std::string show_open_url_dialog = "";
Expand Down
Loading

0 comments on commit 6a1d22b

Please sign in to comment.