From 6786bff07cd2c91740b3b7669f81857012b06636 Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 12 Jul 2026 16:37:35 -0400 Subject: [PATCH] copying items, remove region remapping --- src/anm2/anm2.cpp | 105 ++++++------- src/anm2/anm2.hpp | 1 - src/imgui/window/timeline.cpp | 277 ++++++++++++++++++++-------------- 3 files changed, 211 insertions(+), 172 deletions(-) diff --git a/src/anm2/anm2.cpp b/src/anm2/anm2.cpp index b8f804b..8c13252 100644 --- a/src/anm2/anm2.cpp +++ b/src/anm2/anm2.cpp @@ -101,6 +101,7 @@ namespace anm2ed void group_metadata_extract(Element&, Flags); void groups_flatten(Element&); void group_frames_restore(Element&); + void region_frame_ids_repair(Element&); Element* child_first_get(Element&, ElementType); const Element* child_first_get(const Element&, ElementType); bool is_track(const Element&); @@ -159,6 +160,7 @@ namespace anm2ed group_metadata_embed(anm2.root); group_frames_restore(anm2.root); groups_flatten(anm2.root); + region_frame_ids_repair(anm2.root); anm2.region_frames_sync(true); anm2.isValid = true; return true; @@ -1583,6 +1585,54 @@ namespace anm2ed return nullptr; } + void region_frame_ids_repair(Element& root) + { + auto content = child_first_get(root, ElementType::CONTENT); + auto layers = content ? child_first_get(*content, ElementType::LAYERS) : nullptr; + auto spritesheets = content ? child_first_get(*content, ElementType::SPRITESHEETS) : nullptr; + auto animations = element_first_get(root, ElementType::ANIMATIONS); + if (!layers || !spritesheets || !animations) return; + + for (auto& animation : animations->children) + { + if (animation.type != ElementType::ANIMATION) continue; + auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); + if (!layerAnimations) continue; + + tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, [&](Element& layerAnimation) + { + auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, layerAnimation.layerId); + auto spritesheet = + layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET, layer->spritesheetId) : nullptr; + if (!spritesheet) return; + + for (auto& frame : layerAnimation.children) + { + if (frame.type != ElementType::FRAME) continue; + + auto frameCrop = glm::ivec2(frame.crop); + auto frameSize = glm::ivec2(frame.size); + auto framePivot = glm::ivec2(frame.pivot); + auto is_region_match = [&](const Element& region) + { + return region.type == ElementType::REGION && glm::ivec2(region.crop) == frameCrop && + glm::ivec2(region.size) == frameSize && glm::ivec2(region.pivot) == framePivot; + }; + + auto region = frame.regionId == -1 ? nullptr : child_id_get(*spritesheet, ElementType::REGION, frame.regionId); + if (region && is_region_match(*region)) continue; + + for (const auto& candidate : spritesheet->children) + if (is_region_match(candidate)) + { + frame.regionId = candidate.id; + break; + } + } + }); + } + } + bool is_track_group_visible(const Element& container, const Element& track) { if (track.groupId == -1) return true; @@ -2214,60 +2264,6 @@ namespace anm2ed } } - void Anm2::region_ids_remap() - { - auto content = child_first_get(root, ElementType::CONTENT); - auto spritesheets = content ? child_first_get(*content, ElementType::SPRITESHEETS) : nullptr; - if (!spritesheets) return; - - std::map> remaps{}; - for (auto& spritesheet : spritesheets->children) - { - if (spritesheet.type != ElementType::SPRITESHEET) continue; - - int nextId{}; - std::map remap{}; - for (auto& region : spritesheet.children) - { - if (region.type != ElementType::REGION) continue; - remap[region.id] = nextId; - if (region.id != nextId) - { - region.id = nextId; - } - ++nextId; - } - if (!remap.empty()) remaps[spritesheet.id] = std::move(remap); - } - - auto layers = content ? child_first_get(*content, ElementType::LAYERS) : nullptr; - auto animations = element_first_get(root, ElementType::ANIMATIONS); - if (remaps.empty() || !layers || !animations) return; - - for (auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layerAnimations) continue; - - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, [&](Element& layerAnimation) - { - auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, layerAnimation.layerId); - auto remapIt = layer ? remaps.find(layer->spritesheetId) : remaps.end(); - if (remapIt == remaps.end()) return; - - for (auto& frame : layerAnimation.children) - { - if (frame.type != ElementType::FRAME || frame.regionId == -1) continue; - auto regionIt = remapIt->second.find(frame.regionId); - if (regionIt == remapIt->second.end()) continue; - if (frame.regionId == regionIt->second) continue; - frame.regionId = regionIt->second; - } - }); - } - } - Anm2 Anm2::normalized_for_serialize(Flags flags) const { auto normalized = *this; @@ -2291,7 +2287,6 @@ namespace anm2ed layer_animation_ids_remap(normalized.root, remap); } - normalized.region_ids_remap(); return normalized; } diff --git a/src/anm2/anm2.hpp b/src/anm2/anm2.hpp index 84dbc18..27ea07c 100644 --- a/src/anm2/anm2.hpp +++ b/src/anm2/anm2.hpp @@ -352,7 +352,6 @@ namespace anm2ed bool is_special_interpolated_frames() const; void special_interpolated_frames_bake(int, bool, bool); void region_frames_sync(bool); - void region_ids_remap(); Anm2 normalized_for_serialize(Flags = SERIALIZE_DEFAULT) const; Element* element_get(ElementType); diff --git a/src/imgui/window/timeline.cpp b/src/imgui/window/timeline.cpp index 9b75e37..f2c188e 100644 --- a/src/imgui/window/timeline.cpp +++ b/src/imgui/window/timeline.cpp @@ -381,6 +381,31 @@ namespace anm2ed::imgui }; auto all_frame_references_for_items_get = [&]() { return document.selected_item_frame_references_get(); }; + auto is_frame_copy_item = [](const Reference& itemReference) + { return itemReference.itemType == ROOT || itemReference.itemType == LAYER || itemReference.itemType == NULL_; }; + auto copy_frame_references_get = [&]() + { + auto selectedFrames = frame_references_for_current_get(); + if (!selectedFrames.empty()) return selectedFrames; + + auto selectedItems = document.items.references; + if (selectedItems.empty() && reference.itemType != NONE && reference.frameIndex < 0) + { + auto itemReference = reference; + itemReference.frameIndex = -1; + selectedItems.insert(itemReference); + } + + std::set result{}; + for (auto itemReference : selectedItems) + { + itemReference.frameIndex = -1; + if (!is_frame_copy_item(itemReference)) continue; + auto itemFrames = document.item_frame_references_get(itemReference); + result.insert(itemFrames.begin(), itemFrames.end()); + } + return result; + }; auto frames_selection_reset_for = [this](Document& targetDocument) { targetDocument.frame_references_clear(); @@ -1769,125 +1794,130 @@ namespace anm2ed::imgui }); }; + auto frame_references_copy = [&](const std::set& selectedFrames) + { + if (!animation || selectedFrames.empty()) return; + + std::string clipboardString{}; + for (auto frameReference : selectedFrames) + { + auto item = item_get(frameReference.itemType, frameReference.itemID, frameReference.groupType, + frameReference.groupId); + auto frame = item ? track_frame_get(*item, frameReference.frameIndex) : nullptr; + if (!frame) continue; + auto parentType = item_type_to_track_type_get(item_type_get(frameReference.itemType)); + clipboardString += element_to_string(*frame, parentType); + } + if (!clipboardString.empty()) clipboard.set(clipboardString); + }; + + auto copy = [&]() + { + auto selectedFrames = copy_frame_references_get(); + if (selectedFrames.empty()) return; + + frame_references_copy(selectedFrames); + }; + + auto cut = [&]() + { + auto selectedFrames = frame_references_for_current_get(); + if (selectedFrames.empty()) return; + frame_references_copy(selectedFrames); + edit_command_push(EDIT_CUT_FRAMES, Document::FRAMES, + [=](Manager&, Document& document) mutable + { frames_delete_for(document, selectedFrames); }); + }; + + auto paste = [&]() + { + auto targetReference = reference; + auto selectedFrames = frame_references_for_current_get(); + auto clipboardString = clipboard.get(); + if (clipboardString.empty()) return; + auto targetHoveredTime = hoveredTime; + auto message = std::string(localize.get(EDIT_PASTE_FRAMES)); + command_push([=](Manager&, Document& document) mutable + { + document.anm2_snapshot(message); + auto animation = command_animation_get(document, targetReference.animationIndex); + if (!animation) return; + if (auto item = command_item_reference_get(document, targetReference)) + { + std::set indices{}; + std::string errorString{}; + int insertIndex = (int)item->children.size(); + std::set 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 = command_layer_get(document, targetReference.itemID); + auto spritesheet = layer ? command_spritesheet_get(document, layer->spritesheetId) : nullptr; + + for (auto i : indices) + { + auto frame = track_frame_get(*item, i); + if (!frame || frame->regionId == -1) continue; + auto region = + spritesheet ? element_child_id_get(*spritesheet, ElementType::REGION, frame->regionId) + : nullptr; + if (!region) frame->regionId = -1; + } + } + + std::set pastedSelection{}; + for (auto i : indices) + pastedSelection.insert( + {targetReference.animationIndex, targetReference.itemType, targetReference.itemID, i}); + document.reference = {targetReference.animationIndex, targetReference.itemType, + targetReference.itemID, *indices.begin()}; + document.frame_references_set(std::move(pastedSelection)); + document.anm2_change(Document::FRAMES); + } + else + { + 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 + { + toasts.push(localize.get(TOAST_DESERIALIZE_FRAMES_NO_SELECTION)); + logger.warning(localize.get(TOAST_DESERIALIZE_FRAMES_NO_SELECTION, anm2ed::ENGLISH)); + } + }); + }; + auto context_menu = [&]() { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, style.WindowPadding); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing); - auto copy = [&]() - { - if (!animation) return; - auto selectedFrames = frame_references_for_current_get(); - if (selectedFrames.empty()) return; - - std::string clipboardString{}; - for (auto frameReference : selectedFrames) - { - auto item = item_get(frameReference.itemType, frameReference.itemID, frameReference.groupType, - frameReference.groupId); - auto frame = item ? track_frame_get(*item, frameReference.frameIndex) : nullptr; - if (!frame) continue; - auto parentType = item_type_to_track_type_get(item_type_get(frameReference.itemType)); - clipboardString += element_to_string(*frame, parentType); - } - if (!clipboardString.empty()) clipboard.set(clipboardString); - }; - - auto cut = [&]() - { - copy(); - auto selectedFrames = frame_references_for_current_get(); - edit_command_push(EDIT_CUT_FRAMES, Document::FRAMES, - [=](Manager&, Document& document) mutable - { frames_delete_for(document, selectedFrames); }); - }; - - auto paste = [&]() - { - auto targetReference = reference; - auto selectedFrames = frame_references_for_current_get(); - auto clipboardString = clipboard.get(); - if (clipboardString.empty()) return; - auto targetHoveredTime = hoveredTime; - auto message = std::string(localize.get(EDIT_PASTE_FRAMES)); - command_push([=](Manager&, Document& document) mutable - { - document.anm2_snapshot(message); - auto animation = command_animation_get(document, targetReference.animationIndex); - if (!animation) return; - if (auto item = command_item_reference_get(document, targetReference)) - { - std::set indices{}; - std::string errorString{}; - int insertIndex = (int)item->children.size(); - std::set 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 = command_layer_get(document, targetReference.itemID); - auto spritesheet = - layer ? command_spritesheet_get(document, layer->spritesheetId) : nullptr; - - for (auto i : indices) - { - auto frame = track_frame_get(*item, i); - if (!frame || frame->regionId == -1) continue; - auto region = spritesheet ? element_child_id_get(*spritesheet, ElementType::REGION, - frame->regionId) - : nullptr; - if (!region) frame->regionId = -1; - } - } - - std::set pastedSelection{}; - for (auto i : indices) - pastedSelection.insert( - {targetReference.animationIndex, targetReference.itemType, targetReference.itemID, i}); - document.reference = {targetReference.animationIndex, targetReference.itemType, - targetReference.itemID, *indices.begin()}; - document.frame_references_set(std::move(pastedSelection)); - document.anm2_change(Document::FRAMES); - } - else - { - 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 - { - toasts.push(localize.get(TOAST_DESERIALIZE_FRAMES_NO_SELECTION)); - logger.warning(localize.get(TOAST_DESERIALIZE_FRAMES_NO_SELECTION, anm2ed::ENGLISH)); - } - }); - }; - if (shortcut(manager.chords[SHORTCUT_CUT], shortcut::FOCUSED)) cut(); if (shortcut(manager.chords[SHORTCUT_COPY], shortcut::FOCUSED)) copy(); if (shortcut(manager.chords[SHORTCUT_PASTE], shortcut::FOCUSED)) paste(); @@ -1954,6 +1984,7 @@ namespace anm2ed::imgui auto item = selected_item_get(); auto frame = frame_get(); auto selectedFrames = frame_references_for_current_get(); + auto copyFrames = copy_frame_references_get(); auto selectedBakeFrames = selectedFrames; std::erase_if(selectedBakeFrames, [](const Reference& frameReference) { return frameReference.itemType == TRIGGER; }); @@ -2021,7 +2052,7 @@ namespace anm2ed::imgui .run = [&]() { frames_delete_action(); }}); actions.separator(); actions.add(ACTION_CUT, [=]() { return !selectedFrames.empty(); }, cut); - actions.add(ACTION_COPY, [=]() { return !selectedFrames.empty(); }, copy); + actions.add(ACTION_COPY, [=]() { return !copyFrames.empty(); }, copy); actions.add(ACTION_PASTE, [&]() { return !clipboard.is_empty(); }, paste); actions_context_window_draw("##Context Menu", actions, settings); @@ -2118,9 +2149,9 @@ namespace anm2ed::imgui auto& type = reference.itemType; auto& id = reference.itemID; auto item = selected_item_get(); - auto selectedItems = item_references_for_current_get(); auto selectedRows = selected_row_references_get(); auto selectedGroupableItems = item_references_groupable_get(); + auto copyFrames = copy_frame_references_get(); TimelineItemRow selectedGroupRow{}; Element* selectedGroup{}; if (selectedRows.size() == 1 && selectedRows.front().isGroup) @@ -2131,6 +2162,12 @@ namespace anm2ed::imgui } auto isRemoveAvailable = std::ranges::any_of(selectedRows, [](const TimelineRowReference& row) { return row.type == LAYER || row.type == NULL_; }); + auto item_cut = [&]() + { + if (copyFrames.empty() || !isRemoveAvailable) return; + frame_references_copy(copyFrames); + item_remove(); + }; if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlappedByWindow) && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) @@ -2150,6 +2187,14 @@ namespace anm2ed::imgui actions.add(ACTION_ADD, [&]() { return animation; }, [&]() { itemProperties.open(); }); actions.add(ACTION_REMOVE, [=]() { return isRemoveAvailable; }, [&]() { item_remove(); }); actions.add(ACTION_GROUP, [=]() { return !selectedGroupableItems.empty(); }, [&]() { item_group(); }); + actions.separator(); + actions.add(ACTION_CUT, [=]() { return !copyFrames.empty() && isRemoveAvailable; }, item_cut); + actions.add(ACTION_COPY, [=]() { return !copyFrames.empty(); }, copy); + actions.add(ACTION_PASTE, [&]() { return item && !clipboard.is_empty(); }, paste); + if (shortcut(manager.chords[SHORTCUT_CUT], shortcut::FOCUSED) && !copyFrames.empty() && isRemoveAvailable) + item_cut(); + if (shortcut(manager.chords[SHORTCUT_COPY], shortcut::FOCUSED) && !copyFrames.empty()) copy(); + if (shortcut(manager.chords[SHORTCUT_PASTE], shortcut::FOCUSED) && item && !clipboard.is_empty()) paste(); actions_popup_draw("##Items Context Menu", actions, settings); ImGui::PopStyleVar(2);