Turn SelectionManager into template

This commit is contained in:
NaiJi ✨ 2022-03-14 19:01:15 +03:00
parent b92bffb43e
commit 70d3284eeb
9 changed files with 70 additions and 88 deletions

View File

@ -16,7 +16,6 @@ std::unique_ptr<kku::Game> classic::getGame(const std::shared_ptr<kku::CoreFacto
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 selection_manager = std::make_shared<SelectionManager>();
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager<ClassicNote>>(timeline, factory, visibility_offset); const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager<ClassicNote>>(timeline, factory, visibility_offset);
return std::make_unique<ClassicGame>(timeline, graphics_manager); return std::make_unique<ClassicGame>(timeline, graphics_manager);
@ -29,7 +28,7 @@ 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<ClassicMockNote>>(); const auto timeline = std::make_shared<kku::Timeline<ClassicMockNote>>();
const auto selection_manager = std::make_shared<SelectionManager>(); const auto selection_manager = std::make_shared<SelectionManager<ClassicMockNote>>();
const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager<ClassicMockNote>>(timeline, factory, visibility_offset); const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager<ClassicMockNote>>(timeline, factory, visibility_offset);
return std::make_unique<ClassicEditor>(timeline, graphics_manager, selection_manager); return std::make_unique<ClassicEditor>(timeline, graphics_manager, selection_manager);

View File

@ -13,7 +13,7 @@
ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicMockNote>>& timeline, ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicMockNote>>& timeline,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager, const std::shared_ptr<ClassicGraphicsManager>& graphics_manager,
const std::shared_ptr<SelectionManager>& selection_manager) : const std::shared_ptr<SelectionManager<ClassicMockNote>>& selection_manager) :
_timeline(timeline), _timeline(timeline),
_graphics_manager(graphics_manager), _graphics_manager(graphics_manager),
_selection_manager(selection_manager), _selection_manager(selection_manager),
@ -165,7 +165,6 @@ void ClassicEditor::update(kku::UpdateData&& updatedata)
void ClassicEditor::display() const void ClassicEditor::display() const
{ {
_graphics_manager->display(); _graphics_manager->display();
_selection_manager->display();
} }
void ClassicEditor::recalculate(const kku::microsec& timestamp) void ClassicEditor::recalculate(const kku::microsec& timestamp)

View File

@ -4,19 +4,19 @@
#include "core/editor.h" #include "core/editor.h"
#include "core/timeline.h" #include "core/timeline.h"
#include "selectionmanager.h"
#include "classicmocknote.h" #include "classicmocknote.h"
#include "classicmode/classicactions.h" #include "classicmode/classicactions.h"
class ClassicGraphicsManager; class ClassicGraphicsManager;
class SelectionManager;
class ClassicEditor : public kku::Editor class ClassicEditor : public kku::Editor
{ {
public: public:
explicit ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicMockNote>>& timeline, explicit ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicMockNote>>& timeline,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager, const std::shared_ptr<ClassicGraphicsManager>& graphics_manager,
const std::shared_ptr<SelectionManager>& selection_manager); const std::shared_ptr<SelectionManager<ClassicMockNote>>& 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;
@ -30,7 +30,7 @@ private:
const std::shared_ptr<kku::Timeline<ClassicMockNote>> _timeline; const std::shared_ptr<kku::Timeline<ClassicMockNote>> _timeline;
const std::shared_ptr<ClassicGraphicsManager> _graphics_manager; const std::shared_ptr<ClassicGraphicsManager> _graphics_manager;
const std::shared_ptr<SelectionManager> _selection_manager; const std::shared_ptr<SelectionManager<ClassicMockNote>> _selection_manager;
Type _selected_type; Type _selected_type;
kku::microsec _current_time; kku::microsec _current_time;

View File

@ -3,7 +3,7 @@
#include "graphics/animations/classicanimationscenario.h" #include "graphics/animations/classicanimationscenario.h"
#include "editor/selectionmanager.h" #include "editor/selectionmanager.h"
ClassicMockNote::ClassicMockNote(ArrowNoteInitializer&& init, const std::shared_ptr<SelectionManager>& selection_manager) : ClassicMockNote::ClassicMockNote(ArrowNoteInitializer&& init, const std::shared_ptr<SelectionManager<ClassicMockNote>>& selection_manager) :
ClassicNote(std::move(init.initializer)), ClassicNote(std::move(init.initializer)),
_selection_manager(selection_manager) _selection_manager(selection_manager)
{ {
@ -93,3 +93,9 @@ std::vector<MockElement>& ClassicMockNote::getElements()
{ {
return _elements; return _elements;
} }
void ClassicMockNote::deselect()
{
for (auto& element : _elements)
element.selected = false;
}

View File

@ -4,13 +4,12 @@
#include "mockelement.h" #include "mockelement.h"
#include "classicmode/arrownoteinitializer.h" #include "classicmode/arrownoteinitializer.h"
#include "selectionmanager.h"
class SelectionManager;
class ClassicMockNote : public ClassicNote class ClassicMockNote : public ClassicNote
{ {
public: public:
explicit ClassicMockNote(ArrowNoteInitializer&& init, const std::shared_ptr<SelectionManager>& selection_manager); explicit ClassicMockNote(ArrowNoteInitializer&& init, const std::shared_ptr<SelectionManager<ClassicMockNote>>& selection_manager);
virtual ~ClassicMockNote() = default; virtual ~ClassicMockNote() = default;
virtual bool isActive(const kku::microsec& offset) const override; virtual bool isActive(const kku::microsec& offset) const override;
@ -18,9 +17,10 @@ public:
virtual void input(kku::GameEvent&& input) override; virtual void input(kku::GameEvent&& input) override;
std::vector<MockElement>& getElements(); std::vector<MockElement>& getElements();
void deselect();
private: private:
const std::shared_ptr<SelectionManager> _selection_manager; const std::shared_ptr<SelectionManager<ClassicMockNote>> _selection_manager;
std::vector<MockElement> _elements; std::vector<MockElement> _elements;
}; };

View File

@ -1,63 +0,0 @@
#include "selectionmanager.h"
#include "classicmocknote.h"
#include "mockelement.h"
#include "graphics/classicselectiongraphics.h"
#include <algorithm>
SelectionManager::SelectionManager() :
_multiselection_enabled(false)
{}
void SelectionManager::discard()
{
for (const auto& note : _selected_notes)
for (auto& element : note->getElements())
element.selected = false;
_selected_notes.clear();
}
void SelectionManager::fetch(ClassicMockNote * const note)
{
bool already_there = std::any_of(_selected_notes.begin(), _selected_notes.end(),
[&note](const auto& selected_note)
{
return note == selected_note;
});
if (!already_there)
_selected_notes.emplace_back(note);
}
void SelectionManager::remove(ClassicMockNote * const note)
{
for (std::size_t i = 0; i < _selected_notes.size(); ++i)
{
if (note == _selected_notes.at(i))
{
_selected_notes[i] = std::move(_selected_notes.back());
_selected_notes.pop_back();
break;
}
}
}
void SelectionManager::enableMultiselection(bool enable)
{
_multiselection_enabled = enable;
}
bool SelectionManager::isMultiselectionEnabled() const
{
return _multiselection_enabled;
}
void SelectionManager::display() const
{
for (const auto& note : _selected_notes)
for (const auto& element : note->getElements())
if (element.selected)
{
element.selection->display();
}
}

View File

@ -1,27 +1,68 @@
#pragma once #pragma once
#include "classicmocknote.h"
#include <memory>
#include <vector> #include <vector>
#include <algorithm>
template <class T>
class SelectionManager class SelectionManager
{ {
public: public:
explicit SelectionManager(); SelectionManager() :
_multiselection_enabled(false),
deselection([](T* thing) { thing->deselect(); })
{}
// Remove whole selection completely // Remove whole selection completely
void discard(); void discard()
{
apply(deselection);
_selected_things.clear();
}
void fetch(ClassicMockNote * const note); void fetch(T * const thing)
void remove(ClassicMockNote * const note); {
bool already_there = std::any_of(_selected_things.begin(), _selected_things.end(),
[&thing](const auto& selected_thing)
{
return thing == selected_thing;
});
void enableMultiselection(bool enable = true); if (!already_there)
bool isMultiselectionEnabled() const; _selected_things.emplace_back(thing);
}
void display() const; void remove(T * const thing)
{
for (std::size_t i = 0; i < _selected_things.size(); ++i)
{
if (thing == _selected_things.at(i))
{
_selected_things[i] = std::move(_selected_things.back());
_selected_things.pop_back();
break;
}
}
}
void enableMultiselection(bool enable = true)
{
_multiselection_enabled = enable;
}
bool isMultiselectionEnabled() const
{
return _multiselection_enabled;
}
void apply(const std::function<void(T*)>& function)
{
for (auto& thing : _selected_things)
function(thing);
}
private: private:
std::vector<ClassicMockNote*> _selected_notes; std::vector<T*> _selected_things;
bool _multiselection_enabled; bool _multiselection_enabled;
const std::function<void(T*)> deselection;
}; };

View File

@ -1,8 +1,9 @@
#include "classicarrownote.h" #include "classicarrownote.h"
#include "graphics/classicscenegraphicsmanager.h"
#include "graphics/animations/classicanimationscenario.h" #include "graphics/animations/classicanimationscenario.h"
#include "holdmanager.h" #include "holdmanager.h"
#include <algorithm>
ClassicArrowNote::ClassicArrowNote(ArrowNoteInitializer&& init, const std::shared_ptr<HoldManager>& hold_manager) : ClassicArrowNote::ClassicArrowNote(ArrowNoteInitializer&& init, const std::shared_ptr<HoldManager>& hold_manager) :
ClassicNote(std::move(init.initializer)), ClassicNote(std::move(init.initializer)),
_evaluator(init.initializer.intervals, _perfect_offset), _evaluator(init.initializer.intervals, _perfect_offset),

View File

@ -2,7 +2,6 @@
#include "arrowelement.h" #include "arrowelement.h"
#include "classicmode/classicnote.h" #include "classicmode/classicnote.h"
#include "classicmode/arrownoteinitializer.h" #include "classicmode/arrownoteinitializer.h"
class HoldManager; class HoldManager;