Files

92 lines
2.9 KiB
Luau

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()
Impl.SetRenderRate(0)
ImNodes.CreateContext()
local io = ImGui.GetIO()
local function frame(n, before)
Impl.NewFrame()
io.DeltaTime = 1 / 60
if before then
before()
end
ImGui.NewFrame()
ImGui.SetNextWindowPos(ImVec2(0, 0), G.ImGuiCond.Always)
ImGui.SetNextWindowSize(ImVec2(800, 600), G.ImGuiCond.Always)
ImGui.Begin("editor")
ImNodes.BeginNodeEditor()
if n == 1 then
ImNodes.SetNodeGridSpacePos(1, ImVec2(50, 50))
ImNodes.SetNodeGridSpacePos(2, ImVec2(300, 100))
end
ImNodes.BeginNode(1)
ImNodes.BeginNodeTitleBar()
ImGui.Text("node A")
ImNodes.EndNodeTitleBar()
ImNodes.BeginOutputAttribute(11)
ImGui.Text("out")
ImNodes.EndOutputAttribute()
ImNodes.BeginStaticAttribute(12)
ImGui.Text("static")
ImNodes.EndStaticAttribute()
ImNodes.EndNode()
ImNodes.BeginNode(2)
ImNodes.BeginNodeTitleBar()
ImGui.Text("node B")
ImNodes.EndNodeTitleBar()
ImNodes.BeginInputAttribute(21, ImNodes.ImNodesPinShape.Quad)
ImGui.Text("in")
ImNodes.EndInputAttribute()
ImNodes.EndNode()
ImNodes.Link(100, 11, 21)
ImNodes.EndNodeEditor()
ImGui.End()
ImGui.Render()
local dd = ImGui.GetDrawData()
Impl.RenderDrawData(dd)
return dd.TotalVtxCount
end
local vtx = 0
for n = 1, 20 do
vtx = frame(n)
end
assert(vtx > 0, "no vertices")
local dims = ImNodes.GetNodeDimensions(1)
assert(dims.x > 0 and dims.y > 0)
-- drag node 1 by its title bar
local p0 = ImNodes.GetNodeGridSpacePos(1)
local s = ImNodes.GetNodeScreenSpacePos(1)
local tx, ty = s.x + 12, s.y + 10
frame(21, function() io:AddMousePosEvent(tx, ty) end)
frame(22, function() io:AddMouseButtonEvent(0, true) end)
for i = 1, 5 do
frame(22 + i, function() io:AddMousePosEvent(tx + i * 10, ty + i * 4) end)
end
frame(28, function() io:AddMouseButtonEvent(0, false) end)
local p1 = ImNodes.GetNodeGridSpacePos(1)
assert(math.abs(p1.x - p0.x - 50) < 1e-3 and math.abs(p1.y - p0.y - 20) < 1e-3, `node not dragged: {p0.x},{p0.y} -> {p1.x},{p1.y}`)
assert(ImNodes.GetNodeGridSpacePos(2).x == 300)
-- pan with middle mouse on empty canvas
local pan0 = ImNodes.EditorContextGetPanning()
frame(29, function() io:AddMousePosEvent(700, 500) end)
frame(30, function() io:AddMouseButtonEvent(2, true) end)
frame(31, function() io:AddMousePosEvent(680, 490) end)
frame(32, function() io:AddMouseButtonEvent(2, false) end)
local pan1 = ImNodes.EditorContextGetPanning()
assert(pan1.x - pan0.x == -20 and pan1.y - pan0.y == -10, `pan: {pan1.x},{pan1.y}`)
ImNodes.EditorContextMoveToNode(2)
assert(ImNodes.EditorContextGetPanning().x == -300)
ImNodes.DestroyContext()
print("OK")