From 97be81b5c1b4ba10a1da7ab3a663814358755148 Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 3 Oct 2026 21:11:09 -0400 Subject: [PATCH] Timeline: cut, copy and paste one frame property In the frames context menu, Cut/Copy/Paste each open onto Frame (the whole frame, as Ctrl+X/C/V) or Property, which lists the properties the frame type has. Copying a property copies the focused (or first selected) frame; pasting sets just that property on the selected frames, and is disabled when a target lacks it or what it refers to (region on the target's spritesheet, shader, event, sounds). Cutting resets it to the default. Actions gained submenus; edit::frames_property_set does the work (tested). Co-Authored-By: Claude Opus 5.5 --- src/edit/edit.hpp | 26 ++++++++ src/edit/frames.cpp | 86 +++++++++++++++++++++++++ src/imgui/actions.cpp | 18 +++++- src/imgui/actions.hpp | 1 + src/imgui/window/timeline/context.hpp | 7 ++ src/imgui/window/timeline/edit.cpp | 88 +++++++++++++++++++++++++- src/imgui/window/timeline/menus.cpp | 49 +++++++++++++- src/imgui/window/timeline/timeline.cpp | 5 +- src/imgui/window/timeline/timeline.hpp | 9 +++ src/resource/strings.hpp | 3 + tests/operations.cpp | 23 +++++++ 11 files changed, 306 insertions(+), 9 deletions(-) diff --git a/src/edit/edit.hpp b/src/edit/edit.hpp index fc9a99c..5a9f6eb 100644 --- a/src/edit/edit.hpp +++ b/src/edit/edit.hpp @@ -63,6 +63,32 @@ namespace anm2ed::edit bool isInto{}; }; + // Frame properties that can be copied on their own. + enum class FrameProperty + { + POSITION, + SCALE, + ROTATION, + SHEAR, + PIVOT, + CROP, + SIZE, + REGION, + TINT, + COLOR_OFFSET, + VISIBLE, + INTERPOLATION, + DURATION, + SHADER, + EVENT, + SOUNDS, + COUNT + }; + + bool is_frame_property_valid(FrameProperty, ItemType); + void frame_property_copy(model::Frame&, const model::Frame&, FrameProperty); + Uids frames_property_set(model::Model&, const std::set&, FrameProperty, const model::Frame&); + std::map> frames_by_track_get(const std::set&); Uids frame_insert(model::Model&, Reference, int); diff --git a/src/edit/frames.cpp b/src/edit/frames.cpp index db34a0f..fd379c6 100644 --- a/src/edit/frames.cpp +++ b/src/edit/frames.cpp @@ -197,6 +197,92 @@ namespace anm2ed::edit return frames_uids_get(destination, insertIndex, (int)moved.size()); } + // Triggers have an event and sounds; layer frames add a pivot, crop, size, region and shader to the others' transform. + bool is_frame_property_valid(FrameProperty property, ItemType type) + { + if (type == ItemType::TRIGGER) return property == FrameProperty::EVENT || property == FrameProperty::SOUNDS; + if (property == FrameProperty::EVENT || property == FrameProperty::SOUNDS) return false; + auto isLayerOnly = property == FrameProperty::PIVOT || property == FrameProperty::CROP || + property == FrameProperty::SIZE || property == FrameProperty::REGION || + property == FrameProperty::SHADER; + return type == ItemType::LAYER || !isLayerOnly; + } + + void frame_property_copy(Frame& target, const Frame& source, FrameProperty property) + { + switch (property) + { + case FrameProperty::POSITION: + target.position = source.position; + break; + case FrameProperty::SCALE: + target.scale = source.scale; + break; + case FrameProperty::ROTATION: + target.rotation = source.rotation; + break; + case FrameProperty::SHEAR: + target.shear = source.shear; + break; + case FrameProperty::PIVOT: + target.pivot = source.pivot; + break; + case FrameProperty::CROP: + target.crop = source.crop; + break; + case FrameProperty::SIZE: + target.size = source.size; + break; + case FrameProperty::REGION: + target.regionId = source.regionId; + target.crop = source.crop; + target.size = source.size; + target.pivot = source.pivot; + break; + case FrameProperty::TINT: + target.tint = source.tint; + break; + case FrameProperty::COLOR_OFFSET: + target.colorOffset = source.colorOffset; + break; + case FrameProperty::VISIBLE: + target.isVisible = source.isVisible; + break; + case FrameProperty::INTERPOLATION: + target.interpolation = source.interpolation; + break; + case FrameProperty::DURATION: + target.duration = source.duration; + break; + case FrameProperty::SHADER: + target.shaderId = source.shaderId; + break; + case FrameProperty::EVENT: + target.eventId = source.eventId; + break; + case FrameProperty::SOUNDS: + target.soundIds = source.soundIds; + break; + default: + break; + } + } + + // Sets one property of the frames that have it from a source frame (a default frame clears it). + Uids frames_property_set(Model& model, const std::set& references, FrameProperty property, + const Frame& source) + { + Uids uids{}; + for (auto reference : references) + if (is_frame_property_valid(property, (ItemType)reference.itemType)) + if (auto frame = model.frame_edit(reference)) + { + frame_property_copy(*frame, source, property); + uids.push_back(frame->uid); + } + return uids; + } + Uids frame_durations_set(Model& model, const std::map& durations) { for (auto [reference, duration] : durations) diff --git a/src/imgui/actions.cpp b/src/imgui/actions.cpp index 260caf8..6b68c56 100644 --- a/src/imgui/actions.cpp +++ b/src/imgui/actions.cpp @@ -55,10 +55,10 @@ namespace anm2ed::imgui { manager.command_push({manager.selected, [](Manager&, Document& document) { document.redo(); }}); }); } - void actions_menu_draw(Actions& actions, Settings& settings) + void action_items_menu_draw(std::vector& items, Settings& settings) { bool isPreviousSeparator{}; - for (auto& action : actions.items) + for (auto& action : items) { if (action.isSeparator) { @@ -66,6 +66,17 @@ namespace anm2ed::imgui isPreviousSeparator = true; continue; } + isPreviousSeparator = false; + + if (!action.children.empty()) + { + if (ImGui::BeginMenu(localize.get(action.label), is_action_enabled(action))) + { + action_items_menu_draw(action.children, settings); + ImGui::EndMenu(); + } + continue; + } auto shortcutText = action_shortcut_text_get(action, settings); auto shortcutLabel = shortcutText.empty() ? nullptr : shortcutText.c_str(); @@ -73,10 +84,11 @@ namespace anm2ed::imgui action.run(); if (action.tooltip != STRING_UNDEFINED && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) ImGui::SetItemTooltip("%s", localize.get(action.tooltip)); - isPreviousSeparator = false; } } + void actions_menu_draw(Actions& actions, Settings& settings) { action_items_menu_draw(actions.items, settings); } + bool actions_context_window_draw(const char* label, Actions& actions, Settings& settings, ImGuiPopupFlags flags, ImGuiHoveredFlags hoveredFlags) { diff --git a/src/imgui/actions.hpp b/src/imgui/actions.hpp index 9761c82..37ef719 100644 --- a/src/imgui/actions.hpp +++ b/src/imgui/actions.hpp @@ -69,6 +69,7 @@ namespace anm2ed::imgui bool isSeparator{}; std::function isEnabled{}; std::function run{}; + std::vector children{}; // a submenu, in menus }; struct Actions diff --git a/src/imgui/window/timeline/context.hpp b/src/imgui/window/timeline/context.hpp index 2bc1fac..7e2a102 100644 --- a/src/imgui/window/timeline/context.hpp +++ b/src/imgui/window/timeline/context.hpp @@ -242,6 +242,13 @@ namespace anm2ed::imgui void copy(); void cut(); void paste(); + std::optional property_source_get(); + std::set property_targets_get(); + std::optional property_clipboard_frame_get(); + bool is_property_pasteable(edit::FrameProperty); + void property_copy(edit::FrameProperty); + void property_cut(edit::FrameProperty); + void property_paste(edit::FrameProperty); void context_menu(); void item_base_properties_open(int type, int id); void group_properties_open(const TimelineRow& row, const model::TrackGroup& group); diff --git a/src/imgui/window/timeline/edit.cpp b/src/imgui/window/timeline/edit.cpp index 5c5fb0f..eb35f64 100644 --- a/src/imgui/window/timeline/edit.cpp +++ b/src/imgui/window/timeline/edit.cpp @@ -247,7 +247,9 @@ namespace anm2ed::imgui if (!frame) continue; clipboardString += model::frame_to_string(*frame, (ItemType)frameReference.itemType); } - if (!clipboardString.empty()) clipboard.set(clipboardString); + if (clipboardString.empty()) return; + clipboard.set(clipboardString); + frameClipboard = {.text = clipboardString, .itemType = selectedFrames.begin()->itemType}; } void TimelineContext::copy() @@ -319,4 +321,88 @@ namespace anm2ed::imgui animation->startMarker = animation->endMarker = -1; }); } + + // The frame a property is copied from: the focused frame, or else the first selected one. + std::optional TimelineContext::property_source_get() + { + if (reference.frameIndex >= 0 && model.frame_get(reference)) return reference; + auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE); + if (selectedFrames.empty()) return {}; + return *selectedFrames.begin(); + } + + // The frames a property is cut from or pasted into: the selected ones, or else the focused one. + std::set TimelineContext::property_targets_get() + { + auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE); + if (selectedFrames.empty()) + if (auto source = property_source_get()) selectedFrames.insert(*source); + return selectedFrames; + } + + // The frame this timeline put on the clipboard, while it is still there. + std::optional TimelineContext::property_clipboard_frame_get() + { + if (frameClipboard.text.empty() || clipboard.get() != frameClipboard.text) return {}; + auto frames = model::frames_from_string(frameClipboard.text, (ItemType)frameClipboard.itemType); + if (frames.empty()) return {}; + return frames.front(); + } + + // Every target frame has the property, and what it refers to (a region, shader, event, sound) exists for it. + bool TimelineContext::is_property_pasteable(edit::FrameProperty property) + { + if (frameClipboard.property != -1 && frameClipboard.property != (int)property) return false; + if (!edit::is_frame_property_valid(property, (ItemType)frameClipboard.itemType)) return false; + auto source = property_clipboard_frame_get(); + auto targets = property_targets_get(); + if (!source || targets.empty()) return false; + + auto& content = model.content; + for (auto target : targets) + { + if (!edit::is_frame_property_valid(property, (ItemType)target.itemType)) return false; + if (property == edit::FrameProperty::REGION && source->regionId != -1) + { + auto layer = model::item_get(content.layers, target.itemID); + auto spritesheet = layer ? model::item_get(content.spritesheets, layer->spritesheetId) : nullptr; + if (!spritesheet || !model::item_get(spritesheet->regions, source->regionId)) return false; + } + } + if (property == edit::FrameProperty::SHADER && source->shaderId != -1 && + !model::item_get(content.shaders, source->shaderId)) + return false; + if (property == edit::FrameProperty::EVENT && source->eventId != -1 && + !model::item_get(content.events, source->eventId)) + return false; + if (property == edit::FrameProperty::SOUNDS) + for (auto soundId : source->soundIds) + if (soundId != -1 && !model::item_get(content.sounds, soundId)) return false; + return true; + } + + void TimelineContext::property_copy(edit::FrameProperty property) + { + auto source = property_source_get(); + if (!source) return; + frame_references_copy({*source}); + frameClipboard.property = (int)property; + } + + // Copies the property, then sets it back to its default on the targets. + void TimelineContext::property_cut(edit::FrameProperty property) + { + auto targets = property_targets_get(); + property_copy(property); + edit_push(EDIT_CUT_FRAME_PROPERTY, + [=](model::Model& model) { return edit::frames_property_set(model, targets, property, model::Frame{}); }); + } + + void TimelineContext::property_paste(edit::FrameProperty property) + { + auto source = property_clipboard_frame_get(); + if (!source || !is_property_pasteable(property)) return; + edit_push(EDIT_PASTE_FRAME_PROPERTY, [=, targets = property_targets_get(), source = *source](model::Model& model) + { return edit::frames_property_set(model, targets, property, source); }); + } } diff --git a/src/imgui/window/timeline/menus.cpp b/src/imgui/window/timeline/menus.cpp index 7a1300d..1b5dd99 100644 --- a/src/imgui/window/timeline/menus.cpp +++ b/src/imgui/window/timeline/menus.cpp @@ -2,6 +2,12 @@ namespace anm2ed::imgui { + constexpr StringType FRAME_PROPERTY_LABELS[] = { + BASIC_POSITION, BASIC_SCALE, BASIC_ROTATION, BASIC_SHEAR, BASIC_PIVOT, BASIC_CROP, + BASIC_SIZE, BASIC_REGION, BASIC_TINT, BASIC_COLOR_OFFSET, BASIC_VISIBLE, BASIC_INTERPOLATED, + BASIC_DURATION, BASIC_SHADER, BASIC_EVENT, LABEL_SOUNDS}; + static_assert(std::size(FRAME_PROPERTY_LABELS) == (std::size_t)edit::FrameProperty::COUNT); + void TimelineContext::frame_begin() { iconTintDefault = isLightTheme ? ICON_TINT_DEFAULT_LIGHT : ICON_TINT_DEFAULT_DARK; @@ -150,9 +156,46 @@ namespace anm2ed::imgui .isEnabled = [=, this]() { return !selectedFrames.empty(); }, .run = [&]() { frames_delete_action(); }}); actions.separator(); - actions.add(ACTION_CUT, [=, this]() { return !selectedFrames.empty(); }, [this]() { cut(); }); - actions.add(ACTION_COPY, [=, this]() { return !copyFrames.empty(); }, [this]() { copy(); }); - actions.add(ACTION_PASTE, [&]() { return !clipboard.is_empty(); }, [this]() { paste(); }); + // Cut, copy and paste each open onto the whole frame, or one property of it. + auto source = property_source_get(); + auto sourceFrame = source ? model.frame_get(*source) : nullptr; + auto clipboardFrame = property_clipboard_frame_get(); + auto clipboard_menu_add = [&](ActionType type, std::function isFrameEnabled, std::function frameRun, + int propertyType, std::function is_property_enabled, + std::function property_run) + { + Action properties{.label = LABEL_PROPERTY}; + for (int i = 0; i < (int)edit::FrameProperty::COUNT; ++i) + { + auto property = (edit::FrameProperty)i; + if (!edit::is_frame_property_valid(property, (ItemType)propertyType)) continue; + properties.children.push_back({.label = FRAME_PROPERTY_LABELS[i], + .isEnabled = [=]() { return is_property_enabled(property); }, + .run = [=]() { property_run(property); }}); + } + properties.isEnabled = [children = properties.children]() + { return std::ranges::any_of(children, is_action_enabled); }; + actions.add({.label = ACTION_INFOS[type].label, + .children = {{.label = BASIC_FRAME, + .shortcut = ACTION_INFOS[type].shortcut, + .isEnabled = isFrameEnabled, + .run = frameRun}, + properties}}); + }; + auto sourceType = source ? source->itemType : NONE; + clipboard_menu_add( + ACTION_CUT, [=, this]() { return !selectedFrames.empty(); }, [this]() { cut(); }, sourceType, + [=](edit::FrameProperty) { return sourceFrame != nullptr; }, + [this](edit::FrameProperty property) { property_cut(property); }); + clipboard_menu_add( + ACTION_COPY, [=, this]() { return !copyFrames.empty(); }, [this]() { copy(); }, sourceType, + [=](edit::FrameProperty) { return sourceFrame != nullptr; }, + [this](edit::FrameProperty property) { property_copy(property); }); + clipboard_menu_add( + ACTION_PASTE, [&]() { return !clipboard.is_empty(); }, [this]() { paste(); }, + clipboardFrame ? frameClipboard.itemType : NONE, + [this](edit::FrameProperty property) { return is_property_pasteable(property); }, + [this](edit::FrameProperty property) { property_paste(property); }); // The numbered row has its own (marker) menu. if (isRulerHovered) actions_popup_draw("##Context Menu", actions, settings); diff --git a/src/imgui/window/timeline/timeline.cpp b/src/imgui/window/timeline/timeline.cpp index 6598829..d348623 100644 --- a/src/imgui/window/timeline/timeline.cpp +++ b/src/imgui/window/timeline/timeline.cpp @@ -34,8 +34,9 @@ namespace anm2ed::imgui void Timeline::update(Manager& manager, Settings& settings, Resources& resources, Clipboard& clipboard) { if (!manager.get()) return; - auto state = context ? std::move(static_cast(*context)) : TimelineState{}; - context = std::make_unique(std::move(state), manager, settings, resources, clipboard); + context = + std::make_unique(context ? std::move(static_cast(*context)) : TimelineState{}, + manager, settings, resources, clipboard); context->update(); } diff --git a/src/imgui/window/timeline/timeline.hpp b/src/imgui/window/timeline/timeline.hpp index e94022a..599af0c 100644 --- a/src/imgui/window/timeline/timeline.hpp +++ b/src/imgui/window/timeline/timeline.hpp @@ -97,6 +97,14 @@ namespace anm2ed::imgui bool isActive{}; }; + // What the timeline last put on the clipboard: frames of a type, or one property of a frame. + struct FrameClipboard + { + std::string text{}; + int itemType{NONE}; + int property{-1}; + }; + struct FrameDurationDrag { Reference reference{}; @@ -113,6 +121,7 @@ namespace anm2ed::imgui { bool isDragging{}; bool isRulerHovered{}; + FrameClipboard frameClipboard{}; int markerTime{}; bool isWindowHovered{}; bool isHorizontalScroll{}; diff --git a/src/resource/strings.hpp b/src/resource/strings.hpp index accfd91..d3452ef 100644 --- a/src/resource/strings.hpp +++ b/src/resource/strings.hpp @@ -118,7 +118,9 @@ namespace anm2ed X(EDIT_ADD_SOUND, "Add Sound", "Añadir Sonido", "Добавить звук", "添加声音", "사운드 추가") \ X(EDIT_ADD_SHADER, "Add Shader", "Add Shader", "Add Shader", "Add Shader", "Add Shader") \ X(EDIT_ADD_TRIGGER_SOUND, "Add Trigger Sound", "Añadir Sonido del Trigger", "Добавить звук триггера", "添加事件触发器声音", "트리거 사운드 추가") \ + X(EDIT_CUT_FRAME_PROPERTY, "Cut Frame Property", "Cortar Propiedad del Frame", "Вырезание свойства кадра", "剪切帧属性", "프레임 속성 잘라내기") \ X(EDIT_END_MARKER, "Set End Marker", "Poner Marcador de Fin", "Конечный маркер", "设置结束标记", "끝 마커 설정") \ + X(EDIT_PASTE_FRAME_PROPERTY, "Paste Frame Property", "Pegar Propiedad del Frame", "Вставка свойства кадра", "粘贴帧属性", "프레임 속성 붙여넣기") \ X(EDIT_REMOVE_MARKERS, "Remove Markers", "Quitar Marcadores", "Удаление маркеров", "移除标记", "마커 제거") \ X(EDIT_REMOVE_TRIGGER_SOUND, "Remove Trigger Sound", "Remover Sonido del Trigger", "Удалить звук триггера", "移除事件触发器声音", "트리거 사운드 제거") \ X(EDIT_ANIMATION_LENGTH, "Animation Length", "Duracion De Animacion", "Длина анимации", "动画时长", "애니메이션 길이") \ @@ -266,6 +268,7 @@ namespace anm2ed X(LABEL_ISAAC_COMPATIBILITY_WARNING, "Isaac Compatibility Warning", "Advertencia de Compatibilidad con Isaac", "Предупреждение о совместимости с Isaac", "Isaac 兼容性警告", "Isaac 호환성 경고") \ X(LABEL_ONLY_SELECTED_ITEM, "Only Selected Item", "Solo el Elemento Seleccionado", "Только выбранный элемент", "仅选中项目", "선택한 항목만") \ X(LABEL_PIVOT_PATH, "Pivot Path", "Trayectoria del Pivote", "Путь опорной точки", "枢轴路径", "피벗 경로") \ + X(LABEL_PROPERTY, "Property", "Propiedad", "Свойство", "属性", "속성") \ X(LABEL_REMOVE_MARKERS, "Remove Markers", "Quitar Marcadores", "Удалить маркеры", "移除标记", "마커 제거") \ X(LABEL_SAVE_ANYWAY, "Save Anyway", "Guardar de Todos Modos", "Всё равно сохранить", "仍然保存", "그래도 저장") \ X(LABEL_SPRITESHEETS_PACK_POPUP, "Pack Spritesheet", "Empaquetar spritesheet", "Упаковать спрайт-лист", "打包图集", "스프라이트 시트 패킹") \ diff --git a/tests/operations.cpp b/tests/operations.cpp index 04aaa60..63548c4 100644 --- a/tests/operations.cpp +++ b/tests/operations.cpp @@ -325,3 +325,26 @@ TEST_CASE("isaac_issues_get counts what the game's copy changes") CHECK(shaders.shearFrames > 0); CHECK(model::isaac_issues_get(fixture_load("02_items.anm2")).triggerSounds > 0); } + +TEST_CASE("frames_property_set copies one property to the frames that have it") +{ + auto model = fixture_load("02_items.anm2"); + model::Frame source{.crop = {7, 8}, .position = {3, 4}}; + Reference layer{0, LAYER, 0, 0}; + Reference null{0, NULL_, 0, 0}; + REQUIRE(model.frame_get(layer)); + REQUIRE(model.frame_get(null)); + auto nullCrop = model.frame_get(null)->crop; + + edit::frames_property_set(model, {layer, null}, edit::FrameProperty::POSITION, source); + CHECK(model.frame_get(layer)->position == glm::vec2(3, 4)); + CHECK(model.frame_get(null)->position == glm::vec2(3, 4)); + + edit::frames_property_set(model, {layer, null}, edit::FrameProperty::CROP, source); + CHECK(model.frame_get(layer)->crop == glm::vec2(7, 8)); + CHECK(model.frame_get(null)->crop == nullCrop); + + CHECK_FALSE(edit::is_frame_property_valid(edit::FrameProperty::EVENT, ItemType::LAYER)); + CHECK(edit::is_frame_property_valid(edit::FrameProperty::SOUNDS, ItemType::TRIGGER)); + CHECK_FALSE(edit::is_frame_property_valid(edit::FrameProperty::POSITION, ItemType::TRIGGER)); +}