Stage 3: operations layer (src/edit) and one undo entry point

- src/edit/ (in anm2ed_core): frame, track, animation, region, grid and
  change-all edits as plain functions of the model. Each returns the uids of what
  it created for the UI to select; copies get fresh uids.
- Document::edit_apply snapshots, runs an operation and commits; edit_begin
  starts drag edits. window_edit, edit_command_push, snapshot_command_push,
  SnapshotKind and the tracks/frames/regions/textures snapshot kinds are gone;
  Snapshots has a single push(message, isTextures).
- Timeline, animations, regions, shaders, items, wizards, taskbar and file merge
  call the edit layer; they keep only selection, scrolling and toasts.
- Snapshots, Storage, Playback and ImGui core build into anm2ed_core, so undo is
  testable: tests/undo.cpp runs a seeded random walk of edits per fixture and
  checks every undo/redo state by hash, plus selection follow through undo.
- types::edit renamed types::edit_state (clashed with the edit namespace).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 15:26:53 -04:00
1 parent 20bfbace7a
commit 700b613c62
63 files changed
+3223 -1775

No files matched your search

+182
View File
@@ -0,0 +1,182 @@
#include "edit.hpp"
#include "vector.hpp"
namespace anm2ed::edit
{
std::set<int> animation_group_ids_get(const Element& animations)
{
std::set<int> result{};
for (const auto& item : animations.children)
if (item.type == ElementType::GROUP) result.insert(item.id);
return result;
}
std::set<int> animation_group_indices_get(const Element& animations, int groupId)
{
std::set<int> result{};
int animationIndex{};
for (const auto& animation : animations.children)
if (animation.type == ElementType::ANIMATION)
{
if (animation.groupId == groupId) result.insert(animationIndex);
++animationIndex;
}
return result;
}
// An empty copy of a track container: same tracks and groups, no frames.
Element track_container_shell_copy(const Element* source, const TrackContainer& row)
{
auto destination = element_make(row.container);
if (!source) return destination;
auto nextGroupId = element_child_next_id_get(*source, ElementType::GROUP);
std::set<int> copiedTrackIds{};
auto track_push = [&](const Element& track, int groupId)
{
if (track.type != row.track || !copiedTrackIds.insert(track_id_get(track)).second) return;
auto item = element_make(row.track);
item.*(row.id) = track.*(row.id);
item.groupId = track.groupId != -1 ? track.groupId : groupId;
item.isVisible = track.isVisible;
destination.children.push_back(item);
};
for (const auto& sourceItem : source->children)
{
if (sourceItem.type != ElementType::GROUP)
{
track_push(sourceItem, -1);
continue;
}
auto group = element_make(ElementType::GROUP);
group.id = sourceItem.id >= 0 ? sourceItem.id : nextGroupId++;
group.name = sourceItem.name;
group.isExpanded = sourceItem.isExpanded;
group.isVisible = sourceItem.isVisible;
group.children.push_back(root_animation_make());
destination.children.push_back(group);
for (const auto& child : sourceItem.children)
track_push(child, group.id);
}
return destination;
}
// Adds an animation with the same (empty) tracks as the template animation.
Uids animation_add(Anm2& anm2, int index, int groupId, int templateIndex, const std::string& name)
{
auto animations = anm2.element_get(ElementType::ANIMATIONS);
if (!animations) return {};
auto animation = element_make(ElementType::ANIMATION);
animation.name = name;
animation.groupId = groupId;
animation.children.push_back(root_animation_make());
auto templateAnimation = anm2.element_get(ElementType::ANIMATION, templateIndex);
for (const auto& row : TRACK_CONTAINERS)
animation.children.push_back(track_container_shell_copy(
templateAnimation ? child_first_get(*templateAnimation, row.container) : nullptr, row));
animation.children.push_back(element_make(ElementType::TRIGGERS));
animation = element_clone(animation);
if (animations_count_get(*animations) == 0) animations->defaultAnimation = animation.name;
animations->children.insert(animations->children.begin() + animations_child_insert_index_get(*animations, index),
animation);
return {animation.uid};
}
Uids animations_remove(Anm2& anm2, const std::set<int>& indices, const std::set<int>& groupIds)
{
auto animations = anm2.element_get(ElementType::ANIMATIONS);
if (!animations) return {};
animation_groups_remove(*animations, groupIds);
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
if (auto childIndex = animations_child_index_get(*animations, *it); childIndex != -1)
animations->children.erase(animations->children.begin() + childIndex);
return {};
}
void animation_groups_remove(Element& animations, const std::set<int>& groupIds)
{
for (auto& item : animations.children)
if (item.type == ElementType::ANIMATION && groupIds.contains(item.groupId)) item.groupId = -1;
std::erase_if(animations.children,
[&](const Element& item) { return item.type == ElementType::GROUP && groupIds.contains(item.id); });
}
// Moves animations and whole groups to a child position; animations moved on their own join targetGroupId.
Uids animations_move(Anm2& anm2, const std::set<int>& indices, const std::set<int>& groupIds, int targetChildIndex,
int targetGroupId)
{
auto animations = anm2.element_get(ElementType::ANIMATIONS);
if (!animations) return {};
auto existingGroupIds = animation_group_ids_get(*animations);
auto groupId = existingGroupIds.contains(targetGroupId) ? targetGroupId : -1;
Uids uids{};
std::vector<int> childIndices{};
int animationIndex{};
for (int i = 0; i < (int)animations->children.size(); ++i)
{
auto& item = animations->children[i];
if (item.type == ElementType::GROUP && groupIds.contains(item.id)) childIndices.push_back(i);
if (item.type != ElementType::ANIMATION) continue;
auto isGroupMoved = groupIds.contains(item.groupId);
auto isMoved = indices.contains(animationIndex++);
if (isMoved && !isGroupMoved)
{
item.groupId = groupId;
uids.push_back(item.uid);
}
if (isGroupMoved || isMoved) childIndices.push_back(i);
}
util::vector::move_indices_to_position(animations->children, childIndices, targetChildIndex);
return uids;
}
Uids animations_group(Anm2& anm2, const std::set<int>& indices, const std::string& name)
{
auto animations = anm2.element_get(ElementType::ANIMATIONS);
if (!animations || indices.empty()) return {};
auto group = element_make(ElementType::GROUP);
group.id = element_child_next_id_get(*animations, ElementType::GROUP);
group.name = name;
for (auto index : indices)
if (auto animation = anm2.element_get(ElementType::ANIMATION, index)) animation->groupId = group.id;
animations->children.insert(
animations->children.begin() + animations_child_index_get(*animations, *indices.begin()), group);
return {};
}
Uids animations_paste(Anm2& anm2, const std::string& text, int start, int targetGroupId, std::set<int>& groupIds,
std::string* errorString)
{
std::set<int> indices{};
if (!anm2.animations_deserialize(text, start, indices, errorString, &groupIds)) return {};
anm2.uids_repair();
Uids uids{};
for (auto index : indices)
if (auto animation = anm2.element_get(ElementType::ANIMATION, index))
{
if (targetGroupId != -1 && groupIds.empty()) animation->groupId = targetGroupId;
uids.push_back(animation->uid);
}
return uids;
}
Uids animations_merge(Anm2& anm2, int target, std::set<int> sources, types::merge::Type type, bool isDeleteAfter,
const std::set<int>& ungroupIds)
{
auto merged = sources.size() > 1 || !sources.contains(target)
? anm2.animations_merge(target, sources, type, isDeleteAfter)
: target;
if (merged == -1) return {};
if (auto animations = anm2.element_get(ElementType::ANIMATIONS)) animation_groups_remove(*animations, ungroupIds);
auto animation = anm2.element_get(ElementType::ANIMATION, merged);
return animation ? Uids{animation->uid} : Uids{};
}
}
+127
View File
@@ -0,0 +1,127 @@
#include "edit.hpp"
#include "vector.hpp"
namespace anm2ed
{
std::string region_name_get(const std::string&, int);
}
namespace anm2ed::edit
{
// Clears frame references to the spritesheet's unused regions, then removes them.
Uids regions_remove_unused(Anm2& anm2, int spritesheetId)
{
auto unused = anm2.region_unused(spritesheetId);
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
if (unused.empty() || !spritesheet) return {};
animations_tracks_each(anm2.root, ElementType::LAYER_ANIMATION,
[&](Element& track)
{
auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, track.layerId);
if (!layer || layer->spritesheetId != spritesheetId) return;
for (auto& frame : track.children)
if (frame.type == ElementType::FRAME && unused.contains(frame.regionId))
frame.regionId = -1;
});
for (auto id : unused)
element_child_id_erase(*spritesheet, ElementType::REGION, id);
return {};
}
// Pastes regions into a spritesheet at a child position; they get fresh ids after the existing ones.
Uids regions_paste(Anm2& anm2, int spritesheetId, const std::string& text, int insertIndex, std::string* errorString)
{
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
if (!spritesheet) return {};
auto maxId = element_child_max_id_get(*spritesheet, ElementType::REGION);
if (!anm2.deserialize(ElementType::REGION, text, true, errorString, {}, spritesheetId)) return {};
spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
std::vector<int> pasted{};
for (int i = 0; i < (int)spritesheet->children.size(); i++)
if (spritesheet->children[i].type == ElementType::REGION && spritesheet->children[i].id > maxId)
pasted.push_back(i);
util::vector::move_indices_to_position(spritesheet->children, pasted, insertIndex);
return {};
}
// Appends grid frames to each layer track (optionally making a region per frame) and fits the animation length.
Uids animation_grid_generate(Anm2& anm2, const std::vector<Reference>& tracks, const GridOptions& options)
{
Uids uids{};
for (auto reference : tracks)
{
auto track = anm2.element_get(reference);
auto animation = anm2.element_get(ElementType::ANIMATION, reference.animationIndex);
auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, reference.itemID);
auto spritesheet = layer ? anm2.element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
if (!track || !animation) continue;
auto start = (int)track->children.size();
frames_generate_from_grid(*track, options.start, options.size, options.pivot, options.columns, options.count,
options.delay);
for (int i = start; i < (int)track->children.size(); ++i)
{
auto& frame = track->children[i];
if (frame.type != ElementType::FRAME) continue;
if (!options.isMakeRegions || !spritesheet) continue;
auto region = element_make(ElementType::REGION);
region.id = element_child_next_id_get(*spritesheet, ElementType::REGION);
region.name = region_name_get(options.regionNameFormat, i - start);
region.crop = frame.crop;
region.size = frame.size;
region.pivot = frame.pivot;
frame.regionId = region.id;
spritesheet->children.push_back(region);
}
animation->frameNum = animation_length_get(*animation);
anm2.uids_repair();
for (int i = start; i < (int)track->children.size(); ++i)
uids.push_back(track->children[i].uid);
}
return uids;
}
// Applies a frame change to explicit frames, to whole tracks (frameIndex -1), and to every matching track of the
// given animations.
Uids frames_change_apply(Anm2& anm2, const ChangeTargets& targets, FrameChange change, ChangeType type)
{
std::map<Reference, std::set<int>> frames{};
std::set<Reference> wholeTracks{};
for (auto reference : targets.references)
{
auto index = std::exchange(reference.frameIndex, -1);
index < 0 ? (void)wholeTracks.insert(reference) : (void)frames[reference].insert(index);
}
auto whole_apply = [&](Element* track, ItemType itemType)
{
if (!track) return;
std::set<int> indices{};
for (int i = 0; i < track_frames_count_get(*track); ++i)
indices.insert(i);
frames_change(*track, change, itemType, type, indices);
};
for (auto& [reference, indices] : frames)
if (auto track = anm2.element_get(reference))
frames_change(*track, change, (ItemType)reference.itemType, type, indices);
for (auto reference : wholeTracks)
whole_apply(anm2.element_get(reference), (ItemType)reference.itemType);
for (auto animationIndex : targets.animations)
{
auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex);
if (!animation) continue;
if (targets.isRoot) whole_apply(animation_item_get(*animation, ItemType::ROOT), ItemType::ROOT);
for (const auto& row : TRACK_CONTAINERS)
if (auto container = child_first_get(*animation, row.container);
container && (row.itemType == ItemType::LAYER ? targets.isLayers : targets.isNulls))
tracks_each(*container, row.track, [&](Element& track) { whole_apply(&track, row.itemType); });
}
return {};
}
}
+89
View File
@@ -0,0 +1,89 @@
#pragma once
#include <cstdint>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "anm2/anm2.hpp"
// Document edits as plain functions of the model. Each returns the uids of the elements it created or changed that
// the caller may want to select (the first one is the focus); selection itself stays with the UI.
namespace anm2ed::edit
{
using Uids = std::vector<std::uint64_t>;
struct RowTarget
{
bool isGroup{};
int type{NONE};
int id{-1};
int groupId{-1};
};
struct RootBakeOptions
{
bool isRoundScale{};
bool isRoundRotation{};
bool isMatchRootInterpolation{};
bool isUseRootPivot{};
};
struct GridOptions
{
glm::ivec2 start{};
glm::ivec2 size{};
glm::vec2 pivot{};
int columns{};
int count{};
int delay{};
bool isMakeRegions{};
std::string regionNameFormat{};
};
struct ChangeTargets
{
std::set<Reference> references{};
std::set<int> animations{};
bool isRoot{};
bool isLayers{};
bool isNulls{};
};
Element element_clone(Element);
Element* track_get(Anm2&, Reference);
std::map<Reference, std::set<int>> frames_by_track_get(const std::set<Reference>&);
Uids frame_insert(Anm2&, Reference, int);
Uids frames_delete(Anm2&, const std::set<Reference>&);
Uids frames_duplicate(Anm2&, const std::set<Reference>&, Reference);
Uids frames_reverse(Anm2&, const std::set<Reference>&);
Uids frames_bake(Anm2&, const std::set<Reference>&, int, bool, bool);
Uids frame_split(Anm2&, Reference, int);
Uids frames_move(Anm2&, const std::set<Reference>&, Reference, int);
Uids frame_durations_set(Anm2&, const std::map<Reference, int>&);
Uids trigger_at_frame_set(Anm2&, Reference, int);
Uids frames_paste(Anm2&, Reference, const std::set<Reference>&, const std::string&, int, std::string*);
Uids root_bake_into(Anm2&, const std::set<Reference>&, const std::set<Reference>&, RootBakeOptions);
void frame_root_transform_apply(Element&, const Element&, bool, bool, bool);
Uids items_remove(Anm2&, int, const std::map<int, std::set<int>>&, const std::map<int, std::set<int>>&);
Uids items_group(Anm2&, int, int, const std::set<int>&, const std::string&);
Uids items_move(Anm2&, int, int, const std::set<int>&, const std::set<int>&, RowTarget, bool, bool);
Uids animation_length_fit(Anm2&, int);
std::set<int> animation_group_ids_get(const Element&);
std::set<int> animation_group_indices_get(const Element&, int);
void animation_groups_remove(Element&, const std::set<int>&);
Uids animation_add(Anm2&, int, int, int, const std::string&);
Uids animations_remove(Anm2&, const std::set<int>&, const std::set<int>&);
Uids animations_move(Anm2&, const std::set<int>&, const std::set<int>&, int, int);
Uids animations_group(Anm2&, const std::set<int>&, const std::string&);
Uids animations_paste(Anm2&, const std::string&, int, int, std::set<int>&, std::string*);
Uids regions_remove_unused(Anm2&, int);
Uids regions_paste(Anm2&, int, const std::string&, int, std::string*);
Uids animation_grid_generate(Anm2&, const std::vector<Reference>&, const GridOptions&);
Uids frames_change_apply(Anm2&, const ChangeTargets&, FrameChange, ChangeType);
Uids animations_merge(Anm2&, int, std::set<int>, types::merge::Type, bool, const std::set<int>&);
}
+376
View File
@@ -0,0 +1,376 @@
#include "edit.hpp"
#include <utility>
#include "math.hpp"
#include "uid.hpp"
using namespace anm2ed::util;
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{};
for (auto frame : frames)
{
auto index = std::exchange(frame.frameIndex, -1);
result[frame].insert(index);
}
return result;
}
Uids frames_uids_get(const Element& 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);
return uids;
}
Uids frame_insert(Anm2& anm2, Reference reference, int time)
{
auto track = track_get(anm2, reference);
if (!track) return {};
if (reference.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));
trigger.atFrame = time;
track->children.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);
return {frame.uid};
}
Uids frames_delete(Anm2& anm2, const std::set<Reference>& frames)
{
for (auto& [trackReference, indices] : frames_by_track_get(frames))
if (auto track = track_get(anm2, 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);
return {};
}
Uids frames_duplicate(Anm2& anm2, 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);
if (!track) continue;
std::vector<Element> 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 (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());
}
if (auto it = std::ranges::find(uids, focusUid); focusUid && it != uids.end())
std::rotate(uids.begin(), it, it + 1);
return uids;
}
Uids frames_reverse(Anm2& anm2, const std::set<Reference>& frames)
{
for (auto& [trackReference, indices] : frames_by_track_get(frames))
{
auto track = track_get(anm2, trackReference);
if (!track || indices.size() < 2) continue;
std::vector<Element> reversed{};
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
if (auto childIndex = track_frame_child_index_get(*track, *it); childIndex != -1)
{
reversed.push_back(std::move(track->children[childIndex]));
track->children.erase(track->children.begin() + childIndex);
}
auto childIndex = track_frame_insert_child_index_get(*track, *indices.begin());
track->children.insert(track->children.begin() + childIndex, reversed.begin(), reversed.end());
}
return {};
}
Uids frames_bake(Anm2& anm2, 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);
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);
frame_bake(*track, index, interval, isRoundScale, isRoundRotation);
anm2.uids_repair();
uids.append_range(frames_uids_get(*track, index, count));
inserted += count - 1;
}
}
return uids;
}
Uids frame_split(Anm2& anm2, 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;
if (firstDuration <= 0 || firstDuration >= duration) return {};
auto split = element_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));
frame->duration = firstDuration;
auto uid = frame->uid;
track->children.insert(
track->children.begin() + track_frame_insert_child_index_get(*track, 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)
{
auto targetTrack = track_get(anm2, target);
if (!targetTrack) return {};
insertIndex = std::clamp(insertIndex, 0, track_frames_count_get(*targetTrack));
target.frameIndex = -1;
std::vector<Element> moved{};
for (auto& [trackReference, indices] : frames_by_track_get(frames))
{
auto track = track_get(anm2, trackReference);
if (!track) continue;
std::vector<Element> 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;
}
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());
}
Uids frame_durations_set(Anm2& anm2, const std::map<Reference, int>& durations)
{
for (auto [reference, duration] : durations)
if (auto frame = anm2.element_get(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)
{
auto track = track_get(anm2, reference);
auto trigger = track ? track_frame_get(*track, reference.frameIndex) : nullptr;
if (!trigger) return {};
trigger->atFrame = atFrame;
for (const auto& other : track->children)
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,
int time, std::string* errorString)
{
auto track = track_get(anm2, reference);
if (!track) return {};
auto trackReference = reference;
trackReference.frameIndex = -1;
auto selected = frames_by_track_get(selection);
auto count = (int)track->children.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)
{
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;
}
anm2.uids_repair();
Uids uids{};
for (auto index : indices)
uids.append_range(frames_uids_get(*track, index, 1));
return uids;
}
void frame_root_transform_apply(Element& frame, const Element& rootFrame, bool isRoundScale, bool isRoundRotation,
bool isUseRootPivot)
{
auto rootScale = math::percent_to_unit(rootFrame.scale);
auto pivot = isUseRootPivot ? rootFrame.position : glm::vec2();
auto offset = (frame.position - pivot) * rootScale;
auto radians = glm::radians(rootFrame.rotation);
auto cos = std::cos(radians);
auto sin = std::sin(radians);
frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos);
frame.scale *= rootScale;
frame.rotation += rootFrame.rotation;
frame.tint *= rootFrame.tint;
frame.colorOffset += rootFrame.colorOffset;
if (isRoundScale) frame.scale = glm::round(frame.scale);
if (isRoundRotation) frame.rotation = std::round(frame.rotation);
}
// 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,
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 {};
std::vector<std::pair<int, int>> spans{};
for (auto index : rootIndices)
if (auto frame = track_frame_get(*root, index))
{
auto start = (int)frame_time_from_index_get(*root, index);
spans.emplace_back(start, start + frame->duration);
}
if (spans.empty()) return {};
auto is_spanned = [&](int time)
{ return std::ranges::any_of(spans, [&](auto span) { return time >= span.first && time < span.second; }); };
for (auto target : targets)
{
auto track = track_get(anm2, 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);
}
int frameIndex{};
for (auto& frame : track->children)
{
if (frame.type != frameType) continue;
auto start = (int)frame_time_from_index_get(*track, frameIndex++);
if (is_spanned(start))
frame_root_transform_apply(frame, frame_generate(*root, (float)start), options.isRoundScale,
options.isRoundRotation, options.isUseRootPivot);
}
}
Uids uids{};
std::vector<Element> rewritten{};
int frameIndex{};
int defaultDuration{};
auto default_push = [&]()
{
if (defaultDuration <= 0) return;
auto frame = element_clone(element_make(ElementType::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)
{
if (frame.type == ElementType::FRAME && rootIndices.contains(frameIndex++))
{
defaultDuration += frame.duration;
continue;
}
default_push();
rewritten.push_back(frame);
}
default_push();
root->children = std::move(rewritten);
anm2.uids_repair();
return uids;
}
}
+118
View File
@@ -0,0 +1,118 @@
#include "edit.hpp"
namespace anm2ed::edit
{
Element* track_container_get(Anm2& anm2, int animationIndex, int type)
{
auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex);
return animation ? child_first_get(*animation, TYPE_CONTAINERS[type]) : nullptr;
}
Uids items_remove(Anm2& anm2, int animationIndex, const std::map<int, std::set<int>>& ids,
const std::map<int, std::set<int>>& groupIds)
{
for (auto type : {LAYER, NULL_})
{
auto container = track_container_get(anm2, animationIndex, type);
if (!container) continue;
auto typeIds = ids.contains(type) ? ids.at(type) : std::set<int>{};
auto typeGroupIds = groupIds.contains(type) ? groupIds.at(type) : std::set<int>{};
std::erase_if(container->children,
[&](const Element& item)
{
if (item.type == ElementType::GROUP) return typeGroupIds.contains(item.id);
return item.type == TYPE_TRACKS[type] &&
(typeIds.contains(track_id_get(item)) || typeGroupIds.contains(item.groupId));
});
}
return {};
}
Uids items_group(Anm2& anm2, int animationIndex, int type, const std::set<int>& ids, const std::string& name)
{
auto container = track_container_get(anm2, animationIndex, type);
if (!container) return {};
auto group = element_clone(element_make(ElementType::GROUP));
group.id = element_child_next_id_get(*container, ElementType::GROUP);
group.name = name;
group.children.push_back(element_clone(root_animation_make()));
auto insertIndex = (int)container->children.size();
for (int i = 0; i < (int)container->children.size(); ++i)
{
auto& item = container->children[i];
if (item.type != TYPE_TRACKS[type] || !ids.contains(track_id_get(item))) continue;
insertIndex = std::min(insertIndex, i);
item.groupId = group.id;
}
if (insertIndex == (int)container->children.size()) return {};
container->children.insert(container->children.begin() + insertIndex, group);
return {group.uid};
}
// Moves tracks and whole groups next to (or into) a target row. Layers list top-down in reverse of nulls, so the
// drop side flips for them.
Uids items_move(Anm2& anm2, int animationIndex, int type, const std::set<int>& ids, const std::set<int>& groupIds,
RowTarget target, bool isDropAfter, bool isDropIntoGroup)
{
auto container = track_container_get(anm2, animationIndex, type);
if (!container) return {};
if (target.groupId != -1 && groupIds.contains(target.groupId)) return {};
if (target.isGroup && groupIds.contains(target.id)) return {};
auto trackType = TYPE_TRACKS[type];
auto& children = container->children;
auto is_moved = [&](const Element& item)
{
return (item.type == ElementType::GROUP && groupIds.contains(item.id)) ||
(item.type == trackType && (ids.contains(track_id_get(item)) || groupIds.contains(item.groupId)));
};
std::vector<Element> moved{};
std::ranges::copy_if(children, std::back_inserter(moved), is_moved);
if (moved.empty()) return {};
auto targetIndex = (int)children.size();
if (target.isGroup || target.type == type)
{
auto rowIndex = -1;
auto groupEndIndex = -1;
for (int i = 0; i < (int)children.size(); ++i)
{
auto& item = children[i];
auto isGroupRow = item.type == ElementType::GROUP && item.id == target.id;
if ((target.isGroup && isGroupRow) ||
(!target.isGroup && item.type == trackType && track_id_get(item) == target.id))
if (rowIndex == -1) rowIndex = i;
if (isGroupRow || (item.type == trackType && item.groupId == target.id)) groupEndIndex = i;
}
if (rowIndex == -1) return {};
if (target.isGroup)
targetIndex = type == LAYER ? (isDropAfter ? rowIndex : groupEndIndex + 1) : rowIndex + isDropAfter;
else
targetIndex = rowIndex + (type == LAYER ? !isDropAfter : isDropAfter);
}
for (int i = (int)children.size() - 1; i >= 0; --i)
if (is_moved(children[i]))
{
if (i < targetIndex) --targetIndex;
children.erase(children.begin() + i);
}
auto targetGroupId = target.isGroup && isDropIntoGroup ? target.id : target.type == type ? target.groupId : -1;
for (auto& item : moved)
if (item.type == trackType && !groupIds.contains(item.groupId)) item.groupId = targetGroupId;
children.insert(children.begin() + std::clamp(targetIndex, 0, (int)children.size()), moved.begin(), moved.end());
return {};
}
Uids animation_length_fit(Anm2& anm2, int animationIndex)
{
if (auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex))
animation->frameNum = animation_length_get(*animation);
return {};
}
}