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,353 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
bool App::settings_global_only() const { return scene_ == "main" || !source_; }
|
||||
fs::path App::ui_preferences_file() const {
|
||||
return config_.preferences.empty()
|
||||
? root_ / "userdata" / "modern-ui-preferences.json"
|
||||
: config_.preferences;
|
||||
}
|
||||
void App::reset_settings() {
|
||||
settings_draft_ = Json();
|
||||
settings_base_ = Json();
|
||||
settings_notice_.clear();
|
||||
settings_query_.clear();
|
||||
settings_jump_ = "rules";
|
||||
settings_category_ = "display";
|
||||
settings_scrolls_ = Json::object();
|
||||
settings_restore_scroll_ = false;
|
||||
settings_rendered_category_.clear();
|
||||
settings_world_.reset();
|
||||
settings_source_ = nullptr;
|
||||
settings_revision_ = 0;
|
||||
}
|
||||
void App::begin_settings() {
|
||||
if (!settings_draft_.is_null())
|
||||
return;
|
||||
settings_draft_ = {{"global", ui_preferences_}};
|
||||
if (!settings_global_only()) {
|
||||
const auto s = snap();
|
||||
settings_draft_["local"] = s.state.at("settings");
|
||||
settings_revision_ = s.revision;
|
||||
settings_world_ = s.world;
|
||||
settings_source_ = source_.get();
|
||||
}
|
||||
settings_base_ = settings_draft_;
|
||||
}
|
||||
bool App::settings_dirty() const {
|
||||
return settings_draft_.is_object() && settings_draft_ != settings_base_;
|
||||
}
|
||||
bool App::settings_valid() const {
|
||||
if (!settings_draft_.is_object() ||
|
||||
!valid_ui_preferences(settings_draft_.at("global")))
|
||||
return false;
|
||||
if (settings_global_only())
|
||||
return true;
|
||||
auto copy = snap().state;
|
||||
return edit_demo_settings(copy, settings_draft_.at("local")).empty();
|
||||
}
|
||||
std::string App::settings_row(const std::string &id, const std::string &label,
|
||||
const std::string &help,
|
||||
const std::string &control) const {
|
||||
return "<div class=\"settings-row\"><label for=\"" + esc(id) + "\"><span>" +
|
||||
esc(label) + "</span>" +
|
||||
(help.empty() ? "" : "<small>" + esc(help) + "</small>") + "</label>" +
|
||||
control + "</div>";
|
||||
}
|
||||
std::string App::settings_switch(const std::string &id,
|
||||
const std::string &scope,
|
||||
const std::string &key, bool on,
|
||||
bool mixed) const {
|
||||
return btn("settings-toggle",
|
||||
"<span class=\"settings-switch-knob\" style=\"left:" +
|
||||
std::string(mixed ? "25"
|
||||
: on ? "45"
|
||||
: "5") +
|
||||
"dp;\"></span>",
|
||||
"settings-switch " + std::string(on ? "checked " : "") +
|
||||
(mixed ? "mixed" : ""),
|
||||
"id=\"" + esc(id) + "\" role=\"switch\" aria-checked=\"" +
|
||||
(mixed ? "mixed"
|
||||
: on ? "true"
|
||||
: "false") +
|
||||
"\" data-scope=\"" + scope + "\" data-key=\"" + key + "\"");
|
||||
}
|
||||
std::string App::settings_select(
|
||||
const std::string &id,
|
||||
const std::vector<std::pair<std::string, std::string>> &rows,
|
||||
const std::string &value, const std::string &scope,
|
||||
const std::string §ion, const std::string &key) const {
|
||||
std::string out = "<select class=\"settings-select\" id=\"" + id +
|
||||
"\" data-settings-scope=\"" + scope +
|
||||
"\" data-settings-section=\"" + section +
|
||||
"\" data-settings-key=\"" + key + "\">";
|
||||
for (const auto &[v, label] : rows)
|
||||
out += "<option value=\"" + esc(v) + "\" " +
|
||||
(v == value ? "selected " : "") +
|
||||
(id == "preference-language" && v == "en" ? "disabled " : "") + ">" +
|
||||
esc(label) + "</option>";
|
||||
return out + "</select>";
|
||||
}
|
||||
std::string App::settings_option(const Json &option) const {
|
||||
const auto code = option.at("code").get<std::string>(),
|
||||
id = "game-setting-" + code;
|
||||
const auto &value =
|
||||
settings_draft_.at("local").at("cells")[size_t(settings_index(code))];
|
||||
const auto type = option.at("type").get<std::string>();
|
||||
std::string control;
|
||||
if (type == "toggle")
|
||||
control = settings_switch(id, "cell", code, value == 1);
|
||||
else if (type == "choice") {
|
||||
std::vector<std::pair<std::string, std::string>> rows;
|
||||
for (const auto &choice : option.at("choices"))
|
||||
rows.emplace_back(std::to_string(choice[0].get<int>()),
|
||||
choice[1].get<std::string>());
|
||||
control = settings_select(
|
||||
id, rows,
|
||||
value.is_number_integer() ? std::to_string(value.get<int>()) : "",
|
||||
"cell", "", code);
|
||||
} else
|
||||
control =
|
||||
"<input class=\"settings-number\" type=\"text\" id=\"" + id +
|
||||
"\" data-settings-scope=\"number\" data-settings-key=\"" + code +
|
||||
"\" data-settings-title=\"" +
|
||||
esc(option.at("label").get<std::string>()) + "\" data-settings-max=\"" +
|
||||
std::to_string(option.at("max").get<int>()) +
|
||||
"\" maxlength=\"2\" value=\"" +
|
||||
(value.is_number_integer() ? std::to_string(value.get<int>()) : "") +
|
||||
"\"/>";
|
||||
return settings_row(id, option.at("label").get<std::string>(),
|
||||
option.value("help", ""), control);
|
||||
}
|
||||
std::string App::render_settings_groups(int *count) const {
|
||||
std::istringstream words(settings_search_key(settings_query_));
|
||||
std::vector<std::string> terms;
|
||||
for (std::string word; words >> word;)
|
||||
terms.push_back(word);
|
||||
int found = 0;
|
||||
std::string out;
|
||||
for (const auto &group : settings_catalogue().at("groups")) {
|
||||
std::string rows;
|
||||
const auto matches = [&](const Json &o, const std::string &competition) {
|
||||
const auto text = settings_search_key(
|
||||
group.at("title").get<std::string>() + " " + competition + " " +
|
||||
o.at("label").get<std::string>() + " " + o.value("help", ""));
|
||||
return std::all_of(terms.begin(), terms.end(), [&](const auto &w) {
|
||||
return text.find(w) != text.npos;
|
||||
});
|
||||
};
|
||||
if (group.contains("options")) {
|
||||
for (const auto &o : group.at("options"))
|
||||
if (matches(o, "")) {
|
||||
rows += settings_option(o);
|
||||
++found;
|
||||
}
|
||||
} else
|
||||
for (const auto &competition : group.at("competitions")) {
|
||||
std::string entries;
|
||||
for (const auto &o : competition.at("options"))
|
||||
if (matches(o, competition.at("title").get<std::string>())) {
|
||||
entries += settings_option(o);
|
||||
++found;
|
||||
}
|
||||
if (!entries.empty())
|
||||
rows += "<div class=\"settings-competition\"><h4>" +
|
||||
esc(competition.at("title").get<std::string>()) + "</h4>" +
|
||||
entries + "</div>";
|
||||
}
|
||||
if (!rows.empty())
|
||||
out += "<div id=\"settings-group-" + group.at("id").get<std::string>() +
|
||||
"\" class=\"settings-group\"><h3>" +
|
||||
esc(group.at("title").get<std::string>()) + "</h3>" +
|
||||
(group.contains("help")
|
||||
? "<p class=\"settings-group-help\">" +
|
||||
esc(group.at("help").get<std::string>()) + "</p>"
|
||||
: "") +
|
||||
rows + "</div>";
|
||||
}
|
||||
if (count)
|
||||
*count = found;
|
||||
if (!found)
|
||||
out = "<p class=\"settings-search-empty\"><span>Keine Einstellungen "
|
||||
"gefunden.</span><small>Versuche einen anderen "
|
||||
"Suchbegriff.</small></p>";
|
||||
return out;
|
||||
}
|
||||
std::string App::render_settings() {
|
||||
begin_settings();
|
||||
settings_remember_scroll();
|
||||
const auto &p = settings_draft_.at("global");
|
||||
std::string nav, pane;
|
||||
const std::array<std::pair<const char *, const char *>, 4> categories{
|
||||
{{"display", "Anzeige"},
|
||||
{"sound", "Sound"},
|
||||
{"game", "Spiel"},
|
||||
{"automation", "Automatik"}}};
|
||||
for (const auto &[id, label] : categories)
|
||||
if (!settings_global_only() || std::string(id) == "display" ||
|
||||
std::string(id) == "sound")
|
||||
nav += btn("settings-category", "<span>" + std::string(label) + "</span>",
|
||||
settings_category_ == id ? "active" : "",
|
||||
"id=\"settings-tab-" + std::string(id) +
|
||||
"\" role=\"tab\" aria-selected=\"" +
|
||||
(settings_category_ == id ? "true" : "false") +
|
||||
"\" data-id=\"" + id + "\"");
|
||||
const std::map<std::string, std::pair<std::string, std::string>> captions = {
|
||||
{"display", {"Anzeige", "Bildschirm und Sprache"}},
|
||||
{"sound", {"Sound", "Lautstärke, Effekte und Spielton"}},
|
||||
{"game", {"Spiel", "Spielablauf und Darstellung nach dem Handbuch"}},
|
||||
{"automation",
|
||||
{"Automatik",
|
||||
"Aufstellung, Wechsel, Taktik, Manndeckung und Training"}}};
|
||||
std::string head = "<div><h2>" + captions.at(settings_category_).first +
|
||||
"</h2><p>" + captions.at(settings_category_).second +
|
||||
"</p></div>";
|
||||
if (settings_category_ == "display") {
|
||||
pane =
|
||||
"<div class=\"settings-group\"><h3>Gestaltung</h3><p "
|
||||
"class=\"settings-group-help\">Wähle deinen Look. Die Gestaltung wird "
|
||||
"mit „Anwenden“ übernommen.</p><div class=\"settings-theme-options\">";
|
||||
const std::array<std::array<const char *, 3>, 3> themes{
|
||||
{{{"midnight", "Midnight Glass",
|
||||
"Dunkles Glas · ruhige Blautöne · Mint"}},
|
||||
{{"silver", "Silver Glass",
|
||||
"Helles Glas · klare Schrift · Silberblau"}},
|
||||
{{"matchday", "Matchday", "Graphit · klare Flächen · Vereinsrot"}}}};
|
||||
for (const auto &t : themes) {
|
||||
const std::string type = t[0];
|
||||
const auto mini =
|
||||
"<div class=\"settings-theme-preview preview-" + type +
|
||||
"\"><div class=\"preview-background\">" +
|
||||
img("background", 647.333333, 404.583333, false, true, 650) +
|
||||
"</div><div class=\"preview-bar\"><span></span></div><div "
|
||||
"class=\"preview-widget one\"></div><div class=\"preview-widget "
|
||||
"two\"></div><div class=\"preview-app app-one\"></div><div "
|
||||
"class=\"preview-app app-two\"></div><div class=\"preview-app "
|
||||
"app-three\"></div><div class=\"preview-window\"><div "
|
||||
"class=\"preview-title\"><span></span></div><div "
|
||||
"class=\"preview-line line-one\"></div><div class=\"preview-line "
|
||||
"line-two\"></div><div class=\"preview-line "
|
||||
"line-three\"></div></div></div>";
|
||||
pane +=
|
||||
btn("settings-theme",
|
||||
mini + "<strong>" + t[1] + "</strong><small>" + t[2] + "</small>",
|
||||
"settings-theme-choice",
|
||||
"id=\"settings-theme-" + type + "\" data-id=\"" + type +
|
||||
"\" aria-pressed=\"" +
|
||||
(p.at("theme") == type ? "true" : "false") + "\"");
|
||||
}
|
||||
pane += "</div></div><div class=\"settings-group\"><h3>Bildschirm</h3>";
|
||||
const auto pref =
|
||||
[&](const char *key, const char *label,
|
||||
const std::vector<std::pair<std::string, std::string>> &rows,
|
||||
const char *help = "") {
|
||||
return settings_row(
|
||||
std::string("preference-") + key, label, help,
|
||||
settings_select(std::string("preference-") + key, rows,
|
||||
p.at("display").at(key).get<std::string>(),
|
||||
"global", "display", key));
|
||||
};
|
||||
pane += pref("mode", "Modus",
|
||||
{{"fullscreen", "Vollbild"}, {"window", "Fenster"}});
|
||||
pane += pref("resolution", "Auflösung",
|
||||
{{"1280x720", "1280 × 720"},
|
||||
{"1600x900", "1600 × 900"},
|
||||
{"1920x1080", "1920 × 1080"},
|
||||
{"2560x1440", "2560 × 1440"},
|
||||
{"3840x2160", "3840 × 2160"}},
|
||||
"Beispielauflösungen für den POC.");
|
||||
pane +=
|
||||
pref("scale", "Skalierung",
|
||||
{{"auto", "Auto"}, {"1", "1×"}, {"1.5", "1,5×"}, {"2", "2×"}}) +
|
||||
"</div><div class=\"settings-group\"><h3>Sprache</h3>" +
|
||||
pref("language", "Sprache", {{"de", "Deutsch"}, {"en", "Englisch"}}) +
|
||||
"</div>";
|
||||
} else if (settings_category_ == "sound") {
|
||||
const auto &s = p.at("sound");
|
||||
pane = "<div class=\"settings-group\"><h3>Lautstärke und Effekte</h3>";
|
||||
pane += settings_row(
|
||||
"preference-volume", "Lautstärke", "",
|
||||
"<div class=\"settings-volume\"><input type=\"range\" "
|
||||
"id=\"preference-volume\" min=\"0\" max=\"100\" step=\"1\" value=\"" +
|
||||
std::to_string(s.at("volume").get<int>()) +
|
||||
"\"/><span id=\"settings-volume-value\">" +
|
||||
std::to_string(s.at("volume").get<int>()) + " %</span></div>");
|
||||
pane +=
|
||||
settings_row("preference-effects", "Effekte", "",
|
||||
settings_switch("preference-effects", "sound", "effects",
|
||||
s.at("effects").get<bool>()));
|
||||
pane += settings_row("preference-style", "Effektstil", "",
|
||||
settings_select(
|
||||
"preference-style",
|
||||
{{"classic", "Klassisch"}, {"modern", "Modern"}},
|
||||
s.at("style").get<std::string>(), "global",
|
||||
"sound", "style")) +
|
||||
"</div>";
|
||||
if (!settings_global_only()) {
|
||||
pane += "<div class=\"settings-group\"><h3>Ton im Spiel</h3>";
|
||||
for (const auto &o : settings_catalogue().at("sound"))
|
||||
pane += settings_option(o);
|
||||
pane += "</div>";
|
||||
}
|
||||
} else if (settings_category_ == "automation") {
|
||||
const auto &a = settings_draft_.at("local").at("automation");
|
||||
int on = 0;
|
||||
for (const auto &[k, v] : a.items())
|
||||
if (v == true)
|
||||
++on;
|
||||
pane = "<div class=\"settings-automation-master\">" +
|
||||
settings_row("automation-all", "Alles automatisch",
|
||||
"Alle fünf Bereiche gemeinsam ein- oder ausschalten.",
|
||||
settings_switch("automation-all", "automation", "all",
|
||||
on == 5, on > 0 && on < 5)) +
|
||||
"</div><div class=\"settings-group\"><h3>Aufgaben an den Co-Trainer "
|
||||
"abgeben</h3><p class=\"settings-group-help\">Die fünf "
|
||||
"Automatikfunktionen des Originals. Änderungen werden erst mit "
|
||||
"„Anwenden“ übernommen.</p>";
|
||||
for (const auto &o : settings_catalogue().at("automation")) {
|
||||
const auto key = o[0].get<std::string>();
|
||||
pane += settings_row("automation-" + key, o[1].get<std::string>(),
|
||||
o[2].get<std::string>(),
|
||||
settings_switch("automation-" + key, "automation",
|
||||
key, a.at(key).get<bool>()));
|
||||
}
|
||||
pane += "</div>";
|
||||
} else {
|
||||
std::vector<std::pair<std::string, std::string>> groups;
|
||||
for (const auto &g : settings_catalogue().at("groups"))
|
||||
groups.emplace_back(g.at("id").get<std::string>(),
|
||||
g.at("title").get<std::string>());
|
||||
head += "<div class=\"settings-jump-label\"><span>Zum Abschnitt</span>" +
|
||||
settings_select("settings-jump", groups, settings_jump_, "jump", "",
|
||||
"") +
|
||||
"</div>";
|
||||
pane = "<div id=\"settings-groups\">" + render_settings_groups() + "</div>";
|
||||
}
|
||||
const std::string search =
|
||||
settings_category_ == "game"
|
||||
? "<div class=\"settings-searchbar\"><div "
|
||||
"class=\"settings-search-field\">" +
|
||||
glyph("search", 30) +
|
||||
"<input type=\"text\" id=\"settings-search\" maxlength=\"120\" "
|
||||
"value=\"" +
|
||||
esc(settings_query_) +
|
||||
"\" placeholder=\"Spieloption suchen …\"/>" +
|
||||
btn("settings-search-clear", "×", "",
|
||||
"id=\"settings-search-clear\" aria-label=\"Suche "
|
||||
"leeren\"") +
|
||||
"</div><span id=\"settings-search-count\"></span></div>"
|
||||
: "";
|
||||
settings_restore_scroll_ = true;
|
||||
return "<section class=\"settings-layout\"><nav "
|
||||
"class=\"settings-categories\" role=\"tablist\" "
|
||||
"aria-orientation=\"vertical\">" +
|
||||
nav + "</nav><div class=\"settings-pane\" data-category=\"" +
|
||||
settings_category_ + "\"><header class=\"settings-pane-header\">" +
|
||||
head + "</header>" + search +
|
||||
"<div id=\"settings-options\" class=\"settings-scroll\" "
|
||||
"tabindex=\"0\">" +
|
||||
pane +
|
||||
"</div></div></section><div class=\"settings-actions\"><p "
|
||||
"id=\"settings-draft-status\"></p>" +
|
||||
btn("settings-cancel", "Abbrechen", "", "id=\"settings-cancel\"") +
|
||||
btn("settings-apply", "Anwenden", "", "id=\"settings-apply\"") +
|
||||
"</div>";
|
||||
}
|
||||
Reference in new issue
Block a user