Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
grorp committed Feb 27, 2025
1 parent a72fd4e commit 42e7035
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
7 changes: 2 additions & 5 deletions src/gui/touchcontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,11 @@ void TouchControls::settingChangedCallback(const std::string &name, void *data)
void TouchControls::readSettings()
{
const std::string &s = g_settings->get("touch_interaction_style");
int result;
if (string_to_enum(es_TouchInteractionStyle, result, s)) {
m_interaction_style = (TouchInteractionStyle)result;
} else {
if (!string_to_enum(es_TouchInteractionStyle, m_interaction_style, s)) {
m_interaction_style = TAP;
warningstream << "Invalid touch_interaction_style value" << std::endl;
}

m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold");
m_long_tap_delay = g_settings->getU16("touch_long_tap_delay");
m_fixed_joystick = g_settings->getBool("fixed_virtual_joystick");
Expand Down
2 changes: 1 addition & 1 deletion src/gui/touchcontrols.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TouchControls
return res;
}

bool isShootlineAvailable() const { return m_interaction_style == TAP; }
bool isShootlineAvailable() { return m_interaction_style == TAP; }

/**
* Returns a line which describes what the player is pointing at.
Expand Down
36 changes: 17 additions & 19 deletions src/gui/touchscreenlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,6 @@ ButtonLayout ButtonLayout::postProcessLoaded(const ButtonMap &data)
else
layout.preserved_disallowed.emplace(id, meta);
}

// Force required buttons to exist.
// This is necessary for cases where the user changes "touch_interaction_style"
// to "buttons_crosshair", but already has a button layout without dig/place
// buttons configured.
// This may result in overlapping buttons until the user customizes them
// (could be fixed by resolving collisions).
for (u8 i = 0; i < touch_gui_button_id_END; i++) {
touch_gui_button_id btn = (touch_gui_button_id)i;

if (isButtonRequired(btn)) {
assert(default_data.count(btn) > 0); // required button must be in default layout
assert(layout.layout.count(btn) == data.count(btn)); // required button cannot be disallowed

if (!layout.layout.count(btn))
layout.layout.emplace(btn, default_data.at(btn));
}
}

return layout;
}

Expand Down Expand Up @@ -300,6 +281,7 @@ std::vector<touch_gui_button_id> ButtonLayout::getMissingButtons()
void ButtonLayout::serializeJson(std::ostream &os) const
{
Json::Value root = Json::objectValue;
root["version"] = 1;
root["layout"] = Json::objectValue;

for (const auto &list : {layout, preserved_disallowed}) {
Expand Down Expand Up @@ -333,6 +315,12 @@ ButtonLayout::ButtonMap ButtonLayout::deserializeJson(std::istream &is)
Json::Value root;
is >> root;

u8 version;
if (root["version"].isUInt())
version = root["version"].asUInt();
else
version = 0;

if (!root["layout"].isObject())
throw Json::RuntimeError("invalid type for layout");

Expand Down Expand Up @@ -362,6 +350,16 @@ ButtonLayout::ButtonMap ButtonLayout::deserializeJson(std::istream &is)
data.emplace(id, meta);
}

if (version < 1) {
// Version 0 did not have dig/place buttons, so add them in.
// Otherwise, the missing buttons would cause confusion if the user
// switches to "touch_interaction_style = buttons_crosshair".
// This may result in overlapping buttons (could be fixed by resolving
// collisions in postProcessLoaded).
data.emplace(dig_id, default_data.at(dig_id));
data.emplace(place_id, default_data.at(place_id));
}

return data;
}

Expand Down

0 comments on commit 42e7035

Please sign in to comment.