This commit is contained in:
grorp 2024-05-17 13:35:52 +00:00 committed by GitHub
commit d62c91c3c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 268 additions and 171 deletions

View File

@ -19,7 +19,7 @@
local BASE_SPACING = 0.1
local function get_scroll_btn_width()
return core.settings:get_bool("enable_touch") and 0.8 or 0.5
return core.settings:get_bool("gui_touch") and 0.8 or 0.5
end
local function buttonbar_formspec(self)

View File

@ -190,7 +190,7 @@ local function get_info_formspec(text)
return table.concat({
"formspec_version[6]",
"size[15.75,9.5]",
core.settings:get_bool("enable_touch") and "padding[0.01,0.01]" or "position[0.5,0.55]",
core.settings:get_bool("gui_touch") and "padding[0.01,0.01]" or "position[0.5,0.55]",
"label[4,4.35;", text, "]",
"container[0,", H - 0.8 - 0.375, "]",
@ -221,7 +221,7 @@ local function get_formspec(dlgdata)
local formspec = {
"formspec_version[6]",
"size[15.75,9.5]",
core.settings:get_bool("enable_touch") and "padding[0.01,0.01]" or "position[0.5,0.55]",
core.settings:get_bool("gui_touch") and "padding[0.01,0.01]" or "position[0.5,0.55]",
"style[status,downloading,queued;border=false]",
@ -472,7 +472,7 @@ end
local function handle_events(event)
if event == "DialogShow" then
-- On touchscreen, don't show the "MINETEST" header behind the dialog.
mm_game_theme.set_engine(core.settings:get_bool("enable_touch"))
mm_game_theme.set_engine(core.settings:get_bool("gui_touch"))
-- If ContentDB is already loaded, auto-install packages here.
do_auto_install()

View File

@ -313,11 +313,14 @@ local function check_requirements(name, requires)
local video_driver = core.get_active_driver()
local shaders_support = video_driver == "opengl" or video_driver == "opengl3" or video_driver == "ogles2"
local touch_controls = core.settings:get("touch_controls")
local special = {
android = PLATFORM == "Android",
desktop = PLATFORM ~= "Android",
touchscreen_gui = core.settings:get_bool("enable_touch"),
keyboard_mouse = not core.settings:get_bool("enable_touch"),
-- When touch_controls is "auto", we don't which input method will be used,
-- so we show settings for both.
touchscreen = touch_controls == "auto" or core.is_yes(touch_controls),
keyboard_mouse = touch_controls == "auto" or not core.is_yes(touch_controls),
shaders_support = shaders_support,
shaders = core.settings:get_bool("enable_shaders") and shaders_support,
opengl = video_driver == "opengl",
@ -449,13 +452,13 @@ local function get_formspec(dialogdata)
local extra_h = 1 -- not included in tabsize.height
local tabsize = {
width = core.settings:get_bool("enable_touch") and 16.5 or 15.5,
height = core.settings:get_bool("enable_touch") and (10 - extra_h) or 12,
width = core.settings:get_bool("gui_touch") and 16.5 or 15.5,
height = core.settings:get_bool("gui_touch") and (10 - extra_h) or 12,
}
local scrollbar_w = core.settings:get_bool("enable_touch") and 0.6 or 0.4
local scrollbar_w = core.settings:get_bool("gui_touch") and 0.6 or 0.4
local left_pane_width = core.settings:get_bool("enable_touch") and 4.5 or 4.25
local left_pane_width = core.settings:get_bool("gui_touch") and 4.5 or 4.25
local left_pane_padding = 0.25
local search_width = left_pane_width + scrollbar_w - (0.75 * 2)
@ -469,7 +472,7 @@ local function get_formspec(dialogdata)
local fs = {
"formspec_version[6]",
"size[", tostring(tabsize.width), ",", tostring(tabsize.height + extra_h), "]",
core.settings:get_bool("enable_touch") and "padding[0.01,0.01]" or "",
core.settings:get_bool("gui_touch") and "padding[0.01,0.01]" or "",
"bgcolor[#0000]",
-- HACK: this is needed to allow resubmitting the same formspec
@ -618,6 +621,18 @@ function write_settings_early()
end
end
local function regenerate_page_list(dialogdata)
local suggested_page_id = update_filtered_pages(dialogdata.query)
dialogdata.components = nil
if not filtered_page_by_id[dialogdata.page_id] then
dialogdata.leftscroll = 0
dialogdata.rightscroll = 0
dialogdata.page_id = suggested_page_id
end
end
local function buttonhandler(this, fields)
local dialogdata = this.data
@ -642,27 +657,7 @@ local function buttonhandler(this, fields)
local value = core.is_yes(fields.show_advanced)
core.settings:set_bool("show_advanced", value)
write_settings_early()
end
-- enable_touch is a checkbox in a setting component. We handle this
-- setting differently so we can hide/show pages using the next if-statement
if fields.enable_touch ~= nil then
local value = core.is_yes(fields.enable_touch)
core.settings:set_bool("enable_touch", value)
write_settings_early()
end
if fields.show_advanced ~= nil or fields.enable_touch ~= nil then
local suggested_page_id = update_filtered_pages(dialogdata.query)
dialogdata.components = nil
if not filtered_page_by_id[dialogdata.page_id] then
dialogdata.leftscroll = 0
dialogdata.rightscroll = 0
dialogdata.page_id = suggested_page_id
end
regenerate_page_list(dialogdata)
return true
end
@ -695,20 +690,26 @@ local function buttonhandler(this, fields)
end
end
for i, comp in ipairs(dialogdata.components) do
if comp.on_submit and comp:on_submit(fields, this) then
write_settings_early()
local function after_setting_change(comp)
write_settings_early()
if comp.setting.name == "touch_controls" then
-- Changing the "touch_controls" setting may result in a different
-- page list.
regenerate_page_list(dialogdata)
else
-- Clear components so they regenerate
dialogdata.components = nil
end
end
for i, comp in ipairs(dialogdata.components) do
if comp.on_submit and comp:on_submit(fields, this) then
after_setting_change(comp)
return true
end
if comp.setting and fields["reset_" .. i] then
core.settings:remove(comp.setting.name)
write_settings_early()
-- Clear components so they regenerate
dialogdata.components = nil
after_setting_change(comp)
return true
end
end

View File

@ -94,7 +94,7 @@ function singleplayer_refresh_gamebar()
local btnbar = buttonbar_create(
"game_button_bar",
core.settings:get_bool("enable_touch") and {x = 0, y = 7.25} or {x = 0, y = 7.475},
core.settings:get_bool("gui_touch") and {x = 0, y = 7.25} or {x = 0, y = 7.475},
{x = 15.5, y = 1.25},
"#000000",
game_buttonbar_button_handler)

View File

@ -61,7 +61,7 @@
#
# # This is a comment
# #
# # Requires: shaders, enable_dynamic_shadows, !touchscreen_gui
# # Requires: shaders, enable_dynamic_shadows, !touchscreen
# name (Readable name) type type_args
#
# A requirement can be the name of a boolean setting or an engine-defined value.
@ -72,7 +72,7 @@
# * shaders_support (a video driver that supports shaders, may not be enabled)
# * shaders (both enable_shaders and shaders_support)
# * desktop / android
# * touchscreen_gui / keyboard_mouse
# * touchscreen / keyboard_mouse
# * opengl / gles
# * You can negate any requirement by prepending with !
#
@ -152,40 +152,42 @@ invert_hotbar_mouse_wheel (Hotbar: Invert mouse wheel direction) bool false
[*Touchscreen]
# Enables touchscreen mode, allowing you to play the game with a touchscreen.
enable_touch (Enable touchscreen) bool true
# Enables the touchscreen controls, allowing you to play the game with a touchscreen.
# "auto" means that the touchscreen controls will be enabled and disabled
# automatically depending on the last used input method.
touch_controls (Enable touchscreen controls) enum auto auto,true,false
# Touchscreen sensitivity multiplier.
#
# Requires: touchscreen_gui
# Requires: touchscreen
touchscreen_sensitivity (Touchscreen sensitivity) float 0.2 0.001 10.0
# The length in pixels after which a touch interaction is considered movement.
#
# Requires: touchscreen_gui
# Requires: touchscreen
touchscreen_threshold (Movement threshold) int 20 0 100
# The delay in milliseconds after which a touch interaction is considered a long tap.
#
# Requires: touchscreen_gui
# Requires: touchscreen
touch_long_tap_delay (Threshold for long taps) int 400 100 1000
# Use crosshair to select object instead of whole screen.
# If enabled, a crosshair will be shown and will be used for selecting object.
#
# Requires: touchscreen_gui
# Requires: touchscreen
touch_use_crosshair (Use crosshair for touch screen) bool false
# Fixes the position of virtual joystick.
# If disabled, virtual joystick will center to first-touch's position.
#
# Requires: touchscreen_gui
# Requires: touchscreen
fixed_virtual_joystick (Fixed virtual joystick) bool false
# Use virtual joystick to trigger "Aux1" button.
# If enabled, virtual joystick will also tap "Aux1" button when out of main circle.
#
# Requires: touchscreen_gui
# Requires: touchscreen
virtual_joystick_triggers_aux1 (Virtual joystick triggers Aux1 button) bool false
# The gesture for for punching players/entities.
@ -198,7 +200,7 @@ virtual_joystick_triggers_aux1 (Virtual joystick triggers Aux1 button) bool fals
# Known from the classic Minetest mobile controls.
# Combat is more or less impossible.
#
# Requires: touchscreen_gui
# Requires: touchscreen
touch_punch_gesture (Punch gesture) enum short_tap short_tap,long_tap
@ -685,6 +687,10 @@ language (Language) enum ,be,bg,ca,cs,da,de,el,en,eo,es,et,eu,fi,fr,gd,gl,hu,i
[**GUI]
# When enabled, the GUI is optimized to be more usable on touchscreens.
# Whether this is enabled by default depends on your hardware.
gui_touch (Optimize GUI for touchscreens) bool false
# Scale GUI by a user specified value.
# Use a nearest-neighbor-anti-alias filter to scale the GUI.
# This will smooth over some of the rough edges, and blend

View File

@ -7,6 +7,7 @@
#include "ILogger.h"
#include "Keycodes.h"
#include "irrString.h"
#include <cstring>
namespace irr
{
@ -297,6 +298,11 @@ enum EGUI_EVENT_TYPE
//! SEvents hold information about an event. See irr::IEventReceiver for details on event handling.
struct SEvent
{
SEvent()
{
memset(this, 0, sizeof(*this));
}
//! Any kind of GUI event.
struct SGUIEvent
{
@ -345,6 +351,9 @@ struct SEvent
//! Type of mouse event
EMOUSE_INPUT_EVENT Event;
//! Is this a simulated mouse event generated by Minetest itself?
bool Simulated;
};
//! Any kind of keyboard event.

View File

@ -652,10 +652,17 @@ bool CIrrDeviceSDL::run()
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
MouseX = irrevent.MouseInput.X = SDL_event.motion.x;
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;
if (!SDL_GetRelativeMouseMode()) {
MouseX = irrevent.MouseInput.X = SDL_event.motion.x;
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;
} else {
MouseX = irrevent.MouseInput.X = MouseX + SDL_event.motion.xrel;
MouseY = irrevent.MouseInput.Y = MouseY + SDL_event.motion.yrel;
}
MouseXRel = SDL_event.motion.xrel;
MouseYRel = SDL_event.motion.yrel;
irrevent.MouseInput.ButtonStates = MouseButtonStates;
irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;
irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;

View File

@ -154,8 +154,11 @@ public:
//! Sets the new position of the cursor.
void setPosition(s32 x, s32 y) override
{
#ifndef __ANDROID__
// On Android, this somehow results in a camera jump when enabling
// relative mouse mode and it isn't supported anyway.
SDL_WarpMouseInWindow(Device->Window, x, y);
#endif
if (SDL_GetRelativeMouseMode()) {
// There won't be an event for this warp (details on libsdl-org/SDL/issues/6034)
Device->MouseX = x;
@ -292,6 +295,7 @@ private:
#endif
s32 MouseX, MouseY;
// these two only continue to exist for some Emscripten stuff idk about
s32 MouseXRel, MouseYRel;
u32 MouseButtonStates;

View File

@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gui/mainmenumanager.h"
#include "clouds.h"
#include "gui/touchscreengui.h"
#include "gui/touchcontrols.h"
#include "server.h"
#include "filesys.h"
#include "gui/guiMainMenu.h"
@ -225,9 +225,9 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
m_rendering_engine->get_scene_manager()->clear();
if (g_touchscreengui) {
delete g_touchscreengui;
g_touchscreengui = NULL;
if (g_touchcontrols) {
delete g_touchcontrols;
g_touchcontrols = NULL;
}
/* Save the settings when leaving the game.

View File

@ -40,7 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "content/subgames.h"
#include "client/event_manager.h"
#include "fontengine.h"
#include "gui/touchscreengui.h"
#include "gui/pointer_type.h"
#include "gui/touchcontrols.h"
#include "itemdef.h"
#include "log.h"
#include "filesys.h"
@ -1242,8 +1243,9 @@ void Game::shutdown()
// Clear text when exiting.
m_game_ui->clearText();
if (g_touchscreengui)
g_touchscreengui->hide();
g_touchcontrols_tsrc = nullptr;
if (g_touchcontrols)
g_touchcontrols->hide();
showOverlayMessage(N_("Shutting down..."), 0, 0, false);
@ -1505,8 +1507,8 @@ bool Game::createClient(const GameStartData &start_data)
client->getScript()->on_camera_ready(camera);
client->setCamera(camera);
if (g_touchscreengui) {
g_touchscreengui->setUseCrosshair(!isTouchCrosshairDisabled());
if (g_touchcontrols) {
g_touchcontrols->setUseCrosshair(!isTouchCrosshairDisabled());
}
/* Clouds
@ -1573,8 +1575,9 @@ bool Game::initGui()
gui_chat_console = new GUIChatConsole(guienv, guienv->getRootGUIElement(),
-1, chat_backend, client, &g_menumgr);
if (g_settings->getBool("enable_touch"))
g_touchscreengui = new TouchScreenGUI(device, texture_src);
g_touchcontrols_tsrc = texture_src;
if (should_show_touch_controls())
g_touchcontrols = new TouchControls(device, g_touchcontrols_tsrc);
return true;
}
@ -2013,15 +2016,15 @@ void Game::processUserInput(f32 dtime)
input->clear();
}
if (g_touchscreengui)
g_touchscreengui->hide();
if (g_touchcontrols)
g_touchcontrols->hide();
} else {
if (g_touchscreengui) {
/* on touchscreengui step may generate own input events which ain't
if (g_touchcontrols) {
/* on touchcontrols step may generate own input events which ain't
* what we want in case we just did clear them */
g_touchscreengui->show();
g_touchscreengui->step(dtime);
g_touchcontrols->show();
g_touchcontrols->step(dtime);
}
m_game_focused = true;
@ -2214,8 +2217,8 @@ void Game::processItemSelection(u16 *new_playeritem)
}
}
if (g_touchscreengui) {
std::optional<u16> selection = g_touchscreengui->getHotbarSelection();
if (g_touchcontrols) {
std::optional<u16> selection = g_touchcontrols->getHotbarSelection();
if (selection)
*new_playeritem = *selection;
}
@ -2623,7 +2626,7 @@ void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
this results in duplicated input. To avoid that, we don't enable relative
mouse mode if we're in touchscreen mode. */
if (cur_control)
cur_control->setRelativeMode(!g_touchscreengui && !isMenuActive());
cur_control->setRelativeMode(!g_touchcontrols && !isMenuActive());
if ((device->isWindowActive() && device->isWindowFocused()
&& !isMenuActive()) || input->isRandom()) {
@ -2634,7 +2637,7 @@ void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
cur_control->setVisible(false);
}
if (m_first_loop_after_window_activation) {
if (m_first_loop_after_window_activation && !g_touchcontrols) {
m_first_loop_after_window_activation = false;
input->setMousePos(driver->getScreenSize().Width / 2,
@ -2650,6 +2653,8 @@ void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
m_first_loop_after_window_activation = true;
}
if (g_touchcontrols)
m_first_loop_after_window_activation = true;
}
// Get the factor to multiply with sensitivity to get the same mouse/joystick
@ -2666,9 +2671,9 @@ f32 Game::getSensitivityScaleFactor() const
void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
{
if (g_touchscreengui) {
cam->camera_yaw += g_touchscreengui->getYawChange();
cam->camera_pitch += g_touchscreengui->getPitchChange();
if (g_touchcontrols) {
cam->camera_yaw += g_touchcontrols->getYawChange();
cam->camera_pitch += g_touchcontrols->getPitchChange();
} else {
v2s32 center(driver->getScreenSize().Width / 2, driver->getScreenSize().Height / 2);
v2s32 dist = input->getMousePos() - center;
@ -2733,7 +2738,7 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
* touch then its meaning is inverted (i.e. holding aux1 means walk and
* not fast)
*/
if (g_touchscreengui && m_touch_simulate_aux1) {
if (g_touchcontrols && m_touch_simulate_aux1) {
control.aux1 = control.aux1 ^ true;
}
@ -3222,8 +3227,8 @@ void Game::updateCamera(f32 dtime)
camera->toggleCameraMode();
if (g_touchscreengui)
g_touchscreengui->setUseCrosshair(!isTouchCrosshairDisabled());
if (g_touchcontrols)
g_touchcontrols->setUseCrosshair(!isTouchCrosshairDisabled());
// Make the player visible depending on camera mode.
playercao->updateMeshCulling();
@ -3324,8 +3329,8 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud)
}
shootline.end = shootline.start + camera_direction * BS * d;
if (g_touchscreengui && isTouchCrosshairDisabled()) {
shootline = g_touchscreengui->getShootline();
if (g_touchcontrols && isTouchCrosshairDisabled()) {
shootline = g_touchcontrols->getShootline();
// Scale shootline to the acual distance the player can reach
shootline.end = shootline.start +
shootline.getVector().normalize() * BS * d;
@ -3342,9 +3347,9 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud)
if (pointed != runData.pointed_old)
infostream << "Pointing at " << pointed.dump() << std::endl;
if (g_touchscreengui) {
if (g_touchcontrols) {
auto mode = selected_def.touch_interaction.getMode(pointed.type);
g_touchscreengui->applyContextControls(mode);
g_touchcontrols->applyContextControls(mode);
}
// Note that updating the selection mesh every frame is not particularly efficient,
@ -4331,7 +4336,7 @@ void Game::drawScene(ProfilerGraph *graph, RunStats *stats)
(player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) &&
(this->camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT));
if (g_touchscreengui && isTouchCrosshairDisabled())
if (g_touchcontrols && isTouchCrosshairDisabled())
draw_crosshair = false;
this->m_rendering_engine->draw_scene(sky_color, this->m_game_ui->m_flags.show_hud,
@ -4442,7 +4447,7 @@ void Game::showPauseMenu()
{
std::string control_text;
if (g_touchscreengui) {
if (g_touchcontrols) {
control_text = strgettext("Controls:\n"
"No menu open:\n"
"- slide finger: look around\n"

View File

@ -39,7 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "wieldmesh.h"
#include "client/renderingengine.h"
#include "client/minimap.h"
#include "gui/touchscreengui.h"
#include "gui/touchcontrols.h"
#include "util/enriched_string.h"
#include "irrlicht_changes/CGUITTFont.h"
@ -305,8 +305,8 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
drawItem(mainlist->getItem(i), item_rect, (i + 1) == selectitem);
if (is_hotbar && g_touchscreengui)
g_touchscreengui->registerHotbarRect(i, item_rect);
if (is_hotbar && g_touchcontrols)
g_touchcontrols->registerHotbarRect(i, item_rect);
}
}
@ -770,8 +770,8 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
void Hud::drawHotbar(u16 playeritem)
{
if (g_touchscreengui)
g_touchscreengui->resetHotbarRects();
if (g_touchcontrols)
g_touchcontrols->resetHotbarRects();
InventoryList *mainlist = inventory->getList("main");
if (mainlist == NULL) {

View File

@ -18,10 +18,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "gui/pointer_type.h"
#include "util/numeric.h"
#include "inputhandler.h"
#include "gui/mainmenumanager.h"
#include "gui/touchscreengui.h"
#include "gui/touchcontrols.h"
#include "hud.h"
void KeyCache::populate_nonchanging()
@ -113,13 +114,28 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
return true;
}
if (event.EventType == EET_MOUSE_INPUT_EVENT && !event.MouseInput.Simulated)
g_last_pointer_type = PointerType::Mouse;
else if (event.EventType == EET_TOUCH_INPUT_EVENT)
g_last_pointer_type = PointerType::Touch;
// Let the menu handle events, if one is active.
if (isMenuActive()) {
if (g_touchscreengui)
g_touchscreengui->setVisible(false);
if (g_touchcontrols)
g_touchcontrols->hide();
return g_menumgr.preprocessEvent(event);
}
bool should_show = should_show_touch_controls();
if (g_touchcontrols_tsrc && !should_show && g_touchcontrols) {
TouchControls *gui = g_touchcontrols;
g_touchcontrols = nullptr;
delete gui;
} else if (g_touchcontrols_tsrc && should_show && !g_touchcontrols) {
g_touchcontrols = new TouchControls(RenderingEngine::get_raw_device(),
g_touchcontrols_tsrc);
}
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
const KeyPress keyCode(event.KeyInput);
@ -140,9 +156,9 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
return true;
}
} else if (g_touchscreengui && event.EventType == irr::EET_TOUCH_INPUT_EVENT) {
// In case of touchscreengui, we have to handle different events
g_touchscreengui->translateEvent(event);
} else if (g_touchcontrols && event.EventType == irr::EET_TOUCH_INPUT_EVENT) {
// In case of touchcontrols, we have to handle different events
g_touchcontrols->translateEvent(event);
return true;
} else if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
// joystick may be nullptr if game is launched with '--random-input' parameter
@ -209,8 +225,8 @@ float RealInputHandler::getMovementSpeed()
return 0.0f;
return 1.0f; // If there is a keyboard event, assume maximum speed
}
if (g_touchscreengui && g_touchscreengui->getMovementSpeed())
return g_touchscreengui->getMovementSpeed();
if (g_touchcontrols && g_touchcontrols->getMovementSpeed())
return g_touchcontrols->getMovementSpeed();
return joystick.getMovementSpeed();
}
@ -232,8 +248,8 @@ float RealInputHandler::getMovementDirection()
return std::atan2(x, z);
// `getMovementDirection() == 0` means forward, so we cannot use
// `getMovementDirection()` as a condition.
else if (g_touchscreengui && g_touchscreengui->getMovementSpeed())
return g_touchscreengui->getMovementDirection();
else if (g_touchcontrols && g_touchcontrols->getMovementSpeed())
return g_touchcontrols->getMovementDirection();
return joystick.getMovementDirection();
}

View File

@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "settings.h"
#include "client/renderingengine.h"
#include "gui/touchscreengui.h"
#include "gui/touchcontrols.h"
ClientDynamicInfo ClientDynamicInfo::getCurrent()
{
@ -33,7 +33,7 @@ ClientDynamicInfo ClientDynamicInfo::getCurrent()
f32 hud_scaling = g_settings->getFloat("hud_scaling", 0.5f, 20.0f);
f32 real_gui_scaling = gui_scaling * density;
f32 real_hud_scaling = hud_scaling * density;
bool touch_controls = g_touchscreengui;
bool touch_controls = g_touchcontrols;
return {
screen_size, real_gui_scaling, real_hud_scaling,
@ -44,7 +44,7 @@ ClientDynamicInfo ClientDynamicInfo::getCurrent()
v2f32 ClientDynamicInfo::calculateMaxFSSize(v2u32 render_target_size, f32 gui_scaling)
{
f32 factor = (g_settings->getBool("enable_touch") ? 10 : 15) / gui_scaling;
f32 factor = (g_settings->getBool("gui_touch") ? 10 : 15) / gui_scaling;
f32 ratio = (f32)render_target_size.X / (f32)render_target_size.Y;
if (ratio < 1)
return { factor, factor / ratio };

View File

@ -104,7 +104,21 @@ void set_default_settings()
// Client
settings->setDefault("address", "");
settings->setDefault("enable_sound", "true");
settings->setDefault("enable_touch", bool_to_cstr(has_touch));
#if defined(__linux__) && !defined (__ANDROID__)
// On Linux+X11 (not Linux+Wayland or Linux+XWayland), I've encountered a bug
// where fake mouse events were generated from touch events if in relative
// mouse mode, resulting in the touchscreen controls being instantly disabled
// again and thus making them unusable.
// => We can't switch based on the last input method used.
// => Fall back to hardware detection.
settings->setDefault("touch_controls", bool_to_cstr(has_touch));
#else
settings->setDefault("touch_controls", "auto");
#endif
// Since GUI scaling shouldn't suddenly change during a session, we use
// hardware detection here instead of switching based on the last input
// method used.
settings->setDefault("gui_touch", bool_to_cstr(has_touch));
settings->setDefault("sound_volume", "0.8");
settings->setDefault("sound_volume_unfocused", "0.3");
settings->setDefault("mute_sound", "false");

View File

@ -25,6 +25,6 @@ set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiVolumeChange.cpp
${CMAKE_CURRENT_SOURCE_DIR}/modalMenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp
${CMAKE_CURRENT_SOURCE_DIR}/touchscreengui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/touchcontrols.cpp
PARENT_SCOPE
)

View File

@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h"
#include "gamedef.h"
#include "client/keycode.h"
#include "gui/pointer_type.h"
#include "util/strfnd.h"
#include <IGUIButton.h>
#include <IGUICheckBox.h>
@ -315,7 +316,7 @@ void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
data->invsize.Y = MYMAX(0, stof(parts[1]));
lockSize(false);
if (!g_settings->getBool("enable_touch") && parts.size() == 3) {
if (!g_settings->getBool("gui_touch") && parts.size() == 3) {
if (parts[2] == "true") {
lockSize(true,v2u32(800,600));
}
@ -3283,7 +3284,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
s32 min_screen_dim = std::min(padded_screensize.X, padded_screensize.Y);
double prefer_imgsize;
if (g_settings->getBool("enable_touch")) {
if (g_settings->getBool("gui_touch")) {
// The preferred imgsize should be larger to accommodate the
// smaller screensize.
prefer_imgsize = min_screen_dim / 10 * gui_scaling;
@ -3770,7 +3771,7 @@ void GUIFormSpecMenu::showTooltip(const std::wstring &text,
int tooltip_offset_x = m_btn_height;
int tooltip_offset_y = m_btn_height;
if (m_pointer_type == PointerType::Touch) {
if (g_last_pointer_type == PointerType::Touch) {
tooltip_offset_x *= 3;
tooltip_offset_y = 0;
if (m_pointer.X > (s32)screenSize.X / 2)

View File

@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "guiInventoryList.h"
#include "gui/pointer_type.h"
#include "guiFormSpecMenu.h"
#include "client/hud.h"
#include "client/client.h"
@ -153,7 +154,7 @@ void GUIInventoryList::draw()
// Add hovering tooltip
bool show_tooltip = !item.empty() && hovering && !selected_item;
// Make it possible to see item tooltips on touchscreens
if (m_fs_menu->getPointerType() == PointerType::Touch) {
if (g_last_pointer_type == PointerType::Touch) {
show_tooltip |= hovering && selected && m_fs_menu->getSelectedAmount() != 0;
}
if (show_tooltip) {

View File

@ -26,9 +26,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "modalMenu.h"
#include "gettext.h"
#include "gui/guiInventoryList.h"
#include "gui/pointer_type.h"
#include "porting.h"
#include "settings.h"
#include "touchscreengui.h"
#include "touchcontrols.h"
PointerAction PointerAction::fromEvent(const SEvent &event) {
switch (event.EventType) {
@ -62,7 +63,7 @@ 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();
if (g_settings->getBool("enable_touch")) {
if (g_settings->getBool("gui_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;
@ -117,8 +118,8 @@ void GUIModalMenu::quitMenu()
Environment->removeFocus(this);
m_menumgr->deletingMenu(this);
this->remove();
if (g_touchscreengui)
g_touchscreengui->show();
if (g_touchcontrols)
g_touchcontrols->show();
}
static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
@ -193,6 +194,7 @@ bool GUIModalMenu::simulateMouseEvent(ETOUCH_INPUT_EVENT touch_event, bool secon
mouse_event.EventType = EET_MOUSE_INPUT_EVENT;
mouse_event.MouseInput.X = m_pointer.X;
mouse_event.MouseInput.Y = m_pointer.Y;
mouse_event.MouseInput.Simulated = true;
switch (touch_event) {
case ETIE_PRESSED_DOWN:
mouse_event.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
@ -216,7 +218,6 @@ bool GUIModalMenu::simulateMouseEvent(ETOUCH_INPUT_EVENT touch_event, bool secon
}
bool retval;
m_simulated_mouse = true;
do {
if (preprocessEvent(mouse_event)) {
retval = true;
@ -228,7 +229,6 @@ bool GUIModalMenu::simulateMouseEvent(ETOUCH_INPUT_EVENT touch_event, bool secon
}
retval = target->OnEvent(mouse_event);
} while (false);
m_simulated_mouse = false;
if (!retval && !second_try)
return simulateMouseEvent(touch_event, true);
@ -331,7 +331,6 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
if (event.TouchInput.touchedCount == 1) {
m_pointer_type = PointerType::Touch;
m_pointer = v2s32(event.TouchInput.X, event.TouchInput.Y);
gui::IGUIElement *hovered = Environment->getRootGUIElement()->getElementFromPoint(core::position2d<s32>(m_pointer));
@ -374,9 +373,8 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
}
if (event.EventType == EET_MOUSE_INPUT_EVENT) {
if (!m_simulated_mouse) {
// Only set the pointer type to mouse if this is a real mouse event.
m_pointer_type = PointerType::Mouse;
if (!event.MouseInput.Simulated) {
// Only process if this is a real mouse event.
m_pointer = v2s32(event.MouseInput.X, event.MouseInput.Y);
m_touch_hovered.reset();
}

View File

@ -26,10 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <porting_android.h>
#endif
enum class PointerType {
Mouse,
Touch,
};
struct PointerAction {
v2s32 pos;
@ -74,14 +70,10 @@ public:
porting::AndroidDialogState getAndroidUIInputState();
#endif
PointerType getPointerType() { return m_pointer_type; };
protected:
virtual std::wstring getLabelByID(s32 id) = 0;
virtual std::string getNameByID(s32 id) = 0;
// Stores the last known pointer type.
PointerType m_pointer_type = PointerType::Mouse;
// Stores the last known pointer position.
// If the last input event was a mouse event, it's the cursor position.
// If the last input event was a touch event, it's the finger position.
@ -96,9 +88,6 @@ protected:
// This is set to true if the menu is currently processing a second-touch event.
bool m_second_touch = false;
// This is set to true if the menu is currently processing a mouse event
// that was synthesized by the menu itself from a touch event.
bool m_simulated_mouse = false;
private:
IMenuManager *m_menumgr;

10
src/gui/pointer_type.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include "client/texturesource.h"
enum class PointerType {
Mouse,
Touch,
};
inline PointerType g_last_pointer_type = PointerType::Mouse;
inline ITextureSource *g_touchcontrols_tsrc = nullptr;

View File

@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "touchscreengui.h"
#include "touchcontrols.h"
#include "gettime.h"
#include "irr_v2d.h"
@ -31,12 +31,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/keycode.h"
#include "client/renderingengine.h"
#include "util/numeric.h"
#include "pointer_type.h"
#include <ISceneCollisionManager.h>
#include <iostream>
#include <algorithm>
TouchScreenGUI *g_touchscreengui;
TouchControls *g_touchcontrols;
static const char *button_image_names[] = {
"jump_btn.png",
@ -237,7 +238,7 @@ static EKEY_CODE id_to_keycode(touch_gui_button_id id)
code = keyname_to_keycode(resolved.c_str());
} catch (UnknownKeycode &e) {
code = KEY_UNKNOWN;
warningstream << "TouchScreenGUI: Unknown key '" << resolved
warningstream << "TouchControls: Unknown key '" << resolved
<< "' for '" << key << "', hiding button." << std::endl;
}
return code;
@ -403,7 +404,7 @@ void AutoHideButtonBar::hide()
updateVisibility();
}
TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, ISimpleTextureSource *tsrc):
TouchControls::TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc):
m_device(device),
m_guienv(device->getGUIEnvironment()),
m_receiver(device->getEventReceiver()),
@ -515,7 +516,12 @@ TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, ISimpleTextureSource *tsr
}
}
void TouchScreenGUI::addButton(touch_gui_button_id id, const std::string &image, const recti &rect)
TouchControls::~TouchControls()
{
releaseAll();
}
void TouchControls::addButton(touch_gui_button_id id, const std::string &image, const recti &rect)
{
IGUIImage *btn_gui_button = m_guienv->addImage(rect, nullptr, id);
load_button_texture(btn_gui_button, image, rect,
@ -526,7 +532,7 @@ void TouchScreenGUI::addButton(touch_gui_button_id id, const std::string &image,
btn.gui_button = grab_gui_element<IGUIImage>(btn_gui_button);
}
IGUIImage *TouchScreenGUI::makeJoystickButton(touch_gui_button_id id,
IGUIImage *TouchControls::makeJoystickButton(touch_gui_button_id id,
const recti &button_rect, bool visible)
{
IGUIImage *btn_gui_button = m_guienv->addImage(button_rect, nullptr, id);
@ -537,7 +543,7 @@ IGUIImage *TouchScreenGUI::makeJoystickButton(touch_gui_button_id id,
return btn_gui_button;
}
bool TouchScreenGUI::isHotbarButton(const SEvent &event)
bool TouchControls::isHotbarButton(const SEvent &event)
{
const v2s32 touch_pos = v2s32(event.TouchInput.X, event.TouchInput.Y);
// check if hotbar item is pressed
@ -552,14 +558,14 @@ bool TouchScreenGUI::isHotbarButton(const SEvent &event)
return false;
}
std::optional<u16> TouchScreenGUI::getHotbarSelection()
std::optional<u16> TouchControls::getHotbarSelection()
{
auto selection = m_hotbar_selection;
m_hotbar_selection = std::nullopt;
return selection;
}
void TouchScreenGUI::handleReleaseEvent(size_t pointer_id)
void TouchControls::handleReleaseEvent(size_t pointer_id)
{
// By the way: Android reuses pointer IDs, so m_pointer_pos[pointer_id]
// will be overwritten soon anyway.
@ -605,15 +611,15 @@ void TouchScreenGUI::handleReleaseEvent(size_t pointer_id)
m_joystick_btn_bg->setVisible(false);
m_joystick_btn_center->setVisible(false);
} else {
infostream << "TouchScreenGUI::translateEvent released unknown button: "
infostream << "TouchControls::translateEvent released unknown button: "
<< pointer_id << std::endl;
}
}
void TouchScreenGUI::translateEvent(const SEvent &event)
void TouchControls::translateEvent(const SEvent &event)
{
if (!m_visible) {
infostream << "TouchScreenGUI::translateEvent got event but is not visible!"
infostream << "TouchControls::translateEvent got event but is not visible!"
<< std::endl;
return;
}
@ -780,7 +786,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
}
}
void TouchScreenGUI::applyJoystickStatus()
void TouchControls::applyJoystickStatus()
{
if (m_joystick_triggers_aux1) {
SEvent translated{};
@ -796,7 +802,24 @@ void TouchScreenGUI::applyJoystickStatus()
}
}
void TouchScreenGUI::step(float dtime)
void TouchControls::releaseAll()
{
while (!m_pointer_pos.empty())
handleReleaseEvent(m_pointer_pos.begin()->first);
// Release those manually too since the change initiated by
// handleReleaseEvent will only be applied later by applyContextControls.
if (m_dig_pressed) {
emitMouseEvent(EMIE_LMOUSE_LEFT_UP);
m_dig_pressed = false;
}
if (m_place_pressed) {
emitMouseEvent(EMIE_RMOUSE_LEFT_UP);
m_place_pressed = false;
}
}
void TouchControls::step(float dtime)
{
// simulate keyboard repeats
buttons_step(m_buttons, dtime, m_device->getVideoDriver(), m_receiver, m_texturesource);
@ -832,18 +855,21 @@ void TouchScreenGUI::step(float dtime)
m_had_move_id = false;
}
void TouchScreenGUI::resetHotbarRects()
void TouchControls::resetHotbarRects()
{
m_hotbar_rects.clear();
}
void TouchScreenGUI::registerHotbarRect(u16 index, const recti &rect)
void TouchControls::registerHotbarRect(u16 index, const recti &rect)
{
m_hotbar_rects[index] = rect;
}
void TouchScreenGUI::setVisible(bool visible)
void TouchControls::setVisible(bool visible)
{
if (m_visible == visible)
return;
m_visible = visible;
for (auto &button : m_buttons) {
if (button.gui_button)
@ -853,10 +879,8 @@ void TouchScreenGUI::setVisible(bool visible)
if (m_joystick_btn_off)
m_joystick_btn_off->setVisible(visible);
// clear all active buttons
if (!visible) {
while (!m_pointer_pos.empty())
handleReleaseEvent(m_pointer_pos.begin()->first);
releaseAll();
for (AutoHideButtonBar &bar : m_buttonbars) {
bar.deactivate();
bar.hide();
@ -867,23 +891,17 @@ void TouchScreenGUI::setVisible(bool visible)
}
}
void TouchScreenGUI::hide()
void TouchControls::hide()
{
if (!m_visible)
return;
setVisible(false);
}
void TouchScreenGUI::show()
void TouchControls::show()
{
if (m_visible)
return;
setVisible(true);
}
v2s32 TouchScreenGUI::getPointerPos()
v2s32 TouchControls::getPointerPos()
{
if (m_draw_crosshair)
return v2s32(m_screensize.X / 2, m_screensize.Y / 2);
@ -892,7 +910,7 @@ v2s32 TouchScreenGUI::getPointerPos()
return m_move_pos;
}
void TouchScreenGUI::emitMouseEvent(EMOUSE_INPUT_EVENT type)
void TouchControls::emitMouseEvent(EMOUSE_INPUT_EVENT type)
{
v2s32 pointer_pos = getPointerPos();
@ -904,10 +922,11 @@ void TouchScreenGUI::emitMouseEvent(EMOUSE_INPUT_EVENT type)
event.MouseInput.Control = false;
event.MouseInput.ButtonStates = 0;
event.MouseInput.Event = type;
event.MouseInput.Simulated = true;
m_receiver->OnEvent(event);
}
void TouchScreenGUI::applyContextControls(const TouchInteractionMode &mode)
void TouchControls::applyContextControls(const TouchInteractionMode &mode)
{
// Since the pointed thing has already been determined when this function
// is called, we cannot use this function to update the shootline.
@ -990,3 +1009,12 @@ void TouchScreenGUI::applyContextControls(const TouchInteractionMode &mode)
m_place_pressed = false;
}
}
bool should_show_touch_controls()
{
const std::string &touch_controls = g_settings->get("touch_controls");
if (touch_controls == "auto")
return g_last_pointer_type == PointerType::Touch;
return is_yes(touch_controls);
}

View File

@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "itemdef.h"
#include "client/game.h"
#include "util/basic_macros.h"
using namespace irr;
using namespace irr::core;
@ -183,10 +184,12 @@ private:
void updateVisibility();
};
class TouchScreenGUI
class TouchControls
{
public:
TouchScreenGUI(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc);
~TouchControls();
DISABLE_CLASS_COPY(TouchControls);
void translateEvent(const SEvent &event);
void applyContextControls(const TouchInteractionMode &mode);
@ -220,7 +223,6 @@ public:
void step(float dtime);
inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
void setVisible(bool visible);
void hide();
void show();
@ -238,6 +240,7 @@ private:
double m_touchscreen_threshold;
u16 m_long_tap_delay;
bool m_visible = true; // is the whole touch screen gui visible
void setVisible(bool visible);
std::unordered_map<u16, recti> m_hotbar_rects;
std::optional<u16> m_hotbar_selection = std::nullopt;
@ -295,6 +298,9 @@ private:
// apply joystick status
void applyJoystickStatus();
// release all buttons etc.
void releaseAll();
// map to store the IDs and original positions of currently pressed pointers
std::unordered_map<size_t, v2s32> m_pointer_downpos;
// map to store the IDs and positions of currently pressed pointers
@ -314,4 +320,6 @@ private:
u64 m_place_pressed_until = 0;
};
extern TouchScreenGUI *g_touchscreengui;
extern TouchControls *g_touchcontrols;
bool should_show_touch_controls();

View File

@ -15,7 +15,7 @@ zlib_version=1.3.1
zstd_version=1.5.5
libjpeg_version=3.0.1
libpng_version=1.6.40
sdl2_version=2.28.5
sdl2_version=2.30.3
download () {
local url=$1

View File

@ -19,8 +19,8 @@ da6ad10632cf172992158e9ea0977a87914b5d5de93a972c3430b6a412237556 luajit-2024012
2b1dabe83d478b398cf9226d96de7fa62c973365c4aea70d27ba5782fb49d2d0 luajit-20240125-win64.zip
e2443451fe5c2066eb564c64b8a1762738a88b7fd749c8b5907fed45c785497b openal-soft-1.23.1-win32.zip
cb041445a118469caefbad2647470cb8571c8337bce2adc07634011ab5625417 openal-soft-1.23.1-win64.zip
f9f890af960e92fd3f532f2e9ac00681c33bc67a722e000dfdaeb41b0064f1a0 sdl2-2.28.5-win32.zip
8dde2c6963544b7d8a2e87c128ebbdf51ad0e70c7e2df986ff4e963ce9996d9b sdl2-2.28.5-win64.zip
574e0847e622ff09ab23e2b22b77685a2ab6ee43de3e2932f3e8a14a4d7b9338 sdl2-2.30.3-win32.zip
6127afdfc7b6a4ade8caf9a7267748ffea974f729866dd5be96c7a69d2f0fee7 sdl2-2.30.3-win64.zip
326701086a0ed66e09a9f3ec4d971654c13b6bd79cfdd079c947ecdcd6409525 sqlite3-3.44.2-win32.zip
b2d474e3625f8f426b6cc5c0ecac831a1de46f7d1027bf4a9f6267b0b0411d42 sqlite3-3.44.2-win64.zip
8af10515d57dbfee5d2106cd66cafa2adeb4270d4c6047ccbf7e8b5d2d50681c zlib-1.3.1-win32.zip