Skip to content

Implement ctrl + rmb drag to cut connections #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions export_presets.cfg

Large diffs are not rendered by default.

Binary file added material_maker/icons/knife.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions material_maker/icons/knife.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bekpys1u4i2vy"
path="res://.godot/imported/knife.png-9be43fe7b9385d6c0d1c865ae71cccfc.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://material_maker/icons/knife.png"
dest_files=["res://.godot/imported/knife.png-9be43fe7b9385d6c0d1c865ae71cccfc.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
64 changes: 60 additions & 4 deletions material_maker/panels/graph_edit/graph_edit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var undoredo_move_node_selection_changed : bool = true
enum ConnectionStyle {DIRECT, BEZIER, ROUNDED, MANHATTAN, DIAGONAL}
var connection_line_style : int = ConnectionStyle.BEZIER

@onready var drag_cut_cursor = preload("res://material_maker/icons/knife.png")
var connections_to_cut : Array[Dictionary]
var drag_cut_line : Line2D = Line2D.new()
var valid_drag_cut_entry: bool = false
const CURSOR_HOT_SPOT : Vector2 = Vector2(2.0, 27.22)

signal save_path_changed
signal graph_changed
signal view_updated
Expand All @@ -53,6 +59,7 @@ func _ready() -> void:
for t in range(41):
add_valid_connection_type(t, 42)
add_valid_connection_type(42, t)
node_popup.about_to_popup.connect(func(): valid_drag_cut_entry = false)

func _exit_tree():
remove_crash_recovery_file()
Expand Down Expand Up @@ -121,6 +128,22 @@ func _gui_input(event) -> void:
accept_event()
node_popup.position = Vector2i(get_screen_transform()*get_local_mouse_position())
node_popup.show_popup()
elif event.is_action_released("ui_cut_drag"):
var conns : Array[Dictionary]
for p in len(drag_cut_line.points) - 1:
var rect : Rect2
rect.position = drag_cut_line.points[p]
rect.end = drag_cut_line.points[p + 1]
conns = get_connections_intersecting_with_rect(rect.abs())
if conns.size():
connections_to_cut.append_array(conns)
if connections_to_cut.size():
on_cut_connections(connections_to_cut)
connections_to_cut.clear()
Input.set_custom_mouse_cursor(null)
drag_cut_line.clear_points()
conns.clear()
queue_redraw()
elif event.is_action_pressed("ui_hierarchy_up"):
on_ButtonUp_pressed()
elif event.is_action_pressed("ui_hierarchy_down"):
Expand All @@ -142,6 +165,7 @@ func _gui_input(event) -> void:
event.control = true
do_zoom(1.0/1.1)
elif event.button_index == MOUSE_BUTTON_RIGHT and event.is_pressed():
valid_drag_cut_entry = true
for c in get_children():
if ! c is GraphNode:
continue
Expand All @@ -161,10 +185,11 @@ func _gui_input(event) -> void:
if c.has_method("on_clicked_output"):
c.on_clicked_output(slot.index, Input.is_key_pressed(KEY_SHIFT))
return
# Only popup the UI library if Ctrl is not pressed to avoid conflicting
# with the Ctrl + Space shortcut.
node_popup.position = Vector2i(get_screen_transform()*get_local_mouse_position())
node_popup.show_popup()
# Only show add node popup if Ctrl is not pressed to
# avoid conflicting with drag-cut shortcut (Ctrl + RMB)
if !event.ctrl_pressed:
node_popup.position = Vector2i(get_screen_transform()*get_local_mouse_position())
node_popup.show_popup()
else:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.double_click:
Expand Down Expand Up @@ -219,13 +244,27 @@ func _gui_input(event) -> void:
if rect.has_point(get_global_mouse_position()):
mm_globals.set_tip_text("Space/#RMB: Nodes menu, Arrow keys: Pan, Mouse wheel: Zoom", 3)

if (event.button_mask & MOUSE_BUTTON_MASK_RIGHT) != 0 and valid_drag_cut_entry:
if event.ctrl_pressed:
Input.set_custom_mouse_cursor(
drag_cut_cursor, Input.CURSOR_ARROW, CURSOR_HOT_SPOT)
drag_cut_line.add_point(get_local_mouse_position())
queue_redraw()
elif drag_cut_line.points.size():
drag_cut_line.add_point(get_local_mouse_position())
queue_redraw()

func get_padded_node_rect(graph_node:GraphNode) -> Rect2:
var rect : Rect2 = graph_node.get_global_rect()
var padding := 8 * graph_node.get_global_transform().get_scale().x
rect.position.x -= padding
rect.size.x += padding*2
return Rect2(rect.position, rect.size)

func _draw() -> void:
if drag_cut_line.points.size() > 1:
draw_polyline(drag_cut_line.points, Color.WHITE, 0.5)


# Misc. useful functions
func get_source(node, port) -> Dictionary:
Expand Down Expand Up @@ -306,6 +345,23 @@ func do_disconnect_node(from : String, from_slot : int, to : String, to_slot : i
return true
return false

func on_cut_connections(connections : Array):
var generator_hier_name : String = generator.get_hier_name()
var conns : Array = []
for c in connections:
var from_gen = get_node(str(c.from_node)).generator
var to_gen = get_node(str(c.to_node)).generator
if do_disconnect_node(c.from_node, c.from_port, c.to_node, c.to_port):
conns.append({from=from_gen.name,from_port=c.from_port, to=to_gen.name, to_port=c.to_port})
var undo_actions = [
{ type="add_to_graph", parent=generator_hier_name, generators=[], connections=conns }
]
var redo_actions = [
{ type="remove_connections", parent=generator_hier_name, connections=conns }
]
undoredo.add("Cut node connections", undo_actions, redo_actions)


func on_disconnect_node(from : String, from_slot : int, to : String, to_slot : int) -> void:
var from_gen = get_node(from).generator
var to_gen = get_node(to).generator
Expand Down
5 changes: 5 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ ui_next_tab={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":true,"meta_pressed":true,"pressed":false,"keycode":16777218,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
ui_cut_drag={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":true,"meta_pressed":false,"button_mask":2,"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}

[locale]

Expand Down