material-maker/addons/material_maker/nodes/remote.gd

138 lines
4.6 KiB
GDScript3
Raw Normal View History

tool
extends MMGraphNodeGeneric
class_name MMGraphNodeRemote
2019-09-26 22:32:49 +02:00
var links = {}
onready var grid = $Controls
func add_control(text, control):
2019-09-26 22:32:49 +02:00
var index = grid.get_child_count() / 4
var label = preload("res://addons/material_maker/widgets/linked_widgets/editable_label.tscn").instance()
label.set_text(text)
2019-09-26 22:32:49 +02:00
grid.add_child(label)
grid.add_child(control)
control.connect("mouse_entered", self, "on_enter_widget", [ control ])
control.connect("mouse_exited", self, "on_exit_widget", [ control ])
var button = Button.new()
button.icon = preload("res://addons/material_maker/icons/link.png")
2019-09-26 22:32:49 +02:00
grid.add_child(button)
button.connect("pressed", self, "_on_Link_pressed", [ index ])
button = Button.new()
button.icon = preload("res://addons/material_maker/icons/remove.png")
2019-09-26 22:32:49 +02:00
grid.add_child(button)
button.connect("pressed", generator, "remove_parameter", [ index ])
2019-09-20 20:43:57 +02:00
func update_node():
var i : int = 0
2019-09-26 22:32:49 +02:00
for c in grid.get_children():
c.queue_free()
yield(get_tree(), "idle_frame")
title = generator.get_type_name()
controls = {}
for p in generator.get_parameter_defs():
var control = create_parameter_control(p)
if control != null:
control.name = p.name
controls[control.name] = control
add_control(generator.widgets[i].label, control)
if generator.widgets[i].type == "config_control":
var current = null
if control.get_item_count() > 0:
control.selected = generator.parameters["param"+str(i)]
current = control.get_item_text(control.selected)
control.add_separator()
control.add_item("<add configuration>")
if current != null:
control.add_separator()
control.add_item("<update "+current+">")
control.add_item("<remove "+current+">")
i += 1
rect_size = Vector2(0, 0)
initialize_properties()
func _on_value_changed(new_value, variable):
var param_index = variable.trim_prefix("param").to_int()
var widget = generator.widgets[param_index]
if widget.type == "config_control":
var configuration_count = widget.configurations.size()
2019-09-26 22:32:49 +02:00
var control = grid.get_child(param_index*4+1)
if new_value < configuration_count:
._on_value_changed(new_value, variable)
var current = control.get_item_text(new_value)
control.set_item_text(configuration_count+3, "<update "+current+">")
control.set_item_text(configuration_count+4, "<remove "+current+">")
else:
var current = control.get_item_text(generator.parameters[variable])
var command = new_value - widget.configurations.size()
match command:
1:
var dialog = preload("res://addons/material_maker/widgets/line_dialog.tscn").instance()
add_child(dialog)
dialog.set_texts("Configuration", "Enter a name for the new configuration")
dialog.connect("ok", self, "do_add_configuration", [ param_index ])
dialog.popup_centered()
3:
generator.update_configuration(param_index, current)
4:
generator.parameters[variable] = 0
generator.remove_configuration(param_index, current)
_:
print(command)
else:
._on_value_changed(new_value, variable)
func do_add_configuration(config_name, param_index):
generator.add_configuration(param_index, config_name)
func _on_AddLink_pressed():
var widget = Control.new()
add_control("Unnamed", widget)
var link = MMNodeLink.new(get_parent())
link.pick(widget, generator, generator.create_linked_control("Unnamed"), true)
func _on_AddConfig_pressed():
var widget = Control.new()
add_control("Unnamed", widget)
var link = MMNodeLink.new(get_parent())
link.pick(widget, generator, generator.create_config_control("Unnamed"), true)
func _on_Link_pressed(index):
var link = MMNodeLink.new(get_parent())
2019-09-26 22:32:49 +02:00
link.pick(grid.get_child(index*4+1), generator, index)
func _on_Remote_resize_request(new_minsize):
rect_size = new_minsize
func _on_HBoxContainer_minimum_size_changed():
print("_on_HBoxContainer_minimum_size_changed "+str($HBoxContainer.rect_min_size))
func on_parameter_changed(p, v):
if p == "":
update_node()
else:
2019-09-26 22:32:49 +02:00
.on_parameter_changed(p, v)
func on_enter_widget(widget):
var param_index = widget.name.trim_prefix("param").to_int()
var w = generator.widgets[param_index]
var new_links = []
for l in w.linked_widgets:
var graph_node = get_parent().get_node("node_"+l.node)
if graph_node != null:
var control = graph_node.controls[l.widget]
if control != null:
var link = MMNodeLink.new(get_parent())
link.show_link(widget, control)
new_links.push_back(link)
# free existing links if any
on_exit_widget(widget)
# store new links
links[widget] = new_links
func on_exit_widget(widget):
if links.has(widget):
for l in links[widget]:
l.queue_free()
links.erase(widget)