Implement selection manager

This commit is contained in:
NaiJi ✨ 2022-02-10 02:30:49 +03:00
parent 4ec11560f1
commit 6768aeabdf
25 changed files with 179 additions and 76 deletions

View File

@ -6,6 +6,7 @@
#include "core/timeline.h" #include "core/timeline.h"
#include "game/classicgame.h" #include "game/classicgame.h"
#include "editor/classiceditor.h" #include "editor/classiceditor.h"
#include "editor/selectionmanager.h"
std::unique_ptr<kku::Game> classic::getGame(const std::shared_ptr<kku::CoreFactory>& core_factory) std::unique_ptr<kku::Game> classic::getGame(const std::shared_ptr<kku::CoreFactory>& core_factory)
@ -15,6 +16,7 @@ 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>(timeline, factory, visibility_offset); const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset);
return std::make_unique<ClassicGame>(timeline, graphics_manager); return std::make_unique<ClassicGame>(timeline, graphics_manager);
@ -27,7 +29,8 @@ 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<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>(timeline, factory, visibility_offset); const auto graphics_manager = std::make_shared<ClassicSceneGraphicsManager>(timeline, factory, visibility_offset);
return std::make_unique<ClassicEditor>(timeline, graphics_manager); return std::make_unique<ClassicEditor>(timeline, graphics_manager, selection_manager);
} }

View File

@ -1,9 +1,8 @@
#include "classicnote.h" #include "classicmode/classicnote.h"
ClassicNote::ClassicNote(NoteInitializer &&init) : ClassicNote::ClassicNote(NoteInitializer &&init) :
Note(init.perfect_offset), Note(init.perfect_offset),
_state(State::NONE), _state(State::NONE)
_context(init.context)
{} {}
void ClassicNote::setState(ClassicNote::State state) noexcept void ClassicNote::setState(ClassicNote::State state) noexcept

View File

@ -1,8 +1,8 @@
#include "classiceditor.h" #include "classiceditor.h"
#include "game/classicmocknote.h" #include "classicmocknote.h"
#include "graphics/classicgraphicsmanager.h" #include "graphics/classicgraphicsmanager.h"
#include "game/classicmapcreator.h" #include "editor/selectionmanager.h"
// Replace with interface by dependency injection // Replace with interface by dependency injection
#include "graphics/animations/classicflyinganimationscenario.h" #include "graphics/animations/classicflyinganimationscenario.h"
@ -10,9 +10,11 @@
// //
ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline, ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager) : const std::shared_ptr<ClassicGraphicsManager>& graphics_manager,
const std::shared_ptr<SelectionManager>& selection_manager) :
_timeline(timeline), _timeline(timeline),
_graphics_manager(graphics_manager), _graphics_manager(graphics_manager),
_selection_manager(selection_manager),
_selected_type(Type::UP), _selected_type(Type::UP),
_current_time(0), _current_time(0),
_scroll_step(500000) _scroll_step(500000)
@ -66,7 +68,7 @@ ClassicEditor::ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>&
init.elements = {element}; init.elements = {element};
notes.insert(new ClassicMockNote(std::move(init))); notes.insert(new ClassicMockNote(std::move(init), _selection_manager));
bpm_iterator += tempo_interval; bpm_iterator += tempo_interval;
x += 70; x += 70;
@ -97,6 +99,26 @@ void ClassicEditor::input(kku::GameEvent&& input)
default: default:
break; break;
case kku::SystemEvent::Type::KeyPress:
{
const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data);
if (key_data.view == kku::SystemEvent::Key::Code::LControl)
{
_selection_manager->enableMultiselection(true);
}
break;
}
case kku::SystemEvent::Type::KeyRelease:
{
const auto key_data = std::get<kku::SystemEvent::Key>(input.event.data);
if (key_data.view == kku::SystemEvent::Key::Code::LControl)
{
_selection_manager->enableMultiselection(false);
}
break;
}
case kku::SystemEvent::Type::MousePress: case kku::SystemEvent::Type::MousePress:
{ {
const auto note = _timeline->getNoteBy(_current_time); const auto note = _timeline->getNoteBy(_current_time);
@ -107,7 +129,6 @@ void ClassicEditor::input(kku::GameEvent&& input)
init.initializer.intervals = {}; init.initializer.intervals = {};
init.initializer.perfect_offset = input.timestamp; init.initializer.perfect_offset = input.timestamp;
init.hold = false; init.hold = false;
init.initializer.context = nullptr;
element.element.position = std::get<kku::SystemEvent::Mouse>(event.data).position; element.element.position = std::get<kku::SystemEvent::Mouse>(event.data).position;
element.element.falling_curve_interpolation = {}; element.element.falling_curve_interpolation = {};
@ -119,10 +140,14 @@ void ClassicEditor::input(kku::GameEvent&& input)
init.elements = { element }; init.elements = { element };
_timeline->insertNote(new ClassicMockNote(std::move(init))); _timeline->insertNote(new ClassicMockNote(std::move(init), _selection_manager));
} }
else
if (!_selection_manager->isMultiselectionEnabled())
_selection_manager->discard();
_graphics_manager->input(std::move(input)); _graphics_manager->input(std::move(input));
break; break;
} }

View File

@ -5,16 +5,18 @@
#include "core/editor.h" #include "core/editor.h"
#include "core/timeline.h" #include "core/timeline.h"
#include "game/classicnote.h" #include "classicmode/classicnote.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<ClassicNote>>& timeline, explicit ClassicEditor(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicGraphicsManager>& graphics_manager); const std::shared_ptr<ClassicGraphicsManager>& graphics_manager,
const std::shared_ptr<SelectionManager>& 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;
@ -26,10 +28,9 @@ public:
private: private:
inline kku::microsec adjustOffset(kku::microsec offset) const noexcept; inline kku::microsec adjustOffset(kku::microsec offset) const noexcept;
Context _context;
const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline; const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline;
const std::shared_ptr<ClassicGraphicsManager> _graphics_manager; const std::shared_ptr<ClassicGraphicsManager> _graphics_manager;
const std::shared_ptr<SelectionManager> _selection_manager;
Type _selected_type; Type _selected_type;
kku::microsec _current_time; kku::microsec _current_time;

View File

@ -1,10 +1,11 @@
#include "classicmocknote.h" #include "classicmocknote.h"
#include "graphics/classicscenegraphicsmanager.h" #include "graphics/classicscenegraphicsmanager.h"
#include "graphics/animations/classicanimationscenario.h" #include "graphics/animations/classicanimationscenario.h"
#include "holdmanager.h" #include "editor/selectionmanager.h"
ClassicMockNote::ClassicMockNote(ArrowNoteInitializer&& init) : ClassicMockNote::ClassicMockNote(ArrowNoteInitializer&& init, const std::shared_ptr<SelectionManager>& selection_manager) :
ClassicNote(std::move(init.initializer)) ClassicNote(std::move(init.initializer)),
_selection_manager(selection_manager)
{ {
_elements.resize(init.elements.size()); _elements.resize(init.elements.size());
@ -32,15 +33,29 @@ void ClassicMockNote::input(kku::GameEvent&& input)
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 position = std::get<kku::SystemEvent::Mouse>(input.event.data).position;
for (auto& element : _elements) for (auto& element : _elements)
{ {
if (element.sprite->getRectangle()->contains(position)) if (element.sprite->getRectangle()->contains(position))
{ {
element.selected = !element.selected; element.selected = !element.selected;
element.selection->toggle(element.selected); selection_changed = true;
if (element.selected)
++amount_selected;
} }
} }
if (selection_changed)
{
if (amount_selected > 0)
_selection_manager->fetch(this);
else
_selection_manager->remove(this);
}
break; break;
} }
@ -83,3 +98,8 @@ void ClassicMockNote::setGraphics(ClassicGraphicsManager * const manager, kku::T
{ {
manager->setGraphics(_elements, std::move(range)); manager->setGraphics(_elements, std::move(range));
} }
std::vector<MockElement>& ClassicMockNote::getElements()
{
return _elements;
}

View File

@ -1,15 +1,16 @@
#pragma once #pragma once
#include "arrowelement.h" #include "classicmode/classicnote.h"
#include "classicnote.h"
#include "mockelement.h" #include "mockelement.h"
#include "initializers/arrownoteinitializer.h" #include "classicmode/arrownoteinitializer.h"
class SelectionManager;
class ClassicMockNote : public ClassicNote class ClassicMockNote : public ClassicNote
{ {
public: public:
explicit ClassicMockNote(ArrowNoteInitializer&& init); explicit ClassicMockNote(ArrowNoteInitializer&& init, const std::shared_ptr<SelectionManager>& 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;
@ -19,9 +20,11 @@ public:
virtual void display(const ClassicGraphicsManager * const manager) const override; virtual void display(const ClassicGraphicsManager * const manager) const override;
virtual void setGraphics(ClassicGraphicsManager * const manager, kku::TimeRange&& range) override; virtual void setGraphics(ClassicGraphicsManager * const manager, kku::TimeRange&& range) override;
std::vector<MockElement>& getElements();
private: private:
const std::shared_ptr<SelectionManager> _selection_manager;
std::vector<MockElement> _elements; std::vector<MockElement> _elements;
bool _is_selected;
}; };
using MockElements = std::vector<MockElement>; using MockElements = std::vector<MockElement>;

View File

@ -0,0 +1,53 @@
#include "selectionmanager.h"
#include "classicmocknote.h"
#include "mockelement.h"
#include "graphics/classicselection.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;
}

View File

@ -0,0 +1,25 @@
#pragma once
#include "classicmocknote.h"
#include <memory>
#include <vector>
class SelectionManager
{
public:
explicit SelectionManager();
// Remove whole selection completely
void discard();
void fetch(ClassicMockNote * const note);
void remove(ClassicMockNote * const note);
void enableMultiselection(bool enable = true);
bool isMultiselectionEnabled() const;
private:
std::vector<ClassicMockNote*> _selected_notes;
bool _multiselection_enabled;
};

View File

@ -3,9 +3,10 @@
#include "graphics/animations/classicanimationscenario.h" #include "graphics/animations/classicanimationscenario.h"
#include "holdmanager.h" #include "holdmanager.h"
ClassicArrowNote::ClassicArrowNote(ArrowNoteInitializer&& init) : 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),
_hold_manager(hold_manager),
_is_hold(init.hold) _is_hold(init.hold)
{ {
_elements.resize(init.elements.size()); _elements.resize(init.elements.size());
@ -53,7 +54,7 @@ void ClassicArrowNote::input(kku::GameEvent&& input)
{ {
grade = _evaluator.calculatePrecision(input.timestamp); grade = _evaluator.calculatePrecision(input.timestamp);
if (isHold()) if (isHold())
_context->hold_manager->emplace(this); _hold_manager->emplace(this);
} }
if (all_pressed || !input_valid) if (all_pressed || !input_valid)

View File

@ -1,9 +1,11 @@
#pragma once #pragma once
#include "arrowelement.h" #include "arrowelement.h"
#include "classicnote.h" #include "classicmode/classicnote.h"
#include "initializers/arrownoteinitializer.h" #include "classicmode/arrownoteinitializer.h"
class HoldManager;
class ClassicArrowNote : public ClassicNote class ClassicArrowNote : public ClassicNote
{ {
@ -15,7 +17,7 @@ public:
BAD BAD
}; };
explicit ClassicArrowNote(ArrowNoteInitializer&& init); explicit ClassicArrowNote(ArrowNoteInitializer&& init, const std::shared_ptr<HoldManager>& hold_manager);
virtual ~ClassicArrowNote() = default; virtual ~ClassicArrowNote() = default;
virtual bool isActive(const kku::microsec& offset) const override; virtual bool isActive(const kku::microsec& offset) const override;
@ -31,6 +33,7 @@ public:
private: private:
const kku::PrecisionEvaluator<Grade> _evaluator; const kku::PrecisionEvaluator<Grade> _evaluator;
const std::shared_ptr<HoldManager> _hold_manager;
std::vector<ArrowElement> _elements; std::vector<ArrowElement> _elements;
bool _is_hold; bool _is_hold;

View File

@ -1,5 +1,5 @@
#include "classicgame.h" #include "classicgame.h"
#include "classicnote.h" #include "classicmode/classicnote.h"
#include "classicmapcreator.h" #include "classicmapcreator.h"
#include "graphics/classicscenegraphicsmanager.h" #include "graphics/classicscenegraphicsmanager.h"
#include "holdmanager.h" #include "holdmanager.h"
@ -16,9 +16,7 @@ ClassicGame::~ClassicGame()
void ClassicGame::run() void ClassicGame::run()
{ {
_context.hold_manager = _hold_manager; auto beatmap = classic::createBeatmap("aa", _hold_manager);
auto beatmap = classic::createBeatmap("aa", _context);
_timeline->setNotes(beatmap.notes); _timeline->setNotes(beatmap.notes);
} }

View File

@ -5,10 +5,7 @@
#include "core/game.h" #include "core/game.h"
#include "core/timeline.h" #include "core/timeline.h"
#include "classicmode/classicnote.h"
#include "classicmode/context.h"
#include "classicnote.h"
#include "classicmode/classicactions.h" #include "classicmode/classicactions.h"
class ClassicGraphicsManager; class ClassicGraphicsManager;
@ -30,7 +27,5 @@ public:
private: private:
const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline; const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline;
const std::shared_ptr<ClassicGraphicsManager> _graphics_manager; const std::shared_ptr<ClassicGraphicsManager> _graphics_manager;
std::shared_ptr<HoldManager> _hold_manager; const std::shared_ptr<HoldManager> _hold_manager;
Context _context;
}; };

View File

@ -6,7 +6,7 @@
#include "graphics/animations/classicdyinganimationscenario.h" #include "graphics/animations/classicdyinganimationscenario.h"
// //
auto classic::createBeatmap(const std::string& filepath, const Context &context) -> Beatmap auto classic::createBeatmap(const std::string& filepath, const std::shared_ptr<HoldManager>& hold_manager) -> Beatmap
{ {
(void) filepath; (void) filepath;
@ -36,7 +36,6 @@ auto classic::createBeatmap(const std::string& filepath, const Context &context)
init.initializer.intervals = input_intervals; init.initializer.intervals = input_intervals;
init.initializer.perfect_offset = bpm_iterator; init.initializer.perfect_offset = bpm_iterator;
init.hold = false; init.hold = false;
init.initializer.context = &context;
element.element.position = kku::Point(x, 390.f); element.element.position = kku::Point(x, 390.f);
element.element.falling_curve_interpolation = {}; element.element.falling_curve_interpolation = {};
@ -60,7 +59,7 @@ auto classic::createBeatmap(const std::string& filepath, const Context &context)
init.elements = {element}; init.elements = {element};
notes.insert(new ClassicArrowNote(std::move(init))); notes.insert(new ClassicArrowNote(std::move(init), hold_manager));
bpm_iterator += tempo_interval; bpm_iterator += tempo_interval;
x += 70; x += 70;

View File

@ -3,7 +3,9 @@
#include <set> #include <set>
#include "core/time.h" #include "core/time.h"
#include "classicnote.h" #include "classicmode/classicnote.h"
class HoldManager;
namespace classic namespace classic
{ {
@ -14,7 +16,7 @@ struct Beatmap
kku::microsec visibility_offset; kku::microsec visibility_offset;
}; };
Beatmap createBeatmap(const std::string& filepath, const Context& context); Beatmap createBeatmap(const std::string& filepath, const std::shared_ptr<HoldManager>& hold_manager);
} }

View File

@ -52,5 +52,5 @@ std::shared_ptr<ClassicSprite> ClassicGraphicsFactory::createSprite(Type type) c
std::shared_ptr<ClassicSelection> ClassicGraphicsFactory::createSelection() const std::shared_ptr<ClassicSelection> ClassicGraphicsFactory::createSelection() const
{ {
const auto shape = _core_factory->getRectangle(); const auto shape = _core_factory->getRectangle();
return std::make_shared<ClassicSelection>(ClassicSelection::Init{shape, kku::Color{51, 153, 255, 0}}); return std::make_shared<ClassicSelection>(ClassicSelection::Init{shape, kku::Color{51, 153, 255, 120}});
} }

View File

@ -1,6 +1,6 @@
#include "classicscenegraphicsmanager.h" #include "classicscenegraphicsmanager.h"
#include "game/mockelement.h" #include "editor/mockelement.h"
#include "game/arrowelement.h" #include "game/arrowelement.h"
#include "graphics/animations/classicflyinganimationscenario.h" #include "graphics/animations/classicflyinganimationscenario.h"
@ -92,7 +92,6 @@ void ClassicSceneGraphicsManager::display(const std::vector<MockElement>& elemen
for (std::size_t i = 0; i < elements.size(); ++i) for (std::size_t i = 0; i < elements.size(); ++i)
{ {
const auto& sprite = elements[i].sprite; const auto& sprite = elements[i].sprite;
const auto& selection = elements[i].selection;
if (i >= 1) if (i >= 1)
{ {
@ -105,7 +104,8 @@ void ClassicSceneGraphicsManager::display(const std::vector<MockElement>& elemen
} }
sprite->display(); sprite->display();
selection->display(); if (elements[i].selected)
elements[i].selection->display();
} }
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "game/classicnote.h" #include "classicmode/classicnote.h"
#include "graphics/classicgraphicsmanager.h" #include "graphics/classicgraphicsmanager.h"
#include "graphics/classicgraphicsfactory.h" #include "graphics/classicgraphicsfactory.h"
#include "core/timeline.h" #include "core/timeline.h"

View File

@ -11,7 +11,7 @@ ClassicSelection::ClassicSelection(ClassicSelection::Init&& init) :
void ClassicSelection::reset() void ClassicSelection::reset()
{ {
_shape->setPosition(kku::Point{0, 0}); _shape->setPosition(kku::Point{0, 0});
_shape->setColor(kku::Color{0, 0, 0, 0}); _shape->setColor(kku::Color{51, 153, 255, 120});
} }
void ClassicSelection::display() const void ClassicSelection::display() const
@ -23,12 +23,3 @@ void ClassicSelection::adjustTo(const std::shared_ptr<ClassicSprite>& sprite)
{ {
_shape->setRect(sprite->getRectangle()->getRect()); _shape->setRect(sprite->getRectangle()->getRect());
} }
void ClassicSelection::toggle(bool selected)
{
_fill_color.alpha = selected
? 120
: 0;
_shape->setColor(_fill_color);
}

View File

@ -23,7 +23,6 @@ public:
virtual void display() const override; virtual void display() const override;
void adjustTo(const std::shared_ptr<ClassicSprite>& sprite); void adjustTo(const std::shared_ptr<ClassicSprite>& sprite);
void toggle(bool selected);
protected: protected:
kku::Color _fill_color; kku::Color _fill_color;

View File

@ -3,7 +3,6 @@
#include "core/note.h" #include "core/note.h"
#include "core/precisionevaluator.h" #include "core/precisionevaluator.h"
#include "classicmode/noteinitializer.h" #include "classicmode/noteinitializer.h"
#include "classicmode/context.h"
class ClassicGraphicsManager; class ClassicGraphicsManager;
@ -36,5 +35,4 @@ public:
protected: protected:
State _state; State _state;
const Context *_context;
}; };

View File

@ -1,10 +0,0 @@
#pragma once
#include <memory>
class HoldManager;
struct Context
{
std::shared_ptr<HoldManager> hold_manager;
};

View File

@ -3,12 +3,10 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "context.h"
#include "core/time.h" #include "core/time.h"
struct NoteInitializer struct NoteInitializer
{ {
const Context *context;
std::vector<kku::microsec> intervals; std::vector<kku::microsec> intervals;
kku::microsec perfect_offset = 0; kku::microsec perfect_offset = 0;
}; };