diff --git a/src/ui/animations.cpp b/src/ui/animations.cpp index 4e666a8..f1ff143 100644 --- a/src/ui/animations.cpp +++ b/src/ui/animations.cpp @@ -10,7 +10,6 @@ #include "ui/tree.h" constexpr const char* ANIMATION_DRAG_DROP = "Animation Drag Drop"; -constexpr float DROP_ZONE_FRACTION = 1.0f / 3.0f; constexpr int MERGE_OPTIONS_ROWS = 2; constexpr float MERGE_OPTIONS_HALF = 0.5f; constexpr int MERGE_POPUP_CHILD_COUNT = 3; @@ -32,13 +31,6 @@ constexpr bool is_group_key(int key) return key <= -GROUP_KEY_OFFSET; } -enum class AnimationDropZone -{ - BEFORE, - INSIDE, - AFTER -}; - struct AnimationDragDropItem { bool isGroup = false; @@ -247,17 +239,6 @@ static int animations_merge(Document& document, const AnimationMergeOptions& opt return merged; } -static AnimationDropZone animation_drop_zone_get(ImVec2 min, ImVec2 max) -{ - auto zoneHeight = (max.y - min.y) * DROP_ZONE_FRACTION; - auto mouseY = ImGui::GetIO().MousePos.y; - if (mouseY < min.y + zoneHeight) - return AnimationDropZone::BEFORE; - if (mouseY >= max.y - zoneHeight) - return AnimationDropZone::AFTER; - return AnimationDropZone::INSIDE; -} - static void animation_move_command_push(const Panel& panel, std::vector items, AnimationTarget target) { @@ -318,19 +299,18 @@ static bool animation_group_drag_drop_update(const Panel& panel, int groupId, Im auto isDelivered = false; if (!items.empty()) { - auto dropZone = animation_drop_zone_get(itemMin, itemMax); + auto dropZone = drop_zone_get(itemMin, itemMax); auto isGroupDragged = std::ranges::any_of(items, &AnimationDragDropItem::isGroup); - auto isDropIntoGroup = dropZone == AnimationDropZone::INSIDE && !isGroupDragged; + auto isDropIntoGroup = dropZone == DROP_INSIDE && !isGroupDragged; if (isDropIntoGroup) drop_box_draw(ImGui::GetWindowDrawList(), itemMin, itemMax); else - drop_line_draw(ImGui::GetWindowDrawList(), itemMin, itemMax, dropZone != AnimationDropZone::BEFORE); + drop_line_draw(ImGui::GetWindowDrawList(), itemMin, itemMax, dropZone != DROP_BEFORE); isDelivered = ImGui::GetDragDropPayload()->IsDelivery(); if (isDelivered) animation_move_command_push( - panel, items, - {.groupId = groupId, .isAfter = dropZone != AnimationDropZone::BEFORE, .isInto = isDropIntoGroup}); + panel, items, {.groupId = groupId, .isAfter = dropZone != DROP_BEFORE, .isInto = isDropIntoGroup}); } ImGui::EndDragDropTarget(); return isDelivered; diff --git a/src/ui/draw.cpp b/src/ui/draw.cpp index 8971f78..cc5bb6e 100644 --- a/src/ui/draw.cpp +++ b/src/ui/draw.cpp @@ -26,6 +26,16 @@ bool is_drop_after(ImVec2 min, ImVec2 max) return ImGui::GetIO().MousePos.y >= (min.y + max.y) * 0.5f; } +// Where the mouse is over a row: its top third before it, its bottom third after it, between them inside. +DropZone drop_zone_get(ImVec2 min, ImVec2 max) +{ + auto third = (max.y - min.y) / 3.0f; + auto mouseY = ImGui::GetIO().MousePos.y; + if (mouseY < min.y + third) + return DROP_BEFORE; + return mouseY >= max.y - third ? DROP_AFTER : DROP_INSIDE; +} + void drop_box_draw(ImDrawList* drawList, ImVec2 min, ImVec2 max) { if (!drawList) diff --git a/src/ui/draw.h b/src/ui/draw.h index 1c7efe7..ab28443 100644 --- a/src/ui/draw.h +++ b/src/ui/draw.h @@ -3,7 +3,15 @@ #include "vec.h" #include +enum DropZone +{ + DROP_BEFORE, + DROP_INSIDE, + DROP_AFTER +}; + bool is_drop_after(ImVec2, ImVec2); +DropZone drop_zone_get(ImVec2, ImVec2); void drop_box_draw(ImDrawList*, ImVec2, ImVec2); void drop_line_draw(ImDrawList*, ImVec2, ImVec2, bool); void image_premultiplied_draw(ImTextureID, ImVec2); diff --git a/src/ui/timeline_context.h b/src/ui/timeline_context.h index db2459a..b2da481 100644 --- a/src/ui/timeline_context.h +++ b/src/ui/timeline_context.h @@ -55,12 +55,7 @@ constexpr auto TIMELINE_TICK_COLOR_LIGHT = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); constexpr auto TIMELINE_CHILD_BG_COLOR_LIGHT = ImVec4(0.5490f, 0.5490f, 0.5882f, 1.0f); constexpr auto TIMELINE_TEXT_COLOR_LIGHT = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); -enum class DropZone -{ - BEFORE, - INSIDE, - AFTER -}; +constexpr auto ROW_DRAG_DROP = "Timeline Row Drag Drop"; constexpr Vec4 FRAME_COLOR_LIGHT_BASE[] = {{0.80f, 0.80f, 0.80f, 1.0f}, {0.5216f, 0.7333f, 1.0f, 1.0f}, @@ -230,6 +225,7 @@ struct TimelineContext : TimelineState void reference_set_adjacent_item(int direction); std::vector selected_row_references_get(); std::vector row_drag_references_get(const TimelineRow& row); + void row_drag_drop(const TimelineRow&, ImVec2, ImVec2, ImVec2, ImVec2); void item_remove(); std::vector item_references_groupable_get(); void item_group(); diff --git a/src/ui/timeline_tracks.cpp b/src/ui/timeline_tracks.cpp index c0f3a6f..9cabcec 100644 --- a/src/ui/timeline_tracks.cpp +++ b/src/ui/timeline_tracks.cpp @@ -68,45 +68,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index) imguiStyle.HoverDelayNormal = previousTooltipDelay; } - if (ImGui::BeginDragDropSource(DRAG_DROP_SOURCE_FLAGS)) - { - rowDragReferences = row_drag_references_get(row); - ImGui::SetDragDropPayload("Timeline Row Drag Drop", rowDragReferences.data(), - (int)rowDragReferences.size() * (int)sizeof(TimelineRow)); - ImGui::EndDragDropSource(); - } - - if (ImGui::BeginDragDropTarget()) - { - if (auto payload = ImGui::AcceptDragDropPayload("Timeline Row Drag Drop", - ImGuiDragDropFlags_AcceptBeforeDelivery | - ImGuiDragDropFlags_AcceptNoDrawDefaultRect)) - { - auto groupDropThird = (groupButtonMax.y - groupButtonMin.y) / 3.0f; - auto dropZone = DropZone::BEFORE; - if (mousePos.y >= groupButtonMax.y - groupDropThird) - dropZone = DropZone::AFTER; - else if (mousePos.y >= groupButtonMin.y + groupDropThird) - dropZone = DropZone::INSIDE; - auto isDropAfter = dropZone != DropZone::BEFORE; - auto payloadRows = (TimelineRow*)payload->Data; - auto payloadCount = payload->DataSize / sizeof(TimelineRow); - std::vector draggedRows(payloadRows, payloadRows + payloadCount); - auto isDropIntoGroup = dropZone == DropZone::INSIDE && (row.type == LAYER || row.type == NULL_); - for (auto draggedRow : draggedRows) - if (draggedRow.isGroup || draggedRow.type != row.type) - isDropIntoGroup = false; - - if (isDropIntoGroup) - drop_box_draw(ImGui::GetWindowDrawList(), groupChildMin, groupChildMax); - else - drop_line_draw(ImGui::GetWindowDrawList(), groupChildMin, groupChildMax, isDropAfter); - - if (payload->IsDelivery()) - rows_move_to_row(draggedRows, row, isDropAfter, isDropIntoGroup); - } - ImGui::EndDragDropTarget(); - } + row_drag_drop(row, groupButtonMin, groupButtonMax, groupChildMin, groupChildMax); ImGui::SetCursorPos(cursorPos); auto folderIcon = group->isExpanded ? ICON_FOLDER_OPEN : ICON_FOLDER; @@ -244,33 +206,7 @@ void TimelineContext::item_child(const TimelineRow& row, int index) auto isItemButtonHovered = ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem); if (type == LAYER || type == NULL_) - { - if (ImGui::BeginDragDropSource(DRAG_DROP_SOURCE_FLAGS)) - { - rowDragReferences = row_drag_references_get(row); - ImGui::SetDragDropPayload("Timeline Row Drag Drop", rowDragReferences.data(), - (int)rowDragReferences.size() * (int)sizeof(TimelineRow)); - ImGui::EndDragDropSource(); - } - - if (ImGui::BeginDragDropTarget()) - { - if (auto payload = ImGui::AcceptDragDropPayload("Timeline Row Drag Drop", - ImGuiDragDropFlags_AcceptBeforeDelivery | - ImGuiDragDropFlags_AcceptNoDrawDefaultRect)) - { - auto isDropAfter = is_drop_after(itemButtonMin, itemButtonMax); - drop_line_draw(ImGui::GetWindowDrawList(), itemChildMin, itemChildMax, isDropAfter); - - auto payloadRows = (TimelineRow*)payload->Data; - auto payloadCount = payload->DataSize / sizeof(TimelineRow); - std::vector draggedRows(payloadRows, payloadRows + payloadCount); - if (payload->IsDelivery()) - rows_move_to_row(draggedRows, row, isDropAfter, false); - } - ImGui::EndDragDropTarget(); - } - } + row_drag_drop(row, itemButtonMin, itemButtonMax, itemChildMin, itemChildMax); auto animationLayer = type == LAYER ? ::item_get(model.content.layers, id) : nullptr; if (animationLayer && !animationLayer->is_animation()) @@ -615,3 +551,38 @@ void TimelineContext::items_child() } ImGui::EndChild(); } + +// Lets a row be dragged, and dragged rows land before or after it, or into it when it is a group of their kind. +void TimelineContext::row_drag_drop(const TimelineRow& row, ImVec2 buttonMin, ImVec2 buttonMax, ImVec2 childMin, + ImVec2 childMax) +{ + if (ImGui::BeginDragDropSource(DRAG_DROP_SOURCE_FLAGS)) + { + rowDragReferences = row_drag_references_get(row); + ImGui::SetDragDropPayload(ROW_DRAG_DROP, rowDragReferences.data(), + (int)rowDragReferences.size() * (int)sizeof(TimelineRow)); + ImGui::EndDragDropSource(); + } + if (!ImGui::BeginDragDropTarget()) + return; + if (auto payload = ImGui::AcceptDragDropPayload(ROW_DRAG_DROP, ImGuiDragDropFlags_AcceptBeforeDelivery | + ImGuiDragDropFlags_AcceptNoDrawDefaultRect)) + { + auto rows = (TimelineRow*)payload->Data; + std::vector draggedRows(rows, rows + payload->DataSize / sizeof(TimelineRow)); + auto zone = row.isGroup ? drop_zone_get(buttonMin, buttonMax) + : (is_drop_after(buttonMin, buttonMax) ? DROP_AFTER : DROP_BEFORE); + auto isDropAfter = zone != DROP_BEFORE; + auto isDropIntoGroup = zone == DROP_INSIDE && (row.type == LAYER || row.type == NULL_) && + std::ranges::none_of(draggedRows, [&](const TimelineRow& dragged) { + return dragged.isGroup || dragged.type != row.type; + }); + if (isDropIntoGroup) + drop_box_draw(ImGui::GetWindowDrawList(), childMin, childMax); + else + drop_line_draw(ImGui::GetWindowDrawList(), childMin, childMax, isDropAfter); + if (payload->IsDelivery()) + rows_move_to_row(draggedRows, row, isDropAfter, isDropIntoGroup); + } + ImGui::EndDragDropTarget(); +}