Add imported font colors from openmw.cfg to MyGUI plugin

deque
scrawl 10 years ago
parent 62ab35881e
commit 1afcc7adb5

@ -19,6 +19,7 @@
#include <components/fontloader/fontloader.hpp>
#include <components/widgets/box.hpp>
#include <components/widgets/tags.hpp>
#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<std::string, std::string>::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<str.length();++i){
if(str[i]==',') j++;
else if (str[i] != ' ') ret[j]+=str[i];
}
MyGUI::Colour col (MyGUI::utility::parseInt(ret[0])/255.f,MyGUI::utility::parseInt(ret[1])/255.f,MyGUI::utility::parseInt(ret[2])/255.f);
_result = col.print();
}
else if (tag.compare(0, fontcolourhtmlLength, fontcolourhtml) == 0)
{
std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourhtmlLength);
std::map<std::string, std::string>::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<str.length();++i){
if(str[i]==',') j++;
else if (str[i] != ' ') ret[j]+=str[i];
}
std::stringstream html;
html << "#" << std::hex << MyGUI::utility::parseInt(ret[0]) << MyGUI::utility::parseInt(ret[1]) << MyGUI::utility::parseInt(ret[2]);
_result = html.str();
return;
}
else
{

@ -96,7 +96,7 @@ add_component_dir (ogreinit
)
add_component_dir (widgets
box imagebutton
box imagebutton tags
)
add_component_dir (fontloader

@ -0,0 +1,57 @@
#include "tags.hpp"
#include <MyGUI_Colour.h>
namespace Gui
{
bool replaceTag(const MyGUI::UString& tag, MyGUI::UString& out, const std::map<std::string,std::string>& 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<std::string, std::string>::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<str.length();++i){
if(str[i]==',') j++;
else if (str[i] != ' ') ret[j]+=str[i];
}
MyGUI::Colour col (MyGUI::utility::parseInt(ret[0])/255.f,MyGUI::utility::parseInt(ret[1])/255.f,MyGUI::utility::parseInt(ret[2])/255.f);
out = col.print();
return true;
}
else if (tag.compare(0, fontcolourhtmlLength, fontcolourhtml) == 0)
{
std::string fallbackName = "FontColor_color_" + tag.substr(fontcolourhtmlLength);
std::map<std::string, std::string>::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<str.length();++i){
if(str[i]==',') j++;
else if (str[i] != ' ') ret[j]+=str[i];
}
std::stringstream html;
html << "#" << std::hex << MyGUI::utility::parseInt(ret[0]) << MyGUI::utility::parseInt(ret[1]) << MyGUI::utility::parseInt(ret[2]);
out = html.str();
return true;
}
return false;
}
}

@ -0,0 +1,16 @@
#ifndef OPENMW_WIDGETS_TAGS_H
#define OPENMW_WIDGETS_TAGS_H
#include <MyGUI_UString.h>
#include <string>
#include <map>
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<std::string,std::string>& fallbackSettings);
}
#endif

@ -5,6 +5,7 @@
#include <MyGUI_ScrollBar.h>
#include <MyGUI_Gui.h>
#include <MyGUI_Window.h>
#include <MyGUI_LanguageManager.h>
#include <components/bsa/resources.hpp>
#include <components/files/configurationmanager.hpp>
@ -12,10 +13,49 @@
#include <components/widgets/imagebutton.hpp>
#include <components/widgets/box.hpp>
#include <components/widgets/tags.hpp>
#include <OgreTextureManager.h>
#include <OgreHardwarePixelBuffer.h>
//FIXME: code duplication
namespace boost
{
struct FallbackMap {
std::map<std::string,std::string> mMap;
};
void validate(boost::any &v, std::vector<std::string> const &tokens, FallbackMap*, int)
{
if(v.empty())
{
v = boost::any(FallbackMap());
}
FallbackMap *map = boost::any_cast<FallbackMap>(&v);
std::map<std::string,std::string>::iterator mapIt;
for(std::vector<std::string>::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<bool>()->implicit_value(true)->default_value(false))
("fallback-archive", boost::program_options::value<std::vector<std::string> >()->
default_value(std::vector<std::string>(), "fallback-archive")->multitoken())
("encoding", boost::program_options::value<std::string>()->default_value("win1252"));
("encoding", boost::program_options::value<std::string>()->default_value("win1252"))
("fallback", boost::program_options::value<boost::FallbackMap>()->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<boost::FallbackMap>().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;
}
}

@ -2,6 +2,7 @@
#define OPENMW_MYGUI_RESOURCE_PLUGIN_H
#include <MyGUI_Plugin.h>
#include <MyGUI_UString.h>
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<std::string, std::string> mFallbackMap;
};
}

Loading…
Cancel
Save