Turn SelectionManager into template

selection
NaiJi ✨ 2 years ago
parent b92bffb43e
commit 70d3284eeb

@ -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 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);
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 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);
return std::make_unique<ClassicEditor>(timeline, graphics_manager, selection_manager);

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

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

@ -3,7 +3,7 @@
#include "graphics/animations/classicanimationscenario.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)),
_selection_manager(selection_manager)
{
@ -93,3 +93,9 @@ std::vector<MockElement>& ClassicMockNote::getElements()
{
return _elements;
}
void ClassicMockNote::deselect()
{
for (auto& element : _elements)
element.selected = false;
}

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

@ -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();
}
}

@ -1,27 +1,68 @@
#pragma once
#include "classicmocknote.h"
#include <memory>
#include <vector>
#include <algorithm>
template <class T>
class SelectionManager
{
public:
explicit SelectionManager();
SelectionManager() :
_multiselection_enabled(false),
deselection([](T* thing) { thing->deselect(); })
{}
// Remove whole selection completely
void discard();
void discard()
{
apply(deselection);
_selected_things.clear();
}
void fetch(T * const thing)
{
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);
}
void fetch(ClassicMockNote * const note);
void remove(ClassicMockNote * const note);
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);
bool isMultiselectionEnabled() const;
void enableMultiselection(bool enable = true)
{
_multiselection_enabled = enable;
}
void display() const;
bool isMultiselectionEnabled() const
{
return _multiselection_enabled;
}
void apply(const std::function<void(T*)>& function)
{
for (auto& thing : _selected_things)
function(thing);
}
private:
std::vector<ClassicMockNote*> _selected_notes;
std::vector<T*> _selected_things;
bool _multiselection_enabled;
const std::function<void(T*)> deselection;
};

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

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

Loading…
Cancel
Save