Skip to content

Commit

Permalink
feat(shader_graph): node positions are now saved/loaded with shader g…
Browse files Browse the repository at this point in the history
…raphs
  • Loading branch information
Silverlan committed Dec 24, 2024
1 parent a45955a commit 9f0aaba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
9 changes: 6 additions & 3 deletions assets/addons/shader_graph/lua/gui/shader_graph/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function Element:AddControl(socketType, linkable, title, id, type, defaultVal, m
local ctrlMenu = (socketType == gui.GraphNodeSocket.SOCKET_TYPE_INPUT) and self.m_inputControls
or self.m_outputControls
local elCtrl
local elWrapper
if (socketType == gui.GraphNodeSocket.SOCKET_TYPE_INPUT) and type ~= nil then
local udmType = shader.Socket.to_udm_type(type)
local propInfo = {
Expand All @@ -123,9 +124,11 @@ function Element:AddControl(socketType, linkable, title, id, type, defaultVal, m
end
end)
elCtrl = wrapper:GetWrapperElement()
elWrapper = wrapper
else
local el, wrapper = ctrlMenu:AddInfo(title, id)
elCtrl = wrapper
elWrapper = wrapper
end
local shaderGraph = self:GetShaderGraph()
if util.is_valid(shaderGraph) == false then
Expand Down Expand Up @@ -154,7 +157,7 @@ function Element:AddControl(socketType, linkable, title, id, type, defaultVal, m
controlElement = elCtrl,
}
self:UpdateSocketPosition(t[id], socketType == gui.GraphNodeSocket.SOCKET_TYPE_OUTPUT)
return el, elCtrl
return el, elWrapper
end
function Element:UpdateSocketPosition(elData, output)
local frame = self:GetFrame()
Expand Down Expand Up @@ -218,10 +221,10 @@ function Element:AddInput(name, type, linkable, defaultVal, minVal, maxVal, enum
maxVal,
enumValues
)
return elSocket
return elSocket, elCtrl
end
function Element:AddOutput(name)
local elSocket, elCtrl = self:AddControl(gui.GraphNodeSocket.SOCKET_TYPE_OUTPUT, true, name, name)
return elSocket
return elSocket, elCtrl
end
gui.register("WIGraphNode", Element)
33 changes: 27 additions & 6 deletions assets/addons/shader_graph/lua/gui/shader_graph/shader_graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ function Element:MouseCallback(button, state, mods)
end
end
end
function Element:GetNodeData()
return self.m_nodeData
end
function Element:GetNodeElements()
local t = {}
for _, v in ipairs(self.m_nodeData) do
table.insert(t, v.nodeElement)
end
return t
end
function Element:DeselectAll()
for _, t in ipairs(self.m_nodeData) do
t.nodeElement:SetSelected(false)
Expand All @@ -165,12 +175,12 @@ function Element:SetGraph(graph)
self.m_graph = graph

local nodes = self.m_graph:GetNodes()
local offset = self:GetWidth() * 0.5
for _, graphNode in ipairs(nodes) do
local pos = graphNode:GetPos()
local frame = self:AddNode(graphNode)
frame:SetX(offset)
frame:SetY(self:GetHeight() * 0.5)
offset = offset + frame:GetWidth() + 80
pos.x = pos.x + self:GetHalfWidth()
pos.y = pos.y + self:GetHalfHeight()
frame:SetPos(pos)
end

self:InitializeLinks()
Expand Down Expand Up @@ -318,6 +328,7 @@ function Element:AddNode(graphNode)
local socket = output:GetSocket()
local elOutput = elNode:AddOutput(socket.name)
end
local socketValues = {}
for _, input in ipairs(graphNode:GetInputs()) do
local socket = input:GetSocket()
local enumSet = socket.enumSet
Expand All @@ -328,7 +339,7 @@ function Element:AddNode(graphNode)
table.insert(enumValues, { tostring(k), v })
end
end
local elInput = elNode:AddInput(
local elInput, elWrapper = elNode:AddInput(
socket.name,
socket.type,
socket:IsLinkable(),
Expand All @@ -337,13 +348,23 @@ function Element:AddNode(graphNode)
socket.max,
enumValues
)

table.insert(socketValues, {
elWrapper = elWrapper,
value = graphNode:GetInputValue(socket.name),
})
end
elNode:ResetControls()

for _, val in ipairs(socketValues) do
print(val.elWrapper)
val.elWrapper:SetValue(val.value)
end

local t = {
frame = frame,
nodeElement = elNode,
graphNode = name,
graphNode = graphNode,
}
self.m_frameToNodeData[frame] = t
table.insert(self.m_nodeData, t)
Expand Down

0 comments on commit 9f0aaba

Please sign in to comment.