Slim Document: move data-model code out, unify edit plumbing

- Region generate/scan move to anm2/regions.cpp; both generators number from 1
- file_merge moves to anm2/merge.cpp and is table-driven
- MaxRects packer moves to util/pack
- Uniform config apply/trim/save move to resource/shader
- Snapshots push from their owned state; Document snapshot wrappers removed
- DocumentData base replaces the hand-written move constructor
- toast_log replaces 55 duplicated toast + English log pairs
- Apply repo .clang-format to touched files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 13:08:00 -04:00
1 parent a9f48bc3a0
commit 3f31cedeed
32 files changed
+3405 -4428

No files matched your search

+22 -5
View File
@@ -1,5 +1,6 @@
#pragma once
#include <algorithm>
#include <cstdint>
#include <filesystem>
#include <optional>
@@ -8,7 +9,6 @@
#include <string_view>
#include <type_traits>
#include <vector>
#include <algorithm>
#include <glm/glm.hpp>
#include <tinyxml2/tinyxml2.h>
@@ -193,6 +193,20 @@ namespace anm2ed
#undef X
};
enum class RegionFrameMapping
{
PRESERVE,
SET
};
enum FileMergePreset
{
FILE_MERGE_PRESET_MERGE_BY_NAME,
FILE_MERGE_PRESET_APPEND_AS_NEW,
FILE_MERGE_PRESET_REPLACE_MATCHING,
FILE_MERGE_PRESET_COUNT
};
enum class ChangeType
{
ADJUST,
@@ -401,8 +415,8 @@ namespace anm2ed
template <class E> ElementPointer<E> shader_uniform_component_get(E& uniform, int index)
{
return child_find(
uniform, [&](const Element& child) { return child.type == ElementType::COMPONENT && child.index == index; });
return child_find(uniform, [&](const Element& child)
{ return child.type == ElementType::COMPONENT && child.index == index; });
}
template <class E> ElementPointer<E> element_first_get(E& element, ElementType type)
@@ -432,8 +446,7 @@ namespace anm2ed
}
template <class E>
ElementPointer<E> animation_item_get(E& animation, ItemType type, int id = -1, int groupType = NONE,
int groupId = -1)
ElementPointer<E> animation_item_get(E& animation, ItemType type, int id = -1, int groupType = NONE, int groupId = -1)
{
auto trackType = TYPE_TRACKS[(int)type];
auto isGrouped = groupType != NONE && groupId != -1;
@@ -495,6 +508,7 @@ namespace anm2ed
int element_child_max_id_get(const Element&, ElementType);
bool element_child_id_erase(Element&, ElementType, int);
int track_frames_count_get(const Element&);
int animations_count_get(const Element&);
float interpolation_factor(Interpolation, float);
void frame_mix(Element&, const Element&, float);
Element frame_generate(const Element&, float);
@@ -542,5 +556,8 @@ namespace anm2ed
bool animations_deserialize(const std::string&, int, std::set<int>&, std::string* = nullptr,
std::set<int>* = nullptr);
int animations_merge(int, std::set<int>&, types::merge::Type = types::merge::APPEND, bool = true);
bool file_merge(const std::filesystem::path&, const std::filesystem::path&, FileMergePreset);
bool regions_generate(const std::set<int>&, const std::set<Reference>&, const std::string&, RegionFrameMapping);
void regions_scan();
};
}
+29 -1
View File
@@ -61,7 +61,6 @@ namespace anm2ed
ElementType group_child_type_get(ElementType);
Element* content_container_get(Element&, ElementType);
std::unordered_map<int, int> child_ids_compact(Element&, ElementType);
int animations_count_get(const Element&);
int animations_child_index_get(const Element&, int);
int animations_child_insert_index_get(const Element&, int);
Element root_animation_make();
@@ -88,6 +87,35 @@ namespace anm2ed
void special_interpolated_frames_bake(Element&, int, bool, bool);
void all_interpolated_frames_bake(Element&, int, bool, bool);
int color_write(float);
bool is_region_matched(const Element&, const Element&);
Element* region_match_get(Element&, const Element&);
template <class Callback> void layer_frames_each(Element& root, Element& animation, Callback&& callback)
{
auto layers = content_container_get(root, ElementType::LAYERS);
auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS);
auto tracks = child_first_get(animation, ElementType::LAYER_ANIMATIONS);
if (!layers || !spritesheets || !tracks) return;
tracks_each(*tracks, ElementType::LAYER_ANIMATION,
[&](Element& track)
{
auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, track.layerId);
auto spritesheet =
layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
if (!spritesheet) return;
for (auto& frame : track.children)
if (frame.type == ElementType::FRAME) callback(frame, *spritesheet);
});
}
template <class Callback> void layer_frames_each(Element& root, Callback&& callback)
{
if (auto animations = element_first_get(root, ElementType::ANIMATIONS))
for (auto& animation : animations->children)
if (animation.type == ElementType::ANIMATION) layer_frames_each(root, animation, callback);
}
std::uint64_t anm2_hash_get(const Element&, Options);
std::uint64_t element_hash(const Element&, Flags);
}
+226 -1
View File
@@ -104,7 +104,8 @@ namespace anm2ed
[&](const Element& child)
{
return child.type == ElementType::GROUP &&
child.name == sourceGroup.name && !matchedGroupIds.contains(child.id);
child.name == sourceGroup.name &&
!matchedGroupIds.contains(child.id);
});
if (destinationGroup == destinationTracks->children.end())
@@ -182,4 +183,228 @@ namespace anm2ed
finalAnimation->frameNum = animation_length_get(*finalAnimation);
return finalIndex;
}
bool Anm2::file_merge(const std::filesystem::path& path, const std::filesystem::path& directory,
FileMergePreset preset)
{
Anm2 source(path);
if (!source.isValid) return false;
auto isAppendAsNew = preset == FILE_MERGE_PRESET_APPEND_AS_NEW;
std::map<ElementType, std::unordered_map<int, int>> remaps{};
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 = [](Element& container, ElementType type, const std::string& name)
{ return child_find(container, [&](const Element& child) { return child.type == type && child.name == name; }); };
auto name_unique_get = [&](Element& container, ElementType type, const std::string& name)
{
auto candidate = name;
for (int i = 2; name_find(container, type, candidate); ++i)
candidate = std::format("{} {}", name, i);
return candidate;
};
auto named_element_merge = [&](Element& container, Element item)
{
auto existing = isAppendAsNew ? nullptr : name_find(container, item.type, item.name);
if (existing)
{
item.id = existing->id;
*existing = item;
return item.id;
}
item.id = element_child_next_id_get(container, item.type);
if (isAppendAsNew) item.name = name_unique_get(container, item.type, item.name);
container.children.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 = source.element_get(ElementType::SPRITESHEET, sourceId);
auto destination = element_get(ElementType::SPRITESHEETS);
if (sourceId < 0 || !spritesheet || !destination) return -1;
auto imported = *spritesheet;
imported.id = element_child_next_id_get(*destination, ElementType::SPRITESHEET);
imported.path = path_remap(imported.path);
destination->children.push_back(imported);
return remap[sourceId] = imported.id;
};
auto sourceSounds = source.element_get(ElementType::SOUNDS);
auto destinationSounds = element_get(ElementType::SOUNDS);
if (sourceSounds && destinationSounds)
for (auto sound : sourceSounds->children)
{
if (sound.type != ElementType::SOUND_ELEMENT) continue;
auto sourceId = sound.id;
sound.path = path_remap(sound.path);
auto existing = isAppendAsNew ? nullptr
: child_find(*destinationSounds, [&](const Element& child)
{ return child.type == sound.type && child.path == sound.path; });
sound.id = existing ? existing->id : element_child_next_id_get(*destinationSounds, sound.type);
if (existing)
*existing = sound;
else
destinationSounds->children.push_back(sound);
remaps[sound.type][sourceId] = sound.id;
}
for (auto type : {ElementType::LAYER_ELEMENT, ElementType::NULL_ELEMENT, ElementType::EVENT_ELEMENT})
{
auto sourceContainer = source.element_get(ELEMENT_CONTAINERS[(int)type]);
auto destinationContainer = element_get(ELEMENT_CONTAINERS[(int)type]);
if (!sourceContainer || !destinationContainer) continue;
for (auto item : sourceContainer->children)
{
if (item.type != type) continue;
if (type == ElementType::LAYER_ELEMENT)
{
auto existing = isAppendAsNew ? nullptr : name_find(*destinationContainer, type, item.name);
item.spritesheetId = existing ? existing->spritesheetId : spritesheet_import(item.spritesheetId);
}
remaps[type][item.id] = named_element_merge(*destinationContainer, item);
}
}
auto item_remap = [&](Element& item)
{
for (auto& frame : item.children)
{
if (frame.type != ElementType::FRAME && frame.type != ElementType::TRIGGER) continue;
for (auto& soundId : frame.soundIds)
soundId = id_remap(ElementType::SOUND_ELEMENT, soundId);
frame.eventId = id_remap(ElementType::EVENT_ELEMENT, frame.eventId);
}
};
auto animation_build = [&](const Element& incoming)
{
auto animation = element_make(ElementType::ANIMATION);
animation.name = incoming.name;
animation.frameNum = incoming.frameNum;
animation.isLoop = incoming.isLoop;
auto single_track_build = [&](ElementType type)
{
if (auto track = child_first_get(incoming, type)) item_remap(animation.children.emplace_back(*track));
};
single_track_build(ElementType::ROOT_ANIMATION);
for (const auto& row : TRACK_CONTAINERS)
{
auto sourceTracks = child_first_get(incoming, row.container);
if (!sourceTracks) continue;
auto container = element_make(row.container);
for (auto item : sourceTracks->children)
{
if (item.type == ElementType::GROUP)
{
std::erase_if(item.children,
[](const Element& child) { return child.type != ElementType::ROOT_ANIMATION; });
if (item.children.empty()) item.children.push_back(root_animation_make());
if (item.id == -1) item.id = element_child_next_id_get(container, ElementType::GROUP);
container.children.push_back(item);
continue;
}
if (item.type != row.track) continue;
item.*(row.id) = id_remap(row.element, item.*(row.id));
if (item.*(row.id) < 0) continue;
item_remap(item);
container.children.push_back(item);
}
animation.children.push_back(container);
}
single_track_build(ElementType::TRIGGERS);
return animation;
};
auto sourceAnimations = source.element_get(ElementType::ANIMATIONS);
auto destinationAnimations = element_get(ElementType::ANIMATIONS);
if (!sourceAnimations || !destinationAnimations) return true;
std::string defaultAnimationName{};
for (const auto& incoming : sourceAnimations->children)
{
if (incoming.type != ElementType::ANIMATION) continue;
auto processed = animation_build(incoming);
auto destination = name_find(*destinationAnimations, ElementType::ANIMATION, processed.name);
if (isAppendAsNew)
processed.name = name_unique_get(*destinationAnimations, ElementType::ANIMATION, processed.name);
if (incoming.name == sourceAnimations->defaultAnimation) defaultAnimationName = processed.name;
if (!destination || isAppendAsNew)
{
destinationAnimations->children.push_back(processed);
continue;
}
if (preset == FILE_MERGE_PRESET_REPLACE_MATCHING)
{
*destination = processed;
continue;
}
destination->isLoop = processed.isLoop;
for (auto type : {ElementType::ROOT_ANIMATION, ElementType::TRIGGERS})
if (auto track = child_first_get(processed, type); track && !track->children.empty())
child_ensure(*destination, type) = *track;
for (const auto& row : TRACK_CONTAINERS)
{
auto sourceTracks = child_first_get(processed, row.container);
if (!sourceTracks) continue;
auto& destinationTracks = child_ensure(*destination, row.container);
std::unordered_map<int, int> groupRemap{};
for (auto item : sourceTracks->children)
{
if (item.type != ElementType::GROUP) continue;
auto sourceGroupId = item.id;
item.id = element_child_next_id_get(destinationTracks, ElementType::GROUP);
destinationTracks.children.push_back(item);
groupRemap[sourceGroupId] = item.id;
}
for (auto item : sourceTracks->children)
{
if (item.type != row.track) continue;
if (item.groupId != -1) item.groupId = groupRemap.contains(item.groupId) ? groupRemap[item.groupId] : -1;
if (auto existing = track_find(destinationTracks, row.track, track_id_get(item)))
{
if (!item.children.empty()) *existing = item;
}
else
destinationTracks.children.push_back(item);
}
}
destination->frameNum = std::max({destination->frameNum, processed.frameNum, animation_length_get(*destination)});
}
if (destinationAnimations->defaultAnimation.empty() && !sourceAnimations->defaultAnimation.empty())
destinationAnimations->defaultAnimation =
defaultAnimationName.empty() ? sourceAnimations->defaultAnimation : defaultAnimationName;
return true;
}
}
+3 -30
View File
@@ -15,25 +15,6 @@ namespace anm2ed
});
}
template <class Callback> void layer_frames_each(Element& root, Callback&& callback)
{
auto layers = content_container_get(root, ElementType::LAYERS);
auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS);
if (!layers || !spritesheets) return;
animations_tracks_each(root, ElementType::LAYER_ANIMATION,
[&](Element& track)
{
auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, track.layerId);
auto spritesheet = layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET,
layer->spritesheetId)
: nullptr;
if (!spritesheet) return;
for (auto& frame : track.children)
if (frame.type == ElementType::FRAME) callback(frame, *spritesheet);
});
}
void shader_ids_repair(Element& root)
{
auto shaders = content_container_get(root, ElementType::SHADERS);
@@ -69,21 +50,13 @@ namespace anm2ed
void region_frame_ids_repair(Element& root)
{
layer_frames_each(root,
[](Element& frame, const Element& spritesheet)
[](Element& frame, Element& spritesheet)
{
auto is_region_match = [&](const Element& region)
{
return region.type == ElementType::REGION &&
glm::ivec2(region.crop) == glm::ivec2(frame.crop) &&
glm::ivec2(region.size) == glm::ivec2(frame.size) &&
glm::ivec2(region.pivot) == glm::ivec2(frame.pivot);
};
auto region = frame.regionId == -1
? nullptr
: child_id_get(spritesheet, ElementType::REGION, frame.regionId);
if (region && is_region_match(*region)) return;
if (auto candidate = child_find(spritesheet, is_region_match)) frame.regionId = candidate->id;
if (region && is_region_matched(*region, frame)) return;
if (auto match = region_match_get(spritesheet, frame)) frame.regionId = match->id;
});
}
+82
View File
@@ -0,0 +1,82 @@
#include "internal.hpp"
namespace anm2ed
{
std::string region_name_get(const std::string& format, int number)
{
try
{
return std::vformat(format, std::make_format_args(number));
}
catch (const std::format_error&)
{
return format;
}
}
bool is_region_matched(const Element& region, const Element& frame)
{
return region.type == ElementType::REGION && glm::ivec2(region.crop) == glm::ivec2(frame.crop) &&
glm::ivec2(region.size) == glm::ivec2(frame.size) && glm::ivec2(region.pivot) == glm::ivec2(frame.pivot);
}
Element* region_match_get(Element& spritesheet, const Element& frame)
{
return child_find(spritesheet, [&](const Element& region) { return is_region_matched(region, frame); });
}
bool Anm2::regions_generate(const std::set<int>& animationIndices, const std::set<Reference>& frameReferences,
const std::string& format, RegionFrameMapping mapping)
{
std::vector<std::pair<Element*, Element*>> frames{};
for (auto animationIndex : animationIndices)
if (auto animation = element_get(ElementType::ANIMATION, animationIndex))
layer_frames_each(root, *animation,
[&](Element& frame, Element& spritesheet) { frames.emplace_back(&frame, &spritesheet); });
for (auto frameReference : frameReferences)
{
if (frameReference.itemType != LAYER || frameReference.frameIndex < 0) continue;
auto frame = element_get(frameReference);
auto layer = element_get(ElementType::LAYER_ELEMENT, frameReference.itemID);
auto spritesheet = layer ? element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
if (frame && frame->type == ElementType::FRAME && frame->regionId == -1 && spritesheet)
frames.emplace_back(frame, spritesheet);
}
std::unordered_map<int, int> regionNumbers{};
bool isChanged{};
for (auto [frame, spritesheet] : frames)
{
auto region = region_match_get(*spritesheet, *frame);
if (!region)
{
auto id = element_child_next_id_get(*spritesheet, ElementType::REGION);
region = &spritesheet->children.emplace_back(element_make(ElementType::REGION));
region->id = id;
region->name = region_name_get(format, ++regionNumbers[spritesheet->id]);
region->crop = frame->crop;
region->size = frame->size;
region->pivot = frame->pivot;
isChanged = true;
}
if (mapping == RegionFrameMapping::SET && frame->regionId != region->id)
{
frame->regionId = region->id;
isChanged = true;
}
}
return isChanged;
}
void Anm2::regions_scan()
{
layer_frames_each(root,
[](Element& frame, Element& spritesheet)
{
if (frame.regionId != -1) return;
if (auto region = region_match_get(spritesheet, frame)) frame.regionId = region->id;
});
}
}