2013-08-22 20:29:50 +00:00
|
|
|
#include "ButtonComponent.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
#include "../Window.h"
|
2014-03-12 23:24:34 +00:00
|
|
|
#include "../Util.h"
|
2014-03-15 18:39:19 +00:00
|
|
|
#include "../Log.h"
|
2013-08-22 20:29:50 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
ButtonComponent::ButtonComponent(Window* window, const std::string& text, const std::string& helpText, const std::function<void()>& func) : GuiComponent(window),
|
2013-09-07 22:43:36 +00:00
|
|
|
mBox(window, ":/button.png"),
|
2014-03-15 18:39:19 +00:00
|
|
|
mFont(Font::get(FONT_SIZE_MEDIUM)),
|
2013-09-07 22:43:36 +00:00
|
|
|
mFocused(false),
|
2014-03-12 03:00:08 +00:00
|
|
|
mEnabled(true),
|
2014-03-08 17:48:47 +00:00
|
|
|
mTextColorFocused(0xFFFFFFFF), mTextColorUnfocused(0x777777FF)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
setPressedFunc(func);
|
|
|
|
setText(text, helpText);
|
2014-03-18 21:05:56 +00:00
|
|
|
updateImage();
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
void ButtonComponent::onSizeChanged()
|
|
|
|
{
|
2014-03-08 17:48:47 +00:00
|
|
|
mBox.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::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)
|
|
|
|
{
|
|
|
|
mPressedFunc = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ButtonComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
2013-09-19 23:41:14 +00:00
|
|
|
if(config->isMappedTo("a", input) && input.value != 0)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(mPressedFunc && mEnabled)
|
2013-08-23 17:21:22 +00:00
|
|
|
mPressedFunc();
|
2013-08-22 20:29:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-03-12 23:24:34 +00:00
|
|
|
mText = strToUpper(text);
|
2014-01-25 23:34:29 +00:00
|
|
|
mHelpText = helpText;
|
2014-03-08 17:48:47 +00:00
|
|
|
|
2014-03-15 18:39:19 +00:00
|
|
|
mTextCache = std::unique_ptr<TextCache>(mFont->buildTextCache(mText, 0, 0, getCurTextColor()));
|
2014-03-22 21:02:25 +00:00
|
|
|
|
|
|
|
float minWidth = mFont->sizeText("DELETE").x() + 12;
|
|
|
|
setSize(std::max(mTextCache->metrics.size.x() + 12, minWidth), mTextCache->metrics.size.y());
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
updateHelpPrompts();
|
2013-08-22 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
void ButtonComponent::onFocusGained()
|
|
|
|
{
|
|
|
|
mFocused = true;
|
2014-03-12 03:00:08 +00:00
|
|
|
updateImage();
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonComponent::onFocusLost()
|
|
|
|
{
|
|
|
|
mFocused = false;
|
2014-03-12 03:00:08 +00:00
|
|
|
updateImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonComponent::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
mEnabled = enabled;
|
|
|
|
updateImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonComponent::updateImage()
|
|
|
|
{
|
|
|
|
if(!mEnabled || !mPressedFunc)
|
|
|
|
{
|
2014-03-18 21:05:56 +00:00
|
|
|
mBox.setImagePath(":/button_filled.png");
|
2014-03-12 03:00:08 +00:00
|
|
|
mBox.setCenterColor(0x770000FF);
|
|
|
|
mBox.setEdgeColor(0x770000FF);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBox.setCenterColor(0xFFFFFFFF);
|
|
|
|
mBox.setEdgeColor(0xFFFFFFFF);
|
|
|
|
mBox.setImagePath(mFocused ? ":/button_filled.png" : ":/button.png");
|
2013-09-07 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 20:29:50 +00:00
|
|
|
void ButtonComponent::render(const Eigen::Affine3f& parentTrans)
|
|
|
|
{
|
2014-03-19 20:03:23 +00:00
|
|
|
Eigen::Affine3f trans = roundMatrix(parentTrans * getTransform());
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
mBox.render(trans);
|
|
|
|
|
2013-08-22 20:29:50 +00:00
|
|
|
if(mTextCache)
|
|
|
|
{
|
|
|
|
Eigen::Vector3f centerOffset((mSize.x() - mTextCache->metrics.size.x()) / 2, (mSize.y() - mTextCache->metrics.size.y()) / 2, 0);
|
2014-03-19 20:03:23 +00:00
|
|
|
centerOffset = roundVector(centerOffset);
|
2013-08-22 20:29:50 +00:00
|
|
|
trans = trans.translate(centerOffset);
|
|
|
|
|
|
|
|
Renderer::setMatrix(trans);
|
2013-09-07 22:43:36 +00:00
|
|
|
mTextCache->setColor(getCurTextColor());
|
2014-03-15 18:39:19 +00:00
|
|
|
mFont->renderTextCache(mTextCache.get());
|
2013-08-22 20:29:50 +00:00
|
|
|
trans = trans.translate(-centerOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderChildren(trans);
|
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
unsigned int ButtonComponent::getCurTextColor() const
|
|
|
|
{
|
|
|
|
if(!mFocused)
|
|
|
|
return mTextColorUnfocused;
|
|
|
|
else
|
|
|
|
return mTextColorFocused;
|
|
|
|
}
|
2014-01-25 23:34:29 +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;
|
|
|
|
}
|