Compare commits

..

1 Commits

Author SHA1 Message Date
NaiJi ✨ 1401e51918 chore: Inverse if-else tree in Timeline 2023-02-16 04:05:39 +04:00
15 changed files with 107 additions and 98 deletions

View File

@ -23,10 +23,7 @@ class PrecisionEvaluator
_end_handling_offset = _offset + intervals.back();
}
inline microsec offset() const noexcept
{
return _offset;
}
inline microsec offset() const noexcept { return _offset; }
inline bool isActive(microsec music_play_offset) const noexcept
{
@ -38,6 +35,9 @@ class PrecisionEvaluator
{
microsec shift_from_perfect = std::abs(odds - offset());
std::cout << "Shift " << ((odds > _offset) ? "late: " : "early: ")
<< shift_from_perfect << "\n";
std::size_t raw_grade;
for (raw_grade = 0; raw_grade < _intervals.size(); ++raw_grade)
{

View File

@ -15,7 +15,9 @@ template <class TNote,
class Timeline
{
public:
explicit Timeline() : _current_offset(0) {}
explicit Timeline() : _current_offset(0)
{
}
typedef typename std::set<TNote *>::const_iterator Iterator;
@ -24,30 +26,30 @@ class Timeline
_current_offset = offset;
expire(_top_note);
if (!_timeline.empty())
if (_timeline.empty())
return;
Iterator head_iterator = _timeline.begin();
while (!isExpired(head_iterator))
{
Iterator head_iterator = _timeline.begin();
while (!isExpired(head_iterator))
if ((*head_iterator)->getPerfectOffset() >= offset)
{
if ((*head_iterator)->getPerfectOffset() >= offset)
{
Iterator pre_head = head_iterator;
--pre_head;
Iterator pre_head = head_iterator;
--pre_head;
_top_note =
!isExpired(pre_head) && (*pre_head)->isActive(offset)
? pre_head
: head_iterator;
break;
}
++head_iterator;
_top_note =
!isExpired(pre_head) && (*pre_head)->isActive(offset)
? pre_head
: head_iterator;
break;
}
if (isExpired(_top_note))
_top_note = _timeline.begin();
++head_iterator;
}
if (isExpired(_top_note))
_top_note = _timeline.begin();
}
void setNotes(const std::set<TNote *, NotePtrComparator> &notes)
@ -128,11 +130,20 @@ class Timeline
iterator = _timeline.end();
}
inline Iterator getTopNote() const noexcept { return _top_note; }
inline Iterator getTopNote() const noexcept
{
return _top_note;
}
inline Iterator begin() const noexcept { return _timeline.begin(); }
inline Iterator begin() const noexcept
{
return _timeline.begin();
}
inline Iterator end() const noexcept { return _timeline.end(); }
inline Iterator end() const noexcept
{
return _timeline.end();
}
private:
std::set<TNote *, NotePtrComparator> _timeline;

View File

@ -35,7 +35,7 @@ class Log
void log(LogLevel level, const std::string &fmt, ...);
private:
inline std::string getLabel(LogLevel level)
inline std::string _getLabel(LogLevel level)
{
switch (level)
{

View File

@ -23,5 +23,5 @@ void Log::log(LogLevel level, const std::string &fmt, ...)
std::vsnprintf(&buf[0], len + 1, fmt.c_str(), args);
va_end(args);
std::cout << this->getLabel(level) << " " << &buf[0] << std::endl;
std::cout << this->_getLabel(level) << " " << &buf[0] << std::endl;
}

View File

@ -51,8 +51,8 @@ void ApplicationSFML::run()
}
}
bool is_frame_passed = time_since_last_update >= TIME_PER_FRAME;
if (is_frame_passed)
bool isOneFramePassed = time_since_last_update >= TIME_PER_FRAME;
if (isOneFramePassed)
{
time_since_last_update -= TIME_PER_FRAME;
update(time_since_last_update.asMicroseconds());

View File

@ -36,7 +36,8 @@ classic::getEditor(const std::shared_ptr<kku::CoreFactory> &core_factory)
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 selection_manager =
std::make_shared<SelectionManager<ClassicNote>>();
std::vector<std::shared_ptr<ClassicGraphicsManager>> graphics_managers;
graphics_managers.emplace_back(
std::make_shared<ClassicSceneGraphicsManager>(timeline, factory,

View File

@ -43,7 +43,6 @@ ClassicEditor::ClassicEditor(
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};
@ -104,6 +103,7 @@ void ClassicEditor::input(kku::GameEvent &&input)
switch (input.event.type)
{
default:
break;
@ -153,6 +153,8 @@ void ClassicEditor::input(kku::GameEvent &&input)
if (!_context->getSelectionManager()->isMultiselectionEnabled())
_context->getSelectionManager()->discard();
//_graphics_manager->input(std::move(input));
break;
}
}

View File

@ -5,7 +5,7 @@
#include "graphics/classicnotegraphics.h"
EditorContext::EditorContext(
const std::shared_ptr<SelectionManager> &selection_manager,
const std::shared_ptr<SelectionManager<ClassicNote>> &selection_manager,
std::vector<std::shared_ptr<ClassicGraphicsManager>> &&graphics_managers)
: _selection_manager(selection_manager),
_graphics_managers(std::move(graphics_managers))
@ -14,27 +14,41 @@ EditorContext::EditorContext(
void EditorContext::input(ClassicArrowNote *note, kku::GameEvent &&input) const
{
(void)note;
switch (input.event.type)
{
default:
break;
case kku::SystemEvent::Type::MousePress:
{
const auto clicked_position =
std::get<kku::SystemEvent::Mouse>(input.event.data).position;
for (auto &element : note->getElements())
/*case kku::SystemEvent::Type::MousePress:
{
if (element.sprite->getRectangle()->contains(clicked_position))
bool selection_changed = false;
std::size_t amount_selected = 0;
const auto position =
std::get<kku::SystemEvent::Mouse>(input.event.data).position; for (auto&
element : note->getElements())
{
if (_selection_manager->isSelected(note->getId(), element.id))
_selection_manager->remove(note->getId(), element.id);
else
_selection_manager->fetch(note->getId(), element.id);
if (element.sprite->getRectangle()->contains(position))
{
element.selected = !element.selected;
selection_changed = true;
if (element.selected)
++amount_selected;
}
}
}
break;
}
if (selection_changed)
{
if (amount_selected > 0)
_selection_manager->fetch(note);
else
_selection_manager->remove(note);
}
break;
}*/
}
}
@ -81,7 +95,8 @@ void EditorContext::update(ClassicArrowNote *note,
element.animations[note->getState()]->update(music_offset);
}
std::shared_ptr<SelectionManager> EditorContext::getSelectionManager() const
std::shared_ptr<SelectionManager<ClassicNote>>
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>& selection_manager,
explicit EditorContext(const std::shared_ptr<SelectionManager<ClassicNote>>& 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> getSelectionManager() const;
std::shared_ptr<SelectionManager<ClassicNote>> getSelectionManager() const;
private:
const std::shared_ptr<SelectionManager> _selection_manager;
const std::shared_ptr<SelectionManager<ClassicNote>> _selection_manager;
const std::vector<std::shared_ptr<ClassicGraphicsManager>> _graphics_managers;
};

View File

@ -1,54 +1,51 @@
#pragma once
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
template <class T>
class SelectionManager
{
public:
explicit SelectionManager() :
SelectionManager() :
_multiselection_enabled(false)
{}
void discard()
{
_selected_ids.clear();
_selected_things.clear();
}
void fetch(int note_id, int element_id)
void fetch(T * const thing)
{
if (!isSelected(note_id, element_id))
_selected_ids[note_id].emplace_back(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);
}
void remove(int note_id, int element_id)
void remove(T * const thing)
{
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)
for (std::size_t i = 0; i < _selected_things.size(); ++i)
{
if (element_id == elements_ids.at(i))
if (thing == _selected_things.at(i))
{
elements_ids[i] = std::move(elements_ids.back());
elements_ids.pop_back();
_selected_things[i] = std::move(_selected_things.back());
_selected_things.pop_back();
break;
}
}
}
bool isSelected(int note_id, int element_id) const
bool isSelected(const T * const thing) const
{
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)
for (const auto& selected_thing : thing)
if (thing == selected_thing)
return true;
return false;
@ -64,7 +61,13 @@ public:
return _multiselection_enabled;
}
void apply(const std::function<void(T*)>& function)
{
for (auto& thing : _selected_things)
function(thing);
}
private:
std::map<int, std::vector<int>> _selected_ids;
std::vector<T*> _selected_things;
bool _multiselection_enabled;
};

View File

@ -10,12 +10,10 @@
#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;
@ -26,7 +24,6 @@ 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,7 +7,6 @@
#include <vector>
class ClassicArrowNote;
class SelectionManager;
class ArrowElement;
// class ClassicSliderNote;
@ -23,14 +22,11 @@ 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

@ -87,17 +87,6 @@ void ClassicSceneGraphicsManager::draw(const ClassicArrowNote *const note) const
}
}
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)
{

View File

@ -25,12 +25,10 @@ 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,9 +25,6 @@ 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;