2018-09-08 12:25:28 +02:00
|
|
|
tool
|
|
|
|
extends "res://addons/material_maker/widgets/linked_widgets/linked_control_base.gd"
|
|
|
|
|
|
|
|
var configurations = {}
|
|
|
|
|
|
|
|
var current = null
|
|
|
|
onready var button = null
|
|
|
|
|
2018-09-09 12:09:05 +02:00
|
|
|
const Types = preload("res://addons/material_maker/types/types.gd")
|
|
|
|
|
2018-09-08 12:25:28 +02:00
|
|
|
func _ready():
|
|
|
|
update_options()
|
|
|
|
|
|
|
|
func update_options():
|
|
|
|
# Seems "clear" might cause crashes, so we create a new button...
|
|
|
|
if button != null:
|
|
|
|
button.hide()
|
|
|
|
button.queue_free()
|
|
|
|
button = OptionButton.new()
|
|
|
|
button.connect("item_selected", self, "_on_item_selected")
|
|
|
|
button.connect("mouse_entered", self, "_on_mouse_entered")
|
|
|
|
button.connect("mouse_exited", self, "_on_mouse_exited")
|
|
|
|
add_child(button)
|
|
|
|
# Create list of configurations
|
|
|
|
var keys = configurations.keys()
|
|
|
|
keys.sort()
|
|
|
|
# if no configuration is selected, select the first one
|
|
|
|
if current == null and !keys.empty():
|
|
|
|
current = keys[0]
|
|
|
|
for c in keys:
|
|
|
|
button.add_item(c)
|
|
|
|
button.add_separator()
|
|
|
|
button.add_item("<add configuration>")
|
|
|
|
if current != null:
|
|
|
|
button.add_separator()
|
|
|
|
button.add_item("<update "+current+">")
|
|
|
|
button.add_item("<remove "+current+">")
|
|
|
|
for i in range(button.get_item_count()):
|
|
|
|
if button.get_item_text(i) == current:
|
|
|
|
button.selected = i
|
|
|
|
break
|
|
|
|
|
|
|
|
func add_linked(node, widget):
|
|
|
|
linked_widgets.append({ node=node, widget=widget })
|
|
|
|
|
2018-09-09 12:09:05 +02:00
|
|
|
func duplicate_value(value):
|
|
|
|
if typeof(value) == TYPE_OBJECT and value.has_method("duplicate"):
|
|
|
|
value = value.duplicate()
|
|
|
|
return value
|
|
|
|
|
2019-09-24 22:25:46 +02:00
|
|
|
func apply_configuration(i):
|
|
|
|
get_parent().get_parent().generator.set_parameter("param"+str(get_index()%4-1), i)
|
2018-09-08 12:25:28 +02:00
|
|
|
|
|
|
|
func do_update_configuration(name):
|
|
|
|
var configuration = []
|
|
|
|
for w in linked_widgets:
|
2018-10-02 07:46:20 +02:00
|
|
|
configuration.append({ node=w.node, widget=w.widget, value=duplicate_value(w.node.parameters[w.widget.name]) })
|
2018-09-08 12:25:28 +02:00
|
|
|
configurations[name] = configuration
|
|
|
|
current = name
|
|
|
|
update_options()
|
|
|
|
|
|
|
|
func update_configuration():
|
|
|
|
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_update_configuration", [])
|
|
|
|
dialog.popup_centered()
|
|
|
|
|
|
|
|
func _on_item_selected(ID):
|
|
|
|
var count = configurations.keys().size()
|
|
|
|
if ID >= 0 && ID < count:
|
|
|
|
current = button.get_item_text(ID)
|
|
|
|
update_options()
|
2019-09-24 22:25:46 +02:00
|
|
|
apply_configuration(ID)
|
2018-09-08 12:25:28 +02:00
|
|
|
elif ID == count+1:
|
|
|
|
button.selected = 0
|
|
|
|
update_configuration()
|
|
|
|
elif ID == count+3:
|
|
|
|
do_update_configuration(current)
|
|
|
|
else:
|
|
|
|
configurations.erase(current)
|
|
|
|
current = null
|
|
|
|
update_options()
|
|
|
|
|
|
|
|
func serialize():
|
|
|
|
var data = .serialize()
|
|
|
|
data.type = "config_control"
|
|
|
|
var data_configurations = {}
|
|
|
|
var keys = configurations.keys()
|
|
|
|
for k in keys:
|
|
|
|
var c = configurations[k]
|
|
|
|
var data_configuration = []
|
|
|
|
for e in c:
|
2018-09-09 12:09:05 +02:00
|
|
|
data_configuration.append({ node=e.node.name, widget=e.widget.name, value=Types.serialize_value(e.value) })
|
2018-09-08 12:25:28 +02:00
|
|
|
data_configurations[k] = data_configuration
|
|
|
|
data.configurations = data_configurations
|
|
|
|
return data
|
|
|
|
|
|
|
|
func deserialize(data):
|
|
|
|
.deserialize(data)
|
|
|
|
graph_edit = get_parent()
|
|
|
|
while graph_edit != null && !(graph_edit is GraphEdit):
|
|
|
|
graph_edit = graph_edit.get_parent()
|
|
|
|
if graph_edit == null:
|
|
|
|
return
|
|
|
|
var keys = data.configurations.keys()
|
|
|
|
for k in keys:
|
|
|
|
var c = data.configurations[k]
|
|
|
|
var configuration = []
|
|
|
|
for e in c:
|
2019-09-20 20:43:57 +02:00
|
|
|
var node = graph_edit.get_node("node_"+e.node)
|
2018-09-08 12:25:28 +02:00
|
|
|
var widget = null
|
2019-09-20 20:43:57 +02:00
|
|
|
for w in node.controls:
|
2019-09-24 22:25:46 +02:00
|
|
|
if w == e.widget:
|
2018-09-08 12:25:28 +02:00
|
|
|
widget = w
|
|
|
|
break
|
2018-09-09 12:09:05 +02:00
|
|
|
configuration.append({ node=node, widget=widget, value=Types.deserialize_value(e.value) })
|
2018-09-08 12:25:28 +02:00
|
|
|
configurations[k] = configuration
|
|
|
|
update_options()
|
|
|
|
|
|
|
|
|
|
|
|
|