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

View File

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