mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-02-16 20:15:38 +00:00
Added support for a new type of 'flat style' buttons to ButtonComponent.
Also did some general code cleanup.
This commit is contained in:
parent
03645e2de3
commit
db4fb1ab92
|
@ -15,24 +15,85 @@
|
||||||
ButtonComponent::ButtonComponent(Window* window,
|
ButtonComponent::ButtonComponent(Window* window,
|
||||||
const std::string& text,
|
const std::string& text,
|
||||||
const std::string& helpText,
|
const std::string& helpText,
|
||||||
const std::function<void()>& func)
|
const std::function<void()>& func,
|
||||||
: GuiComponent(window)
|
bool upperCase,
|
||||||
, mBox(window, ":/graphics/button.svg")
|
bool flatStyle)
|
||||||
, mFont(Font::get(FONT_SIZE_MEDIUM))
|
: GuiComponent{window}
|
||||||
, mFocused(false)
|
, mBox{window, ":/graphics/button.svg"}
|
||||||
, mEnabled(true)
|
, mFont{Font::get(FONT_SIZE_MEDIUM)}
|
||||||
, mTextColorFocused(0xFFFFFFFF)
|
, mPadding{{}}
|
||||||
, mTextColorUnfocused(0x777777FF)
|
, mFocused{false}
|
||||||
|
, mEnabled{true}
|
||||||
|
, mFlatStyle{flatStyle}
|
||||||
|
, mTextColorFocused{0xFFFFFFFF}
|
||||||
|
, mTextColorUnfocused{0x777777FF}
|
||||||
|
, mFlatColorFocused{0x878787FF}
|
||||||
|
, mFlatColorUnfocused{0x60606025}
|
||||||
|
|
||||||
{
|
{
|
||||||
setPressedFunc(func);
|
setPressedFunc(func);
|
||||||
setText(text, helpText);
|
setText(text, helpText, upperCase);
|
||||||
|
|
||||||
|
if (!mFlatStyle)
|
||||||
updateImage();
|
updateImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ButtonComponent::onSizeChanged()
|
void ButtonComponent::onSizeChanged()
|
||||||
{
|
{
|
||||||
// Fit to mBox.
|
if (mFlatStyle)
|
||||||
mBox.fitTo(mSize, glm::vec3{}, glm::vec2{-32.0f, -32.0f});
|
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<TextCache>(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)
|
bool ButtonComponent::input(InputConfig* config, Input input)
|
||||||
|
@ -46,58 +107,27 @@ bool ButtonComponent::input(InputConfig* config, Input input)
|
||||||
return GuiComponent::input(config, 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<TextCache>(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)
|
void ButtonComponent::render(const glm::mat4& parentTrans)
|
||||||
{
|
{
|
||||||
glm::mat4 trans{parentTrans * getTransform()};
|
glm::mat4 trans{parentTrans * getTransform()};
|
||||||
|
|
||||||
|
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);
|
mBox.render(trans);
|
||||||
|
}
|
||||||
|
|
||||||
if (mTextCache) {
|
if (mTextCache) {
|
||||||
glm::vec3 centerOffset{(mSize.x - mTextCache->metrics.size.x) / 2.0f,
|
glm::vec3 centerOffset{(mSize.x - mTextCache->metrics.size.x) / 2.0f,
|
||||||
|
@ -121,6 +151,13 @@ void ButtonComponent::render(const glm::mat4& parentTrans)
|
||||||
renderChildren(trans);
|
renderChildren(trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<HelpPrompt> ButtonComponent::getHelpPrompts()
|
||||||
|
{
|
||||||
|
std::vector<HelpPrompt> prompts;
|
||||||
|
prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str()));
|
||||||
|
return prompts;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int ButtonComponent::getCurTextColor() const
|
unsigned int ButtonComponent::getCurTextColor() const
|
||||||
{
|
{
|
||||||
if (!mFocused)
|
if (!mFocused)
|
||||||
|
@ -129,9 +166,16 @@ unsigned int ButtonComponent::getCurTextColor() const
|
||||||
return mTextColorFocused;
|
return mTextColorFocused;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<HelpPrompt> ButtonComponent::getHelpPrompts()
|
void ButtonComponent::updateImage()
|
||||||
{
|
{
|
||||||
std::vector<HelpPrompt> prompts;
|
if (!mEnabled || !mPressedFunc) {
|
||||||
prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str()));
|
mBox.setImagePath(":/graphics/button_filled.svg");
|
||||||
return prompts;
|
mBox.setCenterColor(0x770000FF);
|
||||||
|
mBox.setEdgeColor(0x770000FF);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mBox.setCenterColor(0xFFFFFFFF);
|
||||||
|
mBox.setEdgeColor(0xFFFFFFFF);
|
||||||
|
mBox.setImagePath(mFocused ? ":/graphics/button_filled.svg" : ":/graphics/button.svg");
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,41 +20,56 @@ public:
|
||||||
ButtonComponent(Window* window,
|
ButtonComponent(Window* window,
|
||||||
const std::string& text = "",
|
const std::string& text = "",
|
||||||
const std::string& helpText = "",
|
const std::string& helpText = "",
|
||||||
const std::function<void()>& func = nullptr);
|
const std::function<void()>& func = nullptr,
|
||||||
|
bool upperCase = true,
|
||||||
void setPressedFunc(std::function<void()> f) { mPressedFunc = f; }
|
bool flatStyle = false);
|
||||||
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<void()>& getPressedFunc() const { return mPressedFunc; }
|
|
||||||
|
|
||||||
void onSizeChanged() override;
|
void onSizeChanged() override;
|
||||||
void onFocusGained() override;
|
void onFocusGained() override;
|
||||||
void onFocusLost() 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<void()> 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<void()>& getPressedFunc() const { return mPressedFunc; }
|
||||||
|
|
||||||
|
bool input(InputConfig* config, Input input) override;
|
||||||
|
void render(const glm::mat4& parentTrans) override;
|
||||||
|
|
||||||
virtual std::vector<HelpPrompt> getHelpPrompts() override;
|
virtual std::vector<HelpPrompt> getHelpPrompts() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Font> mFont;
|
|
||||||
std::function<void()> mPressedFunc;
|
|
||||||
|
|
||||||
bool mFocused;
|
|
||||||
bool mEnabled;
|
|
||||||
unsigned int mTextColorFocused;
|
|
||||||
unsigned int mTextColorUnfocused;
|
|
||||||
|
|
||||||
unsigned int getCurTextColor() const;
|
unsigned int getCurTextColor() const;
|
||||||
void updateImage();
|
void updateImage();
|
||||||
|
|
||||||
|
NinePatchComponent mBox;
|
||||||
|
|
||||||
|
std::shared_ptr<Font> mFont;
|
||||||
|
std::unique_ptr<TextCache> mTextCache;
|
||||||
|
std::function<void()> mPressedFunc;
|
||||||
|
|
||||||
|
glm::vec4 mPadding;
|
||||||
|
|
||||||
std::string mText;
|
std::string mText;
|
||||||
std::string mHelpText;
|
std::string mHelpText;
|
||||||
std::unique_ptr<TextCache> 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
|
#endif // ES_CORE_COMPONENTS_BUTTON_COMPONENT_H
|
||||||
|
|
Loading…
Reference in a new issue