ES-DE/src/components/ButtonComponent.cpp
Aloshi 4ef5f64ff3 Added on-screen help system.
Very intrusive right now. You can turn it off in the Settings menu until
that gets worked out.
2014-01-25 17:34:29 -06:00

99 lines
2.2 KiB
C++

#include "ButtonComponent.h"
#include "../Renderer.h"
#include "../Window.h"
ButtonComponent::ButtonComponent(Window* window) : GuiComponent(window),
mBox(window, ":/button.png"),
mFocused(false),
mTextColorFocused(0x000000FF), mTextColorUnfocused(0x333333FF), mTextPulseTime(0)
{
setSize(64, 48);
}
void ButtonComponent::onSizeChanged()
{
mBox.setSize(mSize);
}
void ButtonComponent::setPressedFunc(std::function<void()> f)
{
mPressedFunc = f;
}
bool ButtonComponent::input(InputConfig* config, Input input)
{
if(config->isMappedTo("a", input) && input.value != 0)
{
if(mPressedFunc)
mPressedFunc();
return true;
}
return GuiComponent::input(config, input);
}
void ButtonComponent::setText(const std::string& text, const std::string& helpText, unsigned int focusedColor, unsigned int unfocusedColor)
{
mText = text;
mHelpText = helpText;
mTextColorFocused = focusedColor;
mTextColorUnfocused = unfocusedColor;
std::shared_ptr<Font> f = getFont();
mTextCache = std::unique_ptr<TextCache>(f->buildTextCache(mText, 0, 0, getCurTextColor()));
setSize(mTextCache->metrics.size + Eigen::Vector2f(12, 12));
updateHelpPrompts();
}
void ButtonComponent::onFocusGained()
{
mFocused = true;
}
void ButtonComponent::onFocusLost()
{
mFocused = false;
}
void ButtonComponent::render(const Eigen::Affine3f& parentTrans)
{
Eigen::Affine3f trans = parentTrans * getTransform();
mBox.render(trans);
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());
getFont()->renderTextCache(mTextCache.get());
trans = trans.translate(-centerOffset);
}
renderChildren(trans);
}
std::shared_ptr<Font> ButtonComponent::getFont()
{
return Font::get(FONT_SIZE_SMALL);
}
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;
}