2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// DateTimeEditComponent.cpp
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Date and time edit component.
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
|
2018-10-13 01:08:15 +00:00
|
|
|
#include "components/DateTimeEditComponent.h"
|
|
|
|
|
|
|
|
#include "resources/Font.h"
|
|
|
|
#include "utils/StringUtil.h"
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
DateTimeEditComponent::DateTimeEditComponent(
|
2020-06-21 12:25:28 +00:00
|
|
|
Window* window,
|
2021-03-20 10:08:28 +00:00
|
|
|
bool alignRight,
|
2020-06-21 12:25:28 +00:00
|
|
|
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),
|
2021-03-20 10:08:28 +00:00
|
|
|
mAutoSize(true),
|
|
|
|
mAlignRight(alignRight)
|
2018-10-13 01:08:15 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
updateTextCache();
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::setDisplayMode(DisplayMode mode)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mDisplayMode = mode;
|
|
|
|
updateTextCache();
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DateTimeEditComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (input.value == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (config->isMappedTo("a", input)) {
|
|
|
|
if (mDisplayMode != DISP_RELATIVE_TO_NOW) // Don't allow editing for relative times.
|
|
|
|
mEditing = !mEditing;
|
|
|
|
|
2020-08-02 13:56:32 +00:00
|
|
|
// Change the color of the text to reflect the changes.
|
|
|
|
if (mTime == mOriginalValue)
|
|
|
|
setColor(mColorOriginalValue);
|
|
|
|
else
|
|
|
|
setColor(mColorChangedValue);
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mEditing) {
|
|
|
|
// Started editing.
|
|
|
|
mTimeBeforeEdit = mTime;
|
|
|
|
|
|
|
|
// Initialize to now if unset.
|
2020-08-02 13:56:32 +00:00
|
|
|
if (mTime.getTime() == Utils::Time::DEFAULT_TIMEVALUE) {
|
2020-06-21 12:25:28 +00:00
|
|
|
mTime = Utils::Time::now();
|
|
|
|
updateTextCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEditing) {
|
2020-08-02 13:56:32 +00:00
|
|
|
|
|
|
|
if (config->isMappedLike("lefttrigger", input) ||
|
|
|
|
config->isMappedLike("righttrigger", input))
|
|
|
|
return true;
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (config->isMappedTo("b", input)) {
|
|
|
|
mEditing = false;
|
|
|
|
mTime = mTimeBeforeEdit;
|
|
|
|
updateTextCache();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int incDir = 0;
|
|
|
|
if (config->isMappedLike("up", input) || config->isMappedLike("rightshoulder", input))
|
|
|
|
incDir = 1;
|
|
|
|
else if (config->isMappedLike("down", input) || config->isMappedLike("leftshoulder", input))
|
|
|
|
incDir = -1;
|
|
|
|
|
|
|
|
if (incDir != 0) {
|
|
|
|
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) {
|
|
|
|
new_tm.tm_mon += incDir;
|
|
|
|
|
|
|
|
if (new_tm.tm_mon > 11)
|
|
|
|
new_tm.tm_mon = 0;
|
|
|
|
else if (new_tm.tm_mon < 0)
|
|
|
|
new_tm.tm_mon = 11;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (mEditIndex == 2) {
|
|
|
|
const int days_in_month =
|
|
|
|
Utils::Time::daysInMonth(new_tm.tm_year + 1900, new_tm.tm_mon + 1);
|
|
|
|
new_tm.tm_mday += incDir;
|
|
|
|
|
|
|
|
if (new_tm.tm_mday > days_in_month)
|
|
|
|
new_tm.tm_mday = 1;
|
|
|
|
else if (new_tm.tm_mday < 1)
|
|
|
|
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)
|
|
|
|
new_tm.tm_mday = days_in_month;
|
|
|
|
|
|
|
|
mTime = new_tm;
|
|
|
|
|
|
|
|
updateTextCache();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->isMappedLike("right", input)) {
|
|
|
|
mEditIndex++;
|
2020-11-17 22:06:54 +00:00
|
|
|
if (mEditIndex >= static_cast<int>(mCursorBoxes.size()))
|
2020-06-21 12:25:28 +00:00
|
|
|
mEditIndex--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->isMappedLike("left", input)) {
|
|
|
|
mEditIndex--;
|
|
|
|
if (mEditIndex < 0)
|
|
|
|
mEditIndex++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::update(int deltaTime)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mDisplayMode == DISP_RELATIVE_TO_NOW) {
|
|
|
|
mRelativeUpdateAccumulator += deltaTime;
|
|
|
|
if (mRelativeUpdateAccumulator > 1000) {
|
|
|
|
mRelativeUpdateAccumulator = 0;
|
|
|
|
updateTextCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiComponent::update(deltaTime);
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::render(const Transform4x4f& parentTrans)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mTextCache) {
|
2021-03-20 09:26:18 +00:00
|
|
|
std::shared_ptr<Font> font = getFont();
|
|
|
|
float referenceSize;
|
|
|
|
|
2021-03-20 10:11:19 +00:00
|
|
|
if (mAlignRight) {
|
|
|
|
if (mTime != 0)
|
|
|
|
referenceSize = font->sizeText("ABCDEFG").x();
|
|
|
|
else
|
|
|
|
referenceSize = font->sizeText("ABCDEIJ").x();
|
|
|
|
}
|
2021-03-20 09:26:18 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Vertically center.
|
2021-03-20 09:26:18 +00:00
|
|
|
Vector3f off(0, (mSize.y() - mTextCache->metrics.size.y()) / 2.0f, 0.0f);
|
2021-03-20 10:08:28 +00:00
|
|
|
if (mAlignRight)
|
|
|
|
off.x() += referenceSize - mTextCache->metrics.size.x();
|
2020-06-21 12:25:28 +00:00
|
|
|
trans.translate(off);
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mTextCache->setColor((mColor & 0xFFFFFF00) | getOpacity());
|
|
|
|
font->renderTextCache(mTextCache.get());
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mEditing) {
|
2020-11-17 22:06:54 +00:00
|
|
|
if (mEditIndex >= 0 && static_cast<unsigned int>(mEditIndex) < mCursorBoxes.size())
|
2020-06-21 12:25:28 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mTime = val;
|
2020-07-15 15:44:27 +00:00
|
|
|
mOriginalValue = val;
|
2020-06-21 12:25:28 +00:00
|
|
|
updateTextCache();
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DateTimeEditComponent::getValue() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return mTime;
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DateTimeEditComponent::DisplayMode DateTimeEditComponent::getCurrentDisplayMode() const
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
// 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
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return mDisplayMode;
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DateTimeEditComponent::getDisplayString(DisplayMode mode) const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// ISO 8601 date format.
|
|
|
|
std::string fmt;
|
|
|
|
switch (mode) {
|
|
|
|
case DISP_DATE: {
|
|
|
|
if (mTime.getTime() == 0)
|
2021-03-20 09:26:18 +00:00
|
|
|
return "unknown";
|
2020-06-21 12:25:28 +00:00
|
|
|
fmt = "%Y-%m-%d";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DISP_DATE_TIME: {
|
|
|
|
if (mTime.getTime() == 0)
|
2021-03-20 09:26:18 +00:00
|
|
|
return "unknown";
|
2020-06-21 12:25:28 +00:00
|
|
|
fmt = "%Y-%m-%d %H:%M:%S";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DISP_RELATIVE_TO_NOW: {
|
|
|
|
// Relative time.
|
|
|
|
if (mTime.getTime() == 0)
|
|
|
|
return "never";
|
|
|
|
|
|
|
|
Utils::Time::DateTime now(Utils::Time::now());
|
|
|
|
Utils::Time::Duration dur(now.getTime() - mTime.getTime());
|
|
|
|
|
2020-12-17 19:43:52 +00:00
|
|
|
std::string buf;
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
if (dur.getDays() > 0)
|
2020-12-17 19:43:52 +00:00
|
|
|
buf = std::to_string(dur.getDays()) + " day" +
|
|
|
|
(dur.getDays() > 1 ? "s" : "") + " ago";
|
2020-06-21 12:25:28 +00:00
|
|
|
else if (dur.getHours() > 0)
|
2020-12-17 19:43:52 +00:00
|
|
|
buf = std::to_string(dur.getHours()) + " hour" +
|
|
|
|
(dur.getHours() > 1 ? "s" : "") + " ago";
|
2020-06-21 12:25:28 +00:00
|
|
|
else if (dur.getMinutes() > 0)
|
2020-12-17 19:43:52 +00:00
|
|
|
buf = std::to_string(dur.getMinutes()) + " minute" +
|
|
|
|
(dur.getMinutes() > 1 ? "s" : "") + " ago";
|
2020-06-21 12:25:28 +00:00
|
|
|
else
|
2020-12-17 19:43:52 +00:00
|
|
|
buf = std::to_string(dur.getSeconds()) + " second" +
|
|
|
|
(dur.getSeconds() > 1 || dur.getSeconds() == 0 ? "s" : "") + " ago";
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Utils::Time::timeToString(mTime, fmt);
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Font> DateTimeEditComponent::getFont() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mFont)
|
|
|
|
return mFont;
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return Font::get(FONT_SIZE_MEDIUM);
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::updateTextCache()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
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().
|
2020-07-20 08:19:15 +00:00
|
|
|
if (mTime.getIsoString() == "19700101T010101") {
|
2020-06-21 12:25:28 +00:00
|
|
|
dispString = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dispString = mUppercase ? Utils::String::toUpper(getDisplayString(mode)) :
|
|
|
|
getDisplayString(mode);
|
|
|
|
}
|
|
|
|
std::shared_ptr<Font> font = getFont();
|
|
|
|
mTextCache = std::unique_ptr<TextCache>(font->buildTextCache(dispString, 0, 0, mColor));
|
|
|
|
|
|
|
|
if (mAutoSize) {
|
|
|
|
mSize = mTextCache->metrics.size;
|
|
|
|
|
|
|
|
mAutoSize = false;
|
|
|
|
if (getParent())
|
|
|
|
getParent()->onSizeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dispString == "unknown " || dispString == "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Set up cursor positions.
|
|
|
|
mCursorBoxes.clear();
|
|
|
|
|
|
|
|
if (dispString.empty() || mode == DISP_RELATIVE_TO_NOW)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Year.
|
|
|
|
Vector2f start(0, 0);
|
|
|
|
Vector2f end = font->sizeText(dispString.substr(0, 4));
|
|
|
|
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));
|
|
|
|
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();
|
|
|
|
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)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mColor = color;
|
|
|
|
if (mTextCache)
|
|
|
|
mTextCache->setColor(color);
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::setFont(std::shared_ptr<Font> font)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mFont = font;
|
|
|
|
updateTextCache();
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::onSizeChanged()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mAutoSize = false;
|
|
|
|
updateTextCache();
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeEditComponent::setUppercase(bool uppercase)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mUppercase = uppercase;
|
|
|
|
updateTextCache();
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
void DateTimeEditComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
|
2020-06-21 12:25:28 +00:00
|
|
|
const std::string& view, const std::string& element, unsigned int properties)
|
2018-10-13 01:08:15 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "datetime");
|
2020-06-06 14:48:05 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!elem)
|
|
|
|
return;
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// We set mAutoSize BEFORE calling GuiComponent::applyTheme because it calls
|
|
|
|
// setSize(), which will call updateTextCache(), which will reset mSize if
|
|
|
|
// mAutoSize == true, ignoring the theme's value.
|
|
|
|
if (properties & ThemeFlags::SIZE)
|
|
|
|
mAutoSize = !elem->has("size");
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
GuiComponent::applyTheme(theme, view, element, properties);
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
using namespace ThemeFlags;
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (properties & COLOR && elem->has("color"))
|
|
|
|
setColor(elem->get<unsigned int>("color"));
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
|
|
|
|
setUppercase(elem->get<bool>("forceUppercase"));
|
2018-10-13 01:08:15 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
setFont(Font::getFromTheme(elem, properties, mFont));
|
2018-10-13 01:08:15 +00:00
|
|
|
}
|
2020-06-09 18:03:31 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> DateTimeEditComponent::getHelpPrompts()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
prompts.push_back(HelpPrompt("a", "edit date"));
|
|
|
|
return prompts;
|
2020-06-09 18:03:31 +00:00
|
|
|
}
|