2019-09-20 20:43:57 +02:00
|
|
|
tool
|
|
|
|
extends MMGenBase
|
|
|
|
class_name MMGenRemote
|
|
|
|
|
|
|
|
"""
|
|
|
|
Remote can be used to control parameters from several generators in the same graph
|
|
|
|
"""
|
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
var widgets = []
|
2019-09-20 20:43:57 +02:00
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func can_be_deleted() -> bool:
|
|
|
|
return name != "gen_parameters"
|
|
|
|
|
|
|
|
func set_widgets(w : Array) -> void:
|
2019-09-20 20:43:57 +02:00
|
|
|
widgets = w
|
2019-10-24 09:00:31 +02:00
|
|
|
fix()
|
|
|
|
|
|
|
|
func get_widget(n : String) -> Dictionary:
|
2019-09-22 22:17:26 +02:00
|
|
|
for w in widgets:
|
2019-10-24 09:00:31 +02:00
|
|
|
if w.name == n:
|
|
|
|
return w
|
|
|
|
return {}
|
|
|
|
|
|
|
|
func get_next_widget_name():
|
|
|
|
var i = 0
|
|
|
|
while true:
|
2019-09-22 22:17:26 +02:00
|
|
|
var param_name = "param"+str(i)
|
2019-10-24 09:00:31 +02:00
|
|
|
var used = false
|
|
|
|
for w in widgets:
|
|
|
|
if w.has("name") and w.name == param_name:
|
|
|
|
used = true
|
|
|
|
break
|
|
|
|
if !used:
|
|
|
|
return param_name
|
2019-09-22 22:17:26 +02:00
|
|
|
i += 1
|
2019-09-20 20:43:57 +02:00
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func fix() -> void:
|
|
|
|
# Make sure all widgets have a name
|
|
|
|
var i = 0
|
|
|
|
for w in widgets:
|
|
|
|
if !w.has("name"):
|
|
|
|
w.name = get_next_widget_name()
|
|
|
|
print("Named "+w.name)
|
|
|
|
var parent = get_parent()
|
|
|
|
if parent == null:
|
|
|
|
return
|
|
|
|
var updated_widgets : Array = []
|
|
|
|
var removed_widgets = false
|
|
|
|
for w in widgets:
|
|
|
|
var updated_linked_widgets : Array = []
|
|
|
|
for l in w.linked_widgets:
|
|
|
|
var linked_widget : MMGenBase = parent.get_node(l.node)
|
|
|
|
if linked_widget != null and linked_widget.get_parameter_def(l.widget).size() > 0:
|
|
|
|
updated_linked_widgets.push_back(l)
|
|
|
|
if updated_linked_widgets.size() == 0:
|
|
|
|
removed_widgets = true
|
|
|
|
continue
|
|
|
|
w.linked_widgets = updated_linked_widgets
|
|
|
|
updated_widgets.push_back(w)
|
|
|
|
if !parameters.has(w.name):
|
|
|
|
match w.type:
|
|
|
|
"config_control":
|
|
|
|
parameters[w.name] = 0
|
|
|
|
"linked_control":
|
|
|
|
# Should get current value of a linked widget
|
|
|
|
parameters[w.name] = 0
|
|
|
|
if removed_widgets:
|
|
|
|
widgets = updated_widgets
|
|
|
|
emit_signal("parameter_changed", "__update_all__", null)
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type() -> String:
|
2019-09-24 22:25:46 +02:00
|
|
|
return "remote"
|
2019-09-20 20:43:57 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type_name() -> String:
|
2019-09-29 10:07:32 +02:00
|
|
|
return "Parameters" if name == "gen_parameters" else "Remote"
|
2019-09-20 20:43:57 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_parameter_defs() -> Array:
|
2019-09-22 22:17:26 +02:00
|
|
|
var rv = []
|
|
|
|
var i = 0
|
|
|
|
for w in widgets:
|
|
|
|
match w.type:
|
|
|
|
"config_control":
|
2019-10-24 09:00:31 +02:00
|
|
|
var p : Dictionary = { name=w.name, label=w.label, type="enum", values=[] }
|
2019-09-25 23:29:06 +02:00
|
|
|
var configurations = w.configurations.keys()
|
|
|
|
configurations.sort()
|
|
|
|
for c in configurations:
|
2019-09-22 22:17:26 +02:00
|
|
|
p.values.push_back({ name=c, value=c })
|
|
|
|
rv.append(p)
|
|
|
|
i += 1
|
|
|
|
"linked_control":
|
|
|
|
var linked = w.linked_widgets[0]
|
2019-10-24 09:00:31 +02:00
|
|
|
var p : Dictionary
|
2019-10-12 07:10:25 +02:00
|
|
|
if linked != null && is_inside_tree():
|
|
|
|
var gen = get_parent().get_node(linked.node)
|
|
|
|
if gen != null:
|
|
|
|
var gen_params = gen.get_parameter_defs()
|
|
|
|
for pd in gen_params:
|
|
|
|
if pd.name == linked.widget:
|
|
|
|
p = pd.duplicate(true)
|
|
|
|
break
|
2019-10-24 09:00:31 +02:00
|
|
|
p.name = w.name
|
2019-10-12 07:10:25 +02:00
|
|
|
p.label = w.label
|
|
|
|
rv.append(p)
|
2019-09-22 22:17:26 +02:00
|
|
|
i += 1
|
|
|
|
_:
|
2019-10-24 09:00:31 +02:00
|
|
|
print("Unsupported widget of type "+str(w.type))
|
2019-09-22 22:17:26 +02:00
|
|
|
return rv
|
|
|
|
|
2019-10-21 23:58:14 +02:00
|
|
|
func set_parameter(p : String, v) -> void:
|
2019-09-22 22:17:26 +02:00
|
|
|
var parent = get_parent()
|
2019-10-21 23:58:14 +02:00
|
|
|
if parent == null:
|
|
|
|
return
|
2019-10-24 09:00:31 +02:00
|
|
|
var widget = get_widget(p)
|
2019-10-26 13:49:35 +02:00
|
|
|
if !widget.empty():
|
|
|
|
match widget.type:
|
|
|
|
"linked_control":
|
|
|
|
for w in widget.linked_widgets:
|
2019-09-26 22:32:49 +02:00
|
|
|
var node = parent.get_node(w.node)
|
|
|
|
if node != null:
|
2019-10-26 13:49:35 +02:00
|
|
|
node.set_parameter(w.widget, v)
|
|
|
|
"config_control":
|
|
|
|
if v < widget.configurations.size():
|
|
|
|
var configurations = widget.configurations.keys()
|
|
|
|
configurations.sort()
|
|
|
|
for w in widget.configurations[configurations[v]]:
|
|
|
|
var node = parent.get_node(w.node)
|
|
|
|
if node != null:
|
|
|
|
node.set_parameter(w.widget, MMType.deserialize_value(w.value))
|
|
|
|
else:
|
|
|
|
# incorrect configuration index
|
|
|
|
print("error: incorrect config control parameter value")
|
|
|
|
return
|
2019-09-25 08:04:36 +02:00
|
|
|
.set_parameter(p, v)
|
2019-09-27 08:28:33 +02:00
|
|
|
if name == "gen_parameters":
|
|
|
|
get_parent().parameters[p] = v
|
2019-09-22 22:17:26 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _serialize(data: Dictionary) -> Dictionary:
|
2019-09-20 20:43:57 +02:00
|
|
|
data.type = "remote"
|
|
|
|
data.widgets = widgets
|
|
|
|
return data
|
2019-09-24 22:25:46 +02:00
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func create_linked_control(label : String) -> String:
|
|
|
|
var n = get_next_widget_name()
|
|
|
|
widgets.push_back({ name=n, label=label, type="linked_control", linked_widgets=[] })
|
|
|
|
return n
|
|
|
|
|
|
|
|
func create_config_control(label : String) -> String:
|
|
|
|
var n = get_next_widget_name()
|
|
|
|
widgets.push_back({ name=n, label=label, type="config_control", linked_widgets=[], configurations={} })
|
|
|
|
return n
|
|
|
|
|
|
|
|
func set_label(widget_name : String, new_label : String) -> void:
|
|
|
|
get_widget(widget_name).label = new_label
|
|
|
|
|
|
|
|
func can_link_parameter(widget_name : String, generator : MMGenBase, param : String) -> bool:
|
|
|
|
var widget : Dictionary = get_widget(widget_name)
|
|
|
|
if !widget.linked_widgets.empty():
|
|
|
|
# Check if the param is already linked
|
|
|
|
for lw in widget.linked_widgets:
|
|
|
|
if lw.node == generator.name and lw.widget == param:
|
|
|
|
return false
|
|
|
|
# Check the parameter type
|
|
|
|
var linked : Dictionary = widget.linked_widgets[0]
|
|
|
|
var linked_generator : MMGenBase = get_parent().get_node(linked.node)
|
|
|
|
var linked_parameter : Dictionary = linked_generator.get_parameter_def(linked.widget)
|
|
|
|
var parameter : Dictionary = generator.get_parameter_def(param)
|
|
|
|
if parameter.type != linked_parameter.type:
|
|
|
|
return false
|
|
|
|
match parameter.type:
|
|
|
|
"enum":
|
|
|
|
if to_json(linked_parameter.values) != to_json(parameter.values):
|
|
|
|
return false
|
2019-09-24 22:25:46 +02:00
|
|
|
return true
|
2019-10-20 16:22:06 +02:00
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func link_parameter(widget_name : String, generator : MMGenBase, param : String) -> void:
|
|
|
|
if !can_link_parameter(widget_name, generator, param):
|
2019-09-24 22:25:46 +02:00
|
|
|
return
|
2019-10-24 09:00:31 +02:00
|
|
|
var widget : Dictionary = get_widget(widget_name)
|
2019-09-25 23:29:06 +02:00
|
|
|
widget.linked_widgets.push_back({ node=generator.name, widget=param })
|
|
|
|
if widget.linked_widgets.size() == 1:
|
|
|
|
match widget.type:
|
|
|
|
"linked_control":
|
2019-10-24 09:00:31 +02:00
|
|
|
parameters[widget_name] = generator.parameters[param]
|
2019-09-25 23:29:06 +02:00
|
|
|
"config_control":
|
2019-10-24 09:00:31 +02:00
|
|
|
parameters[widget_name] = 0
|
|
|
|
emit_signal("parameter_changed", "__update_all__", null)
|
|
|
|
|
|
|
|
func remove_parameter(widget_name : String) -> void:
|
|
|
|
for i in range(widgets.size()):
|
|
|
|
if widgets[i].name == widget_name:
|
|
|
|
widgets.remove(i)
|
|
|
|
break
|
|
|
|
emit_signal("parameter_changed", "__update_all__", null)
|
|
|
|
|
|
|
|
func add_configuration(widget_name : String, config_name : String) -> void:
|
|
|
|
var widget = get_widget(widget_name)
|
2019-09-25 23:29:06 +02:00
|
|
|
if widget.type == "config_control":
|
|
|
|
widget.configurations[config_name] = []
|
|
|
|
var configurations = widget.configurations.keys()
|
|
|
|
configurations.sort()
|
2019-10-24 09:00:31 +02:00
|
|
|
parameters[widget.name] = configurations.find(config_name)
|
|
|
|
update_configuration(widget_name, config_name)
|
2019-09-25 23:29:06 +02:00
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func update_configuration(widget_name : String, config_name : String) -> void:
|
|
|
|
var widget = get_widget(widget_name)
|
2019-09-25 23:29:06 +02:00
|
|
|
if widget.type == "config_control":
|
|
|
|
var c = []
|
|
|
|
var parent = get_parent()
|
|
|
|
for w in widget.linked_widgets:
|
|
|
|
var g = parent.get_node(w.node)
|
2019-09-26 22:32:49 +02:00
|
|
|
if g != null:
|
|
|
|
var value = MMType.serialize_value(g.parameters[w.widget])
|
|
|
|
c.push_back({ node=w.node, widget=w.widget, value=value })
|
2019-09-25 23:29:06 +02:00
|
|
|
widget.configurations[config_name] = c
|
2019-10-24 09:00:31 +02:00
|
|
|
emit_signal("parameter_changed", "__update_all__", null)
|
2019-09-25 23:29:06 +02:00
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func remove_configuration(widget_name : String, config_name : String) -> void:
|
|
|
|
var widget = get_widget(widget_name)
|
2019-09-25 23:29:06 +02:00
|
|
|
if widget.type == "config_control":
|
|
|
|
widget.configurations.erase(config_name)
|
2019-10-24 09:00:31 +02:00
|
|
|
emit_signal("parameter_changed", "__update_all__", null)
|