Added graph node initialization

This commit is contained in:
Rodolphe Suescun 2019-08-13 13:38:11 +02:00
parent 57c28993db
commit 049250e073
3 changed files with 70 additions and 16 deletions

View File

@ -9,3 +9,6 @@ func get_port_source(gen_name: String, input_index: int) -> OutputPort:
if c.to == gen_name and c.to_port == input_index:
return OutputPort.new(get_node(c.from), c.from_port)
return null
func connect_children(src, src_index : int, dst, dst_index : int):
pass

View File

@ -29,7 +29,7 @@ func _gui_input(event):
func get_source(node, port):
for c in get_connection_list():
if c.to == node && c.to_port == port:
if c.to == node and c.to_port == port:
return { node=c.from, slot=c.from_port }
func offset_from_global_position(global_position):
@ -40,6 +40,13 @@ func add_node(node):
node.connect("close_request", self, "remove_node", [ node ])
func connect_node(from, from_slot, to, to_slot):
print("Connecting "+str(from)+"("+str(from_slot)+") to "+str(to)+"("+str(to_slot)+")")
if generator.connect_children(get_node(from).generator, from_slot, get_node(to).generator, to_slot):
var disconnect = get_source(to, to_slot)
if disconnect != null:
.disconnect_node(disconnect.node, disconnect.slot, to, to_slot)
.connect_node(from, from_slot, to, to_slot)
send_changed_signal()
var source_list = [ from ]
# Check if the new connection creates a cycle in the graph
while !source_list.empty():
@ -50,10 +57,6 @@ func connect_node(from, from_slot, to, to_slot):
for c in get_connection_list():
if c.to == source and source_list.find(c.from) == -1:
source_list.append(c.from)
var disconnect = get_source(to, to_slot)
if disconnect != null:
.disconnect_node(disconnect.node, disconnect.slot, to, to_slot)
.connect_node(from, from_slot, to, to_slot)
send_changed_signal()
return true
@ -121,7 +124,7 @@ func create_nodes(data, position = null):
if data.has("type"):
var node = node_factory.create_node(data.type)
if node != null:
if data.has("name") && !has_node(data.name):
if data.has("name") and !has_node(data.name):
node.name = data.name
else:
node.name = get_free_name(data.type)
@ -181,21 +184,21 @@ func export_textures(size = null):
if save_path != null:
var prefix = save_path.left(save_path.rfind("."))
for c in get_children():
if c is GraphNode && c.has_method("export_textures"):
if c is GraphNode and c.has_method("export_textures"):
c.export_textures(prefix, size)
# Cut / copy / paste
func remove_selection():
for c in get_children():
if c is GraphNode and c.selected && c.name != "Material":
if c is GraphNode and c.selected and c.name != "Material":
remove_node(c)
func serialize_selection():
var data = { nodes = [], connections = [] }
var nodes = []
for c in get_children():
if c is GraphNode and c.selected && c.name != "Material":
if c is GraphNode and c.selected and c.name != "Material":
nodes.append(c)
if nodes.empty():
return null
@ -217,7 +220,7 @@ func serialize_selection():
func can_copy():
for c in get_children():
if c is GraphNode and c.selected && c.name != "Material":
if c is GraphNode and c.selected and c.name != "Material":
return true
return false
@ -252,7 +255,7 @@ func center_view():
func send_changed_signal():
set_need_save(true)
#$Timer.start()
$Timer.start()
func do_send_changed_signal():
emit_signal("graph_changed")

View File

@ -2,6 +2,8 @@ extends GraphNode
var generator = null setget set_generator
var controls = []
var uses_seed : bool = false
var parameters = {}
@ -15,8 +17,54 @@ func set_generator(g):
generator = g
update_node(g.model_data)
func initialize_properties(p):
pass
func initialize_properties():
for o in controls:
if o == null:
print("error in node "+name)
elif o is LineEdit:
o.text = str(generator.parameters[o.name])
o.connect("text_changed", self, "_on_text_changed", [ o.name ])
elif o is SpinBox:
o.value = generator.parameters[o.name]
o.connect("value_changed", self, "_on_value_changed", [ o.name ])
elif o is HSlider:
o.value = generator.parameters[o.name]
o.connect("value_changed", self, "_on_value_changed", [ o.name ])
elif o is OptionButton:
o.selected = generator.parameters[o.name]
o.connect("item_selected", self, "_on_value_changed", [ o.name ])
elif o is CheckBox:
o.pressed = generator.parameters[o.name]
o.connect("toggled", self, "_on_value_changed", [ o.name ])
elif o is ColorPickerButton:
o.color = generator.parameters[o.name]
o.connect("color_changed", self, "_on_color_changed", [ o.name ])
elif o is Control and o.filename == "res://addons/material_maker/widgets/gradient_editor.tscn":
var gradient : MMGradient = MMGradient.new()
gradient.deserialize(generator.parameters[o.name])
o.value = gradient
o.connect("updated", self, "_on_gradient_changed", [ o.name ])
else:
print("unsupported widget "+str(o))
func update_shaders():
get_parent().send_changed_signal()
func _on_text_changed(new_text, variable):
generator.parameters[variable] = float(new_text)
update_shaders()
func _on_value_changed(new_value, variable):
generator.parameters[variable] = new_value
update_shaders()
func _on_color_changed(new_color, variable):
generator.parameters[variable] = new_color
update_shaders()
func _on_gradient_changed(new_gradient, variable):
generator.parameters[variable] = new_gradient
update_shaders()
func update_node(data):
print("node_generic.update_node")
@ -40,7 +88,7 @@ func update_node(data):
if model_data.has("instance") and model_data.instance.find("$(seed)"):
uses_seed = true
if model_data.has("parameters") and typeof(model_data.parameters) == TYPE_ARRAY:
var control_list = []
controls = []
var sizer = null
for p in model_data.parameters:
if !p.has("name") or !p.has("type"):
@ -79,7 +127,7 @@ func update_node(data):
if control != null:
var label = p.name
control.name = label
control_list.append(control)
controls.append(control)
if p.has("label"):
label = p.label
if sizer == null or label != "nonewline":
@ -93,7 +141,7 @@ func update_node(data):
sizer.add_child(label_widget)
control.size_flags_horizontal = SIZE_EXPAND | SIZE_FILL
sizer.add_child(control)
initialize_properties(control_list)
initialize_properties()
else:
model_data.parameters = []
if model_data.has("inputs") and typeof(model_data.inputs) == TYPE_ARRAY: