From 517f1602aa77c93af5b06f6cd1f2c20075f9547b Mon Sep 17 00:00:00 2001 From: Gregor Parzefall Date: Sun, 24 Mar 2024 22:35:55 +0100 Subject: [PATCH] Re-add "long tap to punch" as a client-side setting --- builtin/settingtypes.txt | 13 +++++++++++++ doc/lua_api.md | 9 +++++---- src/client/game.cpp | 6 ++++-- src/defaultsettings.cpp | 1 + src/gui/touchscreengui.cpp | 4 ++-- src/itemdef.cpp | 31 ++++++++++++++++++++++--------- src/itemdef.h | 8 ++++++-- src/script/common/c_types.cpp | 1 + 8 files changed, 54 insertions(+), 19 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index b039780bc..b9c0cda80 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -185,6 +185,19 @@ fixed_virtual_joystick (Fixed virtual joystick) bool false # Requires: touchscreen_gui virtual_joystick_triggers_aux1 (Virtual joystick triggers Aux1 button) bool false +# The gesture for for punching players/entities. +# This can be overridden by games and mods. +# +# * short_tap +# Easy to use and well-known from other games that shall not be named. +# +# * long_tap +# Known from the classic Minetest mobile controls. +# Combat is more or less impossible. +# +# Requires: touchscreen_gui +touch_punch_gesture (Punch gesture) enum short_tap short_tap,long_tap + [Graphics and Audio] diff --git a/doc/lua_api.md b/doc/lua_api.md index 8a1df92ef..30b7e46bc 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -9098,15 +9098,16 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and touch_interaction = { -- Only affects touchscreen clients. -- Defines the meaning of short and long taps with the item in hand. - -- The fields in this table have two valid values: + -- The fields in this table can be set to the following values: + -- * "user" (meaning depends on client-side settings) -- * "long_dig_short_place" (long tap = dig, short tap = place) -- * "short_dig_long_place" (short tap = dig, long tap = place) -- The field to be used is selected according to the current -- `pointed_thing`. - pointed_nothing = "long_dig_short_place", - pointed_node = "long_dig_short_place", - pointed_object = "short_dig_long_place", + pointed_nothing = "user", + pointed_node = "user", + pointed_object = "user", }, sound = { diff --git a/src/client/game.cpp b/src/client/game.cpp index 67529681b..3c3feaa72 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -3280,8 +3280,10 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud) if (pointed != runData.pointed_old) infostream << "Pointing at " << pointed.dump() << std::endl; - if (g_touchscreengui) - g_touchscreengui->applyContextControls(selected_def.touch_interaction.getMode(pointed)); + if (g_touchscreengui) { + auto mode = selected_def.touch_interaction.getMode(pointed.type); + g_touchscreengui->applyContextControls(mode); + } // Note that updating the selection mesh every frame is not particularly efficient, // but the halo rendering code is already inefficient so there's no point in optimizing it here diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index bb50a00aa..11b42c779 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -490,6 +490,7 @@ void set_default_settings() settings->setDefault("touch_use_crosshair", "false"); settings->setDefault("fixed_virtual_joystick", "false"); settings->setDefault("virtual_joystick_triggers_aux1", "false"); + settings->setDefault("touch_punch_gesture", "short_tap"); #ifdef ENABLE_TOUCH settings->setDefault("clickable_chat_weblinks", "false"); #else diff --git a/src/gui/touchscreengui.cpp b/src/gui/touchscreengui.cpp index 07fe90585..2d41d4fde 100644 --- a/src/gui/touchscreengui.cpp +++ b/src/gui/touchscreengui.cpp @@ -1105,11 +1105,11 @@ void TouchScreenGUI::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. + sanity_check(mode != TouchInteractionMode_USER); + u64 now = porting::getTimeMs(); bool target_dig_pressed = false; bool target_place_pressed = false; - u64 now = porting::getTimeMs(); - // If the meanings of short and long taps have been swapped, abort any ongoing // short taps because they would do something else than the player expected. // Long taps don't need this, they're adjusted to the swapped meanings instead. diff --git a/src/itemdef.cpp b/src/itemdef.cpp index 5031487ea..ad2ed4847 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -40,24 +40,37 @@ with this program; if not, write to the Free Software Foundation, Inc., TouchInteraction::TouchInteraction() { - pointed_nothing = LONG_DIG_SHORT_PLACE; - pointed_node = LONG_DIG_SHORT_PLACE; - // Map punching to single tap by default. - pointed_object = SHORT_DIG_LONG_PLACE; + pointed_nothing = TouchInteractionMode_USER; + pointed_node = TouchInteractionMode_USER; + pointed_object = TouchInteractionMode_USER; } -TouchInteractionMode TouchInteraction::getMode(const PointedThing &pointed) const +TouchInteractionMode TouchInteraction::getMode(PointedThingType pointed_type) const { - switch (pointed.type) { + TouchInteractionMode result; + switch (pointed_type) { case POINTEDTHING_NOTHING: - return pointed_nothing; + result = pointed_nothing; + break; case POINTEDTHING_NODE: - return pointed_node; + result = pointed_node; + break; case POINTEDTHING_OBJECT: - return pointed_object; + result = pointed_object; + break; default: FATAL_ERROR("Invalid PointedThingType given to TouchInteraction::getMode"); } + + if (result == TouchInteractionMode_USER) { + if (pointed_type == POINTEDTHING_OBJECT) + result = g_settings->get("touch_punch_gesture") == "long_tap" ? + LONG_DIG_SHORT_PLACE : SHORT_DIG_LONG_PLACE; + else + result = LONG_DIG_SHORT_PLACE; + } + + return result; } void TouchInteraction::serialize(std::ostream &os) const diff --git a/src/itemdef.h b/src/itemdef.h index 884d31501..782dad816 100644 --- a/src/itemdef.h +++ b/src/itemdef.h @@ -30,10 +30,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "texture_override.h" // TextureOverride #include "tool.h" #include "util/pointabilities.h" +#include "util/pointedthing.h" + class IGameDef; class Client; struct ToolCapabilities; -struct PointedThing; #ifndef SERVER #include "client/texturesource.h" struct ItemMesh; @@ -57,6 +58,7 @@ enum TouchInteractionMode : u8 { LONG_DIG_SHORT_PLACE, SHORT_DIG_LONG_PLACE, + TouchInteractionMode_USER, // Meaning depends on client-side settings TouchInteractionMode_END, // Dummy for validity check }; @@ -67,7 +69,9 @@ struct TouchInteraction TouchInteractionMode pointed_object; TouchInteraction(); - TouchInteractionMode getMode(const PointedThing &pointed) const; + // Returns the right mode for the pointed thing and resolves any occurrence + // of TouchInteractionMode_USER into an actual mode. + TouchInteractionMode getMode(PointedThingType pointed_type) const; void serialize(std::ostream &os) const; void deSerialize(std::istream &is); }; diff --git a/src/script/common/c_types.cpp b/src/script/common/c_types.cpp index 7338834f7..be6ef65ae 100644 --- a/src/script/common/c_types.cpp +++ b/src/script/common/c_types.cpp @@ -37,5 +37,6 @@ struct EnumString es_TouchInteractionMode[] = { {LONG_DIG_SHORT_PLACE, "long_dig_short_place"}, {SHORT_DIG_LONG_PLACE, "short_dig_long_place"}, + {TouchInteractionMode_USER, "user"}, {0, NULL}, };