Port settings and automation to the Modern UI (by Codex)
This commit is contained in:
1 parent
8222926d9c
commit
adb4166a80
26 files changed
+2734
-179
No files matched your search
@@ -0,0 +1,460 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
void App::settings_remember_scroll() {
|
||||
if (menu_ == "settings" && last_rendered_menu_ == "settings")
|
||||
if (auto *pane = element("settings-options"))
|
||||
settings_scrolls_[settings_rendered_category_.empty()
|
||||
? settings_category_
|
||||
: settings_rendered_category_] =
|
||||
pane->GetScrollTop();
|
||||
}
|
||||
void App::settings_after_render() {
|
||||
if (menu_ != "settings")
|
||||
return;
|
||||
settings_sync();
|
||||
settings_rendered_category_ = settings_category_;
|
||||
if (settings_category_ == "game") {
|
||||
int count = 0;
|
||||
render_settings_groups(&count);
|
||||
if (auto *label = element("settings-search-count"))
|
||||
label->SetInnerRML(std::to_string(count) + (settings_query_.empty()
|
||||
? " Optionen"
|
||||
: " von 142 Optionen"));
|
||||
}
|
||||
if (settings_restore_scroll_) {
|
||||
context_->Update();
|
||||
if (auto *pane = element("settings-options"))
|
||||
pane->SetScrollTop(settings_scrolls_.value(settings_category_, 0.f));
|
||||
settings_restore_scroll_ = false;
|
||||
}
|
||||
context_->Update();
|
||||
}
|
||||
void App::settings_sync() {
|
||||
if (menu_ != "settings" || !settings_draft_.is_object())
|
||||
return;
|
||||
const bool valid = settings_valid(), changed = settings_dirty();
|
||||
if (auto *button = element("settings-apply")) {
|
||||
if (valid && changed)
|
||||
button->RemoveAttribute("disabled");
|
||||
else
|
||||
button->SetAttribute("disabled", true);
|
||||
}
|
||||
if (auto *status = element("settings-draft-status"))
|
||||
status->SetInnerRML(esc(!valid ? "Bitte die Spielwerte prüfen."
|
||||
: !settings_notice_.empty() ? settings_notice_
|
||||
: changed ? "Nicht angewendete Änderungen"
|
||||
: "Keine Änderungen"));
|
||||
if (auto *style = element("preference-style")) {
|
||||
if (settings_draft_.at("global").at("sound").at("effects") == true)
|
||||
style->RemoveAttribute("disabled");
|
||||
else
|
||||
style->SetAttribute("disabled", true);
|
||||
}
|
||||
Rml::ElementList toggles;
|
||||
doc_->QuerySelectorAll(toggles, ".settings-switch");
|
||||
for (auto *toggle : toggles) {
|
||||
const auto scope = toggle->GetAttribute<Rml::String>("data-scope", ""),
|
||||
key = toggle->GetAttribute<Rml::String>("data-key", "");
|
||||
bool on = false, mixed = false;
|
||||
if (scope == "sound")
|
||||
on = settings_draft_["global"]["sound"].at(key).get<bool>();
|
||||
else if (scope == "cell")
|
||||
on = settings_draft_["local"]["cells"][size_t(settings_index(key))] == 1;
|
||||
else if (scope == "automation") {
|
||||
const auto &a = settings_draft_["local"]["automation"];
|
||||
if (key == "all") {
|
||||
int n = 0;
|
||||
for (const auto &[k, v] : a.items())
|
||||
if (v == true)
|
||||
++n;
|
||||
on = n == 5;
|
||||
mixed = n > 0 && n < 5;
|
||||
} else
|
||||
on = a.at(key).get<bool>();
|
||||
}
|
||||
toggle->SetClass("checked", on);
|
||||
toggle->SetClass("mixed", mixed);
|
||||
toggle->SetAttribute("aria-checked", mixed ? "mixed"
|
||||
: on ? "true"
|
||||
: "false");
|
||||
if (auto *knob = toggle->QuerySelector(".settings-switch-knob"))
|
||||
knob->SetProperty("left", std::string(mixed ? "25"
|
||||
: on ? "45"
|
||||
: "5") +
|
||||
"dp");
|
||||
}
|
||||
for (const auto *theme : {"midnight", "silver", "matchday"})
|
||||
if (auto *button = element(std::string("settings-theme-") + theme))
|
||||
button->SetAttribute(
|
||||
"aria-pressed",
|
||||
settings_draft_["global"]["theme"] == theme ? "true" : "false");
|
||||
if (auto *clear = element("settings-search-clear")) {
|
||||
if (settings_query_.empty())
|
||||
clear->SetAttribute("disabled", true);
|
||||
else
|
||||
clear->RemoveAttribute("disabled");
|
||||
}
|
||||
dirty_ = true;
|
||||
}
|
||||
void App::settings_filter() {
|
||||
if (menu_ != "settings" || settings_category_ != "game")
|
||||
return;
|
||||
int count = 0;
|
||||
const auto html = render_settings_groups(&count);
|
||||
setting_menu_markup_ = true;
|
||||
if (auto *groups = element("settings-groups"))
|
||||
groups->SetInnerRML(html);
|
||||
setting_menu_markup_ = false;
|
||||
if (auto *label = element("settings-search-count"))
|
||||
label->SetInnerRML(std::to_string(count) + (settings_query_.empty()
|
||||
? " Optionen"
|
||||
: " von 142 Optionen"));
|
||||
if (auto *select = dynamic_cast<Rml::ElementFormControlSelect *>(
|
||||
element("settings-jump"))) {
|
||||
std::string choices;
|
||||
std::string first;
|
||||
for (const auto &g : settings_catalogue().at("groups")) {
|
||||
const auto id = g.at("id").get<std::string>();
|
||||
const bool shown = element("settings-group-" + id) != nullptr;
|
||||
if (shown && first.empty())
|
||||
first = id;
|
||||
choices += "<option value=\"" + id + "\" " + (shown ? "" : "disabled") +
|
||||
">" + esc(g.at("title").get<std::string>()) + "</option>";
|
||||
}
|
||||
if (!element("settings-group-" + settings_jump_))
|
||||
settings_jump_ = first;
|
||||
setting_menu_markup_ = true;
|
||||
select->SetInnerRML(choices);
|
||||
select->SetValue(settings_jump_);
|
||||
setting_menu_markup_ = false;
|
||||
if (count)
|
||||
select->RemoveAttribute("disabled");
|
||||
else
|
||||
select->SetAttribute("disabled", true);
|
||||
}
|
||||
settings_scrolls_["game"] = 0.f;
|
||||
context_->Update();
|
||||
if (auto *pane = element("settings-options"))
|
||||
pane->SetScrollTop(0);
|
||||
center_selects(*doc_);
|
||||
settings_sync();
|
||||
}
|
||||
bool App::settings_change(Rml::Element *target) {
|
||||
if (menu_ != "settings" || !target || !settings_draft_.is_object())
|
||||
return false;
|
||||
const auto id = target->GetId();
|
||||
if (id == "settings-search") {
|
||||
settings_query_ = field(id);
|
||||
settings_filter();
|
||||
return true;
|
||||
}
|
||||
if (id == "preference-volume") {
|
||||
try {
|
||||
settings_draft_["global"]["sound"]["volume"] =
|
||||
std::clamp(int(std::lround(std::stod(field(id)))), 0, 100);
|
||||
} catch (const std::exception &) {
|
||||
return true;
|
||||
}
|
||||
if (auto *label = element("settings-volume-value"))
|
||||
label->SetInnerRML(
|
||||
std::to_string(
|
||||
settings_draft_["global"]["sound"]["volume"].get<int>()) +
|
||||
" %");
|
||||
settings_notice_.clear();
|
||||
settings_sync();
|
||||
return true;
|
||||
}
|
||||
const auto scope =
|
||||
target->GetAttribute<Rml::String>("data-settings-scope", "");
|
||||
if (scope.empty())
|
||||
return false;
|
||||
const auto key = target->GetAttribute<Rml::String>("data-settings-key", ""),
|
||||
section =
|
||||
target->GetAttribute<Rml::String>("data-settings-section", "");
|
||||
const auto value = field(id);
|
||||
if (scope == "jump") {
|
||||
if (auto *group = element("settings-group-" + value)) {
|
||||
settings_jump_ = value;
|
||||
if (auto *pane = element("settings-options")) {
|
||||
const auto offset =
|
||||
group->GetAbsoluteOffset().y - pane->GetAbsoluteOffset().y;
|
||||
pane->SetScrollTop(pane->GetScrollTop() + offset);
|
||||
settings_scrolls_["game"] = pane->GetScrollTop();
|
||||
}
|
||||
}
|
||||
dirty_ = true;
|
||||
return true;
|
||||
}
|
||||
if (scope == "global")
|
||||
settings_draft_["global"][section][key] = value;
|
||||
else if (scope == "number" || scope == "cell") {
|
||||
const int index = settings_index(key);
|
||||
if (index < 0)
|
||||
return true;
|
||||
const bool digits = !value.empty() && value.size() <= 2 &&
|
||||
std::all_of(value.begin(), value.end(), [](char c) {
|
||||
return c >= '0' && c <= '9';
|
||||
});
|
||||
settings_draft_["local"]["cells"][size_t(index)] =
|
||||
digits ? Json(std::stoi(value)) : Json();
|
||||
}
|
||||
settings_notice_.clear();
|
||||
settings_sync();
|
||||
return true;
|
||||
}
|
||||
void App::settings_commit() {
|
||||
if (!settings_dirty() || !settings_valid())
|
||||
return;
|
||||
const bool local = !settings_global_only();
|
||||
if (local &&
|
||||
(settings_source_ != source_.get() || settings_world_ != world_ ||
|
||||
settings_revision_ != snap().revision)) {
|
||||
settings_notice_ =
|
||||
"Die Anzeige ist veraltet. Bitte schließen und erneut öffnen.";
|
||||
settings_sync();
|
||||
return;
|
||||
}
|
||||
const auto global = settings_draft_.at("global");
|
||||
const bool changed_global = global != settings_base_.at("global");
|
||||
const bool changed_local =
|
||||
local && settings_draft_.at("local") != settings_base_.at("local");
|
||||
const auto file = ui_preferences_file();
|
||||
const bool had_file = fs::exists(file);
|
||||
std::string previous;
|
||||
try {
|
||||
if (changed_global) {
|
||||
if (had_file)
|
||||
previous = editor::read_file(file);
|
||||
save_ui_preferences(file, global);
|
||||
}
|
||||
if (changed_local) {
|
||||
const auto result = source_->apply(
|
||||
{settings_revision_, "settings", settings_draft_.at("local")});
|
||||
if (!result.ok) {
|
||||
if (changed_global) {
|
||||
if (had_file)
|
||||
editor::write_atomic(file, previous);
|
||||
else
|
||||
fs::remove(file);
|
||||
}
|
||||
settings_notice_ =
|
||||
"Änderungen konnten nicht übernommen werden. Bitte erneut öffnen.";
|
||||
settings_sync();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (const std::exception &) {
|
||||
settings_notice_ = "Änderungen konnten nicht gespeichert werden.";
|
||||
settings_sync();
|
||||
return;
|
||||
}
|
||||
ui_preferences_ = global;
|
||||
theme_ = global.at("theme").get<std::string>();
|
||||
if (changed_global)
|
||||
ui_preferences_loaded_ = true;
|
||||
reset_settings();
|
||||
menu_.clear();
|
||||
refresh();
|
||||
}
|
||||
bool App::settings_action(const std::string &action, Rml::Element *target) {
|
||||
if (!action.starts_with("settings-"))
|
||||
return false;
|
||||
if (menu_ != "settings")
|
||||
return false;
|
||||
begin_settings();
|
||||
if (action == "settings-apply") {
|
||||
settings_commit();
|
||||
return true;
|
||||
}
|
||||
if (action == "settings-cancel") {
|
||||
reset_settings();
|
||||
menu_.clear();
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
const auto id =
|
||||
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
|
||||
if (action == "settings-category") {
|
||||
if (id != "display" && id != "sound" &&
|
||||
(settings_global_only() || (id != "game" && id != "automation")))
|
||||
return true;
|
||||
settings_remember_scroll();
|
||||
settings_category_ = id;
|
||||
focus_after_render_ = "settings-tab-" + id;
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (action == "settings-theme") {
|
||||
if (id == "midnight" || id == "silver" || id == "matchday")
|
||||
settings_draft_["global"]["theme"] = id;
|
||||
settings_notice_.clear();
|
||||
settings_sync();
|
||||
return true;
|
||||
}
|
||||
if (action == "settings-search-clear") {
|
||||
settings_query_.clear();
|
||||
if (auto *input =
|
||||
dynamic_cast<Rml::ElementFormControl *>(element("settings-search")))
|
||||
input->SetValue("");
|
||||
settings_filter();
|
||||
if (auto *input = element("settings-search"))
|
||||
input->Focus(true);
|
||||
return true;
|
||||
}
|
||||
if (!target)
|
||||
return true;
|
||||
if (action == "settings-toggle") {
|
||||
const auto scope = target->GetAttribute<Rml::String>("data-scope", ""),
|
||||
key = target->GetAttribute<Rml::String>("data-key", "");
|
||||
if (scope == "sound" && key == "effects")
|
||||
settings_draft_["global"]["sound"][key] =
|
||||
!settings_draft_["global"]["sound"][key].get<bool>();
|
||||
else if (!settings_global_only() && scope == "cell" &&
|
||||
settings_index(key) >= 0) {
|
||||
auto &value =
|
||||
settings_draft_["local"]["cells"][size_t(settings_index(key))];
|
||||
value = value == 1 ? 0 : 1;
|
||||
} else if (!settings_global_only() && scope == "automation") {
|
||||
auto &a = settings_draft_["local"]["automation"];
|
||||
if (key == "all") {
|
||||
const bool on = !std::all_of(a.begin(), a.end(),
|
||||
[](const Json &v) { return v == true; });
|
||||
for (auto &[k, v] : a.items())
|
||||
v = on;
|
||||
} else if (a.contains(key))
|
||||
a[key] = !a[key].get<bool>();
|
||||
}
|
||||
settings_notice_.clear();
|
||||
settings_sync();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool App::settings_event(Rml::Event &event) {
|
||||
if (menu_ != "settings" || !popup_.empty() || amount_)
|
||||
return false;
|
||||
begin_settings();
|
||||
const auto type = event.GetType();
|
||||
auto *target = event.GetTargetElement();
|
||||
if (type == "input" || type == "change") {
|
||||
if (auto *select = dynamic_cast<Rml::ElementFormControlSelect *>(target);
|
||||
select && select->IsSelectBoxVisible()) {
|
||||
pending_selects_.insert(select->GetId());
|
||||
dirty_ = true;
|
||||
return true;
|
||||
}
|
||||
if (settings_change(target))
|
||||
return true;
|
||||
}
|
||||
const auto pressed_key =
|
||||
type == "keydown" ? Rml::Input::KeyIdentifier(
|
||||
event.GetParameter<int>("key_identifier", 0))
|
||||
: Rml::Input::KI_UNKNOWN;
|
||||
const int digit =
|
||||
pressed_key >= Rml::Input::KI_0 && pressed_key <= Rml::Input::KI_9
|
||||
? pressed_key - Rml::Input::KI_0
|
||||
: pressed_key >= Rml::Input::KI_NUMPAD0 &&
|
||||
pressed_key <= Rml::Input::KI_NUMPAD9
|
||||
? pressed_key - Rml::Input::KI_NUMPAD0
|
||||
: -1;
|
||||
const bool numeric_key =
|
||||
type == "keydown" && !event.GetParameter<int>("ctrl_key", 0) &&
|
||||
!event.GetParameter<int>("meta_key", 0) &&
|
||||
!event.GetParameter<int>("alt_key", 0) &&
|
||||
(digit >= 0 || pressed_key == Rml::Input::KI_RETURN ||
|
||||
pressed_key == Rml::Input::KI_NUMPADENTER ||
|
||||
pressed_key == Rml::Input::KI_SPACE ||
|
||||
pressed_key == Rml::Input::KI_F4 || pressed_key == Rml::Input::KI_UP ||
|
||||
pressed_key == Rml::Input::KI_DOWN ||
|
||||
pressed_key == Rml::Input::KI_BACK ||
|
||||
pressed_key == Rml::Input::KI_DELETE);
|
||||
if (((type == "mousedown" && event.GetParameter<int>("button", 0) == 0) ||
|
||||
numeric_key) &&
|
||||
target->HasAttribute("data-settings-max")) {
|
||||
const auto key = target->GetAttribute<Rml::String>("data-settings-key", "");
|
||||
const int index = settings_index(key),
|
||||
maximum = target->GetAttribute<int>("data-settings-max", 9);
|
||||
if (index >= 0) {
|
||||
const auto &old = settings_draft_["local"]["cells"][size_t(index)];
|
||||
AmountRequest request;
|
||||
request.title = target->GetAttribute<Rml::String>("data-settings-title",
|
||||
"Spieloption " + key);
|
||||
request.return_focus = target->GetId();
|
||||
request.value = old.is_number_integer() ? old.get<int>() : 0;
|
||||
request.max = maximum;
|
||||
request.step = request.suggestion_step = 1;
|
||||
request.review = false;
|
||||
request.accept = [this, index](int64_t value) {
|
||||
if (menu_ != "settings" || !settings_draft_.is_object())
|
||||
return false;
|
||||
settings_draft_["local"]["cells"][size_t(index)] = value;
|
||||
settings_notice_.clear();
|
||||
return true;
|
||||
};
|
||||
open_amount(std::move(request));
|
||||
if (amount_ && digit >= 0) {
|
||||
amount_->raw = std::to_string(digit);
|
||||
amount_select_all_ = false;
|
||||
amount_caret_end_ = true;
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (type != "keydown")
|
||||
return false;
|
||||
const auto key =
|
||||
Rml::Input::KeyIdentifier(event.GetParameter<int>("key_identifier", 0));
|
||||
if (auto *select = dynamic_cast<Rml::ElementFormControlSelect *>(target);
|
||||
select && select->IsSelectBoxVisible())
|
||||
return false;
|
||||
if (key == Rml::Input::KI_ESCAPE && target->GetId() == "settings-search" &&
|
||||
!settings_query_.empty()) {
|
||||
settings_action("settings-search-clear", nullptr);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (key == Rml::Input::KI_TAB) {
|
||||
Rml::ElementList controls;
|
||||
if (auto *canvas = element("canvas"))
|
||||
canvas->QuerySelectorAll(
|
||||
controls,
|
||||
".window button,.window input,.window select,#settings-options");
|
||||
std::erase_if(controls, [](auto *node) {
|
||||
return !node->IsVisible(true) || node->HasAttribute("disabled");
|
||||
});
|
||||
if (!controls.empty()) {
|
||||
const auto it = std::find(controls.begin(), controls.end(),
|
||||
context_->GetFocusElement());
|
||||
const int pos = it == controls.end() ? -1 : int(it - controls.begin()),
|
||||
n = int(controls.size());
|
||||
const bool back = event.GetParameter<int>("shift_key", 0) != 0;
|
||||
const int next =
|
||||
pos < 0 ? (back ? n - 1 : 0) : (pos + (back ? -1 : 1) + n) % n;
|
||||
controls[size_t(next)]->Focus(true);
|
||||
controls[size_t(next)]->ScrollIntoView(
|
||||
Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (target->GetId().starts_with("settings-tab-") &&
|
||||
(key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN ||
|
||||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
|
||||
const std::vector<std::string> tabs =
|
||||
settings_global_only() ? std::vector<std::string>{"display", "sound"}
|
||||
: std::vector<std::string>{"display", "sound",
|
||||
"game", "automation"};
|
||||
const auto it = std::find(tabs.begin(), tabs.end(), settings_category_);
|
||||
int pos = int(it - tabs.begin());
|
||||
pos = key == Rml::Input::KI_HOME ? 0
|
||||
: key == Rml::Input::KI_END
|
||||
? int(tabs.size() - 1)
|
||||
: (pos + (key == Rml::Input::KI_UP ? -1 : 1) + int(tabs.size())) %
|
||||
int(tabs.size());
|
||||
if (auto *button = element("settings-tab-" + tabs[size_t(pos)]))
|
||||
settings_action("settings-category", button);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user