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

+148 -158
View File
@@ -1,26 +1,17 @@
#include "edit.hpp"
#include <algorithm>
#include <utility>
#include "math.hpp"
#include "uid.hpp"
#include "model/frames.hpp"
#include "model/xml.hpp"
using namespace anm2ed::util;
using namespace anm2ed::model;
namespace anm2ed::edit
{
Element element_clone(Element element)
{
element_each(element, [](Element& child) { child.uid = uid_next(); });
return element;
}
Element* track_get(Anm2& anm2, Reference reference)
{
reference.frameIndex = -1;
return anm2.element_get(reference);
}
std::map<Reference, std::set<int>> frames_by_track_get(const std::set<Reference>& frames)
{
std::map<Reference, std::set<int>> result{};
@@ -32,75 +23,73 @@ namespace anm2ed::edit
return result;
}
Uids frames_uids_get(const Element& track, int start, int count)
Uids frames_uids_get(const Track& track, int start, int count)
{
Uids uids{};
for (int i = start; i < start + count; ++i)
if (auto frame = track_frame_get(track, i)) uids.push_back(frame->uid);
for (int i = std::max(start, 0); i < std::min(start + count, (int)track.frames.size()); ++i)
uids.push_back(track.frames[i].uid);
return uids;
}
Uids frame_insert(Anm2& anm2, Reference reference, int time)
Uids frame_insert(Model& model, Reference reference, int time)
{
auto track = track_get(anm2, reference);
auto track = model.track_edit(reference);
if (!track) return {};
if (reference.itemType == TRIGGER)
if (track->type == ItemType::TRIGGER)
{
if (std::ranges::any_of(track->children, [&](const Element& trigger) { return trigger.atFrame == time; }))
return {};
auto trigger = element_clone(element_make(ElementType::TRIGGER));
if (std::ranges::any_of(track->frames, [&](const Frame& trigger) { return trigger.atFrame == time; })) return {};
Frame trigger{};
trigger.atFrame = time;
track->children.push_back(trigger);
track->frames.push_back(trigger);
frames_sort_by_at_frame(*track);
return {trigger.uid};
}
auto count = track_frames_count_get(*track);
auto source = track_frame_get(*track, reference.frameIndex);
auto last = track_frame_get(*track, count - 1);
auto frame = element_clone(source ? *source : last ? *last : element_make(ElementType::FRAME));
auto index = source ? std::clamp(reference.frameIndex + 1, 0, count) : count;
track->children.insert(track->children.begin() + track_frame_insert_child_index_get(*track, index), frame);
auto count = (int)track->frames.size();
auto isSource = reference.frameIndex >= 0 && reference.frameIndex < count;
auto frame = isSource ? frame_clone(track->frames[reference.frameIndex])
: count ? frame_clone(track->frames.back())
: Frame{};
auto index = isSource ? reference.frameIndex + 1 : count;
track->frames.insert(track->frames.begin() + index, frame);
return {frame.uid};
}
Uids frames_delete(Anm2& anm2, const std::set<Reference>& frames)
Uids frames_delete(Model& model, const std::set<Reference>& frames)
{
for (auto& [trackReference, indices] : frames_by_track_get(frames))
if (auto track = track_get(anm2, trackReference))
if (auto track = model.track_edit(trackReference))
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
if (auto childIndex = track_frame_child_index_get(*track, *it); childIndex != -1)
track->children.erase(track->children.begin() + childIndex);
if (*it >= 0 && *it < (int)track->frames.size()) track->frames.erase(track->frames.begin() + *it);
return {};
}
Uids frames_duplicate(Anm2& anm2, const std::set<Reference>& frames, Reference focus)
Uids frames_duplicate(Model& model, const std::set<Reference>& frames, Reference focus)
{
Uids uids{};
std::uint64_t focusUid{};
for (auto& [trackReference, indices] : frames_by_track_get(frames))
{
auto track = track_get(anm2, trackReference);
auto track = model.track_edit(trackReference);
if (!track) continue;
std::vector<Element> copies{};
std::vector<Frame> copies{};
int lastIndex{-1};
for (auto index : indices)
if (auto frame = track_frame_get(*track, index))
{
copies.push_back(element_clone(*frame));
lastIndex = index;
if (trackReference.animationIndex == focus.animationIndex && index == focus.frameIndex &&
track_get(anm2, focus) == track)
focusUid = copies.back().uid;
}
{
if (index < 0 || index >= (int)track->frames.size()) continue;
copies.push_back(frame_clone(track->frames[index]));
lastIndex = index;
auto focusTrack = focus;
focusTrack.frameIndex = -1;
if (focusTrack == trackReference && index == focus.frameIndex) focusUid = copies.back().uid;
}
if (copies.empty()) continue;
for (const auto& copy : copies)
uids.push_back(copy.uid);
auto childIndex = track_frame_insert_child_index_get(*track, lastIndex + 1);
track->children.insert(track->children.begin() + childIndex, copies.begin(), copies.end());
track->frames.insert(track->frames.begin() + lastIndex + 1, copies.begin(), copies.end());
}
if (auto it = std::ranges::find(uids, focusUid); focusUid && it != uids.end())
@@ -108,44 +97,44 @@ namespace anm2ed::edit
return uids;
}
Uids frames_reverse(Anm2& anm2, const std::set<Reference>& frames)
// Reverses the selected frames of each track as one block placed where the first of them was.
Uids frames_reverse(Model& model, const std::set<Reference>& frames)
{
for (auto& [trackReference, indices] : frames_by_track_get(frames))
{
auto track = track_get(anm2, trackReference);
auto track = model.track_edit(trackReference);
if (!track || indices.size() < 2) continue;
std::vector<Element> reversed{};
std::vector<Frame> reversed{};
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
if (auto childIndex = track_frame_child_index_get(*track, *it); childIndex != -1)
if (*it >= 0 && *it < (int)track->frames.size())
{
reversed.push_back(std::move(track->children[childIndex]));
track->children.erase(track->children.begin() + childIndex);
reversed.push_back(std::move(track->frames[*it]));
track->frames.erase(track->frames.begin() + *it);
}
auto childIndex = track_frame_insert_child_index_get(*track, *indices.begin());
track->children.insert(track->children.begin() + childIndex, reversed.begin(), reversed.end());
auto index = std::min(*indices.begin(), (int)track->frames.size());
track->frames.insert(track->frames.begin() + index, reversed.begin(), reversed.end());
}
return {};
}
Uids frames_bake(Anm2& anm2, const std::set<Reference>& frames, int interval, bool isRoundScale, bool isRoundRotation)
Uids frames_bake(Model& model, const std::set<Reference>& frames, int interval, bool isRoundScale,
bool isRoundRotation)
{
Uids uids{};
for (auto& [trackReference, indices] : frames_by_track_get(frames))
{
auto track = track_get(anm2, trackReference);
auto track = model.track_edit(trackReference);
if (!track) continue;
int inserted{};
for (auto originalIndex : indices)
{
auto index = originalIndex + inserted;
auto frame = track_frame_get(*track, index);
if (!frame) continue;
auto count = frame->duration <= FRAME_DURATION_MIN ? 1 : (int)std::ceil((float)frame->duration / interval);
if (index < 0 || index >= (int)track->frames.size()) continue;
auto duration = track->frames[index].duration;
auto count = duration <= FRAME_DURATION_MIN ? 1 : (int)std::ceil((float)duration / interval);
frame_bake(*track, index, interval, isRoundScale, isRoundRotation);
anm2.uids_repair();
uids.append_range(frames_uids_get(*track, index, count));
inserted += count - 1;
}
@@ -153,124 +142,138 @@ namespace anm2ed::edit
return uids;
}
Uids frame_split(Anm2& anm2, Reference reference, int time)
Uids frame_split(Model& model, Reference reference, int time)
{
auto track = reference.itemType == TRIGGER ? nullptr : track_get(anm2, reference);
auto frame = track ? track_frame_get(*track, reference.frameIndex) : nullptr;
if (!frame || frame->duration <= 1) return {};
auto duration = frame->duration;
auto firstDuration = time - (int)std::round(frame_time_from_index_get(*track, reference.frameIndex)) + 1;
auto source = model.frame_get(reference);
if (!source || reference.itemType == TRIGGER || source->duration <= 1) return {};
auto duration = source->duration;
auto firstDuration =
time - (int)std::round(frame_time_from_index_get(*model.track_get(reference), reference.frameIndex)) + 1;
if (firstDuration <= 0 || firstDuration >= duration) return {};
auto split = element_clone(*frame);
auto& track = *model.track_edit(reference);
auto& frame = track.frames[reference.frameIndex];
auto split = frame_clone(frame);
split.duration = duration - firstDuration;
if (auto next = track_frame_get(*track, reference.frameIndex + 1);
next && frame->interpolation != Interpolation::NONE)
frame_mix(split, *next, interpolation_factor(frame->interpolation, (float)firstDuration / (float)duration));
if (reference.frameIndex + 1 < (int)track.frames.size() && frame.interpolation != Interpolation::NONE)
frame_mix(split, track.frames[reference.frameIndex + 1],
interpolation_factor(frame.interpolation, (float)firstDuration / (float)duration));
frame->duration = firstDuration;
auto uid = frame->uid;
track->children.insert(
track->children.begin() + track_frame_insert_child_index_get(*track, reference.frameIndex + 1), split);
frame.duration = firstDuration;
auto uid = frame.uid;
track.frames.insert(track.frames.begin() + reference.frameIndex + 1, split);
return {uid};
}
// Moves frames (from any tracks) into one track before the frame at the given index.
Uids frames_move(Anm2& anm2, const std::set<Reference>& frames, Reference target, int insertIndex)
Uids frames_move(Model& model, const std::set<Reference>& frames, Reference target, int insertIndex)
{
auto targetTrack = track_get(anm2, target);
if (!targetTrack) return {};
insertIndex = std::clamp(insertIndex, 0, track_frames_count_get(*targetTrack));
target.frameIndex = -1;
auto targetTrack = model.track_get(target);
if (!targetTrack) return {};
auto targetUid = targetTrack->uid;
insertIndex = std::clamp(insertIndex, 0, (int)targetTrack->frames.size());
std::vector<Element> moved{};
std::vector<Frame> moved{};
for (auto& [trackReference, indices] : frames_by_track_get(frames))
{
auto track = track_get(anm2, trackReference);
auto track = model.track_edit(trackReference);
if (!track) continue;
std::vector<Element> trackMoved{};
std::vector<Frame> trackMoved{};
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
if (auto childIndex = track_frame_child_index_get(*track, *it); childIndex != -1)
{
trackMoved.insert(trackMoved.begin(), std::move(track->children[childIndex]));
track->children.erase(track->children.begin() + childIndex);
if (track == targetTrack && *it < insertIndex) --insertIndex;
}
{
if (*it < 0 || *it >= (int)track->frames.size()) continue;
trackMoved.insert(trackMoved.begin(), std::move(track->frames[*it]));
track->frames.erase(track->frames.begin() + *it);
if (track->uid == targetUid && *it < insertIndex) --insertIndex;
}
moved.append_range(trackMoved);
}
if (moved.empty()) return {};
insertIndex = std::clamp(insertIndex, 0, track_frames_count_get(*targetTrack));
auto childIndex = track_frame_insert_child_index_get(*targetTrack, insertIndex);
targetTrack->children.insert(targetTrack->children.begin() + childIndex, moved.begin(), moved.end());
return frames_uids_get(*targetTrack, insertIndex, (int)moved.size());
auto& destination = *model.track_edit(target);
insertIndex = std::clamp(insertIndex, 0, (int)destination.frames.size());
destination.frames.insert(destination.frames.begin() + insertIndex, moved.begin(), moved.end());
return frames_uids_get(destination, insertIndex, (int)moved.size());
}
Uids frame_durations_set(Anm2& anm2, const std::map<Reference, int>& durations)
Uids frame_durations_set(Model& model, const std::map<Reference, int>& durations)
{
for (auto [reference, duration] : durations)
if (auto frame = anm2.element_get(reference))
if (auto frame = model.frame_edit(reference))
frame->duration = std::clamp(duration, FRAME_DURATION_MIN, FRAME_DURATION_MAX);
return {};
}
// Places a trigger at a frame, stepping it back past any other trigger already there.
Uids trigger_at_frame_set(Anm2& anm2, Reference reference, int atFrame)
Uids trigger_at_frame_set(Model& model, Reference reference, int atFrame)
{
auto track = track_get(anm2, reference);
auto trigger = track ? track_frame_get(*track, reference.frameIndex) : nullptr;
auto trigger = model.frame_edit(reference);
if (!trigger) return {};
trigger->atFrame = atFrame;
for (const auto& other : track->children)
for (const auto& other : model.track_get(reference)->frames)
if (&other != trigger && other.atFrame == trigger->atFrame) trigger->atFrame--;
return {};
}
Uids frames_paste(Anm2& anm2, Reference reference, const std::set<Reference>& selection, const std::string& text,
Uids triggers_sort(Model& model, Reference reference)
{
if (auto track = model.track_edit(reference)) frames_sort_by_at_frame(*track);
return {};
}
// Pastes clipboard frames after the track's selected frames (or its current frame); triggers go at `time`, stepping
// past occupied frames. Frames lose regions that do not exist on the target layer.
Uids frames_paste(Model& model, Reference reference, const std::set<Reference>& selection, const std::string& text,
int time, std::string* errorString)
{
auto track = track_get(anm2, reference);
if (!track) return {};
auto source = model.track_get(reference);
if (!source) return {};
auto frames = frames_from_string(text, source->type, errorString);
if (frames.empty()) return {};
auto trackReference = reference;
trackReference.frameIndex = -1;
auto selected = frames_by_track_get(selection);
auto count = (int)track->children.size();
auto count = (int)source->frames.size();
auto insertIndex = count;
if (auto it = selected.find(trackReference); it != selected.end() && !it->second.empty())
insertIndex = std::min(count, *it->second.rbegin() + 1);
else if (reference.frameIndex >= 0 && reference.frameIndex < count)
insertIndex = reference.frameIndex + 1;
std::set<int> indices{};
if (!frames_deserialize(*track, text, reference.itemType == TRIGGER ? time : insertIndex, indices, errorString))
return {};
auto layer = reference.itemType == LAYER ? anm2.element_get(ElementType::LAYER_ELEMENT, reference.itemID) : nullptr;
auto spritesheet = layer ? anm2.element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
for (auto index : indices)
{
auto frame = track_frame_get(*track, index);
if (!frame) continue;
if (reference.itemType != LAYER)
auto spritesheet = reference.itemType == LAYER ? model.layer_spritesheet_get(reference.itemID) : nullptr;
for (auto& frame : frames)
if (reference.itemType != LAYER && reference.itemType != TRIGGER)
{
frame->regionId = -1;
frame->crop = frame->size = frame->pivot = {};
frame.regionId = -1;
frame.crop = frame.size = frame.pivot = {};
}
else if (frame->regionId != -1 &&
(!spritesheet || !child_id_get(*spritesheet, ElementType::REGION, frame->regionId)))
frame->regionId = -1;
else if (frame.regionId != -1 && (!spritesheet || !item_get(spritesheet->regions, frame.regionId)))
frame.regionId = -1;
auto& track = *model.track_edit(reference);
Uids uids{};
if (track.type != ItemType::TRIGGER)
{
track.frames.insert(track.frames.begin() + insertIndex, frames.begin(), frames.end());
return frames_uids_get(track, insertIndex, (int)frames.size());
}
anm2.uids_repair();
Uids uids{};
for (auto index : indices)
uids.append_range(frames_uids_get(*track, index, 1));
for (int i = 0; i < (int)frames.size(); ++i)
{
auto& trigger = frames[i];
trigger.atFrame = time + i;
while (frame_index_from_at_frame_get(track, trigger.atFrame) != -1)
++trigger.atFrame;
uids.push_back(trigger.uid);
track.frames.push_back(trigger);
}
frames_sort_by_at_frame(track);
return uids;
}
void frame_root_transform_apply(Element& frame, const Element& rootFrame, bool isRoundScale, bool isRoundRotation,
void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRoundScale, bool isRoundRotation,
bool isUseRootPivot)
{
auto rootScale = math::percent_to_unit(rootFrame.scale);
@@ -292,21 +295,22 @@ namespace anm2ed::edit
// Applies the selected root frames' transforms to the target tracks' frames under them, then resets those root
// frames to a default frame spanning the same time.
Uids root_bake_into(Anm2& anm2, const std::set<Reference>& rootFrames, const std::set<Reference>& targets,
Uids root_bake_into(Model& model, const std::set<Reference>& rootFrames, const std::set<Reference>& targets,
RootBakeOptions options)
{
auto byTrack = frames_by_track_get(rootFrames);
if (byTrack.size() != 1 || targets.empty()) return {};
auto& [rootReference, rootIndices] = *byTrack.begin();
auto root = track_get(anm2, rootReference);
if (!root) return {};
auto rootTrack = model.track_get(rootReference);
if (!rootTrack) return {};
auto root = *rootTrack;
std::vector<std::pair<int, int>> spans{};
for (auto index : rootIndices)
if (auto frame = track_frame_get(*root, index))
if (index >= 0 && index < (int)root.frames.size())
{
auto start = (int)frame_time_from_index_get(*root, index);
spans.emplace_back(start, start + frame->duration);
auto start = (int)frame_time_from_index_get(root, index);
spans.emplace_back(start, start + root.frames[index].duration);
}
if (spans.empty()) return {};
auto is_spanned = [&](int time)
@@ -314,63 +318,49 @@ namespace anm2ed::edit
for (auto target : targets)
{
auto track = track_get(anm2, target);
auto track = model.track_edit(target);
if (!track) continue;
auto frameType = track_frame_type_get(*track);
if (options.isMatchRootInterpolation)
{
std::vector<int> bakeIndices{};
int frameIndex{};
for (const auto& frame : track->children)
if (frame.type == frameType)
{
if (is_spanned((int)frame_time_from_index_get(*track, frameIndex))) bakeIndices.push_back(frameIndex);
++frameIndex;
}
for (auto it = bakeIndices.rbegin(); it != bakeIndices.rend(); ++it)
frame_bake(*track, *it, FRAME_DURATION_MIN, options.isRoundScale, options.isRoundRotation);
}
for (int index = (int)track->frames.size() - 1; index >= 0; --index)
if (is_spanned((int)frame_time_from_index_get(*track, index)))
frame_bake(*track, index, FRAME_DURATION_MIN, options.isRoundScale, options.isRoundRotation);
int frameIndex{};
for (auto& frame : track->children)
for (int index = 0; index < (int)track->frames.size(); ++index)
{
if (frame.type != frameType) continue;
auto start = (int)frame_time_from_index_get(*track, frameIndex++);
auto start = (int)frame_time_from_index_get(*track, index);
if (is_spanned(start))
frame_root_transform_apply(frame, frame_generate(*root, (float)start), options.isRoundScale,
frame_root_transform_apply(track->frames[index], frame_generate(root, (float)start), options.isRoundScale,
options.isRoundRotation, options.isUseRootPivot);
}
}
Uids uids{};
std::vector<Element> rewritten{};
int frameIndex{};
std::vector<Frame> rewritten{};
int defaultDuration{};
auto default_push = [&]()
{
if (defaultDuration <= 0) return;
auto frame = element_clone(element_make(ElementType::FRAME));
Frame frame{};
frame.duration = std::max(defaultDuration, FRAME_DURATION_MIN);
uids.push_back(frame.uid);
rewritten.push_back(frame);
defaultDuration = 0;
};
for (const auto& frame : root->children)
for (int index = 0; index < (int)root.frames.size(); ++index)
{
if (frame.type == ElementType::FRAME && rootIndices.contains(frameIndex++))
if (rootIndices.contains(index))
{
defaultDuration += frame.duration;
defaultDuration += root.frames[index].duration;
continue;
}
default_push();
rewritten.push_back(frame);
rewritten.push_back(root.frames[index]);
}
default_push();
root->children = std::move(rewritten);
anm2.uids_repair();
model.track_edit(rootReference)->frames = std::move(rewritten);
return uids;
}
}