From ad9af52825646e95dee05e62b166c7968b05e34e Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 17 Oct 2026 04:45:03 -0500 Subject: [PATCH] Save/load ini string API, ported examples, README --- ImNodes.lua | 63 ++++++++++- README.md | 52 ++++++++- examples/roblox/main.client.luau | 189 ++++++++++++++++++++++++++++++- tests/t_saveload.luau | 53 +++++++++ tools/test.sh | 4 + 5 files changed, 356 insertions(+), 5 deletions(-) create mode 100644 tests/t_saveload.luau diff --git a/ImNodes.lua b/ImNodes.lua index fbf5a3d..bf38bab 100644 --- a/ImNodes.lua +++ b/ImNodes.lua @@ -2594,7 +2594,68 @@ return function(G) return ImVec2(origin.x, origin.y) -- [Port] returned by value end - -- [Port] TODO: SaveCurrentEditorStateToIniString/Load* + -- [Port] *ToIniString returns the string (data_size dropped: use #s). *IniFile variants not available on Roblox. + local function trunc(v) + return if v < 0 then math.ceil(v) else math.floor(v) + end + + function ImNodes.SaveEditorStateToIniString(editor) + IM_ASSERT(editor ~= nil) + local out = { string.format("[editor]\npanning=%d,%d\n", trunc(editor.Panning.x), trunc(editor.Panning.y)) } + for i = 1, editor.Nodes.Pool.Size do + if editor.Nodes.InUse[i] then + local node = editor.Nodes.Pool[i] + table.insert(out, string.format("\n[node.%d]\norigin=%d,%d\n", node.Id, trunc(node.Origin.x), trunc(node.Origin.y))) + end + end + return table.concat(out) + end + + function ImNodes.SaveCurrentEditorStateToIniString() + return ImNodes.SaveEditorStateToIniString(internal.EditorContextGet()) + end + + function ImNodes.LoadEditorStateFromIniString(editor, data) + if data == nil or #data == 0 then + return + end + editor = editor or internal.EditorContextGet() + local handler = nil + for line in string.gmatch(data, "[^\r\n]+") do + if string.sub(line, 1, 1) ~= ";" then + if string.match(line, "^%[.*%]$") then + if string.sub(line, 2, 5) == "node" then + handler = "node" + elseif line == "[editor]" then + handler = "editor" + end + end + if handler == "node" then + local id = tonumber(string.match(line, "^%[node%.(%-?%d+)")) + if id then + local node_idx = internal.ObjectPoolFindOrCreateIndex(editor.Nodes, id) + internal.GImNodes.CurrentNodeIdx = node_idx + editor.Nodes.Pool[node_idx].Id = id + else + local x, y = string.match(line, "^origin=(%-?%d+),(%-?%d+)") + if x then + editor.Nodes.Pool[internal.GImNodes.CurrentNodeIdx].Origin = + internal.SnapOriginToGrid(ImVec2(tonumber(x), tonumber(y))) + end + end + elseif handler == "editor" then + local x, y = string.match(line, "^panning=([^,]+),(.+)$") + if x and tonumber(x) and tonumber(y) then + editor.Panning = ImVec2(tonumber(x), tonumber(y)) + end + end + end + end + end + + function ImNodes.LoadCurrentEditorStateFromIniString(data) + ImNodes.LoadEditorStateFromIniString(internal.EditorContextGet(), data) + end function ImNodes.MiniMap(minimap_size_fraction, location, node_hovering_callback, node_hovering_callback_data) minimap_size_fraction = minimap_size_fraction or 0.2 diff --git a/README.md b/README.md index ca85600..15e278c 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,63 @@ Node editor (imnodes) for imgui-luau (Roblox / LÖVE). Luau port of [ImNodes](https://github.com/Nelarius/imnodes) for [imgui-luau](https://git.bug.tools/bug/imgui-luau) ([GitHub](https://github.com/4DBug/imgui-luau)). Mirrors: [git.bug.tools](https://git.bug.tools/bug/imnodes-luau) / [GitHub](https://github.com/4DBug/imnodes-luau). -**Status: work in progress.** +**Status:** near-complete port of upstream imnodes (editor, nodes, pins, links, selection, minimap, style stacks, save/load to string). +Not ported: `*IniFile` save/load (no filesystem on Roblox) and the `color_node_editor` example. + +## Usage ```lua local G = require(ReplicatedStorage.ImGui) local ImNodes = require(ReplicatedStorage.ImNodes)(G) +local ImGui = G.ImGui + +ImGui.CreateContext() +ImNodes.CreateContext() +G.ImGui_ImplRoblox.Init() + +local Links = {} +RunService.RenderStepped:Connect(function() + G.ImGui_ImplRoblox.NewFrame() + ImGui.NewFrame() + ImGui.Begin("node editor") + ImNodes.BeginNodeEditor() + ImNodes.BeginNode(1) + ImNodes.BeginNodeTitleBar() + ImGui.Text("node") + ImNodes.EndNodeTitleBar() + ImNodes.BeginOutputAttribute(2) + ImGui.Text("out") + ImNodes.EndOutputAttribute() + ImNodes.EndNode() + for Id, L in Links do + ImNodes.Link(Id, L[1], L[2]) + end + ImNodes.EndNodeEditor() + local Created, StartAttr, EndAttr = ImNodes.IsLinkCreated() + if Created then + table.insert(Links, { StartAttr, EndAttr }) + end + ImGui.End() + ImGui.Render() + G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) +end) ``` +Full example (ports of upstream `hello`, `multi_editor`, `save_load`): `examples/roblox/main.client.luau`. + +## API differences + +- Pointer out-params become return values: `IsLinkCreated() -> created, start_attr, end_attr, from_snap`, + `IsLinkDestroyed() -> destroyed, link_id`, `IsNodeHovered() -> hovered, node_id`, `IsLinkDropped(...) -> dropped, attr_id`, etc. + Getters like `GetNodeGridSpacePos` / `EditorContextGetPanning` return copies. +- The C++ `IsLinkCreated(int*, int*, int*, int*, bool*)` overload is `IsLinkCreatedWithNodes() + -> created, start_node, start_attr, end_node, end_attr, from_snap`. +- `ImNodesIO.*.Modifier` is `nil` or a function returning bool instead of a `const bool*`: + `ImNodes.GetIO().LinkDetachWithModifierClick.Modifier = function() return ImGui.GetIO().KeyCtrl end`. +- `SaveCurrentEditorStateToIniString()` / `SaveEditorStateToIniString(editor)` return a string (no `data_size`); + `LoadCurrentEditorStateFromIniString(s)` / `LoadEditorStateFromIniString(editor, s)` take one. Persist it via DataStore etc. +- `EditorContextFree` is a no-op (GC). + ## Development ```sh diff --git a/examples/roblox/main.client.luau b/examples/roblox/main.client.luau index 4df541f..a706c63 100644 --- a/examples/roblox/main.client.luau +++ b/examples/roblox/main.client.luau @@ -1,15 +1,198 @@ +-- Ports of the upstream examples: hello.cpp, multi_editor.cpp, save_load.cpp (color_node_editor not ported). local RS = game:GetService("ReplicatedStorage") local RunService = game:GetService("RunService") local G = require(RS:WaitForChild("ImGui")) local ImNodes = require(RS:WaitForChild("ImNodes"))(G) local ImGui = G.ImGui -ImGui.CreateContext() -G.ImGui_ImplRoblox.Init() +local Show = { Hello = true, MultiEditor = true, SaveLoad = true } + +-- hello.cpp +local function ShowHello() + ImGui.Begin("simple node editor") + ImNodes.BeginNodeEditor() + ImNodes.BeginNode(1) + ImNodes.BeginNodeTitleBar() + ImGui.TextUnformatted("simple node :)") + ImNodes.EndNodeTitleBar() + ImNodes.BeginInputAttribute(2) + ImGui.Text("input") + ImNodes.EndInputAttribute() + ImNodes.BeginOutputAttribute(3) + ImGui.Indent(40) + ImGui.Text("output") + ImNodes.EndOutputAttribute() + ImNodes.EndNode() + ImNodes.EndNodeEditor() + ImGui.End() +end + +-- shared by multi_editor.cpp and save_load.cpp +local function NewEditor(Context) + return { Context = Context, Nodes = {}, Links = {}, CurrentId = 0 } +end + +local function ShowEditor(Name, Editor, Snap) + if Editor.Context then + ImNodes.EditorContextSet(Editor.Context) + end + ImGui.Begin(Name) + ImGui.TextUnformatted("A -- add node") + if Editor.Hint then + ImGui.TextUnformatted(Editor.Hint) + end + ImNodes.BeginNodeEditor() + + if ImGui.IsWindowFocused(G.ImGuiFocusedFlags.RootAndChildWindows) and ImNodes.IsEditorHovered() and ImGui.IsKeyReleased(G.ImGuiKey.A) then + Editor.CurrentId += 1 + local NodeId = Editor.CurrentId + ImNodes.SetNodeScreenSpacePos(NodeId, ImGui.GetMousePos()) + if Snap then + ImNodes.SnapNodeToGrid(NodeId) + end + table.insert(Editor.Nodes, { Id = NodeId, Value = 0 }) + end + + for _, Node in Editor.Nodes do + ImNodes.BeginNode(Node.Id) + ImNodes.BeginNodeTitleBar() + ImGui.TextUnformatted("node") + ImNodes.EndNodeTitleBar() + + ImNodes.BeginInputAttribute(bit32.lshift(Node.Id, 8)) + ImGui.TextUnformatted("input") + ImNodes.EndInputAttribute() + + ImNodes.BeginStaticAttribute(bit32.lshift(Node.Id, 16)) + ImGui.PushItemWidth(120) + Node.Value = ImGui.DragFloat("value", Node.Value, 0.01) + ImGui.PopItemWidth() + ImNodes.EndStaticAttribute() + + ImNodes.BeginOutputAttribute(bit32.lshift(Node.Id, 24)) + local TextWidth = ImGui.CalcTextSize("output").x + ImGui.Indent(120 + ImGui.CalcTextSize("value").x - TextWidth) + ImGui.TextUnformatted("output") + ImNodes.EndOutputAttribute() + ImNodes.EndNode() + end + + for _, Link in Editor.Links do + ImNodes.Link(Link.Id, Link.StartAttr, Link.EndAttr) + end + + ImNodes.EndNodeEditor() + + local Created, StartAttr, EndAttr = ImNodes.IsLinkCreated() + if Created then + Editor.CurrentId += 1 + table.insert(Editor.Links, { Id = Editor.CurrentId, StartAttr = StartAttr, EndAttr = EndAttr }) + end + + local Destroyed, LinkId = ImNodes.IsLinkDestroyed() + if Destroyed then + for I, Link in Editor.Links do + if Link.Id == LinkId then + table.remove(Editor.Links, I) + break + end + end + end + + ImGui.End() +end + +-- [Port] save_load.cpp writes files; here state lives in memory (swap for DataStore / attributes as needed). +local SaveLoad = NewEditor(nil) +SaveLoad.Hint = "Save, move nodes around, then Load -- nodes return to where they were." +local Saved = nil + +local function CopyEditor(Src) + local Nodes, Links = {}, {} + for I, N in Src.Nodes do + Nodes[I] = table.clone(N) + end + for I, L in Src.Links do + Links[I] = table.clone(L) + end + return { Nodes = Nodes, Links = Links, CurrentId = Src.CurrentId } +end + +local function ShowSaveLoad() + ImNodes.EditorContextSet(SaveLoad.Context) + ImGui.Begin("Save & load controls") + if ImGui.Button("Save") then + Saved = CopyEditor(SaveLoad) + Saved.Ini = ImNodes.SaveCurrentEditorStateToIniString() + end + ImGui.SameLine() + if ImGui.Button("Load") and Saved then + local C = CopyEditor(Saved) + SaveLoad.Nodes, SaveLoad.Links, SaveLoad.CurrentId = C.Nodes, C.Links, C.CurrentId + ImNodes.LoadCurrentEditorStateFromIniString(Saved.Ini) + end + if Saved then + ImGui.TextUnformatted(Saved.Ini) + end + ImGui.End() + ShowEditor("Save & load example", SaveLoad, false) +end + +local HelloContext, Editor1, Editor2 + +local function Init() + ImGui.CreateContext() + ImNodes.CreateContext() + G.ImGui_ImplRoblox.Init() + + HelloContext = ImNodes.EditorContextCreate() + ImNodes.EditorContextSet(HelloContext) + ImNodes.SetNodeGridSpacePos(1, G.ImVec2(200, 200)) + + Editor1 = NewEditor(ImNodes.EditorContextCreate()) + Editor2 = NewEditor(ImNodes.EditorContextCreate()) + SaveLoad.Context = ImNodes.EditorContextCreate() + + ImNodes.PushAttributeFlag(ImNodes.ImNodesAttributeFlags.EnableLinkDetachWithDragClick) + local IO = ImNodes.GetIO() + local KeyCtrl = function() + return ImGui.GetIO().KeyCtrl + end + IO.LinkDetachWithModifierClick.Modifier = KeyCtrl + IO.MultipleSelectModifier.Modifier = KeyCtrl + + local Style = ImNodes.GetStyle() + Style.Flags = bit32.bor(Style.Flags, ImNodes.ImNodesStyleFlags.GridLinesPrimary, ImNodes.ImNodesStyleFlags.GridSnapping) +end + +local function ShowMenu() + ImGui.Begin("ImNodes examples") + _, Show.Hello = ImGui.Checkbox("Hello", Show.Hello) + _, Show.MultiEditor = ImGui.Checkbox("Multi editor", Show.MultiEditor) + _, Show.SaveLoad = ImGui.Checkbox("Save & load", Show.SaveLoad) + ImGui.End() +end + +local function Render() + ShowMenu() + if Show.Hello then + ImNodes.EditorContextSet(HelloContext) + ShowHello() + end + if Show.MultiEditor then + ShowEditor("editor1", Editor1, true) + ShowEditor("editor2", Editor2, true) + end + if Show.SaveLoad then + ShowSaveLoad() + end +end + +Init() RunService.RenderStepped:Connect(function() G.ImGui_ImplRoblox.NewFrame() ImGui.NewFrame() - -- ImNodes example goes here + Render() ImGui.Render() G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) end) diff --git a/tests/t_saveload.luau b/tests/t_saveload.luau new file mode 100644 index 0000000..99c39b9 --- /dev/null +++ b/tests/t_saveload.luau @@ -0,0 +1,53 @@ +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() +ImNodes.CreateContext() + +local function frame() + Impl.NewFrame() + ImGui.GetIO().DeltaTime = 1 / 60 + ImGui.NewFrame() + ImGui.Begin("editor") + ImNodes.BeginNodeEditor() + for id = 1, 2 do + ImNodes.BeginNode(id) + ImGui.Text("node") + ImNodes.EndNode() + end + ImNodes.EndNodeEditor() + ImGui.End() + ImGui.Render() + Impl.RenderDrawData(ImGui.GetDrawData()) +end + +frame() +ImNodes.SetNodeGridSpacePos(1, ImVec2(123, 45)) +ImNodes.SetNodeGridSpacePos(2, ImVec2(-67, 89)) +ImNodes.EditorContextResetPanning(ImVec2(10, -20)) +frame() +local ini = ImNodes.SaveCurrentEditorStateToIniString() +assert(string.find(ini, "[node.1]", 1, true) and string.find(ini, "origin=-67,89", 1, true), ini) + +ImNodes.SetNodeGridSpacePos(1, ImVec2(0, 0)) +ImNodes.SetNodeGridSpacePos(2, ImVec2(0, 0)) +ImNodes.EditorContextResetPanning(ImVec2(0, 0)) +ImNodes.LoadCurrentEditorStateFromIniString(ini) +frame() + +local p1, p2, pan = ImNodes.GetNodeGridSpacePos(1), ImNodes.GetNodeGridSpacePos(2), ImNodes.EditorContextGetPanning() +assert(p1.x == 123 and p1.y == 45, `node1 {p1.x},{p1.y}`) +assert(p2.x == -67 and p2.y == 89, `node2 {p2.x},{p2.y}`) +assert(pan.x == 10 and pan.y == -20, `pan {pan.x},{pan.y}`) + +-- explicit editor context, round trip +local ed = ImNodes.EditorContextCreate() +ImNodes.LoadEditorStateFromIniString(ed, ini) +assert(ImNodes.SaveEditorStateToIniString(ed) == ini) + +ImNodes.DestroyContext() +print("OK") diff --git a/tools/test.sh b/tools/test.sh index 68167df..d398be6 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -6,4 +6,8 @@ for t in tests/t_*.luau; do out=$(python3 tools/core/tools/test/run.py --module ImNodes=ImNodes.lua --main "$t" --quiet --roblox 2>&1) if echo "$out" | grep -q "^OK"; then echo "PASS $t"; else echo "FAIL $t"; echo "$out" | tail -20; fail=1; fi done +# example LocalScript: 400 frames of random input (mock drives Stepped, not RenderStepped) +ex=$(mktemp --suffix=.luau); sed 's/RenderStepped/Stepped/' examples/roblox/main.client.luau > "$ex" +out=$(python3 tools/core/tools/test/run.py --module ImNodes=ImNodes.lua --main "$ex" --roblox 2>&1); rm -f "$ex" +if echo "$out" | grep -q "^avg frame"; then echo "PASS examples/roblox/main.client.luau"; else echo "FAIL examples/roblox/main.client.luau"; echo "$out" | tail -20; fail=1; fi exit $fail