ES-DE/src/components/ButtonComponent.cpp

119 lines
2.6 KiB
C++
Raw Normal View History

2013-08-22 20:29:50 +00:00
#include "ButtonComponent.h"
#include "../Renderer.h"
#include "../Window.h"
#include "../Util.h"
#include "../Log.h"
2013-08-22 20:29:50 +00:00
ButtonComponent::ButtonComponent(Window* window, const std::string& text, const std::string& helpText, const std::function<void()>& func) : GuiComponent(window),
mBox(window, ":/button.png"),
mFont(Font::get(FONT_SIZE_MEDIUM)),
mFocused(false),
mEnabled(true),
mTextColorFocused(0xFFFFFFFF), mTextColorUnfocused(0x777777FF)
2013-08-22 20:29:50 +00:00
{
setPressedFunc(func);
setText(text, helpText);
2013-08-22 20:29:50 +00:00
}
void ButtonComponent::onSizeChanged()
{
mBox.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(-32, -32));
}
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) && input.value != 0)
2013-08-22 20:29:50 +00:00
{
if(mPressedFunc && mEnabled)
mPressedFunc();
2013-08-22 20:29:50 +00:00
return true;
}
return GuiComponent::input(config, input);
}
void ButtonComponent::setText(const std::string& text, const std::string& helpText)
2013-08-22 20:29:50 +00:00
{
mText = strToUpper(text);
mHelpText = helpText;
mTextCache = std::unique_ptr<TextCache>(mFont->buildTextCache(mText, 0, 0, getCurTextColor()));
setSize(mTextCache->metrics.size + Eigen::Vector2f(12, 0));
updateHelpPrompts();
2013-08-22 20:29:50 +00:00
}
void ButtonComponent::onFocusGained()
{
mFocused = true;
updateImage();
}
void ButtonComponent::onFocusLost()
{
mFocused = false;
updateImage();
}
void ButtonComponent::setEnabled(bool enabled)
{
mEnabled = enabled;
updateImage();
}
void ButtonComponent::updateImage()
{
if(!mEnabled || !mPressedFunc)
{
mBox.setImagePath(":/button.png");
mBox.setCenterColor(0x770000FF);
mBox.setEdgeColor(0x770000FF);
return;
}
mBox.setCenterColor(0xFFFFFFFF);
mBox.setEdgeColor(0xFFFFFFFF);
mBox.setImagePath(mFocused ? ":/button_filled.png" : ":/button.png");
}
2013-08-22 20:29:50 +00:00
void ButtonComponent::render(const Eigen::Affine3f& parentTrans)
{
Eigen::Affine3f trans = parentTrans * getTransform();
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);
mTextCache->setColor(getCurTextColor());
mFont->renderTextCache(mTextCache.get());
2013-08-22 20:29:50 +00:00
trans = trans.translate(-centerOffset);
}
renderChildren(trans);
}
unsigned int ButtonComponent::getCurTextColor() const
{
if(!mFocused)
return mTextColorUnfocused;
else
return mTextColorFocused;
}
std::vector<HelpPrompt> ButtonComponent::getHelpPrompts()
{
std::vector<HelpPrompt> prompts;
prompts.push_back(HelpPrompt("a", mHelpText.empty() ? mText.c_str() : mHelpText.c_str()));
return prompts;
}