From db4fb1ab92e5f06a411da002540c35dd7fee9d7e Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Fri, 17 Sep 2021 21:25:21 +0200 Subject: [PATCH] Added support for a new type of 'flat style' buttons to ButtonComponent. Also did some general code cleanup. --- es-core/src/components/ButtonComponent.cpp | 172 +++++++++++++-------- es-core/src/components/ButtonComponent.h | 59 ++++--- 2 files changed, 145 insertions(+), 86 deletions(-) diff --git a/es-core/src/components/ButtonComponent.cpp b/es-core/src/components/ButtonComponent.cpp index 6ba641495..3143f52a9 100644 --- a/es-core/src/components/ButtonComponent.cpp +++ b/es-core/src/components/ButtonComponent.cpp @@ -15,24 +15,85 @@ ButtonComponent::ButtonComponent(Window* window, const std::string& text, const std::string& helpText, - const std::function& func) - : GuiComponent(window) - , mBox(window, ":/graphics/button.svg") - , mFont(Font::get(FONT_SIZE_MEDIUM)) - , mFocused(false) - , mEnabled(true) - , mTextColorFocused(0xFFFFFFFF) - , mTextColorUnfocused(0x777777FF) + const std::function& func, + bool upperCase, + bool flatStyle) + : GuiComponent{window} + , mBox{window, ":/graphics/button.svg"} + , mFont{Font::get(FONT_SIZE_MEDIUM)} + , mPadding{{}} + , mFocused{false} + , mEnabled{true} + , mFlatStyle{flatStyle} + , mTextColorFocused{0xFFFFFFFF} + , mTextColorUnfocused{0x777777FF} + , mFlatColorFocused{0x878787FF} + , mFlatColorUnfocused{0x60606025} + { setPressedFunc(func); - setText(text, helpText); - updateImage(); + setText(text, helpText, upperCase); + + if (!mFlatStyle) + updateImage(); } void ButtonComponent::onSizeChanged() { - // Fit to mBox. - mBox.fitTo(mSize, glm::vec3{}, glm::vec2{-32.0f, -32.0f}); + if (mFlatStyle) + return; + + auto cornerSize = mBox.getCornerSize(); + + mBox.fitTo(glm::vec2{mSize.x - mPadding.x - mPadding.z, mSize.y - mPadding.y - mPadding.w}, + glm::vec3{mPadding.x, mPadding.y, 0.0f}, + glm::vec2{-cornerSize.x * 2.0f, -cornerSize.y * 2.0f}); +} + +void ButtonComponent::onFocusGained() +{ + mFocused = true; + if (!mFlatStyle) + updateImage(); +} + +void ButtonComponent::onFocusLost() +{ + mFocused = false; + if (!mFlatStyle) + updateImage(); +} + +void ButtonComponent::setText(const std::string& text, const std::string& helpText, bool upperCase) +{ + mText = upperCase ? Utils::String::toUpper(text) : text; + mHelpText = helpText; + + mTextCache = + std::unique_ptr(mFont->buildTextCache(mText, 0.0f, 0.0f, getCurTextColor())); + + float minWidth = mFont->sizeText("DELETE").x + (12.0f * Renderer::getScreenWidthModifier()); + setSize(std::max(mTextCache->metrics.size.x + (12.0f * Renderer::getScreenWidthModifier()), + minWidth), + mTextCache->metrics.size.y); + + updateHelpPrompts(); +} + +void ButtonComponent::setEnabled(bool state) +{ + mEnabled = state; + if (!mFlatStyle) + updateImage(); +} + +void ButtonComponent::setPadding(const glm::vec4 padding) +{ + if (mPadding == padding) + return; + + mPadding = padding; + onSizeChanged(); } bool ButtonComponent::input(InputConfig* config, Input input) @@ -46,58 +107,27 @@ bool ButtonComponent::input(InputConfig* config, Input input) return GuiComponent::input(config, input); } -void ButtonComponent::setText(const std::string& text, const std::string& helpText) -{ - mText = Utils::String::toUpper(text); - mHelpText = helpText; - - mTextCache = std::unique_ptr(mFont->buildTextCache(mText, 0, 0, getCurTextColor())); - - float minWidth = mFont->sizeText("DELETE").x + (12.0f * Renderer::getScreenWidthModifier()); - setSize(std::max(mTextCache->metrics.size.x + (12.0f * Renderer::getScreenWidthModifier()), - minWidth), - mTextCache->metrics.size.y); - - updateHelpPrompts(); -} - -void ButtonComponent::onFocusGained() -{ - mFocused = true; - updateImage(); -} - -void ButtonComponent::onFocusLost() -{ - mFocused = false; - updateImage(); -} - -void ButtonComponent::setEnabled(bool state) -{ - mEnabled = state; - updateImage(); -} - -void ButtonComponent::updateImage() -{ - if (!mEnabled || !mPressedFunc) { - mBox.setImagePath(":/graphics/button_filled.svg"); - mBox.setCenterColor(0x770000FF); - mBox.setEdgeColor(0x770000FF); - return; - } - - mBox.setCenterColor(0xFFFFFFFF); - mBox.setEdgeColor(0xFFFFFFFF); - mBox.setImagePath(mFocused ? ":/graphics/button_filled.svg" : ":/graphics/button.svg"); -} - void ButtonComponent::render(const glm::mat4& parentTrans) { glm::mat4 trans{parentTrans * getTransform()}; - mBox.render(trans); + if (mFlatStyle) { + if (mFocused) { + Renderer::setMatrix(trans); + Renderer::drawRect(mPadding.x, mPadding.y, mSize.x - mPadding.x - mPadding.z, + mSize.y - mPadding.y - mPadding.w, mFlatColorFocused, + mFlatColorFocused); + } + else { + Renderer::setMatrix(trans); + Renderer::drawRect(mPadding.x, mPadding.y, mSize.x - mPadding.x - mPadding.z, + mSize.y - mPadding.y - mPadding.w, mFlatColorUnfocused, + mFlatColorUnfocused); + } + } + else { + mBox.render(trans); + } if (mTextCache) { glm::vec3 centerOffset{(mSize.x - mTextCache->metrics.size.x) / 2.0f, @@ -121,6 +151,13 @@ void ButtonComponent::render(const glm::mat4& parentTrans) renderChildren(trans); } +std::vector ButtonComponent::getHelpPrompts() +{ + std::vector prompts; + prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str())); + return prompts; +} + unsigned int ButtonComponent::getCurTextColor() const { if (!mFocused) @@ -129,9 +166,16 @@ unsigned int ButtonComponent::getCurTextColor() const return mTextColorFocused; } -std::vector ButtonComponent::getHelpPrompts() +void ButtonComponent::updateImage() { - std::vector prompts; - prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str())); - return prompts; + if (!mEnabled || !mPressedFunc) { + mBox.setImagePath(":/graphics/button_filled.svg"); + mBox.setCenterColor(0x770000FF); + mBox.setEdgeColor(0x770000FF); + return; + } + + mBox.setCenterColor(0xFFFFFFFF); + mBox.setEdgeColor(0xFFFFFFFF); + mBox.setImagePath(mFocused ? ":/graphics/button_filled.svg" : ":/graphics/button.svg"); } diff --git a/es-core/src/components/ButtonComponent.h b/es-core/src/components/ButtonComponent.h index 75f3c4660..55ad497db 100644 --- a/es-core/src/components/ButtonComponent.h +++ b/es-core/src/components/ButtonComponent.h @@ -20,41 +20,56 @@ public: ButtonComponent(Window* window, const std::string& text = "", const std::string& helpText = "", - const std::function& func = nullptr); - - void setPressedFunc(std::function f) { mPressedFunc = f; } - void setEnabled(bool state) override; - - bool input(InputConfig* config, Input input) override; - void render(const glm::mat4& parentTrans) override; - - void setText(const std::string& text, const std::string& helpText); - - const std::string& getText() const { return mText; } - const std::function& getPressedFunc() const { return mPressedFunc; } + const std::function& func = nullptr, + bool upperCase = true, + bool flatStyle = false); void onSizeChanged() override; void onFocusGained() override; void onFocusLost() override; + void setText(const std::string& text, const std::string& helpText, bool upperCase = true); + const std::string& getText() const { return mText; } + + void setPressedFunc(std::function f) { mPressedFunc = f; } + void setEnabled(bool state) override; + + void setPadding(const glm::vec4 padding); + glm::vec4 getPadding() { return mPadding; } + + void setFlatColorFocused(unsigned int color) { mFlatColorFocused = color; } + void setFlatColorUnfocused(unsigned int color) { mFlatColorUnfocused = color; } + + const std::function& getPressedFunc() const { return mPressedFunc; } + + bool input(InputConfig* config, Input input) override; + void render(const glm::mat4& parentTrans) override; + virtual std::vector getHelpPrompts() override; private: - std::shared_ptr mFont; - std::function mPressedFunc; - - bool mFocused; - bool mEnabled; - unsigned int mTextColorFocused; - unsigned int mTextColorUnfocused; - unsigned int getCurTextColor() const; void updateImage(); + NinePatchComponent mBox; + + std::shared_ptr mFont; + std::unique_ptr mTextCache; + std::function mPressedFunc; + + glm::vec4 mPadding; + std::string mText; std::string mHelpText; - std::unique_ptr mTextCache; - NinePatchComponent mBox; + + bool mFocused; + bool mEnabled; + bool mFlatStyle; + + unsigned int mTextColorFocused; + unsigned int mTextColorUnfocused; + unsigned int mFlatColorFocused; + unsigned int mFlatColorUnfocused; }; #endif // ES_CORE_COMPONENTS_BUTTON_COMPONENT_H