Added support to TextComponent for lowercase and capitalized text conversions.

Also changed camelCase to capitalized and textStyle to letterCase in various places.
This commit is contained in:
Leon Styhre 2022-02-09 20:44:22 +01:00
parent 9856a3da1b
commit 6db671de3d
9 changed files with 102 additions and 31 deletions

View file

@ -24,7 +24,7 @@ HelpStyle::HelpStyle()
iconColorDimmed = 0x777777FF;
entrySpacing = 16.0f;
iconTextSpacing = 8.0f;
textStyle = "uppercase";
letterCase = "uppercase";
if (FONT_SIZE_SMALL != 0)
font = Font::get(FONT_SIZE_SMALL);
@ -71,8 +71,8 @@ void HelpStyle::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::s
if (elem->has("iconTextSpacing"))
iconTextSpacing = elem->get<float>("iconTextSpacing");
if (elem->has("textStyle"))
textStyle = elem->get<std::string>("textStyle");
if (elem->has("letterCase"))
letterCase = elem->get<std::string>("letterCase");
// Load custom button icons.
// The names may look a bit strange when combined with the PREFIX string "button_" but it's

View file

@ -28,7 +28,7 @@ struct HelpStyle {
std::shared_ptr<Font> font;
float entrySpacing;
float iconTextSpacing;
std::string textStyle;
std::string letterCase;
struct CustomButtonIcons {

View file

@ -150,7 +150,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"alignment", STRING},
{"color", COLOR},
{"backgroundColor", COLOR},
{"forceUppercase", BOOLEAN},
{"letterCase", STRING},
{"forceUppercase", BOOLEAN}, // For backward compatibility with legacy themes.
{"lineSpacing", FLOAT},
{"visible", BOOLEAN},
{"zIndex", FLOAT}}},
@ -166,7 +167,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"alignment", STRING},
{"color", COLOR},
{"backgroundColor", COLOR},
{"forceUppercase", BOOLEAN},
{"letterCase", STRING},
{"forceUppercase", BOOLEAN}, // For backward compatibility with legacy themes.
{"lineSpacing", FLOAT},
{"format", STRING},
{"displayRelative", BOOLEAN},
@ -214,7 +216,7 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"text", STRING},
{"textColor", COLOR},
{"textBackgroundColor", COLOR},
{"textStyle", STRING},
{"letterCase", STRING},
{"fontPath", PATH},
{"fontSize", FLOAT},
{"lineSpacing", FLOAT},
@ -240,7 +242,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"scrollSound", PATH}, // For backward compatibility with legacy themes.
{"alignment", STRING},
{"horizontalMargin", FLOAT},
{"forceUppercase", BOOLEAN},
{"letterCase", STRING},
{"forceUppercase", BOOLEAN}, // For backward compatibility with legacy themes.
{"lineSpacing", FLOAT},
{"zIndex", FLOAT}}},
{"helpsystem",
@ -254,7 +257,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"fontSize", FLOAT},
{"entrySpacing", FLOAT},
{"iconTextSpacing", FLOAT},
{"textStyle", STRING},
{"letterCase", STRING},
{"textStyle", STRING}, // For backward compatibility with legacy themes.
{"customButtonIcon", PATH}}},
{"sound",
{{"path", PATH}}},

View file

@ -395,18 +395,18 @@ void CarouselComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
if (elem->has("lineSpacing"))
mLineSpacing = glm::clamp(elem->get<float>("lineSpacing"), 0.5f, 3.0f);
std::string textStyle;
std::string letterCase;
if (elem->has("textStyle"))
textStyle = elem->get<std::string>("textStyle");
if (elem->has("letterCase"))
letterCase = elem->get<std::string>("letterCase");
if (elem->has("text")) {
if (textStyle == "uppercase")
if (letterCase == "uppercase")
mText = Utils::String::toUpper(elem->get<std::string>("text"));
else if (textStyle == "lowercase")
else if (letterCase == "lowercase")
mText = Utils::String::toLower(elem->get<std::string>("text"));
else if (textStyle == "camelcase")
mText = Utils::String::toCamelCase(elem->get<std::string>("text"));
else if (letterCase == "capitalize")
mText = Utils::String::toCapitalized(elem->get<std::string>("text"));
else
mText = elem->get<std::string>("text");
}

View file

@ -224,10 +224,10 @@ void HelpComponent::updateGrid()
// Apply text style and color from the theme to the label and add it to the label list.
std::string lblInput = it->second;
if (mStyle.textStyle == "lowercase")
if (mStyle.letterCase == "lowercase")
lblInput = Utils::String::toLower(lblInput);
else if (mStyle.textStyle == "camelcase")
lblInput = Utils::String::toCamelCase(lblInput);
else if (mStyle.letterCase == "capitalize")
lblInput = Utils::String::toCapitalized(lblInput);
else
lblInput = Utils::String::toUpper(lblInput);
auto lbl = std::make_shared<TextComponent>(

View file

@ -20,6 +20,8 @@ TextComponent::TextComponent()
, mBgColorOpacity {0x00000000}
, mRenderBackground {false}
, mUppercase {false}
, mLowercase {false}
, mCapitalized {false}
, mAutoCalcExtent {1, 1}
, mHorizontalAlignment {ALIGN_LEFT}
, mVerticalAlignment {ALIGN_CENTER}
@ -43,6 +45,8 @@ TextComponent::TextComponent(const std::string& text,
, mBgColorOpacity {0x00000000}
, mRenderBackground {false}
, mUppercase {false}
, mLowercase {false}
, mCapitalized {false}
, mAutoCalcExtent {1, 1}
, mHorizontalAlignment {align}
, mVerticalAlignment {ALIGN_CENTER}
@ -120,6 +124,30 @@ void TextComponent::setText(const std::string& text, bool update)
void TextComponent::setUppercase(bool uppercase)
{
mUppercase = uppercase;
if (uppercase) {
mLowercase = false;
mCapitalized = false;
}
onTextChanged();
}
void TextComponent::setLowercase(bool lowercase)
{
mLowercase = lowercase;
if (lowercase) {
mUppercase = false;
mCapitalized = false;
}
onTextChanged();
}
void TextComponent::setCapitalized(bool capitalized)
{
mCapitalized = capitalized;
if (capitalized) {
mUppercase = false;
mLowercase = false;
}
onTextChanged();
}
@ -198,14 +226,37 @@ void TextComponent::render(const glm::mat4& parentTrans)
void TextComponent::calculateExtent()
{
if (mAutoCalcExtent.x) {
mSize = mFont->sizeText(mUppercase ? Utils::String::toUpper(mText) : mText, mLineSpacing);
if (mUppercase)
mSize = mFont->sizeText(Utils::String::toUpper(mText), mLineSpacing);
else if (mLowercase)
mSize = mFont->sizeText(Utils::String::toLower(mText), mLineSpacing);
else if (mCapitalized)
mSize = mFont->sizeText(Utils::String::toCapitalized(mText), mLineSpacing);
else
mSize = mFont->sizeText(mText, mLineSpacing); // Original case.
}
else {
if (mAutoCalcExtent.y)
mSize.y = mFont
->sizeWrappedText(mUppercase ? Utils::String::toUpper(mText) : mText,
getSize().x, mLineSpacing)
.y;
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;
}
else if (mCapitalized) {
mSize.y = mFont
->sizeWrappedText(Utils::String::toCapitalized(mText), getSize().x,
mLineSpacing)
.y;
}
else {
mSize.y = mFont->sizeWrappedText(mText, getSize().x, mLineSpacing).y;
}
}
}
}
@ -218,7 +269,16 @@ void TextComponent::onTextChanged()
return;
}
std::string text = mUppercase ? Utils::String::toUpper(mText) : mText;
std::string text;
if (mUppercase)
text = Utils::String::toUpper(mText);
else if (mLowercase)
text = Utils::String::toLower(mText);
else if (mCapitalized)
text = Utils::String::toCapitalized(mText);
else
text = mText; // Original case.
std::shared_ptr<Font> f = mFont;
const bool isMultiline = (mSize.y == 0.0f || mSize.y > f->getHeight() * 1.2f);

View file

@ -35,6 +35,8 @@ public:
void setFont(const std::shared_ptr<Font>& font);
void setUppercase(bool uppercase);
void setLowercase(bool lowercase);
void setCapitalized(bool capitalize);
void onSizeChanged() override;
void setText(const std::string& text, bool update = true);
void setHiddenText(const std::string& text) { mHiddenText = text; }
@ -90,6 +92,8 @@ private:
bool mRenderBackground;
bool mUppercase;
bool mLowercase;
bool mCapitalized;
glm::ivec2 mAutoCalcExtent;
std::shared_ptr<TextCache> mTextCache;
Alignment mHorizontalAlignment;

View file

@ -538,10 +538,13 @@ namespace Utils
return stringUpper;
}
std::string toCamelCase(const std::string& stringArg)
std::string toCapitalized(const std::string& stringArg)
{
std::string line = stringArg;
bool active = true;
if (stringArg == "")
return stringArg;
std::string line {stringArg};
bool active {true};
for (int i = 0; line[i] != '\0'; ++i) {
if (std::isalpha(line[i])) {
@ -552,7 +555,7 @@ namespace Utils
else
line[i] = Utils::String::toLower(std::string(1, line[i]))[0];
}
else if (line[i] == ' ')
else if (line[i] == ' ' || line[i] == '\n' || line[i] == '\r' || line[i] == '\t')
active = true;
}

View file

@ -27,7 +27,7 @@ namespace Utils
size_t moveCursor(const std::string& stringArg, const size_t cursor, const int amount);
std::string toLower(const std::string& stringArg);
std::string toUpper(const std::string& stringArg);
std::string toCamelCase(const std::string& stringArg);
std::string toCapitalized(const std::string& stringArg);
std::string trim(const std::string& stringArg);
std::string replace(const std::string& stringArg,
const std::string& replace,