2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// TextComponent.cpp
|
|
|
|
//
|
|
|
|
// Displays text.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/TextComponent.h"
|
2017-03-13 21:11:07 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Settings.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2013-06-14 15:48:13 +00:00
|
|
|
|
2022-01-19 17:01:54 +00:00
|
|
|
TextComponent::TextComponent()
|
|
|
|
: mFont {Font::get(FONT_SIZE_MEDIUM)}
|
2022-03-14 18:51:48 +00:00
|
|
|
, mRenderer {Renderer::getInstance()}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mColor {0x000000FF}
|
2022-01-30 18:31:53 +00:00
|
|
|
, mBgColor {0x00000000}
|
2022-02-11 21:10:25 +00:00
|
|
|
, mColorOpacity {1.0f}
|
|
|
|
, mBgColorOpacity {0.0f}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mRenderBackground {false}
|
|
|
|
, mUppercase {false}
|
2022-02-09 19:44:22 +00:00
|
|
|
, mLowercase {false}
|
2022-02-09 21:06:34 +00:00
|
|
|
, mCapitalize {false}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mAutoCalcExtent {1, 1}
|
|
|
|
, mHorizontalAlignment {ALIGN_LEFT}
|
|
|
|
, mVerticalAlignment {ALIGN_CENTER}
|
|
|
|
, mLineSpacing {1.5f}
|
|
|
|
, mNoTopMargin {false}
|
|
|
|
, mSelectable {false}
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:01:54 +00:00
|
|
|
TextComponent::TextComponent(const std::string& text,
|
2021-07-07 18:31:46 +00:00
|
|
|
const std::shared_ptr<Font>& font,
|
|
|
|
unsigned int color,
|
|
|
|
Alignment align,
|
2021-08-15 20:03:17 +00:00
|
|
|
glm::vec3 pos,
|
2021-08-16 16:25:01 +00:00
|
|
|
glm::vec2 size,
|
2021-10-15 18:58:40 +00:00
|
|
|
unsigned int bgcolor)
|
2022-01-19 17:01:54 +00:00
|
|
|
: mFont {nullptr}
|
2022-03-14 18:51:48 +00:00
|
|
|
, mRenderer {Renderer::getInstance()}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mColor {0x000000FF}
|
2022-01-30 18:31:53 +00:00
|
|
|
, mBgColor {0x00000000}
|
2022-02-11 21:10:25 +00:00
|
|
|
, mColorOpacity {1.0f}
|
|
|
|
, mBgColorOpacity {0.0f}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mRenderBackground {false}
|
|
|
|
, mUppercase {false}
|
2022-02-09 19:44:22 +00:00
|
|
|
, mLowercase {false}
|
2022-02-09 21:06:34 +00:00
|
|
|
, mCapitalize {false}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mAutoCalcExtent {1, 1}
|
|
|
|
, mHorizontalAlignment {align}
|
|
|
|
, mVerticalAlignment {ALIGN_CENTER}
|
|
|
|
, mLineSpacing {1.5f}
|
|
|
|
, mNoTopMargin {false}
|
|
|
|
, mSelectable {false}
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
setFont(font);
|
|
|
|
setColor(color);
|
|
|
|
setBackgroundColor(bgcolor);
|
2021-09-28 19:46:45 +00:00
|
|
|
setText(text, false);
|
2021-08-15 20:03:17 +00:00
|
|
|
setPosition(pos);
|
2020-06-28 16:39:18 +00:00
|
|
|
setSize(size);
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void TextComponent::onSizeChanged()
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2022-01-16 11:09:55 +00:00
|
|
|
mAutoCalcExtent = glm::ivec2 {(getSize().x == 0), (getSize().y == 0)};
|
2020-06-28 16:39:18 +00:00
|
|
|
onTextChanged();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 01:35:16 +00:00
|
|
|
void TextComponent::setFont(const std::shared_ptr<Font>& font)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2021-09-28 19:46:45 +00:00
|
|
|
if (mFont == font)
|
|
|
|
return;
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mFont = font;
|
|
|
|
onTextChanged();
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// Set the color of the font/text.
|
2013-06-14 15:48:13 +00:00
|
|
|
void TextComponent::setColor(unsigned int color)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mColor = color;
|
2022-02-11 21:10:25 +00:00
|
|
|
mColorOpacity = static_cast<float>(mColor & 0x000000FF) / 255.0f;
|
2020-06-28 16:39:18 +00:00
|
|
|
onColorChanged();
|
2013-10-10 21:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// Set the color of the background box.
|
2017-03-13 21:11:07 +00:00
|
|
|
void TextComponent::setBackgroundColor(unsigned int color)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mBgColor = color;
|
2022-02-11 21:10:25 +00:00
|
|
|
mBgColorOpacity = static_cast<float>(mBgColor & 0x000000FF) / 255.0f;
|
2017-03-13 21:11:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 21:10:25 +00:00
|
|
|
void TextComponent::setOpacity(float opacity)
|
2013-10-10 21:14:33 +00:00
|
|
|
{
|
2022-03-11 22:17:04 +00:00
|
|
|
float textOpacity {opacity * mColorOpacity};
|
2022-02-12 16:38:55 +00:00
|
|
|
mColor = (mColor & 0xFFFFFF00) | static_cast<unsigned char>(textOpacity * 255.0f);
|
2017-05-01 02:54:27 +00:00
|
|
|
|
2022-03-11 22:17:04 +00:00
|
|
|
float textBackgroundOpacity {opacity * mBgColorOpacity};
|
2022-02-12 16:38:55 +00:00
|
|
|
mBgColor = (mBgColor & 0xFFFFFF00) | static_cast<unsigned char>(textBackgroundOpacity * 255.0f);
|
2013-10-10 21:14:33 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
onColorChanged();
|
|
|
|
GuiComponent::setOpacity(opacity);
|
2022-03-11 22:17:04 +00:00
|
|
|
|
|
|
|
if (mTextCache)
|
|
|
|
mTextCache->setOpacity(mThemeOpacity);
|
|
|
|
}
|
|
|
|
|
2022-03-11 22:51:41 +00:00
|
|
|
void TextComponent::setDimming(float dimming)
|
2022-03-11 22:17:04 +00:00
|
|
|
{
|
2022-03-11 22:51:41 +00:00
|
|
|
mDimming = dimming;
|
2022-03-11 22:17:04 +00:00
|
|
|
if (mTextCache)
|
2022-03-11 22:51:41 +00:00
|
|
|
mTextCache->setDimming(dimming);
|
2013-10-10 21:14:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 19:46:45 +00:00
|
|
|
void TextComponent::setText(const std::string& text, bool update)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2021-09-28 19:46:45 +00:00
|
|
|
if (mText == text)
|
|
|
|
return;
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mText = text;
|
2021-09-28 19:46:45 +00:00
|
|
|
|
|
|
|
if (update)
|
|
|
|
onTextChanged();
|
2013-07-02 05:57:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 19:51:50 +00:00
|
|
|
void TextComponent::setUppercase(bool uppercase)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mUppercase = uppercase;
|
2022-02-09 19:44:22 +00:00
|
|
|
if (uppercase) {
|
|
|
|
mLowercase = false;
|
2022-02-09 21:06:34 +00:00
|
|
|
mCapitalize = false;
|
2022-02-09 19:44:22 +00:00
|
|
|
}
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setLowercase(bool lowercase)
|
|
|
|
{
|
|
|
|
mLowercase = lowercase;
|
|
|
|
if (lowercase) {
|
|
|
|
mUppercase = false;
|
2022-02-09 21:06:34 +00:00
|
|
|
mCapitalize = false;
|
2022-02-09 19:44:22 +00:00
|
|
|
}
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
2022-02-09 21:06:34 +00:00
|
|
|
void TextComponent::setCapitalize(bool capitalize)
|
2022-02-09 19:44:22 +00:00
|
|
|
{
|
2022-02-09 21:06:34 +00:00
|
|
|
mCapitalize = capitalize;
|
|
|
|
if (capitalize) {
|
2022-02-09 19:44:22 +00:00
|
|
|
mUppercase = false;
|
|
|
|
mLowercase = false;
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
onTextChanged();
|
2014-05-03 19:51:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void TextComponent::render(const glm::mat4& parentTrans)
|
2013-06-14 15:48:13 +00:00
|
|
|
{
|
2022-08-21 14:51:21 +00:00
|
|
|
if (!isVisible() || mThemeOpacity == 0.0f || mSize.x == 0.0f || mSize.y == 0.0f)
|
2020-06-28 16:39:18 +00:00
|
|
|
return;
|
|
|
|
|
2022-01-16 11:09:55 +00:00
|
|
|
glm::mat4 trans {parentTrans * getTransform()};
|
2022-03-14 18:51:48 +00:00
|
|
|
mRenderer->setMatrix(trans);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-03-11 22:17:04 +00:00
|
|
|
if (mRenderBackground)
|
2022-03-14 18:51:48 +00:00
|
|
|
mRenderer->drawRect(0.0f, 0.0f, mSize.x, mSize.y, mBgColor, mBgColor, false,
|
|
|
|
mOpacity * mThemeOpacity, mDimming);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
if (mTextCache) {
|
2022-01-16 11:09:55 +00:00
|
|
|
const glm::vec2& textSize {mTextCache->metrics.size};
|
2022-04-18 11:47:22 +00:00
|
|
|
float yOff {0.0f};
|
|
|
|
|
|
|
|
if (mSize.y > textSize.y) {
|
|
|
|
switch (mVerticalAlignment) {
|
|
|
|
case ALIGN_TOP: {
|
|
|
|
yOff = 0.0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALIGN_BOTTOM: {
|
|
|
|
yOff = (getSize().y - textSize.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALIGN_CENTER: {
|
|
|
|
yOff = (getSize().y - textSize.y) / 2.0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2022-04-18 11:47:22 +00:00
|
|
|
else {
|
|
|
|
// If height is smaller than the font height, then always center vertically.
|
|
|
|
yOff = (getSize().y - textSize.y) / 2.0f;
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-08-21 14:51:21 +00:00
|
|
|
// Draw the overall textbox area. If we're inside a scrollable container then this
|
|
|
|
// area is rendered inside that component instead of here.
|
|
|
|
if (Settings::getInstance()->getBool("DebugText")) {
|
|
|
|
if (!mParent || !mParent->isScrollable())
|
|
|
|
mRenderer->drawRect(0.0f, 0.0f, mSize.x, mSize.y, 0x0000FF33, 0x0000FF33);
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-04-18 11:47:22 +00:00
|
|
|
trans = glm::translate(trans, glm::vec3 {0.0f, std::round(yOff), 0.0f});
|
2022-04-27 16:40:28 +00:00
|
|
|
|
|
|
|
// Don't round vertices if scaled as it may lead to single-pixel alignment issues.
|
|
|
|
if (mScale == 1.0f)
|
|
|
|
mRenderer->setMatrix(trans, true);
|
|
|
|
else
|
|
|
|
mRenderer->setMatrix(trans, false);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
// Draw the text area, where the text actually is located.
|
|
|
|
if (Settings::getInstance()->getBool("DebugText")) {
|
|
|
|
switch (mHorizontalAlignment) {
|
2021-07-07 18:31:46 +00:00
|
|
|
case ALIGN_LEFT: {
|
2022-03-14 18:51:48 +00:00
|
|
|
mRenderer->drawRect(0.0f, 0.0f, mTextCache->metrics.size.x,
|
|
|
|
mTextCache->metrics.size.y, 0x00000033, 0x00000033);
|
2021-07-07 18:31:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALIGN_CENTER: {
|
2022-03-14 18:51:48 +00:00
|
|
|
mRenderer->drawRect((mSize.x - mTextCache->metrics.size.x) / 2.0f, 0.0f,
|
|
|
|
mTextCache->metrics.size.x, mTextCache->metrics.size.y,
|
|
|
|
0x00000033, 0x00000033);
|
2021-07-07 18:31:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ALIGN_RIGHT: {
|
2022-03-14 18:51:48 +00:00
|
|
|
mRenderer->drawRect(mSize.x - mTextCache->metrics.size.x, 0.0f,
|
|
|
|
mTextCache->metrics.size.x, mTextCache->metrics.size.y,
|
|
|
|
0x00000033, 0x00000033);
|
2021-07-07 18:31:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mFont->renderTextCache(mTextCache.get());
|
|
|
|
}
|
2013-06-14 15:48:13 +00:00
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
void TextComponent::calculateExtent()
|
|
|
|
{
|
2021-08-17 16:41:45 +00:00
|
|
|
if (mAutoCalcExtent.x) {
|
2022-02-09 19:44:22 +00:00
|
|
|
if (mUppercase)
|
|
|
|
mSize = mFont->sizeText(Utils::String::toUpper(mText), mLineSpacing);
|
|
|
|
else if (mLowercase)
|
|
|
|
mSize = mFont->sizeText(Utils::String::toLower(mText), mLineSpacing);
|
2022-02-09 21:06:34 +00:00
|
|
|
else if (mCapitalize)
|
2022-02-09 19:44:22 +00:00
|
|
|
mSize = mFont->sizeText(Utils::String::toCapitalized(mText), mLineSpacing);
|
|
|
|
else
|
|
|
|
mSize = mFont->sizeText(mText, mLineSpacing); // Original case.
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-02-09 19:44:22 +00:00
|
|
|
if (mAutoCalcExtent.y) {
|
|
|
|
if (mUppercase) {
|
|
|
|
mSize.y =
|
|
|
|
mFont->sizeWrappedText(Utils::String::toUpper(mText), getSize().x, mLineSpacing)
|
|
|
|
.y;
|
|
|
|
}
|
|
|
|
else if (mLowercase) {
|
|
|
|
mSize.y =
|
|
|
|
mFont->sizeWrappedText(Utils::String::toLower(mText), getSize().x, mLineSpacing)
|
|
|
|
.y;
|
|
|
|
}
|
2022-02-09 21:06:34 +00:00
|
|
|
else if (mCapitalize) {
|
2022-02-09 19:44:22 +00:00
|
|
|
mSize.y = mFont
|
|
|
|
->sizeWrappedText(Utils::String::toCapitalized(mText), getSize().x,
|
|
|
|
mLineSpacing)
|
|
|
|
.y;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mSize.y = mFont->sizeWrappedText(mText, getSize().x, mLineSpacing).y;
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2013-07-02 05:57:31 +00:00
|
|
|
}
|
2013-08-14 12:16:49 +00:00
|
|
|
|
2013-08-22 01:08:36 +00:00
|
|
|
void TextComponent::onTextChanged()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
calculateExtent();
|
|
|
|
|
2022-08-21 14:51:21 +00:00
|
|
|
if (!mFont || mText.empty() || mSize.x == 0.0f || mSize.y == 0.0f) {
|
2020-06-28 16:39:18 +00:00
|
|
|
mTextCache.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:44:22 +00:00
|
|
|
std::string text;
|
|
|
|
|
|
|
|
if (mUppercase)
|
|
|
|
text = Utils::String::toUpper(mText);
|
|
|
|
else if (mLowercase)
|
|
|
|
text = Utils::String::toLower(mText);
|
2022-02-09 21:06:34 +00:00
|
|
|
else if (mCapitalize)
|
2022-02-09 19:44:22 +00:00
|
|
|
text = Utils::String::toCapitalized(mText);
|
|
|
|
else
|
|
|
|
text = mText; // Original case.
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-08-21 14:51:21 +00:00
|
|
|
std::shared_ptr<Font> f {mFont};
|
|
|
|
const float lineHeight {f->getHeight(mLineSpacing)};
|
|
|
|
const bool isMultiline {mSize.y > lineHeight};
|
|
|
|
const bool isScrollable {mParent && mParent->isScrollable()};
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-08-21 14:51:21 +00:00
|
|
|
bool addAbbrev {false};
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!isMultiline) {
|
2022-08-21 14:51:21 +00:00
|
|
|
size_t newline {text.find('\n')};
|
2020-06-28 16:39:18 +00:00
|
|
|
// Single line of text - stop at the first newline since it'll mess everything up.
|
|
|
|
text = text.substr(0, newline);
|
|
|
|
addAbbrev = newline != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2022-01-16 11:09:55 +00:00
|
|
|
glm::vec2 size {f->sizeText(text)};
|
2022-08-21 14:51:21 +00:00
|
|
|
if (!isMultiline && text.size() && (size.x > mSize.x || addAbbrev)) {
|
2020-06-28 16:39:18 +00:00
|
|
|
// Abbreviate text.
|
2022-08-21 14:51:21 +00:00
|
|
|
const std::string abbrev {"..."};
|
|
|
|
float abbrevSize {f->sizeText(abbrev).x};
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-08-21 14:51:21 +00:00
|
|
|
while (text.size() && size.x + abbrevSize > mSize.x) {
|
|
|
|
size_t newSize {Utils::String::prevCursor(text, text.size())};
|
2020-06-28 16:39:18 +00:00
|
|
|
text.erase(newSize, text.size() - newSize);
|
2021-10-15 18:58:40 +00:00
|
|
|
if (!text.empty() && text.back() == ' ')
|
|
|
|
text.pop_back();
|
2020-06-28 16:39:18 +00:00
|
|
|
size = f->sizeText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
text.append(abbrev);
|
2022-02-11 21:10:25 +00:00
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(
|
2022-02-12 16:38:55 +00:00
|
|
|
text, glm::vec2 {}, mColor, mSize.x, mHorizontalAlignment, mLineSpacing, mNoTopMargin));
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2022-08-21 14:51:21 +00:00
|
|
|
else if (isMultiline && text.size() && !isScrollable) {
|
|
|
|
const std::string wrappedText {
|
|
|
|
f->wrapText(text, mSize.x, mSize.y - lineHeight, mLineSpacing)};
|
|
|
|
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(wrappedText, glm::vec2 {}, mColor,
|
|
|
|
mSize.x, mHorizontalAlignment,
|
|
|
|
mLineSpacing, mNoTopMargin));
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
else {
|
2022-02-11 21:10:25 +00:00
|
|
|
mTextCache = std::shared_ptr<TextCache>(
|
2022-02-12 16:38:55 +00:00
|
|
|
f->buildTextCache(f->wrapText(text, mSize.x), glm::vec2 {}, mColor, mSize.x,
|
|
|
|
mHorizontalAlignment, mLineSpacing, mNoTopMargin));
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2022-01-31 21:37:10 +00:00
|
|
|
|
2022-02-12 16:38:55 +00:00
|
|
|
if (mOpacity != 1.0f || mThemeOpacity != 1.0f)
|
|
|
|
setOpacity(mOpacity);
|
|
|
|
|
2022-01-31 21:37:10 +00:00
|
|
|
// This is required to set the color transparency.
|
|
|
|
onColorChanged();
|
2013-08-22 01:08:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 21:14:33 +00:00
|
|
|
void TextComponent::onColorChanged()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (mTextCache)
|
|
|
|
mTextCache->setColor(mColor);
|
2013-10-10 21:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 02:34:34 +00:00
|
|
|
void TextComponent::setHorizontalAlignment(Alignment align)
|
2014-05-14 22:01:40 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mHorizontalAlignment = align;
|
|
|
|
onTextChanged();
|
2014-05-14 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextComponent::setLineSpacing(float spacing)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mLineSpacing = spacing;
|
|
|
|
onTextChanged();
|
2014-05-14 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 15:52:15 +00:00
|
|
|
void TextComponent::setNoTopMargin(bool margin)
|
|
|
|
{
|
|
|
|
mNoTopMargin = margin;
|
|
|
|
onTextChanged();
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:29:23 +00:00
|
|
|
std::vector<HelpPrompt> TextComponent::getHelpPrompts()
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
if (mSelectable)
|
|
|
|
prompts.push_back(HelpPrompt("a", "select"));
|
|
|
|
return prompts;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
|
|
|
|
const std::string& view,
|
|
|
|
const std::string& element,
|
|
|
|
unsigned int properties)
|
2014-01-01 05:39:22 +00:00
|
|
|
{
|
2022-01-29 17:41:22 +00:00
|
|
|
using namespace ThemeFlags;
|
2020-06-28 16:39:18 +00:00
|
|
|
GuiComponent::applyTheme(theme, view, element, properties);
|
|
|
|
|
2022-01-29 17:41:22 +00:00
|
|
|
std::string elementType {"text"};
|
2022-02-10 23:19:08 +00:00
|
|
|
std::string componentName {"TextComponent"};
|
2022-01-29 17:41:22 +00:00
|
|
|
|
2022-02-10 23:19:08 +00:00
|
|
|
if (element.substr(0, 13) == "gamelistinfo_") {
|
2022-01-29 17:41:22 +00:00
|
|
|
elementType = "gamelistinfo";
|
2022-02-10 23:19:08 +00:00
|
|
|
componentName = "gamelistInfoComponent";
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-01-29 17:41:22 +00:00
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, elementType);
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!elem)
|
|
|
|
return;
|
|
|
|
|
2022-08-18 20:51:21 +00:00
|
|
|
if (elem->has("metadataElement") && elem->get<bool>("metadataElement"))
|
|
|
|
mComponentThemeFlags |= ComponentThemeFlags::METADATA_ELEMENT;
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (properties & COLOR && elem->has("color"))
|
|
|
|
setColor(elem->get<unsigned int>("color"));
|
|
|
|
|
|
|
|
setRenderBackground(false);
|
|
|
|
if (properties & COLOR && elem->has("backgroundColor")) {
|
|
|
|
setBackgroundColor(elem->get<unsigned int>("backgroundColor"));
|
|
|
|
setRenderBackground(true);
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:19:08 +00:00
|
|
|
if (properties & ALIGNMENT && elem->has("horizontalAlignment")) {
|
|
|
|
std::string str {elem->get<std::string>("horizontalAlignment")};
|
|
|
|
if (str == "left")
|
|
|
|
setHorizontalAlignment(ALIGN_LEFT);
|
|
|
|
else if (str == "center")
|
|
|
|
setHorizontalAlignment(ALIGN_CENTER);
|
|
|
|
else if (str == "right")
|
|
|
|
setHorizontalAlignment(ALIGN_RIGHT);
|
|
|
|
else
|
|
|
|
LOG(LogWarning) << componentName
|
|
|
|
<< ": Invalid theme configuration, property "
|
2022-03-18 19:31:04 +00:00
|
|
|
"<horizontalAlignment> defined as \""
|
2022-02-10 23:19:08 +00:00
|
|
|
<< str << "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties & ALIGNMENT && elem->has("verticalAlignment")) {
|
|
|
|
std::string str {elem->get<std::string>("verticalAlignment")};
|
|
|
|
if (str == "top")
|
|
|
|
setVerticalAlignment(ALIGN_TOP);
|
|
|
|
else if (str == "center")
|
|
|
|
setVerticalAlignment(ALIGN_CENTER);
|
|
|
|
else if (str == "bottom")
|
|
|
|
setVerticalAlignment(ALIGN_BOTTOM);
|
|
|
|
else
|
|
|
|
LOG(LogWarning) << componentName
|
|
|
|
<< ": Invalid theme configuration, property "
|
2022-03-18 19:31:04 +00:00
|
|
|
"<verticalAlignment> defined as \""
|
2022-02-10 23:19:08 +00:00
|
|
|
<< str << "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy themes only.
|
2020-06-28 16:39:18 +00:00
|
|
|
if (properties & ALIGNMENT && elem->has("alignment")) {
|
2022-02-10 23:19:08 +00:00
|
|
|
std::string str {elem->get<std::string>("alignment")};
|
2020-06-28 16:39:18 +00:00
|
|
|
if (str == "left")
|
|
|
|
setHorizontalAlignment(ALIGN_LEFT);
|
|
|
|
else if (str == "center")
|
|
|
|
setHorizontalAlignment(ALIGN_CENTER);
|
|
|
|
else if (str == "right")
|
|
|
|
setHorizontalAlignment(ALIGN_RIGHT);
|
|
|
|
else
|
2022-02-10 23:19:08 +00:00
|
|
|
LOG(LogWarning) << componentName
|
|
|
|
<< ": Invalid theme configuration, property "
|
2022-03-18 19:31:04 +00:00
|
|
|
"<alignment> defined as \""
|
2022-02-10 23:19:08 +00:00
|
|
|
<< str << "\"";
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (properties & TEXT && elem->has("text"))
|
|
|
|
setText(elem->get<std::string>("text"));
|
|
|
|
|
2022-08-18 21:44:22 +00:00
|
|
|
if (properties & METADATA && elem->has("systemdata")) {
|
|
|
|
mThemeSystemdata = "";
|
|
|
|
const std::string systemdata {elem->get<std::string>("systemdata")};
|
2022-08-19 15:11:16 +00:00
|
|
|
for (auto& type : supportedSystemdataTypes) {
|
2022-08-18 21:44:22 +00:00
|
|
|
if (type == systemdata) {
|
|
|
|
mThemeSystemdata = type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mThemeSystemdata == "") {
|
2022-08-19 15:11:16 +00:00
|
|
|
LOG(LogError)
|
2022-08-18 21:44:22 +00:00
|
|
|
<< "TextComponent: Invalid theme configuration, property <systemdata> defined as \""
|
|
|
|
<< systemdata << "\"";
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 10:45:06 +00:00
|
|
|
|
2022-08-18 21:44:22 +00:00
|
|
|
if (properties & METADATA && elem->has("metadata")) {
|
|
|
|
mThemeMetadata = "";
|
|
|
|
const std::string metadata {elem->get<std::string>("metadata")};
|
2022-08-19 15:11:16 +00:00
|
|
|
|
|
|
|
for (auto& type : supportedMetadataTypes) {
|
2022-08-18 21:44:22 +00:00
|
|
|
if (type == metadata) {
|
|
|
|
mThemeMetadata = type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mThemeMetadata == "") {
|
2022-08-19 15:11:16 +00:00
|
|
|
LOG(LogError)
|
2022-08-18 21:44:22 +00:00
|
|
|
<< "TextComponent: Invalid theme configuration, property <metadata> defined as \""
|
|
|
|
<< metadata << "\"";
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 20:24:19 +00:00
|
|
|
|
2022-02-09 21:06:34 +00:00
|
|
|
if (properties & LETTER_CASE && elem->has("letterCase")) {
|
|
|
|
std::string letterCase {elem->get<std::string>("letterCase")};
|
|
|
|
if (letterCase == "uppercase") {
|
|
|
|
setUppercase(true);
|
|
|
|
}
|
|
|
|
else if (letterCase == "lowercase") {
|
|
|
|
setLowercase(true);
|
|
|
|
}
|
|
|
|
else if (letterCase == "capitalize") {
|
|
|
|
setCapitalize(true);
|
|
|
|
}
|
|
|
|
else if (letterCase != "none") {
|
|
|
|
LOG(LogWarning)
|
2022-03-18 19:31:04 +00:00
|
|
|
<< "TextComponent: Invalid theme configuration, property <letterCase> defined as \""
|
2022-02-09 21:06:34 +00:00
|
|
|
<< letterCase << "\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy themes only.
|
2020-06-28 16:39:18 +00:00
|
|
|
if (properties & FORCE_UPPERCASE && elem->has("forceUppercase"))
|
|
|
|
setUppercase(elem->get<bool>("forceUppercase"));
|
|
|
|
|
|
|
|
if (properties & LINE_SPACING && elem->has("lineSpacing"))
|
2022-02-13 10:45:06 +00:00
|
|
|
setLineSpacing(glm::clamp(elem->get<float>("lineSpacing"), 0.5f, 3.0f));
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
setFont(Font::getFromTheme(elem, properties, mFont));
|
2014-01-01 05:39:22 +00:00
|
|
|
}
|