Implement scrolling in Editor

This commit is contained in:
NaiJi ✨ 2021-12-06 22:18:04 +03:00
parent 5e3ddccac0
commit ebf736a0bb
8 changed files with 78 additions and 13 deletions

5
.gitignore vendored
View File

@ -4,4 +4,7 @@
build
SFML*
*.user
*CMakeFiles*
*Makefile
*.cmake*
*.cbp

View File

@ -36,7 +36,7 @@ public:
void insertNote(TNote* note)
{
_timeline.insert(_top_note, note);
_top_note = _timeline.insert(note).first;
update(_current_offset);
}
@ -61,6 +61,7 @@ public:
if (isExpired(_top_note))
return;
checkTopNote(_current_offset);
checkCurrentActiveNote();
checkForNextActiveNote();
updateVisibleSprites(_current_offset);
@ -86,7 +87,7 @@ public:
return;
Iterator note_iterator = _top_note;
while (isVisiblyClose(note_iterator, music_offset))
while (!isExpired(note_iterator) && isVisiblyClose(note_iterator, music_offset))
{
if (nothingToDraw())
_first_visible_note = note_iterator;
@ -173,6 +174,15 @@ private:
}
}
void checkTopNote(const microsec& offset)
{
if (isExpired(_top_note) || !isExpired(_active_note))
return;
while ((*_top_note)->offset() < offset)
++_top_note;
}
void checkForNextActiveNote()
{
if (!isExpired(_active_note))

View File

@ -3,10 +3,34 @@
ClassicEditor::ClassicEditor(std::shared_ptr<ClassicGraphicsManager>&& manager) :
_graphics_manager(manager),
_selected_type(Type::UP),
_current_time(0)
_current_time(0),
_scroll_step(500000)
{
_context.graphics_manager = _graphics_manager;
_timeline.setNotes({}, 1648648);
int basic_offset = 500000 * 6;
std::set<MockClassicNote*, NotePtrCompt> _set = {};
for (int i = 1; i < 5; ++i)
{
NoteInitializer init;
init.context = &_context;
init.intervals = {};
init.perfect_offset = basic_offset + (500000 * i);
ElementInitializer elem_init;
elem_init.type = _selected_type;
elem_init.coordinates = Coordinates{ 700 - (i * 120), 550 };
elem_init.falling_curve_interpolation = {};
MockArrowNoteInitializer mock_init;
mock_init.elements = {elem_init};
mock_init.initializer = init;
_set.insert(new MockClassicNote(std::move(mock_init)));
}
_timeline.setNotes(_set, 1648648);
}
void ClassicEditor::input(PlayerInput&& inputdata)

View File

@ -28,4 +28,5 @@ private:
Type _selected_type;
microsec _current_time;
microsec _scroll_step;
};

View File

@ -6,6 +6,8 @@
#include "graphics/classicdyinganimationscenario.h"
//
#include <iostream>
// A LOT OF CODE DUPLICATES game/arrowclassicnote, DO SOMETHING D:<
MockClassicNote::MockClassicNote(MockArrowNoteInitializer&& init) :
@ -49,6 +51,8 @@ void MockClassicNote::putToGame(const microsec &music_offset)
{
_state = State::FLYING;
std::cout << "Put to game " << this << ": " << music_offset << '\n';
for (auto& element : _elements)
{
element.sprite = _context->graphics_manager->getSprite(element.type);
@ -67,11 +71,6 @@ void MockClassicNote::update(const microsec &music_offset)
case State::FLYING:
if (music_offset > offset())
_state = State::DYING;
break;
case State::DYING:
if (_elements[0].animations[_state]->isDone())
_state = State::DEAD;
break;
}

View File

@ -156,7 +156,10 @@ void EditorState::enter()
callbacks.onInput = [&editor, &music](const sf::Event& event)
{
editor->input(PlayerInput{music.fetchOffset(), event});
if (event.type == sf::Event::MouseWheelScrolled)
music.moveOffset(event.mouseWheelScroll.delta > 0 ? 500000 : -500000);
else
editor->input(PlayerInput{music.fetchOffset(), event});
};
callbacks.onUpdate = [&editor, &music](const sf::Time& dt)
@ -167,9 +170,9 @@ void EditorState::enter()
auto editor_widget = std::make_shared<EditorWidget>(std::move(callbacks));
_group = std::make_shared<Group>();
_group->addChild(editor_widget);
_group->addChild(menu_bar);
_group->addChild(bpm_widget);
_group->addChild(editor_widget);
}
void EditorState::leave()
@ -177,4 +180,3 @@ void EditorState::leave()
_group.reset();
_bpm_calculator.reset();
}

View File

@ -21,7 +21,9 @@ public:
void setVolume(int volume);
void setOffset(const microsec& offset);
void moveOffset(const microsec& delta);
microsec fetchOffset();
microsec getDuration() const;
private:
sf::Music _music;

View File

@ -61,3 +61,27 @@ microsec Music::fetchOffset()
return _absolute_offset;
}
void Music::moveOffset(const microsec& delta)
{
const auto offset = fetchOffset();
const auto result = offset + delta;
if (result < 0)
{
setOffset(0);
}
else if (result > getDuration())
{
setOffset(_music.getDuration().asMicroseconds());
pause();
}
else
{
setOffset(result);
}
}
microsec Music::getDuration() const
{
return _music.getDuration().asMicroseconds();
}