Removed some obsolete code from DateTimeEditComponent

This commit is contained in:
Leon Styhre 2024-07-17 16:49:51 +02:00
parent ad2dd1832f
commit 13fadd1658
2 changed files with 23 additions and 109 deletions

View file

@ -16,14 +16,12 @@
#include "utils/LocalizationUtil.h" #include "utils/LocalizationUtil.h"
#include "utils/StringUtil.h" #include "utils/StringUtil.h"
DateTimeEditComponent::DateTimeEditComponent(bool alignRight, DisplayMode dispMode) DateTimeEditComponent::DateTimeEditComponent(bool alignRight)
: mRenderer {Renderer::getInstance()} : mRenderer {Renderer::getInstance()}
, mEditing {false} , mEditing {false}
, mEditIndex {0} , mEditIndex {0}
, mDisplayMode {dispMode}
, mKeyRepeatDir {0} , mKeyRepeatDir {0}
, mKeyRepeatTimer {0} , mKeyRepeatTimer {0}
, mRelativeUpdateAccumulator {0}
, mColor {mMenuColorPrimary} , mColor {mMenuColorPrimary}
, mFont {Font::get(FONT_SIZE_SMALL, FONT_PATH_LIGHT)} , mFont {Font::get(FONT_SIZE_SMALL, FONT_PATH_LIGHT)}
, mAlignRight {alignRight} , mAlignRight {alignRight}
@ -43,15 +41,15 @@ void DateTimeEditComponent::setValue(const std::string& val)
{ {
mTime = val; mTime = val;
mOriginalValue = val; mOriginalValue = val;
if (mAlignRight)
mAutoSize = true;
updateTextCache(); updateTextCache();
} }
bool DateTimeEditComponent::input(InputConfig* config, Input input) bool DateTimeEditComponent::input(InputConfig* config, Input input)
{ {
if (config->isMappedTo("a", input) && input.value) { if (config->isMappedTo("a", input) && input.value) {
if (mDisplayMode != DISP_RELATIVE_TO_NOW) // Don't allow editing for relative times. mEditing = !mEditing;
mEditing = !mEditing;
mKeyRepeatDir = 0; mKeyRepeatDir = 0;
// Change the color of the text to reflect the changes. // Change the color of the text to reflect the changes.
@ -67,6 +65,7 @@ bool DateTimeEditComponent::input(InputConfig* config, Input input)
// Initialize to the arbitrary value 1999-01-01 if unset. // Initialize to the arbitrary value 1999-01-01 if unset.
if (mTime == 0) { if (mTime == 0) {
mTime = Utils::Time::stringToTime("19990101T000000"); mTime = Utils::Time::stringToTime("19990101T000000");
mAutoSize = true;
updateTextCache(); updateTextCache();
} }
} }
@ -76,7 +75,6 @@ bool DateTimeEditComponent::input(InputConfig* config, Input input)
} }
if (mEditing) { if (mEditing) {
if ((config->isMappedLike("lefttrigger", input) || if ((config->isMappedLike("lefttrigger", input) ||
config->isMappedLike("righttrigger", input))) { config->isMappedLike("righttrigger", input))) {
mKeyRepeatDir = 0; mKeyRepeatDir = 0;
@ -95,6 +93,7 @@ bool DateTimeEditComponent::input(InputConfig* config, Input input)
mEditing = false; mEditing = false;
mTime = mTimeBeforeEdit; mTime = mTimeBeforeEdit;
mKeyRepeatDir = 0; mKeyRepeatDir = 0;
mAutoSize = true;
updateTextCache(); updateTextCache();
updateHelpPrompts(); updateHelpPrompts();
return true; return true;
@ -154,14 +153,6 @@ void DateTimeEditComponent::update(int deltaTime)
} }
} }
if (mDisplayMode == DISP_RELATIVE_TO_NOW) {
mRelativeUpdateAccumulator += deltaTime;
if (mRelativeUpdateAccumulator > 1000) {
mRelativeUpdateAccumulator = 0;
updateTextCache();
}
}
GuiComponent::update(deltaTime); GuiComponent::update(deltaTime);
} }
@ -171,18 +162,11 @@ void DateTimeEditComponent::render(const glm::mat4& parentTrans)
if (mTextCache) { if (mTextCache) {
std::shared_ptr<Font> font {getFont()}; std::shared_ptr<Font> font {getFont()};
float referenceSize {0.0f};
if (mAlignRight) // Center vertically.
referenceSize = std::round(mParent->getSize().x * 0.1045f);
// Vertically center.
glm::vec3 off {0.0f, (mSize.y - mTextCache->metrics.size.y) / 2.0f, 0.0f}; glm::vec3 off {0.0f, (mSize.y - mTextCache->metrics.size.y) / 2.0f, 0.0f};
if (mAlignRight)
off.x += referenceSize - mTextCache->metrics.size.x;
trans = glm::translate(trans, glm::round(off)); trans = glm::translate(trans, glm::round(off));
mRenderer->setMatrix(trans); mRenderer->setMatrix(trans);
if (Settings::getInstance()->getBool("DebugText")) { if (Settings::getInstance()->getBool("DebugText")) {
@ -207,12 +191,6 @@ void DateTimeEditComponent::render(const glm::mat4& parentTrans)
} }
} }
void DateTimeEditComponent::setDisplayMode(DisplayMode mode)
{
mDisplayMode = mode;
updateTextCache();
}
void DateTimeEditComponent::setColor(unsigned int color) void DateTimeEditComponent::setColor(unsigned int color)
{ {
mColor = color; mColor = color;
@ -255,51 +233,13 @@ std::shared_ptr<Font> DateTimeEditComponent::getFont() const
return Font::get(FONT_SIZE_MEDIUM); return Font::get(FONT_SIZE_MEDIUM);
} }
std::string DateTimeEditComponent::getDisplayString(DisplayMode mode) const std::string DateTimeEditComponent::getDisplayString() const
{ {
// ISO 8601 date format. // ISO 8601 date format.
std::string fmt; std::string fmt;
switch (mode) { if (mTime.getTime() == 0)
case DISP_DATE: { return _("unknown");
if (mTime.getTime() == 0) fmt = "%Y-%m-%d";
return "unknown";
fmt = "%Y-%m-%d";
break;
}
case DISP_DATE_TIME: {
if (mTime.getTime() == 0)
return "unknown";
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()};
std::string buf;
if (dur.getDays() > 0)
buf = std::to_string(dur.getDays()) + // Line break.
" day" + (dur.getDays() > 1 ? "s" : "") + " ago";
else if (dur.getHours() > 0)
buf = std::to_string(dur.getHours()) + // Line break.
" hour" + (dur.getHours() > 1 ? "s" : "") + " ago";
else if (dur.getMinutes() > 0)
buf = std::to_string(dur.getMinutes()) + // Line break.
" minute" + (dur.getMinutes() > 1 ? "s" : "") + " ago";
else
buf = std::to_string(dur.getSeconds()) + // Line break.
" second" + (dur.getSeconds() > 1 || dur.getSeconds() == 0 ? "s" : "") +
" ago";
return buf;
break;
}
}
return Utils::Time::timeToString(mTime, fmt); return Utils::Time::timeToString(mTime, fmt);
} }
@ -351,13 +291,12 @@ void DateTimeEditComponent::changeDate()
else else
mTime = new_tm; mTime = new_tm;
mAutoSize = true;
updateTextCache(); updateTextCache();
} }
void DateTimeEditComponent::updateTextCache() void DateTimeEditComponent::updateTextCache()
{ {
DisplayMode mode {getCurrentDisplayMode()};
std::string dispString; std::string dispString;
// Hack to set date string to blank instead of 'unknown'. // Hack to set date string to blank instead of 'unknown'.
@ -366,14 +305,16 @@ void DateTimeEditComponent::updateTextCache()
dispString = ""; dispString = "";
} }
else { else {
dispString = dispString = mUppercase ? Utils::String::toUpper(getDisplayString()) : getDisplayString();
mUppercase ? Utils::String::toUpper(getDisplayString(mode)) : getDisplayString(mode);
} }
std::shared_ptr<Font> font = getFont(); std::shared_ptr<Font> font {getFont()};
// Used to initialize all glyphs, which is needed to populate mMaxGlyphHeight. // Used to initialize all glyphs, which is needed to populate mMaxGlyphHeight.
font->loadGlyphs(dispString + "\n"); font->loadGlyphs(dispString + "\n");
mTextCache = std::unique_ptr<TextCache>(font->buildTextCache(dispString, 0, 0, mColor)); mTextCache = std::unique_ptr<TextCache>(font->buildTextCache(dispString, 0, 0, mColor));
if (mAlignRight)
mSize = mTextCache->metrics.size;
if (mAutoSize) { if (mAutoSize) {
mSize = mTextCache->metrics.size; mSize = mTextCache->metrics.size;
mAutoSize = false; mAutoSize = false;
@ -382,14 +323,12 @@ void DateTimeEditComponent::updateTextCache()
getParent()->onSizeChanged(); getParent()->onSizeChanged();
} }
if (dispString == "unknown " || dispString == "") mCursorBoxes.clear();
if (dispString.empty() || dispString == _("unknown"))
return; return;
// Set up cursor positions. // Set up cursor positions.
mCursorBoxes.clear();
if (dispString.empty() || mode == DISP_RELATIVE_TO_NOW)
return;
// Year. // Year.
glm::vec2 start {0.0f, 0.0f}; glm::vec2 start {0.0f, 0.0f};
@ -408,7 +347,4 @@ void DateTimeEditComponent::updateTextCache()
end = font->sizeText(dispString.substr(0, 10)); end = font->sizeText(dispString.substr(0, 10));
diff = end - start; diff = end - start;
mCursorBoxes.push_back(glm::vec4 {start[0], start[1], diff[0], diff[1]}); mCursorBoxes.push_back(glm::vec4 {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.
} }

View file

@ -11,21 +11,13 @@
#include "GuiComponent.h" #include "GuiComponent.h"
#include "renderers/Renderer.h" #include "renderers/Renderer.h"
#include "resources/Font.h"
#include "utils/TimeUtil.h" #include "utils/TimeUtil.h"
class TextCache;
// Used to enter or display a specific point in time.
class DateTimeEditComponent : public GuiComponent class DateTimeEditComponent : public GuiComponent
{ {
public: public:
enum DisplayMode { DateTimeEditComponent(bool alignRight = false);
DISP_DATE,
DISP_DATE_TIME,
DISP_RELATIVE_TO_NOW
};
DateTimeEditComponent(bool alignRight = false, DisplayMode dispMode = DISP_DATE);
void onSizeChanged() override; void onSizeChanged() override;
@ -37,32 +29,20 @@ public:
void update(int deltaTime) override; void update(int deltaTime) override;
void render(const glm::mat4& parentTrans) override; void render(const glm::mat4& parentTrans) override;
// Set how the point in time will be displayed:
// * DISP_DATE - only display the date.
// * DISP_DATE_TIME - display both the date and the time on that date.
// * DISP_RELATIVE_TO_NOW - intelligently display the point in time relative to
// right now (e.g. "5 secs ago", "3 minutes ago", "1 day ago".
// Automatically updates as time marches on.
// The initial value is DISP_DATE.
void setDisplayMode(DisplayMode mode);
// Text color. // Text color.
void setColor(unsigned int color) override; void setColor(unsigned int color) override;
// Font to use. Default is Font::get(FONT_SIZE_MEDIUM).
void setOriginalColor(unsigned int color) override { mColorOriginalValue = color; } void setOriginalColor(unsigned int color) override { mColorOriginalValue = color; }
void setChangedColor(unsigned int color) override { mColorChangedValue = color; } void setChangedColor(unsigned int color) override { mColorChangedValue = color; }
void setFont(std::shared_ptr<Font> font); void setFont(std::shared_ptr<Font> font);
// Force text to be uppercase when in DISP_RELATIVE_TO_NOW mode.
void setUppercase(bool uppercase); void setUppercase(bool uppercase);
std::vector<HelpPrompt> getHelpPrompts() override; std::vector<HelpPrompt> getHelpPrompts() override;
private: private:
std::shared_ptr<Font> getFont() const override; std::shared_ptr<Font> getFont() const override;
std::string getDisplayString(DisplayMode mode) const; std::string getDisplayString() const;
DisplayMode getCurrentDisplayMode() const { return mDisplayMode; }
void changeDate(); void changeDate();
void updateTextCache(); void updateTextCache();
@ -73,11 +53,9 @@ private:
bool mEditing; bool mEditing;
int mEditIndex; int mEditIndex;
DisplayMode mDisplayMode;
int mKeyRepeatDir; int mKeyRepeatDir;
int mKeyRepeatTimer; int mKeyRepeatTimer;
int mRelativeUpdateAccumulator;
std::unique_ptr<TextCache> mTextCache; std::unique_ptr<TextCache> mTextCache;
std::vector<glm::vec4> mCursorBoxes; std::vector<glm::vec4> mCursorBoxes;