Added themeable opacity support for the help system.

This commit is contained in:
Leon Styhre 2022-02-13 12:18:33 +01:00
parent d1f4453e3b
commit 027265da67
4 changed files with 16 additions and 8 deletions

View file

@ -25,6 +25,7 @@ HelpStyle::HelpStyle()
entrySpacing = 16.0f; entrySpacing = 16.0f;
iconTextSpacing = 8.0f; iconTextSpacing = 8.0f;
letterCase = "uppercase"; letterCase = "uppercase";
opacity = 1.0f;
if (FONT_SIZE_SMALL != 0) if (FONT_SIZE_SMALL != 0)
font = Font::get(FONT_SIZE_SMALL); font = Font::get(FONT_SIZE_SMALL);
@ -73,6 +74,9 @@ void HelpStyle::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::s
if (elem->has("letterCase")) if (elem->has("letterCase"))
letterCase = elem->get<std::string>("letterCase"); letterCase = elem->get<std::string>("letterCase");
if (elem->has("opacity"))
opacity = glm::clamp(elem->get<float>("opacity"), 0.2f, 1.0f);
// Load custom button icons. // Load custom button icons.
// The names may look a bit strange when combined with the PREFIX string "button_" but it's // The names may look a bit strange when combined with the PREFIX string "button_" but it's
// because ThemeData adds this prefix to avoid name collisions when using XML attributes. // because ThemeData adds this prefix to avoid name collisions when using XML attributes.

View file

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

View file

@ -292,6 +292,7 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"iconTextSpacing", FLOAT}, {"iconTextSpacing", FLOAT},
{"letterCase", STRING}, {"letterCase", STRING},
{"textStyle", STRING}, // For backward compatibility with legacy themes. {"textStyle", STRING}, // For backward compatibility with legacy themes.
{"opacity", FLOAT},
{"customButtonIcon", PATH}}}, {"customButtonIcon", PATH}}},
{"sound", {"sound",
{{"path", PATH}}}, {{"path", PATH}}},

View file

@ -209,21 +209,22 @@ void HelpComponent::updateGrid()
std::vector<std::shared_ptr<ImageComponent>> icons; std::vector<std::shared_ptr<ImageComponent>> icons;
std::vector<std::shared_ptr<TextComponent>> labels; std::vector<std::shared_ptr<TextComponent>> labels;
float width = 0; float width {0.0f};
const float height = std::round(font->getLetterHeight() * 1.25f); const float height {std::round(font->getLetterHeight() * 1.25f)};
// State variable indicating whether gui is dimmed. // State variable indicating whether gui is dimmed.
bool isDimmed = mWindow->isBackgroundDimmed(); bool isDimmed {mWindow->isBackgroundDimmed()};
for (auto it = mPrompts.cbegin(); it != mPrompts.cend(); ++it) { for (auto it = mPrompts.cbegin(); it != mPrompts.cend(); ++it) {
auto icon = std::make_shared<ImageComponent>(); auto icon = std::make_shared<ImageComponent>();
icon->setImage(getIconTexture(it->first.c_str()), false); icon->setImage(getIconTexture(it->first.c_str()), false);
icon->setColorShift(isDimmed ? mStyle.iconColorDimmed : mStyle.iconColor); icon->setColorShift(isDimmed ? mStyle.iconColorDimmed : mStyle.iconColor);
icon->setResize(0, height); icon->setResize(0, height);
icon->setOpacity(mStyle.opacity);
icons.push_back(icon); icons.push_back(icon);
// Apply text style and color from the theme to the label and add it to the label list. // Apply text style and color from the theme to the label and add it to the label list.
std::string lblInput = it->second; std::string lblInput {it->second};
if (mStyle.letterCase == "lowercase") if (mStyle.letterCase == "lowercase")
lblInput = Utils::String::toLower(lblInput); lblInput = Utils::String::toLower(lblInput);
else if (mStyle.letterCase == "capitalize") else if (mStyle.letterCase == "capitalize")
@ -232,6 +233,7 @@ void HelpComponent::updateGrid()
lblInput = Utils::String::toUpper(lblInput); lblInput = Utils::String::toUpper(lblInput);
auto lbl = std::make_shared<TextComponent>( auto lbl = std::make_shared<TextComponent>(
lblInput, font, isDimmed ? mStyle.textColorDimmed : mStyle.textColor); lblInput, font, isDimmed ? mStyle.textColorDimmed : mStyle.textColor);
lbl->setOpacity(mStyle.opacity);
labels.push_back(lbl); labels.push_back(lbl);
width += width +=
@ -274,18 +276,18 @@ std::shared_ptr<TextureResource> HelpComponent::getIconTexture(const char* name)
return nullptr; return nullptr;
} }
std::shared_ptr<TextureResource> tex = std::shared_ptr<TextureResource> tex {
TextureResource::get(pathLookup->second, false, false, false); TextureResource::get(pathLookup->second, false, false, false)};
mIconCache[std::string(name)] = tex; mIconCache[std::string(name)] = tex;
return tex; return tex;
} }
void HelpComponent::setOpacity(float opacity) void HelpComponent::setOpacity(float opacity)
{ {
GuiComponent::setOpacity(opacity); GuiComponent::setOpacity(opacity * mStyle.opacity);
for (unsigned int i = 0; i < mGrid->getChildCount(); ++i) for (unsigned int i = 0; i < mGrid->getChildCount(); ++i)
mGrid->getChild(i)->setOpacity(opacity); mGrid->getChild(i)->setOpacity(opacity * mStyle.opacity);
} }
void HelpComponent::render(const glm::mat4& parentTrans) void HelpComponent::render(const glm::mat4& parentTrans)