Skip to content

Commit 04f330c

Browse files
committed
Animated highlighted connections
1 parent bbc8d1d commit 04f330c

7 files changed

Lines changed: 135 additions & 18 deletions

File tree

export_presets.cfg

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

material_maker/nodes/base.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ func _draw() -> void:
241241
if control.get("modulate"):
242242
control.modulate.a = opacity
243243

244+
get_parent().get_node("HighlightsOverlay").update_connections()
245+
246+
244247
func draw_portgroup_stylebox(first_port : Vector2, last_port : Vector2) -> void:
245248
var stylebox_position: Vector2 = first_port + Vector2(-0.5,-0.5) * portgroup_width
246249
var stylebox_size: Vector2 = Vector2(portgroup_width, last_port.y - first_port.y + portgroup_width)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://dv1bc32kluxlg"]
2+
3+
[sub_resource type="Shader" id="Shader_pewh7"]
4+
code = "shader_type canvas_item;
5+
6+
void fragment() {
7+
COLOR = vec4(vec3(0.0), 0.25 * sin(UV.x - TIME * TAU) + 0.25);
8+
}"
9+
10+
[resource]
11+
shader = SubResource("Shader_pewh7")

material_maker/panels/graph_edit/graph_edit.gd

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ func do_disconnect_node(from : String, from_slot : int, to : String, to_slot : i
428428
return false
429429

430430
func on_cut_connections(connections_to_cut : Array):
431+
queue_redraw()
431432
var generator_hier_name : String = generator.get_hier_name()
432433
var conns : Array = []
433434
for c in connections_to_cut:
@@ -1025,18 +1026,6 @@ func _on_ButtonTransmitsSeed_toggled(button_pressed) -> void:
10251026

10261027
# Node selection
10271028

1028-
var highlighting_connections : bool = false
1029-
1030-
func highlight_connections() -> void:
1031-
if highlighting_connections:
1032-
return
1033-
highlighting_connections = true
1034-
while Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
1035-
await get_tree().process_frame
1036-
for c in get_connection_list():
1037-
set_connection_activity(c.from_node, c.from_port, c.to_node, c.to_port, 1.0 if get_node(NodePath(c.from_node)).selected or get_node(NodePath(c.to_node)).selected else 0.0)
1038-
highlighting_connections = false
1039-
10401029
func _on_GraphEdit_node_selected(node : GraphElement) -> void:
10411030
if node is MMGraphComment:
10421031
print("Selecting enclosed nodes...")
@@ -1047,7 +1036,6 @@ func _on_GraphEdit_node_selected(node : GraphElement) -> void:
10471036
elif node is MMGraphCommentLine:
10481037
pass
10491038
else:
1050-
highlight_connections()
10511039
await get_tree().process_frame
10521040
if current_preview[0] != null:
10531041
for n in get_selected_nodes():
@@ -1066,7 +1054,6 @@ func _on_GraphEdit_node_selected(node : GraphElement) -> void:
10661054
mm_globals.main_window.update_menus()
10671055

10681056
func _on_GraphEdit_node_unselected(_node):
1069-
highlight_connections()
10701057
undoredo_move_node_selection_changed = true
10711058
mm_globals.main_window.update_menus()
10721059

material_maker/panels/graph_edit/graph_edit.tscn

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
[gd_scene load_steps=8 format=3 uid="uid://dy1u50we7gtru"]
1+
[gd_scene load_steps=9 format=3 uid="uid://dy1u50we7gtru"]
22

33
[ext_resource type="Script" uid="uid://dkp4w3at1o6cm" path="res://material_maker/panels/graph_edit/graph_edit.gd" id="1"]
44
[ext_resource type="Texture2D" uid="uid://c0j4px4n72di5" path="res://material_maker/icons/icons.tres" id="2"]
55
[ext_resource type="Script" uid="uid://bne3k0g56crmy" path="res://material_maker/tools/undo_redo/undo_redo.gd" id="3"]
66
[ext_resource type="PackedScene" uid="uid://buj231c2gxm4o" path="res://material_maker/widgets/desc_button/desc_button.tscn" id="4"]
7+
[ext_resource type="Script" uid="uid://dlah77kjy12l6" path="res://material_maker/panels/graph_edit/highlights_overlay.gd" id="5_u5byk"]
78

89
[sub_resource type="AtlasTexture" id="3"]
910
atlas = ExtResource("2")
@@ -85,6 +86,12 @@ layout_mode = 2
8586
[node name="UndoRedo" type="Node" parent="."]
8687
script = ExtResource("3")
8788

89+
[node name="HighlightsOverlay" type="Control" parent="."]
90+
anchors_preset = 0
91+
offset_right = 40.0
92+
offset_bottom = 40.0
93+
script = ExtResource("5_u5byk")
94+
8895
[connection signal="connection_from_empty" from="." to="." method="request_popup" binds= [true]]
8996
[connection signal="connection_request" from="." to="." method="on_connect_node"]
9097
[connection signal="connection_to_empty" from="." to="." method="request_popup" binds= [false]]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
extends Control
2+
3+
class_name HighlightsOverlay
4+
5+
6+
class AnimatedConnection extends Line2D:
7+
var connection : Dictionary
8+
9+
@onready var graph : MMGraphEdit = owner
10+
11+
var active_connections : Array[Dictionary]
12+
var is_updating_connections := false
13+
14+
const LINE_MATERIAL := preload("res://material_maker/panels/graph_edit/animated_connection.tres")
15+
16+
func _ready() -> void:
17+
graph.child_order_changed.connect(_move_above_connections.call_deferred)
18+
graph.get_node("_connection_layer").draw.connect(queue_redraw)
19+
graph.get_node("_connection_layer").child_order_changed.connect(update_connections)
20+
graph.node_selected.connect(update_connections.unbind(1))
21+
graph.node_deselected.connect(update_connections.unbind(1))
22+
graph.connection_drag_started.connect(
23+
func(_node, _port, _is_output): update_connections(true))
24+
25+
26+
func _draw() -> void:
27+
if get_children().is_empty():
28+
return
29+
30+
for line : AnimatedConnection in get_children():
31+
var conn : Dictionary = line.connection
32+
var positions := get_connection_positions(conn)
33+
if positions.is_empty():
34+
continue
35+
36+
line.visible = get_viewport_rect().intersects(
37+
Rect2(positions.from, positions.to - positions.from).abs())
38+
39+
line.points = graph._get_connection_line(positions.from, positions.to)
40+
line.width = graph.connection_lines_thickness
41+
42+
43+
func _move_above_connections() -> void:
44+
graph.move_child(self, graph.get_node("_connection_layer").get_index() + 1)
45+
46+
47+
func update_connections(should_hide_when_updating : bool = false) -> void:
48+
if is_updating_connections:
49+
return
50+
is_updating_connections = true
51+
52+
if should_hide_when_updating:
53+
hide()
54+
55+
while Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
56+
await get_tree().process_frame
57+
58+
active_connections.clear()
59+
for c in graph.get_connection_list():
60+
if (graph.get_node(NodePath(c.from_node)).selected
61+
or graph.get_node(NodePath(c.to_node)).selected):
62+
active_connections.append(c)
63+
await get_tree().process_frame
64+
create_animated_connections()
65+
66+
# Remove inactive/invalid animated conections
67+
if get_children().size():
68+
for anim_line : AnimatedConnection in get_children():
69+
if (anim_line.connection not in active_connections
70+
or get_connection_positions(anim_line.connection).is_empty()):
71+
remove_child(anim_line)
72+
anim_line.free()
73+
74+
queue_redraw()
75+
is_updating_connections = false
76+
show.call_deferred()
77+
78+
79+
func create_animated_connections() -> void:
80+
if active_connections.size():
81+
for active_connection in active_connections:
82+
var positions := get_connection_positions(active_connection)
83+
if positions.is_empty():
84+
continue
85+
86+
if not get_children().any(func(ac : AnimatedConnection):
87+
return ac.connection == active_connection):
88+
var ac := AnimatedConnection.new()
89+
ac.connection = active_connection
90+
ac.texture_mode = Line2D.LINE_TEXTURE_TILE
91+
ac.material = LINE_MATERIAL
92+
ac.points = graph._get_connection_line(positions.from, positions.to)
93+
ac.width = graph.connection_lines_thickness
94+
add_child(ac)
95+
96+
97+
func get_connection_positions(from_connection : Dictionary) -> Dictionary:
98+
var c := from_connection
99+
var positions : Dictionary
100+
if graph.has_node(NodePath(c.from_node)) and graph.has_node(NodePath(c.to_node)):
101+
var from_node : GraphNode = graph.get_node(NodePath(c.from_node))
102+
var to_node : GraphNode = graph.get_node(NodePath(c.to_node))
103+
if from_node and to_node:
104+
var from_pos := from_node.get_output_port_position(c.from_port)
105+
var to_pos := to_node.get_input_port_position(c.to_port)
106+
positions.from = from_pos * graph.zoom + from_node.position
107+
positions.to = to_pos * graph.zoom + to_node.position
108+
return positions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://dlah77kjy12l6

0 commit comments

Comments
 (0)