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

357 lines
8.4 KiB
C++
Raw Normal View History

//
// DateTimeEditComponent.cpp
//
// Date and time edit component.
//
2018-10-13 01:08:15 +00:00
#include "components/DateTimeEditComponent.h"
#include "resources/Font.h"
#include "utils/StringUtil.h"
DateTimeEditComponent::DateTimeEditComponent(
Window* window,
DisplayMode dispMode)
: GuiComponent(window),
mEditing(false),
mEditIndex(0),
mDisplayMode(dispMode),
mRelativeUpdateAccumulator(0),
mColor(0x777777FF),
mFont(Font::get(FONT_SIZE_SMALL, FONT_PATH_LIGHT)),
mUppercase(false),
mAutoSize(true)
2018-10-13 01:08:15 +00:00
{
updateTextCache();
}
void DateTimeEditComponent::setDisplayMode(DisplayMode mode)
{
mDisplayMode = mode;
updateTextCache();
}
bool DateTimeEditComponent::input(InputConfig* config, Input input)
{
if (input.value == 0)
2018-10-13 01:08:15 +00:00
return false;
if (config->isMappedTo("a", input)) {
if (mDisplayMode != DISP_RELATIVE_TO_NOW) // Don't allow editing for relative times.
2018-10-13 01:08:15 +00:00
mEditing = !mEditing;
if (mEditing) {
// Started editing.
2018-10-13 01:08:15 +00:00
mTimeBeforeEdit = mTime;
// Initialize to now if unset.
if (mTime.getTime() == Utils::Time::NOT_A_DATE_TIME) {
2018-10-13 01:08:15 +00:00
mTime = Utils::Time::now();
updateTextCache();
}
}
return true;
}
if (mEditing) {
if (config->isMappedTo("b", input)) {
2018-10-13 01:08:15 +00:00
mEditing = false;
mTime = mTimeBeforeEdit;
updateTextCache();
return true;
}
int incDir = 0;
if (config->isMappedLike("up", input) || config->isMappedLike("rightshoulder", input))
2018-10-13 01:08:15 +00:00
incDir = 1;
else if (config->isMappedLike("down", input) || config->isMappedLike("leftshoulder", input))
2018-10-13 01:08:15 +00:00
incDir = -1;
if (incDir != 0) {
2018-10-13 01:08:15 +00:00
tm new_tm = mTime;
// ISO 8601 date format.
if (mEditIndex == 0) {
new_tm.tm_year += incDir;
if (new_tm.tm_year < 0)
new_tm.tm_year = 0;
}
else if (mEditIndex == 1) {
2018-10-13 01:08:15 +00:00
new_tm.tm_mon += incDir;
if (new_tm.tm_mon > 11)
2018-10-13 01:08:15 +00:00
new_tm.tm_mon = 0;
else if (new_tm.tm_mon < 0)
2018-10-13 01:08:15 +00:00
new_tm.tm_mon = 11;
2018-10-13 01:08:15 +00:00
}
else if (mEditIndex == 2) {
const int days_in_month =
Utils::Time::daysInMonth(new_tm.tm_year + 1900, new_tm.tm_mon + 1);
2018-10-13 01:08:15 +00:00
new_tm.tm_mday += incDir;
if (new_tm.tm_mday > days_in_month)
2018-10-13 01:08:15 +00:00
new_tm.tm_mday = 1;
else if (new_tm.tm_mday < 1)
2018-10-13 01:08:15 +00:00
new_tm.tm_mday = days_in_month;
}
// Validate day.
const int days_in_month =
Utils::Time::daysInMonth(new_tm.tm_year + 1900, new_tm.tm_mon + 1);
if (new_tm.tm_mday > days_in_month)
2018-10-13 01:08:15 +00:00
new_tm.tm_mday = days_in_month;
mTime = new_tm;
2018-10-13 01:08:15 +00:00
updateTextCache();
return true;
}
if (config->isMappedLike("right", input)) {
2018-10-13 01:08:15 +00:00
mEditIndex++;
if (mEditIndex >= (int)mCursorBoxes.size())
2018-10-13 01:08:15 +00:00
mEditIndex--;
return true;
}
if (config->isMappedLike("left", input)) {
2018-10-13 01:08:15 +00:00
mEditIndex--;
if (mEditIndex < 0)
2018-10-13 01:08:15 +00:00
mEditIndex++;
return true;
}
}
return GuiComponent::input(config, input);
}
void DateTimeEditComponent::update(int deltaTime)
{
if (mDisplayMode == DISP_RELATIVE_TO_NOW) {
2018-10-13 01:08:15 +00:00
mRelativeUpdateAccumulator += deltaTime;
if (mRelativeUpdateAccumulator > 1000) {
2018-10-13 01:08:15 +00:00
mRelativeUpdateAccumulator = 0;
updateTextCache();
}
}
GuiComponent::update(deltaTime);
}
void DateTimeEditComponent::render(const Transform4x4f& parentTrans)
{
Transform4x4f trans = parentTrans * getTransform();
if (mTextCache) {
// Vertically center.
2018-10-13 01:08:15 +00:00
Vector3f off(0, (mSize.y() - mTextCache->metrics.size.y()) / 2, 0);
trans.translate(off);
Renderer::setMatrix(trans);
std::shared_ptr<Font> font = getFont();
mTextCache->setColor((mColor & 0xFFFFFF00) | getOpacity());
font->renderTextCache(mTextCache.get());
if (mEditing) {
if (mEditIndex >= 0 && (unsigned int)mEditIndex < mCursorBoxes.size())
Renderer::drawRect(mCursorBoxes[mEditIndex][0], mCursorBoxes[mEditIndex][1],
mCursorBoxes[mEditIndex][2], mCursorBoxes[mEditIndex][3],
0x00000022, 0x00000022);
2018-10-13 01:08:15 +00:00
}
}
}
void DateTimeEditComponent::setValue(const std::string& val)
{
mTime = val;
updateTextCache();
}
std::string DateTimeEditComponent::getValue() const
{
return mTime;
}
DateTimeEditComponent::DisplayMode DateTimeEditComponent::getCurrentDisplayMode() const
{
// if (mEditing) {
// if (mDisplayMode == DISP_RELATIVE_TO_NOW) {
// // TODO: if time component == 00:00:00, return DISP_DATE, else return DISP_DATE_TIME.
// return DISP_DATE;
// }
// }
2018-10-13 01:08:15 +00:00
return mDisplayMode;
}
std::string DateTimeEditComponent::getDisplayString(DisplayMode mode) const
{
// ISO 8601 date format.
2018-10-13 01:08:15 +00:00
std::string fmt;
switch (mode) {
case DISP_DATE: {
if (mTime.getTime() == 0)
// The extra blankspaces are for visual alignment.
return "unknown ";
fmt = "%Y-%m-%d";
2018-10-13 01:08:15 +00:00
break;
}
case DISP_DATE_TIME: {
if (mTime.getTime() == 0)
// The extra blankspaces are for visual alignment.
return "unknown ";
fmt = "%Y-%m-%d %H:%M:%S";
2018-10-13 01:08:15 +00:00
break;
}
case DISP_RELATIVE_TO_NOW: {
// Relative time.
if (mTime.getTime() == 0)
2018-10-13 01:08:15 +00:00
return "never";
Utils::Time::DateTime now(Utils::Time::now());
Utils::Time::Duration dur(now.getTime() - mTime.getTime());
char buf[64];
if (dur.getDays() > 0)
2018-10-13 01:08:15 +00:00
sprintf(buf, "%d day%s ago", dur.getDays(), (dur.getDays() > 1) ? "s" : "");
else if (dur.getHours() > 0)
2018-10-13 01:08:15 +00:00
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" : "");
2018-10-13 01:08:15 +00:00
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);
}
break;
}
2018-10-13 01:08:15 +00:00
return Utils::Time::timeToString(mTime, fmt);
}
std::shared_ptr<Font> DateTimeEditComponent::getFont() const
{
if (mFont)
2018-10-13 01:08:15 +00:00
return mFont;
return Font::get(FONT_SIZE_MEDIUM);
}
void DateTimeEditComponent::updateTextCache()
{
DisplayMode mode = getCurrentDisplayMode();
std::string dispString;
// Hack to set date string to blank instead of 'unknown'.
// The calling function simply needs to set this string using setValue().
if (mTime.getIsoString() == "99990101T000000") {
dispString = "";
}
else {
dispString = mUppercase ? Utils::String::toUpper(getDisplayString(mode)) :
getDisplayString(mode);
}
2018-10-13 01:08:15 +00:00
std::shared_ptr<Font> font = getFont();
mTextCache = std::unique_ptr<TextCache>(font->buildTextCache(dispString, 0, 0, mColor));
if (mAutoSize) {
2018-10-13 01:08:15 +00:00
mSize = mTextCache->metrics.size;
mAutoSize = false;
if (getParent())
2018-10-13 01:08:15 +00:00
getParent()->onSizeChanged();
}
if (dispString == "unknown " || dispString == "")
return;
// Set up cursor positions.
2018-10-13 01:08:15 +00:00
mCursorBoxes.clear();
if (dispString.empty() || mode == DISP_RELATIVE_TO_NOW)
2018-10-13 01:08:15 +00:00
return;
// Year.
2018-10-13 01:08:15 +00:00
Vector2f start(0, 0);
Vector2f end = font->sizeText(dispString.substr(0, 4));
2018-10-13 01:08:15 +00:00
Vector2f diff = end - start;
mCursorBoxes.push_back(Vector4f(start[0], start[1], diff[0], diff[1]));
// Month.
start[0] = font->sizeText(dispString.substr(0, 5)).x();
end = font->sizeText(dispString.substr(0, 7));
2018-10-13 01:08:15 +00:00
diff = end - start;
mCursorBoxes.push_back(Vector4f(start[0], start[1], diff[0], diff[1]));
// Day.
start[0] = font->sizeText(dispString.substr(0, 8)).x();
2018-10-13 01:08:15 +00:00
end = font->sizeText(dispString.substr(0, 10));
diff = end - start;
mCursorBoxes.push_back(Vector4f(start[0], start[1], diff[0], diff[1]));
// The logic for handling time for 'mode = DISP_DATE_TIME' is missing, but
// nobody will use it anyway so it's not worthwhile implementing.
2018-10-13 01:08:15 +00:00
}
void DateTimeEditComponent::setColor(unsigned int color)
{
mColor = color;
if (mTextCache)
2018-10-13 01:08:15 +00:00
mTextCache->setColor(color);
}
void DateTimeEditComponent::setFont(std::shared_ptr<Font> font)
{
mFont = font;
updateTextCache();
}
void DateTimeEditComponent::onSizeChanged()
{
mAutoSize = false;
updateTextCache();
}
void DateTimeEditComponent::setUppercase(bool uppercase)
{
mUppercase = uppercase;
updateTextCache();
}
void DateTimeEditComponent::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
{
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "datetime");
if (!elem)
2018-10-13 01:08:15 +00:00
return;
// We set mAutoSize BEFORE calling GuiComponent::applyTheme because it calls
// setSize(), which will call updateTextCache(), which will reset mSize if
2018-10-13 01:08:15 +00:00
// mAutoSize == true, ignoring the theme's value.
if (properties & ThemeFlags::SIZE)
2018-10-13 01:08:15 +00:00
mAutoSize = !elem->has("size");
GuiComponent::applyTheme(theme, view, element, properties);
using namespace ThemeFlags;
if (properties & COLOR && elem->has("color"))
2018-10-13 01:08:15 +00:00
setColor(elem->get<unsigned int>("color"));
if (properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
2018-10-13 01:08:15 +00:00
setUppercase(elem->get<bool>("forceUppercase"));
setFont(Font::getFromTheme(elem, properties, mFont));
}