From ada48aefddf1d01e5b6c58f5cd531dd9181e274d Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 22 Nov 2026 18:58:37 -0500 Subject: [PATCH] Name row drops, merge sources, playback looping and spritesheet merge options items_move takes a DropZone, animations_merge MergeSources, Playback::tick PlaybackLoop, and spritesheets_merge SpritesheetMergeOptions. --- src/document.cpp | 7 ++----- src/document.h | 2 +- src/edit/animations.cpp | 4 ++-- src/edit/edit.h | 15 ++++++++++++--- src/edit/spritesheets.cpp | 3 ++- src/edit/tracks.cpp | 4 +++- src/model/common.h | 16 ++++++++++++++++ src/playback.cpp | 6 +++--- src/playback.h | 8 +++++++- src/ui/animation_preview.cpp | 4 ++-- src/ui/animations.cpp | 3 ++- src/ui/draw.h | 9 ++------- src/ui/spritesheets.cpp | 13 +++++++------ src/ui/timeline_context.h | 3 +-- src/ui/timeline_edit.cpp | 7 ++----- src/ui/timeline_tracks.cpp | 2 +- tests/common.h | 3 ++- tests/operations.cpp | 20 ++++++++++---------- tests/selection.cpp | 2 +- 19 files changed, 78 insertions(+), 53 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index 3a7a9e5..b5ecf04 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -645,12 +645,9 @@ bool Document::spritesheet_pack(int id, int padding) return true; } -bool Document::spritesheets_merge(const std::set& ids, bool isAppendRight, bool isMakeRegions, - bool isMakePrimaryRegion, Origin origin) +bool Document::spritesheets_merge(const std::set& ids, SpritesheetMergeOptions options) { - auto merged = ::spritesheets_merge( - model, ids, [this](int id) { return texture_get(id); }, isAppendRight, isMakeRegions, isMakePrimaryRegion, - origin); + auto merged = ::spritesheets_merge(model, ids, [this](int id) { return texture_get(id); }, options); if (!merged) return false; texture_set(*ids.begin(), std::move(*merged)); diff --git a/src/document.h b/src/document.h index 84803e6..7160197 100644 --- a/src/document.h +++ b/src/document.h @@ -157,7 +157,7 @@ public: void library_reload(int); bool regions_trim(int, const std::set&); bool spritesheet_pack(int, int); - bool spritesheets_merge(const std::set&, bool, bool, bool, Origin); + bool spritesheets_merge(const std::set&, SpritesheetMergeOptions); void hash_set(); void clean(); void change(); diff --git a/src/edit/animations.cpp b/src/edit/animations.cpp index 4da3068..8136a70 100644 --- a/src/edit/animations.cpp +++ b/src/edit/animations.cpp @@ -309,7 +309,7 @@ static void track_entries_merge(std::vector& destination, const std: // Merges source animations into the target (frames appended, prepended, replacing or ignored), optionally deleting // the sources. `ungroupIds` dissolves groups afterwards. Returns the merged animation's uid. -Uids animations_merge(Model& model, int target, std::set sources, Merge type, bool isDeleteAfter, +Uids animations_merge(Model& model, int target, std::set sources, Merge type, MergeSources sourcesAfter, const std::set& ungroupIds) { if (!model.animation_get(target)) @@ -346,7 +346,7 @@ Uids animations_merge(Model& model, int target, std::set sources, Merge typ } auto targetUid = model.animation_get(target)->uid; - if (isDeleteAfter) + if (sourcesAfter == SOURCES_DELETED) { sources.erase(target); animations_take(model, sources, {}); diff --git a/src/edit/edit.h b/src/edit/edit.h index ebaef25..1e35237 100644 --- a/src/edit/edit.h +++ b/src/edit/edit.h @@ -111,7 +111,7 @@ Uids root_bake_into(Model&, const std::set&, const std::set>&, const std::map>&); Uids items_group(Model&, int, int, const std::set&, const std::string&); -void items_move(Model&, int, int, const std::set&, const std::set&, RowTarget, bool, bool); +void items_move(Model&, int, int, const std::set&, const std::set&, RowTarget, DropZone); void animation_length_fit(Model&, int); int item_add(Model&, ItemType, int, int, Layer, Null, int, Destination); @@ -123,7 +123,7 @@ void animations_remove(Model&, const std::set&, const std::set&); Uids animations_move(Model&, const std::set&, const std::set&, AnimationTarget); void animations_group(Model&, const std::set&, const std::string&); Uids animations_paste(Model&, const std::string&, int, int, std::set&, std::string*); -Uids animations_merge(Model&, int, std::set, Merge, bool, const std::set&); +Uids animations_merge(Model&, int, std::set, Merge, MergeSources, const std::set&); std::string region_name_get(const std::string&, int); void regions_generate(Model&, const std::set&, const std::set&, const std::string&, RegionFrameMapping); @@ -134,7 +134,16 @@ Uids animation_grid_generate(Model&, const std::vector&, const GridOp Uids frames_change_apply(Model&, const ChangeTargets&, FrameChange, ChangeType); bool regions_trim(Model&, int, const std::set&, const Image&); std::optional spritesheet_pack(Model&, int, const Image&, int); -std::optional spritesheets_merge(Model&, const std::set&, const ImageGet&, bool, bool, bool, Origin); +// How spritesheets merge: side by side or stacked, and the regions made for each sheet. +struct SpritesheetMergeOptions +{ + bool isAppendRight = false; + bool isMakeRegions = false; + bool isMakePrimaryRegion = false; + Origin origin = Origin::CUSTOM; +}; + +std::optional spritesheets_merge(Model&, const std::set&, const ImageGet&, SpritesheetMergeOptions); // Where a spritesheet path of a document (this one, or a library) points, from this document. using SpritesheetPathGet = std::function; diff --git a/src/edit/spritesheets.cpp b/src/edit/spritesheets.cpp index 02c6f55..e1d610f 100644 --- a/src/edit/spritesheets.cpp +++ b/src/edit/spritesheets.cpp @@ -110,8 +110,9 @@ std::optional spritesheet_pack(Model& model, int id, const Image& sheet, // Appends sheets to the first, right or below. The result is an SVG (holding any PNG sheets as images) when any of // them is one. The first sheet's path takes the result's extension. std::optional spritesheets_merge(Model& model, const std::set& ids, const ImageGet& image_get, - bool isAppendRight, bool isMakeRegions, bool isMakePrimaryRegion, Origin origin) + SpritesheetMergeOptions options) { + auto [isAppendRight, isMakeRegions, isMakePrimaryRegion, origin] = options; if (ids.size() < 2) return {}; for (auto id : ids) diff --git a/src/edit/tracks.cpp b/src/edit/tracks.cpp index 5526e69..d45c63e 100644 --- a/src/edit/tracks.cpp +++ b/src/edit/tracks.cpp @@ -36,8 +36,10 @@ Uids items_group(Model& model, int animationIndex, int type, const std::set // Moves tracks and whole groups next to (or into) a target row. Layers are listed bottom-up, so for them "after" on // screen is "before" in the file. void items_move(Model& model, int animationIndex, int type, const std::set& ids, const std::set& groupIds, - RowTarget target, bool isDropAfter, bool isDropIntoGroup) + RowTarget target, DropZone zone) { + auto isDropAfter = zone != DROP_BEFORE; + auto isDropIntoGroup = zone == DROP_INSIDE; if ((target.groupId != -1 && groupIds.contains(target.groupId)) || (target.isGroup && groupIds.contains(target.id))) return; auto entries = track_entries_edit(model, animationIndex, type); diff --git a/src/model/common.h b/src/model/common.h index d3115a2..84bf53f 100644 --- a/src/model/common.h +++ b/src/model/common.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "vec.h" @@ -138,6 +139,21 @@ enum class ChangeType DIVIDE }; +// Where something dragged lands on a row: before it, into it (a group), or after it. +enum DropZone +{ + DROP_BEFORE, + DROP_INSIDE, + DROP_AFTER +}; + +// What an animation merge does with the animations merged in. +enum MergeSources +{ + SOURCES_KEPT, + SOURCES_DELETED +}; + // What a document is written as: the game's copy, the editor's own (embedded in a save), or clipboard text. enum SaveMode { diff --git a/src/playback.cpp b/src/playback.cpp index 4942f03..f3c72d6 100644 --- a/src/playback.cpp +++ b/src/playback.cpp @@ -18,9 +18,9 @@ void Playback::clamp(int length) } // Plays the frames [start, end). A playhead outside them starts at start. -void Playback::tick(int fps, int start, int end, bool isLoop, float deltaSeconds) +void Playback::tick(int fps, int start, int end, PlaybackLoop loop, float deltaSeconds) { - if (isLoop) + if (loop == PLAYBACK_LOOP) isFinished = false; if (isFinished || !isPlaying || fps <= 0 || end <= start) return; @@ -31,7 +31,7 @@ void Playback::tick(int fps, int start, int end, bool isLoop, float deltaSeconds time = (float)start; time += deltaSeconds * (float)fps; - if (isLoop) + if (loop == PLAYBACK_LOOP) { time = (float)start + std::fmod(time - (float)start, (float)(end - start)); return; diff --git a/src/playback.h b/src/playback.h index 770c396..32737bf 100644 --- a/src/playback.h +++ b/src/playback.h @@ -1,5 +1,11 @@ #pragma once +enum PlaybackLoop +{ + PLAYBACK_ONCE, + PLAYBACK_LOOP +}; + class Playback { public: @@ -9,7 +15,7 @@ public: void toggle(); void clamp(int); - void tick(int, int, int, bool, float); + void tick(int, int, int, PlaybackLoop, float); void decrement(int); void increment(int); }; diff --git a/src/ui/animation_preview.cpp b/src/ui/animation_preview.cpp index 502ce0c..9c0f858 100644 --- a/src/ui/animation_preview.cpp +++ b/src/ui/animation_preview.cpp @@ -163,8 +163,8 @@ void AnimationPreview::tick(Manager& manager, Settings& settings, float deltaSec auto range = animation_play_range_get(*animation); if (!wasPlaybackPlaying) soundFrame = (int)std::floor(playback.time) - 1; - playback.tick(fps, range.x, range.y, (animation->isLoop || settings.playbackIsLoop) && !manager.isRecording, - deltaSeconds); + auto isLoop = (animation->isLoop || settings.playbackIsLoop) && !manager.isRecording; + playback.tick(fps, range.x, range.y, isLoop ? PLAYBACK_LOOP : PLAYBACK_ONCE, deltaSeconds); // Each frame playback reaches plays its trigger's sound once, however many ticks it lasts or skips. auto frame = (int)std::floor(playback.time); diff --git a/src/ui/animations.cpp b/src/ui/animations.cpp index f1ff143..7af071b 100644 --- a/src/ui/animations.cpp +++ b/src/ui/animations.cpp @@ -227,7 +227,8 @@ static int animations_merge(Document& document, const AnimationMergeOptions& opt int merged = -1; document.edit_apply(EDIT_MERGE_ANIMATIONS, [&](Model& model) { animation_overlay_reset(document, sources); - auto uids = animations_merge(model, target, sources, type, isDeleteAfter, ungroupIds); + auto uids = + animations_merge(model, target, sources, type, isDeleteAfter ? SOURCES_DELETED : SOURCES_KEPT, ungroupIds); auto indices = animation_indices_get(document, uids); if (indices.empty()) return uids; diff --git a/src/ui/draw.h b/src/ui/draw.h index ab28443..db8a8d0 100644 --- a/src/ui/draw.h +++ b/src/ui/draw.h @@ -1,15 +1,10 @@ #pragma once +#include "model/common.h" + #include "vec.h" #include -enum DropZone -{ - DROP_BEFORE, - DROP_INSIDE, - DROP_AFTER -}; - bool is_drop_after(ImVec2, ImVec2); DropZone drop_zone_get(ImVec2, ImVec2); void drop_box_draw(ImDrawList*, ImVec2, ImVec2); diff --git a/src/ui/spritesheets.cpp b/src/ui/spritesheets.cpp index e6b714f..f403767 100644 --- a/src/ui/spritesheets.cpp +++ b/src/ui/spritesheets.cpp @@ -153,15 +153,16 @@ static void spritesheets_merge_popup_update(Panel& panel, SpritesheetsPanel& spr popup_buttons_draw(manager, {.confirm = BASIC_MERGE, .isConfirmEnabled = spritesheets.mergeIds.size() > 1}); if (result == PopupButton::CONFIRM) command_push(panel, - [ids = spritesheets.mergeIds, isAppendRight = settings.mergeSpritesheetsOrigin == APPEND_RIGHT, - isMakeRegions = settings.mergeSpritesheetsIsMakeRegions, - isMakePrimaryRegion = settings.mergeSpritesheetsIsMakePrimaryRegion, - regionOrigin = MERGE_REGION_ORIGINS[settings.mergeSpritesheetsRegionOrigin]](Document& document) { + [ids = spritesheets.mergeIds, + options = SpritesheetMergeOptions{ + .isAppendRight = settings.mergeSpritesheetsOrigin == APPEND_RIGHT, + .isMakeRegions = settings.mergeSpritesheetsIsMakeRegions, + .isMakePrimaryRegion = settings.mergeSpritesheetsIsMakePrimaryRegion, + .origin = MERGE_REGION_ORIGINS[settings.mergeSpritesheetsRegionOrigin]}](Document& document) { spritesheet_textures_edit( document, EDIT_MERGE_SPRITESHEETS, [&]() { - if (!document.spritesheets_merge(ids, isAppendRight, isMakeRegions, - isMakePrimaryRegion, regionOrigin)) + if (!document.spritesheets_merge(ids, options)) return false; document.selected_ids_set(SelectionKind::SPRITESHEETS, {*ids.begin()}); document.focused_id_set(SelectionKind::SPRITESHEETS, *ids.begin()); diff --git a/src/ui/timeline_context.h b/src/ui/timeline_context.h index 8ce4612..4c9dd06 100644 --- a/src/ui/timeline_context.h +++ b/src/ui/timeline_context.h @@ -227,8 +227,7 @@ struct TimelineContext : TimelineState void item_remove(); std::vector item_references_groupable_get(); void item_group(); - void rows_move_to_row(std::vector draggedRows, TimelineRow targetRow, bool isDropAfter, - bool isDropIntoGroup = false); + void rows_move_to_row(std::vector draggedRows, TimelineRow targetRow, DropZone zone); void fit_animation_length(); void marker_set(bool, int); void markers_remove(); diff --git a/src/ui/timeline_edit.cpp b/src/ui/timeline_edit.cpp index 495804c..e693c35 100644 --- a/src/ui/timeline_edit.cpp +++ b/src/ui/timeline_edit.cpp @@ -203,8 +203,7 @@ void TimelineContext::item_group() }); } -void TimelineContext::rows_move_to_row(std::vector draggedRows, TimelineRow targetRow, bool isDropAfter, - bool isDropIntoGroup) +void TimelineContext::rows_move_to_row(std::vector draggedRows, TimelineRow targetRow, DropZone zone) { if (draggedRows.empty()) return; @@ -237,9 +236,7 @@ void TimelineContext::rows_move_to_row(std::vector draggedRows, Tim edit_push( EDIT_MOVE_ITEMS, - [=](Model& model) { - return items_move(model, animationIndex, targetType, ids, groupIds, target, isDropAfter, isDropIntoGroup); - }, + [=](Model& model) { return items_move(model, animationIndex, targetType, ids, groupIds, target, zone); }, [=, this](Document& document, const Uids&) { document.selection.uids[SelectionKind::TRACKS] = moved.uids.at(SelectionKind::TRACKS); document.selection.uids[SelectionKind::GROUPS] = moved.uids.at(SelectionKind::GROUPS); diff --git a/src/ui/timeline_tracks.cpp b/src/ui/timeline_tracks.cpp index fdd8db5..dcc4d1d 100644 --- a/src/ui/timeline_tracks.cpp +++ b/src/ui/timeline_tracks.cpp @@ -582,7 +582,7 @@ void TimelineContext::row_drag_drop(const TimelineRow& row, ImVec2 buttonMin, Im else drop_line_draw(ImGui::GetWindowDrawList(), childMin, childMax, isDropAfter); if (payload->IsDelivery()) - rows_move_to_row(draggedRows, row, isDropAfter, isDropIntoGroup); + rows_move_to_row(draggedRows, row, isDropIntoGroup ? DROP_INSIDE : isDropAfter ? DROP_AFTER : DROP_BEFORE); } ImGui::EndDragDropTarget(); } diff --git a/tests/common.h b/tests/common.h index 9de3313..9a83bc5 100644 --- a/tests/common.h +++ b/tests/common.h @@ -70,7 +70,8 @@ inline std::vector variants_get(const Model& model) { auto merged = model; std::set sources = {0, 1, 2}; - animations_merge(merged, 0, sources, type, type == MERGE_PREPEND || type == MERGE_REPLACE, {}); + animations_merge(merged, 0, sources, type, + type == MERGE_PREPEND || type == MERGE_REPLACE ? SOURCES_DELETED : SOURCES_KEPT, {}); variants.emplace_back(name, model_serialize(merged, SAVE_EDITOR)); } return variants; diff --git a/tests/operations.cpp b/tests/operations.cpp index c5c995e..dba31f7 100644 --- a/tests/operations.cpp +++ b/tests/operations.cpp @@ -35,7 +35,7 @@ TEST_CASE("animations_merge keeps one group per name") { INFO("merge: ", std::string(name)); auto model = fixture_load("03a_groups_nested.anm2"); - animations_merge(model, 0, {0, 1}, type, false, {}); + animations_merge(model, 0, {0, 1}, type, SOURCES_KEPT, {}); CHECK(groups_named_count(*model.animation_get(0), "Main") == 1); } } @@ -190,7 +190,7 @@ TEST_CASE("edit layer operations") [&](Model& anm2) { return items_group(anm2, 0, LAYER, {2}, "New"); }, 1}, {"items_move", "03b_groups_legacy_flat.anm2", [&](Model& anm2) { - items_move(anm2, 0, LAYER, {2}, {}, {true, LAYER, 0, -1}, false, true); + items_move(anm2, 0, LAYER, {2}, {}, {true, LAYER, 0, -1}, DROP_INSIDE); return Uids{}; }, 0}, @@ -244,7 +244,7 @@ TEST_CASE("edit layer operations") }, 0}, {"animations_merge", "03a_groups_nested.anm2", - [&](Model& anm2) { return animations_merge(anm2, 0, {1}, MERGE_APPEND, true, {0}); }, 1}, + [&](Model& anm2) { return animations_merge(anm2, 0, {1}, MERGE_APPEND, SOURCES_DELETED, {0}); }, 1}, }; for (auto& entry : CASES) @@ -299,7 +299,7 @@ TEST_CASE("spritesheet pixel edits") auto image_get = [&](int id) { return id == 0 ? &sheet : &other; }; - auto merged = spritesheets_merge(model, {0, 1}, image_get, true, true, false, Origin::TOP_LEFT); + auto merged = spritesheets_merge(model, {0, 1}, image_get, {true, true, false, Origin::TOP_LEFT}); REQUIRE(merged); CHECK(merged->size == IVec2(80, 64)); CHECK(model.content.spritesheets.size() == 1); @@ -326,9 +326,9 @@ TEST_CASE("markers stay in the editor's copy and bound playback") Playback playback = {}; playback.isPlaying = true; - playback.tick(10, 2, 6, true, 0.1f); + playback.tick(10, 2, 6, PLAYBACK_LOOP, 0.1f); CHECK(playback.time == doctest::Approx(3.0f)); - playback.tick(10, 2, 6, true, 0.5f); + playback.tick(10, 2, 6, PLAYBACK_LOOP, 0.5f); CHECK(playback.time == doctest::Approx(4.0f)); } @@ -435,11 +435,11 @@ TEST_CASE("animations_merge moves triggers past the animation placed before them }; auto appended = model_make(); - animations_merge(appended, 0, {0, 1}, MERGE_APPEND, false, {}); + animations_merge(appended, 0, {0, 1}, MERGE_APPEND, SOURCES_KEPT, {}); CHECK(appended.animation_get(0)->triggers.frames.at(0).atFrame == 15); auto prepended = model_make(); - animations_merge(prepended, 1, {0, 1}, MERGE_PREPEND, false, {}); + animations_merge(prepended, 1, {0, 1}, MERGE_PREPEND, SOURCES_KEPT, {}); CHECK(prepended.animation_get(1)->triggers.frames.at(0).atFrame == 15); } @@ -489,7 +489,7 @@ TEST_CASE("SVG spritesheets pack and merge into SVGs") return images.at(id); }; auto mixed = model; - auto merged = spritesheets_merge(mixed, {0, 1}, image_get, true, false, false, Origin::TOP_LEFT); + auto merged = spritesheets_merge(mixed, {0, 1}, image_get, {true, false, false, Origin::TOP_LEFT}); REQUIRE(merged); CHECK(merged->is_vector()); CHECK(merged->size == IVec2(36, 16)); @@ -498,7 +498,7 @@ TEST_CASE("SVG spritesheets pack and merge into SVGs") std::swap(model.content.spritesheets.front().id, model.content.spritesheets.back().id); images = {{1, &vector}, {0, &raster}}; - REQUIRE(spritesheets_merge(model, {0, 1}, image_get, true, false, false, Origin::TOP_LEFT)); + REQUIRE(spritesheets_merge(model, {0, 1}, image_get, {true, false, false, Origin::TOP_LEFT})); CHECK(item_get(model.content.spritesheets, 0)->path == "white.svg"); } diff --git a/tests/selection.cpp b/tests/selection.cpp index 304e1fe..1e43f21 100644 --- a/tests/selection.cpp +++ b/tests/selection.cpp @@ -81,7 +81,7 @@ TEST_CASE("track and group selection follow regrouping") selection_references_set(selection, anm2, SelectionKind::TRACKS, {{0, LAYER, 0, -1, LAYER, groupId}}); selection_references_set(selection, anm2, SelectionKind::GROUPS, {{0, LAYER, groupId}}); - selection_edit(anm2, selection, [&] { items_move(anm2, 0, LAYER, {0}, {}, {false, LAYER, 2, -1}, false, false); }); + selection_edit(anm2, selection, [&] { items_move(anm2, 0, LAYER, {0}, {}, {false, LAYER, 2, -1}, DROP_BEFORE); }); UidIndex index(anm2); CHECK(selection_references_get(selection, index, SelectionKind::TRACKS) == std::set{{0, LAYER, 0}});