2018-09-10 22:43:17 +02:00
|
|
|
extends Control
|
2019-09-24 22:25:46 +02:00
|
|
|
class_name MMNodeLink
|
2018-09-08 12:25:28 +02:00
|
|
|
|
|
|
|
var end
|
|
|
|
var source = null
|
|
|
|
var target = null
|
|
|
|
|
2019-09-24 22:25:46 +02:00
|
|
|
var generator = null
|
2019-10-24 09:00:31 +02:00
|
|
|
var param_name : String = ""
|
|
|
|
var creating : bool = false
|
2018-09-10 22:43:17 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _init(parent) -> void:
|
2019-09-24 22:25:46 +02:00
|
|
|
size_flags_horizontal = SIZE_EXPAND_FILL
|
|
|
|
size_flags_vertical = SIZE_EXPAND_FILL
|
|
|
|
rect_size = parent.rect_size
|
2018-09-10 22:43:17 +02:00
|
|
|
rect_clip_content = true
|
2019-09-24 22:25:46 +02:00
|
|
|
parent.add_child(self)
|
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func pick(s, g, n : String, c : bool = false) -> void:
|
2019-09-24 22:25:46 +02:00
|
|
|
source = s
|
|
|
|
end = get_global_transform().xform_inv(source.get_global_transform().xform(0.5*source.rect_size))
|
|
|
|
generator = g
|
2019-10-24 09:00:31 +02:00
|
|
|
param_name = n
|
2019-09-24 22:25:46 +02:00
|
|
|
creating = c
|
|
|
|
set_process_input(true)
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func show_link(s, t) -> void:
|
2019-09-24 22:25:46 +02:00
|
|
|
set_process_input(false)
|
|
|
|
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
|
|
source = s
|
|
|
|
target = t
|
|
|
|
update()
|
2018-09-10 22:43:17 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func closest(rect, point) -> Vector2:
|
2018-09-08 12:25:28 +02:00
|
|
|
return Vector2(max(rect.position.x, min(rect.end.x, point.x)), max(rect.position.y, min(rect.end.y, point.y)))
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func find_control(gp) -> Dictionary:
|
2019-09-24 22:25:46 +02:00
|
|
|
for c in get_parent().get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
if c.get("controls") != null:
|
|
|
|
for w in c.controls:
|
|
|
|
var widget = c.controls[w]
|
2019-12-04 22:26:54 +01:00
|
|
|
if widget != null and Rect2(widget.rect_global_position, widget.rect_size*widget.get_global_transform().get_scale()).has_point(gp):
|
2019-09-24 22:25:46 +02:00
|
|
|
return { node=c, widget=widget }
|
2019-11-06 23:49:26 +01:00
|
|
|
return {}
|
2019-09-24 22:25:46 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _draw() -> void:
|
2019-09-24 22:25:46 +02:00
|
|
|
var start = get_global_transform().xform_inv(source.get_global_transform().xform(0.5*source.rect_size))
|
2018-09-08 12:25:28 +02:00
|
|
|
var color = Color(1, 0.5, 0.5, 0.5)
|
|
|
|
var rect
|
|
|
|
if target != null:
|
|
|
|
color = Color(0.5, 1, 0.5, 0.5)
|
2019-09-24 22:25:46 +02:00
|
|
|
rect = get_global_transform().xform_inv(target.get_global_transform().xform(Rect2(Vector2(0, 0), target.rect_size)))
|
|
|
|
draw_rect(rect, color, false)
|
2018-09-08 12:25:28 +02:00
|
|
|
end = closest(rect, start)
|
2019-09-24 22:25:46 +02:00
|
|
|
rect = get_global_transform().xform_inv(source.get_global_transform().xform(Rect2(Vector2(0, 0), source.rect_size)))
|
|
|
|
draw_rect(rect, color, false)
|
2018-09-08 12:25:28 +02:00
|
|
|
start = closest(rect, end)
|
2019-09-24 22:25:46 +02:00
|
|
|
draw_line(start, end, color, 1, true)
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _input(event: InputEvent) -> void:
|
2019-09-24 22:25:46 +02:00
|
|
|
if event is InputEventKey:
|
|
|
|
if event.scancode == KEY_ESCAPE:
|
|
|
|
set_process_input(false)
|
|
|
|
queue_free()
|
|
|
|
elif event is InputEventMouseMotion:
|
|
|
|
var control = find_control(event.global_position)
|
|
|
|
end = get_global_transform().xform_inv(event.global_position)
|
2019-12-04 22:26:54 +01:00
|
|
|
target = control.widget if control != null and !control.empty() and generator.can_link_parameter(param_name, control.node.generator, control.widget.name) else null
|
2019-09-24 22:25:46 +02:00
|
|
|
update()
|
|
|
|
elif event is InputEventMouseButton:
|
|
|
|
if event.pressed:
|
|
|
|
if event.button_index == BUTTON_LEFT:
|
|
|
|
var control = find_control(event.global_position)
|
2019-11-06 23:49:26 +01:00
|
|
|
if !control.empty():
|
2019-10-24 09:00:31 +02:00
|
|
|
generator.link_parameter(param_name, control.node.generator, control.widget.name)
|
2019-09-24 22:25:46 +02:00
|
|
|
elif creating:
|
2019-10-24 09:00:31 +02:00
|
|
|
generator.remove_parameter(param_name)
|
2019-09-24 22:25:46 +02:00
|
|
|
set_process_input(false)
|
|
|
|
queue_free()
|
|
|
|
get_tree().set_input_as_handled()
|