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
|
|
|
|
|
|
|
#include "resources/Font.h"
|
2018-01-27 17:04:28 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
ButtonComponent::ButtonComponent(
|
|
|
|
Window* window, const std::string& text,
|
|
|
|
const std::string& helpText,
|
|
|
|
const std::function<void()>& func)
|
|
|
|
: GuiComponent(window),
|
|
|
|
mBox(window, ":/graphics/button.png"),
|
|
|
|
mFont(Font::get(FONT_SIZE_MEDIUM)),
|
|
|
|
mFocused(false),
|
|
|
|
mEnabled(true),
|
|
|
|
mTextColorFocused(0xFFFFFFFF), mTextColorUnfocused(0x777777FF)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
setPressedFunc(func);
|
|
|
|
setText(text, helpText);
|
|
|
|
updateImage();
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
void ButtonComponent::onSizeChanged()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mBox.fitTo(mSize, Vector3f::Zero(), Vector2f(-32, -32));
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 20:29:50 +00:00
|
|
|
void ButtonComponent::setPressedFunc(std::function<void()> f)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mPressedFunc = f;
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ButtonComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (config->isMappedTo("a", input) && input.value != 0) {
|
|
|
|
if (mPressedFunc && mEnabled)
|
|
|
|
mPressedFunc();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 17:48:47 +00:00
|
|
|
void ButtonComponent::setText(const std::string& text, const std::string& helpText)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mText = Utils::String::toUpper(text);
|
|
|
|
mHelpText = helpText;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mTextCache = std::unique_ptr<TextCache>(mFont->buildTextCache(mText, 0, 0, getCurTextColor()));
|
2014-03-22 21:02:25 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
float minWidth = mFont->sizeText("DELETE").x() + 12;
|
2020-12-28 10:29:32 +00:00
|
|
|
setSize(std::max(mTextCache->metrics.size.x() + 12, minWidth), 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
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
void ButtonComponent::onFocusGained()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mFocused = true;
|
|
|
|
updateImage();
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonComponent::onFocusLost()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mFocused = false;
|
|
|
|
updateImage();
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:49:43 +00:00
|
|
|
void ButtonComponent::setEnabled(bool state)
|
2014-03-12 03:00:08 +00:00
|
|
|
{
|
2020-12-15 17:49:43 +00:00
|
|
|
mEnabled = state;
|
2020-06-28 16:39:18 +00:00
|
|
|
updateImage();
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonComponent::updateImage()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!mEnabled || !mPressedFunc) {
|
|
|
|
mBox.setImagePath(":/graphics/button_filled.png");
|
|
|
|
mBox.setCenterColor(0x770000FF);
|
|
|
|
mBox.setEdgeColor(0x770000FF);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBox.setCenterColor(0xFFFFFFFF);
|
|
|
|
mBox.setEdgeColor(0xFFFFFFFF);
|
|
|
|
mBox.setImagePath(mFocused ? ":/graphics/button_filled.png" : ":/graphics/button.png");
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void ButtonComponent::render(const Transform4x4f& parentTrans)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mBox.render(trans);
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2020-08-30 20:25:38 +00:00
|
|
|
if (mTextCache) {
|
2020-06-28 16:39:18 +00:00
|
|
|
Vector3f centerOffset((mSize.x() - mTextCache->metrics.size.x()) / 2,
|
|
|
|
(mSize.y() - mTextCache->metrics.size.y()) / 2, 0);
|
|
|
|
trans = trans.translate(centerOffset);
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
mTextCache->setColor(getCurTextColor());
|
|
|
|
mFont->renderTextCache(mTextCache.get());
|
|
|
|
trans = trans.translate(-centerOffset);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
std::vector<HelpPrompt> ButtonComponent::getHelpPrompts()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str()));
|
|
|
|
return prompts;
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|