mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-02-06 07:15:39 +00:00
Use checkbox graphics for switches.
Slight optimization to TextComponent (by guaranteeing always having a font).
This commit is contained in:
parent
076131f35c
commit
1c3135b726
|
@ -3,12 +3,15 @@
|
||||||
#include "../resources/Font.h"
|
#include "../resources/Font.h"
|
||||||
#include "../Window.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),
|
mImage.setImage(":/checkbox_unchecked.png");
|
||||||
// (unsigned int)(Renderer::getScreenHeight() * 0.05));
|
|
||||||
|
|
||||||
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)
|
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)
|
if(config->isMappedTo("a", input) && input.value)
|
||||||
{
|
{
|
||||||
mState = !mState;
|
mState = !mState;
|
||||||
|
onStateChanged();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,19 +28,14 @@ bool SwitchComponent::input(InputConfig* config, Input input)
|
||||||
|
|
||||||
void SwitchComponent::render(const Eigen::Affine3f& parentTrans)
|
void SwitchComponent::render(const Eigen::Affine3f& parentTrans)
|
||||||
{
|
{
|
||||||
//Renderer::pushClipRect(getGlobalOffset(), getSize());
|
|
||||||
|
|
||||||
Eigen::Affine3f trans = parentTrans * getTransform();
|
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);
|
renderChildren(trans);
|
||||||
|
|
||||||
//Renderer::popClipRect();
|
|
||||||
|
|
||||||
GuiComponent::renderChildren(trans);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SwitchComponent::getState()
|
bool SwitchComponent::getState() const
|
||||||
{
|
{
|
||||||
return mState;
|
return mState;
|
||||||
}
|
}
|
||||||
|
@ -44,6 +43,12 @@ bool SwitchComponent::getState()
|
||||||
void SwitchComponent::setState(bool state)
|
void SwitchComponent::setState(bool state)
|
||||||
{
|
{
|
||||||
mState = state;
|
mState = state;
|
||||||
|
onStateChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwitchComponent::onStateChanged()
|
||||||
|
{
|
||||||
|
mImage.setImage(mState ? ":/checkbox_checked.png" : ":/checkbox_unchecked.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<HelpPrompt> SwitchComponent::getHelpPrompts()
|
std::vector<HelpPrompt> SwitchComponent::getHelpPrompts()
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../GuiComponent.h"
|
#include "../GuiComponent.h"
|
||||||
|
#include "ImageComponent.h"
|
||||||
|
|
||||||
// A very simple "on/off" switch.
|
// A very simple "on/off" switch.
|
||||||
// Should hopefully be switched to use images instead of text in the future.
|
// 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;
|
bool input(InputConfig* config, Input input) override;
|
||||||
void render(const Eigen::Affine3f& parentTrans) override;
|
void render(const Eigen::Affine3f& parentTrans) override;
|
||||||
|
|
||||||
bool getState();
|
bool getState() const;
|
||||||
void setState(bool state);
|
void setState(bool state);
|
||||||
|
|
||||||
virtual std::vector<HelpPrompt> getHelpPrompts() override;
|
virtual std::vector<HelpPrompt> getHelpPrompts() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void onStateChanged();
|
||||||
|
|
||||||
|
ImageComponent mImage;
|
||||||
bool mState;
|
bool mState;
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,16 +5,16 @@
|
||||||
#include "../ThemeData.h"
|
#include "../ThemeData.h"
|
||||||
|
|
||||||
TextComponent::TextComponent(Window* window) : GuiComponent(window),
|
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)
|
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
|
||||||
{
|
{
|
||||||
|
setFont(font);
|
||||||
setColor(color);
|
setColor(color);
|
||||||
setText(text);
|
setText(text);
|
||||||
setFont(font);
|
|
||||||
setPosition(pos);
|
setPosition(pos);
|
||||||
setSize(size);
|
setSize(size);
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ void TextComponent::onSizeChanged()
|
||||||
onTextChanged();
|
onTextChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextComponent::setFont(std::shared_ptr<Font> font)
|
void TextComponent::setFont(const std::shared_ptr<Font>& font)
|
||||||
{
|
{
|
||||||
mFont = font;
|
mFont = font;
|
||||||
onTextChanged();
|
onTextChanged();
|
||||||
|
@ -65,21 +65,11 @@ void TextComponent::setCentered(bool center)
|
||||||
mCentered = 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)
|
void TextComponent::render(const Eigen::Affine3f& parentTrans)
|
||||||
{
|
{
|
||||||
std::shared_ptr<Font> font = getFont();
|
|
||||||
|
|
||||||
Eigen::Affine3f trans = parentTrans * getTransform();
|
Eigen::Affine3f trans = parentTrans * getTransform();
|
||||||
|
|
||||||
if(font && !mText.empty())
|
if(mTextCache)
|
||||||
{
|
{
|
||||||
if(mCentered)
|
if(mCentered)
|
||||||
{
|
{
|
||||||
|
@ -93,7 +83,7 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
|
||||||
Renderer::setMatrix(trans);
|
Renderer::setMatrix(trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
font->renderTextCache(mTextCache.get());
|
mFont->renderTextCache(mTextCache.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
GuiComponent::renderChildren(trans);
|
GuiComponent::renderChildren(trans);
|
||||||
|
@ -101,15 +91,13 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
|
||||||
|
|
||||||
void TextComponent::calculateExtent()
|
void TextComponent::calculateExtent()
|
||||||
{
|
{
|
||||||
std::shared_ptr<Font> font = getFont();
|
|
||||||
|
|
||||||
if(mAutoCalcExtent.x())
|
if(mAutoCalcExtent.x())
|
||||||
{
|
{
|
||||||
mSize = font->sizeText(mText);
|
mSize = mFont->sizeText(mText);
|
||||||
}else{
|
}else{
|
||||||
if(mAutoCalcExtent.y())
|
if(mAutoCalcExtent.y())
|
||||||
{
|
{
|
||||||
mSize[1] = font->sizeWrappedText(mText, getSize().x()).y();
|
mSize[1] = mFont->sizeWrappedText(mText, getSize().x()).y();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,9 @@ class TextComponent : public GuiComponent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TextComponent(Window* window);
|
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 onSizeChanged() override;
|
||||||
void setText(const std::string& text);
|
void setText(const std::string& text);
|
||||||
void setColor(unsigned int color);
|
void setColor(unsigned int color);
|
||||||
|
@ -31,7 +31,7 @@ public:
|
||||||
unsigned char getOpacity() const override;
|
unsigned char getOpacity() const override;
|
||||||
void setOpacity(unsigned char opacity) 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;
|
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue