From ebf736a0bb8a148619bcc17e7b47dacfe9e00311 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 6 Dec 2021 22:18:04 +0300 Subject: [PATCH] Implement scrolling in Editor --- .gitignore | 5 +++- core/shared/core/timeline.h | 14 ++++++++-- modes/classicmode/editor/classiceditor.cpp | 28 ++++++++++++++++++-- modes/classicmode/editor/classiceditor.h | 1 + modes/classicmode/editor/mockclassicnote.cpp | 9 +++---- src/application/editorstate.cpp | 8 +++--- tools/shared/tools/music.h | 2 ++ tools/src/music.cpp | 24 +++++++++++++++++ 8 files changed, 78 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index f40fc7b..eba4bb2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ build SFML* *.user - +*CMakeFiles* +*Makefile +*.cmake* +*.cbp diff --git a/core/shared/core/timeline.h b/core/shared/core/timeline.h index d5122ab..f235105 100644 --- a/core/shared/core/timeline.h +++ b/core/shared/core/timeline.h @@ -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)) diff --git a/modes/classicmode/editor/classiceditor.cpp b/modes/classicmode/editor/classiceditor.cpp index a6ec52a..cbc9cfa 100644 --- a/modes/classicmode/editor/classiceditor.cpp +++ b/modes/classicmode/editor/classiceditor.cpp @@ -3,10 +3,34 @@ ClassicEditor::ClassicEditor(std::shared_ptr&& 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 _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) diff --git a/modes/classicmode/editor/classiceditor.h b/modes/classicmode/editor/classiceditor.h index 5ae51c8..02893c2 100644 --- a/modes/classicmode/editor/classiceditor.h +++ b/modes/classicmode/editor/classiceditor.h @@ -28,4 +28,5 @@ private: Type _selected_type; microsec _current_time; + microsec _scroll_step; }; diff --git a/modes/classicmode/editor/mockclassicnote.cpp b/modes/classicmode/editor/mockclassicnote.cpp index 8c5be43..fd4d5c8 100644 --- a/modes/classicmode/editor/mockclassicnote.cpp +++ b/modes/classicmode/editor/mockclassicnote.cpp @@ -6,6 +6,8 @@ #include "graphics/classicdyinganimationscenario.h" // +#include + // 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; } diff --git a/src/application/editorstate.cpp b/src/application/editorstate.cpp index c21e637..6fde314 100644 --- a/src/application/editorstate.cpp +++ b/src/application/editorstate.cpp @@ -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(std::move(callbacks)); _group = std::make_shared(); + _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(); } - diff --git a/tools/shared/tools/music.h b/tools/shared/tools/music.h index 2d597ab..a997ebc 100644 --- a/tools/shared/tools/music.h +++ b/tools/shared/tools/music.h @@ -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; diff --git a/tools/src/music.cpp b/tools/src/music.cpp index 91f678a..45cee13 100644 --- a/tools/src/music.cpp +++ b/tools/src/music.cpp @@ -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(); +}