RetroFE/Source/Graphics/Component/ReloadableText.cpp
2015-01-01 10:14:26 -06:00

158 lines
3.2 KiB
C++

/* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
*/
#include "ReloadableText.h"
#include "../ViewInfo.h"
#include "../../Database/Configuration.h"
#include "../../Utility/Log.h"
#include "../../SDL.h"
#include <fstream>
#include <vector>
#include <iostream>
ReloadableText::ReloadableText(std::string type, Font *font, SDL_Color color, std::string layoutKey, std::string collection, float scaleX, float scaleY)
: ImageInst(NULL)
, LayoutKey(layoutKey)
, Collection(collection)
, ReloadRequested(false)
, FirstLoad(true)
, FontInst(font)
, FontColor(color)
, ScaleX(scaleX)
, ScaleY(scaleY)
{
Type = TextTypeUnknown;
if(type == "numberButtons")
{
Type = TextTypeNumberButtons;
}
else if(type == "numberPlayers")
{
Type = TextTypeNumberPlayers;
}
else if(type == "year")
{
Type = TextTypeYear;
}
else if(type == "title")
{
Type = TextTypeTitle;
}
else if(type == "manufacturer")
{
Type = TextTypeManufacturer;
}
AllocateGraphicsMemory();
}
ReloadableText::~ReloadableText()
{
if (ImageInst != NULL)
{
delete ImageInst;
}
}
void ReloadableText::Update(float dt)
{
if(NewItemSelected)
{
ReloadRequested = true;
}
// wait for the right moment to reload the image
if (ReloadRequested && (HighlightExitComplete || FirstLoad))
{
ReloadTexture();
ReloadRequested = false;
FirstLoad = false;
}
// needs to be ran at the end to prevent the NewItemSelected flag from being detected
Component::Update(dt);
}
void ReloadableText::AllocateGraphicsMemory()
{
FirstLoad = true;
ReloadTexture();
// NOTICE! needs to be done last to prevent flags from being missed
Component::AllocateGraphicsMemory();
}
void ReloadableText::LaunchEnter()
{
}
void ReloadableText::LaunchExit()
{
}
void ReloadableText::FreeGraphicsMemory()
{
Component::FreeGraphicsMemory();
if (ImageInst != NULL)
{
delete ImageInst;
ImageInst = NULL;
}
}
void ReloadableText::ReloadTexture()
{
if (ImageInst != NULL)
{
delete ImageInst;
ImageInst = NULL;
}
Item *selectedItem = GetSelectedItem();
if (selectedItem != NULL)
{
std::stringstream ss;
std::string text;
switch(Type)
{
case TextTypeNumberButtons:
ss << selectedItem->GetNumberButtons();
break;
case TextTypeNumberPlayers:
ss << selectedItem->GetNumberPlayers();
break;
case TextTypeYear:
ss << selectedItem->GetYear();
break;
case TextTypeTitle:
ss << selectedItem->GetTitle();
break;
case TextTypeManufacturer:
ss << selectedItem->GetManufacturer();
break;
default:
break;
}
ImageInst = new Text(ss.str(), FontInst, FontColor, ScaleX, ScaleY);
}
}
void ReloadableText::Draw()
{
ViewInfo *info = GetBaseViewInfo();
if(ImageInst)
{
ImageInst->UpdateBaseViewInfo(*info);
ImageInst->Draw();
}
}