Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Button: setColor, setTextColor
Browse files Browse the repository at this point in the history
+ Box and ItemBox now tints the entire area, not just inside the frame.
  • Loading branch information
xparq committed Dec 6, 2023
1 parent 52a4e66 commit e7b9b19
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
8 changes: 6 additions & 2 deletions include/sfw/Gfx/_backend-specific/SFML/ItemBox.inl
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ void ItemBox<T>::draw(sf::RenderTarget& target, const sf::RenderStates& states)
{
Box::draw(target, states);
target.draw(m_item, states);

// "Tint" the boxes *after* the item has been drawn! (So, alpha is expected to have been set accordingly.)
// "Tint" the box *after* the item has been drawn! (So, alpha is expected to have been set accordingly!)
if (m_tintColor)
{
/*
sf::RectangleShape r(sf::Vector2f(getSize().x - 2 * (float)Theme::borderSize,
getSize().y - 2 * (float)Theme::borderSize));
r.setPosition(getPosition()); r.move({(float)Theme::borderSize, (float)Theme::borderSize});
*/
sf::RectangleShape r(sf::Vector2f(getSize().x, getSize().y));
r.setPosition(getPosition());

r.setFillColor(m_tintColor.value());
r.setOutlineThickness(0);
target.draw(r, states);
Expand Down
4 changes: 4 additions & 0 deletions include/sfw/Widgets/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "sfw/InputWidget.hpp"
#include "sfw/Gfx/Elements/Text.hpp"
#include "sfw/Gfx/Elements/ItemBox.hpp"
#include "sfw/Gfx/Color.hpp"

#include <string>

Expand All @@ -27,6 +28,9 @@ class Button: public InputWidget<Button>
Button* setText(const std::string& text);
std::string getText() const;

Button* setColor(sf::Color); // Overall tint, except the label
Button* setTextColor(sf::Color);

Button* click(); // "Macro" for automation

private:
Expand Down
6 changes: 5 additions & 1 deletion src/lib/Gfx/Elements/Box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,16 @@ void Box::draw(sf::RenderTarget& target, const sf::RenderStates& states) const
auto lstates = states;
lstates.texture = &Theme::getTexture();
target.draw(m_vertices, VERTEX_COUNT, sf::PrimitiveType::TriangleStrip, lstates);
// Override the texture with a filled rect (presumably with some alpha) if fillColor was set:
// Overdraw the with a filled rect (presumably with some alpha!) if fillColor was set:
if (m_fillColor)
{
/*
sf::RectangleShape r(sf::Vector2f(getSize().x - 2 * (float)Theme::borderSize,
getSize().y - 2 * (float)Theme::borderSize));
r.setPosition(getPosition()); r.move({(float)Theme::borderSize, (float)Theme::borderSize});
*/
sf::RectangleShape r(sf::Vector2f(getSize().x, getSize().y));
r.setPosition(getPosition());
r.setFillColor(m_fillColor.value());
r.setOutlineThickness(0);
target.draw(r, lstates);
Expand Down
13 changes: 13 additions & 0 deletions src/lib/Widgets/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ std::string Button::getText() const
}


Button* Button::setColor(sf::Color c)
{
m_box.setTintColor(c);
return this;
}


Button* Button::setTextColor(sf::Color c)
{
m_box.setItemColor(c);
return this;
}

Button* Button::click()
{
//! This weird kludge won't do antything to the visuals. (That would require
Expand Down

0 comments on commit e7b9b19

Please sign in to comment.