Implement selection functions for graphics managers

This commit is contained in:
NaiJi ✨ 2022-07-06 00:31:02 +03:00
parent 4b91146c33
commit 1953d2339d
10 changed files with 65 additions and 60 deletions

View File

@ -33,7 +33,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<ClassicNote>>();
const auto selection_manager = std::make_shared<SelectionManager<ClassicNote>>();
const auto selection_manager = std::make_shared<SelectionManager>();
std::vector<std::shared_ptr<ClassicGraphicsManager>> graphics_managers;
graphics_managers.emplace_back(std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset));
graphics_managers.emplace_back(std::make_shared<ClassicTimelineGraphicsManager>(timeline, factory, visibility_offset * 2));

View File

@ -45,6 +45,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>&
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;
}
}

View File

@ -4,7 +4,7 @@
#include "graphics/classicgraphicsmanager.h"
#include "graphics/classicnotegraphics.h"
EditorContext::EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager,
EditorContext::EditorContext(const std::shared_ptr<SelectionManager>& selection_manager,
std::vector<std::shared_ptr<ClassicGraphicsManager>>&& graphics_managers) :
_selection_manager(selection_manager),
_graphics_managers(std::move(graphics_managers))
@ -12,40 +12,26 @@ EditorContext::EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>
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<kku::SystemEvent::Mouse>(input.event.data).position;
const auto clicked_position = std::get<kku::SystemEvent::Mouse>(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_changed)
{
if (amount_selected > 0)
_selection_manager->fetch(note);
if (_selection_manager->isSelected(note->getId(), element.id))
_selection_manager->remove(note->getId(), element.id);
else
_selection_manager->remove(note);
_selection_manager->fetch(note->getId(), element.id);
}
}
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<SelectionManager<ClassicNote>> EditorContext::getSelectionManager() const
std::shared_ptr<SelectionManager> EditorContext::getSelectionManager() const
{
return _selection_manager;
}

View File

@ -11,7 +11,7 @@ class ClassicGraphicsManager;
class EditorContext : public Context
{
public:
explicit EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>>& selection_manager,
explicit EditorContext(const std::shared_ptr<SelectionManager>& selection_manager,
std::vector<std::shared_ptr<ClassicGraphicsManager>>&& 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<SelectionManager<ClassicNote>> getSelectionManager() const;
std::shared_ptr<SelectionManager> getSelectionManager() const;
private:
const std::shared_ptr<SelectionManager<ClassicNote>> _selection_manager;
const std::shared_ptr<SelectionManager> _selection_manager;
const std::vector<std::shared_ptr<ClassicGraphicsManager>> _graphics_managers;
};

View File

@ -1,50 +1,53 @@
#pragma once
#include <map>
#include <vector>
#include <algorithm>
template <class T>
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<int>& 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<void(T*)>& function)
{
for (auto& thing : _selected_things)
function(thing);
}
private:
std::vector<T*> _selected_things;
std::map<int, std::vector<int>> _selected_ids;
bool _multiselection_enabled;
};

View File

@ -10,10 +10,12 @@
#include <array>
class ClassicNoteGraphics;
class ClassicSelectionGraphics;
class ClassicAnimationScenario;
struct ArrowElement
{
std::shared_ptr<ClassicSelectionGraphics> selection;
std::shared_ptr<ClassicNoteGraphics> sprite;
std::array<std::shared_ptr<ClassicAnimationScenario>, 5> animations;
@ -24,6 +26,7 @@ struct ArrowElement
std::array<kku::SystemEvent::Key::Code, 2> keys;
Type type = Type::NONE;
bool pressed = false;
int id = 0;
// Each note may consist of several buttons.
// For example, ↑ → or ↓ → ←

View File

@ -7,6 +7,7 @@
#include <vector>
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<SelectionManager>& selection_manager) const = 0;
protected:
kku::microsec _visibility_offset;

View File

@ -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<ArrowElement>& elements, kku::TimeRange&& range)
{
for (auto& element : elements)

View File

@ -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<SelectionManager>& selection_manager) const override;
void setGraphics(std::vector<ArrowElement>& elements, kku::TimeRange&& range);
void removeGraphics(std::vector<ArrowElement>& elements);
protected:
kku::SpriteContainer<Type, ClassicGraphicsFactory, ClassicNoteGraphics> _sprite_container;
const std::shared_ptr<const ClassicGraphicsFactory> _factory;

View File

@ -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<SelectionManager>& selection_manager) const override;
protected:
kku::SpriteContainer<Type, ClassicGraphicsFactory, ClassicNoteGraphics> _sprite_container;
const std::shared_ptr<const ClassicGraphicsFactory> _factory;