2021-07-27 14:18:37 -04:00
|
|
|
#include "button.h"
|
|
|
|
|
|
|
|
Button::Button(const std::string &text) :
|
|
|
|
_pressed(false)
|
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
_button_text.setFillColor(sf::Color::Black);
|
|
|
|
_button_content.setFillColor(sf::Color::White);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::input(const sf::Event& event)
|
|
|
|
{
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sf::Event::MouseButtonPressed:
|
|
|
|
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
|
|
|
{
|
|
|
|
_pressed = true;
|
|
|
|
_button_content.setFillColor(sf::Color(155, 155, 155));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sf::Event::MouseButtonReleased:
|
|
|
|
if (_pressed)
|
|
|
|
{
|
|
|
|
_button_content.setFillColor(sf::Color::White);
|
|
|
|
_pressed = false;
|
|
|
|
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
|
|
|
_on_click_callback();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::input(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::update()
|
|
|
|
{
|
|
|
|
Widget::update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
|
|
|
{
|
|
|
|
target.draw(_button_content, states);
|
|
|
|
target.draw(_button_text, states);
|
|
|
|
|
|
|
|
Widget::draw(target, states);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setRect(const sf::FloatRect& rect)
|
|
|
|
{
|
|
|
|
_button_content.setPosition(rect.left, rect.top);
|
2021-08-04 15:06:01 -04:00
|
|
|
_button_content.setSize({rect.width, rect.height});
|
2021-07-27 14:18:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setPosition(const sf::Vector2f &position)
|
|
|
|
{
|
|
|
|
_button_content.setPosition(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::isUnderMouse(int mouse_x, int mouse_y) const
|
|
|
|
{
|
|
|
|
return _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
|
|
|
}
|
|
|
|
|
2021-08-20 14:33:23 -04:00
|
|
|
void Button::setFillColor(sf::Color&& color)
|
|
|
|
{
|
|
|
|
_button_content.setFillColor(std::move(color));
|
|
|
|
}
|
|
|
|
|
2021-07-27 14:18:37 -04:00
|
|
|
void Button::setText(const std::string& text)
|
|
|
|
{
|
|
|
|
_button_text.setString(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCallback(std::function<void(void)> callback)
|
|
|
|
{
|
|
|
|
_on_click_callback = callback;
|
|
|
|
}
|
|
|
|
|