1
0
mirror of synced 2025-02-08 22:59:41 +01:00

Make ButtonHighlight Transformable

This commit is contained in:
Stepland 2020-02-09 21:47:40 +01:00
parent 7facfcda3d
commit 51bc0fc465
2 changed files with 41 additions and 36 deletions

View File

@ -1,35 +1,39 @@
#include "ButtonHighlight.hpp" #include "ButtonHighlight.hpp"
MusicSelect::ButtonHighlight::ButtonHighlight(std::size_t t_panel_size) : namespace MusicSelect {
panel_size(t_panel_size), ButtonHighlight::ButtonHighlight(const float& panel_size, const float& panel_spacing) :
highlight({static_cast<float>(t_panel_size-3), static_cast<float>(t_panel_size-3)}), m_panel_size(panel_size),
time_to_alpha(0.f, 0.25f, 255.f, 0.f) m_panel_spacing(panel_spacing),
{ m_highlight({static_cast<float>(panel_size-3), static_cast<float>(panel_size-3)}),
highlight.setFillColor(sf::Color::Transparent); m_time_to_alpha(0.f, 0.25f, 255.f, 0.f)
highlight.setOutlineThickness(1.f); {
highlight.setOrigin(highlight.getSize().x / 2.f, highlight.getSize().y / 2.f); m_highlight.setFillColor(sf::Color::Transparent);
} m_highlight.setOutlineThickness(1.f);
m_highlight.setOrigin(m_highlight.getSize().x / 2.f, m_highlight.getSize().y / 2.f);
}
void MusicSelect::ButtonHighlight::button_pressed(Button button) { void ButtonHighlight::button_pressed(Button button) {
button_presses_history[button].restart(); m_button_presses_history[button].restart();
} }
void MusicSelect::ButtonHighlight::draw(sf::RenderTarget& target, sf::RenderStates states) const { void ButtonHighlight::draw(sf::RenderTarget& target, sf::RenderStates states) const {
auto it = button_presses_history.begin(); states.transform *= getTransform();
while (it != button_presses_history.end()) { auto it = m_button_presses_history.begin();
auto elapsed = it->second.getElapsedTime(); while (it != m_button_presses_history.end()) {
auto coords = toCoords(it->first); auto elapsed = it->second.getElapsedTime();
if (elapsed > sf::milliseconds(250)) { auto coords = toCoords(it->first);
it = button_presses_history.erase(it); if (elapsed > sf::milliseconds(250)) {
} else { it = m_button_presses_history.erase(it);
auto alpha = time_to_alpha.transform(elapsed.asSeconds()); } else {
highlight.setOutlineColor(sf::Color(255,255,0,static_cast<std::size_t>(alpha))); auto alpha = m_time_to_alpha.transform(elapsed.asSeconds());
highlight.setPosition({ m_highlight.setOutlineColor(sf::Color(255,255,0,static_cast<std::size_t>(alpha)));
static_cast<float>(coords.x * panel_size) + panel_size/2.f, m_highlight.setPosition({
static_cast<float>(coords.y * panel_size) + panel_size/2.f static_cast<float>(coords.x * (m_panel_size+m_panel_spacing)) + m_panel_size/2.f,
}); static_cast<float>(coords.y * (m_panel_size+m_panel_spacing)) + m_panel_size/2.f
target.draw(highlight, states); });
++it; target.draw(m_highlight, states);
} ++it;
} }
}
}
} }

View File

@ -9,15 +9,16 @@
#include "../../Toolkit/AffineTransform.hpp" #include "../../Toolkit/AffineTransform.hpp"
namespace MusicSelect { namespace MusicSelect {
class ButtonHighlight : public sf::Drawable { class ButtonHighlight : public sf::Drawable, public sf::Transformable {
public: public:
explicit ButtonHighlight(std::size_t t_panel_size); ButtonHighlight(const float& panel_size, const float& panel_spacing);
void button_pressed(Button button); void button_pressed(Button button);
private: private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
std::size_t panel_size; const float& m_panel_size;
mutable sf::RectangleShape highlight; const float& m_panel_spacing;
mutable std::map<Button, sf::Clock> button_presses_history; mutable sf::RectangleShape m_highlight;
Toolkit::AffineTransform<float> time_to_alpha; mutable std::map<Button, sf::Clock> m_button_presses_history;
Toolkit::AffineTransform<float> m_time_to_alpha;
}; };
} }