|
| 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 |
0 commit comments