> 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 = "" + captions.at(settings_category_).first +
+ "
" + captions.at(settings_category_).second +
+ "
";
+ if (settings_category_ == "display") {
+ pane =
+ "Gestaltung
Wähle deinen Look. Die Gestaltung wird "
+ "mit „Anwenden“ übernommen.
";
+ const std::array
, 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 =
+ "" +
+ img("background", 647.333333, 404.583333, false, true, 650) +
+ "
";
+ pane +=
+ btn("settings-theme",
+ mini + "" + t[1] + "" + t[2] + "",
+ "settings-theme-choice",
+ "id=\"settings-theme-" + type + "\" data-id=\"" + type +
+ "\" aria-pressed=\"" +
+ (p.at("theme") == type ? "true" : "false") + "\"");
+ }
+ pane += "Bildschirm
";
+ const auto pref =
+ [&](const char *key, const char *label,
+ const std::vector> &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(),
+ "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×"}}) +
+ "Sprache
" +
+ pref("language", "Sprache", {{"de", "Deutsch"}, {"en", "Englisch"}}) +
+ "";
+ } else if (settings_category_ == "sound") {
+ const auto &s = p.at("sound");
+ pane = "Lautstärke und Effekte
";
+ pane += settings_row(
+ "preference-volume", "Lautstärke", "",
+ "
()) +
+ "\"/>" +
+ std::to_string(s.at("volume").get()) + " %
");
+ pane +=
+ settings_row("preference-effects", "Effekte", "",
+ settings_switch("preference-effects", "sound", "effects",
+ s.at("effects").get
()));
+ pane += settings_row("preference-style", "Effektstil", "",
+ settings_select(
+ "preference-style",
+ {{"classic", "Klassisch"}, {"modern", "Modern"}},
+ s.at("style").get(), "global",
+ "sound", "style")) +
+ " ";
+ if (!settings_global_only()) {
+ pane += "Ton im Spiel
";
+ for (const auto &o : settings_catalogue().at("sound"))
+ pane += settings_option(o);
+ pane += "";
+ }
+ } 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 = "" +
+ 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)) +
+ "
Aufgaben an den Co-Trainer "
+ "abgeben
Die fünf "
+ "Automatikfunktionen des Originals. Änderungen werden erst mit "
+ "„Anwenden“ übernommen.
";
+ for (const auto &o : settings_catalogue().at("automation")) {
+ const auto key = o[0].get
();
+ pane += settings_row("automation-" + key, o[1].get(),
+ o[2].get(),
+ settings_switch("automation-" + key, "automation",
+ key, a.at(key).get()));
+ }
+ pane += " ";
+ } else {
+ std::vector> groups;
+ for (const auto &g : settings_catalogue().at("groups"))
+ groups.emplace_back(g.at("id").get(),
+ g.at("title").get());
+ head += "Zum Abschnitt" +
+ settings_select("settings-jump", groups, settings_jump_, "jump", "",
+ "") +
+ "
";
+ pane = "" + render_settings_groups() + "
";
+ }
+ const std::string search =
+ settings_category_ == "game"
+ ? ""
+ : "";
+ settings_restore_scroll_ = true;
+ return "" + search +
+ "
" +
+ pane +
+ "
" +
+ btn("settings-cancel", "Abbrechen", "", "id=\"settings-cancel\"") +
+ btn("settings-apply", "Anwenden", "", "id=\"settings-apply\"") +
+ "
";
+}
diff --git a/src/modern/ui/settings_state.inc b/src/modern/ui/settings_state.inc
new file mode 100644
index 0000000..912b976
--- /dev/null
+++ b/src/modern/ui/settings_state.inc
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-3.0-only
+Json ui_preferences_ = default_ui_preferences();
+bool ui_preferences_loaded_ = false;
+std::string ui_preferences_error_;
+std::string settings_category_ = "display", settings_query_,
+ settings_jump_ = "rules", settings_notice_,
+ settings_rendered_category_;
+Json settings_draft_, settings_base_, settings_scrolls_ = Json::object();
+uint64_t settings_revision_ = 0;
+std::shared_ptr settings_world_;
+const DataSource *settings_source_ = nullptr;
+bool settings_restore_scroll_ = false;
+fs::path ui_preferences_file() const;
+void reset_settings();
+void begin_settings();
+bool settings_global_only() const;
+bool settings_dirty() const;
+bool settings_valid() const;
+std::string render_settings_groups(int *count = nullptr) const;
+std::string settings_row(const std::string &, const std::string &,
+ const std::string &, const std::string &) const;
+std::string settings_switch(const std::string &, const std::string &,
+ const std::string &, bool, bool = false) const;
+std::string settings_option(const Json &) const;
+std::string
+settings_select(const std::string &,
+ const std::vector> &,
+ const std::string &, const std::string &, const std::string &,
+ const std::string &) const;
+bool settings_action(const std::string &, Rml::Element *);
+bool settings_event(Rml::Event &);
+bool settings_change(Rml::Element *);
+void settings_filter();
+void settings_sync();
+void settings_remember_scroll();
+void settings_after_render();
+void settings_commit();
diff --git a/src/modern/ui/ui_preferences.cpp b/src/modern/ui/ui_preferences.cpp
new file mode 100644
index 0000000..e5625fd
--- /dev/null
+++ b/src/modern/ui/ui_preferences.cpp
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-3.0-only
+#include "modern/ui/ui_preferences.hpp"
+#include
+namespace bmh::modern_ui {
+namespace {
+bool keys(const Json &v, std::initializer_list names) {
+ if (!v.is_object() || v.size() != names.size())
+ return false;
+ for (const auto *n : names)
+ if (!v.contains(n))
+ return false;
+ return true;
+}
+bool choice(const Json &v, std::initializer_list names) {
+ if (!v.is_string())
+ return false;
+ for (const auto *n : names)
+ if (v == n)
+ return true;
+ return false;
+}
+} // namespace
+Json default_ui_preferences() {
+ return {{"theme", "midnight"},
+ {"display",
+ {{"mode", "window"},
+ {"resolution", "1920x1080"},
+ {"scale", "auto"},
+ {"language", "de"}}},
+ {"sound", {{"volume", 80}, {"effects", true}, {"style", "classic"}}}};
+}
+bool valid_ui_preferences(const Json &v) {
+ try {
+ if (!keys(v, {"theme", "display", "sound"}) ||
+ !choice(v.at("theme"), {"midnight", "silver", "matchday"}))
+ return false;
+ const auto &d = v.at("display"), &s = v.at("sound");
+ return keys(d, {"mode", "resolution", "scale", "language"}) &&
+ keys(s, {"volume", "effects", "style"}) &&
+ choice(d.at("mode"), {"window", "fullscreen"}) &&
+ choice(d.at("resolution"), {"1280x720", "1600x900", "1920x1080",
+ "2560x1440", "3840x2160"}) &&
+ choice(d.at("scale"), {"auto", "1", "1.5", "2"}) &&
+ d.at("language") == "de" && s.at("volume").is_number_integer() &&
+ s.at("volume") >= 0 && s.at("volume") <= 100 &&
+ s.at("effects").is_boolean() &&
+ choice(s.at("style"), {"classic", "modern"});
+ } catch (const Json::exception &) {
+ return false;
+ }
+}
+Json legacy_ui_preferences(const Json &legacy) {
+ auto next = default_ui_preferences();
+ if (!legacy.is_object())
+ return next;
+ if (legacy.contains("theme"))
+ next["theme"] = legacy["theme"];
+ for (const auto *key : {"mode", "resolution", "scale"})
+ if (legacy.contains(key))
+ next["display"][key] = legacy[key];
+ for (const auto *key : {"volume", "effects"})
+ if (legacy.contains(key))
+ next["sound"][key] = legacy[key];
+ if (legacy.contains("sound-style"))
+ next["sound"]["style"] = legacy["sound-style"];
+ return valid_ui_preferences(next) ? next : default_ui_preferences();
+}
+Json load_ui_preferences(const fs::path &path) {
+ if (!fs::exists(path))
+ return default_ui_preferences();
+ const auto envelope = Json::parse(editor::read_file(path));
+ if (!keys(envelope, {"schema", "preferences"}) ||
+ envelope.at("schema") != "openbmh-ui-preferences@1" ||
+ !valid_ui_preferences(envelope.at("preferences")))
+ throw editor::Error("invalid_ui_preferences");
+ return envelope.at("preferences");
+}
+void save_ui_preferences(const fs::path &path, const Json &v) {
+ if (!valid_ui_preferences(v))
+ throw editor::Error("invalid_ui_preferences");
+ if (!path.parent_path().empty())
+ fs::create_directories(path.parent_path());
+ editor::write_atomic(
+ path,
+ Json{{"schema", "openbmh-ui-preferences@1"}, {"preferences", v}}.dump(2));
+}
+} // namespace bmh::modern_ui
diff --git a/src/modern/ui/ui_preferences.hpp b/src/modern/ui/ui_preferences.hpp
new file mode 100644
index 0000000..ee833ce
--- /dev/null
+++ b/src/modern/ui/ui_preferences.hpp
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-3.0-only
+#pragma once
+#include "modern/ui/catalog.hpp"
+namespace bmh::modern_ui {
+Json default_ui_preferences();
+bool valid_ui_preferences(const Json &);
+Json legacy_ui_preferences(const Json &);
+// A missing file is a normal first launch. Malformed files are reported and
+// left intact; callers decide how to present the error without hiding it.
+Json load_ui_preferences(const fs::path &);
+void save_ui_preferences(const fs::path &, const Json &);
+} // namespace bmh::modern_ui
diff --git a/src/modern/ui/view.cpp b/src/modern/ui/view.cpp
index 71fcd66..f2172d7 100644
--- a/src/modern/ui/view.cpp
+++ b/src/modern/ui/view.cpp
@@ -19,6 +19,8 @@
#include "modern/ui/demo_stadium.hpp"
#include "modern/ui/demo_statistics.hpp"
#include "modern/ui/demo_news.hpp"
+#include "modern/ui/demo_settings.hpp"
+#include "modern/ui/ui_preferences.hpp"
#include "modern/ui/desktop.hpp"
#include "modern/ui/gui_assets.hpp"
#include "modern/ui/model.hpp"
@@ -408,8 +410,7 @@ class App final : public Rml::EventListener {
int last_tactic_ = 0;
bool draw_playing_ = true;
uint64_t draw_at_ = 0;
- std::string settings_category_ = "display";
- Json draft_preferences_;
+#include "modern/ui/settings_state.inc"
std::map selections_;
Rml::Element *element(const std::string &id) const {
return doc_->GetElementById(id);
@@ -585,6 +586,7 @@ class App final : public Rml::EventListener {
return nullptr;
}
void load_world(const fs::path &file) {
+ reset_settings();
auto next = Catalog::open(file);
auto demo = std::make_unique(next);
ticket_.reset();
@@ -807,7 +809,7 @@ class App final : public Rml::EventListener {
pending_selects_.erase(id);
if (auto *select = element(id)) {
// Menu-local select handlers receive only a committed/cancelled value.
- if ((menu_ == "lineup" && id.starts_with("lineup-")) ||
+ if (menu_ == "settings" || (menu_ == "lineup" && id.starts_with("lineup-")) ||
(menu_ == "transfer" && id.starts_with("transfer-")))
select->DispatchEvent("change", {});
else change(select);
@@ -853,6 +855,7 @@ class App final : public Rml::EventListener {
// A window-close save prompt can leave a menu while its local dialog is
// open. Never keep that menu's modal/input gate in the destination view.
if (menu_ != last_rendered_menu_) {
+ if(last_rendered_menu_=="settings")reset_settings();
statistics_trophy_ = Json::object();
planning_dialog_.clear();
career_view_["dialog"] = ""; career_view_["return_focus"] = "";
@@ -1072,6 +1075,7 @@ class App final : public Rml::EventListener {
if (menu_ == "transfer") transfer_restore_scroll();
if (menu_ == "scorers" || menu_ == "fairplay") rankings_restore_scroll();
if (menu_ == "statistics") statistics_restore_scroll();
+ if (menu_ == "settings") settings_after_render();
if (!report_option_to_scroll_.empty()) {
if (auto *node = element(report_option_to_scroll_))
node->ScrollIntoView(Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
@@ -1223,8 +1227,15 @@ public:
"mouseup", "mousemove", "contextmenu", "focus"})
doc_->AddEventListener(event, this, Rml::String(event) == "keydown" ||
Rml::String(event) == "blur" || Rml::String(event) == "focus");
- if (!config_.theme.empty())
- theme_ = config_.theme;
+ try {
+ ui_preferences_loaded_=fs::exists(ui_preferences_file());
+ ui_preferences_=load_ui_preferences(ui_preferences_file());
+ } catch(const std::exception &) {
+ ui_preferences_error_="Die gespeicherten UI-Einstellungen konnten nicht geladen werden. Die Standardwerte werden verwendet.";
+ ui_preferences_=default_ui_preferences();ui_preferences_loaded_=false;
+ }
+ theme_=ui_preferences_.at("theme").get();
+ if (!config_.theme.empty()) theme_ = config_.theme;
if (theme_ != "midnight" && theme_ != "silver" && theme_ != "matchday")
throw editor::Error("Unbekanntes Modern-Theme.");
collect_mods();
@@ -1243,19 +1254,21 @@ public:
if (!config_.state.empty() && source_) {
desktop_ = source_->load(config_.state);
saved_desktop_ = desktop_;
- if (config_.theme.empty())
- theme_ = snap().state.at("preferences").value("theme", theme_);
+ if (config_.theme.empty() && !ui_preferences_loaded_) {
+ ui_preferences_=legacy_ui_preferences(snap().state.at("preferences"));
+ theme_=ui_preferences_.at("theme").get();
+ }
}
- if (source_ && !config_.theme.empty()) {
- auto preferences = snap().state.at("preferences");
- preferences["theme"] = theme_;
- apply("preferences", preferences);
+ if (!config_.theme.empty()) {
+ // A capture/CLI override is visual only and never edits a career.
+ ui_preferences_["theme"]=theme_;
}
if (scene_ == "manager" && selections_["manager-name"].empty())
selections_["manager-name"] = "Alex Berger";
doc_->Show();
resize();
render();
+ if (!ui_preferences_error_.empty()) message(ui_preferences_error_);
SDL_ShowWindow(video_.window);
frame();
} catch (...) {
@@ -1381,6 +1394,8 @@ public:
#include "modern/ui/statistics_controls.inc"
#include "modern/ui/news_screens.inc"
#include "modern/ui/news_controls.inc"
+#include "modern/ui/settings_screens.inc"
+#include "modern/ui/settings_controls.inc"
} // namespace
bool is_modern_world(const fs::path &file) {
try {
diff --git a/src/modern/ui/view.hpp b/src/modern/ui/view.hpp
index 24f8b36..8e84ab1 100644
--- a/src/modern/ui/view.hpp
+++ b/src/modern/ui/view.hpp
@@ -4,7 +4,7 @@
namespace bmh::modern_ui {
struct Config {
- std::filesystem::path world, gui_assets, screenshot, commands, state;
+ std::filesystem::path world, gui_assets, screenshot, commands, state, preferences;
std::string scene = "main", theme;
int width = 1280, height = 720;
};
diff --git a/tests/modern_control_layout_tests.cpp b/tests/modern_control_layout_tests.cpp
index 8c617d9..669a433 100644
--- a/tests/modern_control_layout_tests.cpp
+++ b/tests/modern_control_layout_tests.cpp
@@ -113,7 +113,7 @@ int main(int argc, char **argv) try {
std::ostringstream css;
for (const auto *name : {"theme.rcss", "ticket.rcss", "sports.rcss",
"calendar.rcss", "reports.rcss", "report_details.rcss",
- "report_pickers.rcss", "indoor.rcss", "planning.rcss", "almanac.rcss", "finance.rcss", "bank.rcss", "contracts.rcss", "amount.rcss", "environment.rcss", "advertising.rcss", "toto.rcss", "lineup.rcss", "transfer.rcss", "career_reports.rcss", "rankings.rcss", "stadium.rcss", "statistics.rcss", "news.rcss"}) {
+ "report_pickers.rcss", "indoor.rcss", "planning.rcss", "almanac.rcss", "finance.rcss", "bank.rcss", "contracts.rcss", "amount.rcss", "environment.rcss", "advertising.rcss", "toto.rcss", "lineup.rcss", "transfer.rcss", "career_reports.rcss", "rankings.rcss", "stadium.rcss", "statistics.rcss", "news.rcss", "settings.rcss"}) {
std::ifstream input(root / name);
require(bool(input), "Production stylesheet missing");
css << input.rdbuf() << '\n';
@@ -181,6 +181,13 @@ int main(int argc, char **argv) try {
select("manager-strength") + "