Skip to content

Commit

Permalink
Add imported font colors from openmw.cfg to MyGUI plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
scrawl committed Sep 24, 2014
1 parent 62ab358 commit 1afcc7a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 41 deletions.
42 changes: 3 additions & 39 deletions apps/openmw/mwgui/windowmanagerimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ add_component_dir (ogreinit
)

add_component_dir (widgets
box imagebutton
box imagebutton tags
)

add_component_dir (fontloader
Expand Down
57 changes: 57 additions & 0 deletions components/widgets/tags.cpp
Original file line number Diff line number Diff line change
@@ -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;

}

}
16 changes: 16 additions & 0 deletions components/widgets/tags.hpp
Original file line number Diff line number Diff line change
@@ -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
54 changes: 53 additions & 1 deletion plugins/mygui_resource_plugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,57 @@
#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>
#include <components/fontloader/fontloader.hpp>

#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
{

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -126,6 +170,8 @@ namespace MyGUIPlugin
registerResources();
registerWidgets();
createTransparentBGTexture();

MyGUI::LanguageManager::getInstance().eventRequestTag = MyGUI::newDelegate(this, &ResourcePlugin::onRetrieveTag);
}

void ResourcePlugin::shutdown()
Expand All @@ -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;
}

}
5 changes: 5 additions & 0 deletions plugins/mygui_resource_plugin/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define OPENMW_MYGUI_RESOURCE_PLUGIN_H

#include <MyGUI_Plugin.h>
#include <MyGUI_UString.h>

namespace MyGUIPlugin
{
Expand Down Expand Up @@ -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;
};

}
Expand Down

0 comments on commit 1afcc7a

Please sign in to comment.