Skip to content

Commit 9b4a4c5

Browse files
committed
Move attach/alt-detach to their own functions
1 parent 4e02b37 commit 9b4a4c5

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

material_maker/panels/graph_edit/graph_edit.gd

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -249,47 +249,58 @@ func _gui_input(event) -> void:
249249
if rect.has_point(get_global_mouse_position()):
250250
mm_globals.set_tip_text("Space/#RMB: Nodes menu, Arrow keys: Pan, Mouse wheel: Zoom", 3)
251251

252-
# Detach node from connection (alt pressed while node is dragged)
253-
if (event.alt_pressed and event.button_mask & MOUSE_BUTTON_MASK_LEFT != 0
254-
and get_selected_nodes().size() and event.relative.length() > 0.0):
255-
remove_with_reconnections(true)
256-
target_drop_connection.clear()
257-
target_drop_node = null
252+
handle_node_detach(event)
253+
handle_node_drop(event)
254+
258255

259-
# Attach node to connection while node is dragged over a connection
260-
if (get_selected_nodes().size() == 1
261-
and event.button_mask & MOUSE_BUTTON_MASK_LEFT != 0
262-
and event.relative.length() > 0.0):
263-
hint_node_drop_allowed(not event.alt_pressed)
264-
var node : GraphElement = get_selected_nodes()[0]
265-
if node is not GraphNode:
256+
## Detaches node(s) from connections if alt is held down
257+
## while dragging a node.
258+
## [br][br]Unsets [param target_drop_connection] and [param target_drop_node]
259+
func handle_node_detach(event: InputEventMouseMotion) -> void:
260+
if (event.alt_pressed and event.button_mask & MOUSE_BUTTON_MASK_LEFT != 0
261+
and get_selected_nodes().size() and event.relative.length() > 0.0):
262+
remove_with_reconnections(true)
263+
target_drop_connection.clear()
264+
target_drop_node = null
265+
266+
267+
## Checks whether a node can be dropped onto a connection (Alt key blocks the action)
268+
## and sets [param target_drop_connection] and [param target_drop_node]
269+
## [br][br]Node connections are performed by [method drop_node_on_connection]
270+
func handle_node_drop(event: InputEventMouseMotion) -> void:
271+
if (get_selected_nodes().size() == 1
272+
and event.button_mask & MOUSE_BUTTON_MASK_LEFT != 0
273+
and event.relative.length() > 0.0):
274+
hint_node_drop_allowed(not event.alt_pressed)
275+
var node : GraphElement = get_selected_nodes()[0]
276+
if node is not GraphNode:
277+
return
278+
for c in get_connection_list():
279+
if node.name in c.values():
266280
return
267-
for c in get_connection_list():
268-
if node.name in c.values():
269-
return
270-
var active_conn : Dictionary
271-
var node_rect : Rect2 = node.get_rect()
272-
if node is MMGraphReroute:
273-
node_rect.size.y = node_rect.size.x
274-
node_rect.position.y += node_rect.size.y - 5.0
275-
var conns := get_connections_intersecting_with_rect(node_rect)
276-
if not conns.is_empty():
277-
if conns.size() > 1:
278-
conns.sort_custom(compare_connection_by_port_position_y)
279-
active_conn = conns.front()
280-
highlight_connection(active_conn)
281-
if not event.alt_pressed:
282-
target_drop_connection = active_conn
283-
target_drop_node = node
284-
else:
285-
target_drop_connection.clear()
286-
target_drop_node = null
287-
for c in get_connection_list():
288-
if c != active_conn:
289-
highlight_connection(c, 0.0)
281+
var active_conn : Dictionary
282+
var node_rect : Rect2 = node.get_rect()
283+
if node is MMGraphReroute:
284+
node_rect.size.y = node_rect.size.x
285+
node_rect.position.y += node_rect.size.y - 5.0
286+
var conns := get_connections_intersecting_with_rect(node_rect)
287+
if not conns.is_empty():
288+
if conns.size() > 1:
289+
conns.sort_custom(compare_connection_by_port_position_y)
290+
active_conn = conns.front()
291+
highlight_connection(active_conn)
292+
if not event.alt_pressed:
293+
target_drop_connection = active_conn
294+
target_drop_node = node
290295
else:
291-
remove_theme_color_override("activity")
292-
remove_theme_color_override("connection_hover_tint_color")
296+
target_drop_connection.clear()
297+
target_drop_node = null
298+
for c in get_connection_list():
299+
if c != active_conn:
300+
highlight_connection(c, 0.0)
301+
else:
302+
remove_theme_color_override("activity")
303+
remove_theme_color_override("connection_hover_tint_color")
293304

294305

295306
func compare_connection_by_port_position_y(a, b) -> bool:
@@ -888,6 +899,9 @@ func duplicate_selected() -> void:
888899
func duplicate_selected_with_inputs() -> void:
889900
do_paste(serialize_selection([], true))
890901

902+
903+
## Detaches a node from an existing connection.
904+
## Nodes are kept if [param keep_nodes] is set
891905
func remove_with_reconnections(keep_nodes: bool = false) -> void:
892906
var selection := get_selected_nodes()
893907
var from_node : GraphNode

0 commit comments

Comments
 (0)