diff --git a/ImNodes.lua b/ImNodes.lua index 5187e0e..5a629de 100644 --- a/ImNodes.lua +++ b/ImNodes.lua @@ -2,7 +2,679 @@ -- Usage: local ImNodes = require(path.to.ImNodes)(require(path.to.ImGui)) return function(G) local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4 + local ImRect, ImVector, IM_COL32, IM_ASSERT = G.ImRect, G.ImVector, G.IM_COL32, G.IM_ASSERT local ImNodes = {} - -- [Port] TODO: port from tools/upstream + local internal = {} -- [Port] imnodes_internal.h contents (kept off the public table, out of the 200-locals budget) + ImNodes.internal = internal + + local INT_MAX = 2147483647 + + --------------------------------------------------------------------------- + -- imnodes.h enums + --------------------------------------------------------------------------- + do + ImNodes.ImNodesCol = { + NodeBackground = 0, + NodeBackgroundHovered = 1, + NodeBackgroundSelected = 2, + NodeOutline = 3, + TitleBar = 4, + TitleBarHovered = 5, + TitleBarSelected = 6, + Link = 7, + LinkHovered = 8, + LinkSelected = 9, + Pin = 10, + PinHovered = 11, + BoxSelector = 12, + BoxSelectorOutline = 13, + GridBackground = 14, + GridLine = 15, + GridLinePrimary = 16, + MiniMapBackground = 17, + MiniMapBackgroundHovered = 18, + MiniMapOutline = 19, + MiniMapOutlineHovered = 20, + MiniMapNodeBackground = 21, + MiniMapNodeBackgroundHovered = 22, + MiniMapNodeBackgroundSelected = 23, + MiniMapNodeOutline = 24, + MiniMapLink = 25, + MiniMapLinkSelected = 26, + MiniMapCanvas = 27, + MiniMapCanvasOutline = 28, + COUNT = 29, + } + + ImNodes.ImNodesStyleVar = { + GridSpacing = 0, + NodeCornerRounding = 1, + NodePadding = 2, + NodeBorderThickness = 3, + LinkThickness = 4, + LinkLineSegmentsPerLength = 5, + LinkHoverDistance = 6, + PinCircleRadius = 7, + PinQuadSideLength = 8, + PinTriangleSideLength = 9, + PinLineThickness = 10, + PinHoverRadius = 11, + PinOffset = 12, + MiniMapPadding = 13, + MiniMapOffset = 14, + COUNT = 15, + } + + ImNodes.ImNodesStyleFlags = { + None = 0, + NodeOutline = bit32.lshift(1, 0), + GridLines = bit32.lshift(1, 2), + GridLinesPrimary = bit32.lshift(1, 3), + GridSnapping = bit32.lshift(1, 4), + } + + ImNodes.ImNodesPinShape = { + Circle = 0, + CircleFilled = 1, + Triangle = 2, + TriangleFilled = 3, + Quad = 4, + QuadFilled = 5, + } + + -- This enum controls the way the attribute pins behave. + ImNodes.ImNodesAttributeFlags = { + None = 0, + -- Allow detaching a link by left-clicking and dragging the link at a pin it is connected to. + -- NOTE: the user has to actually delete the link for this to work. A deleted link can be + -- detected by calling IsLinkDestroyed() after EndNodeEditor(). + EnableLinkDetachWithDragClick = bit32.lshift(1, 0), + -- Visual snapping of an in progress link will trigger IsLink Created/Destroyed events. Allows + -- for previewing the creation of a link while dragging it across attributes. See here for demo: + -- https://github.com/Nelarius/imnodes/issues/41#issuecomment-647132113 NOTE: the user has to + -- actually delete the link for this to work. A deleted link can be detected by calling + -- IsLinkDestroyed() after EndNodeEditor(). + EnableLinkCreationOnSnap = bit32.lshift(1, 1), + } + + ImNodes.ImNodesMiniMapLocation = { + BottomLeft = 0, + BottomRight = 1, + TopLeft = 2, + TopRight = 3, + } + end + + --------------------------------------------------------------------------- + -- imnodes_internal.h: [SECTION] internal enums + --------------------------------------------------------------------------- + do + internal.ImNodesScope = { + None = 1, + Editor = bit32.lshift(1, 1), + Node = bit32.lshift(1, 2), + Attribute = bit32.lshift(1, 3), + } + + internal.ImNodesAttributeType = { + None = 0, + Input = 1, + Output = 2, + } + + internal.ImNodesUIState = { + None = 0, + LinkStarted = bit32.lshift(1, 0), + LinkDropped = bit32.lshift(1, 1), + LinkCreated = bit32.lshift(1, 2), + } + + internal.ImNodesClickInteractionType = { + Node = 0, + Link = 1, + LinkCreation = 2, + Panning = 3, + BoxSelection = 4, + ImGuiItem = 5, + None = 6, + } + + internal.ImNodesLinkCreationType = { + Standard = 0, + FromDetach = 1, + } + end + + --------------------------------------------------------------------------- + -- ImNodesIO / ImNodesStyle + --------------------------------------------------------------------------- + do + -- [Port] `const bool* Modifier` has no Luau equivalent: Modifier is nil (NULL) or a function returning bool, + -- e.g. ImNodes.GetIO().EmulateThreeButtonMouse.Modifier = function() return ImGui.GetIO().KeyAlt end + function ImNodes.ImNodesIO() + return { + EmulateThreeButtonMouse = { Modifier = nil }, + LinkDetachWithModifierClick = { Modifier = nil }, + MultipleSelectModifier = { Modifier = nil }, + -- Holding alt mouse button pans the node area, by default middle mouse button will be used + -- Set based on ImGuiMouseButton values + AltMouseButton = G.ImGuiMouseButton.Middle, + -- Panning speed when dragging an element and mouse is outside the main editor view. + AutoPanningSpeed = 1000.0, + } + end + + function ImNodes.ImNodesStyle() + local colors = {} -- [Port] 0-based like ImGuiStyle.Colors in the core port + for i = 0, ImNodes.ImNodesCol.COUNT - 1 do + colors[i] = 0 + end + return { + GridSpacing = 24.0, + NodeCornerRounding = 4.0, + NodePadding = ImVec2(8.0, 8.0), + NodeBorderThickness = 1.0, + LinkThickness = 3.0, + LinkLineSegmentsPerLength = 0.1, + LinkHoverDistance = 10.0, + PinCircleRadius = 4.0, + PinQuadSideLength = 7.0, + PinTriangleSideLength = 9.5, + PinLineThickness = 1.0, + PinHoverRadius = 10.0, + PinOffset = 0.0, + MiniMapPadding = ImVec2(8.0, 8.0), + MiniMapOffset = ImVec2(4.0, 4.0), + -- By default, ImNodesStyleFlags_NodeOutline and ImNodesStyleFlags_Gridlines are enabled. + Flags = bit32.bor(ImNodes.ImNodesStyleFlags.NodeOutline, ImNodes.ImNodesStyleFlags.GridLines), + Colors = colors, + } + end + end + + --------------------------------------------------------------------------- + -- imnodes_internal.h: [SECTION] internal data structures + --------------------------------------------------------------------------- + do + -- [Port] ImObjectPool: IdMap is a plain table (id -> index) instead of ImGuiStorage; pool indices are 1-based. + function internal.ImObjectPool() + return { Pool = ImVector(), InUse = ImVector(), FreeList = ImVector(), IdMap = {} } + end + + -- [Port] ImOptionalIndex is represented by a plain int, with INVALID_INDEX as the "no value" sentinel. + internal.INVALID_INDEX = -1 + + function internal.ImNodeData(node_id) + return { + Id = node_id, + Origin = ImVec2(0.0, 0.0), -- The node origin is in editor space + TitleBarContentRect = ImRect(), + Rect = ImRect(ImVec2(0.0, 0.0), ImVec2(0.0, 0.0)), + ColorStyle = { + Background = 0, BackgroundHovered = 0, BackgroundSelected = 0, Outline = 0, + Titlebar = 0, TitlebarHovered = 0, TitlebarSelected = 0, + }, + LayoutStyle = { CornerRounding = 0.0, Padding = ImVec2(0.0, 0.0), BorderThickness = 0.0 }, + PinIndices = ImVector(), + Draggable = true, + } + end + + function internal.ImPinData(pin_id) + return { + Id = pin_id, + ParentNodeIdx = 0, + AttributeRect = ImRect(), + Type = internal.ImNodesAttributeType.None, + Shape = ImNodes.ImNodesPinShape.CircleFilled, + Pos = ImVec2(0.0, 0.0), -- screen-space coordinates + Flags = ImNodes.ImNodesAttributeFlags.None, + ColorStyle = { Background = 0, Hovered = 0 }, + } + end + + function internal.ImLinkData(link_id) + return { + Id = link_id, + StartPinIdx = 0, + EndPinIdx = 0, + ColorStyle = { Base = 0, Hovered = 0, Selected = 0 }, + } + end + + function internal.ImClickInteractionState() + return { + Type = internal.ImNodesClickInteractionType.None, + LinkCreation = { + StartPinIdx = 0, + EndPinIdx = internal.INVALID_INDEX, + Type = internal.ImNodesLinkCreationType.Standard, + }, + BoxSelector = { + Rect = ImRect(), -- Coordinates in grid space + }, + } + end + + function internal.ImNodesColElement(c, s) + return { Color = c, Item = s } + end + + -- [Port] FloatValue is 1-based: [1] = upstream [0], [2] = upstream [1] + function internal.ImNodesStyleVarElement(variable, value) + if type(value) == "number" then + return { Item = variable, FloatValue = { value, 0.0 } } + end + return { Item = variable, FloatValue = { value.x, value.y } } + end + end + + --------------------------------------------------------------------------- + -- imnodes_internal.h: [SECTION] global and editor context structs + --------------------------------------------------------------------------- + do + function internal.ImNodesEditorContext() + return { + Nodes = internal.ImObjectPool(), + Pins = internal.ImObjectPool(), + Links = internal.ImObjectPool(), + + NodeDepthOrder = ImVector(), + + -- ui related fields + Panning = ImVec2(0.0, 0.0), + AutoPanningDelta = ImVec2(0.0, 0.0), + -- Minimum and maximum extents of all content in grid space. Valid after final + -- ImNodes::EndNode() call. + GridContentBounds = ImRect(), + + SelectedNodeIndices = ImVector(), + SelectedLinkIndices = ImVector(), + + -- Relative origins of selected nodes for snapping of dragged nodes + SelectedNodeOffsets = ImVector(), + -- Offset of the primary node origin relative to the mouse cursor. + PrimaryNodeOffset = ImVec2(0.0, 0.0), + + ClickInteraction = internal.ImClickInteractionState(), + + -- Mini-map state set by MiniMap() + MiniMapEnabled = false, + MiniMapLocation = ImNodes.ImNodesMiniMapLocation.BottomLeft, + MiniMapSizeFraction = 0.0, + MiniMapNodeHoveringCallback = nil, + MiniMapNodeHoveringCallbackUserData = nil, + + -- Mini-map state set during EndNodeEditor() call + MiniMapRectScreenSpace = ImRect(), + MiniMapContentScreenSpace = ImRect(), + MiniMapScaling = 0.0, + } + end + + function internal.ImNodesContext() + return { + DefaultEditorCtx = nil, + EditorCtx = nil, + + -- Canvas draw list and helper state + CanvasDrawList = nil, + NodeIdxToSubmissionIdx = {}, -- [Port] ImGuiStorage -> plain table + NodeIdxSubmissionOrder = ImVector(), + NodeIndicesOverlappingWithMouse = ImVector(), + OccludedPinIndices = ImVector(), + + -- Canvas extents + CanvasOriginScreenSpace = ImVec2(0.0, 0.0), + CanvasRectScreenSpace = ImRect(), + + -- Debug helpers + CurrentScope = internal.ImNodesScope.None, + + -- Configuration state + Io = ImNodes.ImNodesIO(), + Style = ImNodes.ImNodesStyle(), + ColorModifierStack = ImVector(), + StyleModifierStack = ImVector(), + TextBuffer = "", -- [Port] ImGuiTextBuffer -> string + + CurrentAttributeFlags = 0, + AttributeFlagStack = ImVector(), + + -- UI element state + CurrentNodeIdx = 0, + CurrentPinIdx = 0, + CurrentAttributeId = 0, + + HoveredNodeIdx = internal.INVALID_INDEX, + HoveredLinkIdx = internal.INVALID_INDEX, + HoveredPinIdx = internal.INVALID_INDEX, + + DeletedLinkIdx = internal.INVALID_INDEX, + SnapLinkIdx = internal.INVALID_INDEX, + + -- Event helper state + -- TODO: this should be a part of a state machine, and not a member of the global struct. + -- Unclear what parts of the code this relates to. + ImNodesUIState = internal.ImNodesUIState.None, + + ActiveAttributeId = 0, + ActiveAttribute = false, + + -- ImGui::IO cache + MousePos = ImVec2(0.0, 0.0), + + LeftMouseClicked = false, + LeftMouseReleased = false, + AltMouseClicked = false, + LeftMouseDragging = false, + AltMouseDragging = false, + AltMouseScrollDelta = 0.0, + MultipleSelectModifier = false, + } + end + + internal.GImNodes = nil + + function internal.EditorContextGet() + -- No editor context was set! Did you forget to call ImNodes::CreateContext()? + IM_ASSERT(internal.GImNodes.EditorCtx ~= nil) + return internal.GImNodes.EditorCtx + end + end + + --------------------------------------------------------------------------- + -- [SECTION] API implementation: context + --------------------------------------------------------------------------- + do + function internal.Initialize(context) + context.CanvasOriginScreenSpace = ImVec2(0.0, 0.0) + context.CanvasRectScreenSpace = ImRect(ImVec2(0.0, 0.0), ImVec2(0.0, 0.0)) + context.CurrentScope = internal.ImNodesScope.None + + context.CurrentPinIdx = INT_MAX + context.CurrentNodeIdx = INT_MAX + + context.DefaultEditorCtx = ImNodes.EditorContextCreate() + context.EditorCtx = context.DefaultEditorCtx + + context.CurrentAttributeFlags = ImNodes.ImNodesAttributeFlags.None + context.AttributeFlagStack:push_back(internal.GImNodes.CurrentAttributeFlags) + + ImNodes.StyleColorsDark(context.Style) + end + + function internal.Shutdown(ctx) + ImNodes.EditorContextFree(ctx.DefaultEditorCtx) + end + + function ImNodes.CreateContext() + local ctx = internal.ImNodesContext() + if internal.GImNodes == nil then + ImNodes.SetCurrentContext(ctx) + end + internal.Initialize(ctx) + return ctx + end + + function ImNodes.DestroyContext(ctx) -- nil = destroy current context + if ctx == nil then + ctx = internal.GImNodes + end + internal.Shutdown(ctx) + if internal.GImNodes == ctx then + ImNodes.SetCurrentContext(nil) + end + end + + function ImNodes.GetCurrentContext() + return internal.GImNodes + end + + function ImNodes.SetCurrentContext(ctx) + internal.GImNodes = ctx + end + + function ImNodes.EditorContextCreate() + return internal.ImNodesEditorContext() + end + + function ImNodes.EditorContextFree(ctx) + -- [Port] no-op: memory is garbage collected + end + + function ImNodes.EditorContextSet(ctx) + internal.GImNodes.EditorCtx = ctx + end + + function ImNodes.EditorContextGetPanning() + local editor = internal.EditorContextGet() + return ImVec2(editor.Panning.x, editor.Panning.y) -- [Port] returned by value + end + + function ImNodes.EditorContextResetPanning(pos) + local editor = internal.EditorContextGet() + editor.Panning = ImVec2(pos.x, pos.y) + end + + function ImNodes.GetIO() + return internal.GImNodes.Io + end + + -- Returns the global style struct. See the struct declaration for default values. + function ImNodes.GetStyle() + return internal.GImNodes.Style + end + end + + --------------------------------------------------------------------------- + -- Style presets + --------------------------------------------------------------------------- + do + local Col = ImNodes.ImNodesCol + + function ImNodes.StyleColorsDark(dest) + if dest == nil then + dest = internal.GImNodes.Style + end + local c = dest.Colors + + c[Col.NodeBackground] = IM_COL32(50, 50, 50, 255) + c[Col.NodeBackgroundHovered] = IM_COL32(75, 75, 75, 255) + c[Col.NodeBackgroundSelected] = IM_COL32(75, 75, 75, 255) + c[Col.NodeOutline] = IM_COL32(100, 100, 100, 255) + -- title bar colors match ImGui's titlebg colors + c[Col.TitleBar] = IM_COL32(41, 74, 122, 255) + c[Col.TitleBarHovered] = IM_COL32(66, 150, 250, 255) + c[Col.TitleBarSelected] = IM_COL32(66, 150, 250, 255) + -- link colors match ImGui's slider grab colors + c[Col.Link] = IM_COL32(61, 133, 224, 200) + c[Col.LinkHovered] = IM_COL32(66, 150, 250, 255) + c[Col.LinkSelected] = IM_COL32(66, 150, 250, 255) + -- pin colors match ImGui's button colors + c[Col.Pin] = IM_COL32(53, 150, 250, 180) + c[Col.PinHovered] = IM_COL32(53, 150, 250, 255) + + c[Col.BoxSelector] = IM_COL32(61, 133, 224, 30) + c[Col.BoxSelectorOutline] = IM_COL32(61, 133, 224, 150) + + c[Col.GridBackground] = IM_COL32(40, 40, 50, 200) + c[Col.GridLine] = IM_COL32(200, 200, 200, 40) + c[Col.GridLinePrimary] = IM_COL32(240, 240, 240, 60) + + -- minimap colors + c[Col.MiniMapBackground] = IM_COL32(25, 25, 25, 150) + c[Col.MiniMapBackgroundHovered] = IM_COL32(25, 25, 25, 200) + c[Col.MiniMapOutline] = IM_COL32(150, 150, 150, 100) + c[Col.MiniMapOutlineHovered] = IM_COL32(150, 150, 150, 200) + c[Col.MiniMapNodeBackground] = IM_COL32(200, 200, 200, 100) + c[Col.MiniMapNodeBackgroundHovered] = IM_COL32(200, 200, 200, 255) + c[Col.MiniMapNodeBackgroundSelected] = c[Col.MiniMapNodeBackgroundHovered] + c[Col.MiniMapNodeOutline] = IM_COL32(200, 200, 200, 100) + c[Col.MiniMapLink] = c[Col.Link] + c[Col.MiniMapLinkSelected] = c[Col.LinkSelected] + c[Col.MiniMapCanvas] = IM_COL32(200, 200, 200, 25) + c[Col.MiniMapCanvasOutline] = IM_COL32(200, 200, 200, 200) + end + + function ImNodes.StyleColorsClassic(dest) + if dest == nil then + dest = internal.GImNodes.Style + end + local c = dest.Colors + + c[Col.NodeBackground] = IM_COL32(50, 50, 50, 255) + c[Col.NodeBackgroundHovered] = IM_COL32(75, 75, 75, 255) + c[Col.NodeBackgroundSelected] = IM_COL32(75, 75, 75, 255) + c[Col.NodeOutline] = IM_COL32(100, 100, 100, 255) + c[Col.TitleBar] = IM_COL32(69, 69, 138, 255) + c[Col.TitleBarHovered] = IM_COL32(82, 82, 161, 255) + c[Col.TitleBarSelected] = IM_COL32(82, 82, 161, 255) + c[Col.Link] = IM_COL32(255, 255, 255, 100) + c[Col.LinkHovered] = IM_COL32(105, 99, 204, 153) + c[Col.LinkSelected] = IM_COL32(105, 99, 204, 153) + c[Col.Pin] = IM_COL32(89, 102, 156, 170) + c[Col.PinHovered] = IM_COL32(102, 122, 179, 200) + c[Col.BoxSelector] = IM_COL32(82, 82, 161, 100) + c[Col.BoxSelectorOutline] = IM_COL32(82, 82, 161, 255) + c[Col.GridBackground] = IM_COL32(40, 40, 50, 200) + c[Col.GridLine] = IM_COL32(200, 200, 200, 40) + c[Col.GridLinePrimary] = IM_COL32(240, 240, 240, 60) + + -- minimap colors + c[Col.MiniMapBackground] = IM_COL32(25, 25, 25, 100) + c[Col.MiniMapBackgroundHovered] = IM_COL32(25, 25, 25, 200) + c[Col.MiniMapOutline] = IM_COL32(150, 150, 150, 100) + c[Col.MiniMapOutlineHovered] = IM_COL32(150, 150, 150, 200) + c[Col.MiniMapNodeBackground] = IM_COL32(200, 200, 200, 100) + c[Col.MiniMapNodeBackgroundSelected] = c[Col.MiniMapNodeBackgroundHovered] + c[Col.MiniMapNodeBackgroundSelected] = IM_COL32(200, 200, 240, 255) + c[Col.MiniMapNodeOutline] = IM_COL32(200, 200, 200, 100) + c[Col.MiniMapLink] = c[Col.Link] + c[Col.MiniMapLinkSelected] = c[Col.LinkSelected] + c[Col.MiniMapCanvas] = IM_COL32(200, 200, 200, 25) + c[Col.MiniMapCanvasOutline] = IM_COL32(200, 200, 200, 200) + end + + function ImNodes.StyleColorsLight(dest) + if dest == nil then + dest = internal.GImNodes.Style + end + local c = dest.Colors + + c[Col.NodeBackground] = IM_COL32(240, 240, 240, 255) + c[Col.NodeBackgroundHovered] = IM_COL32(240, 240, 240, 255) + c[Col.NodeBackgroundSelected] = IM_COL32(240, 240, 240, 255) + c[Col.NodeOutline] = IM_COL32(100, 100, 100, 255) + c[Col.TitleBar] = IM_COL32(248, 248, 248, 255) + c[Col.TitleBarHovered] = IM_COL32(209, 209, 209, 255) + c[Col.TitleBarSelected] = IM_COL32(209, 209, 209, 255) + -- original imgui values: 66, 150, 250 + c[Col.Link] = IM_COL32(66, 150, 250, 100) + -- original imgui values: 117, 138, 204 + c[Col.LinkHovered] = IM_COL32(66, 150, 250, 242) + c[Col.LinkSelected] = IM_COL32(66, 150, 250, 242) + -- original imgui values: 66, 150, 250 + c[Col.Pin] = IM_COL32(66, 150, 250, 160) + c[Col.PinHovered] = IM_COL32(66, 150, 250, 255) + c[Col.BoxSelector] = IM_COL32(90, 170, 250, 30) + c[Col.BoxSelectorOutline] = IM_COL32(90, 170, 250, 150) + c[Col.GridBackground] = IM_COL32(225, 225, 225, 255) + c[Col.GridLine] = IM_COL32(180, 180, 180, 100) + c[Col.GridLinePrimary] = IM_COL32(120, 120, 120, 100) + + -- minimap colors + c[Col.MiniMapBackground] = IM_COL32(25, 25, 25, 100) + c[Col.MiniMapBackgroundHovered] = IM_COL32(25, 25, 25, 200) + c[Col.MiniMapOutline] = IM_COL32(150, 150, 150, 100) + c[Col.MiniMapOutlineHovered] = IM_COL32(150, 150, 150, 200) + c[Col.MiniMapNodeBackground] = IM_COL32(200, 200, 200, 100) + c[Col.MiniMapNodeBackgroundSelected] = c[Col.MiniMapNodeBackgroundHovered] + c[Col.MiniMapNodeBackgroundSelected] = IM_COL32(200, 200, 240, 255) + c[Col.MiniMapNodeOutline] = IM_COL32(200, 200, 200, 100) + c[Col.MiniMapLink] = c[Col.Link] + c[Col.MiniMapLinkSelected] = c[Col.LinkSelected] + c[Col.MiniMapCanvas] = IM_COL32(200, 200, 200, 25) + c[Col.MiniMapCanvasOutline] = IM_COL32(200, 200, 200, 200) + end + end + + --------------------------------------------------------------------------- + -- Push/Pop style + --------------------------------------------------------------------------- + do + function ImNodes.PushColorStyle(item, color) + local g = internal.GImNodes + g.ColorModifierStack:push_back(internal.ImNodesColElement(g.Style.Colors[item], item)) + g.Style.Colors[item] = color + end + + function ImNodes.PopColorStyle() + local g = internal.GImNodes + IM_ASSERT(g.ColorModifierStack.Size > 0) + local elem = g.ColorModifierStack:back() + g.Style.Colors[elem.Item] = elem.Color + g.ColorModifierStack:pop_back() + end + + -- [Port] ImNodesStyleVarInfo: Offset -> field Name; Type is always ImGuiDataType_Float. Indexed by ImNodesStyleVar (0-based). + local GStyleVarInfo = { + [0] = { Count = 1, Name = "GridSpacing" }, + { Count = 1, Name = "NodeCornerRounding" }, + { Count = 2, Name = "NodePadding" }, + { Count = 1, Name = "NodeBorderThickness" }, + { Count = 1, Name = "LinkThickness" }, + { Count = 1, Name = "LinkLineSegmentsPerLength" }, + { Count = 1, Name = "LinkHoverDistance" }, + { Count = 1, Name = "PinCircleRadius" }, + { Count = 1, Name = "PinQuadSideLength" }, + { Count = 1, Name = "PinTriangleSideLength" }, + { Count = 1, Name = "PinLineThickness" }, + { Count = 1, Name = "PinHoverRadius" }, + { Count = 1, Name = "PinOffset" }, + { Count = 2, Name = "MiniMapPadding" }, + { Count = 2, Name = "MiniMapOffset" }, + } + + local function GetStyleVarInfo(idx) + IM_ASSERT(idx >= 0 and idx < ImNodes.ImNodesStyleVar.COUNT) + return GStyleVarInfo[idx] + end + + -- [Port] float and ImVec2 overloads merged; dispatch on type(value). + function ImNodes.PushStyleVar(item, value) + local g = internal.GImNodes + local var_info = GetStyleVarInfo(item) + if type(value) == "number" then + IM_ASSERT(var_info.Count == 1, "Called PushStyleVar() float variant but variable is not a float!") + g.StyleModifierStack:push_back(internal.ImNodesStyleVarElement(item, g.Style[var_info.Name])) + g.Style[var_info.Name] = value + else + IM_ASSERT(var_info.Count == 2, "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!") + g.StyleModifierStack:push_back(internal.ImNodesStyleVarElement(item, g.Style[var_info.Name])) + g.Style[var_info.Name] = ImVec2(value.x, value.y) + end + end + + function ImNodes.PopStyleVar(count) + if count == nil then + count = 1 + end + local g = internal.GImNodes + while count > 0 do + IM_ASSERT(g.StyleModifierStack.Size > 0) + local style_backup = g.StyleModifierStack:back() + g.StyleModifierStack:pop_back() + local var_info = GetStyleVarInfo(style_backup.Item) + if var_info.Count == 1 then + g.Style[var_info.Name] = style_backup.FloatValue[1] + elseif var_info.Count == 2 then + g.Style[var_info.Name] = ImVec2(style_backup.FloatValue[1], style_backup.FloatValue[2]) + end + count = count - 1 + end + end + end + return ImNodes end diff --git a/tests/t_style.luau b/tests/t_style.luau new file mode 100644 index 0000000..d3785ef --- /dev/null +++ b/tests/t_style.luau @@ -0,0 +1,36 @@ +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImNodes = require(RS:WaitForChild("ImNodes"))(G) +local Col, Var = ImNodes.ImNodesCol, ImNodes.ImNodesStyleVar + +local ctx = ImNodes.CreateContext() +assert(ImNodes.GetCurrentContext() == ctx) +local style = ImNodes.GetStyle() +assert(style.GridSpacing == 24.0 and style.NodePadding.x == 8.0) +assert(style.Colors[Col.NodeBackground] == G.IM_COL32(50, 50, 50, 255)) +assert(style.Colors[Col.MiniMapLink] == style.Colors[Col.Link]) + +ImNodes.PushColorStyle(Col.TitleBar, G.IM_COL32(1, 2, 3, 4)) +assert(style.Colors[Col.TitleBar] == G.IM_COL32(1, 2, 3, 4)) +ImNodes.PopColorStyle() +assert(style.Colors[Col.TitleBar] == G.IM_COL32(41, 74, 122, 255)) + +ImNodes.PushStyleVar(Var.GridSpacing, 10.0) +ImNodes.PushStyleVar(Var.NodePadding, G.ImVec2(1, 2)) +assert(style.GridSpacing == 10.0 and style.NodePadding.y == 2) +ImNodes.PopStyleVar(2) +assert(style.GridSpacing == 24.0 and style.NodePadding.x == 8.0 and style.NodePadding.y == 8.0) + +ImNodes.StyleColorsLight() +assert(style.Colors[Col.NodeBackground] == G.IM_COL32(240, 240, 240, 255)) + +ImNodes.EditorContextResetPanning(G.ImVec2(5, 6)) +assert(ImNodes.EditorContextGetPanning().y == 6) +local ed = ImNodes.EditorContextCreate() +ImNodes.EditorContextSet(ed) +assert(ImNodes.EditorContextGetPanning().x == 0) +ImNodes.EditorContextFree(ed) + +ImNodes.DestroyContext() +assert(ImNodes.GetCurrentContext() == nil) +print("OK")