From 97d2216a2efecb7d489b3b6d74b61158466adb28 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Sun, 22 May 2022 07:17:15 +0300 Subject: [PATCH] Adjust editor process to new editor context --- src/modes/classicmode/classicfactory.cpp | 9 +- .../classicmode/editor/classiceditor.cpp | 84 +++++++------- src/modes/classicmode/editor/classiceditor.h | 9 +- .../classicmode/editor/editorcontext.cpp | 103 ++++++++++++++++++ src/modes/classicmode/editor/editorcontext.h | 28 +++++ .../classicmode/editor/selectionmanager.h | 16 ++- src/modes/classicmode/game/classicgame.cpp | 3 - src/modes/classicmode/game/classicgame.h | 1 - 8 files changed, 199 insertions(+), 54 deletions(-) create mode 100644 src/modes/classicmode/editor/editorcontext.cpp create mode 100644 src/modes/classicmode/editor/editorcontext.h diff --git a/src/modes/classicmode/classicfactory.cpp b/src/modes/classicmode/classicfactory.cpp index 11348b2..5921ebe 100644 --- a/src/modes/classicmode/classicfactory.cpp +++ b/src/modes/classicmode/classicfactory.cpp @@ -8,6 +8,7 @@ #include "game/gamecontext.h" #include "game/holdmanager.h" #include "editor/classiceditor.h" +#include "editor/editorcontext.h" #include "editor/selectionmanager.h" @@ -33,7 +34,11 @@ std::unique_ptr classic::getEditor(const std::shared_ptr(core_factory); const auto timeline = std::make_shared>(); - const auto graphics_manager = std::make_shared(timeline, factory, visibility_offset); + const auto selection_manager = std::make_shared>(); + std::vector> graphics_managers; + graphics_managers.emplace_back(std::make_shared(timeline, factory, visibility_offset)); - return nullptr;//std::make_unique(timeline, graphics_manager); + const auto context = std::make_shared(selection_manager, std::move(graphics_managers)); + + return std::make_unique(timeline, context); } diff --git a/src/modes/classicmode/editor/classiceditor.cpp b/src/modes/classicmode/editor/classiceditor.cpp index d57a5e6..33ede9a 100644 --- a/src/modes/classicmode/editor/classiceditor.cpp +++ b/src/modes/classicmode/editor/classiceditor.cpp @@ -8,19 +8,19 @@ #include "graphics/animations/classicdyinganimationscenario.h" // +#include "game/classicarrownote.h" +#include "editorcontext.h" #include "callbacks/callbacksimple.h" ClassicEditor::ClassicEditor(const std::shared_ptr>& timeline, - const std::shared_ptr& graphics_manager, - const std::shared_ptr>& selection_manager) : + const std::shared_ptr& context) : _timeline(timeline), - _graphics_manager(graphics_manager), - _selection_manager(selection_manager), + _context(context), _selected_type(Type::UP), _current_time(0), _scroll_step(500000) { - /*kku::microsec starting_beat_offset = 402162; + kku::microsec starting_beat_offset = 402162; int amount_of_beats = 209; kku::microsec interval = 1412162; kku::microsec tempo_interval = interval / 4; @@ -41,35 +41,42 @@ ClassicEditor::ClassicEditor(const std::shared_ptr>& while (bpm_iterator < bpm_end) { - ArrowNoteInitializer init; - ArrowElementInitializer element; - init.initializer.intervals = input_intervals; - init.initializer.perfect_offset = bpm_iterator; - init.hold = false; + ArrowElement element; - element.element.position = kku::Point(x, 390.f); - element.element.falling_curve_interpolation = {}; + element.position = kku::Point(x, 390.f); + element.falling_curve_interpolation = {}; element.keys = {kku::SystemEvent::Key::Code::W, kku::SystemEvent::Key::Code::Up}; - element.element.type = Type::UP; + element.type = Type::UP; + + bool hold = false; if (counter == 0) { - init.hold = true; + hold = true; element.keys = {kku::SystemEvent::Key::Code::D, kku::SystemEvent::Key::Code::Right}; - element.element.type = Type::RIGHT; + element.type = Type::RIGHT; + + counter = 13; } --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; x += 70; @@ -78,7 +85,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr>& x = 90.; } - _timeline->setNotes(notes);*/ + _timeline->setNotes(notes); } 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 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) { _current_time = input.timestamp; - //const auto& event = input.event; + const auto& event = input.event; switch (input.event.type) { @@ -105,7 +112,7 @@ void ClassicEditor::input(kku::GameEvent&& input) const auto key_data = std::get(input.event.data); if (key_data.view == kku::SystemEvent::Key::Code::LControl) { - _selection_manager->enableMultiselection(true); + _context->getSelectionManager()->enableMultiselection(true); } break; } @@ -115,39 +122,42 @@ void ClassicEditor::input(kku::GameEvent&& input) const auto key_data = std::get(input.event.data); if (key_data.view == kku::SystemEvent::Key::Code::LControl) { - _selection_manager->enableMultiselection(false); + _context->getSelectionManager()->enableMultiselection(false); } break; } 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) { - ArrowNoteInitializer init; - ArrowElementInitializer element; - init.initializer.intervals = {}; - init.initializer.perfect_offset = input.timestamp; - init.hold = false; + ArrowElement element; - element.element.position = std::get(event.data).position; - element.element.falling_curve_interpolation = {}; + element.position = std::get(event.data).position; + element.falling_curve_interpolation = {}; element.keys = {kku::SystemEvent::Key::Code::W, 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()) - _selection_manager->discard(); + if (!_context->getSelectionManager()->isMultiselectionEnabled()) + _context->getSelectionManager()->discard(); - _graphics_manager->input(std::move(input));*/ + //_graphics_manager->input(std::move(input)); break; } @@ -162,7 +172,7 @@ void ClassicEditor::update(kku::UpdateData&& updatedata) void ClassicEditor::display() const { - _graphics_manager->display(); + _context->displayGraphics(); } void ClassicEditor::recalculate(const kku::microsec& timestamp) diff --git a/src/modes/classicmode/editor/classiceditor.h b/src/modes/classicmode/editor/classiceditor.h index 7ba0a98..2c90754 100644 --- a/src/modes/classicmode/editor/classiceditor.h +++ b/src/modes/classicmode/editor/classiceditor.h @@ -9,14 +9,13 @@ #include "classicmode/classicnote.h" #include "classicmode/classicactions.h" -class ClassicGraphicsManager; +class EditorContext; class ClassicEditor : public kku::Editor { public: explicit ClassicEditor(const std::shared_ptr>& timeline, - const std::shared_ptr& graphics_manager, - const std::shared_ptr>& selection_manager); + const std::shared_ptr& context); virtual void input(kku::GameEvent&& input) override; virtual void update(kku::UpdateData&& updatedata) override; @@ -29,8 +28,8 @@ private: inline kku::microsec adjustOffset(kku::microsec offset) const noexcept; const std::shared_ptr> _timeline; - const std::shared_ptr _graphics_manager; - const std::shared_ptr> _selection_manager; + const std::shared_ptr _context; + Type _selected_type; kku::microsec _current_time; diff --git a/src/modes/classicmode/editor/editorcontext.cpp b/src/modes/classicmode/editor/editorcontext.cpp new file mode 100644 index 0000000..a0beab6 --- /dev/null +++ b/src/modes/classicmode/editor/editorcontext.cpp @@ -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>& selection_manager, + std::vector>&& 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(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> 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(); +} diff --git a/src/modes/classicmode/editor/editorcontext.h b/src/modes/classicmode/editor/editorcontext.h new file mode 100644 index 0000000..60c1361 --- /dev/null +++ b/src/modes/classicmode/editor/editorcontext.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#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>& selection_manager, + std::vector>&& 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> getSelectionManager() const; + +private: + const std::shared_ptr> _selection_manager; + const std::vector> _graphics_managers; +}; diff --git a/src/modes/classicmode/editor/selectionmanager.h b/src/modes/classicmode/editor/selectionmanager.h index cd469c5..4be6c27 100644 --- a/src/modes/classicmode/editor/selectionmanager.h +++ b/src/modes/classicmode/editor/selectionmanager.h @@ -8,14 +8,11 @@ class SelectionManager { public: SelectionManager() : - _multiselection_enabled(false), - deselection([](T* thing) { thing->deselect(); }) + _multiselection_enabled(false) {} - // Remove whole selection completely void discard() { - apply(deselection); _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) { _multiselection_enabled = enable; @@ -63,6 +69,4 @@ public: private: std::vector _selected_things; bool _multiselection_enabled; - - const std::function deselection; }; diff --git a/src/modes/classicmode/game/classicgame.cpp b/src/modes/classicmode/game/classicgame.cpp index 8dcdc49..e1a9138 100644 --- a/src/modes/classicmode/game/classicgame.cpp +++ b/src/modes/classicmode/game/classicgame.cpp @@ -11,9 +11,6 @@ ClassicGame::ClassicGame(const std::shared_ptr>& time _context(context) {} -ClassicGame::~ClassicGame() -{} - void ClassicGame::run() { auto beatmap = classic::createBeatmap("aa", _context); diff --git a/src/modes/classicmode/game/classicgame.h b/src/modes/classicmode/game/classicgame.h index c15150f..85298b7 100644 --- a/src/modes/classicmode/game/classicgame.h +++ b/src/modes/classicmode/game/classicgame.h @@ -16,7 +16,6 @@ class ClassicGame final : public kku::Game public: explicit ClassicGame(const std::shared_ptr>& timeline, const std::shared_ptr& context); - virtual ~ClassicGame() override; virtual void run() override;