From d471cb855c0b1fba4e48ba7888fb9ac135e4f17e Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 17 Oct 2026 06:19:09 -0500 Subject: [PATCH] Logic Simulator example UI (LogicUI), Delete-key test --- README.md | 7 +- examples/roblox/LogicUI.lua | 205 +++++++++++++++++++++++++++++++ examples/roblox/main.client.luau | 174 +------------------------- tests/t_logicui.luau | 35 ++++++ tools/test.sh | 4 +- 5 files changed, 253 insertions(+), 172 deletions(-) create mode 100644 examples/roblox/LogicUI.lua create mode 100644 tests/t_logicui.luau diff --git a/README.md b/README.md index 15e278c..d1a011d 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,12 @@ RunService.RenderStepped:Connect(function() end) ``` -Full example (ports of upstream `hello`, `multi_editor`, `save_load`): `examples/roblox/main.client.luau`. +Full example: `examples/roblox/main.client.luau` — a "Logic Simulator" window (`LogicUI.lua`) over the +`LogicSim.lua` model. Put `ImGui`, `ImNodes`, `LogicSim`, `LogicUI` in ReplicatedStorage. +- File menu: New, Save (graph + editor layout to an in-memory string), Load, Clear (drop saved text), Show Save Text (copyable). +- Examples menu: Half Adder, Full Adder (default), 4-bit Ripple Adder, SR Latch, Blinker. +- Right-click canvas: Add Node (Inputs/Gates/Outputs/Memory); right-click node: Duplicate/Delete; right-click link: Delete. +- Delete/Backspace removes selected nodes and links. Lit pins/links are green. ## API differences diff --git a/examples/roblox/LogicUI.lua b/examples/roblox/LogicUI.lua new file mode 100644 index 0000000..700bd86 --- /dev/null +++ b/examples/roblox/LogicUI.lua @@ -0,0 +1,205 @@ +-- "Logic Simulator" window over LogicSim. Usage: local UI = require(RS.LogicUI)(G, ImNodes, LogicSim); UI.Draw() each frame. +return function(G, ImNodes, LogicSim) + local ImGui, ImVec2, IM_COL32 = G.ImGui, G.ImVec2, G.IM_COL32 + local Col = ImNodes.ImNodesCol + local LIT, DIM = IM_COL32(80, 220, 100, 255), IM_COL32(90, 90, 110, 255) + local Categories = { + { "Inputs", { "Switch", "Clock", "Const0", "Const1" } }, + { "Gates", { "AND", "OR", "NAND", "NOR", "XOR", "XNOR", "NOT" } }, + { "Outputs", { "LED", "HexDisplay" } }, + { "Memory", { "DFlipFlop" } }, + } + local ExampleList = { + { "Half Adder", "HalfAdder" }, { "Full Adder", "FullAdder" }, { "4-bit Ripple Adder", "RippleAdder4" }, + { "SR Latch", "SRLatch" }, { "Blinker", "Blinker" }, + } + + local UI = { Graph = nil, Placed = {}, Saved = nil, SaveBuf = nil, ShowSave = false, AddAt = nil, Target = nil } + + function UI.SetGraph(g) + UI.Graph, UI.Placed = g, {} + if ImNodes.GetCurrentContext() then + ImNodes.ClearNodeSelection() + ImNodes.ClearLinkSelection() + end + end + + function UI.LoadExample(name) + local g = LogicSim.Examples[name]() + for _, n in g.nodes do + n.Pos = { 40 + n.Pos[1] * 1.2, 40 + n.Pos[2] * 1.4 } + end + UI.SetGraph(g) + end + + local function DeleteSelected() + local g = UI.Graph + for _, id in ImNodes.GetSelectedLinks() do LogicSim.RemoveLink(g, id) end + for _, id in ImNodes.GetSelectedNodes() do + if g.nodes[id] then LogicSim.RemoveNode(g, id) end + end + ImNodes.ClearNodeSelection() + ImNodes.ClearLinkSelection() + end + + local function Pin(pin, isOut, label) + ImNodes.PushColorStyle(Col.Pin, LogicSim.Value(UI.Graph, pin) == 1 and LIT or DIM) + if isOut then + ImNodes.BeginOutputAttribute(pin) + ImGui.Indent(60) + ImGui.TextUnformatted(label) + ImNodes.EndOutputAttribute() + else + ImNodes.BeginInputAttribute(pin) + ImGui.TextUnformatted(label) + ImNodes.EndInputAttribute() + end + ImNodes.PopColorStyle() + end + + local function DrawNode(n) + local k = LogicSim.Kinds[n.Kind] + ImNodes.BeginNode(n.Id) + if not UI.Placed[n.Id] then + UI.Placed[n.Id] = true + if n.ScreenPos then + ImNodes.SetNodeScreenSpacePos(n.Id, n.ScreenPos) + n.ScreenPos = nil + else + ImNodes.SetNodeGridSpacePos(n.Id, ImVec2(n.Pos[1], n.Pos[2])) + end + end + ImNodes.BeginNodeTitleBar() + ImGui.TextUnformatted(n.Kind) + ImNodes.EndNodeTitleBar() + ImGui.PushID(n.Id) + local s = n.state + if n.Kind == "Switch" then + if ImGui.Button(s.on and "1" or "0", ImVec2(40, 0)) then s.on = not s.on end + elseif n.Kind == "Clock" then + ImGui.PushItemWidth(70) + s.rate = math.max(ImGui.DragFloat("Hz", s.rate, 0.05, 0.1, 20, "%.2f"), 0.1) + ImGui.PopItemWidth() + elseif n.Kind == "LED" then + local p = ImGui.GetCursorScreenPos() + ImGui.GetWindowDrawList():AddCircleFilled(ImVec2(p.x + 12, p.y + 12), 10, s.on and LIT or IM_COL32(40, 40, 40, 255)) + ImGui.Dummy(ImVec2(24, 24)) + elseif n.Kind == "HexDisplay" then + ImGui.SetWindowFontScale(3) + ImGui.TextUnformatted(string.format("%X", s.value or 0)) + ImGui.SetWindowFontScale(1) + end + ImGui.PopID() + for i, name in k.ins do Pin(LogicSim.InPin(n.Id, i), false, name) end + for i, name in k.outs do Pin(LogicSim.OutPin(n.Id, i), true, name) end + ImNodes.EndNode() + end + + local function MenuBar() + if not ImGui.BeginMenuBar() then return end + if ImGui.BeginMenu("File") then + if ImGui.MenuItem("New") then UI.SetGraph(LogicSim.New()) end + if ImGui.MenuItem("Save") then + UI.Saved = LogicSim.Save(UI.Graph) + UI.SaveBuf = ImGui.NewTextBuffer(UI.Saved) + end + if ImGui.MenuItem("Load", nil, false, UI.Saved ~= nil) then UI.SetGraph(LogicSim.Load(UI.Saved)) end + if ImGui.MenuItem("Clear") then UI.Saved, UI.SaveBuf = nil, nil end + _, UI.ShowSave = ImGui.MenuItem("Show Save Text", nil, UI.ShowSave) + ImGui.EndMenu() + end + if ImGui.BeginMenu("Examples") then + for _, e in ExampleList do + if ImGui.MenuItem(e[1]) then UI.LoadExample(e[2]) end + end + ImGui.EndMenu() + end + ImGui.EndMenuBar() + end + + local function Popups() + local g = UI.Graph + ImGui.PushStyleVar(G.ImGuiStyleVar.WindowPadding, ImVec2(8, 8)) + if ImGui.BeginPopup("Add Node") then + for _, c in Categories do + if ImGui.BeginMenu(c[1]) then + for _, kind in c[2] do + if ImGui.MenuItem(kind) then LogicSim.AddNode(g, kind, 0, 0).ScreenPos = UI.AddAt end + end + ImGui.EndMenu() + end + end + ImGui.EndPopup() + end + if ImGui.BeginPopup("Node Menu") then + local n = g.nodes[UI.Target] + if n and ImGui.MenuItem("Duplicate") then + LogicSim.AddNode(g, n.Kind, n.Pos[1] + 30, n.Pos[2] + 30, table.clone(n.state)) + end + if n and ImGui.MenuItem("Delete") then LogicSim.RemoveNode(g, n.Id) end + ImGui.EndPopup() + end + if ImGui.BeginPopup("Link Menu") then + if ImGui.MenuItem("Delete") then LogicSim.RemoveLink(g, UI.Target) end + ImGui.EndPopup() + end + ImGui.PopStyleVar() + end + + function UI.Draw(dt) + if not UI.Graph then UI.LoadExample("FullAdder") end + local g = UI.Graph + LogicSim.Step(g, dt or ImGui.GetIO().DeltaTime) + + ImGui.SetNextWindowSize(ImVec2(900, 600), G.ImGuiCond.FirstUseEver) + ImGui.Begin("Logic Simulator", nil, G.ImGuiWindowFlags.MenuBar) + MenuBar() + if UI.ShowSave and UI.SaveBuf then + ImGui.InputTextMultiline("##save", UI.SaveBuf, #UI.Saved + 1, ImVec2(-1, 120), G.ImGuiInputTextFlags.ReadOnly) + end + local focused = ImGui.IsWindowFocused(G.ImGuiFocusedFlags.RootAndChildWindows) + + ImNodes.BeginNodeEditor() + for _, id in g.order do DrawNode(g.nodes[id]) end + for _, l in g.links do + ImNodes.PushColorStyle(Col.Link, LogicSim.Value(g, l.Start) == 1 and LIT or DIM) + ImNodes.Link(l.Id, l.Start, l.End) + ImNodes.PopColorStyle() + end + ImNodes.MiniMap(0.2, ImNodes.ImNodesMiniMapLocation.BottomRight) + local editorHovered = ImNodes.IsEditorHovered() + ImNodes.EndNodeEditor() + + for _, id in g.order do + local p = ImNodes.GetNodeGridSpacePos(id) + g.nodes[id].Pos = { p.x, p.y } + end + local created, a, c = ImNodes.IsLinkCreated() + if created then LogicSim.AddLink(g, a, c) end + local destroyed, lid = ImNodes.IsLinkDestroyed() + if destroyed then LogicSim.RemoveLink(g, lid) end + + if focused and editorHovered and ImGui.IsMouseClicked(1) then + local nodeHovered, nid = ImNodes.IsNodeHovered() + local linkHovered, hlid = ImNodes.IsLinkHovered() + if nodeHovered then + UI.Target = nid + ImGui.OpenPopup("Node Menu") + elseif linkHovered then + UI.Target = hlid + ImGui.OpenPopup("Link Menu") + else + UI.AddAt = ImGui.GetMousePos() + ImGui.OpenPopup("Add Node") + end + end + if focused and not ImGui.IsAnyItemActive() + and (ImGui.IsKeyPressed(G.ImGuiKey.Delete) or ImGui.IsKeyPressed(G.ImGuiKey.Backspace)) then + DeleteSelected() + end + Popups() + ImGui.End() + end + + return UI +end diff --git a/examples/roblox/main.client.luau b/examples/roblox/main.client.luau index a706c63..2917c22 100644 --- a/examples/roblox/main.client.luau +++ b/examples/roblox/main.client.luau @@ -1,158 +1,17 @@ --- Ports of the upstream examples: hello.cpp, multi_editor.cpp, save_load.cpp (color_node_editor not ported). +-- Logic Simulator: node-editor front end for LogicSim (see LogicUI.lua). local RS = game:GetService("ReplicatedStorage") local RunService = game:GetService("RunService") local G = require(RS:WaitForChild("ImGui")) local ImNodes = require(RS:WaitForChild("ImNodes"))(G) +local LogicSim = require(RS:WaitForChild("LogicSim"))(G, ImNodes) +local UI = require(RS:WaitForChild("LogicUI"))(G, ImNodes, LogicSim) local ImGui = G.ImGui -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() @@ -162,37 +21,14 @@ local function Init() 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 + Style.Flags = bit32.bor(Style.Flags, ImNodes.ImNodesStyleFlags.GridLinesPrimary) end Init() RunService.RenderStepped:Connect(function() G.ImGui_ImplRoblox.NewFrame() ImGui.NewFrame() - Render() + UI.Draw() ImGui.Render() G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) end) diff --git a/tests/t_logicui.luau b/tests/t_logicui.luau new file mode 100644 index 0000000..e38db5f --- /dev/null +++ b/tests/t_logicui.luau @@ -0,0 +1,35 @@ +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImNodes = require(RS:WaitForChild("ImNodes"))(G) +local LogicSim = require(RS:WaitForChild("LogicSim"))(G, ImNodes) +local UI = require(RS:WaitForChild("LogicUI"))(G, ImNodes, LogicSim) +local ImGui = G.ImGui +local Impl = G.ImGui_ImplRoblox + +ImGui.CreateContext() +Impl.Init() +Impl.SetRenderRate(0) +ImNodes.CreateContext() +local io = ImGui.GetIO() + +local function frame(before) + Impl.NewFrame() + io.DeltaTime = 1 / 60 + if before then before() end + ImGui.NewFrame() + UI.Draw() + ImGui.Render() +end + +frame() +frame() +local g = UI.Graph +local victim = g.order[1] +local count = #g.order +assert(g.nodes[victim] and count == 10, "full adder loaded") +ImNodes.SelectNode(victim) +frame(function() io:AddKeyEvent(G.ImGuiKey.Delete, true) end) +frame(function() io:AddKeyEvent(G.ImGuiKey.Delete, false) end) +assert(g.nodes[victim] == nil and #g.order == count - 1, "Delete removed selected node") +assert(ImNodes.NumSelectedNodes() == 0) +print("OK") diff --git a/tools/test.sh b/tools/test.sh index eb7c0f6..285b036 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -3,11 +3,11 @@ cd "$(dirname "$0")/.." fail=0 for t in tests/t_*.luau; do - out=$(python3 tools/core/tools/test/run.py --module ImNodes=ImNodes.lua --module LogicSim=examples/roblox/LogicSim.lua --main "$t" --quiet --roblox 2>&1) + out=$(python3 tools/core/tools/test/run.py --module ImNodes=ImNodes.lua --module LogicSim=examples/roblox/LogicSim.lua --module LogicUI=examples/roblox/LogicUI.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 --module LogicSim=examples/roblox/LogicSim.lua --main "$ex" --roblox 2>&1); rm -f "$ex" +out=$(python3 tools/core/tools/test/run.py --module ImNodes=ImNodes.lua --module LogicSim=examples/roblox/LogicSim.lua --module LogicUI=examples/roblox/LogicUI.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