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
@@ -5,98 +5,19 @@ namespace anm2ed::imgui
|
||||
void TimelineContext::frame_insert()
|
||||
{
|
||||
auto targetReference = reference;
|
||||
auto targetFrameTime = document.frameTime;
|
||||
auto time = (int)document.frameTime;
|
||||
if (!animation || !command_item_reference_get(document, targetReference)) return;
|
||||
|
||||
edit_command_push(EDIT_INSERT_FRAME, Document::FRAMES,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto animation =
|
||||
document.anm2.element_get(ElementType::ANIMATION, targetReference.animationIndex);
|
||||
auto item = command_item_reference_get(document, targetReference);
|
||||
if (!animation || !item) return;
|
||||
|
||||
auto newReference = targetReference;
|
||||
|
||||
if (targetReference.itemType == TRIGGER)
|
||||
{
|
||||
for (auto& trigger : item->children)
|
||||
if (targetFrameTime == trigger.atFrame) return;
|
||||
|
||||
auto addFrame = element_make(ElementType::TRIGGER);
|
||||
addFrame.atFrame = targetFrameTime;
|
||||
item->children.push_back(addFrame);
|
||||
frames_sort_by_at_frame(*item);
|
||||
newReference.frameIndex = frame_index_from_at_frame_get(*item, addFrame.atFrame);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto frame = command_frame_get(document, targetReference);
|
||||
auto framesCount = track_frames_count_get(*item);
|
||||
if (frame)
|
||||
{
|
||||
auto addFrame = *frame;
|
||||
auto insertIndex = std::clamp(targetReference.frameIndex + 1, 0, framesCount);
|
||||
auto childIndex = track_frame_insert_child_index_get(*item, insertIndex);
|
||||
item->children.insert(item->children.begin() + childIndex, addFrame);
|
||||
newReference.frameIndex = insertIndex;
|
||||
}
|
||||
else if (auto lastFrame = track_frame_get(*item, framesCount - 1))
|
||||
{
|
||||
item->children.emplace_back(*lastFrame);
|
||||
newReference.frameIndex = framesCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
item->children.emplace_back(element_make(ElementType::FRAME));
|
||||
newReference.frameIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
document.reference = newReference;
|
||||
frames_selection_set_reference_for(document);
|
||||
if (newReference.itemType != TRIGGER)
|
||||
document.frameTime = frame_time_from_index_get(*item, newReference.frameIndex);
|
||||
},
|
||||
SnapshotKind::TRACKS, {targetReference});
|
||||
}
|
||||
|
||||
void TimelineContext::frames_delete_for(Document& document, std::set<Reference> selectedFrames)
|
||||
{
|
||||
auto groupedFrames = frames_by_item_get(selectedFrames);
|
||||
|
||||
for (auto& [itemReference, indices] : groupedFrames)
|
||||
{
|
||||
auto item = command_item_reference_get(document, itemReference);
|
||||
if (!item) continue;
|
||||
|
||||
for (auto it = indices.rbegin(); it != indices.rend(); ++it)
|
||||
{
|
||||
auto childIndex = track_frame_child_index_get(*item, *it);
|
||||
if (childIndex != -1) item->children.erase(item->children.begin() + childIndex);
|
||||
}
|
||||
}
|
||||
|
||||
auto item = command_item_reference_get(document, document.reference);
|
||||
if (!item || item->children.empty())
|
||||
{
|
||||
document.reference.frameIndex = -1;
|
||||
frames_selection_reset_for(document);
|
||||
return;
|
||||
}
|
||||
|
||||
document.reference.frameIndex = glm::clamp(document.reference.frameIndex, 0, (int)item->children.size() - 1);
|
||||
frames_selection_set_reference_for(document);
|
||||
edit_push(EDIT_INSERT_FRAME, Document::FRAMES,
|
||||
[=](Anm2& anm2) { return edit::frame_insert(anm2, targetReference, time); });
|
||||
}
|
||||
|
||||
void TimelineContext::frames_delete_action()
|
||||
{
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
if (selectedFrames.empty()) return;
|
||||
auto trackReferences = track_references_from_frame_references_get(selectedFrames);
|
||||
edit_command_push(
|
||||
EDIT_DELETE_FRAMES, Document::FRAMES, [=, this](Manager&, Document& document) mutable
|
||||
{ frames_delete_for(document, selectedFrames); }, SnapshotKind::TRACKS, trackReferences);
|
||||
edit_push(
|
||||
EDIT_DELETE_FRAMES, Document::FRAMES, [=](Anm2& anm2) { return edit::frames_delete(anm2, selectedFrames); },
|
||||
[this](Document& document, const edit::Uids&) { frames_selection_set_reference_for(document); });
|
||||
}
|
||||
|
||||
void TimelineContext::frames_duplicate()
|
||||
@@ -104,63 +25,8 @@ namespace anm2ed::imgui
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
std::erase_if(selectedFrames, is_trigger_reference);
|
||||
if (selectedFrames.empty()) return;
|
||||
|
||||
auto trackReferences = track_references_from_frame_references_get(selectedFrames);
|
||||
edit_command_push(
|
||||
EDIT_DUPLICATE_FRAMES, Document::FRAMES,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto groupedFrames = frames_by_item_get(selectedFrames);
|
||||
|
||||
std::set<Reference> duplicatedSelection{};
|
||||
auto newReference = document.reference;
|
||||
bool isReferenceSet{};
|
||||
|
||||
for (auto& [itemReference, indices] : groupedFrames)
|
||||
{
|
||||
auto item = command_item_reference_get(document, itemReference);
|
||||
if (!item) continue;
|
||||
|
||||
std::vector<int> validIndices{};
|
||||
std::vector<Element> duplicatedFrames{};
|
||||
for (auto i : indices)
|
||||
{
|
||||
auto frame = track_frame_get(*item, i);
|
||||
if (!frame) continue;
|
||||
validIndices.push_back(i);
|
||||
duplicatedFrames.push_back(*frame);
|
||||
}
|
||||
if (validIndices.empty()) continue;
|
||||
|
||||
auto insertIndex = validIndices.back() + 1;
|
||||
auto childIndex = track_frame_insert_child_index_get(*item, insertIndex);
|
||||
item->children.insert(item->children.begin() + childIndex,
|
||||
std::make_move_iterator(duplicatedFrames.begin()),
|
||||
std::make_move_iterator(duplicatedFrames.end()));
|
||||
|
||||
for (int offset = 0; offset < (int)validIndices.size(); ++offset)
|
||||
{
|
||||
auto targetReference = itemReference;
|
||||
targetReference.frameIndex = insertIndex + offset;
|
||||
duplicatedSelection.insert(targetReference);
|
||||
auto sourceReference = itemReference;
|
||||
sourceReference.frameIndex = validIndices[offset];
|
||||
if (sourceReference == document.reference)
|
||||
{
|
||||
newReference = targetReference;
|
||||
isReferenceSet = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (duplicatedSelection.empty()) return;
|
||||
document.reference = isReferenceSet ? newReference : *duplicatedSelection.begin();
|
||||
document.frame_references_set(std::move(duplicatedSelection));
|
||||
if (auto item = command_item_reference_get(document, document.reference))
|
||||
document.frameTime = frame_time_from_index_get(*item, document.reference.frameIndex);
|
||||
frames_focus_sync_for(document);
|
||||
},
|
||||
SnapshotKind::TRACKS, trackReferences);
|
||||
edit_push(EDIT_DUPLICATE_FRAMES, Document::FRAMES,
|
||||
[=, focus = reference](Anm2& anm2) { return edit::frames_duplicate(anm2, selectedFrames, focus); });
|
||||
}
|
||||
|
||||
bool TimelineContext::is_frames_reverse_available(std::set<Reference> selectedFrames)
|
||||
@@ -180,126 +46,19 @@ namespace anm2ed::imgui
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
std::erase_if(selectedFrames, is_trigger_reference);
|
||||
if (!is_frames_reverse_available(selectedFrames)) return;
|
||||
|
||||
auto trackReferences = track_references_from_frame_references_get(selectedFrames);
|
||||
edit_command_push(
|
||||
EDIT_REVERSE_FRAMES, Document::FRAMES,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto groupedFrames = frames_by_item_get(selectedFrames);
|
||||
|
||||
std::set<Reference> reversedSelection{};
|
||||
auto newReference = document.reference;
|
||||
bool isReferenceSet{};
|
||||
|
||||
for (auto& [itemReference, indices] : groupedFrames)
|
||||
{
|
||||
auto item = command_item_reference_get(document, itemReference);
|
||||
if (!item || indices.size() < 2) continue;
|
||||
|
||||
std::vector<int> validIndices{};
|
||||
std::vector<Element> reversedFrames{};
|
||||
for (auto i : indices)
|
||||
{
|
||||
auto frame = track_frame_get(*item, i);
|
||||
if (!frame) continue;
|
||||
validIndices.push_back(i);
|
||||
reversedFrames.push_back(std::move(*frame));
|
||||
}
|
||||
if (validIndices.size() < 2) continue;
|
||||
|
||||
auto insertIndex = validIndices.front();
|
||||
for (auto it = validIndices.rbegin(); it != validIndices.rend(); ++it)
|
||||
{
|
||||
auto childIndex = track_frame_child_index_get(*item, *it);
|
||||
if (childIndex != -1) item->children.erase(item->children.begin() + childIndex);
|
||||
}
|
||||
|
||||
std::ranges::reverse(reversedFrames);
|
||||
auto childIndex = track_frame_insert_child_index_get(*item, insertIndex);
|
||||
item->children.insert(item->children.begin() + childIndex, std::make_move_iterator(reversedFrames.begin()),
|
||||
std::make_move_iterator(reversedFrames.end()));
|
||||
|
||||
for (int offset = 0; offset < (int)validIndices.size(); ++offset)
|
||||
{
|
||||
auto targetReference = itemReference;
|
||||
targetReference.frameIndex = insertIndex + offset;
|
||||
reversedSelection.insert(targetReference);
|
||||
auto sourceIndex = validIndices[(int)validIndices.size() - 1 - offset];
|
||||
auto sourceReference = itemReference;
|
||||
sourceReference.frameIndex = sourceIndex;
|
||||
if (sourceReference == document.reference)
|
||||
{
|
||||
newReference = targetReference;
|
||||
isReferenceSet = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (reversedSelection.empty()) return;
|
||||
document.reference = isReferenceSet ? newReference : *reversedSelection.begin();
|
||||
document.frame_references_set(std::move(reversedSelection));
|
||||
if (auto item = command_item_reference_get(document, document.reference))
|
||||
document.frameTime = frame_time_from_index_get(*item, document.reference.frameIndex);
|
||||
frames_focus_sync_for(document);
|
||||
},
|
||||
SnapshotKind::TRACKS, trackReferences);
|
||||
edit_push(EDIT_REVERSE_FRAMES, Document::FRAMES,
|
||||
[=](Anm2& anm2) { return edit::frames_reverse(anm2, selectedFrames); });
|
||||
}
|
||||
|
||||
void TimelineContext::frames_bake()
|
||||
{
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
auto bakeInterval = settings.bakeInterval;
|
||||
auto isRoundScale = settings.bakeIsRoundScale;
|
||||
auto isRoundRotation = settings.bakeIsRoundRotation;
|
||||
std::erase_if(selectedFrames, is_trigger_reference);
|
||||
if (selectedFrames.empty()) return;
|
||||
|
||||
auto trackReferences = track_references_from_frame_references_get(selectedFrames);
|
||||
edit_command_push(
|
||||
EDIT_BAKE_FRAMES, Document::FRAMES,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto groupedFrames = frames_by_item_get(selectedFrames);
|
||||
|
||||
std::set<Reference> bakedSelection{};
|
||||
for (auto& [itemReference, indices] : groupedFrames)
|
||||
{
|
||||
auto item = command_item_reference_get(document, itemReference);
|
||||
if (!item) continue;
|
||||
|
||||
int insertedBefore = 0;
|
||||
for (auto originalIndex : indices)
|
||||
{
|
||||
auto i = originalIndex + insertedBefore;
|
||||
auto frame = track_frame_get(*item, i);
|
||||
if (!frame) continue;
|
||||
|
||||
auto originalDuration = frame->duration;
|
||||
frame_bake(*item, i, bakeInterval, isRoundScale, isRoundRotation);
|
||||
|
||||
auto bakedCount =
|
||||
originalDuration <= FRAME_DURATION_MIN ? 1 : (int)std::ceil((float)originalDuration / bakeInterval);
|
||||
for (int offset = 0; offset < bakedCount; ++offset)
|
||||
{
|
||||
auto frameReference = itemReference;
|
||||
frameReference.frameIndex = i + offset;
|
||||
bakedSelection.insert(frameReference);
|
||||
}
|
||||
|
||||
insertedBefore += bakedCount - 1;
|
||||
}
|
||||
}
|
||||
|
||||
document.frame_references_set(std::move(bakedSelection));
|
||||
if (!document.frames.references.empty())
|
||||
{
|
||||
frames_focus_sync_for(document);
|
||||
}
|
||||
else
|
||||
frames_selection_reset_for(document);
|
||||
},
|
||||
SnapshotKind::TRACKS, trackReferences);
|
||||
edit_push(EDIT_BAKE_FRAMES, Document::FRAMES,
|
||||
[=, interval = settings.bakeInterval, isRoundScale = settings.bakeIsRoundScale,
|
||||
isRoundRotation = settings.bakeIsRoundRotation](Anm2& anm2)
|
||||
{ return edit::frames_bake(anm2, selectedFrames, interval, isRoundScale, isRoundRotation); });
|
||||
}
|
||||
|
||||
std::set<Reference> TimelineContext::selected_root_frame_references_get()
|
||||
@@ -350,158 +109,16 @@ namespace anm2ed::imgui
|
||||
return !targetItems.empty();
|
||||
}
|
||||
|
||||
void TimelineContext::frame_root_transform_apply(Element& frame, const Element& rootFrame, bool isRoundScale,
|
||||
bool isRoundRotation, bool isUseRootPivot)
|
||||
{
|
||||
auto rootScale = math::percent_to_unit(rootFrame.scale);
|
||||
auto pivot = isUseRootPivot ? rootFrame.position : glm::vec2();
|
||||
auto offset = (frame.position - pivot) * rootScale;
|
||||
auto radians = glm::radians(rootFrame.rotation);
|
||||
auto cos = std::cos(radians);
|
||||
auto sin = std::sin(radians);
|
||||
|
||||
frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos);
|
||||
frame.scale *= rootScale;
|
||||
frame.rotation += rootFrame.rotation;
|
||||
frame.tint *= rootFrame.tint;
|
||||
frame.colorOffset += rootFrame.colorOffset;
|
||||
|
||||
if (isRoundScale) frame.scale = glm::round(frame.scale);
|
||||
if (isRoundRotation) frame.rotation = std::round(frame.rotation);
|
||||
}
|
||||
|
||||
void TimelineContext::bake_into_other_frames()
|
||||
{
|
||||
auto selectedRootFrames = selected_root_frame_references_get();
|
||||
if (selectedRootFrames.empty()) return;
|
||||
if (!animation) return;
|
||||
auto target = bakeIntoOtherFramesTarget;
|
||||
auto isLayers = isBakeIntoOtherFramesLayers;
|
||||
auto isNulls = isBakeIntoOtherFramesNulls;
|
||||
auto isRoundScale = settings.bakeIsRoundScale;
|
||||
auto isRoundRotation = settings.bakeIsRoundRotation;
|
||||
auto isMatchRootInterpolation = settings.bakeIsMatchRootInterpolation;
|
||||
auto isUseRootPivot = settings.bakeIsUseRootPivot;
|
||||
if (selectedRootFrames.empty() || !animation) return;
|
||||
auto targetItems = item_references_for_bake_into_other_frames_get(
|
||||
document, *animation, target, isBakeIntoOtherFramesLayers, isBakeIntoOtherFramesNulls);
|
||||
auto trackReferences = track_references_from_frame_references_get(selectedRootFrames);
|
||||
trackReferences.insert(targetItems.begin(), targetItems.end());
|
||||
edit_command_push(
|
||||
EDIT_BAKE_INTO_OTHER_FRAMES, Document::FRAMES,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, document.reference.animationIndex);
|
||||
if (!animation) return;
|
||||
auto rootItemReference = item_reference_from_frame_get(*selectedRootFrames.begin());
|
||||
for (auto rootReference : selectedRootFrames)
|
||||
if (!is_same_item(rootReference, rootItemReference)) return;
|
||||
auto root = command_item_reference_get(document, rootItemReference);
|
||||
if (!root) return;
|
||||
|
||||
std::vector<RootFrameSpan> spans{};
|
||||
for (auto rootReference : selectedRootFrames)
|
||||
{
|
||||
auto rootFrame = command_frame_get(document, rootReference);
|
||||
if (!rootFrame) continue;
|
||||
auto start = (int)frame_time_from_index_get(*root, rootReference.frameIndex);
|
||||
spans.push_back({start, start + rootFrame->duration});
|
||||
}
|
||||
if (spans.empty()) return;
|
||||
auto is_spanned = [&](int time)
|
||||
{
|
||||
return std::ranges::any_of(spans,
|
||||
[&](RootFrameSpan span) { return time >= span.start && time < span.end; });
|
||||
};
|
||||
|
||||
auto targetItems =
|
||||
item_references_for_bake_into_other_frames_get(document, *animation, target, isLayers, isNulls);
|
||||
if (targetItems.empty()) return;
|
||||
|
||||
for (auto itemReference : targetItems)
|
||||
{
|
||||
auto item = command_item_reference_get(document, itemReference);
|
||||
if (!item) continue;
|
||||
|
||||
if (isMatchRootInterpolation)
|
||||
{
|
||||
std::vector<int> bakeIndices{};
|
||||
auto frameType = track_frame_type_get(*item);
|
||||
int frameIndex{};
|
||||
for (const auto& frame : item->children)
|
||||
{
|
||||
if (frame.type != frameType) continue;
|
||||
if (is_spanned((int)frame_time_from_index_get(*item, frameIndex))) bakeIndices.push_back(frameIndex);
|
||||
++frameIndex;
|
||||
}
|
||||
for (auto it = bakeIndices.rbegin(); it != bakeIndices.rend(); ++it)
|
||||
frame_bake(*item, *it, FRAME_DURATION_MIN, isRoundScale, isRoundRotation);
|
||||
}
|
||||
|
||||
auto frameType = track_frame_type_get(*item);
|
||||
int frameIndex{};
|
||||
for (auto& frame : item->children)
|
||||
{
|
||||
if (frame.type != frameType) continue;
|
||||
auto frameStart = (int)frame_time_from_index_get(*item, frameIndex++);
|
||||
if (is_spanned(frameStart))
|
||||
frame_root_transform_apply(frame, frame_generate(*root, (float)frameStart), isRoundScale,
|
||||
isRoundRotation, isUseRootPivot);
|
||||
}
|
||||
}
|
||||
|
||||
std::set<int> selectedRootFrameIndices{};
|
||||
for (auto rootReference : selectedRootFrames)
|
||||
{
|
||||
auto rootFrame = command_frame_get(document, rootReference);
|
||||
if (!rootFrame) continue;
|
||||
selectedRootFrameIndices.insert(rootReference.frameIndex);
|
||||
}
|
||||
if (selectedRootFrameIndices.empty()) return;
|
||||
|
||||
std::vector<Element> rootFrames{};
|
||||
std::set<Reference> defaultRootSelection{};
|
||||
int frameIndex{};
|
||||
int rewrittenFrameIndex{};
|
||||
int defaultDuration{};
|
||||
auto default_root_frame_push = [&]()
|
||||
{
|
||||
if (defaultDuration <= 0) return;
|
||||
auto frame = element_make(ElementType::FRAME);
|
||||
frame.duration = glm::max(defaultDuration, FRAME_DURATION_MIN);
|
||||
auto defaultRootReference = rootItemReference;
|
||||
defaultRootReference.frameIndex = rewrittenFrameIndex;
|
||||
defaultRootSelection.insert(defaultRootReference);
|
||||
rootFrames.push_back(frame);
|
||||
++rewrittenFrameIndex;
|
||||
defaultDuration = 0;
|
||||
};
|
||||
|
||||
for (const auto& frame : root->children)
|
||||
{
|
||||
if (frame.type != ElementType::FRAME)
|
||||
{
|
||||
default_root_frame_push();
|
||||
rootFrames.push_back(frame);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (selectedRootFrameIndices.contains(frameIndex))
|
||||
defaultDuration += frame.duration;
|
||||
else
|
||||
{
|
||||
default_root_frame_push();
|
||||
rootFrames.push_back(frame);
|
||||
++rewrittenFrameIndex;
|
||||
}
|
||||
++frameIndex;
|
||||
}
|
||||
default_root_frame_push();
|
||||
|
||||
root->children = std::move(rootFrames);
|
||||
document.frame_references_set(std::move(defaultRootSelection));
|
||||
frames_focus_sync_for(document);
|
||||
},
|
||||
SnapshotKind::TRACKS, trackReferences);
|
||||
document, *animation, bakeIntoOtherFramesTarget, isBakeIntoOtherFramesLayers, isBakeIntoOtherFramesNulls);
|
||||
edit::RootBakeOptions options{settings.bakeIsRoundScale, settings.bakeIsRoundRotation,
|
||||
settings.bakeIsMatchRootInterpolation, settings.bakeIsUseRootPivot};
|
||||
edit_push(EDIT_BAKE_INTO_OTHER_FRAMES, Document::FRAMES,
|
||||
[=](Anm2& anm2) { return edit::root_bake_into(anm2, selectedRootFrames, targetItems, options); });
|
||||
}
|
||||
|
||||
void TimelineContext::frame_split()
|
||||
@@ -509,93 +126,26 @@ namespace anm2ed::imgui
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
if (selectedFrames.size() != 1) return;
|
||||
auto targetReference = *selectedFrames.begin();
|
||||
auto splitTime = frameSplitTimeAtCursor.value_or((int)std::floor(playback.time));
|
||||
if (targetReference.itemType == TRIGGER) return;
|
||||
|
||||
edit_command_push(
|
||||
EDIT_SPLIT_FRAME, Document::FRAMES,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
if (targetReference.itemType == TRIGGER) return;
|
||||
|
||||
auto item = command_item_reference_get(document, targetReference);
|
||||
auto frame = command_frame_get(document, targetReference);
|
||||
|
||||
if (!item || !frame) return;
|
||||
|
||||
auto originalDuration = frame->duration;
|
||||
if (originalDuration <= 1) return;
|
||||
|
||||
auto frameStartTime = frame_time_from_index_get(*item, targetReference.frameIndex);
|
||||
int frameStart = (int)std::round(frameStartTime);
|
||||
int firstDuration = splitTime - frameStart + 1;
|
||||
|
||||
if (firstDuration <= 0 || firstDuration >= originalDuration) return;
|
||||
|
||||
int secondDuration = originalDuration - firstDuration;
|
||||
auto splitFrame = *frame;
|
||||
splitFrame.duration = secondDuration;
|
||||
|
||||
auto nextFrame = track_frame_get(*item, targetReference.frameIndex + 1);
|
||||
if (frame->interpolation != Interpolation::NONE && nextFrame)
|
||||
frame_mix(splitFrame, *nextFrame,
|
||||
interpolation_factor(frame->interpolation, (float)firstDuration / (float)originalDuration));
|
||||
|
||||
frame->duration = firstDuration;
|
||||
auto insertIndex = track_frame_insert_child_index_get(*item, targetReference.frameIndex + 1);
|
||||
item->children.insert(item->children.begin() + insertIndex, splitFrame);
|
||||
document.reference = targetReference;
|
||||
frames_selection_set_reference_for(document);
|
||||
},
|
||||
SnapshotKind::TRACKS, {targetReference});
|
||||
auto time = frameSplitTimeAtCursor.value_or((int)std::floor(playback.time));
|
||||
edit_push(EDIT_SPLIT_FRAME, Document::FRAMES,
|
||||
[=](Anm2& anm2) { return edit::frame_split(anm2, targetReference, time); });
|
||||
}
|
||||
|
||||
void TimelineContext::item_remove()
|
||||
{
|
||||
auto targetRows = selected_row_references_get();
|
||||
std::map<int, std::set<int>> targetIds{};
|
||||
std::map<int, std::set<int>> targetGroupIds{};
|
||||
for (auto row : targetRows)
|
||||
{
|
||||
if (row.type != LAYER && row.type != NULL_) continue;
|
||||
if (row.isGroup)
|
||||
targetGroupIds[row.type].insert(row.id);
|
||||
else
|
||||
targetIds[row.type].insert(row.id);
|
||||
}
|
||||
auto animationIndex = reference.animationIndex;
|
||||
if (!animation) return;
|
||||
if (targetIds.empty() && targetGroupIds.empty()) return;
|
||||
edit_command_push(EDIT_REMOVE_ITEMS, Document::ITEMS,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, animationIndex);
|
||||
if (!animation) return;
|
||||
for (auto targetType : {LAYER, NULL_})
|
||||
{
|
||||
auto containerType = TYPE_CONTAINERS[targetType];
|
||||
auto targetTrackType = TYPE_TRACKS[targetType];
|
||||
auto container = child_first_get(*animation, containerType);
|
||||
if (!container) continue;
|
||||
auto ids = targetIds.contains(targetType) ? targetIds.at(targetType) : std::set<int>{};
|
||||
auto groupIds =
|
||||
targetGroupIds.contains(targetType) ? targetGroupIds.at(targetType) : std::set<int>{};
|
||||
for (int i = (int)container->children.size() - 1; i >= 0; --i)
|
||||
{
|
||||
auto& item = container->children[i];
|
||||
if (item.type == ElementType::GROUP && groupIds.contains(item.id))
|
||||
{
|
||||
container->children.erase(container->children.begin() + i);
|
||||
continue;
|
||||
}
|
||||
if (item.type == targetTrackType &&
|
||||
(ids.contains(anm2ed::track_id_get(item)) || groupIds.contains(item.groupId)))
|
||||
container->children.erase(container->children.begin() + i);
|
||||
}
|
||||
}
|
||||
reference_clear_for(document);
|
||||
group_selection_reset_for(document);
|
||||
});
|
||||
std::map<int, std::set<int>> ids{};
|
||||
std::map<int, std::set<int>> groupIds{};
|
||||
for (auto row : selected_row_references_get())
|
||||
if (row.type == LAYER || row.type == NULL_) (row.isGroup ? groupIds : ids)[row.type].insert(row.id);
|
||||
if (!animation || (ids.empty() && groupIds.empty())) return;
|
||||
edit_push(
|
||||
EDIT_REMOVE_ITEMS, Document::ITEMS, [=, animationIndex = reference.animationIndex](Anm2& anm2)
|
||||
{ return edit::items_remove(anm2, animationIndex, ids, groupIds); },
|
||||
[this](Document& document, const edit::Uids&)
|
||||
{
|
||||
reference_clear_for(document);
|
||||
group_selection_reset_for(document);
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<Reference> TimelineContext::item_references_groupable_get()
|
||||
@@ -625,51 +175,21 @@ namespace anm2ed::imgui
|
||||
{
|
||||
auto targetReferences = item_references_groupable_get();
|
||||
if (targetReferences.empty()) return;
|
||||
auto targetType = targetReferences.front().itemType;
|
||||
auto animationIndex = reference.animationIndex;
|
||||
auto groupName = std::string(localize.get(TEXT_NEW_GROUP));
|
||||
|
||||
edit_command_push(EDIT_GROUP_ITEMS, Document::ITEMS,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, animationIndex);
|
||||
if (!animation) return;
|
||||
|
||||
auto containerType = TYPE_CONTAINERS[targetType];
|
||||
auto targetTrackType = TYPE_TRACKS[targetType];
|
||||
auto container = child_first_get(*animation, containerType);
|
||||
if (!container) return;
|
||||
|
||||
std::set<int> targetIds{};
|
||||
for (auto itemReference : targetReferences)
|
||||
targetIds.insert(itemReference.itemID);
|
||||
|
||||
auto group = element_make(ElementType::GROUP);
|
||||
group.id = element_child_next_id_get(*container, ElementType::GROUP);
|
||||
group.name = groupName;
|
||||
group.children.push_back(root_animation_make());
|
||||
int insertIndex = (int)container->children.size();
|
||||
|
||||
for (int i = 0; i < (int)container->children.size(); ++i)
|
||||
{
|
||||
auto& item = container->children[i];
|
||||
if (item.type == targetTrackType && targetIds.contains(anm2ed::track_id_get(item)))
|
||||
{
|
||||
insertIndex = std::min(insertIndex, i);
|
||||
item.groupId = group.id;
|
||||
}
|
||||
}
|
||||
|
||||
if (insertIndex == (int)container->children.size()) return;
|
||||
insertIndex = std::clamp(insertIndex, 0, (int)container->children.size());
|
||||
container->children.insert(container->children.begin() + insertIndex, group);
|
||||
|
||||
document.items.references.clear();
|
||||
for (auto itemReference : targetReferences)
|
||||
document.items.references.insert(itemReference);
|
||||
document.reference = targetReferences.front();
|
||||
frames_selection_reset_for(document);
|
||||
});
|
||||
std::set<int> ids{};
|
||||
for (auto itemReference : targetReferences)
|
||||
ids.insert(itemReference.itemID);
|
||||
edit_push(
|
||||
EDIT_GROUP_ITEMS, Document::ITEMS,
|
||||
[=, animationIndex = reference.animationIndex, type = targetReferences.front().itemType,
|
||||
name = std::string(localize.get(TEXT_NEW_GROUP))](Anm2& anm2)
|
||||
{ return edit::items_group(anm2, animationIndex, type, ids, name); },
|
||||
[=, this](Document& document, const edit::Uids& uids)
|
||||
{
|
||||
if (uids.empty()) return;
|
||||
document.items.references = {targetReferences.begin(), targetReferences.end()};
|
||||
document.reference = targetReferences.front();
|
||||
frames_selection_reset_for(document);
|
||||
});
|
||||
}
|
||||
|
||||
void TimelineContext::rows_move_to_row(std::vector<TimelineRowReference> draggedRows, TimelineItemRow targetRow,
|
||||
@@ -678,126 +198,37 @@ namespace anm2ed::imgui
|
||||
if (draggedRows.empty()) return;
|
||||
auto targetType = draggedRows.front().type;
|
||||
if (targetType != LAYER && targetType != NULL_) return;
|
||||
for (const auto& draggedRow : draggedRows)
|
||||
if (draggedRow.type != targetType) return;
|
||||
|
||||
if (std::ranges::any_of(draggedRows, [&](const auto& row) { return row.type != targetType; })) return;
|
||||
if (targetRow.isGroup && targetRow.type != targetType) return;
|
||||
if (!targetRow.isGroup && targetRow.type != targetType && targetRow.type != NONE && targetRow.type != ROOT &&
|
||||
targetRow.type != TRIGGER)
|
||||
return;
|
||||
|
||||
std::set<int> ids{};
|
||||
std::set<int> groupIds{};
|
||||
for (const auto& row : draggedRows)
|
||||
(row.isGroup ? groupIds : ids).insert(row.id);
|
||||
edit::RowTarget target{targetRow.isGroup, targetRow.type, targetRow.id, targetRow.groupId};
|
||||
auto animationIndex = reference.animationIndex;
|
||||
edit_command_push(
|
||||
|
||||
edit_push(
|
||||
EDIT_MOVE_ITEMS, Document::ITEMS,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
[=](Anm2& anm2)
|
||||
{
|
||||
return edit::items_move(anm2, animationIndex, targetType, ids, groupIds, target, isDropAfter,
|
||||
isDropIntoGroup);
|
||||
},
|
||||
[=, this](Document& document, const edit::Uids&)
|
||||
{
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, animationIndex);
|
||||
if (!animation) return;
|
||||
auto container = child_first_get(*animation, TYPE_CONTAINERS[targetType]);
|
||||
if (!container) return;
|
||||
auto targetTrackType = TYPE_TRACKS[targetType];
|
||||
|
||||
std::set<int> draggedIds{};
|
||||
std::set<int> draggedGroupIds{};
|
||||
for (const auto& draggedRow : draggedRows)
|
||||
{
|
||||
if (draggedRow.isGroup)
|
||||
draggedGroupIds.insert(draggedRow.id);
|
||||
else
|
||||
draggedIds.insert(draggedRow.id);
|
||||
}
|
||||
|
||||
if (targetRow.groupId != -1 && draggedGroupIds.contains(targetRow.groupId)) return;
|
||||
|
||||
auto is_dragged = [&](const Element& item)
|
||||
{
|
||||
return (item.type == ElementType::GROUP && draggedGroupIds.contains(item.id)) ||
|
||||
(item.type == targetTrackType &&
|
||||
(draggedIds.contains(anm2ed::track_id_get(item)) || draggedGroupIds.contains(item.groupId)));
|
||||
};
|
||||
std::vector<Element> movedItems{};
|
||||
std::ranges::copy_if(container->children, std::back_inserter(movedItems), is_dragged);
|
||||
if (movedItems.empty()) return;
|
||||
|
||||
if (targetRow.isGroup && draggedGroupIds.contains(targetRow.id)) return;
|
||||
|
||||
auto row_index_get = [&](const TimelineItemRow& row)
|
||||
{
|
||||
for (int i = 0; i < (int)container->children.size(); ++i)
|
||||
{
|
||||
auto& item = container->children[i];
|
||||
if (row.isGroup && item.type == ElementType::GROUP && item.id == row.id) return i;
|
||||
if (!row.isGroup && row.type == targetType && item.type == targetTrackType &&
|
||||
anm2ed::track_id_get(item) == row.id)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
auto group_end_index_get = [&](int groupId)
|
||||
{
|
||||
int result = -1;
|
||||
for (int i = 0; i < (int)container->children.size(); ++i)
|
||||
{
|
||||
auto& item = container->children[i];
|
||||
if (item.type == ElementType::GROUP && item.id == groupId) result = std::max(result, i);
|
||||
if (item.type == targetTrackType && item.groupId == groupId) result = std::max(result, i);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
int targetIndex = (int)container->children.size();
|
||||
if (targetRow.isGroup || targetRow.type == targetType)
|
||||
{
|
||||
auto rowIndex = row_index_get(targetRow);
|
||||
if (rowIndex == -1) return;
|
||||
if (targetRow.isGroup)
|
||||
{
|
||||
auto groupEndIndex = group_end_index_get(targetRow.id);
|
||||
if (targetType == LAYER)
|
||||
targetIndex = isDropAfter ? rowIndex : groupEndIndex + 1;
|
||||
else
|
||||
targetIndex = rowIndex + isDropAfter;
|
||||
}
|
||||
else
|
||||
targetIndex = rowIndex + (targetType == LAYER ? !isDropAfter : isDropAfter);
|
||||
}
|
||||
|
||||
int removedBeforeTarget = 0;
|
||||
for (int i = (int)container->children.size() - 1; i >= 0; --i)
|
||||
{
|
||||
if (is_dragged(container->children[i]))
|
||||
{
|
||||
if (i < targetIndex) ++removedBeforeTarget;
|
||||
container->children.erase(container->children.begin() + i);
|
||||
}
|
||||
}
|
||||
|
||||
auto targetGroupId = -1;
|
||||
if (targetRow.isGroup && isDropIntoGroup)
|
||||
targetGroupId = targetRow.id;
|
||||
else if (targetRow.type == targetType)
|
||||
targetGroupId = targetRow.groupId;
|
||||
|
||||
for (auto& item : movedItems)
|
||||
if (item.type == targetTrackType && !draggedGroupIds.contains(item.groupId)) item.groupId = targetGroupId;
|
||||
|
||||
targetIndex -= removedBeforeTarget;
|
||||
targetIndex = std::clamp(targetIndex, 0, (int)container->children.size());
|
||||
container->children.insert(container->children.begin() + targetIndex, movedItems.begin(), movedItems.end());
|
||||
|
||||
document.items.references.clear();
|
||||
document.groupReferences.clear();
|
||||
for (const auto& draggedRow : draggedRows)
|
||||
{
|
||||
if (draggedRow.isGroup)
|
||||
document.groupReferences.insert({draggedRow.animationIndex, draggedRow.type, draggedRow.id});
|
||||
for (const auto& row : draggedRows)
|
||||
if (row.isGroup)
|
||||
document.groupReferences.insert({row.animationIndex, row.type, row.id});
|
||||
else
|
||||
document.items.references.insert(row_item_reference_get(draggedRow));
|
||||
}
|
||||
if (!document.items.references.empty())
|
||||
document.reference = *document.items.references.begin();
|
||||
else
|
||||
document.reference = {animationIndex};
|
||||
document.items.references.insert(row_item_reference_get(row));
|
||||
document.reference =
|
||||
document.items.references.empty() ? Reference{animationIndex} : *document.items.references.begin();
|
||||
frames_selection_reset_for(document);
|
||||
});
|
||||
}
|
||||
@@ -805,14 +236,8 @@ namespace anm2ed::imgui
|
||||
void TimelineContext::fit_animation_length()
|
||||
{
|
||||
if (!animation) return;
|
||||
auto animationIndex = reference.animationIndex;
|
||||
edit_command_push(EDIT_FIT_ANIMATION_LENGTH, Document::ANIMATIONS,
|
||||
[=, this](Manager&, Document& document)
|
||||
{
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, animationIndex);
|
||||
if (!animation) return;
|
||||
animation->frameNum = animation_length_get(*animation);
|
||||
});
|
||||
edit_push(EDIT_FIT_ANIMATION_LENGTH, Document::ANIMATIONS, [animationIndex = reference.animationIndex](Anm2& anm2)
|
||||
{ return edit::animation_length_fit(anm2, animationIndex); });
|
||||
}
|
||||
|
||||
void TimelineContext::frame_references_copy(const std::set<Reference>& selectedFrames)
|
||||
@@ -845,94 +270,34 @@ namespace anm2ed::imgui
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
if (selectedFrames.empty()) return;
|
||||
frame_references_copy(selectedFrames);
|
||||
auto trackReferences = track_references_from_frame_references_get(selectedFrames);
|
||||
edit_command_push(
|
||||
EDIT_CUT_FRAMES, Document::FRAMES, [=, this](Manager&, Document& document) mutable
|
||||
{ frames_delete_for(document, selectedFrames); }, SnapshotKind::TRACKS, trackReferences);
|
||||
edit_push(
|
||||
EDIT_CUT_FRAMES, Document::FRAMES, [=](Anm2& anm2) { return edit::frames_delete(anm2, selectedFrames); },
|
||||
[this](Document& document, const edit::Uids&) { frames_selection_set_reference_for(document); });
|
||||
}
|
||||
|
||||
void TimelineContext::paste()
|
||||
{
|
||||
auto targetReference = reference;
|
||||
auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE);
|
||||
auto clipboardString = clipboard.get();
|
||||
if (clipboardString.empty()) return;
|
||||
auto targetHoveredTime = hoveredTime;
|
||||
auto message = std::string(localize.get(EDIT_PASTE_FRAMES));
|
||||
command_push(
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
auto text = clipboard.get();
|
||||
if (text.empty()) return;
|
||||
if (!command_item_reference_get(document, reference))
|
||||
{
|
||||
toast_log(Level::WARNING, TOAST_DESERIALIZE_FRAMES_NO_SELECTION);
|
||||
return;
|
||||
}
|
||||
|
||||
auto errorString = std::make_shared<std::string>();
|
||||
edit_push(
|
||||
EDIT_PASTE_FRAMES, Document::FRAMES,
|
||||
[=, targetReference = reference,
|
||||
selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE),
|
||||
time = hoveredTime](Anm2& anm2)
|
||||
{ return edit::frames_paste(anm2, targetReference, selectedFrames, text, time, errorString.get()); },
|
||||
[=, this](Document& document, const edit::Uids& uids)
|
||||
{
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, targetReference.animationIndex);
|
||||
if (!animation) return;
|
||||
if (auto item = command_item_reference_get(document, targetReference))
|
||||
{
|
||||
document.snapshots.tracks_push(message, {targetReference});
|
||||
std::set<int> indices{};
|
||||
std::string errorString{};
|
||||
int insertIndex = (int)item->children.size();
|
||||
std::set<int> selectedIndices{};
|
||||
for (auto frameReference : selectedFrames)
|
||||
if (is_same_item(frameReference, targetReference)) selectedIndices.insert(frameReference.frameIndex);
|
||||
|
||||
if (!selectedIndices.empty())
|
||||
insertIndex = std::min((int)item->children.size(), *selectedIndices.rbegin() + 1);
|
||||
else if (targetReference.frameIndex >= 0 && targetReference.frameIndex < (int)item->children.size())
|
||||
insertIndex = targetReference.frameIndex + 1;
|
||||
|
||||
auto start = targetReference.itemType == TRIGGER ? targetHoveredTime : insertIndex;
|
||||
if (frames_deserialize(*item, clipboardString, start, indices, &errorString))
|
||||
{
|
||||
if (targetReference.itemType != LAYER)
|
||||
{
|
||||
for (auto i : indices)
|
||||
if (auto frame = track_frame_get(*item, i); frame)
|
||||
{
|
||||
frame->regionId = -1;
|
||||
frame->crop = {};
|
||||
frame->size = {};
|
||||
frame->pivot = {};
|
||||
}
|
||||
}
|
||||
else if (targetReference.itemID != -1)
|
||||
{
|
||||
auto layer = document.anm2.element_get(ElementType::LAYER_ELEMENT, targetReference.itemID);
|
||||
auto spritesheet =
|
||||
layer ? document.anm2.element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
|
||||
|
||||
for (auto i : indices)
|
||||
{
|
||||
auto frame = track_frame_get(*item, i);
|
||||
if (!frame || frame->regionId == -1) continue;
|
||||
auto region =
|
||||
spritesheet ? child_id_get(*spritesheet, ElementType::REGION, frame->regionId) : nullptr;
|
||||
if (!region) frame->regionId = -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::set<Reference> pastedSelection{};
|
||||
for (auto i : indices)
|
||||
{
|
||||
auto pastedReference = targetReference;
|
||||
pastedReference.frameIndex = i;
|
||||
pastedSelection.insert(pastedReference);
|
||||
}
|
||||
document.reference = targetReference;
|
||||
document.reference.frameIndex = *indices.begin();
|
||||
document.frame_references_set(std::move(pastedSelection));
|
||||
document.change(Document::FRAMES);
|
||||
}
|
||||
else
|
||||
{
|
||||
document.snapshots.pendingStep.reset();
|
||||
toasts.push(std::format("{} {}", localize.get(TOAST_DESERIALIZE_FRAMES_FAILED), errorString));
|
||||
logger.error(
|
||||
std::format("{} {}", localize.get(TOAST_DESERIALIZE_FRAMES_FAILED, anm2ed::ENGLISH), errorString));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
toast_log(Level::WARNING, TOAST_DESERIALIZE_FRAMES_NO_SELECTION);
|
||||
}
|
||||
if (errorString->empty()) return frames_select_for(document, uids);
|
||||
toasts.push(std::format("{} {}", localize.get(TOAST_DESERIALIZE_FRAMES_FAILED), *errorString));
|
||||
logger.error(
|
||||
std::format("{} {}", localize.get(TOAST_DESERIALIZE_FRAMES_FAILED, anm2ed::ENGLISH), *errorString));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user