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
|
|
|
// ButtonComponent.cpp
|
|
|
|
//
|
|
|
|
// Basic on/off button.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ButtonComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "Settings.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "resources/Font.h"
|
2018-01-27 17:04:28 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
ButtonComponent::ButtonComponent(Window* window,
|
|
|
|
const std::string& text,
|
|
|
|
const std::string& helpText,
|
2021-09-17 19:25:21 +00:00
|
|
|
const std::function<void()>& 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}
|
|
|
|
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
setPressedFunc(func);
|
2021-09-17 19:25:21 +00:00
|
|
|
setText(text, helpText, upperCase);
|
|
|
|
|
|
|
|
if (!mFlatStyle)
|
|
|
|
updateImage();
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
void ButtonComponent::onSizeChanged()
|
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
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});
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
void ButtonComponent::onFocusGained()
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
mFocused = true;
|
|
|
|
if (!mFlatStyle)
|
|
|
|
updateImage();
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
void ButtonComponent::onFocusLost()
|
|
|
|
{
|
|
|
|
mFocused = false;
|
|
|
|
if (!mFlatStyle)
|
|
|
|
updateImage();
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
void ButtonComponent::setText(const std::string& text, const std::string& helpText, bool upperCase)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
mText = upperCase ? Utils::String::toUpper(text) : text;
|
2020-06-28 16:39:18 +00:00
|
|
|
mHelpText = helpText;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
mTextCache =
|
|
|
|
std::unique_ptr<TextCache>(mFont->buildTextCache(mText, 0.0f, 0.0f, getCurTextColor()));
|
2014-03-22 21:02:25 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
float minWidth = mFont->sizeText("DELETE").x + (12.0f * Renderer::getScreenWidthModifier());
|
|
|
|
setSize(std::max(mTextCache->metrics.size.x + (12.0f * Renderer::getScreenWidthModifier()),
|
2021-07-07 18:31:46 +00:00
|
|
|
minWidth),
|
2021-08-16 16:25:01 +00:00
|
|
|
mTextCache->metrics.size.y);
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
updateHelpPrompts();
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
void ButtonComponent::setEnabled(bool state)
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
mEnabled = state;
|
|
|
|
if (!mFlatStyle)
|
|
|
|
updateImage();
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
void ButtonComponent::setPadding(const glm::vec4 padding)
|
2013-09-07 22:43:36 +00:00
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
if (mPadding == padding)
|
|
|
|
return;
|
2014-03-12 03:00:08 +00:00
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
mPadding = padding;
|
|
|
|
onSizeChanged();
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
bool ButtonComponent::input(InputConfig* config, Input input)
|
2014-03-12 03:00:08 +00:00
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
if (config->isMappedTo("a", input) && input.value != 0) {
|
|
|
|
if (mPressedFunc && mEnabled)
|
|
|
|
mPressedFunc();
|
|
|
|
return true;
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
return GuiComponent::input(config, input);
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void ButtonComponent::render(const glm::mat4& parentTrans)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::mat4 trans{parentTrans * getTransform()};
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2020-08-30 20:25:38 +00:00
|
|
|
if (mTextCache) {
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::vec3 centerOffset{(mSize.x - mTextCache->metrics.size.x) / 2.0f,
|
|
|
|
(mSize.y - mTextCache->metrics.size.y) / 2.0f, 0.0f};
|
2021-08-15 17:30:31 +00:00
|
|
|
trans = glm::translate(trans, centerOffset);
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2021-03-22 17:07:15 +00:00
|
|
|
if (Settings::getInstance()->getBool("DebugText")) {
|
2021-08-16 16:25:01 +00:00
|
|
|
Renderer::drawRect(centerOffset.x, 0.0f, mTextCache->metrics.size.x, mSize.y,
|
2021-07-07 18:31:46 +00:00
|
|
|
0x00000033, 0x00000033);
|
2021-08-16 16:25:01 +00:00
|
|
|
Renderer::drawRect(mBox.getPosition().x, 0.0f, mBox.getSize().x, mSize.y, 0x0000FF33,
|
|
|
|
0x0000FF33);
|
2021-03-22 17:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2021-03-22 17:07:15 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mTextCache->setColor(getCurTextColor());
|
|
|
|
mFont->renderTextCache(mTextCache.get());
|
2021-08-15 17:30:31 +00:00
|
|
|
trans = glm::translate(trans, -centerOffset);
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
renderChildren(trans);
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
std::vector<HelpPrompt> ButtonComponent::getHelpPrompts()
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str()));
|
|
|
|
return prompts;
|
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
unsigned int ButtonComponent::getCurTextColor() const
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!mFocused)
|
|
|
|
return mTextColorUnfocused;
|
|
|
|
else
|
|
|
|
return mTextColorFocused;
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2021-09-17 19:25:21 +00:00
|
|
|
void ButtonComponent::updateImage()
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
2021-09-17 19:25:21 +00:00
|
|
|
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");
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|