120 lines
3.1 KiB
C++
120 lines
3.1 KiB
C++
#include "window.h"
|
|
#include "pushbutton.h"
|
|
|
|
Window::Window(const std::string& text, const std::shared_ptr<sf::Font>& font) :
|
|
_font(font),
|
|
_is_dragging(false)
|
|
{
|
|
_bar_title.setFont(*font);
|
|
_bar_title.setString(text);
|
|
_bar_title.setCharacterSize(12);
|
|
_bar_title.setFillColor(sf::Color(188, 157, 207));
|
|
_bar.setFillColor(sf::Color(88, 57, 107));
|
|
_window_content.setFillColor(sf::Color(188, 157, 207));
|
|
}
|
|
|
|
void Window::input(const sf::Event& event)
|
|
{
|
|
Widget::input(event);
|
|
|
|
switch (event.type)
|
|
{
|
|
default:
|
|
break;
|
|
|
|
case sf::Event::MouseButtonPressed:
|
|
if (_bar.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
|
|
{
|
|
_is_dragging = true;
|
|
_previous_click_position = {static_cast<float>(event.mouseButton.x),
|
|
static_cast<float>(event.mouseButton.y)};
|
|
}
|
|
break;
|
|
|
|
case sf::Event::MouseButtonReleased:
|
|
_is_dragging = false;
|
|
break;
|
|
|
|
case sf::Event::MouseMoved:
|
|
if (_is_dragging)
|
|
{
|
|
float x_mouse_diff = event.mouseMove.x - _previous_click_position.x;
|
|
float y_mouse_diff = event.mouseMove.y - _previous_click_position.y;
|
|
|
|
_previous_click_position = {static_cast<float>(event.mouseMove.x),
|
|
static_cast<float>(event.mouseMove.y)};
|
|
|
|
move({x_mouse_diff, y_mouse_diff});
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Window::update(const sf::Time& dt)
|
|
{
|
|
Widget::update(dt);
|
|
}
|
|
|
|
void Window::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
|
{
|
|
if (_is_visible)
|
|
{
|
|
target.draw(_window_content, states);
|
|
target.draw(_bar, states);
|
|
target.draw(_bar_title, states);
|
|
Widget::draw(target, states);
|
|
}
|
|
}
|
|
|
|
void Window::setRect(const sf::FloatRect& rect)
|
|
{
|
|
_window_content.setPosition(rect.left, rect.top);
|
|
_window_content.setSize({rect.width, rect.height});
|
|
|
|
_bar.setPosition(rect.left, rect.top);
|
|
_bar.setSize({rect.width, 30});
|
|
_bar_title.setPosition(rect.left + 5, rect.top + 5);
|
|
}
|
|
|
|
void Window::setPosition(const sf::Vector2f& position)
|
|
{
|
|
_window_content.setPosition(position);
|
|
_bar.setPosition(position);
|
|
_bar_title.setPosition(position.x + 5, position.y + 5);
|
|
}
|
|
|
|
void Window::move(const sf::Vector2f &delta)
|
|
{
|
|
_window_content.move(delta);
|
|
_bar.move(delta);
|
|
_bar_title.move(delta);
|
|
|
|
Widget::move(delta);
|
|
}
|
|
|
|
bool Window::isUnderMouse(int mouse_x, int mouse_y) const
|
|
{
|
|
return _is_visible && _window_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
|
}
|
|
|
|
void Window::addBarButton(const std::string &text, std::function<void(void)> callback)
|
|
{
|
|
auto b = std::make_shared<PushButton>(text, _font, 20);
|
|
b->setCallback(callback);
|
|
b->setRect({_window_content.getGlobalBounds().left + _window_content.getSize().x - 35,
|
|
_window_content.getGlobalBounds().top,
|
|
30,
|
|
30});
|
|
addChild(b);
|
|
}
|
|
|
|
sf::FloatRect Window::rect() const
|
|
{
|
|
return _window_content.getGlobalBounds();
|
|
}
|
|
|
|
sf::Vector2f Window::position() const
|
|
{
|
|
return _window_content.getPosition();
|
|
}
|