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

+368 -128
View File
@@ -1,182 +1,422 @@
#include "edit.hpp"
#include "vector.hpp"
#include "model/xml.hpp"
using namespace anm2ed::model;
namespace anm2ed::edit
{
std::set<int> animation_group_ids_get(const Element& animations)
constexpr std::string_view ANIMATION_MERGED_SUFFIX = " (Merged)";
using AnimationPointer = std::shared_ptr<const Animation>;
std::set<int> animation_group_ids_get(const Model& model)
{
std::set<int> result{};
for (const auto& item : animations.children)
if (item.type == ElementType::GROUP) result.insert(item.id);
return result;
std::set<int> ids{};
for (const auto& entry : model.animations.entries)
if (auto group = std::get_if<AnimationGroup>(&entry)) ids.insert(group->id);
return ids;
}
std::set<int> animation_group_indices_get(const Element& animations, int groupId)
std::set<int> animation_group_indices_get(const Model& model, 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;
std::set<int> indices{};
for (int i = 0; i < model.animations_count_get(); ++i)
if (model.animation_group_id_get(i) == groupId) indices.insert(i);
return indices;
}
// An empty copy of a track container: same tracks and groups, no frames.
Element track_container_shell_copy(const Element* source, const TrackContainer& row)
// Animations and whole groups taken out of the list, remembering the order they had.
struct AnimationsTaken
{
auto destination = element_make(row.container);
if (!source) return destination;
std::vector<AnimationPointer> animations{};
std::vector<AnimationGroup> groups{};
};
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);
};
AnimationsTaken animations_take(Model& model, const std::set<int>& indices, const std::set<int>& groupIds)
{
AnimationsTaken taken{};
int index{};
std::erase_if(model.animations.entries,
[&](AnimationEntry& entry)
{
if (auto animation = std::get_if<AnimationPointer>(&entry))
{
if (!indices.contains(index++)) return false;
taken.animations.push_back(std::move(*animation));
return true;
}
auto& group = std::get<AnimationGroup>(entry);
if (groupIds.contains(group.id))
{
index += (int)group.animations.size();
taken.groups.push_back(std::move(group));
return true;
}
std::erase_if(group.animations,
[&](AnimationPointer& animation)
{
if (!indices.contains(index++)) return false;
taken.animations.push_back(std::move(animation));
return true;
});
return false;
});
return taken;
}
for (const auto& sourceItem : source->children)
// Where an animation with this uid sits: its top-level entry, and its group (or nullptr) and position there.
struct AnimationPlace
{
int entry{-1};
AnimationGroup* group{};
int position{-1};
};
AnimationPlace animation_place_get(Model& model, std::uint64_t uid)
{
auto& entries = model.animations.entries;
for (int i = 0; i < (int)entries.size(); ++i)
{
if (sourceItem.type != ElementType::GROUP)
if (auto animation = std::get_if<AnimationPointer>(&entries[i]))
{
track_push(sourceItem, -1);
if ((*animation)->uid == uid) return {i, nullptr, -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);
auto& group = std::get<AnimationGroup>(entries[i]);
for (int j = 0; j < (int)group.animations.size(); ++j)
if (group.animations[j]->uid == uid) return {i, &group, j};
}
return destination;
return {};
}
int animation_group_entry_get(const Model& model, int groupId)
{
auto& entries = model.animations.entries;
for (int i = 0; i < (int)entries.size(); ++i)
if (auto group = std::get_if<AnimationGroup>(&entries[i]); group && group->id == groupId) return i;
return -1;
}
AnimationGroup* animation_group_find(Model& model, int groupId)
{
auto entry = animation_group_entry_get(model, groupId);
return entry == -1 ? nullptr : &std::get<AnimationGroup>(model.animations.entries[entry]);
}
// Inserts animations before the animation at a flat index (or at the end), inside `groupId` when given.
void animations_insert(Model& model, int index, int groupId, std::vector<AnimationPointer> animations)
{
auto before = model.animation_get(index);
auto place = before ? animation_place_get(model, before->uid) : AnimationPlace{};
if (auto group = animation_group_find(model, groupId))
{
auto position = place.group == group ? place.position : (int)group->animations.size();
group->animations.insert(group->animations.begin() + position, animations.begin(), animations.end());
return;
}
auto& entries = model.animations.entries;
auto position = place.entry == -1 ? (int)entries.size() : place.entry;
entries.insert(entries.begin() + position, animations.begin(), animations.end());
}
void animation_groups_remove(Model& model, const std::set<int>& groupIds)
{
auto& entries = model.animations.entries;
for (int i = 0; i < (int)entries.size(); ++i)
{
auto group = std::get_if<AnimationGroup>(&entries[i]);
if (!group || !groupIds.contains(group->id)) continue;
auto animations = std::move(group->animations);
entries.erase(entries.begin() + i);
entries.insert(entries.begin() + i, animations.begin(), animations.end());
i += (int)animations.size() - 1;
}
}
// An empty copy of a track list: same tracks and groups, no frames.
std::vector<TrackEntry> track_entries_shell_get(const std::vector<TrackEntry>& entries)
{
std::vector<TrackEntry> shell{};
auto track_shell_get = [](const Track& track)
{ return Track{.type = track.type, .id = track.id, .isVisible = track.isVisible}; };
for (const auto& entry : entries)
if (auto track = std::get_if<Track>(&entry))
shell.emplace_back(track_shell_get(*track));
else
{
const auto& group = std::get<TrackGroup>(entry);
TrackGroup copy{
.id = group.id, .name = group.name, .isExpanded = group.isExpanded, .isVisible = group.isVisible};
for (const auto& groupTrack : group.tracks)
copy.tracks.push_back(track_shell_get(groupTrack));
shell.emplace_back(std::move(copy));
}
return shell;
}
// 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)
Uids animation_add(Model& model, 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};
Animation animation{.name = name};
if (auto source = model.animation_get(templateIndex))
{
animation.layers = track_entries_shell_get(source->layers);
animation.nulls = track_entries_shell_get(source->nulls);
}
if (model.animations_count_get() == 0) model.animations.defaultAnimation = animation.name;
auto uid = animation.uid;
animations_insert(model, index, groupId, {std::make_shared<const Animation>(std::move(animation))});
return {uid};
}
Uids animations_remove(Anm2& anm2, const std::set<int>& indices, const std::set<int>& groupIds)
Uids animations_remove(Model& model, 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);
animations_take(model, indices, {});
animation_groups_remove(model, groupIds);
return {};
}
void animation_groups_remove(Element& animations, const std::set<int>& groupIds)
// Moves animations and whole groups beside a target animation or group; animations dropped on their own join the
// target's group (or the group dropped into).
Uids animations_move(Model& model, const std::set<int>& indices, const std::set<int>& groupIds,
AnimationTarget target)
{
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); });
}
auto targetAnimation = model.animation_get(target.animationIndex);
auto targetUid = targetAnimation ? targetAnimation->uid : 0;
if (targetAnimation && groupIds.contains(model.animation_group_id_get(target.animationIndex))) return {};
if (targetAnimation && indices.contains(target.animationIndex)) return {};
if (!targetAnimation && groupIds.contains(target.groupId)) return {};
// 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;
auto taken = animations_take(model, indices, groupIds);
if (taken.animations.empty() && taken.groups.empty()) return {};
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);
}
for (const auto& animation : taken.animations)
uids.push_back(animation->uid);
util::vector::move_indices_to_position(animations->children, childIndices, targetChildIndex);
auto& entries = model.animations.entries;
auto topIndex = (int)entries.size();
if (targetUid)
{
auto place = animation_place_get(model, targetUid);
if (place.group)
place.group->animations.insert(place.group->animations.begin() + place.position + target.isAfter,
taken.animations.begin(), taken.animations.end());
else if (place.entry != -1)
entries.insert(entries.begin() + place.entry + target.isAfter, taken.animations.begin(),
taken.animations.end());
place = animation_place_get(model, targetUid);
if (place.entry != -1) topIndex = place.entry + (target.isAfter || place.group);
}
else if (auto entry = animation_group_entry_get(model, target.groupId); entry != -1)
{
auto& group = std::get<AnimationGroup>(entries[entry]);
if (target.isInto)
group.animations.insert(group.animations.end(), taken.animations.begin(), taken.animations.end());
else
entries.insert(entries.begin() + entry + target.isAfter, taken.animations.begin(), taken.animations.end());
if (!target.isInto && !target.isAfter) entry += (int)taken.animations.size();
topIndex = entry + (target.isAfter || target.isInto);
}
else
entries.insert(entries.end(), taken.animations.begin(), taken.animations.end());
entries.insert(entries.begin() + std::clamp(topIndex, 0, (int)entries.size()),
std::make_move_iterator(taken.groups.begin()), std::make_move_iterator(taken.groups.end()));
return uids;
}
Uids animations_group(Anm2& anm2, const std::set<int>& indices, const std::string& name)
// Wraps ungrouped animations in a new group placed where the first of them was.
Uids animations_group(Model& model, 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);
if (indices.empty()) return {};
auto first = model.animation_get(*indices.begin());
if (!first) return {};
auto firstUid = first->uid;
auto entry = animation_place_get(model, firstUid).entry;
auto groupIds = animation_group_ids_get(model);
auto taken = animations_take(model, indices, {});
AnimationGroup group{
.id = groupIds.empty() ? 0 : *groupIds.rbegin() + 1, .name = name, .animations = std::move(taken.animations)};
auto& entries = model.animations.entries;
entries.insert(entries.begin() + std::clamp(entry, 0, (int)entries.size()), std::move(group));
return {};
}
Uids animations_paste(Anm2& anm2, const std::string& text, int start, int targetGroupId, std::set<int>& groupIds,
// Pastes clipboard animations (and groups, which get new ids) before the animation at `start`; lone animations join
// `targetGroupId` when no group came with them.
Uids animations_paste(Model& model, 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();
auto entries = animations_from_string(text, errorString);
if (entries.empty()) return {};
auto existingGroupIds = animation_group_ids_get(model);
auto nextGroupId = existingGroupIds.empty() ? 0 : *existingGroupIds.rbegin() + 1;
Uids uids{};
for (auto index : indices)
if (auto animation = anm2.element_get(ElementType::ANIMATION, index))
std::vector<AnimationPointer> loose{};
std::vector<AnimationEntry> placed{};
for (auto& entry : entries)
if (auto animation = std::get_if<AnimationPointer>(&entry))
{
if (targetGroupId != -1 && groupIds.empty()) animation->groupId = targetGroupId;
uids.push_back(animation->uid);
uids.push_back((*animation)->uid);
loose.push_back(*animation);
placed.push_back(entry);
}
else
{
auto& group = std::get<AnimationGroup>(entry);
group.id = nextGroupId++;
groupIds.insert(group.id);
for (const auto& animation : group.animations)
uids.push_back(animation->uid);
placed.push_back(std::move(entry));
}
if (groupIds.empty() && targetGroupId != -1 && animation_group_find(model, targetGroupId))
{
animations_insert(model, start, targetGroupId, std::move(loose));
return uids;
}
auto before = model.animation_get(start);
auto position = before ? animation_place_get(model, before->uid).entry : (int)model.animations.entries.size();
model.animations.entries.insert(model.animations.entries.begin() + position,
std::make_move_iterator(placed.begin()), std::make_move_iterator(placed.end()));
return uids;
}
Uids animations_merge(Anm2& anm2, int target, std::set<int> sources, types::merge::Type type, bool isDeleteAfter,
void track_merge(Track& destination, const Track& source, types::merge::Type type)
{
std::vector<Frame> frames{};
for (const auto& frame : source.frames)
frames.push_back(frame_clone(frame));
switch (type)
{
case types::merge::APPEND:
destination.frames.insert(destination.frames.end(), frames.begin(), frames.end());
break;
case types::merge::PREPEND:
destination.frames.insert(destination.frames.begin(), frames.begin(), frames.end());
break;
case types::merge::REPLACE:
if (destination.frames.size() < frames.size()) destination.frames.resize(frames.size());
std::ranges::copy(frames, destination.frames.begin());
break;
default:
break;
}
}
// Merges a source animation's layer or null tracks: groups match by name (else are added), tracks match by id
// within their group (else are added).
void track_entries_merge(std::vector<TrackEntry>& destination, const std::vector<TrackEntry>& source,
types::merge::Type type)
{
std::map<int, int> groupRemap{};
std::set<int> matchedGroupIds{};
int nextGroupId{};
groups_each(destination, [&](const TrackGroup& group) { nextGroupId = std::max(nextGroupId, group.id + 1); });
groups_each(source,
[&](const TrackGroup& sourceGroup)
{
TrackGroup* matched{};
groups_each(destination,
[&](TrackGroup& group)
{
if (!matched && group.name == sourceGroup.name && !matchedGroupIds.contains(group.id))
matched = &group;
});
if (matched)
track_merge(matched->root, sourceGroup.root, type);
else
{
TrackGroup group{.id = nextGroupId++,
.name = sourceGroup.name,
.isExpanded = sourceGroup.isExpanded,
.isVisible = sourceGroup.isVisible,
.root = track_clone(sourceGroup.root),
.extras = sourceGroup.extras};
destination.emplace_back(std::move(group));
matched = &std::get<TrackGroup>(destination.back());
}
groupRemap[sourceGroup.id] = matched->id;
matchedGroupIds.insert(matched->id);
});
tracks_each(source,
[&](const Track& sourceTrack, const TrackGroup* sourceGroup)
{
TrackGroup* group{};
if (sourceGroup)
groups_each(destination,
[&](TrackGroup& candidate)
{
if (candidate.id == groupRemap[sourceGroup->id]) group = &candidate;
});
Track* existing{};
if (group)
{
for (auto& track : group->tracks)
if (track.id == sourceTrack.id) existing = &track;
}
else
for (auto& entry : destination)
if (auto track = std::get_if<Track>(&entry); track && track->id == sourceTrack.id)
existing = track;
if (existing)
track_merge(*existing, sourceTrack, type);
else if (group)
group->tracks.push_back(track_clone(sourceTrack));
else
destination.emplace_back(track_clone(sourceTrack));
});
}
// Merges source animations into the target (frames appended, prepended, replacing or ignored), optionally deleting
// the sources; `ungroupIds` dissolves groups afterwards. Returns the merged animation's uid.
Uids animations_merge(Model& model, 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{};
if (!model.animation_get(target)) return {};
auto isMerge = sources.size() > 1 || !sources.contains(target);
if (isMerge)
{
auto& merged = *model.animation_edit(target);
if (!merged.name.ends_with(ANIMATION_MERGED_SUFFIX)) merged.name += std::string(ANIMATION_MERGED_SUFFIX);
for (auto index : sources)
{
if (index == target) continue;
auto source = model.animation_get(index);
if (!source) continue;
auto sourceCopy = *source;
auto& destination = *model.animation_edit(target);
track_merge(destination.root, sourceCopy.root, type);
track_entries_merge(destination.layers, sourceCopy.layers, type);
track_entries_merge(destination.nulls, sourceCopy.nulls, type);
track_merge(destination.triggers, sourceCopy.triggers, type);
}
auto targetUid = model.animation_get(target)->uid;
if (isDeleteAfter)
{
sources.erase(target);
animations_take(model, sources, {});
}
for (int i = 0; i < model.animations_count_get(); ++i)
if (model.animation_get(i)->uid == targetUid)
{
auto& finalAnimation = *model.animation_edit(i);
finalAnimation.frameNum = animation_length_get(finalAnimation);
target = i;
}
}
auto uid = model.animation_get(target)->uid;
animation_groups_remove(model, ungroupIds);
return {uid};
}
}
+386 -73
View File
@@ -1,94 +1,187 @@
#include "edit.hpp"
#include "vector.hpp"
#include <format>
#include <unordered_map>
namespace anm2ed
{
std::string region_name_get(const std::string&, int);
}
#include "model/frames.hpp"
#include "model/xml.hpp"
using namespace anm2ed::model;
namespace anm2ed::edit
{
// Clears frame references to the spritesheet's unused regions, then removes them.
Uids regions_remove_unused(Anm2& anm2, int spritesheetId)
std::string region_name_get(const std::string& format, int number)
{
auto unused = anm2.region_unused(spritesheetId);
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
if (unused.empty() || !spritesheet) return {};
try
{
return std::vformat(format, std::make_format_args(number));
}
catch (const std::format_error&)
{
return format;
}
}
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);
bool is_region_matched(const Region& region, const Frame& frame)
{
return glm::ivec2(region.crop) == glm::ivec2(frame.crop) && glm::ivec2(region.size) == glm::ivec2(frame.size) &&
glm::ivec2(region.pivot) == glm::ivec2(frame.pivot);
}
// Every frame of every layer track whose layer has a spritesheet, with that spritesheet's id.
template <class Callback> void layer_frames_each(Model& model, int animationIndex, Callback&& callback)
{
auto animation = model.animation_get(animationIndex);
if (!animation) return;
std::vector<std::pair<int, int>> tracks{};
tracks_each(animation->layers,
[&](const Track& track, const TrackGroup* group)
{
if (model.layer_spritesheet_get(track.id)) tracks.emplace_back(track.id, group ? group->id : -1);
});
for (auto [id, groupId] : tracks)
{
auto spritesheetId = model.layer_spritesheet_get(id)->id;
auto track = model.track_edit({animationIndex, LAYER, id, -1, groupId == -1 ? NONE : LAYER, groupId});
for (auto& frame : track->frames)
callback(frame, spritesheetId);
}
}
// Makes a region for each distinct crop/size/pivot among the frames (and points frames at them when mapping is SET).
Uids regions_generate(Model& model, const std::set<int>& animationIndices, const std::set<Reference>& frames,
const std::string& format, RegionFrameMapping mapping)
{
std::unordered_map<int, int> regionNumbers{};
auto frame_apply = [&](Frame& frame, int spritesheetId)
{
auto& regions = item_get(model.content.spritesheets, spritesheetId)->regions;
auto region =
std::ranges::find_if(regions, [&](const Region& region) { return is_region_matched(region, frame); });
if (region == regions.end())
{
regions.push_back({.id = item_next_id_get(regions),
.name = region_name_get(format, ++regionNumbers[spritesheetId]),
.crop = frame.crop,
.size = frame.size,
.pivot = frame.pivot});
region = regions.end() - 1;
}
if (mapping == RegionFrameMapping::SET) frame.regionId = region->id;
};
for (auto index : animationIndices)
layer_frames_each(model, index, frame_apply);
for (auto reference : frames)
{
auto spritesheet = reference.itemType == LAYER ? model.layer_spritesheet_get(reference.itemID) : nullptr;
auto source = model.frame_get(reference);
if (spritesheet && source && source->regionId == -1) frame_apply(*model.frame_edit(reference), spritesheet->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)
// Points frames without a region at a region of their spritesheet with the same crop/size/pivot.
Uids regions_scan(Model& model)
{
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 {};
for (int i = 0; i < model.animations_count_get(); ++i)
layer_frames_each(model, i,
[&](Frame& frame, int spritesheetId)
{
if (frame.regionId != -1) return;
for (const auto& region : item_get(model.content.spritesheets, spritesheetId)->regions)
if (is_region_matched(region, frame))
{
frame.regionId = region.id;
break;
}
});
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);
// Copies each layer frame's region crop/size/pivot onto it; frames pointing at a missing region lose it.
Uids region_frames_sync(Model& model)
{
for (int i = 0; i < model.animations_count_get(); ++i)
layer_frames_each(model, i,
[&](Frame& frame, int spritesheetId)
{
if (frame.regionId == -1) return;
auto region =
item_get(item_get(model.content.spritesheets, spritesheetId)->regions, frame.regionId);
if (!region)
{
frame.regionId = -1;
return;
}
frame.crop = region->crop;
frame.size = region->size;
frame.pivot = region->pivot;
});
return {};
}
// Clears frame references to the spritesheet's unused regions, then removes them.
Uids regions_remove_unused(Model& model, int spritesheetId)
{
auto unused = model.region_unused_get(spritesheetId);
auto spritesheet = item_get(model.content.spritesheets, spritesheetId);
if (unused.empty() || !spritesheet) return {};
std::erase_if(spritesheet->regions, [&](const Region& region) { return unused.contains(region.id); });
return {};
}
// Pastes regions into a spritesheet at a position; they get fresh ids after the existing ones.
Uids regions_paste(Model& model, int spritesheetId, const std::string& text, int insertIndex,
std::string* errorString)
{
auto spritesheet = item_get(model.content.spritesheets, spritesheetId);
if (!spritesheet) return {};
auto regions = items_from_string<Region>(text, errorString);
for (auto& region : regions)
region.id = item_next_id_get(spritesheet->regions) + (int)(&region - regions.data());
insertIndex = std::clamp(insertIndex, 0, (int)spritesheet->regions.size());
spritesheet->regions.insert(spritesheet->regions.begin() + insertIndex, regions.begin(), regions.end());
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 animation_grid_generate(Model& model, 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,
if (!model.track_get(reference)) continue;
auto spritesheetId =
model.layer_spritesheet_get(reference.itemID) ? model.layer_spritesheet_get(reference.itemID)->id : -1;
auto& track = *model.track_edit(reference);
auto start = (int)track.frames.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 spritesheet = item_get(model.content.spritesheets, spritesheetId);
for (int i = start; i < (int)track.frames.size(); ++i)
{
auto& frame = track->children[i];
if (frame.type != ElementType::FRAME) continue;
auto& frame = track.frames[i];
uids.push_back(frame.uid);
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;
auto& region =
spritesheet->regions.emplace_back(Region{.id = item_next_id_get(spritesheet->regions),
.name = region_name_get(options.regionNameFormat, i - start),
.crop = frame.crop,
.size = frame.size,
.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);
auto& animation = *model.animation_edit(reference.animationIndex);
animation.frameNum = animation_length_get(animation);
}
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)
Uids frames_change_apply(Model& model, const ChangeTargets& targets, FrameChange change, ChangeType type)
{
std::map<Reference, std::set<int>> frames{};
std::set<Reference> wholeTracks{};
@@ -98,30 +191,250 @@ namespace anm2ed::edit
index < 0 ? (void)wholeTracks.insert(reference) : (void)frames[reference].insert(index);
}
auto whole_apply = [&](Element* track, ItemType itemType)
auto whole_apply = [&](Track& track)
{
if (!track) return;
std::set<int> indices{};
for (int i = 0; i < track_frames_count_get(*track); ++i)
for (int i = 0; i < (int)track.frames.size(); ++i)
indices.insert(i);
frames_change(*track, change, itemType, type, indices);
frames_change(track, change, track.type, type, indices);
};
for (auto& [reference, indices] : frames)
if (auto track = anm2.element_get(reference))
frames_change(*track, change, (ItemType)reference.itemType, type, indices);
if (auto track = model.track_edit(reference)) frames_change(*track, change, track->type, type, indices);
for (auto reference : wholeTracks)
whole_apply(anm2.element_get(reference), (ItemType)reference.itemType);
for (auto animationIndex : targets.animations)
if (auto track = model.track_edit(reference)) whole_apply(*track);
for (auto index : targets.animations)
{
auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex);
auto animation = model.animation_edit(index);
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); });
if (targets.isRoot) whole_apply(animation->root);
if (targets.isLayers) tracks_each(animation->layers, [&](Track& track, auto*) { whole_apply(track); });
if (targets.isNulls) tracks_each(animation->nulls, [&](Track& track, auto*) { whole_apply(track); });
}
return {};
}
// Merges another anm2 file: content matched by name (or path for sounds) is replaced unless appending as new,
// animations are added, replaced, or merged track by track depending on the preset.
bool file_merge(Model& model, const std::filesystem::path& path, const std::filesystem::path& directory,
FileMergePreset preset)
{
Model source{};
if (!model_load(source, path) || !source.isValid) return false;
auto isAppendAsNew = preset == FILE_MERGE_PRESET_APPEND_AS_NEW;
std::map<ElementType, std::unordered_map<int, int>> remaps{};
auto& content = model.content;
auto path_remap = [&](const std::filesystem::path& original)
{
if (directory.empty() || original.empty()) return original;
std::error_code ec{};
auto absolute =
original.is_absolute() ? original : std::filesystem::weakly_canonical(path.parent_path() / original, ec);
if (ec) absolute = path.parent_path() / original;
auto relative = std::filesystem::relative(absolute, directory, ec);
return ec ? original : relative;
};
auto id_remap = [&](ElementType type, int id)
{
auto it = remaps[type].find(id);
return it == remaps[type].end() ? -1 : it->second;
};
auto name_find = [](auto& items, const std::string& name)
{
auto it = std::ranges::find_if(items, [&](const auto& item) { return item.name == name; });
return it == items.end() ? nullptr : &*it;
};
auto name_unique_get = [&](auto& items, const std::string& name)
{
auto candidate = name;
for (int i = 2; name_find(items, candidate); ++i)
candidate = std::format("{} {}", name, i);
return candidate;
};
auto named_merge = [&](auto& items, auto item)
{
auto existing = isAppendAsNew ? nullptr : name_find(items, item.name);
if (existing)
{
item.id = existing->id;
item.uid = existing->uid;
*existing = item;
return item.id;
}
item.id = item_next_id_get(items);
if (isAppendAsNew) item.name = name_unique_get(items, item.name);
items.push_back(item);
return item.id;
};
auto spritesheet_import = [&](int sourceId)
{
auto& remap = remaps[ElementType::SPRITESHEET];
if (remap.contains(sourceId)) return remap[sourceId];
auto spritesheet = item_get(source.content.spritesheets, sourceId);
if (sourceId < 0 || !spritesheet) return -1;
auto imported = *spritesheet;
imported.id = item_next_id_get(content.spritesheets);
imported.path = path_remap(imported.path);
content.spritesheets.push_back(imported);
return remap[sourceId] = imported.id;
};
for (auto sound : source.content.sounds)
{
auto sourceId = sound.id;
sound.path = path_remap(sound.path);
auto existing = isAppendAsNew ? content.sounds.end()
: std::ranges::find_if(content.sounds, [&](const Sound& other)
{ return other.path == sound.path; });
sound.id = existing != content.sounds.end() ? existing->id : item_next_id_get(content.sounds);
if (existing != content.sounds.end())
*existing = sound;
else
content.sounds.push_back(sound);
remaps[ElementType::SOUND_ELEMENT][sourceId] = sound.id;
}
for (auto layer : source.content.layers)
{
auto existing = isAppendAsNew ? nullptr : name_find(content.layers, layer.name);
auto sourceId = layer.id;
layer.spritesheetId = existing ? existing->spritesheetId : spritesheet_import(layer.spritesheetId);
remaps[ElementType::LAYER_ELEMENT][sourceId] = named_merge(content.layers, layer);
}
for (auto null : source.content.nulls)
remaps[ElementType::NULL_ELEMENT][null.id] = named_merge(content.nulls, null);
for (auto event : source.content.events)
remaps[ElementType::EVENT_ELEMENT][event.id] = named_merge(content.events, event);
auto track_remap = [&](Track& track)
{
for (auto& frame : track.frames)
{
for (auto& soundId : frame.soundIds)
soundId = id_remap(ElementType::SOUND_ELEMENT, soundId);
frame.eventId = id_remap(ElementType::EVENT_ELEMENT, frame.eventId);
}
};
auto entries_remap = [&](std::vector<TrackEntry>& entries, ElementType type)
{
std::vector<TrackEntry> result{};
auto track_keep = [&](Track& track)
{
track.id = id_remap(type, track.id);
if (track.id < 0) return false;
track_remap(track);
return true;
};
for (auto& entry : entries)
if (auto track = std::get_if<Track>(&entry))
{
if (track_keep(*track)) result.push_back(std::move(entry));
}
else
{
auto& group = std::get<TrackGroup>(entry);
std::erase_if(group.tracks, [&](Track& groupTrack) { return !track_keep(groupTrack); });
result.push_back(std::move(entry));
}
entries = std::move(result);
};
auto animation_build = [&](const Animation& incoming)
{
auto animation = animation_clone(incoming);
animation.extras = animation.layersExtras = animation.nullsExtras = {};
track_remap(animation.root);
track_remap(animation.triggers);
entries_remap(animation.layers, ElementType::LAYER_ELEMENT);
entries_remap(animation.nulls, ElementType::NULL_ELEMENT);
return animation;
};
auto animation_find = [&](const std::string& name) -> int
{
for (int i = 0; i < model.animations_count_get(); ++i)
if (model.animation_get(i)->name == name) return i;
return -1;
};
std::string defaultAnimationName{};
for (auto [index, incoming] : source.animations_get())
{
auto processed = animation_build(*incoming);
auto destination = animation_find(processed.name);
if (isAppendAsNew)
{
auto candidate = processed.name;
for (int i = 2; animation_find(candidate) != -1; ++i)
candidate = std::format("{} {}", processed.name, i);
processed.name = candidate;
}
if (incoming->name == source.animations.defaultAnimation) defaultAnimationName = processed.name;
if (destination == -1 || isAppendAsNew)
{
model.animations.entries.emplace_back(std::make_shared<const Animation>(std::move(processed)));
continue;
}
auto& target = *model.animation_edit(destination);
if (preset == FILE_MERGE_PRESET_REPLACE_MATCHING)
{
processed.uid = target.uid;
target = std::move(processed);
continue;
}
target.isLoop = processed.isLoop;
if (!processed.root.frames.empty()) target.root = processed.root;
if (!processed.triggers.frames.empty()) target.triggers = processed.triggers;
for (auto [targetEntries, sourceEntries] :
{std::pair{&target.layers, &processed.layers}, std::pair{&target.nulls, &processed.nulls}})
{
auto nextGroupId = 0;
groups_each(*targetEntries,
[&](const TrackGroup& group) { nextGroupId = std::max(nextGroupId, group.id + 1); });
for (auto& entry : *sourceEntries)
{
if (auto group = std::get_if<TrackGroup>(&entry))
{
auto copy = *group;
copy.id = nextGroupId++;
std::erase_if(copy.tracks,
[&](const Track& track)
{
auto existing = animation_track_get(target, (int)track.type, track.id);
if (existing && !track.frames.empty()) *existing = track;
return existing != nullptr;
});
targetEntries->push_back(std::move(copy));
continue;
}
auto& track = std::get<Track>(entry);
if (auto existing = animation_track_get(target, (int)track.type, track.id))
{
if (!track.frames.empty()) *existing = track;
}
else
targetEntries->push_back(track);
}
}
target.frameNum = std::max({target.frameNum, processed.frameNum, animation_length_get(target)});
}
if (model.animations.defaultAnimation.empty() && !source.animations.defaultAnimation.empty())
model.animations.defaultAnimation =
defaultAnimationName.empty() ? source.animations.defaultAnimation : defaultAnimationName;
return true;
}
}
+47 -32
View File
@@ -6,7 +6,7 @@
#include <string>
#include <vector>
#include "anm2/anm2.hpp"
#include "model/model.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.
@@ -51,39 +51,54 @@ namespace anm2ed::edit
bool isNulls{};
};
Element element_clone(Element);
Element* track_get(Anm2&, Reference);
struct AnimationTarget
{
int animationIndex{-1};
int groupId{-1};
bool isAfter{};
bool isInto{};
};
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 frame_insert(model::Model&, Reference, int);
Uids frames_delete(model::Model&, const std::set<Reference>&);
Uids frames_duplicate(model::Model&, const std::set<Reference>&, Reference);
Uids frames_reverse(model::Model&, const std::set<Reference>&);
Uids frames_bake(model::Model&, const std::set<Reference>&, int, bool, bool);
Uids frame_split(model::Model&, Reference, int);
Uids frames_move(model::Model&, const std::set<Reference>&, Reference, int);
Uids frame_durations_set(model::Model&, const std::map<Reference, int>&);
Uids trigger_at_frame_set(model::Model&, Reference, int);
Uids triggers_sort(model::Model&, Reference);
Uids frames_paste(model::Model&, Reference, const std::set<Reference>&, const std::string&, int, std::string*);
Uids root_bake_into(model::Model&, const std::set<Reference>&, const std::set<Reference>&, RootBakeOptions);
void frame_root_transform_apply(model::Frame&, const model::Frame&, 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);
Uids items_remove(model::Model&, int, const std::map<int, std::set<int>>&, const std::map<int, std::set<int>>&);
Uids items_group(model::Model&, int, int, const std::set<int>&, const std::string&);
Uids items_move(model::Model&, int, int, const std::set<int>&, const std::set<int>&, RowTarget, bool, bool);
Uids animation_length_fit(model::Model&, int);
int item_add(model::Model&, ItemType, int, int, const std::string&, int, bool, int, types::destination::Type);
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>&);
std::set<int> animation_group_ids_get(const model::Model&);
std::set<int> animation_group_indices_get(const model::Model&, int);
void animation_groups_remove(model::Model&, const std::set<int>&);
Uids animation_add(model::Model&, int, int, int, const std::string&);
Uids animations_remove(model::Model&, const std::set<int>&, const std::set<int>&);
Uids animations_move(model::Model&, const std::set<int>&, const std::set<int>&, AnimationTarget);
Uids animations_group(model::Model&, const std::set<int>&, const std::string&);
Uids animations_paste(model::Model&, const std::string&, int, int, std::set<int>&, std::string*);
Uids animations_merge(model::Model&, int, std::set<int>, types::merge::Type, bool, const std::set<int>&);
std::string region_name_get(const std::string&, int);
Uids regions_generate(model::Model&, const std::set<int>&, const std::set<Reference>&, const std::string&,
RegionFrameMapping);
Uids regions_scan(model::Model&);
Uids region_frames_sync(model::Model&);
Uids regions_remove_unused(model::Model&, int);
Uids regions_paste(model::Model&, int, const std::string&, int, std::string*);
Uids animation_grid_generate(model::Model&, const std::vector<Reference>&, const GridOptions&);
Uids frames_change_apply(model::Model&, const ChangeTargets&, FrameChange, ChangeType);
bool file_merge(model::Model&, const std::filesystem::path&, const std::filesystem::path&, FileMergePreset);
}
+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;
}
}
+160 -73
View File
@@ -1,118 +1,205 @@
#include "edit.hpp"
using namespace anm2ed::model;
namespace anm2ed::edit
{
Element* track_container_get(Anm2& anm2, int animationIndex, int type)
std::vector<TrackEntry>* track_entries_edit(Model& model, int animationIndex, int type)
{
auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex);
return animation ? child_first_get(*animation, TYPE_CONTAINERS[type]) : nullptr;
if (type != LAYER && type != NULL_) return nullptr;
auto animation = model.animation_edit(animationIndex);
return animation ? &animation_entries_get(*animation, type) : nullptr;
}
Uids items_remove(Anm2& anm2, int animationIndex, const std::map<int, std::set<int>>& ids,
Uids items_remove(Model& model, 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 (typeIds.empty() && typeGroupIds.empty()) continue;
auto entries = track_entries_edit(model, animationIndex, type);
if (!entries) continue;
std::erase_if(*entries,
[&](TrackEntry& entry)
{
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));
if (auto group = std::get_if<TrackGroup>(&entry))
{
std::erase_if(group->tracks, [&](const Track& track) { return typeIds.contains(track.id); });
return typeGroupIds.contains(group->id);
}
return typeIds.contains(std::get<Track>(entry).id);
});
}
return {};
}
Uids items_group(Anm2& anm2, int animationIndex, int type, const std::set<int>& ids, const std::string& name)
// Wraps ungrouped tracks in a new group placed where the first of them was.
Uids items_group(Model& model, 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 entries = track_entries_edit(model, animationIndex, type);
if (!entries) 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()));
int nextId{};
groups_each(*entries, [&](const TrackGroup& group) { nextId = std::max(nextId, group.id + 1); });
TrackGroup group{.id = nextId, .name = name};
auto insertIndex = (int)container->children.size();
for (int i = 0; i < (int)container->children.size(); ++i)
auto insertIndex = -1;
for (int i = 0; i < (int)entries->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;
auto track = std::get_if<Track>(&(*entries)[i]);
if (!track || !ids.contains(track->id)) continue;
if (insertIndex == -1) insertIndex = i;
group.tracks.push_back(std::move(*track));
entries->erase(entries->begin() + i--);
}
if (insertIndex == (int)container->children.size()) return {};
if (insertIndex == -1) return {};
container->children.insert(container->children.begin() + insertIndex, group);
return {group.uid};
auto uid = group.uid;
entries->insert(entries->begin() + insertIndex, std::move(group));
return {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,
// Moves tracks and whole groups next to (or into) a target row. Layers are listed bottom-up, so for them "after" on
// screen is "before" in the file.
Uids items_move(Model& model, 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 entries = track_entries_edit(model, animationIndex, type);
if (!entries) return {};
auto trackType = TYPE_TRACKS[type];
auto& children = container->children;
auto is_moved = [&](const Element& item)
std::vector<Track> movedTracks{};
std::vector<TrackEntry> movedGroups{};
std::erase_if(*entries,
[&](TrackEntry& entry)
{
if (auto group = std::get_if<TrackGroup>(&entry))
{
if (groupIds.contains(group->id))
{
movedGroups.push_back(std::move(entry));
return true;
}
std::erase_if(group->tracks,
[&](Track& track)
{
if (!ids.contains(track.id)) return false;
movedTracks.push_back(std::move(track));
return true;
});
return false;
}
auto& track = std::get<Track>(entry);
if (!ids.contains(track.id)) return false;
movedTracks.push_back(std::move(track));
return true;
});
if (movedTracks.empty() && movedGroups.empty()) return {};
auto isAfter = type == LAYER ? !isDropAfter : isDropAfter;
auto group_find = [&](int groupId)
{
return (item.type == ElementType::GROUP && groupIds.contains(item.id)) ||
(item.type == trackType && (ids.contains(track_id_get(item)) || groupIds.contains(item.groupId)));
return std::ranges::find_if(*entries,
[&](const TrackEntry& entry)
{
auto group = std::get_if<TrackGroup>(&entry);
return group && group->id == 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)
// Where top-level items go: beside the target row, or beside the group that holds it.
auto topIndex = (int)entries->size();
if (target.isGroup || target.groupId != -1)
{
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);
auto group = group_find(target.isGroup ? target.id : target.groupId);
if (group != entries->end()) topIndex = (int)(group - entries->begin()) + isAfter;
}
else if (target.type == type)
{
auto track = std::ranges::find_if(*entries,
[&](const TrackEntry& entry)
{
auto track = std::get_if<Track>(&entry);
return track && track->id == target.id;
});
if (track != entries->end()) topIndex = (int)(track - entries->begin()) + isAfter;
}
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 intoGroupId = target.isGroup && isDropIntoGroup ? target.id : !target.isGroup ? target.groupId : -1;
auto intoGroup = intoGroupId == -1 ? entries->end() : group_find(intoGroupId);
if (intoGroup != entries->end())
{
// Into a group header lands just under it on screen; beside a grouped track lands beside it.
auto& tracks = std::get<TrackGroup>(*intoGroup).tracks;
auto beside = std::ranges::find_if(tracks, [&](const Track& track) { return track.id == target.id; });
auto position = target.isGroup ? (type == LAYER ? tracks.end() : tracks.begin())
: beside != tracks.end() ? beside + isAfter
: tracks.end();
tracks.insert(position, std::make_move_iterator(movedTracks.begin()), std::make_move_iterator(movedTracks.end()));
movedTracks.clear();
}
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());
std::vector<TrackEntry> topMoved{};
for (auto& track : movedTracks)
topMoved.emplace_back(std::move(track));
topMoved.insert(topMoved.end(), std::make_move_iterator(movedGroups.begin()),
std::make_move_iterator(movedGroups.end()));
entries->insert(entries->begin() + std::clamp(topIndex, 0, (int)entries->size()),
std::make_move_iterator(topMoved.begin()), std::make_move_iterator(topMoved.end()));
return {};
}
Uids animation_length_fit(Anm2& anm2, int animationIndex)
Uids animation_length_fit(Model& model, int animationIndex)
{
if (auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex))
animation->frameNum = animation_length_get(*animation);
if (auto animation = model.animation_edit(animationIndex)) animation->frameNum = animation_length_get(*animation);
return {};
}
// Adds a layer or null (or updates the one with the item's id) and gives it a track in one or every animation.
int item_add(Model& model, ItemType type, int animationIndex, int id, const std::string& name, int spritesheetId,
bool isShowRect, int insertBeforeId, types::destination::Type destination)
{
auto& content = model.content;
if (type == ItemType::LAYER)
{
if (id == -1) id = item_next_id_get(content.layers);
auto layer = item_get(content.layers, id);
if (!layer) layer = &content.layers.emplace_back(Layer{.id = id});
if (!name.empty()) layer->name = name;
layer->spritesheetId = item_get(content.spritesheets, spritesheetId) ? spritesheetId : 0;
}
else if (type == ItemType::NULL_)
{
if (id == -1) id = item_next_id_get(content.nulls);
auto null = item_get(content.nulls, id);
if (!null) null = &content.nulls.emplace_back(Null{.id = id});
if (!name.empty()) null->name = name;
null->isShowRect = isShowRect;
}
else
return -1;
auto add = [&](int index)
{
if (model.track_get({index, (int)type, id})) return;
auto& entries = animation_entries_get(*model.animation_edit(index), (int)type);
auto before = std::ranges::find_if(entries,
[&](const TrackEntry& entry)
{
auto track = std::get_if<Track>(&entry);
return track && track->id == insertBeforeId;
});
entries.insert(insertBeforeId == -1 ? entries.end() : before, Track{.type = type, .id = id});
};
if (destination == types::destination::ALL)
for (int i = 0; i < model.animations_count_get(); ++i)
add(i);
else if (model.animation_get(animationIndex))
add(animationIndex);
return id;
}
}