forked from NaiJi/project-kyoku
Implement Window, Implement BPMCalculationWidget layout
parent
1ac47b8b24
commit
155ec1fca0
@ -0,0 +1,110 @@
|
||||
#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);
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
|
||||
class Window : public Widget
|
||||
{
|
||||
public:
|
||||
explicit Window(const std::string& text, const std::shared_ptr<sf::Font> &font);
|
||||
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update(const sf::Time& dt) override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
virtual void setRect(const sf::FloatRect& rect) override;
|
||||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual void move(const sf::Vector2f& delta) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final;
|
||||
|
||||
void addBarButton(const std::string& text, std::function<void(void)> callback);
|
||||
|
||||
protected:
|
||||
sf::RectangleShape _bar;
|
||||
sf::RectangleShape _window_content;
|
||||
sf::Text _bar_title;
|
||||
|
||||
const std::shared_ptr<sf::Font> _font;
|
||||
|
||||
private:
|
||||
bool _is_dragging;
|
||||
sf::Vector2f _previous_click_position;
|
||||
};
|
||||
|
Loading…
Reference in New Issue