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 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 15:04:16 -04:00
1 parent 26ab394fe4
commit 20bfbace7a
7 files changed
+395 -77

No files matched your search

+1
View File
@@ -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;
}
+24
View File
@@ -8,6 +8,7 @@
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <glm/glm.hpp>
@@ -325,6 +326,7 @@ namespace anm2ed
std::vector<int> soundIds{};
std::vector<std::pair<std::string, std::string>> 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<int>&, const std::set<Reference>&, 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<std::uint64_t, Reference> locations{};
public:
explicit UidIndex(const Anm2&);
std::optional<Reference> reference_get(Handle, Reference) const;
};
}
+122
View File
@@ -0,0 +1,122 @@
#include "internal.hpp"
#include <unordered_set>
#include "uid.hpp"
namespace anm2ed
{
void Anm2::uids_repair()
{
std::unordered_set<std::uint64_t> 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<std::uint64_t, Reference>& 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<Reference> 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;
}
}
+6 -76
View File
@@ -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);
}
+109 -1
View File
@@ -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<Reference> references_follow(const UidIndex& index, const std::set<Reference>& references,
const std::map<Reference, Handle>& handles)
{
std::set<Reference> 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<int>(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<int>(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<SnapshotStep>(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()
+14
View File
@@ -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<Reference> frameReferences{};
std::set<Reference> itemReferences{};
std::set<Reference> groupReferences{};
std::set<int> frameIndices{};
std::set<int> animationIndices{};
std::map<Reference, Handle> handles{};
std::map<Reference, Handle> groupHandles{};
};
class SnapshotStack
{
public:
@@ -136,6 +149,7 @@ namespace anm2ed
SnapshotStack redoStack{};
Snapshot current{};
std::optional<SnapshotStep> pendingStep{};
std::optional<SnapshotSelection> pendingSelection{};
void anm2_push(const std::string&);
void tracks_push(const std::string&, const std::set<Reference>&);