Changed DateTimeEditComponent to use TextComponent instead of using Font facilities directly

This commit is contained in:
Leon Styhre 2024-08-11 18:22:45 +02:00
parent b697dc2a52
commit b0616fcbb1
2 changed files with 44 additions and 58 deletions

View file

@ -12,7 +12,6 @@
#include "components/DateTimeEditComponent.h" #include "components/DateTimeEditComponent.h"
#include "Settings.h" #include "Settings.h"
#include "resources/Font.h"
#include "utils/LocalizationUtil.h" #include "utils/LocalizationUtil.h"
#include "utils/StringUtil.h" #include "utils/StringUtil.h"
@ -23,18 +22,18 @@ DateTimeEditComponent::DateTimeEditComponent(bool alignRight)
, mKeyRepeatDir {0} , mKeyRepeatDir {0}
, mKeyRepeatTimer {0} , mKeyRepeatTimer {0}
, mColor {mMenuColorPrimary} , mColor {mMenuColorPrimary}
, mFont {Font::get(FONT_SIZE_SMALL, FONT_PATH_LIGHT)}
, mAlignRight {alignRight} , mAlignRight {alignRight}
, mUppercase {false} , mUppercase {false}
, mAutoSize {true} , mAutoSize {true}
{ {
updateTextCache(); mDateText = std::make_unique<TextComponent>("", Font::get(FONT_SIZE_SMALL, FONT_PATH_LIGHT));
updateText();
} }
void DateTimeEditComponent::onSizeChanged() void DateTimeEditComponent::onSizeChanged()
{ {
mAutoSize = false; mAutoSize = false;
updateTextCache(); updateText();
} }
void DateTimeEditComponent::setValue(const std::string& val) void DateTimeEditComponent::setValue(const std::string& val)
@ -43,7 +42,7 @@ void DateTimeEditComponent::setValue(const std::string& val)
mOriginalValue = val; mOriginalValue = val;
if (mAlignRight) if (mAlignRight)
mAutoSize = true; mAutoSize = true;
updateTextCache(); updateText();
} }
bool DateTimeEditComponent::input(InputConfig* config, Input input) bool DateTimeEditComponent::input(InputConfig* config, Input input)
@ -66,7 +65,7 @@ bool DateTimeEditComponent::input(InputConfig* config, Input input)
if (mTime == 0) { if (mTime == 0) {
mTime = Utils::Time::stringToTime("19990101T000000"); mTime = Utils::Time::stringToTime("19990101T000000");
mAutoSize = true; mAutoSize = true;
updateTextCache(); updateText();
} }
} }
@ -85,7 +84,7 @@ bool DateTimeEditComponent::input(InputConfig* config, Input input)
mEditing = false; mEditing = false;
mTime = mTimeBeforeEdit; mTime = mTimeBeforeEdit;
mKeyRepeatDir = 0; mKeyRepeatDir = 0;
updateTextCache(); updateText();
return false; return false;
} }
@ -94,7 +93,7 @@ bool DateTimeEditComponent::input(InputConfig* config, Input input)
mTime = mTimeBeforeEdit; mTime = mTimeBeforeEdit;
mKeyRepeatDir = 0; mKeyRepeatDir = 0;
mAutoSize = true; mAutoSize = true;
updateTextCache(); updateText();
updateHelpPrompts(); updateHelpPrompts();
return true; return true;
} }
@ -160,54 +159,50 @@ void DateTimeEditComponent::render(const glm::mat4& parentTrans)
{ {
glm::mat4 trans {parentTrans * getTransform()}; glm::mat4 trans {parentTrans * getTransform()};
if (mTextCache) { // Center vertically.
std::shared_ptr<Font> font {getFont()}; glm::vec3 off {0.0f, (mSize.y - mDateText->getSize().y) / 2.0f, 0.0f};
// Center vertically. trans = glm::translate(trans, glm::round(off));
glm::vec3 off {0.0f, (mSize.y - mTextCache->metrics.size.y) / 2.0f, 0.0f}; mRenderer->setMatrix(trans);
trans = glm::translate(trans, glm::round(off)); if (Settings::getInstance()->getBool("DebugText")) {
mRenderer->setMatrix(trans); mRenderer->setMatrix(trans);
mDateText->setDebugRendering(false);
if (Settings::getInstance()->getBool("DebugText")) { if (mDateText->getSize().x > 0.0f) {
mRenderer->setMatrix(trans); mRenderer->drawRect(0.0f, 0.0f - off.y, mSize.x - off.x, mSize.y, 0x0000FF33,
if (mTextCache->metrics.size.x > 0.0f) { 0x0000FF33);
mRenderer->drawRect(0.0f, 0.0f - off.y, mSize.x - off.x, mSize.y, 0x0000FF33,
0x0000FF33);
}
mRenderer->drawRect(0.0f, 0.0f, mTextCache->metrics.size.x, mTextCache->metrics.size.y,
0x00000033, 0x00000033);
} }
mRenderer->drawRect(0.0f, 0.0f, mDateText->getSize().x, mDateText->getSize().y, 0x00000033,
0x00000033);
}
mTextCache->setColor((mColor & 0xFFFFFF00) | static_cast<int>(getOpacity() * 255.0f)); mDateText->setColor((mColor & 0xFFFFFF00) | static_cast<int>(getOpacity() * 255.0f));
font->renderTextCache(mTextCache.get()); mDateText->render(trans);
if (mEditing && mTime != 0) { if (mEditing && mTime != 0) {
if (mEditIndex >= 0 && static_cast<unsigned int>(mEditIndex) < mCursorBoxes.size()) if (mEditIndex >= 0 && static_cast<unsigned int>(mEditIndex) < mCursorBoxes.size())
mRenderer->drawRect(mCursorBoxes[mEditIndex][0], mCursorBoxes[mEditIndex][1], mRenderer->drawRect(mCursorBoxes[mEditIndex][0], mCursorBoxes[mEditIndex][1],
mCursorBoxes[mEditIndex][2], mCursorBoxes[mEditIndex][3], mCursorBoxes[mEditIndex][2], mCursorBoxes[mEditIndex][3],
mMenuColorDateTimeEditMarker, mMenuColorDateTimeEditMarker); mMenuColorDateTimeEditMarker, mMenuColorDateTimeEditMarker);
}
} }
} }
void DateTimeEditComponent::setColor(unsigned int color) void DateTimeEditComponent::setColor(unsigned int color)
{ {
mColor = color; mColor = color;
if (mTextCache) mDateText->setColor(color);
mTextCache->setColor(color);
} }
void DateTimeEditComponent::setFont(std::shared_ptr<Font> font) void DateTimeEditComponent::setFont(std::shared_ptr<Font> font)
{ {
mFont = font; mDateText->setFont(font);
updateTextCache(); updateText();
} }
void DateTimeEditComponent::setUppercase(bool uppercase) void DateTimeEditComponent::setUppercase(bool uppercase)
{ {
mUppercase = uppercase; mUppercase = uppercase;
updateTextCache(); updateText();
} }
std::vector<HelpPrompt> DateTimeEditComponent::getHelpPrompts() std::vector<HelpPrompt> DateTimeEditComponent::getHelpPrompts()
@ -225,14 +220,6 @@ std::vector<HelpPrompt> DateTimeEditComponent::getHelpPrompts()
return prompts; return prompts;
} }
std::shared_ptr<Font> DateTimeEditComponent::getFont() const
{
if (mFont)
return mFont;
return Font::get(FONT_SIZE_MEDIUM);
}
std::string DateTimeEditComponent::getDisplayString() const std::string DateTimeEditComponent::getDisplayString() const
{ {
// ISO 8601 date format. // ISO 8601 date format.
@ -292,29 +279,28 @@ void DateTimeEditComponent::changeDate()
mTime = new_tm; mTime = new_tm;
mAutoSize = true; mAutoSize = true;
updateTextCache(); updateText();
} }
void DateTimeEditComponent::updateTextCache() void DateTimeEditComponent::updateText()
{ {
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'.
// The calling function simply needs to set this string using setValue(). // The calling function simply needs to set this string using setValue().
if (mTime.getIsoString() == "19710101T010101") { if (mTime.getIsoString() == "19710101T010101")
dispString = ""; dispString = "";
} else
else {
dispString = mUppercase ? Utils::String::toUpper(getDisplayString()) : getDisplayString(); dispString = mUppercase ? Utils::String::toUpper(getDisplayString()) : getDisplayString();
}
std::shared_ptr<Font> font {getFont()}; mDateText->setText(dispString);
mTextCache = std::unique_ptr<TextCache>(font->buildTextCache(dispString, 0, 0, mColor)); mDateText->setColor(mColor);
if (mAlignRight) if (mAlignRight)
mSize = mTextCache->metrics.size; mSize = mDateText->getSize();
if (mAutoSize) { if (mAutoSize) {
mSize = mTextCache->metrics.size; mSize = mDateText->getSize();
mAutoSize = false; mAutoSize = false;
if (getParent()) if (getParent())
@ -328,6 +314,8 @@ void DateTimeEditComponent::updateTextCache()
// Set up cursor positions. // Set up cursor positions.
std::shared_ptr<Font> font {mDateText->getFont()};
// Year. // Year.
glm::vec2 start {0.0f, 0.0f}; glm::vec2 start {0.0f, 0.0f};
glm::vec2 end {font->sizeText(dispString.substr(0, 4))}; glm::vec2 end {font->sizeText(dispString.substr(0, 4))};

View file

@ -10,8 +10,8 @@
#define ES_CORE_COMPONENTS_DATE_TIME_EDIT_COMPONENT_H #define ES_CORE_COMPONENTS_DATE_TIME_EDIT_COMPONENT_H
#include "GuiComponent.h" #include "GuiComponent.h"
#include "components/TextComponent.h"
#include "renderers/Renderer.h" #include "renderers/Renderer.h"
#include "resources/Font.h"
#include "utils/TimeUtil.h" #include "utils/TimeUtil.h"
class DateTimeEditComponent : public GuiComponent class DateTimeEditComponent : public GuiComponent
@ -41,11 +41,10 @@ public:
std::vector<HelpPrompt> getHelpPrompts() override; std::vector<HelpPrompt> getHelpPrompts() override;
private: private:
std::shared_ptr<Font> getFont() const override;
std::string getDisplayString() const; std::string getDisplayString() const;
void changeDate(); void changeDate();
void updateTextCache(); void updateText();
Renderer* mRenderer; Renderer* mRenderer;
Utils::Time::DateTime mTime; Utils::Time::DateTime mTime;
@ -57,7 +56,7 @@ private:
int mKeyRepeatDir; int mKeyRepeatDir;
int mKeyRepeatTimer; int mKeyRepeatTimer;
std::unique_ptr<TextCache> mTextCache; std::unique_ptr<TextComponent> mDateText;
std::vector<glm::vec4> mCursorBoxes; std::vector<glm::vec4> mCursorBoxes;
unsigned int mColor; unsigned int mColor;
@ -65,7 +64,6 @@ private:
unsigned int mColorOriginalValue; unsigned int mColorOriginalValue;
unsigned int mColorChangedValue; unsigned int mColorChangedValue;
std::shared_ptr<Font> mFont;
bool mAlignRight; bool mAlignRight;
bool mUppercase; bool mUppercase;
bool mAutoSize; bool mAutoSize;