From 20bfbace7a5fa01863c47ce1ebce7c7b3a1d45bb Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 3 Oct 2026 15:04:16 -0400 Subject: [PATCH] Stage 2: stable element uids and selection that follows edits - Element::uid: runtime-only, unique per document (uids_repair on load and after every edit gives copies and new elements fresh uids; never serialized or hashed) - Handle {animation, item, frame} uids and UidIndex (one walk: uid -> Reference) - Snapshots capture the selection's handles when an edit starts and re-resolve the fields the edit left untouched on commit, undo and redo; replaces restored_snapshot_sanitize. Covers the main reference, frame/item/group references, timeline frame selection and the animation list selection. - tests/selection.cpp: references survive inserts, copies, deletes, animation removal/reordering and regrouping Co-Authored-By: Claude Opus 5.5 --- src/anm2/anm2.cpp | 1 + src/anm2/anm2.hpp | 24 +++++++++ src/anm2/handle.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++ src/document.cpp | 82 +++-------------------------- src/snapshots.cpp | 110 ++++++++++++++++++++++++++++++++++++++- src/snapshots.hpp | 14 +++++ tests/selection.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 395 insertions(+), 77 deletions(-) create mode 100644 src/anm2/handle.cpp create mode 100644 tests/selection.cpp diff --git a/src/anm2/anm2.cpp b/src/anm2/anm2.cpp index ebb18af..ddb8937 100644 --- a/src/anm2/anm2.cpp +++ b/src/anm2/anm2.cpp @@ -66,6 +66,7 @@ namespace anm2ed shader_frame_ids_repair(anm2.root); region_frame_ids_repair(anm2.root); anm2.region_frames_sync(true); + anm2.uids_repair(); anm2.isValid = true; return true; } diff --git a/src/anm2/anm2.hpp b/src/anm2/anm2.hpp index 7d1cd3d..a60f957 100644 --- a/src/anm2/anm2.hpp +++ b/src/anm2/anm2.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -325,6 +326,7 @@ namespace anm2ed std::vector soundIds{}; std::vector> extraAttributes{}; std::string text{}; + std::uint64_t uid{}; glm::vec2 pivot{}; glm::vec2 crop{}; glm::vec2 position{}; @@ -347,6 +349,16 @@ namespace anm2ed auto operator<=>(const Reference&) const = default; }; + // A Reference expressed by runtime uids, so it survives inserts, deletes and moves. + struct Handle + { + std::uint64_t animation{}; + std::uint64_t item{}; + std::uint64_t frame{}; + + auto operator<=>(const Handle&) const = default; + }; + struct TrackContainer { ItemType itemType; @@ -573,5 +585,17 @@ namespace anm2ed bool file_merge(const std::filesystem::path&, const std::filesystem::path&, FileMergePreset); bool regions_generate(const std::set&, const std::set&, const std::string&, RegionFrameMapping); void regions_scan(); + void uids_repair(); + Handle handle_get(Reference, bool = false) const; + }; + + // uid -> current Reference, built in one walk; group uids map to {animation, groupType, groupId}. + class UidIndex + { + std::unordered_map locations{}; + + public: + explicit UidIndex(const Anm2&); + std::optional reference_get(Handle, Reference) const; }; } diff --git a/src/anm2/handle.cpp b/src/anm2/handle.cpp new file mode 100644 index 0000000..dff5387 --- /dev/null +++ b/src/anm2/handle.cpp @@ -0,0 +1,122 @@ +#include "internal.hpp" + +#include + +#include "uid.hpp" + +namespace anm2ed +{ + void Anm2::uids_repair() + { + std::unordered_set seen{}; + element_each(root, + [&](Element& element) + { + if (element.uid == 0 || !seen.insert(element.uid).second) element.uid = util::uid_next(); + }); + } + + Handle Anm2::handle_get(Reference reference, bool isGroup) const + { + Handle handle{}; + auto animation = element_get(ElementType::ANIMATION, reference.animationIndex); + if (!animation) return handle; + handle.animation = animation->uid; + + auto item = isGroup ? animation_group_get(*animation, reference.itemType, reference.itemID) + : reference.itemType == NONE + ? nullptr + : animation_item_get(*animation, (ItemType)reference.itemType, reference.itemID, + reference.groupType, reference.groupId); + if (!item) return handle; + handle.item = item->uid; + + if (auto frame = isGroup ? nullptr : track_frame_get(*item, reference.frameIndex)) handle.frame = frame->uid; + return handle; + } + + void track_locations_add(std::unordered_map& locations, const Element& track, + Reference reference) + { + locations[track.uid] = reference; + auto frameType = track_frame_type_get(track); + for (const auto& frame : track.children) + if (frame.type == frameType) + { + ++reference.frameIndex; + locations[frame.uid] = reference; + } + } + + UidIndex::UidIndex(const Anm2& anm2) + { + auto animations = anm2.element_get(ElementType::ANIMATIONS); + if (!animations) return; + + int animationIndex{}; + for (const auto& animation : animations->children) + { + if (animation.type != ElementType::ANIMATION) continue; + locations[animation.uid] = {animationIndex}; + + for (const auto& child : animation.children) + { + if (child.type == ElementType::ROOT_ANIMATION) track_locations_add(locations, child, {animationIndex, ROOT}); + if (child.type == ElementType::TRIGGERS) track_locations_add(locations, child, {animationIndex, TRIGGER}); + + auto row = track_container_get(child.type); + if (!row || row->container != child.type) continue; + auto type = (int)row->itemType; + + auto tracks_add = [&](this auto&& self, const Element& parent, int groupId) -> void + { + for (const auto& track : parent.children) + if (track.type == ElementType::GROUP) + { + locations[track.uid] = {animationIndex, type, track.id}; + self(track, track.id); + } + else if (track.type == ElementType::ROOT_ANIMATION && groupId != -1) + track_locations_add(locations, track, {animationIndex, ROOT, -1, -1, type, groupId}); + else if (track.type == row->track) + { + auto trackGroupId = groupId != -1 ? groupId : track.groupId; + track_locations_add( + locations, track, + {animationIndex, type, track_id_get(track), -1, trackGroupId == -1 ? NONE : type, trackGroupId}); + } + }; + tracks_add(child, -1); + } + ++animationIndex; + } + } + + std::optional UidIndex::reference_get(Handle handle, Reference reference) const + { + auto location_get = [&](std::uint64_t uid) -> const Reference* + { + auto it = locations.find(uid); + return it == locations.end() ? nullptr : &it->second; + }; + + // Resolve the most specific element captured; if it no longer exists, neither does the reference. + auto uid = handle.frame ? handle.frame : handle.item ? handle.item : handle.animation; + if (!uid) return reference; + auto location = location_get(uid); + if (!location) return std::nullopt; + + auto isGroupAgnostic = reference.groupId == -1 && reference.itemType != ROOT; + if (uid == handle.animation) + reference.animationIndex = location->animationIndex; + else + reference = *location; + // References made without group context stay that way, wherever the track now lives. + if (isGroupAgnostic && reference.itemType != ROOT) + { + reference.groupType = NONE; + reference.groupId = -1; + } + return reference; + } +} diff --git a/src/document.cpp b/src/document.cpp index f7a6524..a5f3699 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -200,81 +200,11 @@ namespace anm2ed::document }; } - void restored_snapshot_sanitize(Document& document) + void frame_time_sync(Document& document) { - auto& reference = document.reference; - auto& selection = document.frames.selection; - auto& frameReferences = document.frames.references; - auto& groupReferences = document.groupReferences; - auto& anm2 = document.anm2; - - auto selection_clear = [&]() - { - reference.frameIndex = -1; - selection.clear(); - frameReferences.clear(); - groupReferences.clear(); - document.frameTime = 0.0f; - }; - - auto animations = anm2.element_get(ElementType::ANIMATIONS); - auto animationCount = animations ? animations_count_get(*animations) : 0; - if (animationCount <= 0) - { - reference = {}; - selection_clear(); - document.items.references.clear(); - return; - } - - reference.animationIndex = std::clamp(reference.animationIndex, 0, animationCount - 1); - auto item = anm2.element_get(item_reference_get(reference)); - if (!item) - { - reference = {reference.animationIndex, ROOT, -1, reference.frameIndex}; - item = anm2.element_get(item_reference_get(reference)); - } - if (!item) return selection_clear(); - - std::erase_if(document.items.references, - [&](const Reference& itemReference) { return !anm2.element_get(item_reference_get(itemReference)); }); - std::erase_if(groupReferences, - [&](const Reference& groupReference) - { - auto animation = anm2.element_get(ElementType::ANIMATION, groupReference.animationIndex); - return !animation || - !animation_group_get(*animation, groupReference.itemType, groupReference.itemID); - }); - std::erase_if(frameReferences, [isValid = frame_reference_validator_make(anm2)]( - const Reference& frameReference) mutable { return !isValid(frameReference); }); - - auto frameCount = track_frames_count_get(*item); - std::erase_if(selection, [&](int frameIndex) { return frameIndex < 0 || frameIndex >= frameCount; }); - if (frameCount <= 0) - { - reference.frameIndex = -1; - frameReferences.clear(); - document.frameTime = 0.0f; - return; - } - - if (reference.frameIndex < 0 || reference.frameIndex >= frameCount) - reference.frameIndex = - selection.empty() ? std::clamp(reference.frameIndex, 0, frameCount - 1) : *selection.begin(); - - if (frameReferences.empty()) - for (auto frameIndex : selection) - { - auto frameReference = reference; - frameReference.frameIndex = frameIndex; - frameReferences.insert(frameReference); - } - - selection.clear(); - for (const auto& frameReference : frameReferences) - if (is_reference_item_matched(frameReference, reference)) selection.insert(frameReference.frameIndex); - - document.frameTime = frame_time_from_index_get(*item, reference.frameIndex); + auto item = document.anm2.element_get(item_reference_get(document.reference)); + auto frameIndex = document.reference.frameIndex; + document.frameTime = item && frameIndex >= 0 ? frame_time_from_index_get(*item, frameIndex) : 0.0f; } } @@ -1032,7 +962,7 @@ namespace anm2ed void Document::undo() { if (!snapshots.undo()) return; - document::restored_snapshot_sanitize(*this); + document::frame_time_sync(*this); toast_log(Level::INFO, TOAST_UNDO, message); change(Document::ALL); } @@ -1040,7 +970,7 @@ namespace anm2ed void Document::redo() { if (!snapshots.redo()) return; - document::restored_snapshot_sanitize(*this); + document::frame_time_sync(*this); toast_log(Level::INFO, TOAST_REDO, message); change(Document::ALL); } diff --git a/src/snapshots.cpp b/src/snapshots.cpp index f298ef5..cbc819d 100644 --- a/src/snapshots.cpp +++ b/src/snapshots.cpp @@ -200,6 +200,105 @@ namespace anm2ed::snapshots step.anm2.elements.front().redo.children.empty(); } + bool is_same_item(Reference left, Reference right) + { + left.frameIndex = right.frameIndex = -1; + return left == right; + } + + SnapshotSelection selection_capture(const Snapshot& snapshot) + { + SnapshotSelection selection{snapshot.reference, + snapshot.frames.references, + snapshot.items.references, + snapshot.groupReferences, + {snapshot.frames.selection.begin(), snapshot.frames.selection.end()}, + {snapshot.animation.selection.begin(), snapshot.animation.selection.end()}}; + + auto handle_add = [&](Reference reference) + { selection.handles.try_emplace(reference, snapshot.anm2.handle_get(reference)); }; + handle_add(selection.reference); + for (auto frameIndex : selection.frameIndices) + handle_add({selection.reference.animationIndex, selection.reference.itemType, selection.reference.itemID, + frameIndex, selection.reference.groupType, selection.reference.groupId}); + for (auto animationIndex : selection.animationIndices) + handle_add({animationIndex}); + for (const auto& references : {selection.frameReferences, selection.itemReferences}) + for (auto reference : references) + handle_add(reference); + for (auto reference : selection.groupReferences) + selection.groupHandles.try_emplace(reference, snapshot.anm2.handle_get(reference, true)); + return selection; + } + + // The main reference never disappears: a removed frame falls back to its track, a removed track to the root. + Reference reference_follow(const Anm2& anm2, const UidIndex& index, Handle handle, Reference reference) + { + if (auto followed = index.reference_get(handle, reference)) return *followed; + + if (auto track = index.reference_get({handle.animation, handle.item}, reference)) + { + auto element = anm2.element_get(*track); + auto count = element ? track_frames_count_get(*element) : 0; + track->frameIndex = count > 0 ? std::clamp(reference.frameIndex, 0, count - 1) : -1; + return *track; + } + + if (auto animation = index.reference_get({handle.animation}, {reference.animationIndex, ROOT})) return *animation; + auto animations = anm2.element_get(ElementType::ANIMATIONS); + auto count = animations ? animations_count_get(*animations) : 0; + return count > 0 ? Reference{std::clamp(reference.animationIndex, 0, count - 1), ROOT} : Reference{}; + } + + std::set references_follow(const UidIndex& index, const std::set& references, + const std::map& handles) + { + std::set followed{}; + for (auto reference : references) + if (auto result = index.reference_get(handles.at(reference), reference)) followed.insert(*result); + return followed; + } + + // Selection fields the change left untouched are re-resolved by uid; fields it set itself are kept. + void selection_follow(Snapshot& snapshot, const SnapshotSelection& before) + { + snapshot.anm2.uids_repair(); + UidIndex index(snapshot.anm2); + + auto isReferenceKept = snapshot.reference == before.reference; + if (isReferenceKept) + snapshot.reference = + reference_follow(snapshot.anm2, index, before.handles.at(before.reference), before.reference); + if (snapshot.frames.references == before.frameReferences) + snapshot.frames.references = references_follow(index, before.frameReferences, before.handles); + if (snapshot.items.references == before.itemReferences) + snapshot.items.references = references_follow(index, before.itemReferences, before.handles); + if (snapshot.groupReferences == before.groupReferences) + snapshot.groupReferences = references_follow(index, before.groupReferences, before.groupHandles); + + if (std::set(snapshot.animation.selection.begin(), snapshot.animation.selection.end()) == + before.animationIndices) + { + snapshot.animation.selection.clear(); + for (auto animationIndex : before.animationIndices) + if (auto followed = index.reference_get(before.handles.at({animationIndex}), {animationIndex})) + snapshot.animation.selection.insert(followed->animationIndex); + } + + if (!isReferenceKept || + std::set(snapshot.frames.selection.begin(), snapshot.frames.selection.end()) != before.frameIndices) + return; + + snapshot.frames.selection.clear(); + for (auto frameIndex : before.frameIndices) + { + auto frame = before.reference; + frame.frameIndex = frameIndex; + auto followed = index.reference_get(before.handles.at(frame), frame); + if (followed && is_same_item(*followed, snapshot.reference)) + snapshot.frames.selection.insert(followed->frameIndex); + } + } } namespace anm2ed @@ -283,7 +382,8 @@ namespace anm2ed void Snapshots::step_push(const std::string& message, SnapshotStep step) { - if (pendingStep) commit(); + if (pendingStep || pendingSelection) commit(); + pendingSelection = snapshots::selection_capture(current); current.message = message; step.message = message; pendingStep = step.is_empty() ? std::nullopt : std::optional(std::move(step)); @@ -338,6 +438,7 @@ namespace anm2ed void Snapshots::commit() { const auto& snapshot = current; + if (pendingSelection) snapshots::selection_follow(current, *std::exchange(pendingSelection, std::nullopt)); if (!pendingStep) return; auto step = std::move(*pendingStep); @@ -395,9 +496,12 @@ namespace anm2ed bool Snapshots::undo() { pendingStep.reset(); + pendingSelection.reset(); if (auto step = undoStack.pop()) { + auto selection = snapshots::selection_capture(current); step->apply(current, SnapshotStepDirection::UNDO); + snapshots::selection_follow(current, selection); redoStack.push(std::move(*step)); return true; } @@ -407,9 +511,12 @@ namespace anm2ed bool Snapshots::redo() { pendingStep.reset(); + pendingSelection.reset(); if (auto step = redoStack.pop()) { + auto selection = snapshots::selection_capture(current); step->apply(current, SnapshotStepDirection::REDO); + snapshots::selection_follow(current, selection); undoStack.push(std::move(*step)); return true; } @@ -421,6 +528,7 @@ namespace anm2ed undoStack.clear(); redoStack.clear(); pendingStep.reset(); + pendingSelection.reset(); } void Snapshots::apply_limit() diff --git a/src/snapshots.hpp b/src/snapshots.hpp index 1d0b002..44d6653 100644 --- a/src/snapshots.hpp +++ b/src/snapshots.hpp @@ -110,6 +110,19 @@ namespace anm2ed void apply(Snapshot&, SnapshotStepDirection) const; }; + // The selection as it was before a change, with the uids that let it follow elements through the change. + struct SnapshotSelection + { + Reference reference{}; + std::set frameReferences{}; + std::set itemReferences{}; + std::set groupReferences{}; + std::set frameIndices{}; + std::set animationIndices{}; + std::map handles{}; + std::map groupHandles{}; + }; + class SnapshotStack { public: @@ -136,6 +149,7 @@ namespace anm2ed SnapshotStack redoStack{}; Snapshot current{}; std::optional pendingStep{}; + std::optional pendingSelection{}; void anm2_push(const std::string&); void tracks_push(const std::string&, const std::set&); diff --git a/tests/selection.cpp b/tests/selection.cpp new file mode 100644 index 0000000..70e896a --- /dev/null +++ b/tests/selection.cpp @@ -0,0 +1,119 @@ +#include "common.hpp" + +using namespace anm2ed; +using namespace anm2ed::test; + +Anm2 fixture_load_with_uids(const char* name) +{ + auto anm2 = anm2_load(file_load(CORPUS_DIR / name)); + anm2.uids_repair(); + return anm2; +} + +Element& track_get(Anm2& anm2, Reference reference) +{ + reference.frameIndex = -1; + auto track = anm2.element_get(reference); + REQUIRE(track); + return *track; +} + +std::optional reference_follow(Anm2& anm2, Reference reference, auto&& edit) +{ + auto handle = anm2.handle_get(reference); + edit(); + anm2.uids_repair(); + return UidIndex(anm2).reference_get(handle, reference); +} + +TEST_CASE("uids are unique and non-zero after load") +{ + for (const auto& fixture : fixtures_get()) + { + auto anm2 = anm2_load(file_load(fixture)); + std::set uids{}; + bool isUnique{true}; + element_each(anm2.root, + [&](const Element& element) { isUnique &= element.uid != 0 && uids.insert(element.uid).second; }); + INFO("fixture: ", fixture.filename().string()); + CHECK(isUnique); + } +} + +TEST_CASE("frame references follow inserts, deletes and copies") +{ + auto anm2 = fixture_load_with_uids("02_items.anm2"); + Reference frame{0, LAYER, 0, 1}; + + auto inserted = reference_follow(anm2, frame, + [&] + { + auto& track = track_get(anm2, frame); + track.children.insert(track.children.begin(), element_make(ElementType::FRAME)); + }); + REQUIRE(inserted); + CHECK(inserted->frameIndex == 2); + frame = *inserted; + + auto copied = reference_follow(anm2, frame, + [&] + { + auto& track = track_get(anm2, frame); + track.children.push_back(track.children[frame.frameIndex]); + }); + REQUIRE(copied); + CHECK(copied->frameIndex == 2); + + CHECK_FALSE(reference_follow( + anm2, frame, [&] { track_get(anm2, frame).children.erase(track_get(anm2, frame).children.begin() + 2); })); +} + +TEST_CASE("references follow animation removal and reordering") +{ + auto anm2 = fixture_load_with_uids("02_items.anm2"); + Reference root{1, ROOT, -1, 0}; + auto animations = anm2.element_get(ElementType::ANIMATIONS); + auto followed = reference_follow(anm2, root, [&] { animations->children.erase(animations->children.begin()); }); + REQUIRE(followed); + CHECK(followed->animationIndex == 0); + CHECK(followed->frameIndex == 0); + + Reference animationOnly{0}; + auto reordered = reference_follow( + anm2, animationOnly, + [&] { animations->children.insert(animations->children.begin(), element_make(ElementType::ANIMATION)); }); + REQUIRE(reordered); + CHECK(reordered->animationIndex == 1); +} + +TEST_CASE("track references follow regrouping") +{ + auto anm2 = fixture_load_with_uids("03a_groups_nested.anm2"); + auto animation = anm2.element_get(ElementType::ANIMATION, 0); + auto layers = child_first_get(*animation, ElementType::LAYER_ANIMATIONS); + auto group = child_first_get(*layers, ElementType::GROUP); + REQUIRE(group); + auto grouped = track_find(*layers, ElementType::LAYER_ANIMATION, 0); + REQUIRE(grouped); + REQUIRE(grouped->groupId == group->id); + + Reference withGroup{0, LAYER, 0, 0, LAYER, group->id}; + Reference withoutGroup{0, LAYER, 0, 0}; + auto handleWithGroup = anm2.handle_get(withGroup); + auto handleWithoutGroup = anm2.handle_get(withoutGroup); + grouped->groupId = -1; + + UidIndex index(anm2); + auto followedWithGroup = index.reference_get(handleWithGroup, withGroup); + auto followedWithoutGroup = index.reference_get(handleWithoutGroup, withoutGroup); + REQUIRE(followedWithGroup); + REQUIRE(followedWithoutGroup); + CHECK(*followedWithGroup == Reference{0, LAYER, 0, 0}); + CHECK(*followedWithoutGroup == withoutGroup); + CHECK(anm2.element_get(*followedWithGroup)); + + Reference groupReference{0, LAYER, group->id}; + auto groupHandle = anm2.handle_get(groupReference, true); + CHECK(groupHandle.item == group->uid); + CHECK(index.reference_get(groupHandle, groupReference) == groupReference); +}