68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
#include "button.h"
|
|
#include <iostream>
|
|
|
|
Button::Button(const std::string &text, const std::shared_ptr<sf::Font>& font, unsigned int font_size) :
|
|
_font(font)
|
|
{
|
|
setText(text);
|
|
_button_text.setFillColor(sf::Color::Black);
|
|
_button_text.setCharacterSize(font_size);
|
|
_button_text.setFont(*_font);
|
|
}
|
|
|
|
void Button::update(const sf::Time& dt)
|
|
{
|
|
Widget::update(dt);
|
|
}
|
|
|
|
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
|
{
|
|
if (_is_visible)
|
|
{
|
|
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);
|
|
_button_content.setSize({rect.width, rect.height});
|
|
_button_text.setPosition(rect.left + 5, rect.top + 5);
|
|
}
|
|
|
|
void Button::setPosition(const sf::Vector2f &position)
|
|
{
|
|
_button_content.setPosition(position);
|
|
_button_text.move(position.x + 5, position.y + 5);
|
|
}
|
|
|
|
void Button::move(const sf::Vector2f &delta)
|
|
{
|
|
_button_content.move(delta);
|
|
_button_text.move(delta);
|
|
Widget::move(delta);
|
|
}
|
|
|
|
bool Button::isUnderMouse(int mouse_x, int mouse_y) const
|
|
{
|
|
return _is_visible && _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
|
}
|
|
|
|
void Button::setText(const std::string& text)
|
|
{
|
|
_button_text.setString(text);
|
|
}
|
|
|
|
sf::FloatRect Button::rect() const
|
|
{
|
|
return _button_content.getGlobalBounds();
|
|
}
|
|
|
|
sf::Vector2f Button::position() const
|
|
{
|
|
return _button_content.getPosition();
|
|
}
|