Move Editor initialization from Constructor to ::Enter()

This commit is contained in:
NaiJi ✨ 2021-08-30 22:07:13 +03:00
parent 80bc47e425
commit dbe26c5d9a
6 changed files with 46 additions and 36 deletions

View File

@ -8,18 +8,39 @@
#include <iostream>
Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder) :
_font(font_holder.get(Fonts::Id::GUI)),
_game_window(game_window),
_music(std::move(music)),
_bpm_calculator(std::make_shared<BPMCalculator>(_music)),
_bpm_widget(std::make_shared<BPMCalculatorWidget>(_bpm_calculator, font_holder.get(Fonts::Id::GUI)))
_callbacks(std::move(callbacks)),
_music(std::move(music))
{}
void Editor::input(const sf::Event& event)
{
(void)callbacks;
const float window_width = game_window.getSize().x;
//const float window_height = game_window.getSize().y;
_menu_bar->input(event);
}
_menu_bar = std::make_shared<MenuBar>(font_holder.get(Fonts::Id::GUI));
void Editor::update(const sf::Time& dt)
{
_menu_bar->update(dt);
_bpm_widget->update(dt);
}
auto bpm_button = std::make_shared<PushButton>("Play song :)", font_holder.get(Fonts::Id::GUI));
void Editor::draw() const
{
_game_window.draw(*_menu_bar);
_game_window.draw(*_bpm_widget);
}
void Editor::enter()
{
_bpm_calculator = std::make_shared<BPMCalculator>(_music);
_bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font);
const float window_width = _game_window.getSize().x;
_menu_bar = std::make_shared<MenuBar>(_font);
auto bpm_button = std::make_shared<PushButton>("Calculate BPM", _font);
bpm_button->setCallback([&]()
{
_bpm_widget->setVisibility(true);
@ -34,12 +55,12 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique
auto test_menu_3 = std::make_shared<MenuDrop>();
test_menu_3->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
auto test_cascade_button = std::make_shared<CascadeMenuButton>("Show submenu", font_holder.get(Fonts::Id::GUI));
auto test_cascade_button = std::make_shared<CascadeMenuButton>("Show submenu", _font);
auto test_cascade_button_2 = std::make_shared<CascadeMenuButton>("Show submenu 2", font_holder.get(Fonts::Id::GUI));
auto test_cascade_button_2 = std::make_shared<CascadeMenuButton>("Show submenu 2", _font);
auto quit_button = std::make_shared<PushButton>("Quit", font_holder.get(Fonts::Id::GUI));
quit_button->setCallback(callbacks.onLeaveEditorState);
auto quit_button = std::make_shared<PushButton>("Quit", _font);
quit_button->setCallback(_callbacks.onLeaveEditorState);
auto test_menu = std::make_shared<MenuDrop>();
test_menu->setRect(sf::FloatRect{0, 0, 200, 27 * 3});
@ -61,32 +82,14 @@ Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique
_music->openFromFile("Uta-test.flac");
_music->setVolume(5);
}
void Editor::input(const sf::Event& event)
{
_menu_bar->input(event);
}
void Editor::update(const sf::Time& dt)
{
_menu_bar->update(dt);
_bpm_widget->update(dt);
}
void Editor::draw() const
{
_game_window.draw(*_menu_bar);
_game_window.draw(*_bpm_widget);
}
void Editor::enter()
{
_menu_bar->setVisibility();
_menu_bar->setVisibility(true);
}
void Editor::leave()
{
_menu_bar->setVisibility(false);
_menu_bar.reset();
_bpm_calculator.reset();
_bpm_widget.reset();
}

View File

@ -27,9 +27,12 @@ public:
virtual void leave() override;
private:
const std::shared_ptr<sf::Font> _font;
std::shared_ptr<MenuBar> _menu_bar;
sf::RenderWindow& _game_window;
Callbacks _callbacks;
std::shared_ptr<Music> _music;
std::shared_ptr<BPMCalculator> _bpm_calculator;
std::shared_ptr<BPMCalculatorWidget> _bpm_widget;

View File

@ -61,6 +61,7 @@ void CascadeMenuButton::resetRect()
{
_submenu->setPosition({_button_content.getPosition().x + _button_content.getSize().x,
_button_content.getPosition().y});
_submenu->setVisibility(false);
}
}

View File

@ -3,6 +3,7 @@
MenuBar::MenuBar(const std::shared_ptr<sf::Font>& font) :
_font(font),
_amount_buttons(0),
_button_width(170)
{
_bar_rect.setFillColor(sf::Color(88, 57, 107));

View File

@ -6,7 +6,7 @@ MenuDrop::MenuDrop() :
_button_height(27),
_button_index(0)
{
setVisibility(false);
_is_visible = false;
_content_rect.setFillColor(sf::Color(200, 200, 200));
}
@ -87,9 +87,10 @@ void MenuDrop::addPushButton(const std::shared_ptr<PushButton>& button)
void MenuDrop::addCascadeButton(const std::shared_ptr<CascadeMenuButton>& button)
{
auto& submenu = button->submenu();
submenu->setParent(_parent);
_submenus.emplace_back(submenu);
add(button);
submenu->setParent(_parent);
}
void MenuDrop::addSeparator()

View File

@ -1,4 +1,5 @@
#include "pushbutton.h"
#include <iostream>
PushButton::PushButton(const std::string& text, const std::shared_ptr<sf::Font> &font, unsigned int font_size) :
Button(text, font, font_size),