diff --git a/src/modes/classicmode/classicfactory.cpp b/src/modes/classicmode/classicfactory.cpp index 90a72bf..16a4ea4 100644 --- a/src/modes/classicmode/classicfactory.cpp +++ b/src/modes/classicmode/classicfactory.cpp @@ -33,7 +33,7 @@ std::unique_ptr classic::getEditor(const std::shared_ptr(core_factory); const auto timeline = std::make_shared>(); - const auto selection_manager = std::make_shared>(); + const auto selection_manager = std::make_shared(); std::vector> graphics_managers; graphics_managers.emplace_back(std::make_shared(timeline, factory, visibility_offset)); graphics_managers.emplace_back(std::make_shared(timeline, factory, visibility_offset * 2)); diff --git a/src/modes/classicmode/editor/classiceditor.cpp b/src/modes/classicmode/editor/classiceditor.cpp index ee0490d..9d53ec8 100644 --- a/src/modes/classicmode/editor/classiceditor.cpp +++ b/src/modes/classicmode/editor/classiceditor.cpp @@ -45,6 +45,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr>& element.position = kku::Point(x, 390.f); element.falling_curve_interpolation = {}; + element.id = 0; element.keys = {kku::SystemEvent::Key::Code::W, kku::SystemEvent::Key::Code::Up}; @@ -112,7 +113,6 @@ void ClassicEditor::input(kku::GameEvent&& input) switch (input.event.type) { - default: break; @@ -167,8 +167,6 @@ void ClassicEditor::input(kku::GameEvent&& input) if (!_context->getSelectionManager()->isMultiselectionEnabled()) _context->getSelectionManager()->discard(); - //_graphics_manager->input(std::move(input)); - break; } } diff --git a/src/modes/classicmode/editor/editorcontext.cpp b/src/modes/classicmode/editor/editorcontext.cpp index b69ceb8..90044f5 100644 --- a/src/modes/classicmode/editor/editorcontext.cpp +++ b/src/modes/classicmode/editor/editorcontext.cpp @@ -4,7 +4,7 @@ #include "graphics/classicgraphicsmanager.h" #include "graphics/classicnotegraphics.h" -EditorContext::EditorContext(const std::shared_ptr>& selection_manager, +EditorContext::EditorContext(const std::shared_ptr& selection_manager, std::vector>&& graphics_managers) : _selection_manager(selection_manager), _graphics_managers(std::move(graphics_managers)) @@ -12,40 +12,26 @@ EditorContext::EditorContext(const std::shared_ptr void EditorContext::input(ClassicArrowNote *note, kku::GameEvent&& input) const { - (void)note; switch (input.event.type) { - default: break; - /*case kku::SystemEvent::Type::MousePress: + case kku::SystemEvent::Type::MousePress: { - bool selection_changed = false; - std::size_t amount_selected = 0; - - const auto position = std::get(input.event.data).position; + const auto clicked_position = std::get(input.event.data).position; for (auto& element : note->getElements()) { - if (element.sprite->getRectangle()->contains(position)) + if (element.sprite->getRectangle()->contains(clicked_position)) { - element.selected = !element.selected; - selection_changed = true; - if (element.selected) - ++amount_selected; + if (_selection_manager->isSelected(note->getId(), element.id)) + _selection_manager->remove(note->getId(), element.id); + else + _selection_manager->fetch(note->getId(), element.id); } } - - if (selection_changed) - { - if (amount_selected > 0) - _selection_manager->fetch(note); - else - _selection_manager->remove(note); - } - break; - }*/ + } } } @@ -56,7 +42,8 @@ void EditorContext::update(ClassicArrowNote *note, const kku::microsec& music_of switch (note->getState()) { - default: return; + default: + return; break; case ClassicArrowNote::State::INITIAL: @@ -89,7 +76,7 @@ void EditorContext::update(ClassicArrowNote *note, const kku::microsec& music_of element.animations[note->getState()]->update(music_offset); } -std::shared_ptr> EditorContext::getSelectionManager() const +std::shared_ptr EditorContext::getSelectionManager() const { return _selection_manager; } diff --git a/src/modes/classicmode/editor/editorcontext.h b/src/modes/classicmode/editor/editorcontext.h index a7813ab..5a97fd3 100644 --- a/src/modes/classicmode/editor/editorcontext.h +++ b/src/modes/classicmode/editor/editorcontext.h @@ -11,7 +11,7 @@ class ClassicGraphicsManager; class EditorContext : public Context { public: - explicit EditorContext(const std::shared_ptr>& selection_manager, + explicit EditorContext(const std::shared_ptr& selection_manager, std::vector>&& graphics_managers); virtual void input(ClassicArrowNote *note, kku::GameEvent&& input) const override; @@ -21,9 +21,9 @@ public: void updateGraphics(const kku::microsec& music_offset); void displayGraphics() const; - std::shared_ptr> getSelectionManager() const; + std::shared_ptr getSelectionManager() const; private: - const std::shared_ptr> _selection_manager; + 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 4be6c27..6142a82 100644 --- a/src/modes/classicmode/editor/selectionmanager.h +++ b/src/modes/classicmode/editor/selectionmanager.h @@ -1,50 +1,53 @@ #pragma once +#include #include #include -template class SelectionManager { public: - SelectionManager() : + explicit SelectionManager() : _multiselection_enabled(false) {} void discard() { - _selected_things.clear(); + _selected_ids.clear(); } - void fetch(T * const thing) + void fetch(int note_id, int element_id) { - bool already_there = std::any_of(_selected_things.begin(), _selected_things.end(), - [&thing](const auto& selected_thing) - { - return thing == selected_thing; - }); - - if (!already_there) - _selected_things.emplace_back(thing); + if (!isSelected(note_id, element_id)) + _selected_ids[note_id].emplace_back(element_id); } - void remove(T * const thing) + void remove(int note_id, int element_id) { - for (std::size_t i = 0; i < _selected_things.size(); ++i) + if (_selected_ids.count(note_id) == 0) + return; + + auto& elements_ids = _selected_ids[note_id]; + + for (std::size_t i = 0; i < elements_ids.size(); ++i) { - if (thing == _selected_things.at(i)) + if (element_id == elements_ids.at(i)) { - _selected_things[i] = std::move(_selected_things.back()); - _selected_things.pop_back(); + elements_ids[i] = std::move(elements_ids.back()); + elements_ids.pop_back(); break; } } } - bool isSelected(const T * const thing) const + bool isSelected(int note_id, int element_id) const { - for (const auto& selected_thing : thing) - if (thing == selected_thing) + if (_selected_ids.count(note_id) == 0) + return false; + + const std::vector& selected_elements = _selected_ids.at(note_id); + for (const auto& selected_element : selected_elements) + if (selected_element == element_id) return true; return false; @@ -60,13 +63,7 @@ public: return _multiselection_enabled; } - void apply(const std::function& function) - { - for (auto& thing : _selected_things) - function(thing); - } - private: - std::vector _selected_things; + std::map> _selected_ids; bool _multiselection_enabled; }; diff --git a/src/modes/classicmode/game/arrowelement.h b/src/modes/classicmode/game/arrowelement.h index 3eb43bd..2c426be 100644 --- a/src/modes/classicmode/game/arrowelement.h +++ b/src/modes/classicmode/game/arrowelement.h @@ -10,10 +10,12 @@ #include class ClassicNoteGraphics; +class ClassicSelectionGraphics; class ClassicAnimationScenario; struct ArrowElement { + std::shared_ptr selection; std::shared_ptr sprite; std::array, 5> animations; @@ -24,6 +26,7 @@ struct ArrowElement std::array keys; Type type = Type::NONE; bool pressed = false; + int id = 0; // Each note may consist of several buttons. // For example, ↑ → or ↓ → ← diff --git a/src/modes/classicmode/graphics/classicgraphicsmanager.h b/src/modes/classicmode/graphics/classicgraphicsmanager.h index 69c2ab3..252674f 100644 --- a/src/modes/classicmode/graphics/classicgraphicsmanager.h +++ b/src/modes/classicmode/graphics/classicgraphicsmanager.h @@ -7,6 +7,7 @@ #include class ClassicArrowNote; +class SelectionManager; class ArrowElement; // class ClassicSliderNote; @@ -22,11 +23,14 @@ public: virtual void display() const = 0; virtual void update(const kku::microsec& offset) = 0; + virtual void switchSelection(ArrowElement* element) = 0; + virtual void update(const kku::microsec& offset, ClassicArrowNote* note) = 0; // virtual void update(const kku::microsec& offset, ClassicSliderNote* note) = 0; virtual void draw(const ClassicArrowNote * const note) const = 0; // virtual void draw(ClassicSliderNote* note) const = 0; + virtual void draw(const std::shared_ptr& selection_manager) const = 0; protected: kku::microsec _visibility_offset; diff --git a/src/modes/classicmode/graphics/classicscenegraphicsmanager.cpp b/src/modes/classicmode/graphics/classicscenegraphicsmanager.cpp index f89a93d..4b58651 100644 --- a/src/modes/classicmode/graphics/classicscenegraphicsmanager.cpp +++ b/src/modes/classicmode/graphics/classicscenegraphicsmanager.cpp @@ -84,6 +84,17 @@ void ClassicSceneGraphicsManager::draw(const ClassicArrowNote * const note) cons } } +void ClassicSceneGraphicsManager::switchSelection(ArrowElement* element) +{ + if (element->selection) + element->selection->reset(); + else + { + element->selection = _factory->createSelection(); + element->selection->adjustTo(element->sprite); + } +} + void ClassicSceneGraphicsManager::setGraphics(std::vector& elements, kku::TimeRange&& range) { for (auto& element : elements) diff --git a/src/modes/classicmode/graphics/classicscenegraphicsmanager.h b/src/modes/classicmode/graphics/classicscenegraphicsmanager.h index 552b129..8e6d81f 100644 --- a/src/modes/classicmode/graphics/classicscenegraphicsmanager.h +++ b/src/modes/classicmode/graphics/classicscenegraphicsmanager.h @@ -25,10 +25,12 @@ public: virtual void update(const kku::microsec& offset, ClassicArrowNote* note) override; virtual void draw(const ClassicArrowNote * const note) const override; + virtual void switchSelection(ArrowElement* element) override; + virtual void draw(const std::shared_ptr& selection_manager) const override; + void setGraphics(std::vector& elements, kku::TimeRange&& range); void removeGraphics(std::vector& elements); - protected: kku::SpriteContainer _sprite_container; const std::shared_ptr _factory; diff --git a/src/modes/classicmode/graphics/classictimelinegraphicsmanager.h b/src/modes/classicmode/graphics/classictimelinegraphicsmanager.h index b84bd8a..7d3e230 100644 --- a/src/modes/classicmode/graphics/classictimelinegraphicsmanager.h +++ b/src/modes/classicmode/graphics/classictimelinegraphicsmanager.h @@ -25,6 +25,9 @@ public: virtual void update(const kku::microsec& offset, ClassicArrowNote* note) override; virtual void draw(const ClassicArrowNote * const note) const override; + virtual void switchSelection(ArrowElement* element) override; + virtual void draw(const std::shared_ptr& selection_manager) const override; + protected: kku::SpriteContainer _sprite_container; const std::shared_ptr _factory;