2021-04-08 15:34:03 +02:00
|
|
|
#include "timeline.h"
|
|
|
|
#include "note.h"
|
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
#include <SFML/Graphics/RenderTarget.hpp>
|
2021-04-08 21:51:59 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2021-04-08 15:34:03 +02:00
|
|
|
Timeline::Timeline()
|
|
|
|
{
|
|
|
|
// BPM of METEOR is 170.
|
|
|
|
// Length is 1:14
|
|
|
|
// I calculated that the time between beats is about 1412162 microseconds
|
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
_timeline.reserve(1000);
|
|
|
|
|
2021-04-08 15:34:03 +02:00
|
|
|
microsec starting_beat_offset = 372162;
|
|
|
|
int amount_of_beats = 209;
|
2021-04-09 15:28:45 +02:00
|
|
|
microsec interval = 1412162;
|
2021-04-08 15:34:03 +02:00
|
|
|
microsec note_input_offset = 412162;
|
2021-04-09 15:28:45 +02:00
|
|
|
microsec bpm_iterator = starting_beat_offset;
|
|
|
|
microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);
|
|
|
|
_visibility_offset = note_input_offset * 3;
|
2021-04-08 15:34:03 +02:00
|
|
|
|
2021-04-08 21:51:59 +02:00
|
|
|
Note::resetPrecisionQualifier(note_input_offset / 3);
|
2021-04-08 15:34:03 +02:00
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
while (bpm_iterator < bpm_end)
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
2021-04-09 15:28:45 +02:00
|
|
|
_timeline.emplace_back(new Note(bpm_iterator, note_input_offset));
|
|
|
|
bpm_iterator += interval;
|
2021-04-08 15:34:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
_timeline[0]->setPosition({200, 200});
|
|
|
|
_timeline[1]->setPosition({250, 200});
|
|
|
|
_timeline[2]->setPosition({300, 200});
|
|
|
|
_timeline[3]->setPosition({350, 200});
|
|
|
|
_timeline[4]->setPosition({400, 200});
|
|
|
|
_timeline[5]->setPosition({450, 200});
|
|
|
|
_timeline[6]->setPosition({200, 300});
|
|
|
|
_timeline[7]->setPosition({250, 300});
|
|
|
|
_timeline[8]->setPosition({300, 300});
|
|
|
|
_timeline[9]->setPosition({350, 300});
|
|
|
|
_timeline[10]->setPosition({400, 300});
|
|
|
|
_timeline[11]->setPosition({450, 300});
|
|
|
|
|
2021-04-08 15:34:03 +02:00
|
|
|
_active_note = nullptr;
|
2021-04-09 15:28:45 +02:00
|
|
|
_last_visible_note = _timeline.end();
|
2021-04-08 15:34:03 +02:00
|
|
|
_top_note = _timeline.begin();
|
2021-04-09 15:28:45 +02:00
|
|
|
|
|
|
|
prepareNotesToDraw(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::prepareNotesToDraw(const microsec &music_offset)
|
|
|
|
{
|
|
|
|
auto note_iterator = _top_note;
|
|
|
|
|
|
|
|
while (((*note_iterator)->offset() - _visibility_offset) >= music_offset)
|
|
|
|
++note_iterator;
|
|
|
|
|
|
|
|
_last_visible_note = note_iterator;
|
2021-04-08 15:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Timeline::~Timeline()
|
2021-04-09 15:28:45 +02:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::clear()
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
|
|
|
for (auto note : _timeline)
|
|
|
|
delete note;
|
|
|
|
|
|
|
|
_timeline.clear();
|
|
|
|
_top_note = _timeline.end();
|
2021-04-09 15:28:45 +02:00
|
|
|
_last_visible_note = _timeline.end();
|
2021-04-08 15:34:03 +02:00
|
|
|
_active_note = nullptr;
|
|
|
|
|
|
|
|
Note::resetPrecisionQualifier();
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
static sf::RectangleShape createNoteGlyph(const std::vector<Note*>::const_iterator& note_to_draw)
|
|
|
|
{ // Temporary solution
|
|
|
|
sf::RectangleShape ret;
|
|
|
|
const auto position = (*note_to_draw)->position();
|
|
|
|
ret.setPosition(position.x, position.y);
|
|
|
|
ret.setFillColor(sf::Color(255, 100, 0));
|
|
|
|
ret.setSize({10.f, 10.f});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::draw(sf::RenderTarget& target, sf::RenderStates states) const // Temporary solution
|
|
|
|
{
|
|
|
|
if (_last_visible_note == _timeline.end() || _top_note > _last_visible_note)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto note_to_draw = _top_note;
|
|
|
|
while (note_to_draw != (_last_visible_note + 1))
|
|
|
|
{
|
|
|
|
target.draw(createNoteGlyph(note_to_draw), states);
|
|
|
|
++note_to_draw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::update(const microsec &music_offset)
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
2021-04-09 15:28:45 +02:00
|
|
|
checkCurrentActiveNote(music_offset);
|
|
|
|
checkForNextActiveNote(music_offset);
|
|
|
|
prepareNotesToDraw(music_offset);
|
2021-04-08 15:34:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
void Timeline::checkCurrentActiveNote(const microsec &music_offset)
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
2021-04-09 15:28:45 +02:00
|
|
|
if (_active_note && !_active_note->isActive(music_offset))
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
|
|
|
_active_note = nullptr;
|
|
|
|
++_top_note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:28:45 +02:00
|
|
|
void Timeline::checkForNextActiveNote(const microsec &music_offset)
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
2021-04-09 15:28:45 +02:00
|
|
|
if (!_active_note && (*_top_note)->isActive(music_offset))
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
2021-04-09 15:28:45 +02:00
|
|
|
std::cout << "New active note: " << music_offset << '\n';
|
2021-04-08 15:34:03 +02:00
|
|
|
_active_note = *_top_note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 17:03:35 +02:00
|
|
|
Note* Timeline::fetchActiveNote(const microsec &music_offset) noexcept
|
2021-04-08 15:34:03 +02:00
|
|
|
{
|
2021-04-09 15:28:45 +02:00
|
|
|
std::cout << "Clicked at: " << music_offset << '\n';
|
|
|
|
update(music_offset);
|
2021-04-08 15:34:03 +02:00
|
|
|
return _active_note;
|
|
|
|
}
|