mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-02 16:38:41 +00:00
Touchscreen: Allow mods to swap the meaning of short and long taps (punch with single tap) (#14087)
This works through a new field "touch_interaction" in item definitions. The two most important use cases are: - Punching players/entities with short tap instead of long tap (enabled by default) - Making items usable that require holding the place button (e.g. bows and shields in MC-like games)
This commit is contained in:
parent
8cbd629010
commit
404a063fdf
10 changed files with 288 additions and 108 deletions
|
@ -35,9 +35,60 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "util/serialize.h"
|
||||
#include "util/container.h"
|
||||
#include "util/thread.h"
|
||||
#include "util/pointedthing.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
TouchInteractionMode TouchInteraction::getMode(const PointedThing &pointed) const
|
||||
{
|
||||
switch (pointed.type) {
|
||||
case POINTEDTHING_NOTHING:
|
||||
return pointed_nothing;
|
||||
case POINTEDTHING_NODE:
|
||||
return pointed_node;
|
||||
case POINTEDTHING_OBJECT:
|
||||
return pointed_object;
|
||||
default:
|
||||
FATAL_ERROR("Invalid PointedThingType given to TouchInteraction::getMode");
|
||||
}
|
||||
}
|
||||
|
||||
void TouchInteraction::serialize(std::ostream &os) const
|
||||
{
|
||||
writeU8(os, pointed_nothing);
|
||||
writeU8(os, pointed_node);
|
||||
writeU8(os, pointed_object);
|
||||
}
|
||||
|
||||
void TouchInteraction::deSerialize(std::istream &is)
|
||||
{
|
||||
u8 tmp = readU8(is);
|
||||
if (is.eof())
|
||||
throw SerializationError("");
|
||||
if (tmp < TouchInteractionMode_END)
|
||||
pointed_nothing = (TouchInteractionMode)tmp;
|
||||
|
||||
tmp = readU8(is);
|
||||
if (is.eof())
|
||||
throw SerializationError("");
|
||||
if (tmp < TouchInteractionMode_END)
|
||||
pointed_node = (TouchInteractionMode)tmp;
|
||||
|
||||
tmp = readU8(is);
|
||||
if (is.eof())
|
||||
throw SerializationError("");
|
||||
if (tmp < TouchInteractionMode_END)
|
||||
pointed_object = (TouchInteractionMode)tmp;
|
||||
}
|
||||
|
||||
/*
|
||||
ItemDefinition
|
||||
*/
|
||||
|
@ -84,6 +135,7 @@ ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
|
|||
range = def.range;
|
||||
palette_image = def.palette_image;
|
||||
color = def.color;
|
||||
touch_interaction = def.touch_interaction;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -126,6 +178,7 @@ void ItemDefinition::reset()
|
|||
node_placement_prediction.clear();
|
||||
place_param2.reset();
|
||||
wallmounted_rotate_vertical = false;
|
||||
touch_interaction = TouchInteraction();
|
||||
}
|
||||
|
||||
void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
|
||||
|
@ -185,7 +238,9 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
|
|||
os << (u8)place_param2.has_value(); // protocol_version >= 43
|
||||
if (place_param2)
|
||||
os << *place_param2;
|
||||
|
||||
writeU8(os, wallmounted_rotate_vertical);
|
||||
touch_interaction.serialize(os);
|
||||
}
|
||||
|
||||
void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
|
||||
|
@ -260,6 +315,7 @@ void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
|
|||
place_param2 = readU8(is);
|
||||
|
||||
wallmounted_rotate_vertical = readU8(is); // 0 if missing
|
||||
touch_interaction.deSerialize(is);
|
||||
} catch(SerializationError &e) {};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue