Format like smack-a-brother, and write comments as sentences
Four-space indents, braces on their own lines, and one-statement bodies without braces. Semicolons in comments became full stops. Designated initializers may leave fields out without a warning.
This commit is contained in:
1 parent
40f6b554b5
commit
7afdf709c3
176 files changed
+20500
-19048
No files matched your search
+409
-413
@@ -6,538 +6,534 @@
|
||||
#include "model/frames.h"
|
||||
#include "model/xml.h"
|
||||
|
||||
std::string region_name_get(const std::string& format, int number)
|
||||
{
|
||||
std::string region_name_get(const std::string& format, int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
return std::vformat(format, std::make_format_args(number));
|
||||
return std::vformat(format, std::make_format_args(number));
|
||||
}
|
||||
catch (const std::format_error&)
|
||||
{
|
||||
return format;
|
||||
return format;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_region_matched(const Region& region, const Frame& frame)
|
||||
{
|
||||
static 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> static void layer_frames_each(Model& model, int animationIndex, Callback&& callback)
|
||||
{
|
||||
// Every frame of every layer track whose layer has a spritesheet, with that spritesheet's id.
|
||||
template <class Callback> static void layer_frames_each(Model& model, int animationIndex, Callback&& callback)
|
||||
{
|
||||
auto animation = model.animation_get(animationIndex);
|
||||
if (!animation) return;
|
||||
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);
|
||||
});
|
||||
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);
|
||||
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)
|
||||
{
|
||||
// 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;
|
||||
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);
|
||||
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);
|
||||
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 {};
|
||||
}
|
||||
}
|
||||
|
||||
// Points frames without a region at a region of their spritesheet with the same crop/size/pivot.
|
||||
Uids regions_scan(Model& model)
|
||||
{
|
||||
// Points frames without a region at a region of their spritesheet with the same crop/size/pivot.
|
||||
Uids regions_scan(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;
|
||||
for (auto& region : item_get(model.content.spritesheets, spritesheetId)->regions)
|
||||
if (is_region_matched(region, frame))
|
||||
{
|
||||
frame.regionId = region.id;
|
||||
break;
|
||||
}
|
||||
});
|
||||
layer_frames_each(model, i, [&](Frame& frame, int spritesheetId) {
|
||||
if (frame.regionId != -1)
|
||||
return;
|
||||
for (auto& region : item_get(model.content.spritesheets, spritesheetId)->regions)
|
||||
if (is_region_matched(region, frame))
|
||||
{
|
||||
frame.regionId = region.id;
|
||||
break;
|
||||
}
|
||||
});
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
// 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;
|
||||
});
|
||||
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.
|
||||
// Removes content items (regions of spritesheet `spritesheetId`) with what refers to them: a layer's or null's tracks
|
||||
// in every animation, an event or sound from its triggers (dropping those left with neither), a shader from its
|
||||
// frames (which go back to the default), a region from its frames (which keep its crop, size and pivot), a
|
||||
// spritesheet's plain layers, and a library's layers, flattened into plain ones.
|
||||
Uids content_remove(Model& model, ElementType type, const std::set<int>& ids, int spritesheetId,
|
||||
const LibraryGet& library_get, const SpritesheetPathGet& path_get)
|
||||
{
|
||||
// Clears frame references to the spritesheet's unused regions, then removes them.
|
||||
// Removes content items (regions of spritesheet `spritesheetId`) with what refers to them: a layer's or null's tracks
|
||||
// in every animation, an event or sound from its triggers (dropping those left with neither), a shader from its
|
||||
// frames (which go back to the default), a region from its frames (which keep its crop, size and pivot), a
|
||||
// spritesheet's plain layers, and a library's layers, flattened into plain ones.
|
||||
Uids content_remove(Model& model, ElementType type, const std::set<int>& ids, int spritesheetId,
|
||||
const LibraryGet& library_get, const SpritesheetPathGet& path_get)
|
||||
{
|
||||
auto& content = model.content;
|
||||
auto items_erase = [&](auto& items)
|
||||
{ std::erase_if(items, [&](const auto& item) { return ids.contains(item.id); }); };
|
||||
auto layer_ids_get = [&](auto&& is_removed)
|
||||
{
|
||||
std::set<int> layerIds;
|
||||
for (auto& layer : content.layers)
|
||||
if (is_removed(layer)) layerIds.insert(layer.id);
|
||||
return layerIds;
|
||||
auto items_erase = [&](auto& items) {
|
||||
std::erase_if(items, [&](const auto& item) { return ids.contains(item.id); });
|
||||
};
|
||||
auto layer_ids_get = [&](auto&& is_removed) {
|
||||
std::set<int> layerIds;
|
||||
for (auto& layer : content.layers)
|
||||
if (is_removed(layer))
|
||||
layerIds.insert(layer.id);
|
||||
return layerIds;
|
||||
};
|
||||
// Only the animations that refer to them are edited.
|
||||
auto animations_edit = [&](auto&& edit)
|
||||
{
|
||||
for (int i = 0; i < model.animations_count_get(); ++i)
|
||||
if (model.uses_count(type, ids, spritesheetId, model.animation_get(i)) > 0) edit(*model.animation_edit(i), i);
|
||||
auto animations_edit = [&](auto&& edit) {
|
||||
for (int i = 0; i < model.animations_count_get(); ++i)
|
||||
if (model.uses_count(type, ids, spritesheetId, model.animation_get(i)) > 0)
|
||||
edit(*model.animation_edit(i), i);
|
||||
};
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case ElementType::LIBRARY:
|
||||
model =
|
||||
animation_layers_flatten(std::move(model), library_get, path_get,
|
||||
layer_ids_get([&](const Layer& layer) { return ids.contains(layer.libraryId); }));
|
||||
items_erase(content.libraries);
|
||||
break;
|
||||
case ElementType::SPRITESHEET:
|
||||
content_remove(model, ElementType::LAYER_ELEMENT,
|
||||
layer_ids_get([&](const Layer& layer)
|
||||
{ return !layer.is_animation() && ids.contains(layer.spritesheetId); }),
|
||||
-1, library_get, path_get);
|
||||
items_erase(content.spritesheets);
|
||||
break;
|
||||
case ElementType::LAYER_ELEMENT:
|
||||
case ElementType::NULL_ELEMENT:
|
||||
{
|
||||
auto itemType = type == ElementType::LAYER_ELEMENT ? LAYER : NULL_;
|
||||
animations_edit([&](Animation&, int index) { items_remove(model, index, {{itemType, ids}}, {}); });
|
||||
if (type == ElementType::LAYER_ELEMENT)
|
||||
items_erase(content.layers);
|
||||
else
|
||||
items_erase(content.nulls);
|
||||
break;
|
||||
}
|
||||
case ElementType::EVENT_ELEMENT:
|
||||
case ElementType::SOUND_ELEMENT:
|
||||
animations_edit(
|
||||
[&](Animation& animation, int)
|
||||
{
|
||||
std::erase_if(animation.triggers.frames,
|
||||
[&](Frame& trigger)
|
||||
{
|
||||
auto& triggerIds =
|
||||
type == ElementType::EVENT_ELEMENT ? trigger.eventIds : trigger.soundIds;
|
||||
if (!std::erase_if(triggerIds, [&](int id) { return ids.contains(id); })) return false;
|
||||
return trigger.eventIds.empty() && trigger.soundIds.empty();
|
||||
});
|
||||
case ElementType::LIBRARY:
|
||||
model = animation_layers_flatten(
|
||||
std::move(model), library_get, path_get,
|
||||
layer_ids_get([&](const Layer& layer) { return ids.contains(layer.libraryId); }));
|
||||
items_erase(content.libraries);
|
||||
break;
|
||||
case ElementType::SPRITESHEET:
|
||||
content_remove(model, ElementType::LAYER_ELEMENT, layer_ids_get([&](const Layer& layer) {
|
||||
return !layer.is_animation() && ids.contains(layer.spritesheetId);
|
||||
}),
|
||||
-1, library_get, path_get);
|
||||
items_erase(content.spritesheets);
|
||||
break;
|
||||
case ElementType::LAYER_ELEMENT:
|
||||
case ElementType::NULL_ELEMENT:
|
||||
{
|
||||
auto itemType = type == ElementType::LAYER_ELEMENT ? LAYER : NULL_;
|
||||
animations_edit([&](Animation&, int index) { items_remove(model, index, {{itemType, ids}}, {}); });
|
||||
if (type == ElementType::LAYER_ELEMENT)
|
||||
items_erase(content.layers);
|
||||
else
|
||||
items_erase(content.nulls);
|
||||
break;
|
||||
}
|
||||
case ElementType::EVENT_ELEMENT:
|
||||
case ElementType::SOUND_ELEMENT:
|
||||
animations_edit([&](Animation& animation, int) {
|
||||
std::erase_if(animation.triggers.frames, [&](Frame& trigger) {
|
||||
auto& triggerIds = type == ElementType::EVENT_ELEMENT ? trigger.eventIds : trigger.soundIds;
|
||||
if (!std::erase_if(triggerIds, [&](int id) { return ids.contains(id); }))
|
||||
return false;
|
||||
return trigger.eventIds.empty() && trigger.soundIds.empty();
|
||||
});
|
||||
});
|
||||
if (type == ElementType::EVENT_ELEMENT)
|
||||
items_erase(content.events);
|
||||
else
|
||||
items_erase(content.sounds);
|
||||
break;
|
||||
case ElementType::SHADER:
|
||||
animations_edit(
|
||||
[&](Animation& animation, int)
|
||||
{
|
||||
animation_tracks_each(animation,
|
||||
[&](Track& track)
|
||||
{
|
||||
for (auto& frame : track.frames)
|
||||
if (ids.contains(frame.shaderId)) frame.shaderId = -1;
|
||||
});
|
||||
if (type == ElementType::EVENT_ELEMENT)
|
||||
items_erase(content.events);
|
||||
else
|
||||
items_erase(content.sounds);
|
||||
break;
|
||||
case ElementType::SHADER:
|
||||
animations_edit([&](Animation& animation, int) {
|
||||
animation_tracks_each(animation, [&](Track& track) {
|
||||
for (auto& frame : track.frames)
|
||||
if (ids.contains(frame.shaderId))
|
||||
frame.shaderId = -1;
|
||||
});
|
||||
});
|
||||
items_erase(content.shaders);
|
||||
break;
|
||||
case ElementType::REGION:
|
||||
animations_edit(
|
||||
[&](Animation& animation, int)
|
||||
{
|
||||
tracks_each(animation.layers,
|
||||
[&](Track& track, auto*)
|
||||
{
|
||||
auto layer = item_get(content.layers, track.id);
|
||||
if (!layer || layer->spritesheetId != spritesheetId) return;
|
||||
for (auto& frame : track.frames)
|
||||
if (ids.contains(frame.regionId)) frame.regionId = -1;
|
||||
});
|
||||
items_erase(content.shaders);
|
||||
break;
|
||||
case ElementType::REGION:
|
||||
animations_edit([&](Animation& animation, int) {
|
||||
tracks_each(animation.layers, [&](Track& track, auto*) {
|
||||
auto layer = item_get(content.layers, track.id);
|
||||
if (!layer || layer->spritesheetId != spritesheetId)
|
||||
return;
|
||||
for (auto& frame : track.frames)
|
||||
if (ids.contains(frame.regionId))
|
||||
frame.regionId = -1;
|
||||
});
|
||||
});
|
||||
if (auto spritesheet = item_get(content.spritesheets, spritesheetId)) items_erase(spritesheet->regions);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (auto spritesheet = item_get(content.spritesheets, spritesheetId))
|
||||
items_erase(spritesheet->regions);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
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)
|
||||
{
|
||||
// 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 {};
|
||||
if (!spritesheet)
|
||||
return {};
|
||||
auto regions = items_from_string<Region>(text, errorString);
|
||||
for (auto& region : regions)
|
||||
region.id = item_next_id_get(spritesheet->regions) + (int)(®ion - regions.data());
|
||||
region.id = item_next_id_get(spritesheet->regions) + (int)(®ion - 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(Model& model, const std::vector<Reference>& tracks, const GridOptions& options)
|
||||
{
|
||||
// Appends grid frames to each layer track (optionally making a region per frame) and fits the animation length.
|
||||
Uids animation_grid_generate(Model& model, const std::vector<Reference>& tracks, const GridOptions& options)
|
||||
{
|
||||
Uids uids = {};
|
||||
for (auto reference : tracks)
|
||||
{
|
||||
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);
|
||||
auto spritesheet = item_get(model.content.spritesheets, spritesheetId);
|
||||
for (int i = start; i < (int)track.frames.size(); ++i)
|
||||
{
|
||||
auto& frame = track.frames[i];
|
||||
uids.push_back(frame.uid);
|
||||
if (!options.isMakeRegions || !spritesheet) continue;
|
||||
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;
|
||||
}
|
||||
auto& animation = *model.animation_edit(reference.animationIndex);
|
||||
animation.frameNum = animation_length_get(animation);
|
||||
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);
|
||||
auto spritesheet = item_get(model.content.spritesheets, spritesheetId);
|
||||
for (int i = start; i < (int)track.frames.size(); ++i)
|
||||
{
|
||||
auto& frame = track.frames[i];
|
||||
uids.push_back(frame.uid);
|
||||
if (!options.isMakeRegions || !spritesheet)
|
||||
continue;
|
||||
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;
|
||||
}
|
||||
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(Model& model, const ChangeTargets& targets, FrameChange change, ChangeType type)
|
||||
{
|
||||
// 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(Model& model, const ChangeTargets& targets, FrameChange change, ChangeType type)
|
||||
{
|
||||
std::map<Reference, std::set<int>> frames;
|
||||
std::set<Reference> wholeTracks;
|
||||
for (auto reference : targets.references)
|
||||
{
|
||||
auto index = std::exchange(reference.frameIndex, -1);
|
||||
index < 0 ? (void)wholeTracks.insert(reference) : (void)frames[reference].insert(index);
|
||||
auto index = std::exchange(reference.frameIndex, -1);
|
||||
index < 0 ? (void)wholeTracks.insert(reference) : (void)frames[reference].insert(index);
|
||||
}
|
||||
|
||||
auto is_animation_layer = [&](const Track& track)
|
||||
{
|
||||
auto layer = track.type == ItemType::LAYER ? item_get(model.content.layers, track.id) : nullptr;
|
||||
return layer && layer->is_animation();
|
||||
auto is_animation_layer = [&](const Track& track) {
|
||||
auto layer = track.type == ItemType::LAYER ? item_get(model.content.layers, track.id) : nullptr;
|
||||
return layer && layer->is_animation();
|
||||
};
|
||||
auto whole_apply = [&](Track& track)
|
||||
{
|
||||
std::set<int> indices;
|
||||
for (int i = 0; i < (int)track.frames.size(); ++i)
|
||||
indices.insert(i);
|
||||
frames_change(track, change, track.type, type, indices, is_animation_layer(track));
|
||||
auto whole_apply = [&](Track& track) {
|
||||
std::set<int> indices;
|
||||
for (int i = 0; i < (int)track.frames.size(); ++i)
|
||||
indices.insert(i);
|
||||
frames_change(track, change, track.type, type, indices, is_animation_layer(track));
|
||||
};
|
||||
|
||||
for (auto& [reference, indices] : frames)
|
||||
if (auto track = model.track_edit(reference))
|
||||
frames_change(*track, change, track->type, type, indices, is_animation_layer(*track));
|
||||
if (auto track = model.track_edit(reference))
|
||||
frames_change(*track, change, track->type, type, indices, is_animation_layer(*track));
|
||||
for (auto reference : wholeTracks)
|
||||
if (auto track = model.track_edit(reference)) whole_apply(*track);
|
||||
if (auto track = model.track_edit(reference))
|
||||
whole_apply(*track);
|
||||
for (auto index : targets.animations)
|
||||
{
|
||||
auto animation = model.animation_edit(index);
|
||||
if (!animation) continue;
|
||||
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); });
|
||||
auto animation = model.animation_edit(index);
|
||||
if (!animation)
|
||||
continue;
|
||||
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)
|
||||
{
|
||||
// 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;
|
||||
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 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 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_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 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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
};
|
||||
|
||||
// Sounds and libraries are matched by path.
|
||||
auto paths_merge = [&](auto& items, const auto& sourceItems, ElementType type)
|
||||
{
|
||||
for (auto item : sourceItems)
|
||||
{
|
||||
auto sourceId = item.id;
|
||||
item.path = path_remap(item.path);
|
||||
auto existing = isAppendAsNew
|
||||
? items.end()
|
||||
: std::ranges::find_if(items, [&](const auto& other) { return other.path == item.path; });
|
||||
item.id = existing != items.end() ? existing->id : item_next_id_get(items);
|
||||
if (existing != items.end())
|
||||
*existing = item;
|
||||
else
|
||||
items.push_back(item);
|
||||
remaps[type][sourceId] = item.id;
|
||||
}
|
||||
auto paths_merge = [&](auto& items, const auto& sourceItems, ElementType type) {
|
||||
for (auto item : sourceItems)
|
||||
{
|
||||
auto sourceId = item.id;
|
||||
item.path = path_remap(item.path);
|
||||
auto existing =
|
||||
isAppendAsNew ? items.end()
|
||||
: std::ranges::find_if(items, [&](const auto& other) { return other.path == item.path; });
|
||||
item.id = existing != items.end() ? existing->id : item_next_id_get(items);
|
||||
if (existing != items.end())
|
||||
*existing = item;
|
||||
else
|
||||
items.push_back(item);
|
||||
remaps[type][sourceId] = item.id;
|
||||
}
|
||||
};
|
||||
paths_merge(content.sounds, source.content.sounds, ElementType::SOUND_ELEMENT);
|
||||
paths_merge(content.libraries, source.content.libraries, ElementType::LIBRARY);
|
||||
|
||||
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);
|
||||
layer.libraryId = id_remap(ElementType::LIBRARY, layer.libraryId);
|
||||
remaps[ElementType::LAYER_ELEMENT][sourceId] = named_merge(content.layers, layer);
|
||||
auto existing = isAppendAsNew ? nullptr : name_find(content.layers, layer.name);
|
||||
auto sourceId = layer.id;
|
||||
layer.spritesheetId = existing ? existing->spritesheetId : spritesheet_import(layer.spritesheetId);
|
||||
layer.libraryId = id_remap(ElementType::LIBRARY, layer.libraryId);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
for (auto& eventId : frame.eventIds)
|
||||
eventId = id_remap(ElementType::EVENT_ELEMENT, 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))
|
||||
auto track_remap = [&](Track& track) {
|
||||
for (auto& frame : track.frames)
|
||||
{
|
||||
if (track_keep(*track)) result.push_back(std::move(entry));
|
||||
for (auto& soundId : frame.soundIds)
|
||||
soundId = id_remap(ElementType::SOUND_ELEMENT, soundId);
|
||||
for (auto& eventId : frame.eventIds)
|
||||
eventId = id_remap(ElementType::EVENT_ELEMENT, eventId);
|
||||
}
|
||||
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 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_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;
|
||||
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)
|
||||
auto processed = animation_build(*incoming);
|
||||
auto destination = animation_find(processed.name);
|
||||
if (isAppendAsNew)
|
||||
{
|
||||
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);
|
||||
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;
|
||||
|
||||
target.frameNum = std::max({target.frameNum, processed.frameNum, animation_length_get(target)});
|
||||
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;
|
||||
model.animations.defaultAnimation =
|
||||
defaultAnimationName.empty() ? source.animations.defaultAnimation : defaultAnimationName;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user