Stage 7: typed model; Element tree is internal to model/xml.cpp

- src/model: typed Model (Info, Content, nested TrackGroup/AnimationGroup,
  copy-on-write shared animations, Extras/XmlNode passthrough for unknown XML,
  layout vectors so saves stay byte-identical).
- src/edit, Document, Selection, Snapshots, every window, the timeline, wizards
  and popups now read and edit model::Model; no UI code touches Element.
- anm2/ is reduced to the XML reader/writer/normalizer behind model/xml.cpp;
  dead Element operations (merge, regions, item_add, rect, frame helpers,
  element_get/unused, deserialize) are deleted.
- Goldens, invariants, undo walk and the local corpus (1,251 files) match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 16:53:17 -04:00
1 parent 2d0f70b1e5
commit 0b6f67d9bd
72 files changed
+4974 -4025

No files matched your search

+21 -21
View File
@@ -8,7 +8,8 @@
#include <fstream>
#include <sstream>
#include "anm2/anm2.hpp"
#include "edit/edit.hpp"
#include "model/xml.hpp"
#include "util/xml.hpp"
namespace anm2ed::test
@@ -52,32 +53,33 @@ namespace anm2ed::test
return fixtures;
}
inline Anm2 anm2_load(const std::string& text)
inline model::Model model_load(const std::string& text)
{
Anm2 anm2{};
REQUIRE(anm2.load_string(text));
return anm2;
model::Model model{};
REQUIRE(model::model_load_string(model, text));
return model;
}
// Every serialization and merge the editor can produce for a document; goldens lock each one down.
inline std::vector<Variant> variants_get(const Anm2& anm2)
inline std::vector<Variant> variants_get(const model::Model& model)
{
std::vector<Variant> variants{};
std::string hashes{};
for (auto [name, flags] : PRESETS)
{
variants.emplace_back(name, element_to_string(anm2.normalized_for_serialize(flags).root, flags));
variants.emplace_back(name, model::model_serialize(model, flags));
for (auto [formatName, isExtended] : FORMATS)
hashes += std::format("{}.{} {:016x}\n", name, formatName, anm2.hash({flags, isExtended}));
hashes += std::format("{}.{} {:016x}\n", name, formatName, model::model_hash(model, {flags, isExtended}));
}
for (auto [name, isExtended] : FORMATS)
variants.emplace_back(name, anm2.to_string({.isExtendedFormat = isExtended}));
variants.emplace_back(name, model::model_to_string(model, {.isExtendedFormat = isExtended}));
for (auto [name, type] : MERGES)
{
auto merged = anm2;
auto merged = model;
std::set<int> sources{0, 1, 2};
merged.animations_merge(0, sources, type, type == types::merge::PREPEND || type == types::merge::REPLACE);
variants.emplace_back(name, merged.to_string({.isExtendedFormat = true}));
edit::animations_merge(merged, 0, sources, type, type == types::merge::PREPEND || type == types::merge::REPLACE,
{});
variants.emplace_back(name, model::model_to_string(merged, {.isExtendedFormat = true}));
}
variants.emplace_back("hashes", hashes);
return variants;
@@ -94,25 +96,23 @@ namespace anm2ed::test
}
// Save/reload properties every document must hold.
inline void invariants_check(const Anm2& anm2)
inline void invariants_check(const model::Model& model)
{
for (auto [name, isExtended] : FORMATS)
{
INFO("format: ", std::string(name));
// The first save may repair invalid input (dangling ids, duplicates); after that, saves must be fixed points.
auto text = anm2.to_string({.isExtendedFormat = isExtended});
auto reloaded = anm2_load(text);
auto resaved = reloaded.to_string({.isExtendedFormat = isExtended});
auto text = model::model_to_string(model, {.isExtendedFormat = isExtended});
auto reloaded = model_load(text);
auto resaved = model::model_to_string(reloaded, {.isExtendedFormat = isExtended});
CHECK(resaved == text);
CHECK(anm2_load(resaved).hash() == reloaded.hash());
CHECK(model::model_hash(model_load(resaved)) == model::model_hash(reloaded));
}
// Loading only the game's view and re-saving it for the game must not change it.
auto game = game_document_get(anm2.to_string());
auto gameAnm2 = anm2_load(game);
CHECK(element_to_string(gameAnm2.normalized_for_serialize(SERIALIZE_ISAAC_DEFAULT).root, SERIALIZE_ISAAC_DEFAULT) ==
game);
auto game = game_document_get(model::model_to_string(model));
CHECK(model::model_serialize(model_load(game), SERIALIZE_ISAAC_DEFAULT) == game);
}
// Compares against a stored golden; ANM2ED_UPDATE_GOLDEN (or a missing golden when isBootstrap) rewrites it.
+2 -2
View File
@@ -17,9 +17,9 @@ TEST_CASE("local corpus matches baselines" * doctest::skip(!is_env_set("ANM2ED_C
if (line.empty()) continue;
INFO("file: ", line);
auto key = std::format("{:016x}", std::hash<std::string>{}(line));
Anm2 anm2{};
model::Model anm2{};
std::string error{};
if (!anm2.load(line, &error))
if (!model::model_load(anm2, line, &error))
{
golden_check(baseline / (key + ".error.txt"), error, true);
continue;
+1 -1
View File
@@ -8,6 +8,6 @@ TEST_CASE("per-fixture invariants")
for (const auto& fixture : fixtures_get())
{
INFO("fixture: ", fixture.filename().string());
invariants_check(anm2_load(file_load(fixture)));
invariants_check(model_load(file_load(fixture)));
}
}
+75 -75
View File
@@ -1,29 +1,31 @@
#include "common.hpp"
#include "edit/edit.hpp"
#include "model/frames.hpp"
#include "util/pack.hpp"
using namespace anm2ed;
using namespace anm2ed::test;
Anm2 fixture_load(const char* name) { return anm2_load(file_load(CORPUS_DIR / name)); }
model::Model fixture_load(const char* name) { return model_load(file_load(CORPUS_DIR / name)); }
void operation_check(const char* name, const Anm2& anm2)
void operation_check(const char* name, const model::Model& model)
{
golden_check(GOLDEN_DIR / std::format("operation.{}.xml", name), anm2.to_string({.isExtendedFormat = true}));
golden_check(GOLDEN_DIR / std::format("operation.{}.xml", name),
model::model_to_string(model, {.isExtendedFormat = true}));
}
Element& layer_track_get(Anm2& anm2, int animationIndex, int layerId)
model::Track& layer_track_get(model::Model& model, int animationIndex, int layerId)
{
auto track = animation_item_get(*anm2.element_get(ElementType::ANIMATION, animationIndex), ItemType::LAYER, layerId);
auto track = model.track_edit({animationIndex, LAYER, layerId});
REQUIRE(track);
return *track;
}
int groups_named_count(const Element& element, std::string_view name)
int groups_named_count(const model::Animation& animation, std::string_view name)
{
int count{};
element_each(element, [&](const Element& child) { count += child.type == ElementType::GROUP && child.name == name; });
for (const auto* entries : {&animation.layers, &animation.nulls})
model::groups_each(*entries, [&](const model::TrackGroup& group) { count += group.name == name; });
return count;
}
@@ -32,10 +34,9 @@ TEST_CASE("animations_merge keeps one group per name")
for (auto [name, type] : MERGES)
{
INFO("merge: ", std::string(name));
auto anm2 = fixture_load("03a_groups_nested.anm2");
std::set<int> sources{0, 1};
anm2.animations_merge(0, sources, type, false);
CHECK(groups_named_count(*anm2.element_get(ElementType::ANIMATION, 0), "Main") == 1);
auto model = fixture_load("03a_groups_nested.anm2");
edit::animations_merge(model, 0, {0, 1}, type, false, {});
CHECK(groups_named_count(*model.animation_get(0), "Main") == 1);
}
}
@@ -47,34 +48,31 @@ TEST_CASE("file_merge presets")
{"file_merge_replace", FILE_MERGE_PRESET_REPLACE_MATCHING}};
for (auto [name, preset] : PRESETS_FILE_MERGE)
{
auto anm2 = fixture_load("02_items.anm2");
CHECK(anm2.file_merge(CORPUS_DIR / "05_regions.anm2", CORPUS_DIR, preset));
operation_check(name, anm2);
auto model = fixture_load("02_items.anm2");
CHECK(edit::file_merge(model, CORPUS_DIR / "05_regions.anm2", CORPUS_DIR, preset));
operation_check(name, model);
}
}
TEST_CASE("item_add")
{
auto anm2 = fixture_load("02_items.anm2");
auto layer = element_make(ElementType::LAYER_ELEMENT);
layer.name = "added";
layer.spritesheetId = 1;
CHECK(anm2.item_add(ItemType::LAYER, 0, layer, 1, types::destination::ALL) == 2);
auto null = element_make(ElementType::NULL_ELEMENT);
null.name = "thisOnly";
CHECK(anm2.item_add(ItemType::NULL_, 1, null, -1, types::destination::THIS) == 2);
operation_check("item_add", anm2);
auto model = fixture_load("02_items.anm2");
CHECK(edit::item_add(model, ItemType::LAYER, 0, -1, "added", 1, false, 1, types::destination::ALL) == 2);
CHECK(edit::item_add(model, ItemType::NULL_, 1, -1, "thisOnly", 0, false, -1, types::destination::THIS) == 2);
operation_check("item_add", model);
}
TEST_CASE("regions_generate and regions_scan")
{
auto anm2 = fixture_load("02_items.anm2");
CHECK(anm2.regions_generate({0}, {}, "Region {}", RegionFrameMapping::SET));
CHECK_FALSE(anm2.regions_generate({0}, {}, "Region {}", RegionFrameMapping::SET));
operation_check("regions_generate", anm2);
auto model = fixture_load("02_items.anm2");
edit::regions_generate(model, {0}, {}, "Region {}", RegionFrameMapping::SET);
auto generated = model;
edit::regions_generate(model, {0}, {}, "Region {}", RegionFrameMapping::SET);
CHECK(model::is_model_equal(model, generated));
operation_check("regions_generate", model);
auto scanned = fixture_load("05_regions.anm2");
scanned.regions_scan();
edit::regions_scan(scanned);
operation_check("regions_scan", scanned);
}
@@ -87,30 +85,30 @@ TEST_CASE("frames_change")
{"divide", ChangeType::DIVIDE}};
for (auto [name, type] : CHANGES)
{
auto anm2 = fixture_load("02_items.anm2");
auto model = fixture_load("02_items.anm2");
FrameChange change{.cropX = 2.0f, .positionY = 4.0f, .scaleX = 50.0f, .rotation = 90.0f, .tintR = 0.5f};
change.duration = 3;
change.interpolation = Interpolation::EASE_IN;
change.isFlipX = true;
frames_change(layer_track_get(anm2, 0, 0), change, ItemType::LAYER, type, {0, 1});
operation_check(std::format("frames_change_{}", name).c_str(), anm2);
model::frames_change(layer_track_get(model, 0, 0), change, ItemType::LAYER, type, {0, 1});
operation_check(std::format("frames_change_{}", name).c_str(), model);
}
}
TEST_CASE("frame_bake and frame_mix")
{
auto anm2 = fixture_load("07_special_interpolation.anm2");
frame_bake(layer_track_get(anm2, 0, 0), 0, 2, true, false);
frame_bake(layer_track_get(anm2, 0, 0), 3, 1, false, true);
operation_check("frame_bake", anm2);
auto model = fixture_load("07_special_interpolation.anm2");
model::frame_bake(layer_track_get(model, 0, 0), 0, 2, true, false);
model::frame_bake(layer_track_get(model, 0, 0), 3, 1, false, true);
operation_check("frame_bake", model);
auto from = element_make(ElementType::FRAME);
auto to = element_make(ElementType::FRAME);
model::Frame from{};
model::Frame to{};
to.position = {10.0f, -20.0f};
to.shear = {30.0f, 0.0f};
to.rotation = 90.0f;
to.tint = {0.0f, 1.0f, 1.0f, 0.5f};
frame_mix(from, to, 0.5f);
model::frame_mix(from, to, 0.5f);
CHECK(from.position == glm::vec2(5.0f, -10.0f));
CHECK(from.shear == glm::vec2(15.0f, 0.0f));
CHECK(from.rotation == doctest::Approx(45.0f));
@@ -144,80 +142,83 @@ TEST_CASE("edit layer operations")
{
const char* name;
const char* fixture;
std::function<edit::Uids(Anm2&)> operation;
std::function<edit::Uids(model::Model&)> operation;
std::size_t uidCount;
};
const Reference head{0, LAYER, 0};
const Case CASES[] = {
{"frame_insert", "02_items.anm2", [&](Anm2& anm2) { return edit::frame_insert(anm2, {0, LAYER, 0, 0}, 0); }, 1},
{"trigger_insert", "02_items.anm2", [&](Anm2& anm2) { return edit::frame_insert(anm2, {0, TRIGGER, -1, 0}, 2); },
1},
{"frame_insert", "02_items.anm2",
[&](model::Model& anm2) { return edit::frame_insert(anm2, {0, LAYER, 0, 0}, 0); }, 1},
{"trigger_insert", "02_items.anm2",
[&](model::Model& anm2) { return edit::frame_insert(anm2, {0, TRIGGER, -1, 0}, 2); }, 1},
{"frames_delete", "02_items.anm2",
[&](Anm2& anm2) { return edit::frames_delete(anm2, {{0, LAYER, 0, 0}, {0, ROOT, -1, 1}}); }, 0},
{"frames_duplicate", "02_items.anm2", [&](Anm2& anm2)
[&](model::Model& anm2) { return edit::frames_delete(anm2, {{0, LAYER, 0, 0}, {0, ROOT, -1, 1}}); }, 0},
{"frames_duplicate", "02_items.anm2", [&](model::Model& anm2)
{ return edit::frames_duplicate(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, {0, LAYER, 0, 1}); }, 2},
{"frames_reverse", "07_special_interpolation.anm2",
[&](Anm2& anm2) { return edit::frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 2}}); }, 0},
{"frames_bake", "07_special_interpolation.anm2",
[&](Anm2& anm2) { return edit::frames_bake(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, 2, true, false); }, 5},
{"frame_split", "02_items.anm2", [&](Anm2& anm2) { return edit::frame_split(anm2, {0, LAYER, 1, 0}, 2); }, 1},
[&](model::Model& anm2) { return edit::frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 2}}); }, 0},
{"frames_bake", "07_special_interpolation.anm2", [&](model::Model& anm2)
{ return edit::frames_bake(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, 2, true, false); }, 5},
{"frame_split", "02_items.anm2", [&](model::Model& anm2) { return edit::frame_split(anm2, {0, LAYER, 1, 0}, 2); },
1},
{"frames_move", "02_items.anm2",
[&](Anm2& anm2) { return edit::frames_move(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 1, 0}}, head, 2); }, 2},
[&](model::Model& anm2) { return edit::frames_move(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 1, 0}}, head, 2); }, 2},
{"frames_paste", "02_items.anm2",
[&](Anm2& anm2)
[&](model::Model& anm2)
{
auto frame = anm2.element_get(Reference{0, LAYER, 1, 0});
auto text = element_to_string(*frame, ElementType::LAYER_ANIMATION) +
element_to_string(*frame, ElementType::LAYER_ANIMATION);
auto frame = anm2.frame_get(Reference{0, LAYER, 1, 0});
auto text = model::frame_to_string(*frame, ItemType::LAYER) + model::frame_to_string(*frame, ItemType::LAYER);
return edit::frames_paste(anm2, {0, LAYER, 0, 0}, {}, text, 0, nullptr);
},
2},
{"root_bake_into", "04_group_root_transform.anm2",
[&](Anm2& anm2)
[&](model::Model& anm2)
{
return edit::root_bake_into(anm2, {{0, ROOT, -1, 0}}, {{0, LAYER, 2}},
{.isRoundScale = true, .isMatchRootInterpolation = true});
},
1},
{"items_group", "03b_groups_legacy_flat.anm2",
[&](Anm2& anm2) { return edit::items_group(anm2, 0, LAYER, {2}, "New"); }, 1},
{"items_move", "03b_groups_legacy_flat.anm2",
[&](Anm2& anm2) { return edit::items_move(anm2, 0, LAYER, {2}, {}, {true, LAYER, 0, -1}, false, true); }, 0},
[&](model::Model& anm2) { return edit::items_group(anm2, 0, LAYER, {2}, "New"); }, 1},
{"items_move", "03b_groups_legacy_flat.anm2", [&](model::Model& anm2)
{ return edit::items_move(anm2, 0, LAYER, {2}, {}, {true, LAYER, 0, -1}, false, true); }, 0},
{"items_remove", "03b_groups_legacy_flat.anm2",
[&](Anm2& anm2) { return edit::items_remove(anm2, 0, {{LAYER, {2}}}, {{NULL_, {3}}}); }, 0},
[&](model::Model& anm2) { return edit::items_remove(anm2, 0, {{LAYER, {2}}}, {{NULL_, {3}}}); }, 0},
{"animation_add", "03a_groups_nested.anm2",
[&](Anm2& anm2) { return edit::animation_add(anm2, 1, 0, 0, "Added"); }, 1},
[&](model::Model& anm2) { return edit::animation_add(anm2, 1, 0, 0, "Added"); }, 1},
{"animations_remove", "03a_groups_nested.anm2",
[&](Anm2& anm2) { return edit::animations_remove(anm2, {1}, {0}); }, 0},
{"animations_move", "02_items.anm2", [&](Anm2& anm2) { return edit::animations_move(anm2, {0}, {}, 2, -1); }, 1},
{"animations_group", "02_items.anm2", [&](Anm2& anm2) { return edit::animations_group(anm2, {0, 1}, "Group"); },
0},
[&](model::Model& anm2) { return edit::animations_remove(anm2, {1}, {0}); }, 0},
{"animations_move", "02_items.anm2", [&](model::Model& anm2)
{ return edit::animations_move(anm2, {0}, {}, {.animationIndex = 1, .isAfter = true}); }, 1},
{"animations_group", "02_items.anm2",
[&](model::Model& anm2) { return edit::animations_group(anm2, {0, 1}, "Group"); }, 0},
{"animations_paste", "02_items.anm2",
[&](Anm2& anm2)
[&](model::Model& anm2)
{
std::set<int> groupIds{};
auto text = element_to_string(*anm2.element_get(ElementType::ANIMATION, 1));
auto text = model::animation_to_string(*anm2.animation_get(1));
return edit::animations_paste(anm2, text, 0, -1, groupIds, nullptr);
},
1},
{"regions_remove_unused", "05_regions.anm2", [&](Anm2& anm2) { return edit::regions_remove_unused(anm2, 0); }, 0},
{"regions_remove_unused", "05_regions.anm2",
[&](model::Model& anm2) { return edit::regions_remove_unused(anm2, 0); }, 0},
{"regions_paste", "05_regions.anm2",
[&](Anm2& anm2)
[&](model::Model& anm2)
{
auto region = child_id_get(*anm2.element_get(ElementType::SPRITESHEET, 0), ElementType::REGION, 1);
return edit::regions_paste(anm2, 0, element_to_string(*region), 1, nullptr);
auto region = model::item_get(model::item_get(anm2.content.spritesheets, 0)->regions, 1);
return edit::regions_paste(anm2, 0, model::item_to_string(*region), 1, nullptr);
},
0},
{"animation_grid_generate", "05_regions.anm2",
[&](Anm2& anm2)
[&](model::Model& anm2)
{
return edit::animation_grid_generate(anm2, {{0, LAYER, 0}},
{{0, 0}, {8, 8}, {4, 4}, 2, 3, 2, true, "Grid {}"});
},
3},
{"frames_change_apply", "02_items.anm2",
[&](Anm2& anm2)
[&](model::Model& anm2)
{
FrameChange change{.positionX = 3.0f, .rotation = 10.0f};
return edit::frames_change_apply(
@@ -226,18 +227,17 @@ TEST_CASE("edit layer operations")
},
0},
{"animations_merge", "03a_groups_nested.anm2",
[&](Anm2& anm2) { return edit::animations_merge(anm2, 0, {1}, types::merge::APPEND, true, {0}); }, 1},
[&](model::Model& anm2) { return edit::animations_merge(anm2, 0, {1}, types::merge::APPEND, true, {0}); }, 1},
};
for (const auto& entry : CASES)
{
INFO("operation: ", std::string(entry.name));
auto anm2 = fixture_load(entry.fixture);
anm2.uids_repair();
auto uids = entry.operation(anm2);
CHECK(uids.size() == entry.uidCount);
anm2.uids_repair();
UidIndex index(anm2);
model::UidIndex index(anm2);
for (auto uid : uids)
CHECK(index.reference_get(uid));
operation_check(entry.name, anm2);
+1 -1
View File
@@ -9,7 +9,7 @@ TEST_CASE("fixtures match goldens")
REQUIRE(!fixtures.empty());
for (const auto& fixture : fixtures)
{
auto anm2 = anm2_load(file_load(fixture));
auto anm2 = model_load(file_load(fixture));
for (const auto& [name, text] : variants_get(anm2))
golden_check(GOLDEN_DIR / std::format("{}.{}.xml", fixture.stem().string(), name), text);
}
+24 -24
View File
@@ -6,31 +6,32 @@
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;
}
model::Model fixture_load_with_uids(const char* name) { return model_load(file_load(CORPUS_DIR / name)); }
// Applies an edit and follows the selection through it, the way a commit does.
void selection_edit(Anm2& anm2, Selection& selection, auto&& edit)
void selection_edit(model::Model& anm2, Selection& selection, auto&& edit)
{
auto before = selection_focus_get(selection, UidIndex(anm2));
auto before = selection_focus_get(selection, model::UidIndex(anm2));
edit();
anm2.uids_repair();
selection_follow(selection, anm2, UidIndex(anm2), before);
selection_follow(selection, anm2, model::UidIndex(anm2), before);
}
TEST_CASE("uids are unique and non-zero after load")
{
for (const auto& fixture : fixtures_get())
{
auto anm2 = anm2_load(file_load(fixture));
auto anm2 = model_load(file_load(fixture));
std::set<std::uint64_t> uids{};
bool isUnique{true};
element_each(anm2.root,
[&](const Element& element) { isUnique &= element.uid != 0 && uids.insert(element.uid).second; });
for (auto [index, animation] : anm2.animations_get())
model::animation_tracks_each(*animation,
[&](const model::Track& track)
{
isUnique &= uids.insert(track.uid).second;
for (const auto& frame : track.frames)
isUnique &= uids.insert(frame.uid).second;
});
INFO("fixture: ", fixture.filename().string());
CHECK(isUnique);
}
@@ -46,7 +47,7 @@ TEST_CASE("selected frames and focus follow inserts and moves")
selection_edit(anm2, selection, [&] { edit::frame_insert(anm2, {0, LAYER, 0, -1}, 0); });
selection_edit(anm2, selection, [&] { edit::frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}); });
UidIndex index(anm2);
model::UidIndex index(anm2);
CHECK(selection_focus_get(selection, index) == Reference{0, LAYER, 0, 0});
CHECK(selection_references_get(selection, index, SelectionKind::FRAMES) ==
std::set<Reference>{{0, LAYER, 0, 0}, {0, LAYER, 1, 0}});
@@ -60,33 +61,32 @@ TEST_CASE("a removed focus falls back to its nearest survivor")
selection_references_set(selection, anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}});
selection_edit(anm2, selection, [&] { edit::frames_delete(anm2, {{0, LAYER, 0, 1}}); });
CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, LAYER, 0, 0});
CHECK(selection_focus_get(selection, model::UidIndex(anm2)) == Reference{0, LAYER, 0, 0});
CHECK(selection.uids[SelectionKind::FRAMES].empty());
selection_edit(anm2, selection, [&] { edit::items_remove(anm2, 0, {{LAYER, {0}}}, {}); });
CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, ROOT});
CHECK(selection_focus_get(selection, model::UidIndex(anm2)) == Reference{0, ROOT});
selection_focus_set(selection, anm2, {1, ROOT, -1, 0});
selection_edit(anm2, selection, [&] { edit::animations_remove(anm2, {1}, {}); });
CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, ROOT});
CHECK(selection_focus_get(selection, model::UidIndex(anm2)) == Reference{0, ROOT});
}
TEST_CASE("track and group selection follow regrouping")
{
auto anm2 = fixture_load_with_uids("03a_groups_nested.anm2");
auto group =
child_first_get(*child_first_get(*anm2.element_get(ElementType::ANIMATION, 0), ElementType::LAYER_ANIMATIONS),
ElementType::GROUP);
const model::TrackGroup* group{};
model::groups_each(anm2.animation_get(0)->layers, [&](const model::TrackGroup& candidate) { group = &candidate; });
REQUIRE(group);
auto groupId = group->id;
Selection selection{};
selection_references_set(selection, anm2, SelectionKind::TRACKS, {{0, LAYER, 0, -1, LAYER, group->id}});
selection_references_set(selection, anm2, SelectionKind::GROUPS, {{0, LAYER, group->id}});
selection_references_set(selection, anm2, SelectionKind::TRACKS, {{0, LAYER, 0, -1, LAYER, groupId}});
selection_references_set(selection, anm2, SelectionKind::GROUPS, {{0, LAYER, groupId}});
selection_edit(anm2, selection,
[&] { edit::items_move(anm2, 0, LAYER, {0}, {}, {false, LAYER, 2, -1}, false, false); });
UidIndex index(anm2);
model::UidIndex index(anm2);
CHECK(selection_references_get(selection, index, SelectionKind::TRACKS) == std::set<Reference>{{0, LAYER, 0}});
CHECK(selection_references_get(selection, index, SelectionKind::GROUPS) ==
std::set<Reference>{{0, LAYER, group->id}});
CHECK(selection_references_get(selection, index, SelectionKind::GROUPS) == std::set<Reference>{{0, LAYER, groupId}});
}
+33 -26
View File
@@ -12,22 +12,29 @@ 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)
std::vector<Reference> references_all_get(const model::Model& anm2, bool isFrames)
{
UidIndex index(anm2);
model::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);
});
auto add = [&](std::uint64_t uid)
{
auto reference = index.reference_get(uid);
if (!reference || reference->itemType == NONE || reference->itemType == TRIGGER) return;
if (isFrames == (reference->frameIndex >= 0)) result.push_back(*reference);
};
for (auto [i, animation] : anm2.animations_get())
model::animation_tracks_each(*animation,
[&](const model::Track& track)
{
add(track.uid);
for (const auto& frame : track.frames)
add(frame.uid);
});
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)
bool edit_random_apply(model::Model& anm2, std::mt19937& random)
{
auto frames = references_all_get(anm2, true);
auto tracks = references_all_get(anm2, false);
@@ -64,7 +71,7 @@ bool edit_random_apply(Anm2& anm2, std::mt19937& random)
edit::animation_add(anm2, 0, -1, frame.animationIndex, "Walk");
break;
default:
edit::animations_move(anm2, {frame.animationIndex}, {}, 0, -1);
edit::animations_move(anm2, {frame.animationIndex}, {}, {.animationIndex = 0});
break;
}
return true;
@@ -78,30 +85,31 @@ TEST_CASE("undo and redo restore every state of a random edit walk")
{
INFO("fixture: ", name);
Snapshots snapshots{};
snapshots.current.anm2 = anm2_load(file_load(CORPUS_DIR / name));
snapshots.current.model = model_load(file_load(CORPUS_DIR / name));
std::mt19937 random(WALK_SEED);
std::vector<std::uint64_t> hashes{snapshots.current.anm2.hash()};
std::vector<std::uint64_t> hashes{model::model_hash(snapshots.current.model)};
for (int step = 0; step < WALK_STEPS; ++step)
{
snapshots.push("Edit");
if (!edit_random_apply(snapshots.current.anm2, random)) break;
if (!edit_random_apply(snapshots.current.model, random)) break;
auto count = snapshots.undoStack.size();
snapshots.commit();
if (snapshots.current.anm2.hash() != hashes.back()) hashes.push_back(snapshots.current.anm2.hash());
if (snapshots.undoStack.size() != count) hashes.push_back(model::model_hash(snapshots.current.model));
}
REQUIRE(hashes.size() > 1);
for (auto it = hashes.rbegin() + 1; it != hashes.rend(); ++it)
{
REQUIRE(snapshots.undo());
CHECK(snapshots.current.anm2.hash() == *it);
CHECK(model::model_hash(snapshots.current.model) == *it);
}
CHECK_FALSE(snapshots.undo());
for (auto it = hashes.begin() + 1; it != hashes.end(); ++it)
{
REQUIRE(snapshots.redo());
CHECK(snapshots.current.anm2.hash() == *it);
CHECK(model::model_hash(snapshots.current.model) == *it);
}
}
}
@@ -110,21 +118,20 @@ 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}});
current.model = model_load(file_load(CORPUS_DIR / "02_items.anm2"));
selection_focus_set(current.selection, current.model, {0, LAYER, 0, 1});
selection_references_set(current.selection, current.model, 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());
edit::frame_insert(current.model, {0, LAYER, 0, -1}, 0);
auto& track = *current.model.track_edit(Reference{0, LAYER, 0});
std::rotate(track.frames.begin(), track.frames.end() - 1, track.frames.end());
snapshots.commit();
UidIndex index(current.anm2);
model::UidIndex index(current.model);
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});
CHECK(selection_focus_get(current.selection, model::UidIndex(current.model)) == Reference{0, LAYER, 0, 1});
}