2021-10-05 20:48:28 +02:00
|
|
|
#include "editorstate.h"
|
2021-08-12 21:10:52 +02:00
|
|
|
#include "widgets/button.h"
|
|
|
|
#include "widgets/group.h"
|
2021-08-16 20:54:03 +02:00
|
|
|
#include "widgets/menubar.h"
|
2021-08-31 21:09:34 +02:00
|
|
|
#include "widgets/window.h"
|
2021-11-17 23:14:52 +01:00
|
|
|
#include "widgets/editorwidget.h"
|
2021-08-27 19:40:48 +02:00
|
|
|
#include "widgets/bpmcalculatorwidget.h"
|
2021-08-12 21:10:52 +02:00
|
|
|
#include "tools/bpmcalculator.h"
|
|
|
|
|
2021-12-02 17:02:13 +01:00
|
|
|
#include "core/editor.h"
|
|
|
|
|
2021-08-12 21:10:52 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2021-12-02 17:02:13 +01:00
|
|
|
EditorState::EditorState(sf::RenderWindow& game_window, std::unique_ptr<Editor>&& editor, Callbacks&& callbacks, const FontHolder& font_holder) :
|
2021-08-30 21:07:13 +02:00
|
|
|
_font(font_holder.get(Fonts::Id::GUI)),
|
2021-08-12 21:10:52 +02:00
|
|
|
_game_window(game_window),
|
2021-08-30 21:07:13 +02:00
|
|
|
_callbacks(std::move(callbacks)),
|
2021-12-02 17:02:13 +01:00
|
|
|
_editor(std::move(editor))
|
|
|
|
{}
|
|
|
|
|
|
|
|
EditorState::~EditorState()
|
2021-08-30 21:07:13 +02:00
|
|
|
{}
|
|
|
|
|
2021-10-05 20:48:28 +02:00
|
|
|
void EditorState::input(const sf::Event& event)
|
2021-08-30 21:07:13 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
_group->input(event);
|
2021-08-30 21:07:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:48:28 +02:00
|
|
|
void EditorState::update(const sf::Time& dt)
|
2021-08-30 21:07:13 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
_group->update(dt);
|
2021-08-30 21:07:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:48:28 +02:00
|
|
|
void EditorState::draw() const
|
2021-08-30 21:07:13 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
_game_window.draw(*_group);
|
2021-08-30 21:07:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:48:28 +02:00
|
|
|
void EditorState::enter()
|
2021-08-12 21:10:52 +02:00
|
|
|
{
|
2021-12-03 20:21:27 +01:00
|
|
|
_music.openFromFile("Uta-test.flac");
|
|
|
|
_music.setVolume(5);
|
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
auto& group = _group;
|
2021-09-01 21:04:18 +02:00
|
|
|
auto& music = _music;
|
2021-12-02 17:02:13 +01:00
|
|
|
auto& editor = _editor;
|
|
|
|
|
2021-12-03 20:21:27 +01:00
|
|
|
_bpm_calculator = std::make_shared<BPMCalculator>();
|
|
|
|
auto& bpm_calculator = _bpm_calculator;
|
2021-08-31 21:09:34 +02:00
|
|
|
std::shared_ptr<BPMCalculatorWidget> bpm_widget = std::make_shared<BPMCalculatorWidget>(_bpm_calculator, _font);
|
2021-12-03 20:21:27 +01:00
|
|
|
|
|
|
|
auto button_start = std::make_shared<PushButton>("Start", _font);
|
|
|
|
auto button_stop = std::make_shared<PushButton>("Stop", _font);
|
|
|
|
auto button_apply = std::make_shared<PushButton>("Apply", _font);
|
|
|
|
|
|
|
|
button_start->setCallback([bpm_calculator, button_start, button_stop, &music]()
|
|
|
|
{
|
|
|
|
music.play();
|
|
|
|
bpm_calculator->start();
|
|
|
|
button_start->setVisibility(false);
|
|
|
|
button_stop->setVisibility(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
button_stop->setCallback([bpm_calculator, button_start, button_stop, &music]()
|
|
|
|
{
|
|
|
|
music.stop();
|
|
|
|
bpm_calculator->stop();
|
|
|
|
button_start->setVisibility(true);
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
|
|
|
|
BPMCalculatorWidget::Init bpm_widget_init;
|
|
|
|
bpm_widget_init.stop = button_stop;
|
|
|
|
bpm_widget_init.apply = button_apply;
|
|
|
|
bpm_widget_init.start = button_start;
|
|
|
|
bpm_widget_init.current_time = [&music]() -> microsec { return music.fetchOffset(); };
|
|
|
|
|
|
|
|
bpm_widget->init(std::move(bpm_widget_init));
|
2021-09-01 21:04:18 +02:00
|
|
|
const auto bpm_widget_callback = [&group, bpm_widget=bpm_widget, &music]()
|
2021-08-31 21:09:34 +02:00
|
|
|
{
|
2021-12-03 20:21:27 +01:00
|
|
|
music.stop();
|
2021-08-31 21:09:34 +02:00
|
|
|
bpm_widget->setVisibility(false);
|
|
|
|
group->unblock();
|
|
|
|
};
|
2021-08-30 21:07:13 +02:00
|
|
|
|
|
|
|
const float window_width = _game_window.getSize().x;
|
2021-08-12 21:10:52 +02:00
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
auto menu_bar = std::make_shared<MenuBar>(_font);
|
2021-08-26 19:41:16 +02:00
|
|
|
|
2021-08-30 21:07:13 +02:00
|
|
|
auto bpm_button = std::make_shared<PushButton>("Calculate BPM", _font);
|
2021-08-31 21:09:34 +02:00
|
|
|
|
|
|
|
bpm_button->setCallback([&group, bpm_widget=bpm_widget]()
|
2021-08-26 18:54:30 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
group->blockBy(bpm_widget);
|
|
|
|
bpm_widget->setVisibility(true);
|
2021-08-26 18:54:30 +02:00
|
|
|
});
|
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
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->addBarButton("X", bpm_widget_callback);
|
|
|
|
bpm_widget->setVisibility(false);
|
2021-08-27 19:40:48 +02:00
|
|
|
|
2021-08-26 20:22:19 +02:00
|
|
|
auto test_menu_2 = std::make_shared<MenuDrop>();
|
|
|
|
test_menu_2->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
|
|
|
|
|
2021-08-27 19:40:48 +02:00
|
|
|
auto test_menu_3 = std::make_shared<MenuDrop>();
|
|
|
|
test_menu_3->setRect(sf::FloatRect{0, 0, 200, 27 * 5});
|
|
|
|
|
2021-08-30 21:07:13 +02:00
|
|
|
auto test_cascade_button = std::make_shared<CascadeMenuButton>("Show submenu", _font);
|
2021-08-26 20:22:19 +02:00
|
|
|
|
2021-08-30 21:07:13 +02:00
|
|
|
auto test_cascade_button_2 = std::make_shared<CascadeMenuButton>("Show submenu 2", _font);
|
2021-08-27 19:40:48 +02:00
|
|
|
|
2021-08-30 21:07:13 +02:00
|
|
|
auto quit_button = std::make_shared<PushButton>("Quit", _font);
|
|
|
|
quit_button->setCallback(_callbacks.onLeaveEditorState);
|
2021-08-26 18:54:30 +02:00
|
|
|
|
|
|
|
auto test_menu = std::make_shared<MenuDrop>();
|
2021-08-26 20:22:19 +02:00
|
|
|
test_menu->setRect(sf::FloatRect{0, 0, 200, 27 * 3});
|
2021-08-26 18:54:30 +02:00
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
menu_bar->setRect(sf::FloatRect(0, 0, window_width, 27));
|
|
|
|
menu_bar->addRootSubMenu("test", test_menu);
|
|
|
|
menu_bar->addDependentSubmenu(test_menu_2);
|
|
|
|
menu_bar->addDependentSubmenu(test_menu_3);
|
2021-08-26 18:54:30 +02:00
|
|
|
|
2021-08-26 20:22:19 +02:00
|
|
|
test_cascade_button->setSubmenu(test_menu_2);
|
|
|
|
|
2021-08-26 18:54:30 +02:00
|
|
|
test_menu->addPushButton(bpm_button);
|
2021-08-26 20:22:19 +02:00
|
|
|
test_menu->addCascadeButton(test_cascade_button);
|
2021-08-26 18:54:30 +02:00
|
|
|
test_menu->addPushButton(quit_button);
|
|
|
|
|
2021-08-26 20:51:49 +02:00
|
|
|
test_cascade_button->resetRect();
|
2021-08-26 20:22:19 +02:00
|
|
|
|
2021-08-27 19:40:48 +02:00
|
|
|
test_cascade_button_2->setSubmenu(test_menu_3);
|
|
|
|
test_menu_2->addCascadeButton(test_cascade_button_2);
|
|
|
|
test_cascade_button_2->resetRect();
|
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
menu_bar->setVisibility(true);
|
|
|
|
|
2021-12-02 17:02:13 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2021-12-03 20:21:27 +01:00
|
|
|
callbacks.onInput = [&editor, &music](const sf::Event& event)
|
2021-12-02 17:02:13 +01:00
|
|
|
{
|
2021-12-08 19:00:47 +01:00
|
|
|
if (event.key.code == sf::Keyboard::Space && event.type == sf::Event::KeyReleased)
|
|
|
|
music.isPaused() ? music.play() : music.pause();
|
|
|
|
else if (event.type == sf::Event::MouseWheelScrolled)
|
|
|
|
{
|
2021-12-06 20:18:04 +01:00
|
|
|
music.moveOffset(event.mouseWheelScroll.delta > 0 ? 500000 : -500000);
|
2021-12-08 19:00:47 +01:00
|
|
|
editor->recalculate(music.fetchOffset());
|
|
|
|
}
|
2021-12-06 20:18:04 +01:00
|
|
|
else
|
|
|
|
editor->input(PlayerInput{music.fetchOffset(), event});
|
2021-12-02 17:02:13 +01:00
|
|
|
};
|
|
|
|
|
2021-12-03 20:21:27 +01:00
|
|
|
callbacks.onUpdate = [&editor, &music](const sf::Time& dt)
|
2021-12-02 17:02:13 +01:00
|
|
|
{
|
2021-12-03 20:21:27 +01:00
|
|
|
editor->update(UpdateData{music.fetchOffset(), dt});
|
2021-12-02 17:02:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
auto editor_widget = std::make_shared<EditorWidget>(std::move(callbacks));
|
2021-11-17 23:14:52 +01:00
|
|
|
|
2021-08-31 21:09:34 +02:00
|
|
|
_group = std::make_shared<Group>();
|
2021-12-06 20:18:04 +01:00
|
|
|
_group->addChild(editor_widget);
|
2021-08-31 21:09:34 +02:00
|
|
|
_group->addChild(menu_bar);
|
|
|
|
_group->addChild(bpm_widget);
|
2021-08-12 21:10:52 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:48:28 +02:00
|
|
|
void EditorState::leave()
|
2021-08-12 21:10:52 +02:00
|
|
|
{
|
2021-08-31 21:09:34 +02:00
|
|
|
_group.reset();
|
2021-09-08 21:05:56 +02:00
|
|
|
_bpm_calculator.reset();
|
2021-08-12 21:10:52 +02:00
|
|
|
}
|