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 <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)
, 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());
}

View File

@ -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;

View File

@ -23,13 +23,12 @@
#include <vector>
#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)
, 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);
}
}

View File

@ -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;

View File

@ -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 &copy)
, 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)

View File

@ -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;
};

View File

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

View File

@ -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;
};

View File

@ -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)
{

View File

@ -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);