From 1afcc7adb56c8792577f326735ec1725ab748ad6 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 25 Sep 2014 00:04:38 +0200 Subject: [PATCH] Add imported font colors from openmw.cfg to MyGUI plugin --- apps/openmw/mwgui/windowmanagerimp.cpp | 42 ++--------------- components/CMakeLists.txt | 2 +- components/widgets/tags.cpp | 57 ++++++++++++++++++++++++ components/widgets/tags.hpp | 16 +++++++ plugins/mygui_resource_plugin/plugin.cpp | 54 +++++++++++++++++++++- plugins/mygui_resource_plugin/plugin.hpp | 5 +++ 6 files changed, 135 insertions(+), 41 deletions(-) create mode 100644 components/widgets/tags.cpp create mode 100644 components/widgets/tags.hpp diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index 3bf4e588a..198d293b3 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "../mwbase/inputmanager.hpp" #include "../mwbase/statemanager.hpp" @@ -991,50 +992,13 @@ namespace MWGui std::string tokenToFind = "sCell="; size_t tokenLength = tokenToFind.length(); - std::string fontcolour = "fontcolour="; - size_t fontcolourLength = fontcolour.length(); - - std::string fontcolourhtml = "fontcolourhtml="; - size_t fontcolourhtmlLength = fontcolourhtml.length(); - if (tag.compare(0, tokenLength, tokenToFind) == 0) { _result = mTranslationDataStorage.translateCellName(tag.substr(tokenLength)); } - else if (tag.compare(0, fontcolourLength, fontcolour) == 0) + else if (Gui::replaceTag(tag, _result, mFallbackMap)) { - std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourLength); - std::map::const_iterator it = mFallbackMap.find(fallbackName); - if (it == mFallbackMap.end()) - throw std::runtime_error("Unknown fallback name: " + fallbackName); - std::string str = it->second; - - std::string ret[3]; - unsigned int j=0; - for(unsigned int i=0;i::const_iterator it = mFallbackMap.find(fallbackName); - if (it == mFallbackMap.end()) - throw std::runtime_error("Unknown fallback name: " + fallbackName); - std::string str = it->second; - - std::string ret[3]; - unsigned int j=0; - for(unsigned int i=0;i + +namespace Gui +{ + +bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out, const std::map& fallbackSettings) +{ + std::string fontcolour = "fontcolour="; + size_t fontcolourLength = fontcolour.length(); + + std::string fontcolourhtml = "fontcolourhtml="; + size_t fontcolourhtmlLength = fontcolourhtml.length(); + + if (tag.compare(0, fontcolourLength, fontcolour) == 0) + { + std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourLength); + std::map::const_iterator it = fallbackSettings.find(fallbackName); + if (it == fallbackSettings.end()) + throw std::runtime_error("Unknown fallback name: " + fallbackName); + std::string str = it->second; + + std::string ret[3]; + unsigned int j=0; + for(unsigned int i=0;i::const_iterator it = fallbackSettings.find(fallbackName); + if (it == fallbackSettings.end()) + throw std::runtime_error("Unknown fallback name: " + fallbackName); + std::string str = it->second; + + std::string ret[3]; + unsigned int j=0; + for(unsigned int i=0;i +#include +#include + +namespace Gui +{ + +/// Try to replace a tag. Returns true on success and writes the result to \a out. +bool replaceTag (const MyGUI::UString& tag, MyGUI::UString& out, const std::map& fallbackSettings); + +} + +#endif diff --git a/plugins/mygui_resource_plugin/plugin.cpp b/plugins/mygui_resource_plugin/plugin.cpp index 9633bc51b..7b5572e62 100644 --- a/plugins/mygui_resource_plugin/plugin.cpp +++ b/plugins/mygui_resource_plugin/plugin.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -12,10 +13,49 @@ #include #include +#include #include #include +//FIXME: code duplication +namespace boost +{ +struct FallbackMap { + std::map mMap; +}; + +void validate(boost::any &v, std::vector const &tokens, FallbackMap*, int) +{ + if(v.empty()) + { + v = boost::any(FallbackMap()); + } + + FallbackMap *map = boost::any_cast(&v); + + std::map::iterator mapIt; + for(std::vector::const_iterator it=tokens.begin(); it != tokens.end(); ++it) + { + int sep = it->find(","); + if(sep < 1 || sep == (int)it->length()-1) +#if (BOOST_VERSION < 104200) + throw boost::program_options::validation_error("invalid value"); +#else + throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value); +#endif + + std::string key(it->substr(0,sep)); + std::string value(it->substr(sep+1)); + + if((mapIt = map->mMap.find(key)) == map->mMap.end()) + { + map->mMap.insert(std::make_pair (key,value)); + } + } +} +} + namespace MyGUIPlugin { @@ -51,7 +91,9 @@ namespace MyGUIPlugin ("fs-strict", boost::program_options::value()->implicit_value(true)->default_value(false)) ("fallback-archive", boost::program_options::value >()-> default_value(std::vector(), "fallback-archive")->multitoken()) - ("encoding", boost::program_options::value()->default_value("win1252")); + ("encoding", boost::program_options::value()->default_value("win1252")) + ("fallback", boost::program_options::value()->default_value(boost::FallbackMap(), "") + ->multitoken()->composing(), "fallback values"); boost::program_options::notify(variables); @@ -86,6 +128,8 @@ namespace MyGUIPlugin Gui::FontLoader loader(ToUTF8::calculateEncoding(encoding)); loader.loadAllFonts(false); + + mFallbackMap = variables["fallback"].as().mMap; } void ResourcePlugin::registerWidgets() @@ -126,6 +170,8 @@ namespace MyGUIPlugin registerResources(); registerWidgets(); createTransparentBGTexture(); + + MyGUI::LanguageManager::getInstance().eventRequestTag = MyGUI::newDelegate(this, &ResourcePlugin::onRetrieveTag); } void ResourcePlugin::shutdown() @@ -135,4 +181,10 @@ namespace MyGUIPlugin MYGUI_LOGGING("OpenMW_Resource_Plugin", Info, "shutdown"); } + void ResourcePlugin::onRetrieveTag(const MyGUI::UString& tag, MyGUI::UString& out) + { + if (!Gui::replaceTag(tag, out, mFallbackMap)) + out = tag; + } + } diff --git a/plugins/mygui_resource_plugin/plugin.hpp b/plugins/mygui_resource_plugin/plugin.hpp index 9d035f09c..6a06060d9 100644 --- a/plugins/mygui_resource_plugin/plugin.hpp +++ b/plugins/mygui_resource_plugin/plugin.hpp @@ -2,6 +2,7 @@ #define OPENMW_MYGUI_RESOURCE_PLUGIN_H #include +#include namespace MyGUIPlugin { @@ -40,6 +41,10 @@ namespace MyGUIPlugin void registerResources(); void registerWidgets(); void createTransparentBGTexture(); + + void onRetrieveTag(const MyGUI::UString& tag, MyGUI::UString& out); + + std::map mFallbackMap; }; }