Remove items in use; drop Remove Unused

Every content window's Remove (the Delete key) takes the selection out with
what refers to it: a layer's or null's tracks in every animation, a
spritesheet's plain layers (and regions), an event or sound from its triggers
(dropping triggers left with neither), a shader or region from its frames
(which keep the region's rectangle), and a library's layers, flattened into
plain ones. When anything is in use it asks first, listing what goes ("This
will remove:" ... "Are you sure?"). Remove Unused is gone; Remove takes its
place in each footer.

One reference walk (model.cpp) counts uses and finds unused items, so
shaders on null and root frames count, and an animation layer's unused
spritesheet doesn't. Flattening can be limited to some layers, merging only
their children's triggers, which now record the layer they fired through.
edit::content_remove does the removal; Document::spritesheet_path_resolver_get
is shared with saving.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-28 22:33:25 -04:00
1 parent c9d68781d0
commit 065dd522f3
22 files changed
+475 -224

No files matched your search

+99 -5
View File
@@ -123,12 +123,106 @@ namespace anm2ed::edit
}
// Clears frame references to the spritesheet's unused regions, then removes them.
Uids regions_remove_unused(Model& model, int spritesheetId)
// 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 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); });
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 (const 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);
};
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();
});
});
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;
});
});
if (auto spritesheet = item_get(content.spritesheets, spritesheetId)) items_erase(spritesheet->regions);
break;
default:
break;
}
return {};
}
+4 -2
View File
@@ -129,7 +129,6 @@ namespace anm2ed::edit
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);
@@ -140,6 +139,9 @@ namespace anm2ed::edit
// Where a spritesheet path of a document (this one, or a library) points, from this document.
using SpritesheetPathGet = std::function<std::filesystem::path(const model::Model&, const std::filesystem::path&)>;
model::Model animation_layers_flatten(model::Model, const model::LibraryGet&, const SpritesheetPathGet&);
model::Model animation_layers_flatten(model::Model, const model::LibraryGet&, const SpritesheetPathGet&,
const std::set<int>& = {});
Uids content_remove(model::Model&, ElementType, const std::set<int>&, int, const model::LibraryGet&,
const SpritesheetPathGet&);
bool file_merge(model::Model&, const std::filesystem::path&, const std::filesystem::path&, FileMergePreset);
}
+26 -19
View File
@@ -42,12 +42,16 @@ namespace anm2ed::edit
return a == b;
}
// The game's copy of a document: each animation layer's track becomes plain layer tracks (one per layer its child
// animation draws), baked a frame at a time. A library's spritesheets join the copy, at paths from `path_get`.
Model animation_layers_flatten(Model model, const LibraryGet& library_get, const SpritesheetPathGet& path_get)
// The game's copy of a document: each animation layer's track (or only those of `layerIds`) becomes plain layer
// tracks (one per layer its child animation draws), baked a frame at a time. A library's spritesheets join the copy,
// at paths from `path_get`.
Model animation_layers_flatten(Model model, const LibraryGet& library_get, const SpritesheetPathGet& path_get,
const std::set<int>& layerIds)
{
auto& content = model.content;
if (std::ranges::none_of(content.layers, &Layer::is_animation)) return model;
auto is_flattened = [&](const Layer& layer)
{ return layer.is_animation() && (layerIds.empty() || layerIds.contains(layer.id)); };
if (std::ranges::none_of(content.layers, is_flattened)) return model;
std::map<std::pair<const Model*, int>, int> spritesheetIds{};
auto spritesheet_id_get = [&](const Model* source, int id)
@@ -63,10 +67,10 @@ namespace anm2ed::edit
};
// (animation layer, source, source layer) -> the plain layer standing in for it.
std::map<std::tuple<int, const Model*, int>, int> layerIds{};
std::map<std::tuple<int, const Model*, int>, int> plainIds{};
auto layer_id_get = [&](const Draw& draw)
{
auto [it, isNew] = layerIds.try_emplace({draw.id, draw.source, draw.sourceId});
auto [it, isNew] = plainIds.try_emplace({draw.id, draw.source, draw.sourceId});
if (!isNew) return it->second;
auto parent = item_get(content.layers, draw.id);
auto sourceLayer = item_get(draw.source->content.layers, draw.sourceId);
@@ -84,7 +88,7 @@ namespace anm2ed::edit
auto tracks_flatten = [&](const Track& track)
{
auto layer = item_get(content.layers, track.id);
if (!layer || !layer->is_animation()) return std::vector{track};
if (!layer || !is_flattened(*layer)) return std::vector{track};
std::map<int, std::vector<Frame>> frames{};
std::vector<int> order{};
for (int time = 0; time < source.frameNum; ++time)
@@ -123,24 +127,27 @@ namespace anm2ed::edit
group.tracks.push_back(std::move(flat));
entries.emplace_back(std::move(group));
}
// The children's triggers join the animation's own, on the frames they fire (their events found by name).
// The flattened children's triggers join the animation's own, on the frames they fire (their events found by
// name).
auto triggers = source.triggers;
for (int time = 0; time < source.frameNum; ++time)
{
auto fires = animation_triggers_get(model, source, time, library_get);
auto isOwn = source.triggers.isVisible && frame_generate(source.triggers, (float)time).isVisible;
std::vector<int> eventIds{};
for (int i = isOwn ? 1 : 0; i < (int)fires.size(); ++i)
for (auto eventId : fires[i].trigger.eventIds)
if (auto event = item_get(fires[i].source->content.events, eventId))
for (const auto& fire : animation_triggers_get(model, source, time, library_get))
{
auto layer = item_get(content.layers, fire.layerId);
if (!layer || !is_flattened(*layer)) continue;
for (auto eventId : fire.trigger.eventIds)
if (auto event = item_get(fire.source->content.events, eventId))
{
auto existing = std::ranges::find(content.events, event->name, &Event::name);
eventIds.push_back(existing != content.events.end()
? existing->id
: content.events.emplace_back(Event{.id = item_next_id_get(content.events),
.name = event->name})
.id);
eventIds.push_back(
existing != content.events.end()
? existing->id
: content.events.emplace_back(Event{.id = item_next_id_get(content.events), .name = event->name})
.id);
}
}
if (eventIds.empty()) continue;
auto trigger = std::ranges::find(triggers.frames, time, &Frame::atFrame);
if (trigger == triggers.frames.end())
@@ -153,7 +160,7 @@ namespace anm2ed::edit
flattened->layers = std::move(entries);
flattened->triggers = std::move(triggers);
}
std::erase_if(content.layers, [](const Layer& layer) { return layer.is_animation(); });
std::erase_if(content.layers, is_flattened);
return model;
}
}