From 65a3546c3b8e559fc7a787c6b9fed59e5d481f17 Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 3 Oct 2026 15:46:47 -0400 Subject: [PATCH] Stage 4: one uid-based Selection for the timeline and animations list - Selection { map> uids; Handle focus; } (src/selection.*, core) replaces Document::reference, the frames/items/animation Storages, groupReferences, and the timeline's selection snapshot/lock/focus state. - Because selection is by uid it follows elements through edits by itself; after an edit or undo, selection_follow only drops removed uids and moves a removed focus to its nearest survivor. The Stage 2 handle capture/re-resolve, frames_reference_normalize_for, frames_selection_sync_for and the index-based frame validity repair are gone. - Document keeps a UidIndex (rebuilt on every change) and exposes reference_get/reference_set, selected_get/selected_set(kind), frame_references_get/set and animations_selected_get/set. - UidIndex records each uid's element type. - The animations list keeps its ImGui multi-select Storage as a per-frame mirror of the Selection; content windows are unchanged until Stage 8. - tests/selection.cpp and tests/undo.cpp cover selection through inserts, reverses, deletes, item and animation removal, regrouping and undo. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 2 +- src/anm2/anm2.hpp | 16 +- src/anm2/handle.cpp | 45 ++--- src/document.cpp | 139 ++++++------- src/document.hpp | 11 +- src/imgui/popup/item_properties.cpp | 4 +- src/imgui/taskbar.cpp | 19 +- src/imgui/window/animation_preview.cpp | 6 +- src/imgui/window/animations.cpp | 106 ++++++---- src/imgui/window/frame_properties.cpp | 12 +- src/imgui/window/regions.cpp | 10 +- src/imgui/window/spritesheet_editor.cpp | 4 +- src/imgui/window/timeline/context.hpp | 7 +- src/imgui/window/timeline/edit.cpp | 41 ++-- src/imgui/window/timeline/frames.cpp | 37 +--- src/imgui/window/timeline/menus.cpp | 1 - src/imgui/window/timeline/popups.cpp | 6 +- src/imgui/window/timeline/selection.cpp | 184 ++++++------------ src/imgui/window/timeline/timeline.cpp | 2 +- src/imgui/window/timeline/timeline.hpp | 6 - .../wizard/change_all_frame_properties.cpp | 16 +- .../wizard/generate_animation_from_grid.cpp | 2 +- src/imgui/wizard/render_animation.cpp | 8 +- src/selection.cpp | 72 +++++++ src/selection.hpp | 34 ++++ src/snapshots.cpp | 117 ++--------- src/snapshots.hpp | 28 +-- tests/selection.cpp | 137 ++++++------- tests/undo.cpp | 20 +- 29 files changed, 484 insertions(+), 608 deletions(-) create mode 100644 src/selection.cpp create mode 100644 src/selection.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2dd9033..a1452e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,7 +110,7 @@ file(GLOB PROJECT_SRC CONFIGURE_DEPENDS set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/anm2/*.cpp ${CORE_DIR}/edit/*.cpp) foreach(CORE_FILE log resource/strings resource/image resource/audio_data util/file util/path util/math util/string util/time util/working_directory - util/xml util/pack util/imgui/multiselect snapshots storage playback) + util/xml util/pack util/imgui/multiselect snapshots storage playback selection) list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp) endforeach() list(REMOVE_ITEM PROJECT_SRC ${CORE_SRC}) diff --git a/src/anm2/anm2.hpp b/src/anm2/anm2.hpp index 9fefb5f..a7f637a 100644 --- a/src/anm2/anm2.hpp +++ b/src/anm2/anm2.hpp @@ -589,14 +589,24 @@ namespace anm2ed Handle handle_get(Reference, bool = false) const; }; - // uid -> current Reference, built in one walk; group uids map to {animation, groupType, groupId}. + // uid -> where that element is now (as a Reference) and what it is, built in one walk of the animations. class UidIndex { - std::unordered_map locations{}; + public: + struct Location + { + Reference reference{}; + ElementType type{}; + }; + + private: + std::unordered_map locations{}; public: + UidIndex() = default; explicit UidIndex(const Anm2&); - std::optional reference_get(Handle, Reference) const; + bool contains(std::uint64_t) const; std::optional reference_get(std::uint64_t) const; + ElementType type_get(std::uint64_t) const; }; } diff --git a/src/anm2/handle.cpp b/src/anm2/handle.cpp index 08ad944..99f7e32 100644 --- a/src/anm2/handle.cpp +++ b/src/anm2/handle.cpp @@ -35,16 +35,17 @@ namespace anm2ed return handle; } - void track_locations_add(std::unordered_map& locations, const Element& track, - Reference reference) + using Locations = std::unordered_map; + + void track_locations_add(Locations& locations, const Element& track, Reference reference) { - locations[track.uid] = reference; + locations[track.uid] = {reference, track.type}; auto frameType = track_frame_type_get(track); for (const auto& frame : track.children) if (frame.type == frameType) { ++reference.frameIndex; - locations[frame.uid] = reference; + locations[frame.uid] = {reference, frame.type}; } } @@ -57,7 +58,7 @@ namespace anm2ed for (const auto& animation : animations->children) { if (animation.type != ElementType::ANIMATION) continue; - locations[animation.uid] = {animationIndex}; + locations[animation.uid] = {{animationIndex}, animation.type}; for (const auto& child : animation.children) { @@ -73,7 +74,7 @@ namespace anm2ed for (const auto& track : parent.children) if (track.type == ElementType::GROUP) { - locations[track.uid] = {animationIndex, type, track.id}; + locations[track.uid] = {{animationIndex, type, track.id}, track.type}; self(track, track.id); } else if (track.type == ElementType::ROOT_ANIMATION && groupId != -1) @@ -92,37 +93,17 @@ namespace anm2ed } } + bool UidIndex::contains(std::uint64_t uid) const { return uid && locations.contains(uid); } + std::optional UidIndex::reference_get(std::uint64_t uid) const { auto it = locations.find(uid); - return it == locations.end() ? std::nullopt : std::optional(it->second); + return it == locations.end() ? std::nullopt : std::optional(it->second.reference); } - std::optional UidIndex::reference_get(Handle handle, Reference reference) const + ElementType UidIndex::type_get(std::uint64_t uid) const { - auto location_get = [&](std::uint64_t uid) -> const Reference* - { - auto it = locations.find(uid); - return it == locations.end() ? nullptr : &it->second; - }; - - // Resolve the most specific element captured; if it no longer exists, neither does the reference. - auto uid = handle.frame ? handle.frame : handle.item ? handle.item : handle.animation; - if (!uid) return reference; - auto location = location_get(uid); - if (!location) return std::nullopt; - - auto isGroupAgnostic = reference.groupId == -1 && reference.itemType != ROOT; - if (uid == handle.animation) - reference.animationIndex = location->animationIndex; - else - reference = *location; - // References made without group context stay that way, wherever the track now lives. - if (isGroupAgnostic && reference.itemType != ROOT) - { - reference.groupType = NONE; - reference.groupId = -1; - } - return reference; + auto it = locations.find(uid); + return it == locations.end() ? ElementType::UNKNOWN : it->second.type; } } diff --git a/src/document.cpp b/src/document.cpp index eb20e34..3269078 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -173,37 +173,17 @@ namespace anm2ed::document return static_cast(seed); } - bool is_reference_item_matched(const Reference& left, const Reference& right) - { - return left.animationIndex == right.animationIndex && left.itemType == right.itemType && - left.itemID == right.itemID && left.groupType == right.groupType && left.groupId == right.groupId; - } - Reference item_reference_get(Reference reference) { reference.frameIndex = -1; return reference; } - auto frame_reference_validator_make(const Anm2& anm2) - { - return [&anm2, frameCounts = std::map{}](const Reference& frameReference) mutable - { - if (frameReference.itemType == NONE || frameReference.frameIndex < 0) return false; - auto itemReference = item_reference_get(frameReference); - if (!frameCounts.contains(itemReference)) - { - auto item = anm2.element_get(itemReference); - frameCounts[itemReference] = item ? track_frames_count_get(*item) : 0; - } - return frameReference.frameIndex < frameCounts[itemReference]; - }; - } - void frame_time_sync(Document& document) { - auto item = document.anm2.element_get(item_reference_get(document.reference)); - auto frameIndex = document.reference.frameIndex; + auto reference = document.reference_get(); + auto item = document.anm2.element_get(item_reference_get(reference)); + auto frameIndex = reference.frameIndex; document.frameTime = item && frameIndex >= 0 ? frame_time_from_index_get(*item, frameIndex) : 0.0f; } } @@ -729,6 +709,7 @@ namespace anm2ed } snapshots.commit(); + index = UidIndex(anm2); } void Document::edit_begin(StringType label, bool isTextures) { snapshots.push(localize.get(label), isTextures); } @@ -793,9 +774,34 @@ namespace anm2ed if (command.run) command.run(manager, *this); } - bool Document::is_frame_reference_valid(Reference frameReference) const + Reference Document::reference_get() const { return selection_focus_get(selection, index); } + + void Document::reference_set(Reference reference) { selection_focus_set(selection, anm2, reference); } + + std::set Document::selected_get(SelectionKind kind) const { - return document::frame_reference_validator_make(anm2)(frameReference); + return selection_references_get(selection, index, kind); + } + + void Document::selected_set(SelectionKind kind, const std::set& references) + { + selection_references_set(selection, anm2, kind, references); + } + + std::set Document::animations_selected_get() const + { + std::set indices{}; + for (auto reference : selected_get(SelectionKind::ANIMATIONS)) + indices.insert(reference.animationIndex); + return indices; + } + + void Document::animations_selected_set(const std::set& indices) + { + std::set references{}; + for (auto index : indices) + references.insert({index}); + selected_set(SelectionKind::ANIMATIONS, references); } std::set Document::item_frame_references_get(Reference itemReference) const @@ -813,8 +819,8 @@ namespace anm2ed std::set Document::selected_item_frame_references_get() const { - auto selectedItems = items.references; - if (selectedItems.empty() && reference.itemType != NONE) + auto selectedItems = selected_get(SelectionKind::TRACKS); + if (auto reference = reference_get(); selectedItems.empty() && reference.itemType != NONE) selectedItems.insert(document::item_reference_get(reference)); std::set result{}; @@ -825,77 +831,39 @@ namespace anm2ed std::set Document::frame_references_get(FrameReferenceFallback fallback) const { - auto result = frames.references; - for (auto frameIndex : frames.selection) - { - auto frameReference = reference; - frameReference.frameIndex = frameIndex; - result.insert(frameReference); - } - - auto isValid = document::frame_reference_validator_make(anm2); - auto is_invalid = [&](const Reference& frameReference) { return !isValid(frameReference); }; - bool isMultiFrameSelection = frames.references.size() > 1 || frames.selection.size() > 1; - std::erase_if(result, is_invalid); - - if (isMultiFrameSelection && result.size() <= 1) - { - auto itemFrames = selected_item_frame_references_get(); - std::erase_if(itemFrames, is_invalid); - if (itemFrames.size() > result.size()) result = std::move(itemFrames); - } - - if (result.empty() && fallback == FrameReferenceFallback::CURRENT && isValid(reference)) result.insert(reference); + auto result = selected_get(SelectionKind::FRAMES); + auto reference = reference_get(); + if (result.empty() && fallback == FrameReferenceFallback::CURRENT && reference.frameIndex >= 0) + result.insert(reference); return result; } + // Selects frames together with their tracks; the focus moves to the first frame unless it is already selected. void Document::frame_references_set(std::set frameReferences) { - auto isValid = document::frame_reference_validator_make(anm2); - std::erase_if(frameReferences, [&](const Reference& frameReference) { return !isValid(frameReference); }); - - frames.references = std::move(frameReferences); - frames.selection.clear(); - items.references.clear(); - if (frames.references.empty()) return; - - if (!frames.references.contains(reference) || !isValid(reference)) reference = *frames.references.begin(); - - for (auto frameReference : frames.references) - { - items.references.insert(document::item_reference_get(frameReference)); - if (document::is_reference_item_matched(frameReference, reference) && frameReference.frameIndex >= 0) - frames.selection.insert(frameReference.frameIndex); - } + std::erase_if(frameReferences, + [&](const Reference& frame) { return frame.frameIndex < 0 || !anm2.element_get(frame); }); + std::set tracks{}; + for (auto frame : frameReferences) + tracks.insert(document::item_reference_get(frame)); + selected_set(SelectionKind::FRAMES, frameReferences); + selected_set(SelectionKind::TRACKS, tracks); + if (!frameReferences.empty() && !frameReferences.contains(reference_get())) reference_set(*frameReferences.begin()); } - void Document::frame_references_clear() - { - frames.references.clear(); - frames.selection.clear(); - } + void Document::frame_references_clear() { selection.uids[SelectionKind::FRAMES].clear(); } std::vector Document::layer_references_get() { - std::set selectedReferences = items.references; - if (selectedReferences.empty()) - for (auto frameReference : frame_references_get(FrameReferenceFallback::NONE)) - selectedReferences.insert(document::item_reference_get(frameReference)); + auto reference = reference_get(); + std::set selectedReferences = selected_get(SelectionKind::TRACKS); if (selectedReferences.empty() && reference.itemType != NONE) selectedReferences.insert(document::item_reference_get(reference)); std::vector result{}; for (auto itemReference : selectedReferences) { - itemReference.frameIndex = -1; if (itemReference.itemType != LAYER) return {}; - if (!anm2.element_get(itemReference)) - { - auto targetReference = itemReference; - targetReference.animationIndex = reference.animationIndex; - if (reference.animationIndex == itemReference.animationIndex || !anm2.element_get(targetReference)) return {}; - itemReference = targetReference; - } result.push_back(itemReference); } return result; @@ -908,9 +876,12 @@ namespace anm2ed return regions == regionBySpritesheet.end() ? nullptr : ®ions->second; } - Element* Document::frame_get() { return anm2.element_get(reference); } - Element* Document::item_get() { return anm2.element_get(document::item_reference_get(reference)); } - Element* Document::animation_get() { return anm2.element_get(ElementType::ANIMATION, reference.animationIndex); } + Element* Document::frame_get() + { + auto reference = reference_get(); + return reference.frameIndex < 0 ? nullptr : anm2.element_get(reference); + } + Element* Document::item_get() { return anm2.element_get(document::item_reference_get(reference_get())); } Element* Document::spritesheet_get() { return anm2.element_get(ElementType::SPRITESHEET, spritesheet.reference); } void Document::spritesheets_add(const std::vector& paths) diff --git a/src/document.hpp b/src/document.hpp index 22eef0f..0e08271 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -10,6 +10,7 @@ #include #include "edit/edit.hpp" +#include "selection.hpp" #include "snapshots.hpp" #include "strings.hpp" @@ -30,6 +31,7 @@ namespace anm2ed uint64_t tabId{}; Snapshots snapshots{}; std::map regionBySpritesheet{}; + Storage animation{}; int changeAllFramePropertiesRegionId{-1}; int changeAllFramePropertiesShaderId{-1}; @@ -100,6 +102,7 @@ namespace anm2ed Anm2& anm2 = current.anm2; std::string& message = current.message; EditTarget editTarget{EditTarget::NONE}; + UidIndex index{}; Document(const std::filesystem::path&, bool = false, std::string* = nullptr); Document(const Document&) = delete; @@ -152,7 +155,12 @@ namespace anm2ed bool spritesheet_any_dirty(); void spritesheet_hashes_reset(); void spritesheet_hashes_sync(); - bool is_frame_reference_valid(Reference) const; + Reference reference_get() const; + void reference_set(Reference); + std::set selected_get(SelectionKind) const; + void selected_set(SelectionKind, const std::set&); + std::set animations_selected_get() const; + void animations_selected_set(const std::set&); std::set item_frame_references_get(Reference) const; std::set selected_item_frame_references_get() const; std::set frame_references_get(FrameReferenceFallback = FrameReferenceFallback::CURRENT) const; @@ -164,7 +172,6 @@ namespace anm2ed Element* frame_get(); Element* item_get(); Element* spritesheet_get(); - Element* animation_get(); void spritesheets_add(const std::vector&); void sounds_add(const std::vector&); diff --git a/src/imgui/popup/item_properties.cpp b/src/imgui/popup/item_properties.cpp index 5abe4ea..44be8ff 100644 --- a/src/imgui/popup/item_properties.cpp +++ b/src/imgui/popup/item_properties.cpp @@ -214,8 +214,8 @@ namespace anm2ed::imgui::popup if (addId != -1) { - document.reference = {queuedAnimationIndex, (int)queuedType, addId}; - document.items.references = {document.reference}; + document.reference_set({queuedAnimationIndex, (int)queuedType, addId}); + document.selected_set(SelectionKind::TRACKS, {document.reference_get()}); document.frame_references_clear(); if (queuedType == LAYER) if (auto layer = document.anm2.element_get(ElementType::LAYER_ELEMENT, addId)) diff --git a/src/imgui/taskbar.cpp b/src/imgui/taskbar.cpp index 46dc71e..319a409 100644 --- a/src/imgui/taskbar.cpp +++ b/src/imgui/taskbar.cpp @@ -64,10 +64,10 @@ namespace anm2ed::imgui void Taskbar::update(Manager& manager, Settings& settings, Resources& resources, Dialog& dialog, bool& isQuitting) { auto document = manager.get(); - auto itemType = document ? (ItemType)document->reference.itemType : ItemType::NONE; - auto animation = - document ? document->anm2.element_get(ElementType::ANIMATION, document->reference.animationIndex) : nullptr; - auto frames = document ? &document->frames : nullptr; + auto itemType = document ? (ItemType)document->reference_get().itemType : ItemType::NONE; + auto animation = document + ? document->anm2.element_get(ElementType::ANIMATION, document->reference_get().animationIndex) + : nullptr; auto layerReferences = document ? document->layer_references_get() : std::vector{}; bool isGenerateAnimationFromGridAvailable = !layerReferences.empty(); bool hasRegions = false; @@ -181,8 +181,9 @@ namespace anm2ed::imgui ImGui::Separator(); - bool isChangeAllFramesAvailable = frames && !frames->selection.empty() && itemType != ItemType::TRIGGER; - bool isChangeAllAnimationsAvailable = document && !document->animation.selection.empty(); + bool isChangeAllFramesAvailable = + document && !document->frame_references_get().empty() && itemType != ItemType::TRIGGER; + bool isChangeAllAnimationsAvailable = document && !document->animations_selected_get().empty(); if (ImGui::MenuItem(localize.get(LABEL_CHANGE_ALL_FRAME_PROPERTIES), nullptr, false, isChangeAllFramesAvailable || isChangeAllAnimationsAvailable)) changePopup.open(); @@ -272,9 +273,9 @@ namespace anm2ed::imgui } else { - animationIndices.insert(document->animation.selection.begin(), document->animation.selection.end()); - if (animationIndices.empty() && document->reference.animationIndex >= 0) - animationIndices.insert(document->reference.animationIndex); + animationIndices = document->animations_selected_get(); + if (animationIndices.empty() && document->reference_get().animationIndex >= 0) + animationIndices.insert(document->reference_get().animationIndex); } auto queuedAnimationIndices = animationIndices; diff --git a/src/imgui/window/animation_preview.cpp b/src/imgui/window/animation_preview.cpp index 3e82b63..5714251 100644 --- a/src/imgui/window/animation_preview.cpp +++ b/src/imgui/window/animation_preview.cpp @@ -416,7 +416,7 @@ namespace anm2ed::imgui if (playback.isPlaying) { - auto animation = anm2.element_get(ElementType::ANIMATION, document.reference.animationIndex); + auto animation = anm2.element_get(ElementType::ANIMATION, document.reference_get().animationIndex); auto& isSound = settings.timelineIsSound; if (!animation) @@ -453,7 +453,7 @@ namespace anm2ed::imgui auto& document = *manager.get(); auto& anm2 = document.anm2; auto& playback = document.playback; - auto& reference = document.reference; + auto reference = document.reference_get(); auto animation = anm2.element_get(ElementType::ANIMATION, reference.animationIndex); auto& pan = document.previewPan; auto& zoom = document.previewZoom; @@ -793,7 +793,7 @@ namespace anm2ed::imgui if (&sampleDocument != &document) return false; if (reference.animationIndex != -1 && referenceItemType == ItemType::LAYER && reference.itemID == id) return true; - for (auto itemReference : document.items.references) + for (auto itemReference : document.selected_get(SelectionKind::TRACKS)) if (itemReference.animationIndex == reference.animationIndex && itemReference.itemType == LAYER && itemReference.itemID == id) return true; diff --git a/src/imgui/window/animations.cpp b/src/imgui/window/animations.cpp index 450727c..6bb4f2b 100644 --- a/src/imgui/window/animations.cpp +++ b/src/imgui/window/animations.cpp @@ -65,6 +65,30 @@ namespace anm2ed::imgui return insertIndex; } + // The list draws from Storage/Window mirrors of the document Selection: loaded each frame, written back on change. + void animations_selection_set(Window& window, Document& document, const std::set& indices, + const std::set& groupIds) + { + document.animations_selected_set(indices); + auto& groupUids = document.selection.uids[SelectionKind::ANIMATION_GROUPS]; + groupUids.clear(); + if (auto animations = document.anm2.element_get(ElementType::ANIMATIONS)) + for (const auto& item : animations->children) + if (item.type == ElementType::GROUP && groupIds.contains(item.id)) groupUids.insert(item.uid); + document.animation.selection = indices; + window.selection = groupIds; + } + + void animations_selection_load(Window& window, Document& document) + { + document.animation.selection = document.animations_selected_get(); + window.selection.clear(); + auto& groupUids = document.selection.uids[SelectionKind::ANIMATION_GROUPS]; + if (auto animations = document.anm2.element_get(ElementType::ANIMATIONS)) + for (const auto& item : animations->children) + if (item.type == ElementType::GROUP && groupUids.contains(item.uid)) window.selection.insert(item.id); + } + bool is_animation_grouped(const std::set& groupIds, const Element& animation) { return animation.groupId != -1 && groupIds.contains(animation.groupId); @@ -126,10 +150,7 @@ namespace anm2ed::imgui [&](Anm2& anm2) { animation_overlay_reset(document, indices); - if (indices.contains(document.reference.animationIndex)) - document.reference.animationIndex = -1; - document.animation.selection.clear(); - window.selection.clear(); + animations_selection_set(window, document, {}, {}); return edit::animations_remove(anm2, indices, groupIds); }); } @@ -165,24 +186,23 @@ namespace anm2ed::imgui { std::set groupIds{}; std::string errorString{}; - auto uids = document.edit_apply(edit, window.changeType, - [&](Anm2& anm2) - { - auto uids = edit::animations_paste(anm2, text, start, targetGroupId, groupIds, - &errorString); - if (uids.empty()) return uids; - auto indices = animation_indices_get(document, uids); - document.animation.selection = groupIds.empty() ? indices : std::set{}; - window.selection = groupIds; - document.reference = {}; - window.newElementId = -1; - if (indices.empty()) return uids; - window.scrollQueued = *indices.rbegin(); - if (!groupIds.empty()) return uids; - document.reference = {*indices.rbegin()}; - if (indices.size() == 1) window.newElementId = *indices.rbegin(); - return uids; - }); + auto uids = document.edit_apply( + edit, window.changeType, + [&](Anm2& anm2) + { + auto uids = edit::animations_paste(anm2, text, start, targetGroupId, groupIds, &errorString); + if (uids.empty()) return uids; + auto indices = animation_indices_get(document, uids); + animations_selection_set(window, document, groupIds.empty() ? indices : std::set{}, groupIds); + document.reference_set({}); + window.newElementId = -1; + if (indices.empty()) return uids; + window.scrollQueued = *indices.rbegin(); + if (!groupIds.empty()) return uids; + document.reference_set({*indices.rbegin()}); + if (indices.size() == 1) window.newElementId = *indices.rbegin(); + return uids; + }); if (!errorString.empty()) toast_log(Level::ERROR, TOAST_DESERIALIZE_ANIMATIONS_FAILED, errorString); } @@ -219,8 +239,8 @@ namespace anm2ed::imgui auto indices = animation_indices_get(document, uids); if (indices.empty()) return uids; merged = *indices.begin(); - document.animation.selection = {merged}; - document.reference = {merged}; + document.animations_selected_set({merged}); + document.reference_set({merged}); return uids; }); return merged; @@ -244,8 +264,7 @@ namespace anm2ed::imgui (item.type == AnimationDragDropType::GROUP ? groupIds : indices).insert(item.id); auto uids = edit::animations_move(anm2, indices, groupIds, targetChildIndex, targetGroupId); if (uids.empty() && groupIds.empty()) return; - document.animation.selection = animation_indices_get(document, uids); - window.selection = groupIds; + animations_selection_set(window, document, animation_indices_get(document, uids), groupIds); } void animation_move_command_push(Window& window, Manager& manager, std::vector items, @@ -426,8 +445,8 @@ namespace anm2ed::imgui if (isDeselected ? selection.empty() && groupSelection.empty() : !isCtrl || selection.empty()) { document.animation.reference = -1; - document.reference = {}; - document.frames.clear(); + document.reference_set({}); + document.frame_references_clear(); } } @@ -481,7 +500,7 @@ namespace anm2ed::imgui { auto animations = document.anm2.element_get(ElementType::ANIMATIONS); auto isDefault = animations && animations->defaultAnimation == animation.name; - auto isReferenced = document.reference.animationIndex == index; + auto isReferenced = document.reference_get().animationIndex == index; return isDefault && isReferenced ? font::BOLD_ITALICS : isDefault ? font::BOLD : isReferenced ? font::ITALICS @@ -491,8 +510,8 @@ namespace anm2ed::imgui { auto& io = ImGui::GetIO(); if (!io.KeyCtrl && !io.KeyShift) window.selection.clear(); - document.reference = {index}; - document.frames.clear(); + document.reference_set({index}); + document.frame_references_clear(); }; window.rename_finish = [](Document& document, Element& animation, int, int count) { @@ -521,7 +540,7 @@ namespace anm2ed::imgui auto container = window_container_get(window, document); if (!container) return; auto groupIds = edit::animation_group_ids_get(*container); - std::erase_if(window.selection, [&](int groupId) { return !groupIds.contains(groupId); }); + animations_selection_load(window, document); std::vector visibleIds{}; window.order.clear(); @@ -556,6 +575,9 @@ namespace anm2ed::imgui ++animationIndex; } window_selection_finish(window, manager, settings, clipboard, document, arrowSelectionId); + animations_selection_set(window, document, + {document.animation.selection.begin(), document.animation.selection.end()}, + std::set(window.selection)); }; window.add = [](Window& window, Manager&, Settings&, Document& document, Clipboard&) { @@ -581,11 +603,11 @@ namespace anm2ed::imgui document.edit_apply(EDIT_ADD_ANIMATION, window.changeType, [&](Anm2& anm2) { - auto uids = edit::animation_add(anm2, index, groupId, document.reference.animationIndex, - localize.get(TEXT_NEW_ANIMATION)); - selection = {index}; - window.selection.clear(); - document.reference = {index}; + auto uids = + edit::animation_add(anm2, index, groupId, document.reference_get().animationIndex, + localize.get(TEXT_NEW_ANIMATION)); + animations_selection_set(window, document, {index}, {}); + document.reference_set({index}); window.newElementId = index; window.scrollQueued = index; return uids; @@ -675,9 +697,8 @@ namespace anm2ed::imgui [&](Anm2& anm2) { auto uids = edit::animations_group(anm2, targetSet, localize.get(TEXT_NEW_GROUP)); - document.animation.selection = targetSet; - window.selection = {groupId}; - document.reference = {*targetSet.begin()}; + animations_selection_set(window, document, targetSet, {groupId}); + document.reference_set({*targetSet.begin()}); window.scrollQueued = *targetSet.begin(); return uids; }); @@ -812,9 +833,10 @@ namespace anm2ed::imgui auto count = animations ? animations_count_get(*animations) : 0; if ((!isPrevious && !isNext) || count <= 0) return; - auto& reference = document.reference; + auto reference = document.reference_get(); reference.animationIndex = std::clamp(reference.animationIndex + (isNext ? 1 : -1), 0, count - 1); - document.animation.selection = {reference.animationIndex}; + document.reference_set(reference); + animations_selection_set(window, document, {reference.animationIndex}, std::set(window.selection)); window.scrollQueued = reference.animationIndex; }; return window; diff --git a/src/imgui/window/frame_properties.cpp b/src/imgui/window/frame_properties.cpp index c556f3d..498cdd8 100644 --- a/src/imgui/window/frame_properties.cpp +++ b/src/imgui/window/frame_properties.cpp @@ -48,10 +48,10 @@ namespace anm2ed::imgui { auto& document = *manager.get(); auto frameSelectionCount = document.frame_references_get(Document::FrameReferenceFallback::NONE).size(); - if (frameSelectionCount == 0 && document.reference.frameIndex >= 0) frameSelectionCount = 1; + if (frameSelectionCount == 0 && document.reference_get().frameIndex >= 0) frameSelectionCount = 1; auto isSingleFrameSelection = frameSelectionCount == 1; auto isMultiFrameSelection = frameSelectionCount > 1; - auto isSingleFrameBatchMode = isSingleFrameSelection && document.reference.itemType != TRIGGER && isBatchMode; + auto isSingleFrameBatchMode = isSingleFrameSelection && document.reference_get().itemType != TRIGGER && isBatchMode; auto isBatchFrameProperties = isMultiFrameSelection || isSingleFrameBatchMode; auto windowLabel = std::string( localize.get(isBatchFrameProperties ? LABEL_CHANGE_ALL_FRAME_PROPERTIES : LABEL_FRAME_PROPERTIES_WINDOW)); @@ -60,7 +60,7 @@ namespace anm2ed::imgui if (ImGui::Begin(windowLabel.c_str(), &settings.windowIsFrameProperties)) { auto& anm2 = document.anm2; - auto& reference = document.reference; + auto reference = document.reference_get(); auto& type = reference.itemType; auto frame = reference.frameIndex >= 0 ? anm2.element_get(reference) : nullptr; @@ -138,8 +138,10 @@ namespace anm2ed::imgui frame.atFrame = atFrame; if (!item) return; frames_sort_by_at_frame(*item); - document.reference.frameIndex = frame_index_from_at_frame_get(*item, atFrame); - document.frame_references_set({document.reference}); + auto reference = document.reference_get(); + reference.frameIndex = frame_index_from_at_frame_get(*item, atFrame); + document.reference_set(reference); + document.frame_references_set({reference}); }); } ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_TRIGGER_AT_FRAME)); diff --git a/src/imgui/window/regions.cpp b/src/imgui/window/regions.cpp index 106494b..af91b0e 100644 --- a/src/imgui/window/regions.cpp +++ b/src/imgui/window/regions.cpp @@ -409,9 +409,8 @@ namespace anm2ed::imgui window.row_select = [](Window&, Document& document, int) { document.editTarget = Document::EditTarget::REGION; - document.reference = {document.reference.animationIndex}; - document.frames.reference = -1; - document.frames.selection.clear(); + document.reference_set({document.reference_get().animationIndex}); + document.frame_references_clear(); }; window.tooltip_draw = [](Document& document, Resources& resources, const Element& region) { @@ -464,9 +463,8 @@ namespace anm2ed::imgui if (!document.regions_trim(document.spritesheet.reference, region.selection)) return; if (region.reference != -1 && !region.selection.contains(region.reference)) region.reference = *region.selection.begin(); - document.reference = {document.reference.animationIndex}; - document.frames.reference = -1; - document.frames.selection.clear(); + document.reference_set({document.reference_get().animationIndex}); + document.frame_references_clear(); }); }; window.paste = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard) diff --git a/src/imgui/window/spritesheet_editor.cpp b/src/imgui/window/spritesheet_editor.cpp index ebfd368..87b3074 100644 --- a/src/imgui/window/spritesheet_editor.cpp +++ b/src/imgui/window/spritesheet_editor.cpp @@ -30,7 +30,7 @@ namespace anm2ed::imgui auto& document = *manager.get(); auto& anm2 = document.anm2; - auto& reference = document.reference; + auto reference = document.reference_get(); auto& referenceSpritesheet = document.spritesheet.reference; auto& pan = document.editorPan; auto& zoom = document.editorZoom; @@ -51,7 +51,7 @@ namespace anm2ed::imgui if (auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, frameReference.itemID)) return layer->spritesheetId; } - if (document.is_frame_reference_valid(reference) && reference.itemType == LAYER) + if (reference.frameIndex >= 0 && reference.itemType == LAYER) if (auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, reference.itemID)) return layer->spritesheetId; return -1; }; diff --git a/src/imgui/window/timeline/context.hpp b/src/imgui/window/timeline/context.hpp index 5ddfb84..6294c28 100644 --- a/src/imgui/window/timeline/context.hpp +++ b/src/imgui/window/timeline/context.hpp @@ -162,8 +162,7 @@ namespace anm2ed::imgui Document& document; Anm2& anm2; Playback& playback; - Reference& reference; - Storage& frames; + Reference reference; Storage& region; Element* animation{}; float rowFrameChildHeight{}; @@ -207,7 +206,6 @@ namespace anm2ed::imgui int group_items_count_get(int type, int groupId); Element* command_item_reference_get(Document& document, Reference itemReference); glm::vec4 color_get(TimelineColor, int); - void frames_focus_sync_for(Document&); std::set drag_frame_references_get(const Reference&); // Queues an edit on the document; its result is selected (frames by default). template @@ -222,6 +220,7 @@ namespace anm2ed::imgui }); } void edit_begin_push(StringType); + void reference_set(Reference); void frames_select_for(Document&, const edit::Uids&); Element* command_frame_get(Document& document, const Reference& targetReference); Reference item_reference_get(int type, int id, int groupType = NONE, int groupId = -1); @@ -229,7 +228,6 @@ namespace anm2ed::imgui bool is_same_item(const Reference& left, const Reference& right); void group_selection_reset_for(Document& targetDocument); std::set item_references_for_current_get(); - void frames_selection_sync_for(Document& targetDocument); void item_selection_set_for(Document& targetDocument, Reference itemReference); void frame_selection_set_for(Document& targetDocument, Reference frameReference); void frame_selection_toggle_for(Document& targetDocument, Reference frameReference); @@ -239,7 +237,6 @@ namespace anm2ed::imgui std::set copy_frame_references_get(); void frames_selection_reset_for(Document& targetDocument); void frames_selection_set_reference_for(Document& targetDocument); - void frames_reference_normalize_for(Document& targetDocument); void reference_clear_for(Document& targetDocument); void reference_set_item_reference_for(Document& targetDocument, Reference itemReference); void reference_set_timeline_item_reference_for(Document& targetDocument, Reference itemReference); diff --git a/src/imgui/window/timeline/edit.cpp b/src/imgui/window/timeline/edit.cpp index 0bc633b..6eff7c3 100644 --- a/src/imgui/window/timeline/edit.cpp +++ b/src/imgui/window/timeline/edit.cpp @@ -74,14 +74,11 @@ namespace anm2ed::imgui bool isLayers, bool isNulls) { std::set result{}; - auto animationIndex = targetDocument.reference.animationIndex; + auto animationIndex = targetDocument.reference_get().animationIndex; if (target == BakeIntoOtherFramesTarget::CURRENT_SELECTION) { - for (auto itemReference : targetDocument.items.references) - { - itemReference.frameIndex = -1; + for (auto itemReference : targetDocument.selected_get(SelectionKind::TRACKS)) if (itemReference.itemType == LAYER || itemReference.itemType == NULL_) result.insert(itemReference); - } return result; } @@ -93,8 +90,8 @@ namespace anm2ed::imgui [&](const Element& track) { auto itemType = (int)row.itemType; - result.insert({animationIndex, itemType, anm2ed::track_id_get(track), -1, - track.groupId == -1 ? NONE : itemType, track.groupId}); + result.insert(Reference{animationIndex, itemType, anm2ed::track_id_get(track), -1, + track.groupId == -1 ? NONE : itemType, track.groupId}); }); } return result; @@ -150,7 +147,7 @@ namespace anm2ed::imgui std::vector TimelineContext::item_references_groupable_get() { - if (!document.groupReferences.empty()) return std::vector{}; + if (!document.selection.uids[SelectionKind::GROUPS].empty()) return std::vector{}; auto selectedItems = item_references_for_current_get(); std::erase_if(selectedItems, [](const Reference& itemReference) { return itemReference.itemType != LAYER && itemReference.itemType != NULL_; }); @@ -186,8 +183,8 @@ namespace anm2ed::imgui [=, this](Document& document, const edit::Uids& uids) { if (uids.empty()) return; - document.items.references = {targetReferences.begin(), targetReferences.end()}; - document.reference = targetReferences.front(); + document.selected_set(SelectionKind::TRACKS, {targetReferences.begin(), targetReferences.end()}); + document.reference_set(targetReferences.front()); frames_selection_reset_for(document); }); } @@ -210,6 +207,15 @@ namespace anm2ed::imgui (row.isGroup ? groupIds : ids).insert(row.id); edit::RowTarget target{targetRow.isGroup, targetRow.type, targetRow.id, targetRow.groupId}; auto animationIndex = reference.animationIndex; + std::set tracks{}; + std::set groups{}; + for (const auto& row : draggedRows) + (row.isGroup ? groups : tracks) + .insert(row.isGroup ? Reference{row.animationIndex, row.type, row.id} : row_item_reference_get(row)); + Selection moved{}; + selection_references_set(moved, anm2, SelectionKind::TRACKS, tracks); + selection_references_set(moved, anm2, SelectionKind::GROUPS, groups); + if (!tracks.empty()) selection_focus_set(moved, anm2, *tracks.begin()); edit_push( EDIT_MOVE_ITEMS, Document::ITEMS, @@ -220,15 +226,12 @@ namespace anm2ed::imgui }, [=, this](Document& document, const edit::Uids&) { - document.items.references.clear(); - document.groupReferences.clear(); - 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(row)); - document.reference = - document.items.references.empty() ? Reference{animationIndex} : *document.items.references.begin(); + document.selection.uids[SelectionKind::TRACKS] = moved.uids.at(SelectionKind::TRACKS); + document.selection.uids[SelectionKind::GROUPS] = moved.uids.at(SelectionKind::GROUPS); + if (tracks.empty()) + document.reference_set({document.reference_get().animationIndex}); + else + document.selection.focus = moved.focus; frames_selection_reset_for(document); }); } diff --git a/src/imgui/window/timeline/frames.cpp b/src/imgui/window/timeline/frames.cpp index 1b3fb8c..6120d1f 100644 --- a/src/imgui/window/timeline/frames.cpp +++ b/src/imgui/window/timeline/frames.cpp @@ -5,7 +5,6 @@ namespace anm2ed::imgui void TimelineContext::frame_move_drag_clear() { frameMoveDrag = {}; - frameSelectionLocked.clear(); frameMoveDropType = NONE; frameMoveDropItemID = -1; frameMoveDropGroupType = NONE; @@ -189,9 +188,9 @@ namespace anm2ed::imgui if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) && ImGui::Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteFocused)) { - if (!frames.references.empty() || !frames.selection.empty()) + if (!document.selection.uids[SelectionKind::FRAMES].empty()) { - reference.frameIndex = -1; + reference_set(item_reference_from_frame_get(reference)); frames_selection_reset_for(document); } else if (reference.itemType != NONE || reference.itemID != -1) @@ -370,6 +369,7 @@ namespace anm2ed::imgui } auto frameType = track_frame_type_get(*item); + auto& selectedFrames = document.selection.uids[SelectionKind::FRAMES]; int frameIndex{}; for (int childIndex = 0; childIndex < (int)item->children.size(); ++childIndex) { @@ -380,10 +380,7 @@ namespace anm2ed::imgui auto frameReference = row_reference_make(frameIndex); auto isFrameVisible = isVisible && frame.isVisible; auto isReferenced = reference == frameReference; - auto isSelected = frames.references.contains(frameReference) || - (frames.references.empty() && frames.selection.contains(frameIndex) && - reference.itemType == type && reference.itemID == id && - reference.groupType == row.rootGroupType && reference.groupId == row.rootGroupId); + auto isSelected = selectedFrames.contains(frame.uid); if (type == TRIGGER) frameTime = frame.atFrame; @@ -399,9 +396,6 @@ namespace anm2ed::imgui } auto buttonPos = ImVec2(cursorPos.x + (frameTime * frameSize.x), cursorPos.y); - if (frameFocusRequested && frameFocusIndex == frameIndex && reference == frameReference) - frameFocusRequested = false; - ImGui::SetCursorPos(buttonPos); auto buttonScreenPos = ImGui::GetCursorScreenPos(); @@ -419,7 +413,6 @@ namespace anm2ed::imgui ImGui::SetNextItemAllowOverlap(); ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, FRAME_ROUNDING); ImGui::SetNextItemSelectionUserData(frameIndex); - bool isDifferentItem = !is_same_item(reference, frameReference); if (ImGui::Selectable("##Frame Button", isSelected, ImGuiSelectableFlags_None, buttonSize)) { if (type == LAYER) @@ -467,11 +460,10 @@ namespace anm2ed::imgui frameSelectionAnchor = frameReference; isFrameSelectionAnchorSet = true; } - reference = frameReference; + reference_set(frameReference); isReferenced = true; region.reference = -1; region.selection.clear(); - if (isDifferentItem) frames_selection_sync_for(document); } ImGui::PopStyleVar(); @@ -563,22 +555,6 @@ namespace anm2ed::imgui (ImGui::IsMouseReleased(ImGuiMouseButton_Left) || ImGui::IsMouseReleased(ImGuiMouseButton_Right)) && !ImGui::IsAnyItemHovered()) row_selection_set(row); - - if (isFrameSelectionLocked) - { - std::set lockedSelection{}; - for (int idx : frameSelectionLocked) - lockedSelection.insert(row_reference_make(idx)); - document.frame_references_set(std::move(lockedSelection)); - isFrameSelectionLocked = false; - frameSelectionLocked.clear(); - } - if (reference.itemType == type && reference.itemID == id && reference.groupType == row.rootGroupType && - reference.groupId == row.rootGroupId) - { - frameSelectionSnapshot.assign(frames.selection.begin(), frames.selection.end()); - frameSelectionSnapshotReference = reference; - } } } @@ -639,7 +615,6 @@ namespace anm2ed::imgui draggedFrameStartMouseX = 0.0f; draggedFrameWidth = 0.0f; isDraggedFrameSnapshot = false; - frameSelectionLocked.clear(); } } @@ -692,7 +667,6 @@ namespace anm2ed::imgui { group_selection_reset_for(document); document.frame_references_set(document.selected_item_frame_references_get()); - if (!document.frames.references.empty()) frames_selection_sync_for(document); } ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2()); @@ -809,7 +783,6 @@ namespace anm2ed::imgui } else document.frame_references_set(frameBoxSelection); - frames_selection_sync_for(document); } isFrameBoxPending = false; isFrameBoxSelecting = false; diff --git a/src/imgui/window/timeline/menus.cpp b/src/imgui/window/timeline/menus.cpp index 50c46d1..bff4b57 100644 --- a/src/imgui/window/timeline/menus.cpp +++ b/src/imgui/window/timeline/menus.cpp @@ -4,7 +4,6 @@ namespace anm2ed::imgui { void TimelineContext::frame_begin() { - frames_reference_normalize_for(document); iconTintDefault = isLightTheme ? ICON_TINT_DEFAULT_LIGHT : ICON_TINT_DEFAULT_DARK; itemIconTint = isLightTheme ? ICON_TINT_DEFAULT_LIGHT : iconTintDefault; frameBorderColor = isLightTheme ? FRAME_BORDER_COLOR_LIGHT : FRAME_BORDER_COLOR_DARK; diff --git a/src/imgui/window/timeline/popups.cpp b/src/imgui/window/timeline/popups.cpp index c1b0266..2756fd8 100644 --- a/src/imgui/window/timeline/popups.cpp +++ b/src/imgui/window/timeline/popups.cpp @@ -197,8 +197,10 @@ namespace anm2ed::imgui if (isPreviousFrame || isNextFrame) if (auto item = selected_item_get(); item && !item->children.empty()) { - reference.frameIndex = glm::clamp(reference.frameIndex + (int)isNextFrame - (int)isPreviousFrame, 0, - (int)item->children.size() - 1); + auto frameReference = reference; + frameReference.frameIndex = glm::clamp(reference.frameIndex + (int)isNextFrame - (int)isPreviousFrame, 0, + (int)item->children.size() - 1); + reference_set(frameReference); frames_selection_set_reference_for(document); document.frameTime = frame_time_from_index_get(*item, reference.frameIndex); } diff --git a/src/imgui/window/timeline/selection.cpp b/src/imgui/window/timeline/selection.cpp index 53774ee..7d4cb5f 100644 --- a/src/imgui/window/timeline/selection.cpp +++ b/src/imgui/window/timeline/selection.cpp @@ -62,15 +62,6 @@ namespace anm2ed::imgui return isLightTheme ? colors.light[std::clamp(type, 0, TRIGGER)] : colors.dark[type]; } - void TimelineContext::frames_focus_sync_for(Document& targetDocument) - { - frames_selection_sync_for(targetDocument); - frameSelectionLocked.clear(); - isFrameSelectionLocked = false; - frameFocusIndex = targetDocument.reference.frameIndex; - frameFocusRequested = true; - } - std::set TimelineContext::drag_frame_references_get(const Reference& frameReference) { auto selectedReferences = document.frame_references_get(Document::FrameReferenceFallback::NONE); @@ -84,17 +75,22 @@ namespace anm2ed::imgui command_push([label](Manager&, Document& document) { document.edit_begin(label); }); } + void TimelineContext::reference_set(Reference value) + { + reference = value; + document.reference_set(value); + } + void TimelineContext::frames_select_for(Document& targetDocument, const edit::Uids& uids) { auto references = targetDocument.references_get(uids); if (references.empty()) return; - targetDocument.reference = references.front(); + auto focus = references.front(); targetDocument.editTarget = Document::EditTarget::FRAME; targetDocument.frame_references_set({references.begin(), references.end()}); - if (auto item = command_item_reference_get(targetDocument, targetDocument.reference); - item && targetDocument.reference.itemType != TRIGGER) - targetDocument.frameTime = frame_time_from_index_get(*item, targetDocument.reference.frameIndex); - frames_focus_sync_for(targetDocument); + targetDocument.reference_set(focus); + if (auto item = command_item_reference_get(targetDocument, focus); item && focus.itemType != TRIGGER) + targetDocument.frameTime = frame_time_from_index_get(*item, focus.frameIndex); } Reference TimelineContext::item_reference_get(int type, int id, int groupType, int groupId) @@ -116,34 +112,20 @@ namespace anm2ed::imgui void TimelineContext::group_selection_reset_for(Document& targetDocument) { - targetDocument.groupReferences.clear(); + targetDocument.selection.uids[SelectionKind::GROUPS].clear(); isRowSelectionAnchorSet = false; } std::set TimelineContext::item_references_for_current_get() { - std::set result = document.items.references; - if (result.empty() && reference.itemType != NONE) - { - auto itemReference = reference; - itemReference.frameIndex = -1; - result.insert(itemReference); - } + auto result = document.selected_get(SelectionKind::TRACKS); + if (result.empty() && reference.itemType != NONE) result.insert(item_reference_from_frame_get(reference)); return result; } - void TimelineContext::frames_selection_sync_for(Document& targetDocument) - { - auto selectedFrames = targetDocument.frame_references_get(Document::FrameReferenceFallback::NONE); - targetDocument.frame_references_set(std::move(selectedFrames)); - frameSelectionSnapshot.assign(targetDocument.frames.selection.begin(), targetDocument.frames.selection.end()); - frameSelectionSnapshotReference = targetDocument.reference; - } - void TimelineContext::item_selection_set_for(Document& targetDocument, Reference itemReference) { - itemReference.frameIndex = -1; - targetDocument.items.references = {itemReference}; + targetDocument.selected_set(SelectionKind::TRACKS, {item_reference_from_frame_get(itemReference)}); } void TimelineContext::frame_selection_set_for(Document& targetDocument, Reference frameReference) @@ -151,7 +133,7 @@ namespace anm2ed::imgui group_selection_reset_for(targetDocument); targetDocument.editTarget = Document::EditTarget::FRAME; targetDocument.frame_references_set({frameReference}); - frames_selection_sync_for(targetDocument); + targetDocument.reference_set(frameReference); } void TimelineContext::frame_selection_toggle_for(Document& targetDocument, Reference frameReference) @@ -159,24 +141,12 @@ namespace anm2ed::imgui group_selection_reset_for(targetDocument); targetDocument.editTarget = Document::EditTarget::FRAME; auto selection = targetDocument.frame_references_get(Document::FrameReferenceFallback::NONE); - auto itemReference = item_reference_from_frame_get(frameReference); - if (selection.contains(frameReference)) - { - if (selection.size() > 1) selection.erase(frameReference); - bool isItemStillSelected{}; - for (const auto& selectedFrame : selection) - if (is_same_item(selectedFrame, itemReference)) isItemStillSelected = true; - if (!isItemStillSelected && targetDocument.items.references.size() > 1) - targetDocument.items.references.erase(itemReference); - } - else - { + if (!selection.contains(frameReference)) selection.insert(frameReference); - targetDocument.items.references.insert(itemReference); - } - targetDocument.reference = frameReference; - targetDocument.frame_references_set(std::move(selection)); - frames_selection_sync_for(targetDocument); + else if (selection.size() > 1) + selection.erase(frameReference); + targetDocument.frame_references_set(selection); + targetDocument.reference_set(frameReference); } bool TimelineContext::frame_selection_range_set_for(Document& targetDocument, Reference firstReference, @@ -188,17 +158,12 @@ namespace anm2ed::imgui return false; auto item = command_item_reference_get(targetDocument, lastReference); - if (!item) return false; - if (firstReference.frameIndex >= (int)item->children.size() || - lastReference.frameIndex >= (int)item->children.size()) + if (!item || std::max(firstReference.frameIndex, lastReference.frameIndex) >= track_frames_count_get(*item)) return false; - auto firstIndex = firstReference.frameIndex; - auto lastIndex = lastReference.frameIndex; - if (firstIndex > lastIndex) std::swap(firstIndex, lastIndex); + auto [firstIndex, lastIndex] = std::minmax(firstReference.frameIndex, lastReference.frameIndex); auto selectedFrames = isAdditive ? targetDocument.frame_references_get(Document::FrameReferenceFallback::NONE) : std::set{}; - for (int i = firstIndex; i <= lastIndex; ++i) { auto frameReference = lastReference; @@ -206,9 +171,8 @@ namespace anm2ed::imgui selectedFrames.insert(frameReference); } - targetDocument.reference = lastReference; - targetDocument.frame_references_set(std::move(selectedFrames)); - frames_selection_sync_for(targetDocument); + targetDocument.frame_references_set(selectedFrames); + targetDocument.reference_set(lastReference); return true; } @@ -222,13 +186,9 @@ namespace anm2ed::imgui auto selectedFrames = document.frame_references_get(Document::FrameReferenceFallback::NONE); if (!selectedFrames.empty()) return selectedFrames; - auto selectedItems = document.items.references; + auto selectedItems = document.selected_get(SelectionKind::TRACKS); if (selectedItems.empty() && reference.itemType != NONE && reference.frameIndex < 0) - { - auto itemReference = reference; - itemReference.frameIndex = -1; - selectedItems.insert(itemReference); - } + selectedItems.insert(item_reference_from_frame_get(reference)); std::set result{}; for (auto itemReference : selectedItems) @@ -244,57 +204,27 @@ namespace anm2ed::imgui void TimelineContext::frames_selection_reset_for(Document& targetDocument) { targetDocument.frame_references_clear(); - frameSelectionSnapshot.clear(); - frameSelectionLocked.clear(); - isFrameSelectionLocked = false; - frameFocusRequested = false; - frameFocusIndex = -1; - frameSelectionSnapshotReference = targetDocument.reference; } void TimelineContext::frames_selection_set_reference_for(Document& targetDocument) { - auto& targetReference = targetDocument.reference; - if (targetReference.frameIndex >= 0) - { - targetDocument.editTarget = Document::EditTarget::FRAME; - targetDocument.frame_references_set({targetReference}); - } - else - targetDocument.frame_references_clear(); - frameSelectionSnapshot.assign(targetDocument.frames.selection.begin(), targetDocument.frames.selection.end()); - frameSelectionSnapshotReference = targetReference; - frameSelectionLocked.clear(); - isFrameSelectionLocked = false; - frameFocusIndex = targetReference.frameIndex; - frameFocusRequested = targetReference.frameIndex >= 0; - } - - void TimelineContext::frames_reference_normalize_for(Document& targetDocument) - { - auto selectedFrames = targetDocument.frame_references_get(Document::FrameReferenceFallback::NONE); - if (!selectedFrames.empty()) - { - targetDocument.frame_references_set(std::move(selectedFrames)); - frames_selection_sync_for(targetDocument); - return; - } - - if (targetDocument.reference.frameIndex >= 0 && !targetDocument.is_frame_reference_valid(targetDocument.reference)) - targetDocument.reference.frameIndex = -1; + auto targetReference = targetDocument.reference_get(); + if (targetReference.frameIndex < 0) return targetDocument.frame_references_clear(); + targetDocument.editTarget = Document::EditTarget::FRAME; + targetDocument.frame_references_set({targetReference}); } void TimelineContext::reference_clear_for(Document& targetDocument) { - targetDocument.reference = {targetDocument.reference.animationIndex}; + targetDocument.reference_set({targetDocument.reference_get().animationIndex}); frames_selection_reset_for(targetDocument); - targetDocument.items.references.clear(); + targetDocument.selection.uids[SelectionKind::TRACKS].clear(); } void TimelineContext::reference_set_item_reference_for(Document& targetDocument, Reference itemReference) { itemReference.frameIndex = -1; - targetDocument.reference = itemReference; + targetDocument.reference_set(itemReference); frames_selection_reset_for(targetDocument); item_selection_set_for(targetDocument, itemReference); } @@ -429,49 +359,59 @@ namespace anm2ed::imgui bool TimelineContext::is_group_selected(const TimelineItemRow& row) { - return document.groupReferences.contains(group_reference_get(row)); + return document.selected_get(SelectionKind::GROUPS).contains(group_reference_get(row)); } bool TimelineContext::is_row_selected(const TimelineItemRow& row) { if (row.isGroup) return is_group_selected(row); auto itemReference = item_reference_get(row.type, row.id, row.rootGroupType, row.rootGroupId); - auto isReferenced = is_same_item(reference, itemReference); - return document.items.references.contains(itemReference) || - (document.items.references.empty() && document.groupReferences.empty() && isReferenced); + auto tracks = document.selected_get(SelectionKind::TRACKS); + return tracks.contains(itemReference) || + (tracks.empty() && document.selection.uids[SelectionKind::GROUPS].empty() && + is_same_item(reference, itemReference)); + } + + SelectionKind row_kind_get(const TimelineRowReference& row) + { + return row.isGroup ? SelectionKind::GROUPS : SelectionKind::TRACKS; + } + + Reference row_selection_reference_get(const TimelineRowReference& row) + { + return row.isGroup ? Reference{row.animationIndex, row.type, row.id} + : Reference{row.animationIndex, row.type, row.id, -1, row.groupType, row.groupId}; } void TimelineContext::row_selection_clear() { - document.items.references.clear(); - document.groupReferences.clear(); + document.selection.uids[SelectionKind::TRACKS].clear(); + document.selection.uids[SelectionKind::GROUPS].clear(); } void TimelineContext::row_selection_insert(const TimelineRowReference& row) { - if (row.isGroup) - document.groupReferences.insert({row.animationIndex, row.type, row.id}); - else - document.items.references.insert(row_item_reference_get(row)); + auto references = document.selected_get(row_kind_get(row)); + references.insert(row_selection_reference_get(row)); + document.selected_set(row_kind_get(row), references); } void TimelineContext::row_selection_erase(const TimelineRowReference& row) { - if (row.isGroup) - document.groupReferences.erase({row.animationIndex, row.type, row.id}); - else - document.items.references.erase(row_item_reference_get(row)); + auto references = document.selected_get(row_kind_get(row)); + references.erase(row_selection_reference_get(row)); + document.selected_set(row_kind_get(row), references); } bool TimelineContext::is_row_reference_selected(const TimelineRowReference& row) { - if (row.isGroup) return document.groupReferences.contains({row.animationIndex, row.type, row.id}); - return document.items.references.contains(row_item_reference_get(row)); + return document.selected_get(row_kind_get(row)).contains(row_selection_reference_get(row)); } std::size_t TimelineContext::row_selection_count_get() { - return document.items.references.size() + document.groupReferences.size(); + return document.selection.uids[SelectionKind::TRACKS].size() + + document.selection.uids[SelectionKind::GROUPS].size(); } void TimelineContext::row_selection_set(const TimelineItemRow& row) @@ -481,13 +421,13 @@ namespace anm2ed::imgui auto isShiftDown = ImGui::IsKeyDown(ImGuiMod_Shift); if (row.isGroup) - reference = {reference.animationIndex}; + reference_set({reference.animationIndex}); else { if (row.type == LAYER) if (auto layer = anm2.element_get(ElementType::LAYER_ELEMENT, row.id)) document.spritesheet.reference = layer->spritesheetId; - reference = row_item_reference_get(rowReference); + reference_set(row_item_reference_get(rowReference)); } frames_selection_reset_for(document); diff --git a/src/imgui/window/timeline/timeline.cpp b/src/imgui/window/timeline/timeline.cpp index e15453b..5c170ef 100644 --- a/src/imgui/window/timeline/timeline.cpp +++ b/src/imgui/window/timeline/timeline.cpp @@ -9,7 +9,7 @@ namespace anm2ed::imgui Clipboard& clipboard) : TimelineState(std::move(state)), manager(manager), settings(settings), resources(resources), clipboard(clipboard), document(*manager.get()), anm2(document.anm2), playback(document.playback), - reference(document.reference), frames(document.frames), region(document.region) + reference(document.reference_get()), region(document.region) { } diff --git a/src/imgui/window/timeline/timeline.hpp b/src/imgui/window/timeline/timeline.hpp index 19a394e..1a3a7fe 100644 --- a/src/imgui/window/timeline/timeline.hpp +++ b/src/imgui/window/timeline/timeline.hpp @@ -95,13 +95,7 @@ namespace anm2ed::imgui float draggedFrameStartMouseX{}; float draggedFrameWidth{}; bool isDraggedFrameSnapshot{}; - bool frameFocusRequested{}; - int frameFocusIndex{-1}; FrameMoveDrag frameMoveDrag{}; - std::vector frameSelectionSnapshot{}; - std::vector frameSelectionLocked{}; - bool isFrameSelectionLocked{}; - Reference frameSelectionSnapshotReference{}; Reference frameSelectionAnchor{}; bool isFrameSelectionAnchorSet{}; int animationLengthEditIndex{-1}; diff --git a/src/imgui/wizard/change_all_frame_properties.cpp b/src/imgui/wizard/change_all_frame_properties.cpp index 3ffb508..2cf1b3f 100644 --- a/src/imgui/wizard/change_all_frame_properties.cpp +++ b/src/imgui/wizard/change_all_frame_properties.cpp @@ -97,18 +97,19 @@ namespace anm2ed::imgui::wizard { isChanged = false; - auto& animations = document.animation.selection; + auto animations = document.animations_selected_get(); auto& destination = settings.changeDestination; auto& regionId = document.changeAllFramePropertiesRegionId; auto& shaderId = document.changeAllFramePropertiesShaderId; auto selectedFrameReferences = document.frame_references_get(Document::FrameReferenceFallback::NONE); std::erase_if(selectedFrameReferences, [](const Reference& reference) { return reference.itemType == TRIGGER; }); - auto selectedItemReferences = document.items.references; - if (selectedItemReferences.empty() && document.reference.itemType != NONE) - selectedItemReferences.insert({document.reference.animationIndex, document.reference.itemType, - document.reference.itemID, -1, document.reference.groupType, - document.reference.groupId}); + auto selectedItemReferences = document.selected_get(SelectionKind::TRACKS); + if (auto reference = document.reference_get(); selectedItemReferences.empty() && reference.itemType != NONE) + { + reference.frameIndex = -1; + selectedItemReferences.insert(reference); + } std::erase_if(selectedItemReferences, [](const Reference& reference) { return reference.itemType == TRIGGER; }); bool isSelectedFramesAvailable = !selectedFrameReferences.empty(); @@ -196,7 +197,8 @@ namespace anm2ed::imgui::wizard interpolationIds.push_back(i); interpolationLabels.push_back(localize.get(INTERPOLATION_LABELS[i])); } - auto regions = document.layer_regions_get(document.reference.itemType == LAYER ? document.reference.itemID : -1); + auto regions = + document.layer_regions_get(document.reference_get().itemType == LAYER ? document.reference_get().itemID : -1); if (!isLayerPropertyAvailable) regionId = shaderId = -1; ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImGui::GetStyle().ItemInnerSpacing); diff --git a/src/imgui/wizard/generate_animation_from_grid.cpp b/src/imgui/wizard/generate_animation_from_grid.cpp index 137fb51..5bfd79b 100644 --- a/src/imgui/wizard/generate_animation_from_grid.cpp +++ b/src/imgui/wizard/generate_animation_from_grid.cpp @@ -21,7 +21,7 @@ namespace anm2ed::imgui::wizard isEnd = false; auto& anm2 = document.anm2; - auto& reference = document.reference; + auto reference = document.reference_get(); auto& startPosition = settings.generateStartPosition; auto& size = settings.generateSize; auto& pivot = settings.generatePivot; diff --git a/src/imgui/wizard/render_animation.cpp b/src/imgui/wizard/render_animation.cpp index 4f2d56f..1409fcb 100644 --- a/src/imgui/wizard/render_animation.cpp +++ b/src/imgui/wizard/render_animation.cpp @@ -21,7 +21,7 @@ namespace anm2ed::imgui::wizard { void RenderAnimation::range_to_animation_set(Manager& manager, Document& document) { - if (auto animation = document.anm2.element_get(ElementType::ANIMATION, document.reference.animationIndex)) + if (auto animation = document.anm2.element_get(ElementType::ANIMATION, document.reference_get().animationIndex)) { manager.recordingStart = 0; manager.recordingEnd = animation->frameNum - 1; @@ -31,7 +31,7 @@ namespace anm2ed::imgui::wizard void RenderAnimation::range_to_frames_set(Manager& manager, Document& document) { auto frameReferences = document.frame_references_get(Document::FrameReferenceFallback::NONE); - auto& reference = document.reference; + auto reference = document.reference_get(); std::erase_if(frameReferences, [&](const Reference& frameReference) { @@ -72,7 +72,7 @@ namespace anm2ed::imgui::wizard { isEnd = false; - auto animation = document.anm2.element_get(ElementType::ANIMATION, document.reference.animationIndex); + auto animation = document.anm2.element_get(ElementType::ANIMATION, document.reference_get().animationIndex); if (!animation) return; auto& ffmpegPath = settings.renderFFmpegPath; @@ -88,7 +88,7 @@ namespace anm2ed::imgui::wizard auto& rows = settings.renderRows; auto& columns = settings.renderColumns; auto& isRange = manager.isRecordingRange; - auto& reference = document.reference; + auto reference = document.reference_get(); auto& frameNum = animation->frameNum; auto animationFrameNum = std::max(frameNum, 1); auto frameMax = animationFrameNum - 1; diff --git a/src/selection.cpp b/src/selection.cpp new file mode 100644 index 0000000..7098e03 --- /dev/null +++ b/src/selection.cpp @@ -0,0 +1,72 @@ +#include "selection.hpp" + +namespace anm2ed +{ + std::set selection_references_get(const Selection& selection, const UidIndex& index, SelectionKind kind) + { + std::set references{}; + if (auto uids = selection.uids.find(kind); uids != selection.uids.end()) + for (auto uid : uids->second) + if (auto reference = index.reference_get(uid)) references.insert(*reference); + return references; + } + + std::uint64_t handle_uid_get(Handle handle) + { + return handle.frame ? handle.frame : handle.item ? handle.item : handle.animation; + } + + void selection_references_set(Selection& selection, const Anm2& anm2, SelectionKind kind, + const std::set& references) + { + auto& uids = selection.uids[kind]; + uids.clear(); + for (auto reference : references) + if (auto uid = handle_uid_get(anm2.handle_get(reference, kind == SelectionKind::GROUPS))) uids.insert(uid); + } + + // The most specific part of the focus that still exists. + Reference selection_focus_get(const Selection& selection, const UidIndex& index) + { + for (auto uid : {selection.focus.frame, selection.focus.item, selection.focus.animation}) + if (auto reference = index.reference_get(uid)) return *reference; + return {}; + } + + void selection_focus_set(Selection& selection, const Anm2& anm2, Reference reference) + { + selection.focus = anm2.handle_get(reference); + } + + // After an edit: drops uids that no longer exist, and moves a focus whose element was removed to its nearest + // survivor (a frame at the same index of its track, the animation's root track, or the nearest animation). + void selection_follow(Selection& selection, const Anm2& anm2, const UidIndex& index, Reference before) + { + for (auto& [kind, uids] : selection.uids) + std::erase_if(uids, [&](std::uint64_t uid) { return !index.contains(uid); }); + + auto [animationUid, itemUid, frameUid] = selection.focus; + auto is_removed = [&](std::uint64_t uid) { return uid && !index.contains(uid); }; + auto reference = selection_focus_get(selection, index); + + if (is_removed(animationUid) || !animationUid) + { + auto animations = anm2.element_get(ElementType::ANIMATIONS); + auto count = animations ? animations_count_get(*animations) : 0; + reference = + count > 0 && animationUid ? Reference{std::clamp(before.animationIndex, 0, count - 1), ROOT} : Reference{}; + } + else if (is_removed(itemUid)) + reference = {reference.animationIndex, ROOT}; + else if (is_removed(frameUid)) + { + auto track = anm2.element_get(reference); + auto count = track ? track_frames_count_get(*track) : 0; + reference.frameIndex = count > 0 ? std::clamp(before.frameIndex, 0, count - 1) : -1; + } + else + return; + + selection_focus_set(selection, anm2, reference); + } +} diff --git a/src/selection.hpp b/src/selection.hpp new file mode 100644 index 0000000..79e187b --- /dev/null +++ b/src/selection.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#include "anm2/anm2.hpp" + +namespace anm2ed +{ + enum class SelectionKind + { + ANIMATIONS, + ANIMATION_GROUPS, + TRACKS, + GROUPS, + FRAMES + }; + + // What is selected, by element uid, plus the focused animation/track/frame. uids follow elements through edits, + // so nothing here is repaired by index; the UI reads it as References through a UidIndex. + struct Selection + { + std::map> uids{}; + Handle focus{}; + + bool operator==(const Selection&) const = default; + }; + + std::set selection_references_get(const Selection&, const UidIndex&, SelectionKind); + void selection_references_set(Selection&, const Anm2&, SelectionKind, const std::set&); + Reference selection_focus_get(const Selection&, const UidIndex&); + void selection_focus_set(Selection&, const Anm2&, Reference); + void selection_follow(Selection&, const Anm2&, const UidIndex&, Reference); +} diff --git a/src/snapshots.cpp b/src/snapshots.cpp index 1c632aa..54523d9 100644 --- a/src/snapshots.cpp +++ b/src/snapshots.cpp @@ -168,104 +168,15 @@ namespace anm2ed::snapshots return step; } - bool is_same_item(Reference left, Reference right) + Reference focus_get(const Snapshot& snapshot) { - left.frameIndex = right.frameIndex = -1; - return left == right; + return selection_focus_get(snapshot.selection, UidIndex(snapshot.anm2)); } - SnapshotSelection selection_capture(const Snapshot& snapshot) - { - SnapshotSelection selection{snapshot.reference, - snapshot.frames.references, - snapshot.items.references, - snapshot.groupReferences, - {snapshot.frames.selection.begin(), snapshot.frames.selection.end()}, - {snapshot.animation.selection.begin(), snapshot.animation.selection.end()}}; - - auto handle_add = [&](Reference reference) - { selection.handles.try_emplace(reference, snapshot.anm2.handle_get(reference)); }; - handle_add(selection.reference); - for (auto frameIndex : selection.frameIndices) - handle_add({selection.reference.animationIndex, selection.reference.itemType, selection.reference.itemID, - frameIndex, selection.reference.groupType, selection.reference.groupId}); - for (auto animationIndex : selection.animationIndices) - handle_add({animationIndex}); - for (const auto& references : {selection.frameReferences, selection.itemReferences}) - for (auto reference : references) - handle_add(reference); - for (auto reference : selection.groupReferences) - selection.groupHandles.try_emplace(reference, snapshot.anm2.handle_get(reference, true)); - return selection; - } - - // The main reference never disappears: a removed frame falls back to its track, a removed track to the root. - Reference reference_follow(const Anm2& anm2, const UidIndex& index, Handle handle, Reference reference) - { - if (auto followed = index.reference_get(handle, reference)) return *followed; - - if (auto track = index.reference_get({handle.animation, handle.item}, reference)) - { - auto element = anm2.element_get(*track); - auto count = element ? track_frames_count_get(*element) : 0; - track->frameIndex = count > 0 ? std::clamp(reference.frameIndex, 0, count - 1) : -1; - return *track; - } - - if (auto animation = index.reference_get({handle.animation}, {reference.animationIndex, ROOT})) return *animation; - auto animations = anm2.element_get(ElementType::ANIMATIONS); - auto count = animations ? animations_count_get(*animations) : 0; - return count > 0 ? Reference{std::clamp(reference.animationIndex, 0, count - 1), ROOT} : Reference{}; - } - - std::set references_follow(const UidIndex& index, const std::set& references, - const std::map& handles) - { - std::set followed{}; - for (auto reference : references) - if (auto result = index.reference_get(handles.at(reference), reference)) followed.insert(*result); - return followed; - } - - // Selection fields the change left untouched are re-resolved by uid; fields it set itself are kept. - void selection_follow(Snapshot& snapshot, const SnapshotSelection& before) + void selection_follow(Snapshot& snapshot, Reference before) { snapshot.anm2.uids_repair(); - UidIndex index(snapshot.anm2); - - auto isReferenceKept = snapshot.reference == before.reference; - if (isReferenceKept) - snapshot.reference = - reference_follow(snapshot.anm2, index, before.handles.at(before.reference), before.reference); - if (snapshot.frames.references == before.frameReferences) - snapshot.frames.references = references_follow(index, before.frameReferences, before.handles); - if (snapshot.items.references == before.itemReferences) - snapshot.items.references = references_follow(index, before.itemReferences, before.handles); - if (snapshot.groupReferences == before.groupReferences) - snapshot.groupReferences = references_follow(index, before.groupReferences, before.groupHandles); - - if (std::set(snapshot.animation.selection.begin(), snapshot.animation.selection.end()) == - before.animationIndices) - { - snapshot.animation.selection.clear(); - for (auto animationIndex : before.animationIndices) - if (auto followed = index.reference_get(before.handles.at({animationIndex}), {animationIndex})) - snapshot.animation.selection.insert(followed->animationIndex); - } - - if (!isReferenceKept || - std::set(snapshot.frames.selection.begin(), snapshot.frames.selection.end()) != before.frameIndices) - return; - - snapshot.frames.selection.clear(); - for (auto frameIndex : before.frameIndices) - { - auto frame = before.reference; - frame.frameIndex = frameIndex; - auto followed = index.reference_get(before.handles.at(frame), frame); - if (followed && is_same_item(*followed, snapshot.reference)) - snapshot.frames.selection.insert(followed->frameIndex); - } + ::anm2ed::selection_follow(snapshot.selection, snapshot.anm2, UidIndex(snapshot.anm2), before); } } @@ -350,8 +261,8 @@ namespace anm2ed void Snapshots::step_push(const std::string& message, SnapshotStep step) { - if (pendingStep || pendingSelection) commit(); - pendingSelection = snapshots::selection_capture(current); + if (pendingStep || pendingFocus) commit(); + pendingFocus = snapshots::focus_get(current); current.message = message; step.message = message; pendingStep = step.is_empty() ? std::nullopt : std::optional(std::move(step)); @@ -367,7 +278,7 @@ namespace anm2ed void Snapshots::commit() { const auto& snapshot = current; - if (pendingSelection) snapshots::selection_follow(current, *std::exchange(pendingSelection, std::nullopt)); + if (pendingFocus) snapshots::selection_follow(current, *std::exchange(pendingFocus, std::nullopt)); if (!pendingStep) return; auto step = std::move(*pendingStep); @@ -422,12 +333,12 @@ namespace anm2ed bool Snapshots::undo() { pendingStep.reset(); - pendingSelection.reset(); + pendingFocus.reset(); if (auto step = undoStack.pop()) { - auto selection = snapshots::selection_capture(current); + auto focus = snapshots::focus_get(current); step->apply(current, SnapshotStepDirection::UNDO); - snapshots::selection_follow(current, selection); + snapshots::selection_follow(current, focus); redoStack.push(std::move(*step)); return true; } @@ -437,12 +348,12 @@ namespace anm2ed bool Snapshots::redo() { pendingStep.reset(); - pendingSelection.reset(); + pendingFocus.reset(); if (auto step = redoStack.pop()) { - auto selection = snapshots::selection_capture(current); + auto focus = snapshots::focus_get(current); step->apply(current, SnapshotStepDirection::REDO); - snapshots::selection_follow(current, selection); + snapshots::selection_follow(current, focus); undoStack.push(std::move(*step)); return true; } @@ -454,7 +365,7 @@ namespace anm2ed undoStack.clear(); redoStack.clear(); pendingStep.reset(); - pendingSelection.reset(); + pendingFocus.reset(); } void Snapshots::apply_limit() diff --git a/src/snapshots.hpp b/src/snapshots.hpp index 8623a6b..bb2107f 100644 --- a/src/snapshots.hpp +++ b/src/snapshots.hpp @@ -10,6 +10,7 @@ #include "audio_data.hpp" #include "image.hpp" #include "playback.hpp" +#include "selection.hpp" #include "storage.hpp" namespace anm2ed::snapshots @@ -52,10 +53,7 @@ namespace anm2ed { public: Playback playback{}; - Storage animation{}; Storage event{}; - Storage frames{}; - Storage items{}; Storage layer{}; Storage merge{}; Storage null{}; @@ -66,8 +64,7 @@ namespace anm2ed std::map textures{}; std::map sounds{}; Anm2 anm2{}; - Reference reference{}; - std::set groupReferences{}; + Selection selection{}; float frameTime{}; std::string message = snapshots::ACTION; }; @@ -77,10 +74,7 @@ namespace anm2ed #define SNAPSHOT_STEP_STATE_FIELDS \ X(Playback, playback) \ - X(Storage, animation) \ X(Storage, event) \ - X(Storage, frames) \ - X(Storage, items) \ X(Storage, layer) \ X(Storage, merge) \ X(Storage, null) \ @@ -88,8 +82,7 @@ namespace anm2ed X(Storage, shader) \ X(Storage, sound) \ X(Storage, spritesheet) \ - X(Reference, reference) \ - X(std::set, groupReferences) \ + X(Selection, selection) \ X(float, frameTime) #define SNAPSHOT_STEP_RESOURCE_FIELDS \ @@ -110,19 +103,6 @@ namespace anm2ed void apply(Snapshot&, SnapshotStepDirection) const; }; - // The selection as it was before a change, with the uids that let it follow elements through the change. - struct SnapshotSelection - { - Reference reference{}; - std::set frameReferences{}; - std::set itemReferences{}; - std::set groupReferences{}; - std::set frameIndices{}; - std::set animationIndices{}; - std::map handles{}; - std::map groupHandles{}; - }; - class SnapshotStack { public: @@ -149,7 +129,7 @@ namespace anm2ed SnapshotStack redoStack{}; Snapshot current{}; std::optional pendingStep{}; - std::optional pendingSelection{}; + std::optional pendingFocus{}; void push(const std::string&, bool = false); void step_push(const std::string&, SnapshotStep); diff --git a/tests/selection.cpp b/tests/selection.cpp index 70e896a..0d624ab 100644 --- a/tests/selection.cpp +++ b/tests/selection.cpp @@ -1,5 +1,8 @@ #include "common.hpp" +#include "edit/edit.hpp" +#include "selection.hpp" + using namespace anm2ed; using namespace anm2ed::test; @@ -10,20 +13,13 @@ Anm2 fixture_load_with_uids(const char* name) return anm2; } -Element& track_get(Anm2& anm2, Reference reference) +// Applies an edit and follows the selection through it, the way a commit does. +void selection_edit(Anm2& anm2, Selection& selection, auto&& edit) { - reference.frameIndex = -1; - auto track = anm2.element_get(reference); - REQUIRE(track); - return *track; -} - -std::optional reference_follow(Anm2& anm2, Reference reference, auto&& edit) -{ - auto handle = anm2.handle_get(reference); + auto before = selection_focus_get(selection, UidIndex(anm2)); edit(); anm2.uids_repair(); - return UidIndex(anm2).reference_get(handle, reference); + selection_follow(selection, anm2, UidIndex(anm2), before); } TEST_CASE("uids are unique and non-zero after load") @@ -40,80 +36,57 @@ TEST_CASE("uids are unique and non-zero after load") } } -TEST_CASE("frame references follow inserts, deletes and copies") +TEST_CASE("selected frames and focus follow inserts and moves") { auto anm2 = fixture_load_with_uids("02_items.anm2"); - Reference frame{0, LAYER, 0, 1}; + Selection selection{}; + selection_references_set(selection, anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}, {0, LAYER, 1, 0}}); + selection_focus_set(selection, anm2, {0, LAYER, 0, 1}); - auto inserted = reference_follow(anm2, frame, - [&] - { - auto& track = track_get(anm2, frame); - track.children.insert(track.children.begin(), element_make(ElementType::FRAME)); - }); - REQUIRE(inserted); - CHECK(inserted->frameIndex == 2); - frame = *inserted; - - auto copied = reference_follow(anm2, frame, - [&] - { - auto& track = track_get(anm2, frame); - track.children.push_back(track.children[frame.frameIndex]); - }); - REQUIRE(copied); - CHECK(copied->frameIndex == 2); - - CHECK_FALSE(reference_follow( - anm2, frame, [&] { track_get(anm2, frame).children.erase(track_get(anm2, frame).children.begin() + 2); })); -} - -TEST_CASE("references follow animation removal and reordering") -{ - auto anm2 = fixture_load_with_uids("02_items.anm2"); - Reference root{1, ROOT, -1, 0}; - auto animations = anm2.element_get(ElementType::ANIMATIONS); - auto followed = reference_follow(anm2, root, [&] { animations->children.erase(animations->children.begin()); }); - REQUIRE(followed); - CHECK(followed->animationIndex == 0); - CHECK(followed->frameIndex == 0); - - Reference animationOnly{0}; - auto reordered = reference_follow( - anm2, animationOnly, - [&] { animations->children.insert(animations->children.begin(), element_make(ElementType::ANIMATION)); }); - REQUIRE(reordered); - CHECK(reordered->animationIndex == 1); -} - -TEST_CASE("track references follow regrouping") -{ - auto anm2 = fixture_load_with_uids("03a_groups_nested.anm2"); - auto animation = anm2.element_get(ElementType::ANIMATION, 0); - auto layers = child_first_get(*animation, ElementType::LAYER_ANIMATIONS); - auto group = child_first_get(*layers, ElementType::GROUP); - REQUIRE(group); - auto grouped = track_find(*layers, ElementType::LAYER_ANIMATION, 0); - REQUIRE(grouped); - REQUIRE(grouped->groupId == group->id); - - Reference withGroup{0, LAYER, 0, 0, LAYER, group->id}; - Reference withoutGroup{0, LAYER, 0, 0}; - auto handleWithGroup = anm2.handle_get(withGroup); - auto handleWithoutGroup = anm2.handle_get(withoutGroup); - grouped->groupId = -1; + selection_edit(anm2, selection, [&] { edit::frame_insert(anm2, {0, LAYER, 0, -1}, 0); }); + selection_edit(anm2, selection, [&] { edit::frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}); }); UidIndex index(anm2); - auto followedWithGroup = index.reference_get(handleWithGroup, withGroup); - auto followedWithoutGroup = index.reference_get(handleWithoutGroup, withoutGroup); - REQUIRE(followedWithGroup); - REQUIRE(followedWithoutGroup); - CHECK(*followedWithGroup == Reference{0, LAYER, 0, 0}); - CHECK(*followedWithoutGroup == withoutGroup); - CHECK(anm2.element_get(*followedWithGroup)); - - Reference groupReference{0, LAYER, group->id}; - auto groupHandle = anm2.handle_get(groupReference, true); - CHECK(groupHandle.item == group->uid); - CHECK(index.reference_get(groupHandle, groupReference) == groupReference); + CHECK(selection_focus_get(selection, index) == Reference{0, LAYER, 0, 0}); + CHECK(selection_references_get(selection, index, SelectionKind::FRAMES) == + std::set{{0, LAYER, 0, 0}, {0, LAYER, 1, 0}}); +} + +TEST_CASE("a removed focus falls back to its nearest survivor") +{ + auto anm2 = fixture_load_with_uids("02_items.anm2"); + Selection selection{}; + selection_focus_set(selection, anm2, {0, LAYER, 0, 1}); + selection_references_set(selection, anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}}); + + selection_edit(anm2, selection, [&] { edit::frames_delete(anm2, {{0, LAYER, 0, 1}}); }); + CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, LAYER, 0, 0}); + CHECK(selection.uids[SelectionKind::FRAMES].empty()); + + selection_edit(anm2, selection, [&] { edit::items_remove(anm2, 0, {{LAYER, {0}}}, {}); }); + CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, ROOT}); + + selection_focus_set(selection, anm2, {1, ROOT, -1, 0}); + selection_edit(anm2, selection, [&] { edit::animations_remove(anm2, {1}, {}); }); + CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, ROOT}); +} + +TEST_CASE("track and group selection follow regrouping") +{ + auto anm2 = fixture_load_with_uids("03a_groups_nested.anm2"); + auto group = + child_first_get(*child_first_get(*anm2.element_get(ElementType::ANIMATION, 0), ElementType::LAYER_ANIMATIONS), + ElementType::GROUP); + REQUIRE(group); + Selection selection{}; + selection_references_set(selection, anm2, SelectionKind::TRACKS, {{0, LAYER, 0, -1, LAYER, group->id}}); + selection_references_set(selection, anm2, SelectionKind::GROUPS, {{0, LAYER, group->id}}); + + selection_edit(anm2, selection, + [&] { edit::items_move(anm2, 0, LAYER, {0}, {}, {false, LAYER, 2, -1}, false, false); }); + + UidIndex index(anm2); + CHECK(selection_references_get(selection, index, SelectionKind::TRACKS) == std::set{{0, LAYER, 0}}); + CHECK(selection_references_get(selection, index, SelectionKind::GROUPS) == + std::set{{0, LAYER, group->id}}); } diff --git a/tests/undo.cpp b/tests/undo.cpp index 76ef1b2..e22ab4b 100644 --- a/tests/undo.cpp +++ b/tests/undo.cpp @@ -109,18 +109,22 @@ TEST_CASE("undo and redo restore every state of a random edit walk") TEST_CASE("selection follows its frame through an edit and its undo") { Snapshots snapshots{}; - snapshots.current.anm2 = anm2_load(file_load(CORPUS_DIR / "02_items.anm2")); - snapshots.current.reference = {0, LAYER, 0, 1}; - snapshots.current.frames.references = {{0, LAYER, 0, 1}}; + auto& current = snapshots.current; + current.anm2 = anm2_load(file_load(CORPUS_DIR / "02_items.anm2")); + current.anm2.uids_repair(); + selection_focus_set(current.selection, current.anm2, {0, LAYER, 0, 1}); + selection_references_set(current.selection, current.anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}}); snapshots.push("Insert"); - edit::frame_insert(snapshots.current.anm2, {0, LAYER, 0, -1}, 0); - auto& track = *snapshots.current.anm2.element_get(Reference{0, LAYER, 0}); + edit::frame_insert(current.anm2, {0, LAYER, 0, -1}, 0); + auto& track = *current.anm2.element_get(Reference{0, LAYER, 0}); std::rotate(track.children.begin(), track.children.end() - 1, track.children.end()); snapshots.commit(); - CHECK(snapshots.current.reference == Reference{0, LAYER, 0, 2}); - CHECK(snapshots.current.frames.references == std::set{{0, LAYER, 0, 2}}); + UidIndex index(current.anm2); + CHECK(selection_focus_get(current.selection, index) == Reference{0, LAYER, 0, 2}); + CHECK(selection_references_get(current.selection, index, SelectionKind::FRAMES) == + std::set{{0, LAYER, 0, 2}}); REQUIRE(snapshots.undo()); - CHECK(snapshots.current.reference == Reference{0, LAYER, 0, 1}); + CHECK(selection_focus_get(current.selection, UidIndex(current.anm2)) == Reference{0, LAYER, 0, 1}); }