Made IOs node editable (can add/remove ports)

This commit is contained in:
RodZill4 2019-10-29 22:10:08 +01:00
parent cd08666123
commit 4c2274104b
8 changed files with 200 additions and 4 deletions

View File

@ -8,9 +8,12 @@ IOs just forward their inputs to their outputs and are used to specify graph int
var ports : Array = []
var editable = false
func can_be_deleted() -> bool:
return name != "gen_inputs" and name != "gen_outputs"
func get_type() -> String:
return "ios"
@ -20,6 +23,7 @@ func get_type_name() -> String:
"gen_outputs": return "Outputs"
_: return "IOs"
func get_io_defs() -> Array:
var rv : Array = []
for p in ports:
@ -32,6 +36,47 @@ func get_input_defs() -> Array:
func get_output_defs() -> Array:
return [] if name == "gen_outputs" else get_io_defs()
func toggle_editable() -> bool:
editable = !editable
if editable:
model = null
return true
func is_editable() -> bool:
return editable
func add_port() -> void:
ports.append({ name="unnamed", type="rgba" })
emit_signal("parameter_changed", "__update_all__", null)
func set_port_name(i : int, n : String) -> void:
ports[i].name = n
func delete_port(i : int) -> void:
ports.remove(i)
var input_gen = get_parent() if name == "gen_inputs" else self
var output_gen = get_parent() if name == "gen_outputs" else self
var port_reconnects = { i:-1 }
while i < ports.size():
port_reconnects[i+1] = i
i += 1
input_gen.get_parent().reconnect_inputs(input_gen, port_reconnects)
output_gen.get_parent().reconnect_outputs(output_gen, port_reconnects)
emit_signal("parameter_changed", "__update_all__", null)
func swap_ports(i1 : int, i2 : int) -> void:
var tmp = ports[i1]
ports[i1] = ports[i2]
ports[i2] = tmp
var input_gen = get_parent() if name == "gen_inputs" else self
var output_gen = get_parent() if name == "gen_outputs" else self
var port_reconnects = { i1:i2, i2:i1 }
input_gen.get_parent().reconnect_inputs(input_gen, port_reconnects)
output_gen.get_parent().reconnect_outputs(output_gen, port_reconnects)
emit_signal("parameter_changed", "__update_all__", null)
func source_changed(input_index : int) -> void:
if name == "gen_outputs":
if get_parent() != null:

View File

@ -15,7 +15,7 @@
viewBox="0 0 64 64"
version="1.1"
id="svg8"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
inkscape:version="0.92.3 (2405546, 2018-03-11)"
sodipodi:docname="icons.svg">
<defs
id="defs2">
@ -48,9 +48,9 @@
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="40.459029"
inkscape:cx="17.695886"
inkscape:cy="7.9897925"
inkscape:zoom="14.304427"
inkscape:cx="23.663017"
inkscape:cy="5.4603811"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="true"
@ -352,5 +352,11 @@
id="path1477"
inkscape:connector-curvature="0"
sodipodi:nodetypes="cccccccc" />
<path
sodipodi:nodetypes="cccccccc"
inkscape:connector-curvature="0"
id="path4561"
d="m 33.500409,335.78733 4.34961,-0.0488 0.123047,-6.82227 h 4.27539 l 0.125,6.82227 4.34961,0.0488 -6.611328,6.8457 z"
style="fill:#ffffff;fill-opacity:0.84322037;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -0,0 +1,41 @@
extends MMGraphNodeBase
func set_generator(g) -> void:
.set_generator(g)
generator.connect("parameter_changed", self, "on_parameter_changed")
update_node()
func on_parameter_changed(p, v) -> void:
if p == "__update_all__":
call_deferred("update_node")
func update_up_down_buttons() -> void:
for c in get_children():
if ! (c is Button):
c.update_up_down_button()
func update_node() -> void:
for c in get_children():
remove_child(c)
c.free()
rect_size = Vector2(0, 0)
title = generator.get_type_name()
var color = Color(0.0, 0.5, 0.0, 0.5)
for p in generator.get_io_defs():
set_slot(get_child_count(), generator.name != "gen_inputs", 0, color, generator.name != "gen_outputs", 0, color)
var port : Control
if generator.is_editable():
port = preload("res://addons/material_maker/nodes/ios/port.tscn").instance()
if p.has("name"):
port.set_label(p.name)
else:
port = Label.new()
port.text = p.name
add_child(port)
if generator.is_editable():
var add_button : Button = preload("res://addons/material_maker/nodes/ios/add.tscn").instance()
add_child(add_button)
add_button.connect("pressed", generator, "add_port")
set_slot(get_child_count()-1, false, 0, color, false, 0, color)
update_up_down_buttons()

View File

@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/material_maker/nodes/ios.gd" type="Script" id=1]
[node name="IOs" type="GraphNode"]
margin_right = 32.0
margin_bottom = 29.0
script = ExtResource( 1 )

View File

@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://addons/material_maker/icons/icons.svg" type="Texture" id=1]
[sub_resource type="AtlasTexture" id=4]
flags = 4
atlas = ExtResource( 1 )
region = Rect2( 50, 1, 12, 14 )
[node name="Add" type="Button"]
margin_left = 16.0
margin_top = 44.0
margin_right = 40.0
margin_bottom = 64.0
size_flags_horizontal = 0
icon = SubResource( 4 )

View File

@ -0,0 +1,23 @@
extends HBoxContainer
func set_label(l : String) -> void:
$Name.set_text(l)
func update_up_down_button() -> void:
var parent = get_parent()
if parent == null:
return
$Up.disabled = (get_index() == 0)
$Down.disabled = (get_index() == get_parent().get_child_count()-2)
func _on_Name_label_changed(new_label):
get_parent().generator.set_port_name(get_index(), new_label)
func _on_Delete_pressed():
get_parent().generator.delete_port(get_index())
func _on_Up_pressed():
get_parent().generator.swap_ports(get_index(), get_index()-1)
func _on_Down_pressed():
get_parent().generator.swap_ports(get_index(), get_index()+1)

View File

@ -0,0 +1,54 @@
[gd_scene load_steps=7 format=2]
[ext_resource path="res://addons/material_maker/nodes/ios/port.gd" type="Script" id=1]
[ext_resource path="res://addons/material_maker/icons/icons.svg" type="Texture" id=2]
[ext_resource path="res://addons/material_maker/widgets/linked_widgets/editable_label.tscn" type="PackedScene" id=3]
[sub_resource type="AtlasTexture" id=1]
flags = 4
atlas = ExtResource( 2 )
region = Rect2( 2, 17, 12, 14 )
[sub_resource type="AtlasTexture" id=2]
flags = 4
atlas = ExtResource( 2 )
region = Rect2( 18, 49, 12, 14 )
[sub_resource type="AtlasTexture" id=3]
flags = 4
atlas = ExtResource( 2 )
region = Rect2( 34, 49, 12, 14 )
[node name="Port" type="HBoxContainer"]
margin_left = 16.0
margin_top = 24.0
margin_right = 130.0
margin_bottom = 44.0
script = ExtResource( 1 )
[node name="Delete" type="Button" parent="."]
margin_right = 24.0
margin_bottom = 20.0
icon = SubResource( 1 )
[node name="Up" type="Button" parent="."]
margin_left = 28.0
margin_right = 52.0
margin_bottom = 20.0
icon = SubResource( 2 )
[node name="Down" type="Button" parent="."]
margin_left = 56.0
margin_right = 80.0
margin_bottom = 20.0
icon = SubResource( 3 )
[node name="Name" parent="." instance=ExtResource( 3 )]
margin_left = 84.0
margin_right = 114.0
margin_bottom = 20.0
rect_min_size = Vector2( 30, 0 )
[connection signal="pressed" from="Delete" to="." method="_on_Delete_pressed"]
[connection signal="pressed" from="Up" to="." method="_on_Up_pressed"]
[connection signal="pressed" from="Down" to="." method="_on_Down_pressed"]
[connection signal="label_changed" from="Name" to="." method="_on_Name_label_changed"]

View File

@ -9,14 +9,17 @@ script = ExtResource( 1 )
[node name="Label" type="Label" parent="."]
margin_top = 13.0
margin_right = 62.0
margin_bottom = 27.0
mouse_filter = 0
size_flags_horizontal = 3
[node name="Editor" type="LineEdit" parent="."]
visible = false
margin_left = 4.0
margin_right = 62.0
margin_bottom = 40.0
size_flags_horizontal = 3
[connection signal="gui_input" from="Label" to="." method="_on_gui_input"]
[connection signal="focus_exited" from="Editor" to="." method="_on_Editor_focus_exited"]
[connection signal="text_entered" from="Editor" to="." method="_on_Editor_text_entered"]