-- GENERATED by tools/love.sh from the Luau source - do not edit local function __iter(t, ...) if type(t) ~= 'table' or (getmetatable(t) and getmetatable(t).__call) then return t, ... end local n, i, k = #t, 0, nil return function() if i < n then i = i + 1 return i, t[i] end repeat k = next(t, k) until k == nil or type(k) ~= 'number' or k < 1 or k > n or k % 1 ~= 0 if k ~= nil then return k, t[k] end end end table.create = table.create or function(n, v) local t = {} if v ~= nil then for i = 1, n do t[i] = v end end return t end table.clone = table.clone or function(t) local c = {} for k, v in pairs(t) do c[k] = v end return setmetatable(c, getmetatable(t)) end math.clamp = math.clamp or function(x, a, b) return math.max(a, math.min(b, x)) end math.sign = math.sign or function(x) return x > 0 and 1 or (x < 0 and -1 or 0) end math.round = math.round or function(x) return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5) end -- "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 __iter(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 __iter(ImNodes.GetSelectedLinks()) do LogicSim.RemoveLink(g, id) end for _, id in __iter(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 __iter(k.ins) do Pin(LogicSim.InPin(n.Id, i), false, name) end for i, name in __iter(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 __iter(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 __iter(Categories) do if ImGui.BeginMenu(c[1]) then for _, kind in __iter(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 __iter(g.order) do DrawNode(g.nodes[id]) end for _, l in __iter(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 __iter(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