Add offset adjustment for note insertion

This commit is contained in:
NaiJi ✨ 2021-12-09 10:55:53 +03:00
parent 94e3793ec6
commit 0cf711db94
6 changed files with 90 additions and 19 deletions

View File

@ -7,6 +7,7 @@ struct BPMSection
int bpm = 120; // Hi, osu
int fraction = 2;
microsec offset_start = 0;
microsec interval = 0;
};
struct BPMSectionCompt

View File

@ -15,43 +15,68 @@ public:
virtual void draw() const = 0;
virtual void recalculate(const microsec& timestamp) = 0;
inline void setBPMSections(const std::set<BPMSection, BPMSectionCompt>& sections) noexcept
void setBPMSections(const std::set<BPMSection, BPMSectionCompt>& sections) noexcept
{
_bpm_sections = sections;
}
inline void setBPMSections(std::set<BPMSection, BPMSectionCompt>&& sections) noexcept
void setBPMSections(std::set<BPMSection, BPMSectionCompt>&& sections) noexcept
{
_bpm_sections = std::move(sections);
}
void insertBPMSection(const BPMSection& section)
bool insertBPMSection(const BPMSection& section)
{
_bpm_sections.insert(section);
return _bpm_sections.insert(section).second;
}
void insertBPMSection(BPMSection&& section)
bool insertBPMSection(BPMSection&& section)
{
_bpm_sections.insert(std::move(section));
return _bpm_sections.insert(std::move(section)).second;
}
/*void removeBPMSectionAt(const microsec& offset)
bool removeBPMSectionAt(const microsec& offset)
{
const auto section_it = std::find_if(_bpm_sections.rbegin(), _bpm_sections.rend(),
[offset](const auto& section)
{
return section.start_offset < offset;
});
auto section_it = std::find_if(_bpm_sections.rbegin(), _bpm_sections.rend(),
[offset](const auto& section)
{
return section.offset_start < offset;
});
if (section_it != _bpm_sections.rend())
_bpm_sections.erase((section_it + 1).base());
}*/
{
++section_it;
_bpm_sections.erase(section_it.base());
inline void clearBPMSections() noexcept
return true;
}
return false;
}
BPMSection getBPMSectionAt(const microsec& offset) const
{
auto section_it = std::find_if(_bpm_sections.rbegin(), _bpm_sections.rend(),
[offset](const auto& section)
{
return section.offset_start < offset;
});
if (section_it != _bpm_sections.rend())
{
++section_it;
return *section_it.base();
}
return BPMSection();
}
void clearBPMSections() noexcept
{
_bpm_sections.clear();
}
protected:
std::set<BPMSection, BPMSectionCompt> _bpm_sections;
std::set<microsec> _ticks;
};

View File

@ -30,6 +30,49 @@ ClassicEditor::ClassicEditor(std::shared_ptr<ClassicGraphicsManager>&& manager)
_timeline.setNotes(_set, 1648648);
}
microsec ClassicEditor::adjustOffset(microsec offset) const noexcept
{
microsec left = -1;
microsec right = -1;
std::set<microsec>::iterator i;
for (i = _ticks.begin(); i != _ticks.end(); ++i)
{
if (*i >= offset)
{
right = *i;
break;
}
}
for (; i != _ticks.end(); --i)
{
if (*i <= offset)
{
left = *i;
break;
}
}
if (right == -1)
offset = left;
else if (left == -1)
offset = right;
else
{
const microsec right_distance = right - offset;
const microsec left_distance = offset - left;
offset = (right_distance > left_distance)
? left_distance
: right_distance;
}
return offset;
}
void ClassicEditor::input(PlayerInput&& inputdata)
{
_current_time = inputdata.timestamp;
@ -44,12 +87,12 @@ void ClassicEditor::input(PlayerInput&& inputdata)
case sf::Event::MouseButtonPressed:
{
const auto note = _timeline.getNoteBy(_current_time);
if (_timeline.isExpired(note))
if (_timeline.isExpired(note) && !_ticks.empty())
{
NoteInitializer init;
init.context = &_context;
init.intervals = {};
init.perfect_offset = _current_time;
init.perfect_offset = adjustOffset(_current_time);
ElementInitializer elem_init;
elem_init.type = _selected_type;

View File

@ -22,6 +22,8 @@ public:
void selectNoteType(Type type) noexcept;
private:
inline microsec adjustOffset(microsec offset) const noexcept;
Context _context;
std::shared_ptr<ClassicGraphicsManager> _graphics_manager;

View File

@ -9,7 +9,7 @@
#include "core/game.h"
#include "core/timeline.h"
#include "context.h"
#include "classicmode/context.h"
#include "classicnote.h"
#include "classicmode/classicactions.h"

View File

@ -1,6 +1,6 @@
#pragma once
#include "context.h"
#include "classicmode/context.h"
#include "core/note.h"
#include "core/precisionevaluator.h"
#include "classicmode/noteinitializer.h"