Stage 3: operations layer (src/edit) and one undo entry point
- src/edit/ (in anm2ed_core): frame, track, animation, region, grid and change-all edits as plain functions of the model. Each returns the uids of what it created for the UI to select; copies get fresh uids. - Document::edit_apply snapshots, runs an operation and commits; edit_begin starts drag edits. window_edit, edit_command_push, snapshot_command_push, SnapshotKind and the tracks/frames/regions/textures snapshot kinds are gone; Snapshots has a single push(message, isTextures). - Timeline, animations, regions, shaders, items, wizards, taskbar and file merge call the edit layer; they keep only selection, scrolling and toasts. - Snapshots, Storage, Playback and ImGui core build into anm2ed_core, so undo is testable: tests/undo.cpp runs a seeded random walk of edits per fixture and checks every undo/redo state by hash, plus selection follow through undo. - types::edit renamed types::edit_state (clashed with the edit namespace). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
20bfbace7a
commit
700b613c62
63 files changed
+3223
-1775
No files matched your search
@@ -0,0 +1,127 @@
|
||||
#include "edit.hpp"
|
||||
|
||||
#include "vector.hpp"
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
std::string region_name_get(const std::string&, int);
|
||||
}
|
||||
|
||||
namespace anm2ed::edit
|
||||
{
|
||||
// Clears frame references to the spritesheet's unused regions, then removes them.
|
||||
Uids regions_remove_unused(Anm2& anm2, int spritesheetId)
|
||||
{
|
||||
auto unused = anm2.region_unused(spritesheetId);
|
||||
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
|
||||
if (unused.empty() || !spritesheet) return {};
|
||||
|
||||
animations_tracks_each(anm2.root, ElementType::LAYER_ANIMATION,
|
||||
[&](Element& track)
|
||||
{
|
||||
auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, track.layerId);
|
||||
if (!layer || layer->spritesheetId != spritesheetId) return;
|
||||
for (auto& frame : track.children)
|
||||
if (frame.type == ElementType::FRAME && unused.contains(frame.regionId))
|
||||
frame.regionId = -1;
|
||||
});
|
||||
for (auto id : unused)
|
||||
element_child_id_erase(*spritesheet, ElementType::REGION, id);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Pastes regions into a spritesheet at a child position; they get fresh ids after the existing ones.
|
||||
Uids regions_paste(Anm2& anm2, int spritesheetId, const std::string& text, int insertIndex, std::string* errorString)
|
||||
{
|
||||
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
|
||||
if (!spritesheet) return {};
|
||||
auto maxId = element_child_max_id_get(*spritesheet, ElementType::REGION);
|
||||
if (!anm2.deserialize(ElementType::REGION, text, true, errorString, {}, spritesheetId)) return {};
|
||||
|
||||
spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
|
||||
std::vector<int> pasted{};
|
||||
for (int i = 0; i < (int)spritesheet->children.size(); i++)
|
||||
if (spritesheet->children[i].type == ElementType::REGION && spritesheet->children[i].id > maxId)
|
||||
pasted.push_back(i);
|
||||
util::vector::move_indices_to_position(spritesheet->children, pasted, insertIndex);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Appends grid frames to each layer track (optionally making a region per frame) and fits the animation length.
|
||||
Uids animation_grid_generate(Anm2& anm2, const std::vector<Reference>& tracks, const GridOptions& options)
|
||||
{
|
||||
Uids uids{};
|
||||
for (auto reference : tracks)
|
||||
{
|
||||
auto track = anm2.element_get(reference);
|
||||
auto animation = anm2.element_get(ElementType::ANIMATION, reference.animationIndex);
|
||||
auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, reference.itemID);
|
||||
auto spritesheet = layer ? anm2.element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
|
||||
if (!track || !animation) continue;
|
||||
|
||||
auto start = (int)track->children.size();
|
||||
frames_generate_from_grid(*track, options.start, options.size, options.pivot, options.columns, options.count,
|
||||
options.delay);
|
||||
for (int i = start; i < (int)track->children.size(); ++i)
|
||||
{
|
||||
auto& frame = track->children[i];
|
||||
if (frame.type != ElementType::FRAME) continue;
|
||||
if (!options.isMakeRegions || !spritesheet) continue;
|
||||
|
||||
auto region = element_make(ElementType::REGION);
|
||||
region.id = element_child_next_id_get(*spritesheet, ElementType::REGION);
|
||||
region.name = region_name_get(options.regionNameFormat, i - start);
|
||||
region.crop = frame.crop;
|
||||
region.size = frame.size;
|
||||
region.pivot = frame.pivot;
|
||||
frame.regionId = region.id;
|
||||
spritesheet->children.push_back(region);
|
||||
}
|
||||
animation->frameNum = animation_length_get(*animation);
|
||||
|
||||
anm2.uids_repair();
|
||||
for (int i = start; i < (int)track->children.size(); ++i)
|
||||
uids.push_back(track->children[i].uid);
|
||||
}
|
||||
return uids;
|
||||
}
|
||||
|
||||
// Applies a frame change to explicit frames, to whole tracks (frameIndex -1), and to every matching track of the
|
||||
// given animations.
|
||||
Uids frames_change_apply(Anm2& anm2, const ChangeTargets& targets, FrameChange change, ChangeType type)
|
||||
{
|
||||
std::map<Reference, std::set<int>> frames{};
|
||||
std::set<Reference> wholeTracks{};
|
||||
for (auto reference : targets.references)
|
||||
{
|
||||
auto index = std::exchange(reference.frameIndex, -1);
|
||||
index < 0 ? (void)wholeTracks.insert(reference) : (void)frames[reference].insert(index);
|
||||
}
|
||||
|
||||
auto whole_apply = [&](Element* track, ItemType itemType)
|
||||
{
|
||||
if (!track) return;
|
||||
std::set<int> indices{};
|
||||
for (int i = 0; i < track_frames_count_get(*track); ++i)
|
||||
indices.insert(i);
|
||||
frames_change(*track, change, itemType, type, indices);
|
||||
};
|
||||
|
||||
for (auto& [reference, indices] : frames)
|
||||
if (auto track = anm2.element_get(reference))
|
||||
frames_change(*track, change, (ItemType)reference.itemType, type, indices);
|
||||
for (auto reference : wholeTracks)
|
||||
whole_apply(anm2.element_get(reference), (ItemType)reference.itemType);
|
||||
for (auto animationIndex : targets.animations)
|
||||
{
|
||||
auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex);
|
||||
if (!animation) continue;
|
||||
if (targets.isRoot) whole_apply(animation_item_get(*animation, ItemType::ROOT), ItemType::ROOT);
|
||||
for (const auto& row : TRACK_CONTAINERS)
|
||||
if (auto container = child_first_get(*animation, row.container);
|
||||
container && (row.itemType == ItemType::LAYER ? targets.isLayers : targets.isNulls))
|
||||
tracks_each(*container, row.track, [&](Element& track) { whole_apply(&track, row.itemType); });
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user