- 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>
131 lines
4.3 KiB
C++
131 lines
4.3 KiB
C++
#include "common.hpp"
|
|
|
|
#include <random>
|
|
|
|
#include "edit/edit.hpp"
|
|
#include "snapshots.hpp"
|
|
|
|
using namespace anm2ed;
|
|
using namespace anm2ed::test;
|
|
|
|
constexpr int WALK_STEPS = 150;
|
|
constexpr int WALK_SEED = 1234;
|
|
constexpr int UNDO_LIMIT = 1000;
|
|
|
|
std::vector<Reference> references_all_get(const Anm2& anm2, bool isFrames)
|
|
{
|
|
UidIndex index(anm2);
|
|
std::vector<Reference> result{};
|
|
element_each(anm2.root,
|
|
[&](const Element& element)
|
|
{
|
|
auto reference = index.reference_get(element.uid);
|
|
if (!reference || reference->itemType == NONE || reference->itemType == TRIGGER) return;
|
|
if (isFrames == (reference->frameIndex >= 0)) result.push_back(*reference);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
// One random edit on randomly chosen live elements; returns false when nothing applicable exists.
|
|
bool edit_random_apply(Anm2& anm2, std::mt19937& random)
|
|
{
|
|
auto frames = references_all_get(anm2, true);
|
|
auto tracks = references_all_get(anm2, false);
|
|
if (frames.empty() || tracks.empty()) return false;
|
|
auto pick = [&](const auto& items)
|
|
{ return items[std::uniform_int_distribution<int>(0, (int)items.size() - 1)(random)]; };
|
|
auto frame = pick(frames);
|
|
auto other = pick(frames);
|
|
|
|
switch (std::uniform_int_distribution<int>(0, 8)(random))
|
|
{
|
|
case 0:
|
|
edit::frame_insert(anm2, frame, 0);
|
|
break;
|
|
case 1:
|
|
if (frames.size() > 4) edit::frames_delete(anm2, {frame, other});
|
|
break;
|
|
case 2:
|
|
edit::frames_duplicate(anm2, {frame, other}, frame);
|
|
break;
|
|
case 3:
|
|
edit::frames_reverse(anm2, {frame, other});
|
|
break;
|
|
case 4:
|
|
edit::frames_bake(anm2, {frame}, 1, false, false);
|
|
break;
|
|
case 5:
|
|
edit::frame_split(anm2, frame, 1);
|
|
break;
|
|
case 6:
|
|
if (frame.itemType != ROOT) edit::frames_move(anm2, {frame}, pick(tracks), 0);
|
|
break;
|
|
case 7:
|
|
edit::animation_add(anm2, 0, -1, frame.animationIndex, "Walk");
|
|
break;
|
|
default:
|
|
edit::animations_move(anm2, {frame.animationIndex}, {}, 0, -1);
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
TEST_CASE("undo and redo restore every state of a random edit walk")
|
|
{
|
|
SnapshotStack::max_size_set(UNDO_LIMIT);
|
|
for (auto name :
|
|
{"02_items.anm2", "03a_groups_nested.anm2", "04_group_root_transform.anm2", "07_special_interpolation.anm2"})
|
|
{
|
|
INFO("fixture: ", name);
|
|
Snapshots snapshots{};
|
|
snapshots.current.anm2 = anm2_load(file_load(CORPUS_DIR / name));
|
|
std::mt19937 random(WALK_SEED);
|
|
|
|
std::vector<std::uint64_t> hashes{snapshots.current.anm2.hash()};
|
|
for (int step = 0; step < WALK_STEPS; ++step)
|
|
{
|
|
snapshots.push("Edit");
|
|
if (!edit_random_apply(snapshots.current.anm2, random)) break;
|
|
snapshots.commit();
|
|
if (snapshots.current.anm2.hash() != hashes.back()) hashes.push_back(snapshots.current.anm2.hash());
|
|
}
|
|
REQUIRE(hashes.size() > 1);
|
|
|
|
for (auto it = hashes.rbegin() + 1; it != hashes.rend(); ++it)
|
|
{
|
|
REQUIRE(snapshots.undo());
|
|
CHECK(snapshots.current.anm2.hash() == *it);
|
|
}
|
|
CHECK_FALSE(snapshots.undo());
|
|
|
|
for (auto it = hashes.begin() + 1; it != hashes.end(); ++it)
|
|
{
|
|
REQUIRE(snapshots.redo());
|
|
CHECK(snapshots.current.anm2.hash() == *it);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("selection follows its frame through an edit and its undo")
|
|
{
|
|
Snapshots snapshots{};
|
|
auto& current = snapshots.current;
|
|
current.anm2 = anm2_load(file_load(CORPUS_DIR / "02_items.anm2"));
|
|
current.anm2.uids_repair();
|
|
selection_focus_set(current.selection, current.anm2, {0, LAYER, 0, 1});
|
|
selection_references_set(current.selection, current.anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}});
|
|
|
|
snapshots.push("Insert");
|
|
edit::frame_insert(current.anm2, {0, LAYER, 0, -1}, 0);
|
|
auto& track = *current.anm2.element_get(Reference{0, LAYER, 0});
|
|
std::rotate(track.children.begin(), track.children.end() - 1, track.children.end());
|
|
snapshots.commit();
|
|
UidIndex index(current.anm2);
|
|
CHECK(selection_focus_get(current.selection, index) == Reference{0, LAYER, 0, 2});
|
|
CHECK(selection_references_get(current.selection, index, SelectionKind::FRAMES) ==
|
|
std::set<Reference>{{0, LAYER, 0, 2}});
|
|
|
|
REQUIRE(snapshots.undo());
|
|
CHECK(selection_focus_get(current.selection, UidIndex(current.anm2)) == Reference{0, LAYER, 0, 1});
|
|
}
|