Adjust editor process to new editor context

This commit is contained in:
NaiJi ✨ 2022-05-22 07:17:15 +03:00
parent c24ecc0acc
commit 90292750fb
10 changed files with 208 additions and 62 deletions

View File

@ -21,13 +21,10 @@ bool Application::init()
[&](){ pushState(GUIState::Tag::ABOUT); }, [&](){ pushState(GUIState::Tag::ABOUT); },
}; };
EditorState::Callbacks editor_callbacks = {[&](){ popState(); }};
About::Callbacks about_callbacks = {[&](){ popState(); }};
const auto main_menu = std::make_shared<MainMenu>(_core_factory, std::move(callbacks)); const auto main_menu = std::make_shared<MainMenu>(_core_factory, std::move(callbacks));
const auto game_state = std::make_shared<GameState>(_core_factory, classic::getGame(_core_factory), GameState::Callbacks()); const auto game_state = std::make_shared<GameState>(_core_factory, classic::getGame(_core_factory), GameState::Callbacks{[&](){ popState(); }});
const auto editor = std::make_shared<EditorState>(_core_factory, classic::getEditor(_core_factory), std::move(editor_callbacks)); const auto editor = std::make_shared<EditorState>(_core_factory, classic::getEditor(_core_factory), EditorState::Callbacks{[&](){ popState(); }});
const auto about = std::make_shared<About>(_core_factory, std::move(about_callbacks)); const auto about = std::make_shared<About>(_core_factory, About::Callbacks{[&](){ popState(); }});
_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;

View File

@ -20,8 +20,11 @@ void GameState::input(const kku::SystemEvent& event)
break; break;
case kku::SystemEvent::Type::KeyRelease: case kku::SystemEvent::Type::KeyRelease:
if (std::get<kku::SystemEvent::Key>(event.data).view == kku::SystemEvent::Key::Code::Space) const auto view = std::get<kku::SystemEvent::Key>(event.data).view;
if (view == kku::SystemEvent::Key::Code::Space)
_music->isPlaying() ? _music->pause() : _music->play(); _music->isPlaying() ? _music->pause() : _music->play();
if (view == kku::SystemEvent::Key::Code::Escape)
_onLeaveGameCallback();
} }
_game->input(kku::GameEvent{_music->fetchOffset(), event}); _game->input(kku::GameEvent{_music->fetchOffset(), event});
@ -48,5 +51,5 @@ void GameState::enter()
void GameState::leave() void GameState::leave()
{ {
_onLeaveGameCallback(); _music->stop();
} }

View File

@ -8,6 +8,7 @@
#include "game/gamecontext.h" #include "game/gamecontext.h"
#include "game/holdmanager.h" #include "game/holdmanager.h"
#include "editor/classiceditor.h" #include "editor/classiceditor.h"
#include "editor/editorcontext.h"
#include "editor/selectionmanager.h" #include "editor/selectionmanager.h"
@ -33,7 +34,11 @@ std::unique_ptr<kku::Editor> classic::getEditor(const std::shared_ptr<kku::CoreF
const auto factory = std::make_shared<ClassicGraphicsFactory>(core_factory); const auto factory = std::make_shared<ClassicGraphicsFactory>(core_factory);
const auto timeline = std::make_shared<kku::Timeline<ClassicNote>>(); const auto timeline = std::make_shared<kku::Timeline<ClassicNote>>();
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset); const auto selection_manager = std::make_shared<SelectionManager<ClassicNote>>();
std::vector<std::shared_ptr<ClassicGraphicsManager>> graphics_managers;
graphics_managers.emplace_back(std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset));
return nullptr;//std::make_unique<ClassicEditor>(timeline, graphics_manager); const auto context = std::make_shared<EditorContext>(selection_manager, std::move(graphics_managers));
return std::make_unique<ClassicEditor>(timeline, context);
} }

View File

@ -8,19 +8,19 @@
#include "graphics/animations/classicdyinganimationscenario.h" #include "graphics/animations/classicdyinganimationscenario.h"
// //
#include "game/classicarrownote.h"
#include "editorcontext.h"
#include "callbacks/callbacksimple.h" #include "callbacks/callbacksimple.h"
ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline, ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager, const std::shared_ptr<EditorContext>& context) :
const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager) :
_timeline(timeline), _timeline(timeline),
_graphics_manager(graphics_manager), _context(context),
_selection_manager(selection_manager),
_selected_type(Type::UP), _selected_type(Type::UP),
_current_time(0), _current_time(0),
_scroll_step(500000) _scroll_step(500000)
{ {
/*kku::microsec starting_beat_offset = 402162; kku::microsec starting_beat_offset = 402162;
int amount_of_beats = 209; int amount_of_beats = 209;
kku::microsec interval = 1412162; kku::microsec interval = 1412162;
kku::microsec tempo_interval = interval / 4; kku::microsec tempo_interval = interval / 4;
@ -41,35 +41,42 @@ ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>&
while (bpm_iterator < bpm_end) while (bpm_iterator < bpm_end)
{ {
ArrowNoteInitializer init; ArrowElement element;
ArrowElementInitializer element;
init.initializer.intervals = input_intervals;
init.initializer.perfect_offset = bpm_iterator;
init.hold = false;
element.element.position = kku::Point(x, 390.f); element.position = kku::Point(x, 390.f);
element.element.falling_curve_interpolation = {}; element.falling_curve_interpolation = {};
element.keys = {kku::SystemEvent::Key::Code::W, element.keys = {kku::SystemEvent::Key::Code::W,
kku::SystemEvent::Key::Code::Up}; kku::SystemEvent::Key::Code::Up};
element.element.type = Type::UP; element.type = Type::UP;
bool hold = false;
if (counter == 0) if (counter == 0)
{ {
init.hold = true; hold = true;
element.keys = {kku::SystemEvent::Key::Code::D, element.keys = {kku::SystemEvent::Key::Code::D,
kku::SystemEvent::Key::Code::Right}; kku::SystemEvent::Key::Code::Right};
element.element.type = Type::RIGHT; element.type = Type::RIGHT;
counter = 13;
} }
--counter; --counter;
init.elements = {element}; ClassicArrowNote::Init init
{
context,
bpm_iterator,
input_intervals,
{element},
hold
};
notes.insert(new ClassicMockNote(std::move(init), _selection_manager)); notes.insert(new ClassicArrowNote(std::move(init)));
bpm_iterator += tempo_interval; bpm_iterator += tempo_interval;
x += 70; x += 70;
@ -78,7 +85,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>&
x = 90.; x = 90.;
} }
_timeline->setNotes(notes);*/ _timeline->setNotes(notes);
} }
kku::microsec ClassicEditor::adjustOffset(kku::microsec offset) const noexcept kku::microsec ClassicEditor::adjustOffset(kku::microsec offset) const noexcept
@ -86,13 +93,13 @@ kku::microsec ClassicEditor::adjustOffset(kku::microsec offset) const noexcept
const auto& section = getBPMSectionAt(offset); const auto& section = getBPMSectionAt(offset);
const kku::microsec actual_offset = offset - section.offset_start; const kku::microsec actual_offset = offset - section.offset_start;
return actual_offset + (actual_offset % section.interval); return actual_offset + (actual_offset % (section.interval + 1));
} }
void ClassicEditor::input(kku::GameEvent&& input) void ClassicEditor::input(kku::GameEvent&& input)
{ {
_current_time = input.timestamp; _current_time = input.timestamp;
//const auto& event = input.event; const auto& event = input.event;
switch (input.event.type) switch (input.event.type)
{ {
@ -105,7 +112,7 @@ void ClassicEditor::input(kku::GameEvent&& input)
const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data); const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data);
if (key_data.view == kku::SystemEvent::Key::Code::LControl) if (key_data.view == kku::SystemEvent::Key::Code::LControl)
{ {
_selection_manager->enableMultiselection(true); _context->getSelectionManager()->enableMultiselection(true);
} }
break; break;
} }
@ -115,39 +122,42 @@ void ClassicEditor::input(kku::GameEvent&& input)
const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data); const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data);
if (key_data.view == kku::SystemEvent::Key::Code::LControl) if (key_data.view == kku::SystemEvent::Key::Code::LControl)
{ {
_selection_manager->enableMultiselection(false); _context->getSelectionManager()->enableMultiselection(false);
} }
break; break;
} }
case kku::SystemEvent::Type::MousePress: case kku::SystemEvent::Type::MousePress:
{ {
/*const auto note = _timeline->getNoteBy(_current_time); const auto note = _timeline->getNoteBy(_current_time);
if (_timeline->isExpired(note) && !_bpm_sections.empty() && _current_time >= (*_bpm_sections.begin()).offset_start) if (_timeline->isExpired(note) && !_bpm_sections.empty() && _current_time >= (*_bpm_sections.begin()).offset_start)
{ {
ArrowNoteInitializer init; ArrowElement element;
ArrowElementInitializer element;
init.initializer.intervals = {};
init.initializer.perfect_offset = input.timestamp;
init.hold = false;
element.element.position = std::get<kku::SystemEvent::Mouse>(event.data).position; element.position = std::get<kku::SystemEvent::Mouse>(event.data).position;
element.element.falling_curve_interpolation = {}; element.falling_curve_interpolation = {};
element.keys = {kku::SystemEvent::Key::Code::W, element.keys = {kku::SystemEvent::Key::Code::W,
kku::SystemEvent::Key::Code::Up}; kku::SystemEvent::Key::Code::Up};
element.element.type = Type::UP; element.type = Type::UP;
init.elements = { element }; ClassicArrowNote::Init init
{
_context,
_current_time,
{},
{element},
false
};
_timeline->insertNote(new ClassicMockNote(std::move(init), _selection_manager)); _timeline->insertNote(new ClassicArrowNote(std::move(init)));
} }
if (!_selection_manager->isMultiselectionEnabled()) if (!_context->getSelectionManager()->isMultiselectionEnabled())
_selection_manager->discard(); _context->getSelectionManager()->discard();
_graphics_manager->input(std::move(input));*/ //_graphics_manager->input(std::move(input));
break; break;
} }
@ -158,11 +168,12 @@ void ClassicEditor::input(kku::GameEvent&& input)
void ClassicEditor::update(kku::UpdateData&& updatedata) void ClassicEditor::update(kku::UpdateData&& updatedata)
{ {
_timeline->update(updatedata.timestamp); _timeline->update(updatedata.timestamp);
_context->updateGraphics(updatedata.timestamp);
} }
void ClassicEditor::display() const void ClassicEditor::display() const
{ {
_graphics_manager->display(); _context->displayGraphics();
} }
void ClassicEditor::recalculate(const kku::microsec& timestamp) void ClassicEditor::recalculate(const kku::microsec& timestamp)

View File

@ -9,14 +9,13 @@
#include "classicmode/classicnote.h" #include "classicmode/classicnote.h"
#include "classicmode/classicactions.h" #include "classicmode/classicactions.h"
class ClassicGraphicsManager; class EditorContext;
class ClassicEditor : public kku::Editor class ClassicEditor : public kku::Editor
{ {
public: public:
explicit ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline, explicit ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager, const std::shared_ptr<EditorContext>& context);
const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager);
virtual void input(kku::GameEvent&& input) override; virtual void input(kku::GameEvent&& input) override;
virtual void update(kku::UpdateData&& updatedata) override; virtual void update(kku::UpdateData&& updatedata) override;
@ -29,8 +28,8 @@ private:
inline kku::microsec adjustOffset(kku::microsec offset) const noexcept; inline kku::microsec adjustOffset(kku::microsec offset) const noexcept;
const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline; const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline;
const std::shared_ptr<ClassicGraphicsManager> _graphics_manager; const std::shared_ptr<EditorContext> _context;
const std::shared_ptr<SelectionManager<ClassicNote>> _selection_manager;
Type _selected_type; Type _selected_type;
kku::microsec _current_time; kku::microsec _current_time;

View File

@ -0,0 +1,103 @@
#include "editorcontext.h"
#include "game/classicarrownote.h"
#include "graphics/animations/classicanimationscenario.h"
#include "graphics/classicgraphicsmanager.h"
#include "graphics/classicnotegraphics.h"
EditorContext::EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager,
std::vector<std::shared_ptr<ClassicGraphicsManager>>&& graphics_managers) :
_selection_manager(selection_manager),
_graphics_managers(std::move(graphics_managers))
{}
void EditorContext::input(ClassicArrowNote *note, kku::GameEvent&& input) const
{
(void)note;
switch (input.event.type)
{
default:
break;
/*case kku::SystemEvent::Type::MousePress:
{
bool selection_changed = false;
std::size_t amount_selected = 0;
const auto position = std::get<kku::SystemEvent::Mouse>(input.event.data).position;
for (auto& element : note->getElements())
{
if (element.sprite->getRectangle()->contains(position))
{
element.selected = !element.selected;
selection_changed = true;
if (element.selected)
++amount_selected;
}
}
if (selection_changed)
{
if (amount_selected > 0)
_selection_manager->fetch(note);
else
_selection_manager->remove(note);
}
break;
}*/
}
}
void EditorContext::update(ClassicArrowNote *note, const kku::microsec& music_offset) const
{
auto& elements = note->getElements();
switch (note->getState())
{
default: return;
break;
case ClassicArrowNote::State::INITIAL:
note->setState(ClassicArrowNote::State::FLYING);
for (auto& manager : _graphics_managers)
manager->update(music_offset, note);
break;
case ClassicArrowNote::State::FLYING:
if (!note->isActive(music_offset) && music_offset > note->getPerfectOffset())
{
note->setState(ClassicArrowNote::State::DYING);
for (auto& element : elements)
element.animations[note->getState()]->launch(element.sprite, music_offset, note->getPerfectOffset());
}
break;
case ClassicArrowNote::State::DYING:
if (elements[0].animations[note->getState()]->isDone())
note->setState(ClassicArrowNote::State::DEAD);
break;
}
for (auto& element : elements)
if (element.animations[note->getState()])
element.animations[note->getState()]->update(music_offset);
}
std::shared_ptr<SelectionManager<ClassicNote>> EditorContext::getSelectionManager() const
{
return _selection_manager;
}
void EditorContext::updateGraphics(const kku::microsec& music_offset)
{
for (auto& manager : _graphics_managers)
manager->update(music_offset);
}
void EditorContext::displayGraphics() const
{
for (const auto& manager : _graphics_managers)
manager->display();
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <memory>
#include <vector>
#include "classicmode/classicnote.h"
#include "classicmode/context.h"
#include "editor/selectionmanager.h"
class ClassicGraphicsManager;
class EditorContext : public Context
{
public:
explicit EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager,
std::vector<std::shared_ptr<ClassicGraphicsManager>>&& graphics_managers);
virtual void input(ClassicArrowNote *note, kku::GameEvent&& input) const override;
virtual void update(ClassicArrowNote *note, const kku::microsec &music_offset) const override;
void updateGraphics(const kku::microsec& music_offset);
void displayGraphics() const;
std::shared_ptr<SelectionManager<ClassicNote>> getSelectionManager() const;
private:
const std::shared_ptr<SelectionManager<ClassicNote>> _selection_manager;
const std::vector<std::shared_ptr<ClassicGraphicsManager>> _graphics_managers;
};

View File

@ -8,14 +8,11 @@ class SelectionManager
{ {
public: public:
SelectionManager() : SelectionManager() :
_multiselection_enabled(false), _multiselection_enabled(false)
deselection([](T* thing) { thing->deselect(); })
{} {}
// Remove whole selection completely
void discard() void discard()
{ {
apply(deselection);
_selected_things.clear(); _selected_things.clear();
} }
@ -44,6 +41,15 @@ public:
} }
} }
bool isSelected(const T * const thing) const
{
for (const auto& selected_thing : thing)
if (thing == selected_thing)
return true;
return false;
}
void enableMultiselection(bool enable = true) void enableMultiselection(bool enable = true)
{ {
_multiselection_enabled = enable; _multiselection_enabled = enable;
@ -63,6 +69,4 @@ public:
private: private:
std::vector<T*> _selected_things; std::vector<T*> _selected_things;
bool _multiselection_enabled; bool _multiselection_enabled;
const std::function<void(T*)> deselection;
}; };

View File

@ -11,9 +11,6 @@ ClassicGame::ClassicGame(const std::shared_ptr<kku::Timeline<ClassicNote>>& time
_context(context) _context(context)
{} {}
ClassicGame::~ClassicGame()
{}
void ClassicGame::run() void ClassicGame::run()
{ {
auto beatmap = classic::createBeatmap("aa", _context); auto beatmap = classic::createBeatmap("aa", _context);

View File

@ -16,7 +16,6 @@ class ClassicGame final : public kku::Game
public: public:
explicit ClassicGame(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline, explicit ClassicGame(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<GameContext>& context); const std::shared_ptr<GameContext>& context);
virtual ~ClassicGame() override;
virtual void run() override; virtual void run() override;