Logic Simulator example UI (LogicUI), Delete-key test
This commit is contained in:
1 parent
53d4cfba75
commit
d471cb855c
5 files changed
+253
-172
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user