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

147 lines
4.9 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) -> void:
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-10-24 09:00:31 +02:00
label.connect("label_changed", self, "on_label_changed", [ control.name ])
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()
2019-10-06 14:07:49 +02:00
button.icon = preload("res://addons/material_maker/icons/link.tres")
2019-09-26 22:32:49 +02:00
grid.add_child(button)
2019-10-24 09:00:31 +02:00
button.connect("pressed", self, "_on_Link_pressed", [ control.name ])
button = Button.new()
2019-10-06 14:07:49 +02:00
button.icon = preload("res://addons/material_maker/icons/remove.tres")
2019-09-26 22:32:49 +02:00
grid.add_child(button)
2019-10-24 09:00:31 +02:00
button.connect("pressed", generator, "remove_parameter", [ control.name ])
2019-09-20 20:43:57 +02:00
func update_node() -> void:
2019-10-24 09:00:31 +02:00
# Show or hide the close button
show_close = generator.can_be_deleted()
# Delete the contents and wait until it's done
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
2019-10-24 09:00:31 +02:00
add_control(generator.get_widget(p.name).label, control)
if generator.widgets[i].type == "config_control":
var current = null
if control.get_item_count() > 0:
2019-10-24 09:00:31 +02:00
control.selected = generator.parameters[p.name]
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()
2019-10-24 09:00:31 +02:00
func _on_value_changed(new_value, variable : String) -> void:
var widget = generator.get_widget(variable)
if widget.type == "config_control":
var configuration_count = widget.configurations.size()
2019-10-24 09:00:31 +02:00
var control = controls[variable]
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")
2019-10-24 09:00:31 +02:00
dialog.connect("ok", self, "do_add_configuration", [ variable ])
dialog.popup_centered()
3:
2019-10-24 09:00:31 +02:00
generator.update_configuration(variable, current)
4:
generator.parameters[variable] = 0
2019-10-24 09:00:31 +02:00
generator.remove_configuration(variable, current)
_:
print(command)
else:
._on_value_changed(new_value, variable)
2019-10-24 09:00:31 +02:00
func do_add_configuration(config_name : String, param_name : String) -> void:
generator.add_configuration(param_name, config_name)
2019-10-24 09:00:31 +02:00
func on_label_changed(new_label, param_name) -> void:
generator.set_label(param_name, new_label)
2019-10-10 22:43:34 +02:00
func _on_AddLink_pressed() -> void:
2019-10-24 09:00:31 +02:00
var control = generator.create_linked_control("Unnamed")
var widget = Control.new()
2019-10-24 09:00:31 +02:00
widget.name = control
add_control("Unnamed", widget)
var link = MMNodeLink.new(get_parent())
2019-10-24 09:00:31 +02:00
link.pick(widget, generator, control, true)
func _on_AddConfig_pressed() -> void:
2019-10-24 09:00:31 +02:00
var control = generator.create_config_control("Unnamed")
var widget = Control.new()
2019-10-24 09:00:31 +02:00
widget.name = control
add_control("Unnamed", widget)
var link = MMNodeLink.new(get_parent())
2019-10-24 09:00:31 +02:00
link.pick(widget, generator, control, true)
2019-10-24 09:00:31 +02:00
func _on_Link_pressed(param_name) -> void:
var link = MMNodeLink.new(get_parent())
2019-10-24 09:00:31 +02:00
link.pick(controls[param_name], generator, param_name)
func _on_Remote_resize_request(new_minsize) -> void:
rect_size = new_minsize
func _on_HBoxContainer_minimum_size_changed() -> void:
print("_on_HBoxContainer_minimum_size_changed "+str($HBoxContainer.rect_min_size))
func on_parameter_changed(p, v) -> void:
if p == "":
update_node()
else:
2019-09-26 22:32:49 +02:00
.on_parameter_changed(p, v)
func on_enter_widget(widget) -> void:
2019-10-24 09:00:31 +02:00
var w = generator.get_widget(widget.name)
2019-09-26 22:32:49 +02:00
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) -> void:
2019-09-26 22:32:49 +02:00
if links.has(widget):
for l in links[widget]:
l.queue_free()
links.erase(widget)