199 lines
5.5 KiB
Luau
199 lines
5.5 KiB
Luau
-- 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
|
|
|
|
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()
|
|
Render()
|
|
ImGui.Render()
|
|
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
|
end)
|