Port link creation/detach, box selection, link selection, minimap, query + selection API

This commit is contained in:
4DBug committed 2026-10-17 04:42:22 -05:00
1 parent 6824176a53
commit 16872277f2
2 files changed
+919 -19

No files matched your search

+804 -19
View File
@@ -830,7 +830,89 @@ return function(G)
}
end
-- [Port] TODO: RectangleOverlapsLineSegment / RectangleOverlapsBezier / RectangleOverlapsLink (box selection)
function internal.EvalImplicitLineEq(p1, p2, p)
return (p2.y - p1.y) * p.x + (p1.x - p2.x) * p.y + (p2.x * p1.y - p1.x * p2.y)
end
local function Sign(val)
return (val > 0.0 and 1 or 0) - (val < 0.0 and 1 or 0)
end
function internal.RectangleOverlapsLineSegment(rect, p1, p2)
-- Trivial case: rectangle contains an endpoint
if rect:Contains(p1) or rect:Contains(p2) then
return true
end
-- Flip rectangle if necessary
local flip_rect = ImRect(
math.min(rect.Min.x, rect.Max.x), math.min(rect.Min.y, rect.Max.y),
math.max(rect.Min.x, rect.Max.x), math.max(rect.Min.y, rect.Max.y))
-- Trivial case: line segment lies to one particular side of rectangle
if (p1.x < flip_rect.Min.x and p2.x < flip_rect.Min.x)
or (p1.x > flip_rect.Max.x and p2.x > flip_rect.Max.x)
or (p1.y < flip_rect.Min.y and p2.y < flip_rect.Min.y)
or (p1.y > flip_rect.Max.y and p2.y > flip_rect.Max.y) then
return false
end
local corner_signs = {
Sign(internal.EvalImplicitLineEq(p1, p2, flip_rect.Min)),
Sign(internal.EvalImplicitLineEq(p1, p2, ImVec2(flip_rect.Max.x, flip_rect.Min.y))),
Sign(internal.EvalImplicitLineEq(p1, p2, ImVec2(flip_rect.Min.x, flip_rect.Max.y))),
Sign(internal.EvalImplicitLineEq(p1, p2, flip_rect.Max)),
}
local sum = 0
local sum_abs = 0
for i = 1, 4 do
sum = sum + corner_signs[i]
sum_abs = sum_abs + math.abs(corner_signs[i])
end
-- At least one corner of rectangle lies on a different side of line segment
return math.abs(sum) ~= sum_abs
end
function internal.RectangleOverlapsBezier(rectangle, cb)
local current = internal.EvalCubicBezier(0.0, cb.P0, cb.P1, cb.P2, cb.P3)
local dt = 1.0 / cb.NumSegments
for s = 0, cb.NumSegments - 1 do
local next_ = internal.EvalCubicBezier((s + 1) * dt, cb.P0, cb.P1, cb.P2, cb.P3)
if internal.RectangleOverlapsLineSegment(rectangle, current, next_) then
return true
end
current = next_
end
return false
end
function internal.RectangleOverlapsLink(rectangle, start, end_, start_type)
-- First level: simple rejection test via rectangle overlap:
local lrect = ImRect(
math.min(start.x, end_.x), math.min(start.y, end_.y),
math.max(start.x, end_.x), math.max(start.y, end_.y))
if rectangle:Overlaps(lrect) then
-- First, check if either one or both endpoinds are trivially contained
-- in the rectangle
if rectangle:Contains(start) or rectangle:Contains(end_) then
return true
end
-- Second level of refinement: do a more expensive test against the
-- link
local cubic_bezier = internal.GetCubicBezier(start, end_, start_type, internal.GImNodes.Style.LinkLineSegmentsPerLength)
return internal.RectangleOverlapsBezier(rectangle, cubic_bezier)
end
return false
end
function internal.ScreenSpaceToGridSpace(editor, v)
return v - internal.GImNodes.CanvasOriginScreenSpace - editor.Panning
@@ -856,7 +938,18 @@ return function(G)
return internal.GImNodes.CanvasOriginScreenSpace + v
end
-- [Port] TODO: MiniMapSpaceToGridSpace / ScreenSpaceToMiniMapSpace (minimap)
function internal.MiniMapSpaceToGridSpace(editor, v)
return (v - editor.MiniMapContentScreenSpace.Min) / editor.MiniMapScaling + editor.GridContentBounds.Min
end
function internal.ScreenSpaceToMiniMapSpace(editor, v)
return (internal.ScreenSpaceToGridSpace(editor, v) - editor.GridContentBounds.Min) * editor.MiniMapScaling
+ editor.MiniMapContentScreenSpace.Min
end
function internal.ScreenSpaceToMiniMapSpaceRect(editor, r)
return ImRect(internal.ScreenSpaceToMiniMapSpace(editor, r.Min), internal.ScreenSpaceToMiniMapSpace(editor, r.Max))
end
end
---------------------------------------------------------------------------
@@ -1131,7 +1224,68 @@ return function(G)
end
end
-- [Port] TODO: BeginLinkSelection / BeginLinkDetach / BeginLinkCreation / BeginLinkInteraction (link creation, selection)
function internal.BeginLinkSelection(editor, link_idx)
editor.ClickInteraction.Type = ClickType.Link
-- When a link is selected, clear all other selections, and insert the link
-- as the sole selection.
editor.SelectedNodeIndices:clear()
editor.SelectedLinkIndices:clear()
editor.SelectedLinkIndices:push_back(link_idx)
end
function internal.BeginLinkDetach(editor, link_idx, detach_pin_idx)
local link = editor.Links.Pool[link_idx]
local state = editor.ClickInteraction
state.Type = ClickType.LinkCreation
state.LinkCreation.EndPinIdx = INVALID_INDEX
state.LinkCreation.StartPinIdx = if detach_pin_idx == link.StartPinIdx then link.EndPinIdx else link.StartPinIdx
internal.GImNodes.DeletedLinkIdx = link_idx
end
function internal.BeginLinkCreation(editor, hovered_pin_idx)
editor.ClickInteraction.Type = ClickType.LinkCreation
editor.ClickInteraction.LinkCreation.StartPinIdx = hovered_pin_idx
editor.ClickInteraction.LinkCreation.EndPinIdx = INVALID_INDEX
editor.ClickInteraction.LinkCreation.Type = internal.ImNodesLinkCreationType.Standard
local g = internal.GImNodes
g.ImNodesUIState = bit32.bor(g.ImNodesUIState, internal.ImNodesUIState.LinkStarted)
end
-- [Port] pin_idx is optional (nil or INVALID_INDEX = no value)
function internal.BeginLinkInteraction(editor, link_idx, pin_idx)
local g = internal.GImNodes
-- Check if we are clicking the link with the modifier pressed.
-- This will in a link detach via clicking.
local modifier = g.Io.LinkDetachWithModifierClick.Modifier
local modifier_pressed = modifier ~= nil and modifier()
if modifier_pressed then
local link = editor.Links.Pool[link_idx]
local start_pin = editor.Pins.Pool[link.StartPinIdx]
local end_pin = editor.Pins.Pool[link.EndPinIdx]
local mouse_pos = g.MousePos
local dist_to_start = G.ImLengthSqr(start_pin.Pos - mouse_pos)
local dist_to_end = G.ImLengthSqr(end_pin.Pos - mouse_pos)
local closest_pin_idx = if dist_to_start < dist_to_end then link.StartPinIdx else link.EndPinIdx
editor.ClickInteraction.Type = ClickType.LinkCreation
internal.BeginLinkDetach(editor, link_idx, closest_pin_idx)
editor.ClickInteraction.LinkCreation.Type = internal.ImNodesLinkCreationType.FromDetach
elseif pin_idx ~= nil and pin_idx ~= INVALID_INDEX then
local hovered_pin_flags = editor.Pins.Pool[pin_idx].Flags
-- Check the 'click and drag to detach' case.
if bit32.band(hovered_pin_flags, ImNodes.ImNodesAttributeFlags.EnableLinkDetachWithDragClick) ~= 0 then
internal.BeginLinkDetach(editor, link_idx, pin_idx)
editor.ClickInteraction.LinkCreation.Type = internal.ImNodesLinkCreationType.FromDetach
else
internal.BeginLinkCreation(editor, pin_idx)
end
else
internal.BeginLinkSelection(editor, link_idx)
end
end
function internal.BeginCanvasInteraction(editor)
local g = internal.GImNodes
@@ -1150,11 +1304,57 @@ return function(G)
if started_panning then
editor.ClickInteraction.Type = ClickType.Panning
elseif g.LeftMouseClicked then
-- [Port] TODO: box selection (ImNodesClickInteractionType_BoxSelection)
editor.ClickInteraction.Type = ClickType.BoxSelection
editor.ClickInteraction.BoxSelector.Rect.Min = internal.ScreenSpaceToGridSpace(editor, g.MousePos)
end
end
-- [Port] TODO: BoxSelectorUpdateSelection
function internal.BoxSelectorUpdateSelection(editor, box_rect)
-- Invert box selector coordinates as needed
box_rect = ImRect(
math.min(box_rect.Min.x, box_rect.Max.x), math.min(box_rect.Min.y, box_rect.Max.y),
math.max(box_rect.Min.x, box_rect.Max.x), math.max(box_rect.Min.y, box_rect.Max.y))
-- Update node selection
editor.SelectedNodeIndices:clear()
-- Test for overlap against node rectangles
for node_idx = 1, editor.Nodes.Pool.Size do
if editor.Nodes.InUse[node_idx] then
local node = editor.Nodes.Pool[node_idx]
if box_rect:Overlaps(node.Rect) then
editor.SelectedNodeIndices:push_back(node_idx)
end
end
end
-- Update link selection
editor.SelectedLinkIndices:clear()
-- Test for overlap against links
for link_idx = 1, editor.Links.Pool.Size do
if editor.Links.InUse[link_idx] then
local link = editor.Links.Pool[link_idx]
local pin_start = editor.Pins.Pool[link.StartPinIdx]
local pin_end = editor.Pins.Pool[link.EndPinIdx]
local node_start_rect = editor.Nodes.Pool[pin_start.ParentNodeIdx].Rect
local node_end_rect = editor.Nodes.Pool[pin_end.ParentNodeIdx].Rect
local start = internal.GetScreenSpacePinCoordinates(node_start_rect, pin_start.AttributeRect, pin_start.Type)
local end_ = internal.GetScreenSpacePinCoordinates(node_end_rect, pin_end.AttributeRect, pin_end.Type)
-- Test
if internal.RectangleOverlapsLink(box_rect, start, end_, pin_start.Type) then
editor.SelectedLinkIndices:push_back(link_idx)
end
end
end
end
function internal.SnapOriginToGrid(origin)
local g = internal.GImNodes
@@ -1199,14 +1399,146 @@ return function(G)
end
end
-- [Port] TODO: LinkPredicate / FindDuplicateLink / ShouldLinkSnapToPin (link creation)
-- [Port] LinkPredicate folded in: links are equal if they join the same pins, in either order.
function internal.FindDuplicateLink(editor, start_pin_idx, end_pin_idx)
local lo, hi = math.min(start_pin_idx, end_pin_idx), math.max(start_pin_idx, end_pin_idx)
for link_idx = 1, editor.Links.Pool.Size do
local link = editor.Links.Pool[link_idx]
if editor.Links.InUse[link_idx]
and math.min(link.StartPinIdx, link.EndPinIdx) == lo
and math.max(link.StartPinIdx, link.EndPinIdx) == hi then
return link_idx
end
end
return INVALID_INDEX
end
function internal.ShouldLinkSnapToPin(editor, start_pin, hovered_pin_idx, duplicate_link)
local end_pin = editor.Pins.Pool[hovered_pin_idx]
-- The end pin must be in a different node
if start_pin.ParentNodeIdx == end_pin.ParentNodeIdx then
return false
end
-- The end pin must be of a different type
if start_pin.Type == end_pin.Type then
return false
end
-- The link to be created must not be a duplicate, unless it is the link which was created on
-- snap. In that case we want to snap, since we want it to appear visually as if the created
-- link remains snapped to the pin.
if duplicate_link ~= INVALID_INDEX and duplicate_link ~= internal.GImNodes.SnapLinkIdx then
return false
end
return true
end
function internal.GetScreenSpacePinCoordinatesForPin(editor, pin)
local parent_node_rect = editor.Nodes.Pool[pin.ParentNodeIdx].Rect
return internal.GetScreenSpacePinCoordinates(parent_node_rect, pin.AttributeRect, pin.Type)
end
local function UpdateLinkCreation(editor)
local g = internal.GImNodes
local lc = editor.ClickInteraction.LinkCreation
local start_pin = editor.Pins.Pool[lc.StartPinIdx]
local hovered = g.HoveredPinIdx ~= INVALID_INDEX
local maybe_duplicate_link_idx = if hovered then internal.FindDuplicateLink(editor, lc.StartPinIdx, g.HoveredPinIdx) else INVALID_INDEX
local should_snap = hovered and internal.ShouldLinkSnapToPin(editor, start_pin, g.HoveredPinIdx, maybe_duplicate_link_idx)
-- If we created on snap and the hovered pin is empty or changed, then we need signal that
-- the link's state has changed.
local snapping_pin_changed = lc.EndPinIdx ~= INVALID_INDEX and g.HoveredPinIdx ~= lc.EndPinIdx
-- Detach the link that was created by this link event if it's no longer in snap range
if snapping_pin_changed and g.SnapLinkIdx ~= INVALID_INDEX then
internal.BeginLinkDetach(editor, g.SnapLinkIdx, lc.EndPinIdx)
end
local start_pos = internal.GetScreenSpacePinCoordinatesForPin(editor, start_pin)
-- If we are within the hover radius of a receiving pin, snap the link
-- endpoint to it
local end_pos = if should_snap then internal.GetScreenSpacePinCoordinatesForPin(editor, editor.Pins.Pool[g.HoveredPinIdx]) else g.MousePos
local cubic_bezier = internal.GetCubicBezier(start_pos, end_pos, start_pin.Type, g.Style.LinkLineSegmentsPerLength)
g.CanvasDrawList:AddBezierCubic(cubic_bezier.P0, cubic_bezier.P1, cubic_bezier.P2, cubic_bezier.P3,
g.Style.Colors[ImNodes.ImNodesCol.Link], g.Style.LinkThickness, cubic_bezier.NumSegments)
local link_creation_on_snap = hovered
and bit32.band(editor.Pins.Pool[g.HoveredPinIdx].Flags, ImNodes.ImNodesAttributeFlags.EnableLinkCreationOnSnap) ~= 0
if not should_snap then
lc.EndPinIdx = INVALID_INDEX
end
local create_link = should_snap and (g.LeftMouseReleased or link_creation_on_snap)
if create_link and maybe_duplicate_link_idx == INVALID_INDEX then
-- Avoid send OnLinkCreated() events every frame if the snap link is not saved
-- (only applies for EnableLinkCreationOnSnap)
if not g.LeftMouseReleased and lc.EndPinIdx == g.HoveredPinIdx then
return
end
g.ImNodesUIState = bit32.bor(g.ImNodesUIState, internal.ImNodesUIState.LinkCreated)
lc.EndPinIdx = g.HoveredPinIdx
end
if g.LeftMouseReleased then
editor.ClickInteraction.Type = ClickType.None
if not create_link then
g.ImNodesUIState = bit32.bor(g.ImNodesUIState, internal.ImNodesUIState.LinkDropped)
end
end
end
function internal.ClickInteractionUpdate(editor)
local g = internal.GImNodes
local t = editor.ClickInteraction.Type
if t == ClickType.BoxSelection then
-- [Port] TODO: box selection
local selector = editor.ClickInteraction.BoxSelector
selector.Rect.Max = internal.ScreenSpaceToGridSpace(editor, g.MousePos)
local box_rect = ImRect(internal.GridSpaceToScreenSpace(editor, selector.Rect.Min), internal.GridSpaceToScreenSpace(editor, selector.Rect.Max))
internal.BoxSelectorUpdateSelection(editor, box_rect)
local box_selector_color = g.Style.Colors[ImNodes.ImNodesCol.BoxSelector]
local box_selector_outline = g.Style.Colors[ImNodes.ImNodesCol.BoxSelectorOutline]
g.CanvasDrawList:AddRectFilled(box_rect.Min, box_rect.Max, box_selector_color)
g.CanvasDrawList:AddRect(box_rect.Min, box_rect.Max, box_selector_outline)
if g.LeftMouseReleased then
local depth_stack = editor.NodeDepthOrder
local selected_idxs = editor.SelectedNodeIndices
-- Bump the selected node indices, in order, to the top of the depth stack.
-- NOTE: this algorithm has worst case time complexity of O(N^2), if the node selection
-- is ~ N (due to selected_idxs.contains()).
if selected_idxs.Size > 0 and selected_idxs.Size < depth_stack.Size then
local num_moved = 0 -- The number of indices moved. Stop after selected_idxs.Size
for i = 1, depth_stack.Size - selected_idxs.Size do
local node_idx = depth_stack[i]
while selected_idxs:contains(node_idx) do
depth_stack:erase(i)
depth_stack:push_back(node_idx)
num_moved = num_moved + 1
node_idx = depth_stack[i]
end
if num_moved == selected_idxs.Size then
break
end
end
end
editor.ClickInteraction.Type = ClickType.None
end
elseif t == ClickType.Node then
@@ -1220,10 +1552,7 @@ return function(G)
editor.ClickInteraction.Type = ClickType.None
end
elseif t == ClickType.LinkCreation then
-- [Port] TODO: link creation (partial link drawing, snapping, LinkCreated/LinkDropped events)
if g.LeftMouseReleased then
editor.ClickInteraction.Type = ClickType.None
end
UpdateLinkCreation(editor)
elseif t == ClickType.Panning then
local dragging = g.AltMouseDragging
@@ -1639,13 +1968,219 @@ return function(G)
node.PinIndices:push_back(g.CurrentPinIdx)
end
-- [Port] TODO: [SECTION] minimap (IsMiniMapActive, CalcMiniMapLayout, MiniMapUpdate, ...)
end
---------------------------------------------------------------------------
-- [SECTION] minimap
---------------------------------------------------------------------------
do
local Col = ImNodes.ImNodesCol
local Loc = ImNodes.ImNodesMiniMapLocation
local ClickType = internal.ImNodesClickInteractionType
local function ImFloorVec2(v)
return ImVec2(math.floor(v.x), math.floor(v.y))
end
function internal.IsMiniMapActive()
return false
local editor = internal.EditorContextGet()
return editor.MiniMapEnabled and editor.MiniMapSizeFraction > 0.0
end
function internal.IsMiniMapHovered()
return false
local editor = internal.EditorContextGet()
return internal.IsMiniMapActive()
and ImGui.IsMouseHoveringRect(editor.MiniMapRectScreenSpace.Min, editor.MiniMapRectScreenSpace.Max)
end
function internal.CalcMiniMapLayout()
local g = internal.GImNodes
local editor = internal.EditorContextGet()
local offset = g.Style.MiniMapOffset
local border = g.Style.MiniMapPadding
local editor_rect = g.CanvasRectScreenSpace
-- Compute the size of the mini-map area
local mini_map_size, mini_map_scaling
do
local max_size = ImFloorVec2(editor_rect:GetSize() * editor.MiniMapSizeFraction - border * 2.0)
local max_size_aspect_ratio = max_size.x / max_size.y
local grid_content_size = if editor.GridContentBounds:IsInverted() then max_size else ImFloorVec2(editor.GridContentBounds:GetSize())
local grid_content_aspect_ratio = grid_content_size.x / grid_content_size.y
mini_map_size = ImFloorVec2(if grid_content_aspect_ratio > max_size_aspect_ratio
then ImVec2(max_size.x, max_size.x / grid_content_aspect_ratio)
else ImVec2(max_size.y * grid_content_aspect_ratio, max_size.y))
mini_map_scaling = mini_map_size.x / grid_content_size.x
end
-- Compute location of the mini-map
local mini_map_pos
do
local loc = editor.MiniMapLocation
local align = ImVec2(
if loc == Loc.BottomRight or loc == Loc.TopRight then 1.0 else 0.0,
if loc == Loc.BottomRight or loc == Loc.BottomLeft then 1.0 else 0.0)
local top_left_pos = editor_rect.Min + offset + border
local bottom_right_pos = editor_rect.Max - offset - border - mini_map_size
mini_map_pos = ImFloorVec2(ImVec2(
top_left_pos.x + (bottom_right_pos.x - top_left_pos.x) * align.x,
top_left_pos.y + (bottom_right_pos.y - top_left_pos.y) * align.y))
end
editor.MiniMapRectScreenSpace = ImRect(mini_map_pos - border, mini_map_pos + mini_map_size + border)
editor.MiniMapContentScreenSpace = ImRect(mini_map_pos, mini_map_pos + mini_map_size)
editor.MiniMapScaling = mini_map_scaling
end
local function MiniMapDrawNode(editor, node_idx)
local g = internal.GImNodes
local node = editor.Nodes.Pool[node_idx]
local node_rect = internal.ScreenSpaceToMiniMapSpaceRect(editor, node.Rect)
-- Round to near whole pixel value for corner-rounding to prevent visual glitches
local mini_map_node_rounding = math.floor(node.LayoutStyle.CornerRounding * editor.MiniMapScaling)
local mini_map_node_background
if editor.ClickInteraction.Type == ClickType.None and ImGui.IsMouseHoveringRect(node_rect.Min, node_rect.Max) then
mini_map_node_background = g.Style.Colors[Col.MiniMapNodeBackgroundHovered]
-- Run user callback when hovering a mini-map node
if editor.MiniMapNodeHoveringCallback then
editor.MiniMapNodeHoveringCallback(node.Id, editor.MiniMapNodeHoveringCallbackUserData)
end
elseif editor.SelectedNodeIndices:contains(node_idx) then
mini_map_node_background = g.Style.Colors[Col.MiniMapNodeBackgroundSelected]
else
mini_map_node_background = g.Style.Colors[Col.MiniMapNodeBackground]
end
local mini_map_node_outline = g.Style.Colors[Col.MiniMapNodeOutline]
g.CanvasDrawList:AddRectFilled(node_rect.Min, node_rect.Max, mini_map_node_background, mini_map_node_rounding)
g.CanvasDrawList:AddRect(node_rect.Min, node_rect.Max, mini_map_node_outline, mini_map_node_rounding)
end
local function MiniMapDrawLink(editor, link_idx)
local g = internal.GImNodes
local link = editor.Links.Pool[link_idx]
local start_pin = editor.Pins.Pool[link.StartPinIdx]
local end_pin = editor.Pins.Pool[link.EndPinIdx]
local cubic_bezier = internal.GetCubicBezier(
internal.ScreenSpaceToMiniMapSpace(editor, start_pin.Pos),
internal.ScreenSpaceToMiniMapSpace(editor, end_pin.Pos),
start_pin.Type,
g.Style.LinkLineSegmentsPerLength / editor.MiniMapScaling)
-- It's possible for a link to be deleted in begin_link_interaction. A user
-- may detach a link, resulting in the link wire snapping to the mouse
-- position.
--
-- In other words, skip rendering the link if it was deleted.
if g.DeletedLinkIdx == link_idx then
return
end
local link_color = g.Style.Colors[if editor.SelectedLinkIndices:contains(link_idx) then Col.MiniMapLinkSelected else Col.MiniMapLink]
g.CanvasDrawList:AddBezierCubic(cubic_bezier.P0, cubic_bezier.P1, cubic_bezier.P2, cubic_bezier.P3,
link_color, g.Style.LinkThickness * editor.MiniMapScaling, cubic_bezier.NumSegments)
end
function internal.MiniMapUpdate()
local g = internal.GImNodes
local editor = internal.EditorContextGet()
local mini_map_background
if internal.IsMiniMapHovered() then
mini_map_background = g.Style.Colors[Col.MiniMapBackgroundHovered]
else
mini_map_background = g.Style.Colors[Col.MiniMapBackground]
end
-- Create a child window bellow mini-map, so it blocks all mouse interaction on canvas.
local flags = G.ImGuiWindowFlags.NoBackground
ImGui.SetCursorScreenPos(editor.MiniMapRectScreenSpace.Min)
ImGui.BeginChild("minimap", editor.MiniMapRectScreenSpace:GetSize(), false, flags)
local mini_map_rect = editor.MiniMapRectScreenSpace
-- Draw minimap background and border
g.CanvasDrawList:AddRectFilled(mini_map_rect.Min, mini_map_rect.Max, mini_map_background)
g.CanvasDrawList:AddRect(mini_map_rect.Min, mini_map_rect.Max, g.Style.Colors[Col.MiniMapOutline])
-- Clip draw list items to mini-map rect (after drawing background/outline)
g.CanvasDrawList:PushClipRect(mini_map_rect.Min, mini_map_rect.Max, true --[[ intersect with editor clip-rect ]])
-- Draw links first so they appear under nodes, and we can use the same draw channel
for link_idx = 1, editor.Links.Pool.Size do
if editor.Links.InUse[link_idx] then
MiniMapDrawLink(editor, link_idx)
end
end
for node_idx = 1, editor.Nodes.Pool.Size do
if editor.Nodes.InUse[node_idx] then
MiniMapDrawNode(editor, node_idx)
end
end
-- Draw editor canvas rect inside mini-map
do
local canvas_color = g.Style.Colors[Col.MiniMapCanvas]
local outline_color = g.Style.Colors[Col.MiniMapCanvasOutline]
local rect = internal.ScreenSpaceToMiniMapSpaceRect(editor, g.CanvasRectScreenSpace)
g.CanvasDrawList:AddRectFilled(rect.Min, rect.Max, canvas_color)
g.CanvasDrawList:AddRect(rect.Min, rect.Max, outline_color)
end
-- Have to pop mini-map clip rect
g.CanvasDrawList:PopClipRect()
local mini_map_is_hovered = ImGui.IsWindowHovered()
ImGui.EndChild()
local center_on_click = mini_map_is_hovered and ImGui.IsMouseDown(G.ImGuiMouseButton.Left)
and editor.ClickInteraction.Type == ClickType.None
and not g.NodeIdxSubmissionOrder:empty()
if center_on_click then
local target = internal.MiniMapSpaceToGridSpace(editor, ImGui.GetMousePos())
local center = g.CanvasRectScreenSpace:GetSize() * 0.5
editor.Panning = ImFloorVec2(center - target)
end
-- Reset callback info after use
editor.MiniMapNodeHoveringCallback = nil
editor.MiniMapNodeHoveringCallbackUserData = nil
end
end
---------------------------------------------------------------------------
-- [SECTION] selection helpers
---------------------------------------------------------------------------
do
function internal.SelectObject(objects, selected_indices, id)
local idx = internal.ObjectPoolFind(objects, id)
IM_ASSERT(idx >= 0)
IM_ASSERT(not selected_indices:contains(idx))
selected_indices:push_back(idx)
end
function internal.ClearObjectSelection(objects, selected_indices, id)
local idx = internal.ObjectPoolFind(objects, id)
IM_ASSERT(idx >= 0)
IM_ASSERT(selected_indices:contains(idx))
selected_indices:find_erase_unsorted(idx)
end
function internal.IsObjectSelected(objects, selected_indices, id)
local idx = internal.ObjectPoolFind(objects, id)
return selected_indices:contains(idx)
end
end
@@ -1800,15 +2335,18 @@ return function(G)
internal.DrawListAppendClickInteractionChannel()
internal.DrawListActivateClickInteractionChannel()
-- [Port] TODO: minimap (CalcMiniMapLayout / MiniMapUpdate)
if internal.IsMiniMapActive() then
internal.CalcMiniMapLayout()
internal.MiniMapUpdate()
end
-- Handle node graph interaction
if not internal.IsMiniMapHovered() then
if g.LeftMouseClicked and g.HoveredLinkIdx ~= INVALID_INDEX then
-- [Port] TODO: BeginLinkInteraction (link selection / detach)
internal.BeginLinkInteraction(editor, g.HoveredLinkIdx, g.HoveredPinIdx)
elseif g.LeftMouseClicked and g.HoveredPinIdx ~= INVALID_INDEX then
-- [Port] TODO: BeginLinkCreation
internal.BeginLinkCreation(editor, g.HoveredPinIdx)
elseif g.LeftMouseClicked and g.HoveredNodeIdx ~= INVALID_INDEX then
internal.BeginNodeSelection(editor, g.HoveredNodeIdx)
elseif g.LeftMouseClicked or g.LeftMouseReleased or g.AltMouseClicked or g.AltMouseScrollDelta ~= 0.0 then
@@ -2056,8 +2594,255 @@ return function(G)
return ImVec2(origin.x, origin.y) -- [Port] returned by value
end
-- [Port] TODO: MiniMap, PushAttributeFlag/PopAttributeFlag, IsEditorHovered/IsNodeHovered/..., selection API,
-- IsLinkStarted/Dropped/Created/Destroyed, SaveCurrentEditorStateToIniString/Load*
-- [Port] TODO: SaveCurrentEditorStateToIniString/Load*
function ImNodes.MiniMap(minimap_size_fraction, location, node_hovering_callback, node_hovering_callback_data)
minimap_size_fraction = minimap_size_fraction or 0.2
location = location or ImNodes.ImNodesMiniMapLocation.TopLeft
-- Check that editor size fraction is sane; must be in the range (0, 1]
IM_ASSERT(minimap_size_fraction > 0.0 and minimap_size_fraction <= 1.0)
-- Remember to call before EndNodeEditor
IM_ASSERT(internal.GImNodes.CurrentScope == Scope.Editor)
local editor = internal.EditorContextGet()
editor.MiniMapEnabled = true
editor.MiniMapSizeFraction = minimap_size_fraction
editor.MiniMapLocation = location
-- Set node hovering callback information
editor.MiniMapNodeHoveringCallback = node_hovering_callback
editor.MiniMapNodeHoveringCallbackUserData = node_hovering_callback_data
-- Actual drawing/updating of the MiniMap is done in EndNodeEditor so that
-- mini map is draw over everything and all pin/link positions are updated
-- correctly relative to their respective nodes.
end
function ImNodes.PushAttributeFlag(flag)
local g = internal.GImNodes
g.CurrentAttributeFlags = bit32.bor(g.CurrentAttributeFlags, flag)
g.AttributeFlagStack:push_back(g.CurrentAttributeFlags)
end
function ImNodes.PopAttributeFlag()
local g = internal.GImNodes
-- PopAttributeFlag called without a matching PushAttributeFlag!
-- The bottom value is always the default value, pushed in Initialize().
IM_ASSERT(g.AttributeFlagStack.Size > 1)
g.AttributeFlagStack:pop_back()
g.CurrentAttributeFlags = g.AttributeFlagStack:back()
end
function ImNodes.SnapNodeToGrid(node_id)
local editor = internal.EditorContextGet()
local node = internal.ObjectPoolFindOrCreateObject(editor.Nodes, node_id)
node.Origin = internal.SnapOriginToGrid(node.Origin)
end
function ImNodes.IsEditorHovered()
return internal.MouseInCanvas()
end
-- [Port] int* out-params become return values: IsNodeHovered() -> hovered, node_id
function ImNodes.IsNodeHovered()
local g = internal.GImNodes
IM_ASSERT(g.CurrentScope == Scope.None)
if g.HoveredNodeIdx == INVALID_INDEX then
return false, nil
end
return true, internal.EditorContextGet().Nodes.Pool[g.HoveredNodeIdx].Id
end
function ImNodes.IsLinkHovered()
local g = internal.GImNodes
IM_ASSERT(g.CurrentScope == Scope.None)
if g.HoveredLinkIdx == INVALID_INDEX then
return false, nil
end
return true, internal.EditorContextGet().Links.Pool[g.HoveredLinkIdx].Id
end
function ImNodes.IsPinHovered()
local g = internal.GImNodes
IM_ASSERT(g.CurrentScope == Scope.None)
if g.HoveredPinIdx == INVALID_INDEX then
return false, nil
end
return true, internal.EditorContextGet().Pins.Pool[g.HoveredPinIdx].Id
end
function ImNodes.NumSelectedNodes()
IM_ASSERT(internal.GImNodes.CurrentScope == Scope.None)
return internal.EditorContextGet().SelectedNodeIndices.Size
end
function ImNodes.NumSelectedLinks()
IM_ASSERT(internal.GImNodes.CurrentScope == Scope.None)
return internal.EditorContextGet().SelectedLinkIndices.Size
end
-- [Port] returns a new array of ids instead of filling an int* buffer
function ImNodes.GetSelectedNodes()
local editor = internal.EditorContextGet()
local node_ids = table.create(editor.SelectedNodeIndices.Size)
for i = 1, editor.SelectedNodeIndices.Size do
node_ids[i] = editor.Nodes.Pool[editor.SelectedNodeIndices[i]].Id
end
return node_ids
end
function ImNodes.GetSelectedLinks()
local editor = internal.EditorContextGet()
local link_ids = table.create(editor.SelectedLinkIndices.Size)
for i = 1, editor.SelectedLinkIndices.Size do
link_ids[i] = editor.Links.Pool[editor.SelectedLinkIndices[i]].Id
end
return link_ids
end
-- [Port] ClearNodeSelection() / ClearNodeSelection(node_id) overloads merged on nil
function ImNodes.ClearNodeSelection(node_id)
local editor = internal.EditorContextGet()
if node_id == nil then
editor.SelectedNodeIndices:clear()
else
internal.ClearObjectSelection(editor.Nodes, editor.SelectedNodeIndices, node_id)
end
end
function ImNodes.ClearLinkSelection(link_id)
local editor = internal.EditorContextGet()
if link_id == nil then
editor.SelectedLinkIndices:clear()
else
internal.ClearObjectSelection(editor.Links, editor.SelectedLinkIndices, link_id)
end
end
function ImNodes.SelectNode(node_id)
local editor = internal.EditorContextGet()
internal.SelectObject(editor.Nodes, editor.SelectedNodeIndices, node_id)
end
function ImNodes.SelectLink(link_id)
local editor = internal.EditorContextGet()
internal.SelectObject(editor.Links, editor.SelectedLinkIndices, link_id)
end
function ImNodes.IsNodeSelected(node_id)
local editor = internal.EditorContextGet()
return internal.IsObjectSelected(editor.Nodes, editor.SelectedNodeIndices, node_id)
end
function ImNodes.IsLinkSelected(link_id)
local editor = internal.EditorContextGet()
return internal.IsObjectSelected(editor.Links, editor.SelectedLinkIndices, link_id)
end
function ImNodes.IsAttributeActive()
local g = internal.GImNodes
IM_ASSERT(bit32.band(g.CurrentScope, Scope.Node) ~= 0)
if not g.ActiveAttribute then
return false
end
return g.ActiveAttributeId == g.CurrentAttributeId
end
-- -> active, attribute_id
function ImNodes.IsAnyAttributeActive()
local g = internal.GImNodes
IM_ASSERT(bit32.band(g.CurrentScope, bit32.bor(Scope.Node, Scope.Attribute)) == 0)
if not g.ActiveAttribute then
return false, nil
end
return true, g.ActiveAttributeId
end
-- -> started, started_at_id
function ImNodes.IsLinkStarted()
local g = internal.GImNodes
-- Call this function after EndNodeEditor()!
IM_ASSERT(g.CurrentScope == Scope.None)
if bit32.band(g.ImNodesUIState, internal.ImNodesUIState.LinkStarted) == 0 then
return false, nil
end
local editor = internal.EditorContextGet()
return true, editor.Pins.Pool[editor.ClickInteraction.LinkCreation.StartPinIdx].Id
end
-- -> dropped, started_at_id
function ImNodes.IsLinkDropped(including_detached_links)
local g = internal.GImNodes
-- Call this function after EndNodeEditor()!
IM_ASSERT(g.CurrentScope == Scope.None)
if including_detached_links == nil then
including_detached_links = true
end
local editor = internal.EditorContextGet()
local link_dropped = bit32.band(g.ImNodesUIState, internal.ImNodesUIState.LinkDropped) ~= 0
and (including_detached_links or editor.ClickInteraction.LinkCreation.Type ~= internal.ImNodesLinkCreationType.FromDetach)
if not link_dropped then
return false, nil
end
return true, editor.Pins.Pool[editor.ClickInteraction.LinkCreation.StartPinIdx].Id
end
-- Returns created, start_pin, start_node, end_pin, end_node, from_snap (start is always the output pin)
local function LinkCreatedInfo()
local g = internal.GImNodes
IM_ASSERT(g.CurrentScope == Scope.None)
if bit32.band(g.ImNodesUIState, internal.ImNodesUIState.LinkCreated) == 0 then
return false
end
local editor = internal.EditorContextGet()
local lc = editor.ClickInteraction.LinkCreation
local start_pin = editor.Pins.Pool[lc.StartPinIdx]
local end_pin = editor.Pins.Pool[lc.EndPinIdx]
if start_pin.Type ~= internal.ImNodesAttributeType.Output then
start_pin, end_pin = end_pin, start_pin
end
local from_snap = editor.ClickInteraction.Type == ClickType.LinkCreation
return true, start_pin.Id, editor.Nodes.Pool[start_pin.ParentNodeIdx].Id,
end_pin.Id, editor.Nodes.Pool[end_pin.ParentNodeIdx].Id, from_snap
end
-- [Port] -> created, started_at_attribute_id, ended_at_attribute_id, created_from_snap
function ImNodes.IsLinkCreated()
local created, start_pin, _, end_pin, _, from_snap = LinkCreatedInfo()
return created, start_pin, end_pin, from_snap
end
-- [Port] node-id overload -> created, started_at_node_id, started_at_attribute_id,
-- ended_at_node_id, ended_at_attribute_id, created_from_snap
function ImNodes.IsLinkCreatedWithNodes()
local created, start_pin, start_node, end_pin, end_node, from_snap = LinkCreatedInfo()
return created, start_node, start_pin, end_node, end_pin, from_snap
end
-- -> destroyed, link_id
function ImNodes.IsLinkDestroyed()
local g = internal.GImNodes
IM_ASSERT(g.CurrentScope == Scope.None)
if g.DeletedLinkIdx == INVALID_INDEX then
return false, nil
end
return true, internal.EditorContextGet().Links.Pool[g.DeletedLinkIdx].Id
end
end
return ImNodes
+115
View File
@@ -0,0 +1,115 @@
local RS = game:GetService("ReplicatedStorage")
local G = require(RS:WaitForChild("ImGui"))
local ImNodes = require(RS:WaitForChild("ImNodes"))(G)
local ImGui, ImVec2 = G.ImGui, G.ImVec2
local Impl = G.ImGui_ImplRoblox
ImGui.CreateContext()
Impl.Init()
Impl.SetRenderRate(0)
ImNodes.CreateContext()
local io = ImGui.GetIO()
local internal = ImNodes.internal
local links = {} -- id -> {start, end}
local minimap = false
local created
local function frame(n, before)
Impl.NewFrame()
io.DeltaTime = 1 / 60
if before then
before()
end
ImGui.NewFrame()
ImGui.SetNextWindowPos(ImVec2(0, 0), G.ImGuiCond.Always)
ImGui.SetNextWindowSize(ImVec2(800, 600), G.ImGuiCond.Always)
ImGui.Begin("editor")
ImNodes.BeginNodeEditor()
if n == 1 then
ImNodes.SetNodeGridSpacePos(1, ImVec2(50, 50))
ImNodes.SetNodeGridSpacePos(2, ImVec2(300, 100))
end
ImNodes.BeginNode(1)
ImNodes.BeginNodeTitleBar()
ImGui.Text("node A")
ImNodes.EndNodeTitleBar()
ImNodes.BeginOutputAttribute(11)
ImGui.Text("out")
ImNodes.EndOutputAttribute()
ImNodes.EndNode()
ImNodes.BeginNode(2)
ImNodes.BeginNodeTitleBar()
ImGui.Text("node B")
ImNodes.EndNodeTitleBar()
ImNodes.BeginInputAttribute(21)
ImGui.Text("in")
ImNodes.EndInputAttribute()
ImNodes.EndNode()
for id, l in links do
ImNodes.Link(id, l[1], l[2])
end
if minimap then
ImNodes.MiniMap(0.2, ImNodes.ImNodesMiniMapLocation.BottomRight, function(id) assert(id == 1 or id == 2) end)
end
ImNodes.EndNodeEditor()
local ok, s, e, snap = ImNodes.IsLinkCreated()
if ok then
created = { s, e, snap }
links[100] = { s, e }
end
ImGui.End()
ImGui.Render()
local dd = ImGui.GetDrawData()
Impl.RenderDrawData(dd)
return dd.TotalVtxCount
end
local function pinPos(id)
local editor = internal.EditorContextGet()
return editor.Pins.Pool[editor.Pins.IdMap[id]].Pos
end
for n = 1, 5 do
frame(n)
end
-- drag from output pin 11 to input pin 21
local a, b = pinPos(11), pinPos(21)
frame(6, function() io:AddMousePosEvent(a.x, a.y) end)
frame(7, function() io:AddMouseButtonEvent(0, true) end)
local started, start_id = ImNodes.IsLinkStarted()
assert(started and start_id == 11, "link not started")
for i = 1, 5 do
frame(7 + i, function() io:AddMousePosEvent(a.x + (b.x - a.x) * i / 5, a.y + (b.y - a.y) * i / 5) end)
end
frame(13, function() io:AddMouseButtonEvent(0, false) end)
assert(created, "IsLinkCreated never fired")
assert(created[1] == 11 and created[2] == 21 and created[3] == false, `created: {created[1]},{created[2]},{created[3]}`)
frame(14)
assert(ImNodes.IsLinkCreated() == false)
-- box select over both nodes from empty canvas
local canvas = internal.GImNodes.CanvasRectScreenSpace
local p0, p1 = canvas.Min + ImVec2(5, 5), canvas.Max - ImVec2(5, 5)
frame(15, function() io:AddMousePosEvent(p0.x, p0.y) end)
frame(16, function() io:AddMouseButtonEvent(0, true) end)
frame(17, function() io:AddMousePosEvent(p1.x, p1.y) end)
frame(18, function() io:AddMouseButtonEvent(0, false) end)
assert(ImNodes.NumSelectedNodes() == 2, `selected nodes: {ImNodes.NumSelectedNodes()}`)
assert(#ImNodes.GetSelectedNodes() == 2)
assert(ImNodes.NumSelectedLinks() == 1 and ImNodes.GetSelectedLinks()[1] == 100)
ImNodes.ClearNodeSelection()
ImNodes.ClearLinkSelection()
ImNodes.SelectNode(2)
assert(ImNodes.IsNodeSelected(2) and not ImNodes.IsNodeSelected(1))
-- minimap renders
minimap = true
for n = 19, 28 do
assert(frame(n) > 0)
end
assert(internal.EditorContextGet().MiniMapScaling > 0)
ImNodes.DestroyContext()
print("OK")