forked from NaiJi/project-kyoku
Implement Window, Implement BPMCalculationWidget layout
This commit is contained in:
parent
1ac47b8b24
commit
155ec1fca0
|
@ -2,6 +2,7 @@
|
||||||
#include "widgets/button.h"
|
#include "widgets/button.h"
|
||||||
#include "widgets/group.h"
|
#include "widgets/group.h"
|
||||||
#include "widgets/menubar.h"
|
#include "widgets/menubar.h"
|
||||||
|
#include "widgets/window.h"
|
||||||
#include "widgets/bpmcalculatorwidget.h"
|
#include "widgets/bpmcalculatorwidget.h"
|
||||||
#include "tools/bpmcalculator.h"
|
#include "tools/bpmcalculator.h"
|
||||||
|
|
||||||
|
@ -16,38 +17,46 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique
|
||||||
|
|
||||||
void Editor::input(const sf::Event& event)
|
void Editor::input(const sf::Event& event)
|
||||||
{
|
{
|
||||||
_menu_bar->input(event);
|
_group->input(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::update(const sf::Time& dt)
|
void Editor::update(const sf::Time& dt)
|
||||||
{
|
{
|
||||||
_menu_bar->update(dt);
|
_group->update(dt);
|
||||||
_bpm_widget->update(dt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::draw() const
|
void Editor::draw() const
|
||||||
{
|
{
|
||||||
_game_window.draw(*_menu_bar);
|
_game_window.draw(*_group);
|
||||||
_game_window.draw(*_bpm_widget);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::enter()
|
void Editor::enter()
|
||||||
{
|
{
|
||||||
|
auto& group = _group;
|
||||||
|
|
||||||
_bpm_calculator = std::make_shared<BPMCalculator>(_music);
|
_bpm_calculator = std::make_shared<BPMCalculator>(_music);
|
||||||
_bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font);
|
std::shared_ptr<BPMCalculatorWidget> bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font);
|
||||||
|
const auto bpm_widget_callback = [&group, bpm_widget=bpm_widget]()
|
||||||
|
{
|
||||||
|
bpm_widget->setVisibility(false);
|
||||||
|
group->unblock();
|
||||||
|
};
|
||||||
|
|
||||||
const float window_width = _game_window.getSize().x;
|
const float window_width = _game_window.getSize().x;
|
||||||
|
|
||||||
_menu_bar = std::make_shared<MenuBar>(_font);
|
auto menu_bar = std::make_shared<MenuBar>(_font);
|
||||||
|
|
||||||
auto bpm_button = std::make_shared<PushButton>("Calculate BPM", _font);
|
auto bpm_button = std::make_shared<PushButton>("Calculate BPM", _font);
|
||||||
bpm_button->setCallback([&]()
|
|
||||||
|
bpm_button->setCallback([&group, bpm_widget=bpm_widget]()
|
||||||
{
|
{
|
||||||
_bpm_widget->setVisibility(true);
|
group->blockBy(bpm_widget);
|
||||||
|
bpm_widget->setVisibility(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
_bpm_widget->setRect(sf::FloatRect(_game_window.getSize().x / 3, _game_window.getSize().y / 3, _game_window.getSize().x / 3, _game_window.getSize().y / 3));
|
bpm_widget->setRect(sf::FloatRect(_game_window.getSize().x / 3, _game_window.getSize().y / 3, _game_window.getSize().x / 3, _game_window.getSize().y / 3));
|
||||||
_bpm_widget->setVisibility(false);
|
bpm_widget->addBarButton("X", bpm_widget_callback);
|
||||||
|
bpm_widget->setVisibility(false);
|
||||||
|
|
||||||
auto test_menu_2 = std::make_shared<MenuDrop>();
|
auto test_menu_2 = std::make_shared<MenuDrop>();
|
||||||
test_menu_2->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
|
test_menu_2->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
|
||||||
|
@ -65,10 +74,10 @@ void Editor::enter()
|
||||||
auto test_menu = std::make_shared<MenuDrop>();
|
auto test_menu = std::make_shared<MenuDrop>();
|
||||||
test_menu->setRect(sf::FloatRect{0, 0, 200, 27 * 3});
|
test_menu->setRect(sf::FloatRect{0, 0, 200, 27 * 3});
|
||||||
|
|
||||||
_menu_bar->setRect(sf::FloatRect(0, 0, window_width, 27));
|
menu_bar->setRect(sf::FloatRect(0, 0, window_width, 27));
|
||||||
_menu_bar->addRootSubMenu("test", test_menu);
|
menu_bar->addRootSubMenu("test", test_menu);
|
||||||
_menu_bar->addDependentSubmenu(test_menu_2);
|
menu_bar->addDependentSubmenu(test_menu_2);
|
||||||
_menu_bar->addDependentSubmenu(test_menu_3);
|
menu_bar->addDependentSubmenu(test_menu_3);
|
||||||
|
|
||||||
test_cascade_button->setSubmenu(test_menu_2);
|
test_cascade_button->setSubmenu(test_menu_2);
|
||||||
|
|
||||||
|
@ -85,13 +94,15 @@ void Editor::enter()
|
||||||
_music->openFromFile("Uta-test.flac");
|
_music->openFromFile("Uta-test.flac");
|
||||||
_music->setVolume(5);
|
_music->setVolume(5);
|
||||||
|
|
||||||
_menu_bar->setVisibility(true);
|
menu_bar->setVisibility(true);
|
||||||
|
|
||||||
|
_group = std::make_shared<Group>();
|
||||||
|
_group->addChild(menu_bar);
|
||||||
|
_group->addChild(bpm_widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::leave()
|
void Editor::leave()
|
||||||
{
|
{
|
||||||
_menu_bar.reset();
|
_group.reset();
|
||||||
_bpm_calculator.reset();
|
|
||||||
_bpm_widget.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,8 @@
|
||||||
#include "tools/resourceholder.h"
|
#include "tools/resourceholder.h"
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
|
||||||
class MenuBar;
|
|
||||||
class BPMCalculatorWidget;
|
|
||||||
class BPMCalculator;
|
class BPMCalculator;
|
||||||
|
class Group;
|
||||||
|
|
||||||
class Editor : public GUIState
|
class Editor : public GUIState
|
||||||
{
|
{
|
||||||
|
@ -28,13 +27,12 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::shared_ptr<sf::Font> _font;
|
const std::shared_ptr<sf::Font> _font;
|
||||||
std::shared_ptr<MenuBar> _menu_bar;
|
|
||||||
sf::RenderWindow& _game_window;
|
sf::RenderWindow& _game_window;
|
||||||
|
|
||||||
Callbacks _callbacks;
|
Callbacks _callbacks;
|
||||||
|
|
||||||
std::shared_ptr<Music> _music;
|
std::shared_ptr<Music> _music;
|
||||||
std::shared_ptr<BPMCalculator> _bpm_calculator;
|
std::shared_ptr<BPMCalculator> _bpm_calculator;
|
||||||
std::shared_ptr<BPMCalculatorWidget> _bpm_widget;
|
std::shared_ptr<Group> _group;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,10 @@
|
||||||
#include "tools/bpmcalculator.h"
|
#include "tools/bpmcalculator.h"
|
||||||
|
|
||||||
BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font>& font) :
|
BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font>& font) :
|
||||||
|
Window("BPM Calculation", font),
|
||||||
_bpm_calculator(bpm_calculator),
|
_bpm_calculator(bpm_calculator),
|
||||||
_slider(std::make_shared<BPMSlider>(font))
|
_slider(std::make_shared<BPMSlider>())
|
||||||
{
|
{
|
||||||
_widget_window.setFillColor(sf::Color(88, 57, 107));
|
|
||||||
//addChild(_slider);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMCalculatorWidget::input(const sf::Event& event)
|
void BPMCalculatorWidget::input(const sf::Event& event)
|
||||||
|
@ -24,39 +23,38 @@ void BPMCalculatorWidget::input(const sf::Event& event)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget::input(event);
|
Window::input(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMCalculatorWidget::update(const sf::Time& dt)
|
void BPMCalculatorWidget::update(const sf::Time& dt)
|
||||||
{
|
{
|
||||||
Widget::update(dt);
|
Window::update(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
|
Window::draw(target, states);
|
||||||
|
|
||||||
if (_is_visible)
|
if (_is_visible)
|
||||||
{
|
_slider->draw(target, states);
|
||||||
target.draw(_widget_window, states);
|
|
||||||
Widget::draw(target, states);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
|
void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
|
||||||
{
|
{
|
||||||
_widget_window.setPosition(rect.left, rect.top);
|
Window::setRect(rect);
|
||||||
_widget_window.setSize({rect.width, rect.height});
|
_slider->setRect(sf::FloatRect{0, 0, rect.width / 8 * 6, 100});
|
||||||
_slider->setRect(sf::FloatRect{rect.width / 3, rect.height / 2 - 50, rect.width / 3, 100});
|
_slider->setPosition({_window_content.getGlobalBounds().left + rect.width / 8,
|
||||||
|
_window_content.getGlobalBounds().top + rect.height / 8 * 3});
|
||||||
|
}
|
||||||
|
|
||||||
|
void BPMCalculatorWidget::move(const sf::Vector2f &delta)
|
||||||
|
{
|
||||||
|
Window::move(delta);
|
||||||
|
_slider->move(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
|
void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
|
||||||
{
|
{
|
||||||
_widget_window.setPosition(position);
|
Window::setPosition(position);
|
||||||
_slider->setRect(sf::FloatRect{_widget_window.getSize().x / 3,
|
_slider->setPosition(position);
|
||||||
_widget_window.getSize().y / 2 - 50,
|
|
||||||
_widget_window.getSize().x / 3, 100});
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BPMCalculatorWidget::isUnderMouse(int mouse_x, int mouse_y) const
|
|
||||||
{
|
|
||||||
return _is_visible && _widget_window.getGlobalBounds().contains(mouse_x, mouse_y);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "widget.h"
|
#include "window.h"
|
||||||
#include "bpmslider.h"
|
#include "bpmslider.h"
|
||||||
|
#include "pushbutton.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
|
||||||
class BPMCalculator;
|
class BPMCalculator;
|
||||||
|
|
||||||
class BPMCalculatorWidget : public Widget
|
class BPMCalculatorWidget : public Window
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font> &font);
|
explicit BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<sf::Font> &font);
|
||||||
|
@ -18,14 +19,10 @@ public:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
virtual void setPosition(const sf::Vector2f& position) override;
|
virtual void setPosition(const sf::Vector2f& position) override;
|
||||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
virtual void move(const sf::Vector2f& delta) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<BPMCalculator> _bpm_calculator;
|
std::shared_ptr<BPMCalculator> _bpm_calculator;
|
||||||
std::shared_ptr<BPMSlider> _slider;
|
std::shared_ptr<BPMSlider> _slider;
|
||||||
|
|
||||||
sf::RectangleShape _widget_window;
|
|
||||||
|
|
||||||
std::function<void(void)> _on_click_callback;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
#include "bpmslider.h"
|
#include "bpmslider.h"
|
||||||
|
|
||||||
BPMSlider::BPMSlider(const std::shared_ptr<sf::Font> &font)
|
BPMSlider::BPMSlider()
|
||||||
{
|
{
|
||||||
_bpm_value.setFont(*font);
|
_slider_background.setFillColor(sf::Color::Black);
|
||||||
|
_slider_tick.setFillColor(sf::Color::Red);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMSlider::input(const sf::Event& event)
|
void BPMSlider::input(const sf::Event& event)
|
||||||
|
@ -17,6 +18,8 @@ void BPMSlider::update(const sf::Time& dt)
|
||||||
|
|
||||||
void BPMSlider::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
void BPMSlider::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
|
target.draw(_slider_background, states);
|
||||||
|
target.draw(_slider_tick, states);
|
||||||
Widget::draw(target, states);
|
Widget::draw(target, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,11 +27,21 @@ void BPMSlider::setRect(const sf::FloatRect& rect)
|
||||||
{
|
{
|
||||||
_slider_background.setPosition(rect.left, rect.top);
|
_slider_background.setPosition(rect.left, rect.top);
|
||||||
_slider_background.setSize({rect.width, rect.height});
|
_slider_background.setSize({rect.width, rect.height});
|
||||||
|
|
||||||
|
_slider_tick.setPosition(rect.left, rect.top);
|
||||||
|
_slider_tick.setSize({rect.width / 30, rect.height});
|
||||||
}
|
}
|
||||||
|
|
||||||
void BPMSlider::setPosition(const sf::Vector2f& position)
|
void BPMSlider::setPosition(const sf::Vector2f& position)
|
||||||
{
|
{
|
||||||
_slider_background.setPosition(position);
|
_slider_background.setPosition(position);
|
||||||
|
_slider_tick.setPosition(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BPMSlider::move(const sf::Vector2f& delta)
|
||||||
|
{
|
||||||
|
_slider_background.move(delta);
|
||||||
|
_slider_tick.move(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BPMSlider::isUnderMouse(int mouse_x, int mouse_y) const
|
bool BPMSlider::isUnderMouse(int mouse_x, int mouse_y) const
|
||||||
|
|
|
@ -8,19 +8,19 @@
|
||||||
class BPMSlider : public Widget
|
class BPMSlider : public Widget
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit BPMSlider(const std::shared_ptr<sf::Font> &font);
|
explicit BPMSlider();
|
||||||
|
|
||||||
virtual void input(const sf::Event& event) override;
|
virtual void input(const sf::Event& event) override;
|
||||||
virtual void update(const sf::Time& dt) override;
|
virtual void update(const sf::Time& dt) override;
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
virtual void setPosition(const sf::Vector2f& position) 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;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::RectangleShape _slider_background;
|
sf::RectangleShape _slider_background;
|
||||||
sf::RectangleShape _slider_tick;
|
sf::RectangleShape _slider_tick;
|
||||||
sf::Text _bpm_value;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,13 @@ void Button::setPosition(const sf::Vector2f &position)
|
||||||
_button_content.setPosition(position);
|
_button_content.setPosition(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
bool Button::isUnderMouse(int mouse_x, int mouse_y) const
|
||||||
{
|
{
|
||||||
return _is_visible && _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
return _is_visible && _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||||
|
|
|
@ -14,6 +14,7 @@ public:
|
||||||
virtual void update(const sf::Time& dt) override final;
|
virtual void update(const sf::Time& dt) override final;
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override final;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override final;
|
||||||
virtual void setPosition(const sf::Vector2f& position) override final;
|
virtual void setPosition(const sf::Vector2f& position) override final;
|
||||||
|
virtual void move(const sf::Vector2f& delta) override final;
|
||||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override final;
|
||||||
|
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
|
|
|
@ -26,6 +26,14 @@ void Group::setPosition(const sf::Vector2f& position)
|
||||||
_rect.left = position.x;
|
_rect.left = position.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Group::move(const sf::Vector2f& delta)
|
||||||
|
{
|
||||||
|
_rect.top += delta.y;
|
||||||
|
_rect.left += delta.x;
|
||||||
|
|
||||||
|
Widget::move(delta);
|
||||||
|
}
|
||||||
|
|
||||||
bool Group::isUnderMouse(int mouse_x, int mouse_y) const
|
bool Group::isUnderMouse(int mouse_x, int mouse_y) const
|
||||||
{
|
{
|
||||||
return _rect.contains(mouse_x, mouse_y);
|
return _rect.contains(mouse_x, mouse_y);
|
||||||
|
|
|
@ -10,6 +10,7 @@ public:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
virtual void setPosition(const sf::Vector2f& position) 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;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -54,6 +54,16 @@ void MenuBar::setPosition(const sf::Vector2f& position)
|
||||||
_bar_rect.setPosition(position);
|
_bar_rect.setPosition(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MenuBar::move(const sf::Vector2f &delta)
|
||||||
|
{
|
||||||
|
_bar_rect.move(delta);
|
||||||
|
|
||||||
|
Widget::move(delta);
|
||||||
|
|
||||||
|
for (auto& menu : _submenus)
|
||||||
|
menu->move(delta);
|
||||||
|
}
|
||||||
|
|
||||||
bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const
|
bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const
|
||||||
{
|
{
|
||||||
bool bar_under_mouse = _bar_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
bool bar_under_mouse = _bar_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||||
|
|
|
@ -16,6 +16,7 @@ public:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
virtual void setPosition(const sf::Vector2f& position) 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;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||||
virtual void setVisibility(bool is_visible = true) override;
|
virtual void setVisibility(bool is_visible = true) override;
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,16 @@ void MenuDrop::input(const sf::Event& event)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sf::Event::MouseButtonReleased:
|
case sf::Event::MouseButtonReleased:
|
||||||
|
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
||||||
|
{
|
||||||
|
setVisibility(false);
|
||||||
|
for (auto& submenu : _submenus)
|
||||||
|
{
|
||||||
|
submenu->unlock();
|
||||||
|
submenu->setVisibility(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
case sf::Event::MouseMoved:
|
case sf::Event::MouseMoved:
|
||||||
if (!isUnderMouse(event.mouseMove.x, event.mouseMove.y))
|
if (!isUnderMouse(event.mouseMove.x, event.mouseMove.y))
|
||||||
{
|
{
|
||||||
|
@ -66,6 +76,16 @@ void MenuDrop::setPosition(const sf::Vector2f& position)
|
||||||
_content_rect.setPosition(position);
|
_content_rect.setPosition(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MenuDrop::move(const sf::Vector2f& delta)
|
||||||
|
{
|
||||||
|
_content_rect.move(delta);
|
||||||
|
|
||||||
|
Widget::move(delta);
|
||||||
|
|
||||||
|
for (auto& submenu : _submenus)
|
||||||
|
submenu->move(delta);
|
||||||
|
}
|
||||||
|
|
||||||
bool MenuDrop::isUnderMouse(int mouse_x, int mouse_y) const
|
bool MenuDrop::isUnderMouse(int mouse_x, int mouse_y) const
|
||||||
{
|
{
|
||||||
return _is_visible && _content_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
return _is_visible && _content_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||||
|
|
|
@ -15,6 +15,7 @@ public:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
virtual void setPosition(const sf::Vector2f& position) 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;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||||
virtual void setVisibility(bool is_visible = true) override;
|
virtual void setVisibility(bool is_visible = true) override;
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,11 @@ void MenuSeparator::setPosition(const sf::Vector2f& position)
|
||||||
(void)position;
|
(void)position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MenuSeparator::move(const sf::Vector2f& delta)
|
||||||
|
{
|
||||||
|
(void)delta;
|
||||||
|
}
|
||||||
|
|
||||||
bool MenuSeparator::isUnderMouse(int mouse_x, int mouse_y) const
|
bool MenuSeparator::isUnderMouse(int mouse_x, int mouse_y) const
|
||||||
{
|
{
|
||||||
return _is_visible && _rect.contains(mouse_x, mouse_y);
|
return _is_visible && _rect.contains(mouse_x, mouse_y);
|
||||||
|
|
|
@ -12,6 +12,7 @@ public:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||||
virtual void setRect(const sf::FloatRect& rect) override;
|
virtual void setRect(const sf::FloatRect& rect) override;
|
||||||
virtual void setPosition(const sf::Vector2f& position) 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;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -23,6 +23,12 @@ void Widget::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
child->draw(target, states);
|
child->draw(target, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::move(const sf::Vector2f &delta)
|
||||||
|
{
|
||||||
|
for (auto& child : _children)
|
||||||
|
child->move(delta);
|
||||||
|
}
|
||||||
|
|
||||||
void Widget::setVisibility(bool is_visible)
|
void Widget::setVisibility(bool is_visible)
|
||||||
{
|
{
|
||||||
_is_visible = is_visible;
|
_is_visible = is_visible;
|
||||||
|
|
|
@ -16,6 +16,7 @@ public:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
|
||||||
virtual void setRect(const sf::FloatRect& rect) = 0;
|
virtual void setRect(const sf::FloatRect& rect) = 0;
|
||||||
virtual void setPosition(const sf::Vector2f& position) = 0;
|
virtual void setPosition(const sf::Vector2f& position) = 0;
|
||||||
|
virtual void move(const sf::Vector2f& delta) = 0;
|
||||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const = 0;
|
virtual bool isUnderMouse(int mouse_x, int mouse_y) const = 0;
|
||||||
|
|
||||||
virtual void setVisibility(bool is_visible = true);
|
virtual void setVisibility(bool is_visible = true);
|
||||||
|
|
|
@ -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