Implement EditorWidget logic

selection
NaiJi ✨ 2 years ago
parent 43e09a6db1
commit cf205810b2

@ -1,5 +1,6 @@
#include "application.h" #include "application.h"
#include "core/inputtype.h" #include "core/inputtype.h"
#include "core/editor.h"
#include "mainmenu.h" #include "mainmenu.h"
#include "gamestate.h" #include "gamestate.h"
@ -32,7 +33,7 @@ Application::Application() :
const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks), _font_holder); const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks), _font_holder);
const auto game_state = std::make_shared<GameState>(_game_window, classic::initGame(_game_window), GameState::Callbacks()); const auto game_state = std::make_shared<GameState>(_game_window, classic::initGame(_game_window), GameState::Callbacks());
const auto editor = std::make_shared<EditorState>(_game_window, std::move(editor_callbacks), std::make_unique<Music>(), _font_holder); const auto editor = std::make_shared<EditorState>(_game_window, classic::initEditor(_game_window), std::move(editor_callbacks), _font_holder);
_states[GUIState::Tag::MAIN_MENU] = main_menu; _states[GUIState::Tag::MAIN_MENU] = main_menu;
_states[GUIState::Tag::GAME] = game_state; _states[GUIState::Tag::GAME] = game_state;

@ -7,13 +7,18 @@
#include "widgets/bpmcalculatorwidget.h" #include "widgets/bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h" #include "tools/bpmcalculator.h"
#include "core/editor.h"
#include <iostream> #include <iostream>
EditorState::EditorState(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder) : EditorState::EditorState(sf::RenderWindow& game_window, std::unique_ptr<Editor>&& editor, Callbacks&& callbacks, const FontHolder& font_holder) :
_font(font_holder.get(Fonts::Id::GUI)), _font(font_holder.get(Fonts::Id::GUI)),
_game_window(game_window), _game_window(game_window),
_callbacks(std::move(callbacks)), _callbacks(std::move(callbacks)),
_music(std::move(music)) _editor(std::move(editor))
{}
EditorState::~EditorState()
{} {}
void EditorState::input(const sf::Event& event) void EditorState::input(const sf::Event& event)
@ -35,12 +40,14 @@ void EditorState::enter()
{ {
auto& group = _group; auto& group = _group;
auto& music = _music; auto& music = _music;
auto& editor = _editor;
_music->openFromFile("Uta-test.flac"); _music->openFromFile("Uta-test.flac");
_music->setVolume(5); _music->setVolume(5);
_bpm_calculator = std::make_shared<BPMCalculator>(_music); _bpm_calculator = std::make_shared<BPMCalculator>(_music);
std::shared_ptr<BPMCalculatorWidget> bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font); std::shared_ptr<BPMCalculatorWidget> bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font);
bpm_widget->init(); bpm_widget->init(_editor);
const auto bpm_widget_callback = [&group, bpm_widget=bpm_widget, &music]() const auto bpm_widget_callback = [&group, bpm_widget=bpm_widget, &music]()
{ {
music->stop(); music->stop();
@ -99,11 +106,29 @@ void EditorState::enter()
menu_bar->setVisibility(true); menu_bar->setVisibility(true);
//auto editor_widget = std::make_unique<EditorWidget>(_edito) EditorWidget::Callbacks callbacks;
callbacks.onDraw = [&editor](sf::RenderTarget& target, sf::RenderStates states)
{
(void)target; (void)states; // fucking shit i am a retard damn fuck fuck
editor->draw();
};
callbacks.onInput = [&editor](const sf::Event& event)
{
editor->input(PlayerInput{0, event});
};
callbacks.onUpdate = [&editor](const sf::Time& dt)
{
editor->update(dt);
};
auto editor_widget = std::make_shared<EditorWidget>(std::move(callbacks));
_group = std::make_shared<Group>(); _group = std::make_shared<Group>();
_group->addChild(menu_bar); _group->addChild(menu_bar);
_group->addChild(bpm_widget); _group->addChild(bpm_widget);
_group->addChild(editor_widget);
} }
void EditorState::leave() void EditorState::leave()

@ -7,6 +7,7 @@
class BPMCalculator; class BPMCalculator;
class Group; class Group;
class Editor;
class EditorState : public GUIState class EditorState : public GUIState
{ {
@ -17,7 +18,8 @@ public:
std::function<void(void)> onLeaveEditorState; std::function<void(void)> onLeaveEditorState;
}; };
explicit EditorState(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music, const FontHolder& font_holder); explicit EditorState(sf::RenderWindow& game_window, std::unique_ptr<Editor>&& editor, Callbacks&& callbacks, const FontHolder& font_holder);
virtual ~EditorState() override;
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() const override; virtual void draw() const override;
@ -34,5 +36,7 @@ private:
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<Group> _group; std::shared_ptr<Group> _group;
std::unique_ptr<Editor> _editor;
}; };

@ -1,5 +1,6 @@
#include "bpmcalculatorwidget.h" #include "bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h" #include "tools/bpmcalculator.h"
#include "core/editor.h"
#include <iostream> #include <iostream>
@ -70,6 +71,7 @@ void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states
_slider->draw(target, states); _slider->draw(target, states);
_button_start->draw(target, states); _button_start->draw(target, states);
_button_stop->draw(target, states); _button_stop->draw(target, states);
_button_apply->draw(target, states);
target.draw(_bpm_value, states); target.draw(_bpm_value, states);
} }
} }
@ -89,6 +91,10 @@ void BPMCalculatorWidget::setRect(const sf::FloatRect& rect)
_button_stop->setPosition({_window_content.getGlobalBounds().left + rect.width / 7, _button_stop->setPosition({_window_content.getGlobalBounds().left + rect.width / 7,
_window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40}); _window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40});
_button_apply->setRect(sf::FloatRect{0, 0, rect.width / 10 * 3, 30});
_button_apply->setPosition({_window_content.getGlobalBounds().left + 20 + (2 * (rect.width / 7)),
_window_content.getGlobalBounds().top + _window_content.getGlobalBounds().height - 40});
_bpm_value.setPosition({_window_content.getGlobalBounds().left + rect.width / 8, _bpm_value.setPosition({_window_content.getGlobalBounds().left + rect.width / 8,
_window_content.getGlobalBounds().top + rect.height / 8 }); _window_content.getGlobalBounds().top + rect.height / 8 });
} }
@ -105,12 +111,13 @@ void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
Window::setPosition(position); Window::setPosition(position);
} }
void BPMCalculatorWidget::init() void BPMCalculatorWidget::init(const std::unique_ptr<Editor>& _editor)
{ {
auto& bpm_calculator = _bpm_calculator; auto& bpm_calculator = _bpm_calculator;
_button_start = std::make_shared<PushButton>("Start", _font); _button_start = std::make_shared<PushButton>("Start", _font);
_button_stop = std::make_shared<PushButton>("Stop", _font); _button_stop = std::make_shared<PushButton>("Stop", _font);
_button_apply = std::make_shared<PushButton>("Apply", _font);
_button_start->setCallback([bpm_calculator, button_start=_button_start, button_stop=_button_stop]() _button_start->setCallback([bpm_calculator, button_start=_button_start, button_stop=_button_stop]()
{ {
@ -128,8 +135,19 @@ void BPMCalculatorWidget::init()
button_stop->setVisibility(false); button_stop->setVisibility(false);
}); });
_button_apply->setCallback([&_editor, bpm_calculator]()
{
BPMSection section;
section.bpm = bpm_calculator->fetchApproximatedInfo().BPM;
section.fraction = 2;
section.offset_start = bpm_calculator->getStartingOffset();
_editor->insertBPMSection(std::move(section));
});
addChild(_button_start); addChild(_button_start);
addChild(_button_stop); addChild(_button_stop);
addChild(_button_apply);
_button_stop->setVisibility(false); _button_stop->setVisibility(false);
} }
@ -139,6 +157,6 @@ void BPMCalculatorWidget::setVisibility(bool is_visible)
Window::setVisibility(is_visible); Window::setVisibility(is_visible);
bool can_stop = _bpm_calculator->calculating(); bool can_stop = _bpm_calculator->calculating();
_button_stop->setVisibility(can_stop); _button_stop->setVisibility(can_stop && is_visible);
_button_start->setVisibility(!can_stop); _button_start->setVisibility(!can_stop && is_visible);
} }

@ -10,6 +10,7 @@
#include <SFML/Audio/SoundBuffer.hpp> #include <SFML/Audio/SoundBuffer.hpp>
class BPMCalculator; class BPMCalculator;
class Editor;
class BPMCalculatorWidget : public Window class BPMCalculatorWidget : public Window
{ {
@ -25,11 +26,13 @@ public:
virtual void setVisibility(bool is_visible = true) override; virtual void setVisibility(bool is_visible = true) override;
void init(); void init(const std::unique_ptr<Editor>& _editor);
private: private:
std::shared_ptr<PushButton> _button_start; std::shared_ptr<PushButton> _button_start;
std::shared_ptr<PushButton> _button_stop; std::shared_ptr<PushButton> _button_stop;
std::shared_ptr<PushButton> _button_apply;
std::shared_ptr<BPMCalculator> _bpm_calculator; std::shared_ptr<BPMCalculator> _bpm_calculator;
std::shared_ptr<BPMSlider> _slider; std::shared_ptr<BPMSlider> _slider;

@ -1,25 +1,25 @@
#include "editorwidget.h" #include "editorwidget.h"
#include "core/editor.h" #include "core/editor.h"
EditorWidget::EditorWidget(const std::shared_ptr<Editor>& editor) : EditorWidget::EditorWidget(Callbacks&& callbacks) :
_editor(editor) _input(std::move(callbacks.onInput)),
_update(std::move(callbacks.onUpdate)),
_draw(std::move(callbacks.onDraw))
{} {}
void EditorWidget::input(const sf::Event& event) void EditorWidget::input(const sf::Event& event)
{ {
_editor->input(PlayerInput{0, event}); _input(event);
} }
void EditorWidget::update(const sf::Time& dt) void EditorWidget::update(const sf::Time& dt)
{ {
_editor->update(dt); _update(dt);
} }
void EditorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const void EditorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
{ {
// !!!!!!!!!!!!!!!!! _draw(target, states);
(void)target; (void)states;
_editor->draw();
} }
void EditorWidget::move(const sf::Vector2f& delta) void EditorWidget::move(const sf::Vector2f& delta)

@ -2,12 +2,18 @@
#include "widget.h" #include "widget.h"
class Editor;
class EditorWidget : public Widget class EditorWidget : public Widget
{ {
public: public:
explicit EditorWidget(const std::shared_ptr<Editor>& editor);
struct Callbacks
{
std::function<void(const sf::Event& event)> onInput;
std::function<void(const sf::Time& dt)> onUpdate;
std::function<void(sf::RenderTarget& target, sf::RenderStates states)> onDraw;
};
explicit EditorWidget(Callbacks&& callbacks);
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;
@ -22,6 +28,8 @@ public:
virtual sf::Vector2f position() const override; virtual sf::Vector2f position() const override;
private: private:
std::shared_ptr<Editor> _editor; std::function<void(const sf::Event& event)> _input;
std::function<void(const sf::Time& dt)> _update;
std::function<void(sf::RenderTarget& target, sf::RenderStates states)> _draw;
}; };

Loading…
Cancel
Save