1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Allow toggling touchscreen mode at runtime (#14075)

Signed-off-by: David Heidelberg <david@ixit.cz>
Co-authored-by: Gregor Parzefall <gregor.parzefall@posteo.de>
This commit is contained in:
David Heidelberg 2024-02-22 16:44:49 +01:00 committed by GitHub
parent e3cc26cb7c
commit 34286d77c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 175 additions and 220 deletions

View file

@ -1,8 +1,3 @@
set(extra_gui_SRCS "")
if(ENABLE_TOUCH)
set(extra_gui_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/touchscreengui.cpp)
endif()
set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiAnimatedImage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/guiBackgroundImage.cpp
@ -29,6 +24,6 @@ set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiVolumeChange.cpp
${CMAKE_CURRENT_SOURCE_DIR}/modalMenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp
${extra_gui_SRCS}
${CMAKE_CURRENT_SOURCE_DIR}/touchscreengui.cpp
PARENT_SCOPE
)

View file

@ -316,13 +316,11 @@ void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
data->invsize.Y = MYMAX(0, stof(parts[1]));
lockSize(false);
#ifndef HAVE_TOUCHSCREENGUI
if (parts.size() == 3) {
if (!g_settings->getBool("enable_touch") && parts.size() == 3) {
if (parts[2] == "true") {
lockSize(true,v2u32(800,600));
}
}
#endif
data->explicit_size = true;
return;
}
@ -3284,14 +3282,15 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
s32 min_screen_dim = std::min(padded_screensize.X, padded_screensize.Y);
#ifdef HAVE_TOUCHSCREENGUI
// In Android, the preferred imgsize should be larger to accommodate the
// smaller screensize.
double prefer_imgsize = min_screen_dim / 10 * gui_scaling;
#else
// Desktop computers have more space, so try to fit 15 coordinates.
double prefer_imgsize = min_screen_dim / 15 * gui_scaling;
#endif
double prefer_imgsize;
if (g_settings->getBool("enable_touch")) {
// The preferred imgsize should be larger to accommodate the
// smaller screensize.
prefer_imgsize = min_screen_dim / 10 * gui_scaling;
} else {
// Desktop computers have more space, so try to fit 15 coordinates.
prefer_imgsize = min_screen_dim / 15 * gui_scaling;
}
// Try to use the preferred imgsize, but if that's bigger than the maximum
// size, use the maximum size.
use_imgsize = std::min(prefer_imgsize,

View file

@ -25,10 +25,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <IGUIStaticText.h>
#include <IGUIFont.h>
#ifdef HAVE_TOUCHSCREENGUI
#include "client/renderingengine.h"
#endif
#include "porting.h"
#include "gettext.h"
@ -66,11 +62,8 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
#ifdef HAVE_TOUCHSCREENGUI
const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
#else
const float s = m_gui_scale;
#endif
DesiredRect = core::rect<s32>(
screensize.X / 2 - 580 * s / 2,
screensize.Y / 2 - 300 * s / 2,

View file

@ -27,10 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gettext.h"
#include "porting.h"
#include "settings.h"
#ifdef HAVE_TOUCHSCREENGUI
#include "touchscreengui.h"
#endif
GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent,
s32 id, IMenuManager *menumgr, bool remap_dbl_click) :
@ -44,11 +41,12 @@ GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent,
{
m_gui_scale = std::max(g_settings->getFloat("gui_scaling"), 0.5f);
const float screen_dpi_scale = RenderingEngine::getDisplayDensity();
#ifdef HAVE_TOUCHSCREENGUI
m_gui_scale *= 1.1f - 0.3f * screen_dpi_scale + 0.2f * screen_dpi_scale * screen_dpi_scale;
#else
m_gui_scale *= screen_dpi_scale;
#endif
if (g_settings->getBool("enable_touch")) {
m_gui_scale *= 1.1f - 0.3f * screen_dpi_scale + 0.2f * screen_dpi_scale * screen_dpi_scale;
} else {
m_gui_scale *= screen_dpi_scale;
}
setVisible(true);
m_menumgr->createdMenu(this);
@ -102,10 +100,8 @@ void GUIModalMenu::quitMenu()
Environment->removeFocus(this);
m_menumgr->deletingMenu(this);
this->remove();
#ifdef HAVE_TOUCHSCREENGUI
if (g_touchscreengui)
g_touchscreengui->show();
#endif
}
static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)

View file

@ -55,6 +55,7 @@ const std::string joystick_image_names[] = {
static EKEY_CODE id_to_keycode(touch_gui_button_id id)
{
EKEY_CODE code;
// ESC isn't part of the keymap.
if (id == exit_id)
return KEY_ESCAPE;
@ -110,7 +111,15 @@ static EKEY_CODE id_to_keycode(touch_gui_button_id id)
break;
}
assert(!key.empty());
return keyname_to_keycode(g_settings->get("keymap_" + key).c_str());
std::string resolved = g_settings->get("keymap_" + key);
try {
code = keyname_to_keycode(resolved.c_str());
} catch (UnknownKeycode &e) {
code = KEY_UNKNOWN;
warningstream << "TouchScreenGUI: Unknown key '" << resolved
<< "' for '" << key << "', hiding button." << std::endl;
}
return code;
}
static void load_button_texture(const button_info *btn, const std::string &path,
@ -523,13 +532,23 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
+ 0.5f * button_size),
AHBB_Dir_Right_Left, 3.0f);
m_settings_bar.addButton(fly_id, L"fly", "fly_btn.png");
m_settings_bar.addButton(noclip_id, L"noclip", "noclip_btn.png");
m_settings_bar.addButton(fast_id, L"fast", "fast_btn.png");
m_settings_bar.addButton(debug_id, L"debug", "debug_btn.png");
m_settings_bar.addButton(camera_id, L"camera", "camera_btn.png");
m_settings_bar.addButton(range_id, L"rangeview", "rangeview_btn.png");
m_settings_bar.addButton(minimap_id, L"minimap", "minimap_btn.png");
const static std::map<touch_gui_button_id, std::string> settings_bar_buttons {
{fly_id, "fly"},
{noclip_id, "noclip"},
{fast_id, "fast"},
{debug_id, "debug"},
{camera_id, "camera"},
{range_id, "rangeview"},
{minimap_id, "minimap"},
};
for (const auto &pair : settings_bar_buttons) {
if (id_to_keycode(pair.first) == KEY_UNKNOWN)
continue;
std::wstring wide = utf8_to_wide(pair.second);
m_settings_bar.addButton(pair.first, wide.c_str(),
pair.second + "_btn.png");
}
// Chat is shown by default, so chat_hide_btn.png is shown first.
m_settings_bar.addToggleButton(toggle_chat_id, L"togglechat",
@ -545,10 +564,20 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
+ 0.5f * button_size),
AHBB_Dir_Left_Right, 2.0f);
m_rare_controls_bar.addButton(chat_id, L"chat", "chat_btn.png");
m_rare_controls_bar.addButton(inventory_id, L"inv", "inventory_btn.png");
m_rare_controls_bar.addButton(drop_id, L"drop", "drop_btn.png");
m_rare_controls_bar.addButton(exit_id, L"exit", "exit_btn.png");
const static std::map<touch_gui_button_id, std::string> rare_controls_bar_buttons {
{chat_id, "chat"},
{inventory_id, "inventory"},
{drop_id, "drop"},
{exit_id, "exit"},
};
for (const auto &pair : rare_controls_bar_buttons) {
if (id_to_keycode(pair.first) == KEY_UNKNOWN)
continue;
std::wstring wide = utf8_to_wide(pair.second);
m_rare_controls_bar.addButton(pair.first, wide.c_str(),
pair.second + "_btn.png");
}
m_initialized = true;
}