2013-08-22 20:29:50 +00:00
|
|
|
#include "ButtonComponent.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
#include "../Window.h"
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
ButtonComponent::ButtonComponent(Window* window) : GuiComponent(window),
|
2013-09-07 22:43:36 +00:00
|
|
|
mBox(window, ":/button.png"),
|
|
|
|
mFocused(false),
|
|
|
|
mTextColorFocused(0x000000FF), mTextColorUnfocused(0x333333FF), mTextPulseTime(0)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
|
|
|
setSize(64, 48);
|
|
|
|
}
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
void ButtonComponent::onSizeChanged()
|
|
|
|
{
|
|
|
|
mBox.setSize(mSize);
|
|
|
|
}
|
|
|
|
|
2013-08-22 20:29:50 +00:00
|
|
|
void ButtonComponent::setPressedFunc(std::function<void()> f)
|
|
|
|
{
|
|
|
|
mPressedFunc = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ButtonComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("a", input))
|
|
|
|
{
|
2013-08-23 17:21:22 +00:00
|
|
|
if(mPressedFunc)
|
|
|
|
mPressedFunc();
|
2013-08-22 20:29:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
void ButtonComponent::setText(const std::string& text, unsigned int focusedColor, unsigned int unfocusedColor)
|
2013-08-22 20:29:50 +00:00
|
|
|
{
|
|
|
|
mText = text;
|
2013-09-07 22:43:36 +00:00
|
|
|
mTextColorFocused = focusedColor;
|
|
|
|
mTextColorUnfocused = unfocusedColor;
|
2013-08-22 20:29:50 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Font> f = getFont();
|
2013-09-07 22:43:36 +00:00
|
|
|
mTextCache = std::unique_ptr<TextCache>(f->buildTextCache(mText, 0, 0, getCurTextColor()));
|
2013-08-22 20:29:50 +00:00
|
|
|
|
|
|
|
setSize(mTextCache->metrics.size + Eigen::Vector2f(12, 12));
|
|
|
|
}
|
|
|
|
|
2013-09-07 22:43:36 +00:00
|
|
|
void ButtonComponent::onFocusGained()
|
|
|
|
{
|
|
|
|
mFocused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonComponent::onFocusLost()
|
|
|
|
{
|
|
|
|
mFocused = false;
|
|
|
|
}
|
|
|
|
|
2013-08-22 20:29:50 +00:00
|
|
|
void ButtonComponent::render(const Eigen::Affine3f& parentTrans)
|
|
|
|
{
|
|
|
|
Eigen::Affine3f trans = 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);
|
|
|
|
trans = trans.translate(centerOffset);
|
|
|
|
|
|
|
|
Renderer::setMatrix(trans);
|
2013-09-07 22:43:36 +00:00
|
|
|
mTextCache->setColor(getCurTextColor());
|
2013-08-22 20:29:50 +00:00
|
|
|
getFont()->renderTextCache(mTextCache.get());
|
|
|
|
trans = trans.translate(-centerOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderChildren(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Font> ButtonComponent::getFont()
|
|
|
|
{
|
|
|
|
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
|
|
|
|
}
|
2013-09-07 22:43:36 +00:00
|
|
|
|
|
|
|
unsigned int ButtonComponent::getCurTextColor() const
|
|
|
|
{
|
|
|
|
if(!mFocused)
|
|
|
|
return mTextColorUnfocused;
|
|
|
|
else
|
|
|
|
return mTextColorFocused;
|
|
|
|
}
|