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

View file

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