From 69e78ff77a8c0dd53a3d5f405c85b5a2e8393f36 Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 3 Oct 2026 19:01:40 -0400 Subject: [PATCH] Timeline: per-frame work runs once, not once per row Escape, the context menu (and its shortcuts), frame resize drags and the frame-move drop now run once from frames_child instead of inside every row's child. draw() hands popups and global shortcuts to popups_update() and shortcuts_update(). Drops a dead style push/pop. Co-Authored-By: Claude Opus 5.5 --- src/imgui/actions.cpp | 6 +- src/imgui/actions.hpp | 3 +- src/imgui/window/timeline/context.hpp | 3 + src/imgui/window/timeline/frames.cpp | 161 +++++++++++++------------ src/imgui/window/timeline/menus.cpp | 3 +- src/imgui/window/timeline/timeline.cpp | 147 +++++++++++----------- 6 files changed, 168 insertions(+), 155 deletions(-) diff --git a/src/imgui/actions.cpp b/src/imgui/actions.cpp index bb06736..260caf8 100644 --- a/src/imgui/actions.cpp +++ b/src/imgui/actions.cpp @@ -77,10 +77,12 @@ namespace anm2ed::imgui } } - bool actions_context_window_draw(const char* label, Actions& actions, Settings& settings, ImGuiPopupFlags flags) + bool actions_context_window_draw(const char* label, Actions& actions, Settings& settings, ImGuiPopupFlags flags, + ImGuiHoveredFlags hoveredFlags) { auto mouseButton = flags & ImGuiPopupFlags_MouseButtonMask_; - if (ImGui::IsMouseReleased(mouseButton) && ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + if (ImGui::IsMouseReleased(mouseButton) && + ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup | hoveredFlags)) if (!(flags & ImGuiPopupFlags_NoOpenOverItems) || !ImGui::IsAnyItemHovered()) ImGui::OpenPopup(label, flags); if (!ImGui::BeginPopup(label, ImGuiWindowFlags_NoMove)) return false; diff --git a/src/imgui/actions.hpp b/src/imgui/actions.hpp index 7365f42..9761c82 100644 --- a/src/imgui/actions.hpp +++ b/src/imgui/actions.hpp @@ -86,7 +86,8 @@ namespace anm2ed::imgui int = ACTION_COUNT); void actions_undo_redo_add(Actions&, Manager&, Document&); void actions_menu_draw(Actions&, Settings&); - bool actions_context_window_draw(const char*, Actions&, Settings&, ImGuiPopupFlags = ImGuiPopupFlags_MouseButtonRight); + bool actions_context_window_draw(const char*, Actions&, Settings&, ImGuiPopupFlags = ImGuiPopupFlags_MouseButtonRight, + ImGuiHoveredFlags = ImGuiHoveredFlags_None); bool actions_popup_draw(const char*, Actions&, Settings&); void actions_shortcuts_update(Actions&, Manager&, types::shortcut::Type = types::shortcut::FOCUSED); void action_button_draw(Action&, Manager&, Settings&, ImVec2, bool&); diff --git a/src/imgui/window/timeline/context.hpp b/src/imgui/window/timeline/context.hpp index d49be1c..1a45998 100644 --- a/src/imgui/window/timeline/context.hpp +++ b/src/imgui/window/timeline/context.hpp @@ -254,7 +254,10 @@ namespace anm2ed::imgui ImVec2 frame_box_screen_point_get(ImVec2); void frame_overlay_draw(ImDrawList*, ImVec2, ImVec2); void frame_child(const TimelineRow&, int&, float); + void frame_drag_update(); void frames_child(); + void popups_update(); + void shortcuts_update(); void draw(); }; } diff --git a/src/imgui/window/timeline/frames.cpp b/src/imgui/window/timeline/frames.cpp index 97643c4..b22af51 100644 --- a/src/imgui/window/timeline/frames.cpp +++ b/src/imgui/window/timeline/frames.cpp @@ -107,11 +107,6 @@ namespace anm2ed::imgui auto colorActiveHidden = to_imvec4(colorActiveVec * COLOR_HIDDEN_MULTIPLIER); auto colorHoveredHidden = to_imvec4(colorHoveredVec * COLOR_HIDDEN_MULTIPLIER); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing); - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, style.WindowPadding); - - ImGui::PopStyleVar(2); - ImGui::PushID(index); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2()); @@ -161,19 +156,6 @@ namespace anm2ed::imgui if (isFramesChildVisible) { - if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) && - ImGui::Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteFocused)) - { - if (!document.selection.uids[SelectionKind::FRAMES].empty()) - { - reference_set(item_reference_from_frame_get(reference)); - document.frame_references_clear(); - } - else if (reference.itemType != NONE || reference.itemID != -1) - group_selection_reset_for(document); - document.focus_clear(); - } - if (type == NONE) { if (length > 0) @@ -503,67 +485,6 @@ namespace anm2ed::imgui } } - if (isDraggedFrameActive) - { - ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); - auto durationDelta = - draggedFrameWidth > 0.0f - ? static_cast((ImGui::GetIO().MousePos.x - draggedFrameStartMouseX) / draggedFrameWidth) - : hoveredTime - draggedFrameStart; - auto isDraggedFrameChanged = draggedFrameType == TRIGGER ? hoveredTime != draggedFrameStart : durationDelta != 0; - - if (!isDraggedFrameSnapshot && isDraggedFrameChanged) - { - isDraggedFrameSnapshot = true; - edit_begin_push(draggedFrameType == TRIGGER ? EDIT_TRIGGER_AT_FRAME : EDIT_FRAME_DURATION); - } - - if (isDraggedFrameSnapshot) - { - auto targetReference = draggedFrameReference; - if (draggedFrameType == TRIGGER) - { - auto atFrame = glm::clamp( - hoveredTime, 0, settings.playbackIsClamp && animation ? animation->frameNum - 1 : FRAME_NUM_MAX - 1); - command_push([=](Manager&, Document& document) - { edit::trigger_at_frame_set(document.model, targetReference, atFrame); }); - } - else - { - std::map durations{}; - if (draggedFrameStartDurations.empty()) - durations[targetReference] = draggedFrameStartDuration + durationDelta; - for (auto frameDuration : draggedFrameStartDurations) - durations[frameDuration.reference] = frameDuration.duration + durationDelta; - command_push([=](Manager&, Document& document) { edit::frame_durations_set(document.model, durations); }); - } - } - - if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) - { - auto targetReference = draggedFrameReference; - auto targetType = draggedFrameType; - command_push( - [=, this](Manager&, Document& document) - { - auto item = document.model.track_edit(targetReference); - if (targetType == TRIGGER && item) model::frames_sort_by_at_frame(*item); - document.change(); - }); - isDraggedFrameActive = false; - draggedFrameReference = {}; - draggedFrameType = NONE; - draggedFrameStart = -1; - draggedFrameStartDuration = -1; - draggedFrameStartDurations.clear(); - draggedFrameStartMouseX = 0.0f; - draggedFrameWidth = 0.0f; - isDraggedFrameSnapshot = false; - } - } - - context_menu(); - ImGui::EndChild(); if (isLightTheme && isDefaultChild) ImGui::PopStyleColor(); ImGui::PopStyleVar(); @@ -572,6 +493,72 @@ namespace anm2ed::imgui ImGui::PopID(); } + // Frame drags, once per frame after the rows: dropping moved frames, and resizing frames (or moving a trigger). + void TimelineContext::frame_drag_update() + { + if (frameMoveDrag.isActive && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) + { + if (frameMoveDropTarget) frames_move_to(*frameMoveDropTarget); + frameMoveDrag = {}; + } + + if (!isDraggedFrameActive) return; + ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); + auto durationDelta = + draggedFrameWidth > 0.0f + ? static_cast((ImGui::GetIO().MousePos.x - draggedFrameStartMouseX) / draggedFrameWidth) + : hoveredTime - draggedFrameStart; + auto isDraggedFrameChanged = draggedFrameType == TRIGGER ? hoveredTime != draggedFrameStart : durationDelta != 0; + + if (!isDraggedFrameSnapshot && isDraggedFrameChanged) + { + isDraggedFrameSnapshot = true; + edit_begin_push(draggedFrameType == TRIGGER ? EDIT_TRIGGER_AT_FRAME : EDIT_FRAME_DURATION); + } + + if (isDraggedFrameSnapshot) + { + auto targetReference = draggedFrameReference; + if (draggedFrameType == TRIGGER) + { + auto atFrame = glm::clamp(hoveredTime, 0, + settings.playbackIsClamp && animation ? animation->frameNum - 1 : FRAME_NUM_MAX - 1); + command_push([=](Manager&, Document& document) + { edit::trigger_at_frame_set(document.model, targetReference, atFrame); }); + } + else + { + std::map durations{}; + if (draggedFrameStartDurations.empty()) durations[targetReference] = draggedFrameStartDuration + durationDelta; + for (auto frameDuration : draggedFrameStartDurations) + durations[frameDuration.reference] = frameDuration.duration + durationDelta; + command_push([=](Manager&, Document& document) { edit::frame_durations_set(document.model, durations); }); + } + } + + if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) + { + auto targetReference = draggedFrameReference; + auto targetType = draggedFrameType; + command_push( + [=, this](Manager&, Document& document) + { + auto item = document.model.track_edit(targetReference); + if (targetType == TRIGGER && item) model::frames_sort_by_at_frame(*item); + document.change(); + }); + isDraggedFrameActive = false; + draggedFrameReference = {}; + draggedFrameType = NONE; + draggedFrameStart = -1; + draggedFrameStartDuration = -1; + draggedFrameStartDurations.clear(); + draggedFrameStartMouseX = 0.0f; + draggedFrameWidth = 0.0f; + isDraggedFrameSnapshot = false; + } + } + void TimelineContext::frames_child() { auto cursorPos = ImGui::GetCursorPos(); @@ -612,6 +599,19 @@ namespace anm2ed::imgui document.frame_references_set(document.selected_item_frame_references_get()); } + if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) && + ImGui::Shortcut(ImGuiKey_Escape, ImGuiInputFlags_RouteFocused)) + { + if (!document.selection.uids[SelectionKind::FRAMES].empty()) + { + reference_set(item_reference_from_frame_get(reference)); + document.frame_references_clear(); + } + else if (reference.itemType != NONE || reference.itemID != -1) + group_selection_reset_for(document); + document.focus_clear(); + } + ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2()); if (ImGui::BeginTable("##Frames List Table", 1, ImGuiTableFlags_Borders | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) @@ -696,6 +696,9 @@ namespace anm2ed::imgui ImGui::EndTable(); } + frame_drag_update(); + context_menu(); + if (isFrameBoxClipSet) { auto overlayCursor = ImGui::GetCursorScreenPos(); diff --git a/src/imgui/window/timeline/menus.cpp b/src/imgui/window/timeline/menus.cpp index 5a6f29d..7cc0f79 100644 --- a/src/imgui/window/timeline/menus.cpp +++ b/src/imgui/window/timeline/menus.cpp @@ -153,7 +153,8 @@ namespace anm2ed::imgui 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(); }); - actions_context_window_draw("##Context Menu", actions, settings); + actions_context_window_draw("##Context Menu", actions, settings, ImGuiPopupFlags_MouseButtonRight, + ImGuiHoveredFlags_ChildWindows); ImGui::PopStyleVar(2); } diff --git a/src/imgui/window/timeline/timeline.cpp b/src/imgui/window/timeline/timeline.cpp index 7ebe977..519137f 100644 --- a/src/imgui/window/timeline/timeline.cpp +++ b/src/imgui/window/timeline/timeline.cpp @@ -47,11 +47,6 @@ namespace anm2ed::imgui isWindowHovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem); frames_child(); - if (frameMoveDrag.isActive && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) - { - if (frameMoveDropTarget) frames_move_to(*frameMoveDropTarget); - frameMoveDrag = {}; - } items_child(); } ImGui::PopStyleVar(); @@ -60,6 +55,14 @@ namespace anm2ed::imgui if (itemProperties.update(manager, settings, document, reference)) group_selection_reset_for(document); group_properties_update(); + popups_update(); + shortcuts_update(); + + if (isTextPushed) ImGui::PopStyleColor(); + } + + void TimelineContext::popups_update() + { makeManyRegionsPopup.trigger(); if (ImGui::BeginPopupModal(makeManyRegionsPopup.label(), &makeManyRegionsPopup.isOpen, ImGuiWindowFlags_NoResize)) { @@ -174,76 +177,76 @@ namespace anm2ed::imgui ImGui::EndPopup(); } + } - if (animation) + // Global timeline shortcuts: playback, playhead, frame resizing and frame/item navigation. + void TimelineContext::shortcuts_update() + { + if (!animation) return; + + if (shortcut(manager.chords[SHORTCUT_PLAY_PAUSE], shortcut::GLOBAL)) playback.toggle(); + + if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_BACK], shortcut::GLOBAL)) { - if (shortcut(manager.chords[SHORTCUT_PLAY_PAUSE], shortcut::GLOBAL)) playback.toggle(); - - if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_BACK], shortcut::GLOBAL)) - { - playback_stop(); - playback.decrement(settings.playbackIsClamp ? animation->frameNum : FRAME_NUM_MAX); - document.frameTime = playback.time; - } - - if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_FORWARD], shortcut::GLOBAL)) - { - playback_stop(); - playback.increment(settings.playbackIsClamp ? animation->frameNum : FRAME_NUM_MAX); - document.frameTime = playback.time; - } - - struct FrameResize - { - int shortcut; - StringType edit; - int delta; - }; - constexpr FrameResize FRAME_RESIZES[] = {{SHORTCUT_SHORTEN_FRAME, EDIT_SHORTEN_FRAME, -1}, - {SHORTCUT_EXTEND_FRAME, EDIT_EXTEND_FRAME, 1}}; - for (int i = 0; i < (int)std::size(FRAME_RESIZES); ++i) - { - auto resize = FRAME_RESIZES[i]; - auto isPressed = shortcut(manager.chords[resize.shortcut], shortcut::GLOBAL); - auto selectedFrames = - isPressed ? document.frame_references_get(Document::FrameReferenceFallback::NONE) : std::set{}; - std::erase_if(selectedFrames, is_trigger_reference); - if (!selectedFrames.empty()) - { - if (resizeChordTabIds[i] != document.tabId) edit_begin_push(resize.edit); - command_push( - [=, this](Manager&, Document& document) - { - for (auto frameReference : selectedFrames) - if (auto frame = document.model.frame_edit(frameReference)) - frame->duration = - std::clamp(frame->duration + resize.delta, FRAME_DURATION_MIN, FRAME_DURATION_MAX); - document.change(); - }); - } - resizeChordTabIds[i] = isPressed ? document.tabId : 0; - } - - auto isPreviousFrame = shortcut(manager.chords[SHORTCUT_PREVIOUS_FRAME], shortcut::GLOBAL); - auto isNextFrame = shortcut(manager.chords[SHORTCUT_NEXT_FRAME], shortcut::GLOBAL); - auto isPreviousItem = shortcut(manager.chords[SHORTCUT_PREVIOUS_ITEM], shortcut::GLOBAL); - auto isNextItem = shortcut(manager.chords[SHORTCUT_NEXT_ITEM], shortcut::GLOBAL); - - if (isPreviousFrame || isNextFrame) - if (auto item = selected_item_get(); item && !item->frames.empty()) - { - auto frameReference = reference; - frameReference.frameIndex = glm::clamp(reference.frameIndex + (int)isNextFrame - (int)isPreviousFrame, 0, - (int)item->frames.size() - 1); - reference_set(frameReference); - document.frame_focus_select(); - document.frameTime = model::frame_time_from_index_get(*item, reference.frameIndex); - } - - if (isPreviousItem) reference_set_adjacent_item(-1); - if (isNextItem) reference_set_adjacent_item(1); + playback_stop(); + playback.decrement(settings.playbackIsClamp ? animation->frameNum : FRAME_NUM_MAX); + document.frameTime = playback.time; } - if (isTextPushed) ImGui::PopStyleColor(); + if (shortcut(manager.chords[SHORTCUT_MOVE_PLAYHEAD_FORWARD], shortcut::GLOBAL)) + { + playback_stop(); + playback.increment(settings.playbackIsClamp ? animation->frameNum : FRAME_NUM_MAX); + document.frameTime = playback.time; + } + + struct FrameResize + { + int shortcut; + StringType edit; + int delta; + }; + constexpr FrameResize FRAME_RESIZES[] = {{SHORTCUT_SHORTEN_FRAME, EDIT_SHORTEN_FRAME, -1}, + {SHORTCUT_EXTEND_FRAME, EDIT_EXTEND_FRAME, 1}}; + for (int i = 0; i < (int)std::size(FRAME_RESIZES); ++i) + { + auto resize = FRAME_RESIZES[i]; + auto isPressed = shortcut(manager.chords[resize.shortcut], shortcut::GLOBAL); + auto selectedFrames = + isPressed ? document.frame_references_get(Document::FrameReferenceFallback::NONE) : std::set{}; + std::erase_if(selectedFrames, is_trigger_reference); + if (!selectedFrames.empty()) + { + if (resizeChordTabIds[i] != document.tabId) edit_begin_push(resize.edit); + command_push( + [=, this](Manager&, Document& document) + { + for (auto frameReference : selectedFrames) + if (auto frame = document.model.frame_edit(frameReference)) + frame->duration = std::clamp(frame->duration + resize.delta, FRAME_DURATION_MIN, FRAME_DURATION_MAX); + document.change(); + }); + } + resizeChordTabIds[i] = isPressed ? document.tabId : 0; + } + + auto isPreviousFrame = shortcut(manager.chords[SHORTCUT_PREVIOUS_FRAME], shortcut::GLOBAL); + auto isNextFrame = shortcut(manager.chords[SHORTCUT_NEXT_FRAME], shortcut::GLOBAL); + auto isPreviousItem = shortcut(manager.chords[SHORTCUT_PREVIOUS_ITEM], shortcut::GLOBAL); + auto isNextItem = shortcut(manager.chords[SHORTCUT_NEXT_ITEM], shortcut::GLOBAL); + + if (isPreviousFrame || isNextFrame) + if (auto item = selected_item_get(); item && !item->frames.empty()) + { + auto frameReference = reference; + frameReference.frameIndex = + glm::clamp(reference.frameIndex + (int)isNextFrame - (int)isPreviousFrame, 0, (int)item->frames.size() - 1); + reference_set(frameReference); + document.frame_focus_select(); + document.frameTime = model::frame_time_from_index_get(*item, reference.frameIndex); + } + + if (isPreviousItem) reference_set_adjacent_item(-1); + if (isNextItem) reference_set_adjacent_item(1); } }