Implement EditorWidget logic

This commit is contained in:
NaiJi ✨ 2021-12-02 19:02:13 +03:00
parent 43e09a6db1
commit cf205810b2
7 changed files with 80 additions and 21 deletions

View File

@ -1,5 +1,6 @@
#include "application.h"
#include "core/inputtype.h"
#include "core/editor.h"
#include "mainmenu.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 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::GAME] = game_state;

View File

@ -7,13 +7,18 @@
#include "widgets/bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h"
#include "core/editor.h"
#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)),
_game_window(game_window),
_callbacks(std::move(callbacks)),
_music(std::move(music))
_editor(std::move(editor))
{}
EditorState::~EditorState()
{}
void EditorState::input(const sf::Event& event)
@ -35,12 +40,14 @@ void EditorState::enter()
{
auto& group = _group;
auto& music = _music;
auto& editor = _editor;
_music->openFromFile("Uta-test.flac");
_music->setVolume(5);
_bpm_calculator = std::make_shared<BPMCalculator>(_music);
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]()
{
music->stop();
@ -99,11 +106,29 @@ void EditorState::enter()
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->addChild(menu_bar);
_group->addChild(bpm_widget);
_group->addChild(editor_widget);
}
void EditorState::leave()

View File

@ -7,6 +7,7 @@
class BPMCalculator;
class Group;
class Editor;
class EditorState : public GUIState
{
@ -17,7 +18,8 @@ public:
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 update(const sf::Time& dt) override;
virtual void draw() const override;
@ -34,5 +36,7 @@ private:
std::shared_ptr<Music> _music;
std::shared_ptr<BPMCalculator> _bpm_calculator;
std::shared_ptr<Group> _group;
std::unique_ptr<Editor> _editor;
};

View File

@ -1,5 +1,6 @@
#include "bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h"
#include "core/editor.h"
#include <iostream>
@ -70,6 +71,7 @@ void BPMCalculatorWidget::draw(sf::RenderTarget& target, sf::RenderStates states
_slider->draw(target, states);
_button_start->draw(target, states);
_button_stop->draw(target, states);
_button_apply->draw(target, 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,
_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,
_window_content.getGlobalBounds().top + rect.height / 8 });
}
@ -105,12 +111,13 @@ void BPMCalculatorWidget::setPosition(const sf::Vector2f &position)
Window::setPosition(position);
}
void BPMCalculatorWidget::init()
void BPMCalculatorWidget::init(const std::unique_ptr<Editor>& _editor)
{
auto& bpm_calculator = _bpm_calculator;
_button_start = std::make_shared<PushButton>("Start", _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]()
{
@ -128,8 +135,19 @@ void BPMCalculatorWidget::init()
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_stop);
addChild(_button_apply);
_button_stop->setVisibility(false);
}
@ -139,6 +157,6 @@ void BPMCalculatorWidget::setVisibility(bool is_visible)
Window::setVisibility(is_visible);
bool can_stop = _bpm_calculator->calculating();
_button_stop->setVisibility(can_stop);
_button_start->setVisibility(!can_stop);
_button_stop->setVisibility(can_stop && is_visible);
_button_start->setVisibility(!can_stop && is_visible);
}

View File

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

View File

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

View File

@ -2,12 +2,18 @@
#include "widget.h"
class Editor;
class EditorWidget : public Widget
{
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 update(const sf::Time& dt) override;
@ -22,6 +28,8 @@ public:
virtual sf::Vector2f position() const override;
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;
};