From 758315f53352fff165347b2b0145f48cb5ca4fcb Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 5 Sep 2016 13:43:58 -0400 Subject: [PATCH] Enable GUI2 themes --- data/gui/_main.cfg | 5 +++++ data/gui/default.cfg | 2 +- src/gui/widgets/settings.cpp | 35 ++++++++++++++++++++++++----------- src/preferences.cpp | 10 ++++++++++ src/preferences.hpp | 3 +++ 5 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 data/gui/_main.cfg diff --git a/data/gui/_main.cfg b/data/gui/_main.cfg new file mode 100644 index 00000000000..522a61207dc --- /dev/null +++ b/data/gui/_main.cfg @@ -0,0 +1,5 @@ +# This is the main config file for the GUI definitions +# Currently it just includes the default GUI. +# However, perhaps in the future it may include additional GUI theme definitions. + +{./default.cfg} diff --git a/data/gui/default.cfg b/data/gui/default.cfg index 97377dfcad8..a8d46900ce3 100644 --- a/data/gui/default.cfg +++ b/data/gui/default.cfg @@ -20,7 +20,7 @@ sound_toggle_panel_click = "select.wav" sound_slider_adjust = "slider.wav" - has_helptip_message = " (Press '$hotkey' for more information)" + has_helptip_message = _" (Press '$hotkey' for more information)" [/settings] {tips.cfg} diff --git a/src/gui/widgets/settings.cpp b/src/gui/widgets/settings.cpp index 8e55671e83f..64c101bf725 100644 --- a/src/gui/widgets/settings.cpp +++ b/src/gui/widgets/settings.cpp @@ -33,6 +33,8 @@ #include "serialization/schema_validator.hpp" #include "formula/string_utils.hpp" #include "wml_exception.hpp" +#include "gui/core/log.hpp" +#include "preferences.hpp" namespace gui2 { @@ -435,9 +437,10 @@ void tgui_definition::load_widget_definitions( for(const auto & def : definitions) { - // We assume all definitions are unique if not we would leak memory. - assert(control_definition[definition_type].find(def->id) - == control_definition[definition_type].end()); + if(control_definition[definition_type].find(def->id) != control_definition[definition_type].end()) { + ERR_GUI_P << "Skipping duplicate control definition '" << def->id << "' for '" << definition_type << "'\n"; + continue; + } control_definition[definition_type] .insert(std::make_pair(def->id, def)); @@ -464,6 +467,9 @@ static std::map guis; /** Points to the current gui. */ static std::map::const_iterator current_gui = guis.end(); +/** Points to the default gui. */ +static std::map::const_iterator default_gui = guis.end(); + void register_window(const std::string& id) { const std::vector::iterator itor @@ -497,14 +503,14 @@ void load_settings() preproc_map preproc( game_config::config_cache::instance().get_preproc_map()); filesystem::scoped_istream stream = preprocess_file( - filesystem::get_wml_location("gui/default.cfg"), &preproc); + filesystem::get_wml_location("gui/_main.cfg"), &preproc); read(cfg, *stream, &validator); } catch(config::error& e) { ERR_GUI_P << e.what() << '\n'; - ERR_GUI_P << "Setting: could not read file 'data/gui/default.cfg'." + ERR_GUI_P << "Setting: could not read file 'data/gui/_main.cfg'." << std::endl; } catch(const abstract_validator::error& e) @@ -521,9 +527,15 @@ void load_settings() guis.insert(child); } - VALIDATE(guis.find("default") != guis.end(), _("No default gui defined.")); + default_gui = guis.find("default"); + VALIDATE(default_gui != guis.end(), _("No default gui defined.")); - current_gui = guis.find("default"); + std::string current_theme = preferences::gui_theme(); + current_gui = current_theme.empty() ? default_gui : guis.find(current_theme); + if(current_gui == guis.end()) { + ERR_GUI_P << "Missing [gui] definition for '" << current_theme << "'\n"; + current_gui = default_gui; + } current_gui->second.activate(); } @@ -628,12 +640,13 @@ get_window_builder(const std::string& type) std::map::const_iterator window = current_gui->second.window_types.find(type); - if(true) { // FIXME Test for default gui. - if(window == current_gui->second.window_types.end()) { + if(window == current_gui->second.window_types.end()) { + if(current_gui != default_gui) { + window = default_gui->second.window_types.find(type); + } + if(window == current_gui->second.window_types.end() || window == default_gui->second.window_types.end()) { throw twindow_builder_invalid_id(); } - } else { - // FIXME Get the definition in the default gui and do an assertion test. } for(std::vector::const_iterator itor diff --git a/src/preferences.cpp b/src/preferences.cpp index 82026271cf7..6628b814251 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -468,6 +468,16 @@ void set_language(const std::string& s) preferences::set("locale", s); } +std::string gui_theme() +{ + return prefs["gui2_theme"]; +} + +void set_gui_theme(const std::string& s) +{ + preferences::set("gui2_theme", s); +} + bool ellipses() { return get("show_side_colors", false); diff --git a/src/preferences.hpp b/src/preferences.hpp index 57f18d82acc..e4c3a5440bb 100644 --- a/src/preferences.hpp +++ b/src/preferences.hpp @@ -101,6 +101,9 @@ namespace preferences { std::string language(); void set_language(const std::string& s); + std::string gui_theme(); + void set_gui_theme(const std::string& s); + // Don't rename it to sound() because of a gcc-3.3 branch bug, // which will cause it to conflict with the sound namespace. bool sound_on();