Use checkbox graphics for switches.

Slight optimization to TextComponent (by guaranteeing always having a font).
This commit is contained in:
Aloshi 2014-03-07 19:35:16 -06:00
parent 076131f35c
commit 1c3135b726
4 changed files with 34 additions and 37 deletions

View file

@ -3,12 +3,15 @@
#include "../resources/Font.h"
#include "../Window.h"
SwitchComponent::SwitchComponent(Window* window, bool state) : GuiComponent(window), mState(state)
SwitchComponent::SwitchComponent(Window* window, bool state) : GuiComponent(window), mImage(window), mState(state)
{
//mSize = Vector2u((unsigned int)(Renderer::getScreenWidth() * 0.05),
// (unsigned int)(Renderer::getScreenHeight() * 0.05));
mImage.setImage(":/checkbox_unchecked.png");
mSize = Font::get(FONT_SIZE_MEDIUM)->sizeText("OFF");
float height = (float)FONT_SIZE_MEDIUM;
if(mImage.getTextureSize().y() > height)
mImage.setResize(0, height);
mSize = mImage.getSize();
}
bool SwitchComponent::input(InputConfig* config, Input input)
@ -16,6 +19,7 @@ bool SwitchComponent::input(InputConfig* config, Input input)
if(config->isMappedTo("a", input) && input.value)
{
mState = !mState;
onStateChanged();
return true;
}
@ -24,19 +28,14 @@ bool SwitchComponent::input(InputConfig* config, Input input)
void SwitchComponent::render(const Eigen::Affine3f& parentTrans)
{
//Renderer::pushClipRect(getGlobalOffset(), getSize());
Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans);
mImage.render(trans);
Font::get(FONT_SIZE_MEDIUM)->drawText(mState ? "ON" : "OFF", Eigen::Vector2f(0, 0), mState ? 0x00FF00FF : 0xFF0000FF);
//Renderer::popClipRect();
GuiComponent::renderChildren(trans);
renderChildren(trans);
}
bool SwitchComponent::getState()
bool SwitchComponent::getState() const
{
return mState;
}
@ -44,6 +43,12 @@ bool SwitchComponent::getState()
void SwitchComponent::setState(bool state)
{
mState = state;
onStateChanged();
}
void SwitchComponent::onStateChanged()
{
mImage.setImage(mState ? ":/checkbox_checked.png" : ":/checkbox_unchecked.png");
}
std::vector<HelpPrompt> SwitchComponent::getHelpPrompts()

View file

@ -1,6 +1,7 @@
#pragma once
#include "../GuiComponent.h"
#include "ImageComponent.h"
// A very simple "on/off" switch.
// Should hopefully be switched to use images instead of text in the future.
@ -12,11 +13,14 @@ public:
bool input(InputConfig* config, Input input) override;
void render(const Eigen::Affine3f& parentTrans) override;
bool getState();
bool getState() const;
void setState(bool state);
virtual std::vector<HelpPrompt> getHelpPrompts() override;
private:
void onStateChanged();
ImageComponent mImage;
bool mState;
};

View file

@ -5,16 +5,16 @@
#include "../ThemeData.h"
TextComponent::TextComponent(Window* window) : GuiComponent(window),
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
mFont(Font::get(FONT_SIZE_MEDIUM)), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
{
}
TextComponent::TextComponent(Window* window, const std::string& text, std::shared_ptr<Font> font, unsigned int color, Eigen::Vector3f pos, Eigen::Vector2f size) : GuiComponent(window),
TextComponent::TextComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color, Eigen::Vector3f pos, Eigen::Vector2f size) : GuiComponent(window),
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
{
setFont(font);
setColor(color);
setText(text);
setFont(font);
setPosition(pos);
setSize(size);
}
@ -25,7 +25,7 @@ void TextComponent::onSizeChanged()
onTextChanged();
}
void TextComponent::setFont(std::shared_ptr<Font> font)
void TextComponent::setFont(const std::shared_ptr<Font>& font)
{
mFont = font;
onTextChanged();
@ -65,21 +65,11 @@ void TextComponent::setCentered(bool center)
mCentered = center;
}
std::shared_ptr<Font> TextComponent::getFont() const
{
if(mFont)
return mFont;
else
return Font::get(FONT_SIZE_MEDIUM);
}
void TextComponent::render(const Eigen::Affine3f& parentTrans)
{
std::shared_ptr<Font> font = getFont();
Eigen::Affine3f trans = parentTrans * getTransform();
if(font && !mText.empty())
if(mTextCache)
{
if(mCentered)
{
@ -93,7 +83,7 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
Renderer::setMatrix(trans);
}
font->renderTextCache(mTextCache.get());
mFont->renderTextCache(mTextCache.get());
}
GuiComponent::renderChildren(trans);
@ -101,15 +91,13 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
void TextComponent::calculateExtent()
{
std::shared_ptr<Font> font = getFont();
if(mAutoCalcExtent.x())
{
mSize = font->sizeText(mText);
mSize = mFont->sizeText(mText);
}else{
if(mAutoCalcExtent.y())
{
mSize[1] = font->sizeWrappedText(mText, getSize().x()).y();
mSize[1] = mFont->sizeWrappedText(mText, getSize().x()).y();
}
}
}

View file

@ -15,9 +15,9 @@ class TextComponent : public GuiComponent
{
public:
TextComponent(Window* window);
TextComponent(Window* window, const std::string& text, std::shared_ptr<Font> font, unsigned int color = 0x000000FF, Eigen::Vector3f pos = Eigen::Vector3f::Zero(), Eigen::Vector2f size = Eigen::Vector2f::Zero());
TextComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color = 0x000000FF, Eigen::Vector3f pos = Eigen::Vector3f::Zero(), Eigen::Vector2f size = Eigen::Vector2f::Zero());
void setFont(std::shared_ptr<Font> font);
void setFont(const std::shared_ptr<Font>& font);
void onSizeChanged() override;
void setText(const std::string& text);
void setColor(unsigned int color);
@ -31,7 +31,7 @@ public:
unsigned char getOpacity() const override;
void setOpacity(unsigned char opacity) override;
std::shared_ptr<Font> getFont() const;
inline std::shared_ptr<Font> getFont() const { return mFont; }
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;