From 1fc6b064ae62d84a3adf136eb9ae8dbbacc51164 Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 22 Nov 2026 18:55:16 -0500 Subject: [PATCH] Pass BakeOptions to every bake instead of rounding bools RootBakeOptions moves into the model as BakeOptions, used by frame and track bakes and root transforms. --- src/edit/edit.h | 16 ++++------------ src/edit/frames.cpp | 11 +++++------ src/model/frames.cpp | 22 ++++++++++------------ src/model/frames.h | 15 ++++++++++++--- src/model/xml.cpp | 2 +- src/ui/timeline_edit.cpp | 7 ++++--- tests/invariants.cpp | 3 +-- tests/operations.cpp | 9 ++++++--- tests/undo.cpp | 2 +- 9 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/edit/edit.h b/src/edit/edit.h index f1ba92f..ebaef25 100644 --- a/src/edit/edit.h +++ b/src/edit/edit.h @@ -10,6 +10,7 @@ #include "image.h" #include "model/draw.h" +#include "model/frames.h" #include "model/model.h" // Document edits as plain functions of the model. Each returns the uids of the elements it created or changed that @@ -25,14 +26,6 @@ struct RowTarget int groupId = -1; }; -struct RootBakeOptions -{ - bool isRoundScale = false; - bool isRoundRotation = false; - bool isMatchRootInterpolation = false; - bool isUseRootPivot = false; -}; - struct GridOptions { IVec2 start{}; @@ -94,10 +87,9 @@ enum class FrameProperty #define X(property, tracks, ...) property, FRAME_PROPERTIES(X) #undef X - COUNT + COUNT }; - bool is_frame_property_valid(FrameProperty, ItemType); void frame_property_copy(Frame&, const Frame&, FrameProperty); Uids frames_property_set(Model&, const std::set&, FrameProperty, const Frame&); @@ -108,14 +100,14 @@ Uids frame_insert(Model&, Reference, int); void frames_delete(Model&, const std::set&); Uids frames_duplicate(Model&, const std::set&, Reference); void frames_reverse(Model&, const std::set&); -Uids frames_bake(Model&, const std::set&, int, bool, bool); +Uids frames_bake(Model&, const std::set&, int, BakeOptions); Uids frame_split(Model&, Reference, int); Uids frames_split(Model&, const std::set&, int); Uids frames_move(Model&, const std::set&, Reference, int); void frame_durations_set(Model&, const std::map&); void trigger_at_frame_set(Model&, Reference, int); Uids frames_paste(Model&, Reference, const std::set&, const std::string&, int, std::string*); -Uids root_bake_into(Model&, const std::set&, const std::set&, RootBakeOptions); +Uids root_bake_into(Model&, const std::set&, const std::set&, BakeOptions); void items_remove(Model&, int, const std::map>&, const std::map>&); Uids items_group(Model&, int, int, const std::set&, const std::string&); diff --git a/src/edit/frames.cpp b/src/edit/frames.cpp index 82ce7a0..5de4f54 100644 --- a/src/edit/frames.cpp +++ b/src/edit/frames.cpp @@ -119,7 +119,7 @@ void frames_reverse(Model& model, const std::set& frames) } } -Uids frames_bake(Model& model, const std::set& frames, int interval, bool isRoundScale, bool isRoundRotation) +Uids frames_bake(Model& model, const std::set& frames, int interval, BakeOptions options) { Uids uids = {}; for (auto& [trackReference, indices] : frames_by_track_get(frames)) @@ -136,7 +136,7 @@ Uids frames_bake(Model& model, const std::set& frames, int interval, continue; auto duration = track->frames[index].duration; auto count = duration <= FRAME_DURATION_MIN ? 1 : (int)std::ceil((float)duration / interval); - frame_bake(*track, index, interval, isRoundScale, isRoundRotation); + frame_bake(*track, index, interval, options); uids.append_range(frames_uids_get(*track, index, count)); inserted += count - 1; } @@ -340,7 +340,7 @@ Uids frames_paste(Model& model, Reference reference, const std::set& // Applies the selected root frames' transforms to the target tracks' frames under them, then resets those root // frames to a default frame spanning the same time. Uids root_bake_into(Model& model, const std::set& rootFrames, const std::set& targets, - RootBakeOptions options) + BakeOptions options) { auto byTrack = frames_by_track_get(rootFrames); if (byTrack.size() != 1 || targets.empty()) @@ -373,14 +373,13 @@ Uids root_bake_into(Model& model, const std::set& rootFrames, const s if (options.isMatchRootInterpolation) for (int index = (int)track->frames.size() - 1; index >= 0; --index) if (is_spanned((int)frame_time_from_index_get(*track, index))) - frame_bake(*track, index, FRAME_DURATION_MIN, options.isRoundScale, options.isRoundRotation); + frame_bake(*track, index, FRAME_DURATION_MIN, options); for (int index = 0; index < (int)track->frames.size(); ++index) { auto start = (int)frame_time_from_index_get(*track, index); if (is_spanned(start)) - frame_root_transform_apply(track->frames[index], frame_generate(root, (float)start), - options.isRoundScale, options.isRoundRotation, options.isUseRootPivot); + frame_root_transform_apply(track->frames[index], frame_generate(root, (float)start), options); } } diff --git a/src/model/frames.cpp b/src/model/frames.cpp index 0515049..ad3ee77 100644 --- a/src/model/frames.cpp +++ b/src/model/frames.cpp @@ -18,8 +18,7 @@ void frame_mix(Frame& frame, const Frame& next, float amount) } // Splits an interpolated frame into uninterpolated frames of at most `interval`. The first keeps the frame's uid. -std::vector frame_bake_split(const Frame& original, const Frame& next, int interval, bool isRoundScale, - bool isRoundRotation) +std::vector frame_bake_split(const Frame& original, const Frame& next, int interval, BakeOptions options) { auto total = std::max(original.duration, FRAME_DURATION_MIN); interval = std::max(interval, FRAME_DURATION_MIN); @@ -30,9 +29,9 @@ std::vector frame_bake_split(const Frame& original, const Frame& next, in frame_mix(baked, next, interpolation_factor(original.interpolation, (float)duration / total)); baked.duration = std::min(interval, total - duration); baked.interpolation = Interpolation::NONE; - if (isRoundScale) + if (options.isRoundScale) baked.scale = vec_round(baked.scale); - if (isRoundRotation) + if (options.isRoundRotation) baked.rotation = std::round(baked.rotation); } return frames; @@ -44,11 +43,10 @@ Mat4 frame_parent_model_get(const Frame& frame) percent_to_unit(frame.skew)); } -void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRoundScale, bool isRoundRotation, - bool isUseRootPivot) +void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, BakeOptions options) { auto rootScale = percent_to_unit(rootFrame.scale); - auto pivot = isUseRootPivot ? rootFrame.position : Vec2(); + auto pivot = options.isUseRootPivot ? rootFrame.position : Vec2(); auto offset = (frame.position - pivot) * rootScale; auto angle = radians(rootFrame.rotation); auto cos = std::cos(angle); @@ -60,9 +58,9 @@ void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRou frame.tint *= rootFrame.tint; frame.colorOffset += rootFrame.colorOffset; - if (isRoundScale) + if (options.isRoundScale) frame.scale = vec_round(frame.scale); - if (isRoundRotation) + if (options.isRoundRotation) frame.rotation = std::round(frame.rotation); } @@ -77,7 +75,7 @@ void track_group_root_bake(Track& track, const Track& root, int frameNum) for (int time = 0; time < length; ++time) { auto& frame = frames.emplace_back(frame_generate(track, (float)time)); - frame_root_transform_apply(frame, frame_generate(root, (float)time), false, false, false); + frame_root_transform_apply(frame, frame_generate(root, (float)time)); frame.duration = FRAME_DURATION_MIN; frame.interpolation = Interpolation::NONE; } @@ -164,7 +162,7 @@ float frame_time_from_index_get(const Track& track, int index) return time; } -void frame_bake(Track& track, int index, int interval, bool isRoundScale, bool isRoundRotation) +void frame_bake(Track& track, int index, int interval, BakeOptions options) { if (index < 0 || index >= (int)track.frames.size()) return; @@ -176,7 +174,7 @@ void frame_bake(Track& track, int index, int interval, bool isRoundScale, bool i } auto next = index + 1 < (int)track.frames.size() ? track.frames[index + 1] : original; - auto baked = frame_bake_split(original, next, interval, isRoundScale, isRoundRotation); + auto baked = frame_bake_split(original, next, interval, options); track.frames.erase(track.frames.begin() + index); track.frames.insert(track.frames.begin() + index, baked.begin(), baked.end()); } diff --git a/src/model/frames.h b/src/model/frames.h index 9470051..0e9e5f0 100644 --- a/src/model/frames.h +++ b/src/model/frames.h @@ -5,16 +5,25 @@ #include "model/draw.h" +// How baking makes frames: whole scales and rotations, a root's easing kept, and turning around the root's position. +struct BakeOptions +{ + bool isRoundScale = false; + bool isRoundRotation = false; + bool isMatchRootInterpolation = false; + bool isUseRootPivot = false; +}; + void frame_mix(Frame&, const Frame&, float); -std::vector frame_bake_split(const Frame&, const Frame&, int, bool, bool); +std::vector frame_bake_split(const Frame&, const Frame&, int, BakeOptions = {}); Mat4 frame_parent_model_get(const Frame&); -void frame_root_transform_apply(Frame&, const Frame&, bool, bool, bool); +void frame_root_transform_apply(Frame&, const Frame&, BakeOptions = {}); Frame frame_generate(const Track&, float); void track_group_root_bake(Track&, const Track&, int); int frame_index_from_at_frame_get(const Track&, int); int frame_index_from_time_get(const Track&, float); float frame_time_from_index_get(const Track&, int); -void frame_bake(Track&, int, int, bool, bool); +void frame_bake(Track&, int, int, BakeOptions = {}); void frames_generate_from_grid(Track&, IVec2, IVec2, Vec2, int, int, int); void frames_sort_by_at_frame(Track&); void frames_change(Track&, FrameChange, ItemType, ChangeType, const std::set&, bool isAnimationLayer = false); diff --git a/src/model/xml.cpp b/src/model/xml.cpp index 1db1625..9eb48fd 100644 --- a/src/model/xml.cpp +++ b/src/model/xml.cpp @@ -1495,7 +1495,7 @@ static Animation animation_bake(const Writer& writer, Animation animation) return; for (int index = (int)track.frames.size() - 1; index >= 0; --index) if (track.frames[index].interpolation != Interpolation::NONE) - frame_bake(track, index, FRAME_DURATION_MIN, false, false); + frame_bake(track, index, FRAME_DURATION_MIN); }); return animation; } diff --git a/src/ui/timeline_edit.cpp b/src/ui/timeline_edit.cpp index 8ef6d0b..495804c 100644 --- a/src/ui/timeline_edit.cpp +++ b/src/ui/timeline_edit.cpp @@ -58,7 +58,8 @@ void TimelineContext::frames_bake() return; edit_push(EDIT_BAKE_FRAMES, [=, interval = settings.bakeInterval, isRoundScale = settings.bakeIsRoundScale, isRoundRotation = settings.bakeIsRoundRotation](Model& model) { - return ::frames_bake(model, selectedFrames, interval, isRoundScale, isRoundRotation); + return ::frames_bake(model, selectedFrames, interval, + {.isRoundScale = isRoundScale, .isRoundRotation = isRoundRotation}); }); } @@ -113,8 +114,8 @@ void TimelineContext::bake_into_other_frames() return; auto targetItems = item_references_for_bake_into_other_frames_get( document, *animation, bakeIntoOtherFramesTarget, isBakeIntoOtherFramesLayers, isBakeIntoOtherFramesNulls); - RootBakeOptions options{settings.bakeIsRoundScale, settings.bakeIsRoundRotation, - settings.bakeIsMatchRootInterpolation, settings.bakeIsUseRootPivot}; + BakeOptions options{settings.bakeIsRoundScale, settings.bakeIsRoundRotation, settings.bakeIsMatchRootInterpolation, + settings.bakeIsUseRootPivot}; edit_push(EDIT_BAKE_INTO_OTHER_FRAMES, [=](Model& model) { return root_bake_into(model, selectedRootFrames, targetItems, options); }); } diff --git a/tests/invariants.cpp b/tests/invariants.cpp index 6d57e43..88cc595 100644 --- a/tests/invariants.cpp +++ b/tests/invariants.cpp @@ -51,8 +51,7 @@ TEST_CASE("JSON documents read back as the editor's copy") // Unknown XML, element order, dangling references and repeated animation names are not kept. auto isQuirky = fixture.filename() == "09_legacy_quirks.anm2" || fixture.filename() == "11_unknown.anm2"; if (!isQuirky) - CHECK(model_serialize(reloaded, SAVE_EDITOR) == - model_serialize(model, SAVE_EDITOR)); + CHECK(model_serialize(reloaded, SAVE_EDITOR) == model_serialize(model, SAVE_EDITOR)); } } diff --git a/tests/operations.cpp b/tests/operations.cpp index 4b40cc8..c5c995e 100644 --- a/tests/operations.cpp +++ b/tests/operations.cpp @@ -98,8 +98,8 @@ TEST_CASE("frames_change") TEST_CASE("frame_bake and frame_mix") { auto model = fixture_load("07_special_interpolation.anm2"); - frame_bake(layer_track_get(model, 0, 0), 0, 2, true, false); - frame_bake(layer_track_get(model, 0, 0), 3, 1, false, true); + frame_bake(layer_track_get(model, 0, 0), 0, 2, {.isRoundScale = true}); + frame_bake(layer_track_get(model, 0, 0), 3, 1, {.isRoundRotation = true}); operation_check("frame_bake", model); Frame from = {}; @@ -166,7 +166,10 @@ TEST_CASE("edit layer operations") }, 0}, {"frames_bake", "07_special_interpolation.anm2", - [&](Model& anm2) { return frames_bake(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, 2, true, false); }, 5}, + [&](Model& anm2) { + return frames_bake(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, 2, {.isRoundScale = true}); + }, + 5}, {"frame_split", "02_items.anm2", [&](Model& anm2) { return frame_split(anm2, {0, LAYER, 1, 0}, 2); }, 1}, {"frames_move", "02_items.anm2", [&](Model& anm2) { return frames_move(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 1, 0}}, head, 2); }, 2}, diff --git a/tests/undo.cpp b/tests/undo.cpp index e98298e..aa7479b 100644 --- a/tests/undo.cpp +++ b/tests/undo.cpp @@ -58,7 +58,7 @@ static bool edit_random_apply(Model& anm2, std::mt19937& random) frames_reverse(anm2, {frame, other}); break; case 4: - frames_bake(anm2, {frame}, 1, false, false); + frames_bake(anm2, {frame}, 1, {}); break; case 5: frame_split(anm2, frame, 1);