RetroFE/RetroFE/Source/Graphics/Component/ReloadableText.cpp
Pieter Hulshoff da0fe6c299 Replaced numberPlayersRange, numberPlayersPlayers, and numberPlayersRangePlayers types with more general singlePrefix, single Postfix, pluralPrefix, and pluralPostfix attributes for reloadableText. This prints as follows:
printed text = 0: singlePrefix text pluralPostfix
printed text = 1: singlePrefix text singlePostfix
otherwise       : pluralPrefix text pluralPostfix
This allows support for themes in different languages or simply textual printing freedom.
2016-06-25 10:36:33 +02:00

245 lines
6.9 KiB
C++

/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ReloadableText.h"
#include "../ViewInfo.h"
#include "../../Database/Configuration.h"
#include "../../Utility/Log.h"
#include "../../SDL.h"
#include <fstream>
#include <vector>
#include <iostream>
#include <time.h>
#include <algorithm>
ReloadableText::ReloadableText(std::string type, Page &page, Configuration &config, Font *font, std::string layoutKey, std::string timeFormat, std::string textFormat, std::string singlePrefix, std::string singlePostfix, std::string pluralPrefix, std::string pluralPostfix, float scaleX, float scaleY)
: Component(page)
, config_(config)
, imageInst_(NULL)
, type_(type)
, layoutKey_(layoutKey)
, fontInst_(font)
, timeFormat_(timeFormat)
, textFormat_(textFormat)
, singlePrefix_(singlePrefix)
, singlePostfix_(singlePostfix)
, pluralPrefix_(pluralPrefix)
, pluralPostfix_(pluralPostfix)
, scaleX_(scaleX)
, scaleY_(scaleY)
{
allocateGraphicsMemory();
}
ReloadableText::~ReloadableText()
{
if (imageInst_ != NULL)
{
delete imageInst_;
}
}
void ReloadableText::update(float dt)
{
if (newItemSelected || type_ == "time")
{
ReloadTexture();
newItemSelected = false;
}
// needs to be ran at the end to prevent the NewItemSelected flag from being detected
Component::update(dt);
}
void ReloadableText::allocateGraphicsMemory()
{
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 = page.getSelectedItem();
if (selectedItem != NULL)
{
std::stringstream ss;
std::string text = "";
if (type_ == "time")
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), timeFormat_.c_str(), &tstruct);
ss << buf;
}
if (type_ == "numberButtons")
{
text = selectedItem->numberButtons;
}
else if (type_ == "numberPlayers")
{
text = selectedItem->numberPlayers;
}
else if (type_ == "ctrlType")
{
text = selectedItem->ctrlType;
}
else if (type_ == "numberJoyWays")
{
text = selectedItem->joyWays;
}
else if (type_ == "rating")
{
text = selectedItem->rating;
}
else if (type_ == "year")
{
if (selectedItem->leaf) // item is a leaf
text = selectedItem->year;
else // item is a collection
(void)config_.getProperty("collections." + selectedItem->name + ".year", text );
}
else if (type_ == "title")
{
text = selectedItem->title;
}
else if (type_ == "manufacturer")
{
if (selectedItem->leaf) // item is a leaf
text = selectedItem->manufacturer;
else // item is a collection
(void)config_.getProperty("collections." + selectedItem->name + ".manufacturer", text );
}
else if (type_ == "genre")
{
if (selectedItem->leaf) // item is a leaf
text = selectedItem->genre;
else // item is a collection
(void)config_.getProperty("collections." + selectedItem->name + ".genre", text );
}
else if (type_ == "playlist")
{
text = playlistName;
}
else if (type_ == "collectionName")
{
text = page.getCollectionName();
}
else if (type_ == "collectionSize")
{
if (page.getCollectionSize() == 0)
{
ss << singlePrefix_ << page.getCollectionSize() << pluralPostfix_;
}
else if (page.getCollectionSize() == 1)
{
ss << singlePrefix_ << page.getCollectionSize() << singlePostfix_;
}
else
{
ss << pluralPrefix_ << page.getCollectionSize() << pluralPostfix_;
}
}
else if (type_ == "collectionIndex")
{
if (page.getSelectedIndex() == 0)
{
ss << singlePrefix_ << page.getSelectedIndex() << pluralPostfix_;
}
else if (page.getSelectedIndex() == 1)
{
ss << singlePrefix_ << page.getSelectedIndex() << singlePostfix_;
}
else
{
ss << pluralPrefix_ << page.getSelectedIndex() << pluralPostfix_;
}
} else if (!selectedItem->leaf) // item is not a leaf
{
(void)config_.getProperty("collections." + selectedItem->name + "." + type_, text );
}
if (text == "0")
{
text = singlePrefix_ + text + pluralPostfix_;
}
else if (text == "1")
{
text = singlePrefix_ + text + singlePostfix_;
}
else if (text != "")
{
text = pluralPrefix_ + text + pluralPostfix_;
}
if (text != "")
{
if (textFormat_ == "uppercase")
{
std::transform(text.begin(), text.end(), text.begin(), ::toupper);
}
if (textFormat_ == "lowercase")
{
std::transform(text.begin(), text.end(), text.begin(), ::tolower);
}
ss << text;
}
imageInst_ = new Text(ss.str(), page, fontInst_, scaleX_, scaleY_);
}
}
void ReloadableText::draw()
{
if(imageInst_)
{
imageInst_->baseViewInfo = baseViewInfo;
imageInst_->draw();
}
}