From 86e37ab2302a7aea73601d3e61f366ae25e9d605 Mon Sep 17 00:00:00 2001 From: emb <> Date: Fri, 27 Feb 2015 20:03:27 -0600 Subject: [PATCH] Multi-color font support. --- .../Graphics/Component/ReloadableMedia.cpp | 5 +- .../Graphics/Component/ReloadableMedia.h | 3 +- .../Graphics/Component/ReloadableText.cpp | 5 +- .../Graphics/Component/ReloadableText.h | 3 +- .../Graphics/Component/ScrollingList.cpp | 5 +- .../Source/Graphics/Component/ScrollingList.h | 2 - RetroFE/Source/Graphics/Component/Text.cpp | 3 +- RetroFE/Source/Graphics/Component/Text.h | 3 +- RetroFE/Source/Graphics/PageBuilder.cpp | 90 +++++++++++++++---- RetroFE/Source/Graphics/PageBuilder.h | 4 +- 10 files changed, 87 insertions(+), 36 deletions(-) diff --git a/RetroFE/Source/Graphics/Component/ReloadableMedia.cpp b/RetroFE/Source/Graphics/Component/ReloadableMedia.cpp index c6e7af7..e209dfb 100644 --- a/RetroFE/Source/Graphics/Component/ReloadableMedia.cpp +++ b/RetroFE/Source/Graphics/Component/ReloadableMedia.cpp @@ -28,7 +28,7 @@ #include #include -ReloadableMedia::ReloadableMedia(Configuration &config, std::string type, bool isVideo, Font *font, SDL_Color fontColor, float scaleX, float scaleY) +ReloadableMedia::ReloadableMedia(Configuration &config, std::string type, bool isVideo, Font *font, float scaleX, float scaleY) : Config(config) , LoadedComponent(NULL) , ReloadRequested(false) @@ -36,7 +36,6 @@ ReloadableMedia::ReloadableMedia(Configuration &config, std::string type, bool i , VideoInst(NULL) , IsVideo(isVideo) , FontInst(font) - , FontColor(fontColor) , TextFallback(false) , Type(type) , ScaleX(scaleX) @@ -229,7 +228,7 @@ void ReloadableMedia::ReloadTexture() if(!LoadedComponent && TextFallback) { - LoadedComponent = new Text(imageBasename, FontInst, FontColor, ScaleX, ScaleY); + LoadedComponent = new Text(imageBasename, FontInst, ScaleX, ScaleY); GetBaseViewInfo()->SetImageWidth(LoadedComponent->GetBaseViewInfo()->GetImageWidth()); GetBaseViewInfo()->SetImageHeight(LoadedComponent->GetBaseViewInfo()->GetImageHeight()); } diff --git a/RetroFE/Source/Graphics/Component/ReloadableMedia.h b/RetroFE/Source/Graphics/Component/ReloadableMedia.h index d7bdb40..15e6273 100644 --- a/RetroFE/Source/Graphics/Component/ReloadableMedia.h +++ b/RetroFE/Source/Graphics/Component/ReloadableMedia.h @@ -27,7 +27,7 @@ class Image; class ReloadableMedia : public Component { public: - ReloadableMedia(Configuration &config, std::string type, bool isVideo, Font *font, SDL_Color fontColor, float scaleX, float scaleY); + ReloadableMedia(Configuration &config, std::string type, bool isVideo, Font *font, float scaleX, float scaleY); virtual ~ReloadableMedia(); void Update(float dt); void Draw(); @@ -46,7 +46,6 @@ private: IVideo *VideoInst; bool IsVideo; Font *FontInst; - SDL_Color FontColor; bool TextFallback; std::string Type; float ScaleX; diff --git a/RetroFE/Source/Graphics/Component/ReloadableText.cpp b/RetroFE/Source/Graphics/Component/ReloadableText.cpp index 91c368e..2fdce16 100644 --- a/RetroFE/Source/Graphics/Component/ReloadableText.cpp +++ b/RetroFE/Source/Graphics/Component/ReloadableText.cpp @@ -23,13 +23,12 @@ #include #include -ReloadableText::ReloadableText(std::string type, Font *font, SDL_Color color, std::string layoutKey, float scaleX, float scaleY) +ReloadableText::ReloadableText(std::string type, Font *font, std::string layoutKey, float scaleX, float scaleY) : ImageInst(NULL) , LayoutKey(layoutKey) , ReloadRequested(false) , FirstLoad(true) , FontInst(font) - , FontColor(color) , ScaleX(scaleX) , ScaleY(scaleY) { @@ -152,7 +151,7 @@ void ReloadableText::ReloadTexture() break; } - ImageInst = new Text(ss.str(), FontInst, FontColor, ScaleX, ScaleY); + ImageInst = new Text(ss.str(), FontInst, ScaleX, ScaleY); } } diff --git a/RetroFE/Source/Graphics/Component/ReloadableText.h b/RetroFE/Source/Graphics/Component/ReloadableText.h index 934fe52..7972784 100644 --- a/RetroFE/Source/Graphics/Component/ReloadableText.h +++ b/RetroFE/Source/Graphics/Component/ReloadableText.h @@ -24,7 +24,7 @@ class ReloadableText : public Component { public: - ReloadableText(std::string type, Font *font, SDL_Color color, std::string layoutKey, float scaleX, float scaleY); + ReloadableText(std::string type, Font *font, std::string layoutKey, float scaleX, float scaleY); virtual ~ReloadableText(); void Update(float dt); void Draw(); @@ -52,7 +52,6 @@ private: bool ReloadRequested; bool FirstLoad; Font *FontInst; - SDL_Color FontColor; float ScaleX; float ScaleY; diff --git a/RetroFE/Source/Graphics/Component/ScrollingList.cpp b/RetroFE/Source/Graphics/Component/ScrollingList.cpp index 100dd95..6990d30 100644 --- a/RetroFE/Source/Graphics/Component/ScrollingList.cpp +++ b/RetroFE/Source/Graphics/Component/ScrollingList.cpp @@ -42,7 +42,6 @@ ScrollingList::ScrollingList(Configuration &c, float scaleX, float scaleY, Font *font, - SDL_Color fontColor, std::string layoutKey, std::string imageType) : SpriteList(NULL) @@ -64,7 +63,6 @@ ScrollingList::ScrollingList(Configuration &c, , ScaleX(scaleX) , ScaleY(scaleY) , FontInst(font) - , FontColor(fontColor) , LayoutKey(layoutKey) , ImageType(imageType) { @@ -89,7 +87,6 @@ ScrollingList::ScrollingList(const ScrollingList ©) , ScaleX(copy.ScaleX) , ScaleY(copy.ScaleY) , FontInst(copy.FontInst) - , FontColor(copy.FontColor) , LayoutKey(copy.LayoutKey) , ImageType(copy.ImageType) { @@ -701,7 +698,7 @@ bool ScrollingList::AllocateTexture(ComponentItemBinding *s) } if (!t) { - t = new Text(item->GetTitle(), FontInst, FontColor, ScaleX, ScaleY); + t = new Text(item->GetTitle(), FontInst, ScaleX, ScaleY); } if(t) diff --git a/RetroFE/Source/Graphics/Component/ScrollingList.h b/RetroFE/Source/Graphics/Component/ScrollingList.h index a35dc9d..41d41a7 100644 --- a/RetroFE/Source/Graphics/Component/ScrollingList.h +++ b/RetroFE/Source/Graphics/Component/ScrollingList.h @@ -47,7 +47,6 @@ public: float scaleX, float scaleY, Font *font, - SDL_Color fontColor, std::string layoutKey, std::string imageType); @@ -126,7 +125,6 @@ private: float ScaleX; float ScaleY; Font *FontInst; - SDL_Color FontColor; std::string LayoutKey; std::string ImageType; }; diff --git a/RetroFE/Source/Graphics/Component/Text.cpp b/RetroFE/Source/Graphics/Component/Text.cpp index ed0a311..4012621 100644 --- a/RetroFE/Source/Graphics/Component/Text.cpp +++ b/RetroFE/Source/Graphics/Component/Text.cpp @@ -20,10 +20,9 @@ #include "../Font.h" #include -Text::Text(std::string text, Font *font, SDL_Color fontColor, float scaleX, float scaleY) +Text::Text(std::string text, Font *font, float scaleX, float scaleY) : TextData(text) , FontInst(font) - , FontColor(fontColor) , ScaleX(scaleX) , ScaleY(scaleY) { diff --git a/RetroFE/Source/Graphics/Component/Text.h b/RetroFE/Source/Graphics/Component/Text.h index 5fbcbba..d33f3a0 100644 --- a/RetroFE/Source/Graphics/Component/Text.h +++ b/RetroFE/Source/Graphics/Component/Text.h @@ -26,7 +26,7 @@ class Text : public Component { public: //todo: should have a Font flass that references fontcache, pass that in as an argument - Text(std::string text, Font *font, SDL_Color fontColor, float scaleX, float scaleY); + Text(std::string text, Font *font, float scaleX, float scaleY); void SetText(std::string text); virtual ~Text(); void AllocateGraphicsMemory(); @@ -36,7 +36,6 @@ public: private: std::string TextData; Font *FontInst; - SDL_Color FontColor; float ScaleX; float ScaleY; }; diff --git a/RetroFE/Source/Graphics/PageBuilder.cpp b/RetroFE/Source/Graphics/PageBuilder.cpp index 5033ae5..9a3aecf 100644 --- a/RetroFE/Source/Graphics/PageBuilder.cpp +++ b/RetroFE/Source/Graphics/PageBuilder.cpp @@ -123,14 +123,11 @@ Page *PageBuilder::BuildPage() } if(fontXml) { - std::string fontPropertyKey = "layouts." + LayoutKey + ".font"; - Config.SetProperty(fontPropertyKey, fontXml->value()); - - Font = Config.ConvertToAbsolutePath( + FontName = Config.ConvertToAbsolutePath( Config.GetAbsolutePath() + "/layouts/" + LayoutKey + "/", fontXml->value()); - Logger::Write(Logger::ZONE_DEBUG, "Layout", "Layout font set to " + Font); + Logger::Write(Logger::ZONE_DEBUG, "Layout", "Layout font set to " + FontName); } @@ -369,8 +366,8 @@ bool PageBuilder::BuildComponents(xml_node<> *layout, Page *page) } else { - FC->LoadFont(Font, FontSize, FontColor); - Text *c = new Text(value->value(), FC->GetFont(Font, FontSize, FontColor), FontColor, ScaleX, ScaleY); + Font *font = AddFont(componentXml, NULL); + Text *c = new Text(value->value(), font, ScaleX, ScaleY); ViewInfo *v = c->GetBaseViewInfo(); BuildViewInfo(componentXml, v); @@ -382,8 +379,8 @@ bool PageBuilder::BuildComponents(xml_node<> *layout, Page *page) for(xml_node<> *componentXml = layout->first_node("statusText"); componentXml; componentXml = componentXml->next_sibling("statusText")) { - FC->LoadFont(Font, FontSize, FontColor); - Text *c = new Text("", FC->GetFont(Font, FontSize, FontColor), FontColor, ScaleX, ScaleY); + Font *font = AddFont(componentXml, NULL); + Text *c = new Text("", font, ScaleX, ScaleY); ViewInfo *v = c->GetBaseViewInfo(); BuildViewInfo(componentXml, v); @@ -431,14 +428,14 @@ void PageBuilder::LoadReloadableImages(xml_node<> *layout, std::string tagName, { if(type) { - FC->LoadFont(Font, FontSize, FontColor); - c = new ReloadableText(type->value(), FC->GetFont(Font, FontSize, FontColor), FontColor, LayoutKey, ScaleX, ScaleY); + Font *font = AddFont(componentXml, NULL); + c = new ReloadableText(type->value(), font, LayoutKey, ScaleX, ScaleY); } } else { - FC->LoadFont(Font, FontSize, FontColor); - c = new ReloadableMedia(Config, type->value(), (tagName == "reloadableVideo"), FC->GetFont(Font, FontSize, FontColor), FontColor, ScaleX, ScaleY); + Font *font = AddFont(componentXml, NULL); + c = new ReloadableMedia(Config, type->value(), (tagName == "reloadableVideo"), font, ScaleX, ScaleY); xml_attribute<> *textFallback = componentXml->first_attribute("textFallback"); if(textFallback && Utils::ToLower(textFallback->value()) == "true") @@ -459,6 +456,69 @@ void PageBuilder::LoadReloadableImages(xml_node<> *layout, std::string tagName, } } } + +Font *PageBuilder::AddFont(xml_node<> *component, xml_node<> *defaults) +{ + xml_attribute<> *fontXml = component->first_attribute("font"); + xml_attribute<> *fontColorXml = component->first_attribute("fontColor"); + xml_attribute<> *fontSizeXml = component->first_attribute("loadFontSize"); + + if(defaults) + { + if(defaults->first_attribute("font")) + { + fontXml = defaults->first_attribute("font"); + } + + if(defaults->first_attribute("fontColor")) + { + fontColorXml = defaults->first_attribute("fontColor"); + } + + if(defaults->first_attribute("loadFontSize")) + { + fontSizeXml = defaults->first_attribute("loadFontSize"); + } + } + + + // use layout defaults unless overridden + std::string fontName = FontName; + SDL_Color fontColor = FontColor; + int fontSize = FontSize; + + if(fontXml) + { + fontName = Config.ConvertToAbsolutePath( + Config.GetAbsolutePath() + "/layouts/" + LayoutKey + "/", + fontXml->value()); + + Logger::Write(Logger::ZONE_DEBUG, "Layout", "loading font " + fontName ); + } + if(fontColorXml) + { + int intColor = 0; + std::stringstream ss; + ss << std::hex << fontColorXml->value(); + ss >> intColor; + + fontColor.b = intColor & 0xFF; + intColor >>= 8; + fontColor.g = intColor & 0xFF; + intColor >>= 8; + fontColor.r = intColor & 0xFF; + } + + if(fontSizeXml) + { + fontSize = Utils::ConvertInt(fontSizeXml->value()); + } + + FC->LoadFont(fontName, fontSize, fontColor); + + return FC->GetFont(fontName, fontSize, fontColor); +} + void PageBuilder::LoadTweens(Component *c, xml_node<> *componentXml) { ViewInfo *v = c->GetBaseViewInfo(); @@ -526,9 +586,9 @@ ScrollingList * PageBuilder::BuildMenu(xml_node<> *menuXml) } // on default, text will be rendered to the menu. Preload it into cache. - FC->LoadFont(Font, FontSize, FontColor); + Font *font = AddFont(itemDefaults, NULL); - menu = new ScrollingList(Config, ScaleX, ScaleY, FC->GetFont(Font, FontSize, FontColor), FontColor, LayoutKey, imageType); + menu = new ScrollingList(Config, ScaleX, ScaleY, font, LayoutKey, imageType); if(scrollTimeXml) { diff --git a/RetroFE/Source/Graphics/PageBuilder.h b/RetroFE/Source/Graphics/PageBuilder.h index b344491..95020b3 100644 --- a/RetroFE/Source/Graphics/PageBuilder.h +++ b/RetroFE/Source/Graphics/PageBuilder.h @@ -26,6 +26,7 @@ class ScrollingList; class Page; class ViewInfo; class Configuration; +class Font; class PageBuilder { @@ -44,10 +45,11 @@ private: int ScreenHeight; int ScreenWidth; SDL_Color FontColor; - std::string Font; + std::string FontName; int FontSize; FontCache *FC; + Font *AddFont(rapidxml::xml_node<> *component, rapidxml::xml_node<> *defaults); void LoadReloadableImages(rapidxml::xml_node<> *layout, std::string tagName, Page *page); float GetVerticalAlignment(rapidxml::xml_attribute<> *attribute, float valueIfNull); float GetHorizontalAlignment(rapidxml::xml_attribute<> *attribute, float valueIfNull);