Multi-color font support.

This commit is contained in:
emb
2015-02-27 20:03:27 -06:00
parent 861b6cafd2
commit 86e37ab230
10 changed files with 87 additions and 36 deletions

View File

@@ -28,7 +28,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
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) : Config(config)
, LoadedComponent(NULL) , LoadedComponent(NULL)
, ReloadRequested(false) , ReloadRequested(false)
@@ -36,7 +36,6 @@ ReloadableMedia::ReloadableMedia(Configuration &config, std::string type, bool i
, VideoInst(NULL) , VideoInst(NULL)
, IsVideo(isVideo) , IsVideo(isVideo)
, FontInst(font) , FontInst(font)
, FontColor(fontColor)
, TextFallback(false) , TextFallback(false)
, Type(type) , Type(type)
, ScaleX(scaleX) , ScaleX(scaleX)
@@ -229,7 +228,7 @@ void ReloadableMedia::ReloadTexture()
if(!LoadedComponent && TextFallback) if(!LoadedComponent && TextFallback)
{ {
LoadedComponent = new Text(imageBasename, FontInst, FontColor, ScaleX, ScaleY); LoadedComponent = new Text(imageBasename, FontInst, ScaleX, ScaleY);
GetBaseViewInfo()->SetImageWidth(LoadedComponent->GetBaseViewInfo()->GetImageWidth()); GetBaseViewInfo()->SetImageWidth(LoadedComponent->GetBaseViewInfo()->GetImageWidth());
GetBaseViewInfo()->SetImageHeight(LoadedComponent->GetBaseViewInfo()->GetImageHeight()); GetBaseViewInfo()->SetImageHeight(LoadedComponent->GetBaseViewInfo()->GetImageHeight());
} }

View File

@@ -27,7 +27,7 @@ class Image;
class ReloadableMedia : public Component class ReloadableMedia : public Component
{ {
public: 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(); virtual ~ReloadableMedia();
void Update(float dt); void Update(float dt);
void Draw(); void Draw();
@@ -46,7 +46,6 @@ private:
IVideo *VideoInst; IVideo *VideoInst;
bool IsVideo; bool IsVideo;
Font *FontInst; Font *FontInst;
SDL_Color FontColor;
bool TextFallback; bool TextFallback;
std::string Type; std::string Type;
float ScaleX; float ScaleX;

View File

@@ -23,13 +23,12 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
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) : ImageInst(NULL)
, LayoutKey(layoutKey) , LayoutKey(layoutKey)
, ReloadRequested(false) , ReloadRequested(false)
, FirstLoad(true) , FirstLoad(true)
, FontInst(font) , FontInst(font)
, FontColor(color)
, ScaleX(scaleX) , ScaleX(scaleX)
, ScaleY(scaleY) , ScaleY(scaleY)
{ {
@@ -152,7 +151,7 @@ void ReloadableText::ReloadTexture()
break; break;
} }
ImageInst = new Text(ss.str(), FontInst, FontColor, ScaleX, ScaleY); ImageInst = new Text(ss.str(), FontInst, ScaleX, ScaleY);
} }
} }

View File

@@ -24,7 +24,7 @@
class ReloadableText : public Component class ReloadableText : public Component
{ {
public: 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(); virtual ~ReloadableText();
void Update(float dt); void Update(float dt);
void Draw(); void Draw();
@@ -52,7 +52,6 @@ private:
bool ReloadRequested; bool ReloadRequested;
bool FirstLoad; bool FirstLoad;
Font *FontInst; Font *FontInst;
SDL_Color FontColor;
float ScaleX; float ScaleX;
float ScaleY; float ScaleY;

View File

@@ -42,7 +42,6 @@ ScrollingList::ScrollingList(Configuration &c,
float scaleX, float scaleX,
float scaleY, float scaleY,
Font *font, Font *font,
SDL_Color fontColor,
std::string layoutKey, std::string layoutKey,
std::string imageType) std::string imageType)
: SpriteList(NULL) : SpriteList(NULL)
@@ -64,7 +63,6 @@ ScrollingList::ScrollingList(Configuration &c,
, ScaleX(scaleX) , ScaleX(scaleX)
, ScaleY(scaleY) , ScaleY(scaleY)
, FontInst(font) , FontInst(font)
, FontColor(fontColor)
, LayoutKey(layoutKey) , LayoutKey(layoutKey)
, ImageType(imageType) , ImageType(imageType)
{ {
@@ -89,7 +87,6 @@ ScrollingList::ScrollingList(const ScrollingList &copy)
, ScaleX(copy.ScaleX) , ScaleX(copy.ScaleX)
, ScaleY(copy.ScaleY) , ScaleY(copy.ScaleY)
, FontInst(copy.FontInst) , FontInst(copy.FontInst)
, FontColor(copy.FontColor)
, LayoutKey(copy.LayoutKey) , LayoutKey(copy.LayoutKey)
, ImageType(copy.ImageType) , ImageType(copy.ImageType)
{ {
@@ -701,7 +698,7 @@ bool ScrollingList::AllocateTexture(ComponentItemBinding *s)
} }
if (!t) if (!t)
{ {
t = new Text(item->GetTitle(), FontInst, FontColor, ScaleX, ScaleY); t = new Text(item->GetTitle(), FontInst, ScaleX, ScaleY);
} }
if(t) if(t)

View File

@@ -47,7 +47,6 @@ public:
float scaleX, float scaleX,
float scaleY, float scaleY,
Font *font, Font *font,
SDL_Color fontColor,
std::string layoutKey, std::string layoutKey,
std::string imageType); std::string imageType);
@@ -126,7 +125,6 @@ private:
float ScaleX; float ScaleX;
float ScaleY; float ScaleY;
Font *FontInst; Font *FontInst;
SDL_Color FontColor;
std::string LayoutKey; std::string LayoutKey;
std::string ImageType; std::string ImageType;
}; };

View File

@@ -20,10 +20,9 @@
#include "../Font.h" #include "../Font.h"
#include <sstream> #include <sstream>
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) : TextData(text)
, FontInst(font) , FontInst(font)
, FontColor(fontColor)
, ScaleX(scaleX) , ScaleX(scaleX)
, ScaleY(scaleY) , ScaleY(scaleY)
{ {

View File

@@ -26,7 +26,7 @@ class Text : public Component
{ {
public: public:
//todo: should have a Font flass that references fontcache, pass that in as an argument //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); void SetText(std::string text);
virtual ~Text(); virtual ~Text();
void AllocateGraphicsMemory(); void AllocateGraphicsMemory();
@@ -36,7 +36,6 @@ public:
private: private:
std::string TextData; std::string TextData;
Font *FontInst; Font *FontInst;
SDL_Color FontColor;
float ScaleX; float ScaleX;
float ScaleY; float ScaleY;
}; };

View File

@@ -123,14 +123,11 @@ Page *PageBuilder::BuildPage()
} }
if(fontXml) if(fontXml)
{ {
std::string fontPropertyKey = "layouts." + LayoutKey + ".font"; FontName = Config.ConvertToAbsolutePath(
Config.SetProperty(fontPropertyKey, fontXml->value());
Font = Config.ConvertToAbsolutePath(
Config.GetAbsolutePath() + "/layouts/" + LayoutKey + "/", Config.GetAbsolutePath() + "/layouts/" + LayoutKey + "/",
fontXml->value()); 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 else
{ {
FC->LoadFont(Font, FontSize, FontColor); Font *font = AddFont(componentXml, NULL);
Text *c = new Text(value->value(), FC->GetFont(Font, FontSize, FontColor), FontColor, ScaleX, ScaleY); Text *c = new Text(value->value(), font, ScaleX, ScaleY);
ViewInfo *v = c->GetBaseViewInfo(); ViewInfo *v = c->GetBaseViewInfo();
BuildViewInfo(componentXml, v); 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")) for(xml_node<> *componentXml = layout->first_node("statusText"); componentXml; componentXml = componentXml->next_sibling("statusText"))
{ {
FC->LoadFont(Font, FontSize, FontColor); Font *font = AddFont(componentXml, NULL);
Text *c = new Text("", FC->GetFont(Font, FontSize, FontColor), FontColor, ScaleX, ScaleY); Text *c = new Text("", font, ScaleX, ScaleY);
ViewInfo *v = c->GetBaseViewInfo(); ViewInfo *v = c->GetBaseViewInfo();
BuildViewInfo(componentXml, v); BuildViewInfo(componentXml, v);
@@ -431,14 +428,14 @@ void PageBuilder::LoadReloadableImages(xml_node<> *layout, std::string tagName,
{ {
if(type) if(type)
{ {
FC->LoadFont(Font, FontSize, FontColor); Font *font = AddFont(componentXml, NULL);
c = new ReloadableText(type->value(), FC->GetFont(Font, FontSize, FontColor), FontColor, LayoutKey, ScaleX, ScaleY); c = new ReloadableText(type->value(), font, LayoutKey, ScaleX, ScaleY);
} }
} }
else else
{ {
FC->LoadFont(Font, FontSize, FontColor); Font *font = AddFont(componentXml, NULL);
c = new ReloadableMedia(Config, type->value(), (tagName == "reloadableVideo"), FC->GetFont(Font, FontSize, FontColor), FontColor, ScaleX, ScaleY); c = new ReloadableMedia(Config, type->value(), (tagName == "reloadableVideo"), font, ScaleX, ScaleY);
xml_attribute<> *textFallback = componentXml->first_attribute("textFallback"); xml_attribute<> *textFallback = componentXml->first_attribute("textFallback");
if(textFallback && Utils::ToLower(textFallback->value()) == "true") 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) void PageBuilder::LoadTweens(Component *c, xml_node<> *componentXml)
{ {
ViewInfo *v = c->GetBaseViewInfo(); 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. // 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) if(scrollTimeXml)
{ {

View File

@@ -26,6 +26,7 @@ class ScrollingList;
class Page; class Page;
class ViewInfo; class ViewInfo;
class Configuration; class Configuration;
class Font;
class PageBuilder class PageBuilder
{ {
@@ -44,10 +45,11 @@ private:
int ScreenHeight; int ScreenHeight;
int ScreenWidth; int ScreenWidth;
SDL_Color FontColor; SDL_Color FontColor;
std::string Font; std::string FontName;
int FontSize; int FontSize;
FontCache *FC; FontCache *FC;
Font *AddFont(rapidxml::xml_node<> *component, rapidxml::xml_node<> *defaults);
void LoadReloadableImages(rapidxml::xml_node<> *layout, std::string tagName, Page *page); void LoadReloadableImages(rapidxml::xml_node<> *layout, std::string tagName, Page *page);
float GetVerticalAlignment(rapidxml::xml_attribute<> *attribute, float valueIfNull); float GetVerticalAlignment(rapidxml::xml_attribute<> *attribute, float valueIfNull);
float GetHorizontalAlignment(rapidxml::xml_attribute<> *attribute, float valueIfNull); float GetHorizontalAlignment(rapidxml::xml_attribute<> *attribute, float valueIfNull);