Stage 4: one uid-based Selection for the timeline and animations list

- Selection { map<kind, set<uid>> uids; Handle focus; } (src/selection.*, core)
  replaces Document::reference, the frames/items/animation Storages,
  groupReferences, and the timeline's selection snapshot/lock/focus state.
- Because selection is by uid it follows elements through edits by itself;
  after an edit or undo, selection_follow only drops removed uids and moves a
  removed focus to its nearest survivor. The Stage 2 handle capture/re-resolve,
  frames_reference_normalize_for, frames_selection_sync_for and the index-based
  frame validity repair are gone.
- Document keeps a UidIndex (rebuilt on every change) and exposes
  reference_get/reference_set, selected_get/selected_set(kind),
  frame_references_get/set and animations_selected_get/set.
- UidIndex records each uid's element type.
- The animations list keeps its ImGui multi-select Storage as a per-frame mirror
  of the Selection; content windows are unchanged until Stage 8.
- tests/selection.cpp and tests/undo.cpp cover selection through inserts,
  reverses, deletes, item and animation removal, regrouping and undo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 15:46:47 -04:00
1 parent 700b613c62
commit 65a3546c3b
29 files changed
+484 -608

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
#include "selection.hpp"
namespace anm2ed
{
std::set<Reference> selection_references_get(const Selection& selection, const UidIndex& index, SelectionKind kind)
{
std::set<Reference> references{};
if (auto uids = selection.uids.find(kind); uids != selection.uids.end())
for (auto uid : uids->second)
if (auto reference = index.reference_get(uid)) references.insert(*reference);
return references;
}
std::uint64_t handle_uid_get(Handle handle)
{
return handle.frame ? handle.frame : handle.item ? handle.item : handle.animation;
}
void selection_references_set(Selection& selection, const Anm2& anm2, SelectionKind kind,
const std::set<Reference>& references)
{
auto& uids = selection.uids[kind];
uids.clear();
for (auto reference : references)
if (auto uid = handle_uid_get(anm2.handle_get(reference, kind == SelectionKind::GROUPS))) uids.insert(uid);
}
// The most specific part of the focus that still exists.
Reference selection_focus_get(const Selection& selection, const UidIndex& index)
{
for (auto uid : {selection.focus.frame, selection.focus.item, selection.focus.animation})
if (auto reference = index.reference_get(uid)) return *reference;
return {};
}
void selection_focus_set(Selection& selection, const Anm2& anm2, Reference reference)
{
selection.focus = anm2.handle_get(reference);
}
// After an edit: drops uids that no longer exist, and moves a focus whose element was removed to its nearest
// survivor (a frame at the same index of its track, the animation's root track, or the nearest animation).
void selection_follow(Selection& selection, const Anm2& anm2, const UidIndex& index, Reference before)
{
for (auto& [kind, uids] : selection.uids)
std::erase_if(uids, [&](std::uint64_t uid) { return !index.contains(uid); });
auto [animationUid, itemUid, frameUid] = selection.focus;
auto is_removed = [&](std::uint64_t uid) { return uid && !index.contains(uid); };
auto reference = selection_focus_get(selection, index);
if (is_removed(animationUid) || !animationUid)
{
auto animations = anm2.element_get(ElementType::ANIMATIONS);
auto count = animations ? animations_count_get(*animations) : 0;
reference =
count > 0 && animationUid ? Reference{std::clamp(before.animationIndex, 0, count - 1), ROOT} : Reference{};
}
else if (is_removed(itemUid))
reference = {reference.animationIndex, ROOT};
else if (is_removed(frameUid))
{
auto track = anm2.element_get(reference);
auto count = track ? track_frames_count_get(*track) : 0;
reference.frameIndex = count > 0 ? std::clamp(before.frameIndex, 0, count - 1) : -1;
}
else
return;
selection_focus_set(selection, anm2, reference);
}
}