ES-DE/es-core/src/components/DateTimeComponent.cpp

130 lines
3.4 KiB
C++
Raw Normal View History

#include "components/DateTimeComponent.h"
2017-11-01 22:21:10 +00:00
#include "utils/StringUtil.h"
2018-10-13 01:08:15 +00:00
#include "Log.h"
#include "Settings.h"
2018-10-13 01:08:15 +00:00
DateTimeComponent::DateTimeComponent(Window* window) : TextComponent(window), mDisplayRelative(false)
{
2018-10-13 01:08:15 +00:00
setFormat("%m/%d/%Y");
}
2018-10-13 01:08:15 +00:00
DateTimeComponent::DateTimeComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color, Alignment align,
Vector3f pos, Vector2f size, unsigned int bgcolor) : TextComponent(window, text, font, color, align, pos, size, bgcolor), mDisplayRelative(false)
{
2018-10-13 01:08:15 +00:00
setFormat("%m/%d/%Y");
}
void DateTimeComponent::setValue(const std::string& val)
{
mTime = val;
2018-10-13 01:08:15 +00:00
onTextChanged();
}
std::string DateTimeComponent::getValue() const
{
return mTime;
}
2018-10-13 01:08:15 +00:00
void DateTimeComponent::setFormat(const std::string& format)
{
2018-10-13 01:08:15 +00:00
mFormat = format;
onTextChanged();
}
2018-10-13 01:08:15 +00:00
void DateTimeComponent::setDisplayRelative(bool displayRelative)
{
2018-10-13 01:08:15 +00:00
mDisplayRelative = displayRelative;
onTextChanged();
}
2018-10-13 01:08:15 +00:00
void DateTimeComponent::onTextChanged()
{
2018-10-13 01:08:15 +00:00
mText = getDisplayString();
2018-10-13 01:08:15 +00:00
TextComponent::onTextChanged();
}
2018-10-13 01:08:15 +00:00
std::string DateTimeComponent::getDisplayString() const
{
2018-10-13 01:08:15 +00:00
if (mDisplayRelative) {
//relative time
if(mTime.getTime() == 0)
return "never";
2018-10-13 01:08:15 +00:00
Utils::Time::DateTime now(Utils::Time::now());
Utils::Time::Duration dur(now.getTime() - mTime.getTime());
2018-10-13 01:08:15 +00:00
char buf[64];
2018-10-13 01:08:15 +00:00
if(dur.getDays() > 0)
sprintf(buf, "%d day%s ago", dur.getDays(), (dur.getDays() > 1) ? "s" : "");
else if(dur.getHours() > 0)
sprintf(buf, "%d hour%s ago", dur.getHours(), (dur.getHours() > 1) ? "s" : "");
else if(dur.getMinutes() > 0)
sprintf(buf, "%d minute%s ago", dur.getMinutes(), (dur.getMinutes() > 1) ? "s" : "");
else
sprintf(buf, "%d second%s ago", dur.getSeconds(), (dur.getSeconds() > 1) ? "s" : "");
2018-10-13 01:08:15 +00:00
return std::string(buf);
}
2018-10-13 01:08:15 +00:00
if(mTime.getTime() == 0)
return "unknown";
2018-10-13 01:08:15 +00:00
return Utils::Time::timeToString(mTime.getTime(), mFormat);
}
2018-10-13 01:08:15 +00:00
void DateTimeComponent::render(const Transform4x4f& parentTrans)
{
2018-10-13 01:08:15 +00:00
TextComponent::render(parentTrans);
}
2018-10-13 01:08:15 +00:00
void DateTimeComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
{
2018-10-13 01:08:15 +00:00
GuiComponent::applyTheme(theme, view, element, properties);
2018-10-13 01:08:15 +00:00
using namespace ThemeFlags;
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "datetime");
if(!elem)
return;
2018-10-13 01:08:15 +00:00
if(elem->has("displayRelative"))
setDisplayRelative(elem->get<bool>("displayRelative"));
2018-10-13 01:08:15 +00:00
if(elem->has("format"))
setFormat(elem->get<std::string>("format"));
2018-10-13 01:08:15 +00:00
if (properties & COLOR && elem->has("color"))
setColor(elem->get<unsigned int>("color"));
2018-10-13 01:08:15 +00:00
setRenderBackground(false);
if (properties & COLOR && elem->has("backgroundColor")) {
setBackgroundColor(elem->get<unsigned int>("backgroundColor"));
setRenderBackground(true);
}
if(properties & ALIGNMENT && elem->has("alignment"))
{
std::string str = elem->get<std::string>("alignment");
if(str == "left")
setHorizontalAlignment(ALIGN_LEFT);
else if(str == "center")
setHorizontalAlignment(ALIGN_CENTER);
else if(str == "right")
setHorizontalAlignment(ALIGN_RIGHT);
else
LOG(LogError) << "Unknown text alignment string: " << str;
}
if(properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
setUppercase(elem->get<bool>("forceUppercase"));
2018-10-13 01:08:15 +00:00
if(properties & LINE_SPACING && elem->has("lineSpacing"))
setLineSpacing(elem->get<float>("lineSpacing"));
setFont(Font::getFromTheme(elem, properties, mFont));
}