Skip to content

Commit c5c4873

Browse files
committed
DragDropArea: Add drag_started signal
Instead of emitting mouse_down and mouse_up signals, change DragDropArea to detect drag gestures by monitoring mouse input events.
1 parent 756d62b commit c5c4873

File tree

9 files changed

+75
-31
lines changed

9 files changed

+75
-31
lines changed

addons/block_code/ui/blocks/control_block/control_block.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func _ready():
1717
%SnapGutter.custom_minimum_size.x = Constants.CONTROL_MARGIN
1818

1919

20-
func _on_drag_drop_area_mouse_down():
20+
func _on_drag_drop_area_drag_started() -> void:
2121
_drag_started()
2222

2323

addons/block_code/ui/blocks/control_block/control_block.tscn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/control_block/control_block.gd" id="1_2hbir"]
44
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/utilities/background/gutter.gd" id="2_6o8pf"]
5-
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_lpu3c"]
65
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/utilities/background/background.gd" id="2_tx0qr"]
6+
[ext_resource type="PackedScene" uid="uid://c1l0mpkvmvwfd" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="3_21e8n"]
77
[ext_resource type="PackedScene" uid="uid://b1oge52xhjqnu" path="res://addons/block_code/ui/blocks/utilities/snap_point/snap_point.tscn" id="3_nhryi"]
88
[ext_resource type="PackedScene" uid="uid://b1xvp3u11h41s" path="res://addons/block_code/ui/blocks/utilities/template_editor/template_editor.tscn" id="4_6uktl"]
99

@@ -48,7 +48,7 @@ script = ExtResource("2_tx0qr")
4848
color = Color(1, 1, 1, 1)
4949
shift_bottom = 20.0
5050

51-
[node name="DragDropArea" parent="VBoxContainer/MarginContainer/Rows/Row" instance=ExtResource("2_lpu3c")]
51+
[node name="DragDropArea" parent="VBoxContainer/MarginContainer/Rows/Row" instance=ExtResource("3_21e8n")]
5252
layout_mode = 2
5353

5454
[node name="RowHBoxContainer" type="MarginContainer" parent="VBoxContainer/MarginContainer/Rows/Row"]
@@ -95,4 +95,4 @@ shift_top = 20.0
9595
[node name="SnapPoint" parent="VBoxContainer" instance=ExtResource("3_nhryi")]
9696
layout_mode = 2
9797

98-
[connection signal="mouse_down" from="VBoxContainer/MarginContainer/Rows/Row/DragDropArea" to="." method="_on_drag_drop_area_mouse_down"]
98+
[connection signal="drag_started" from="VBoxContainer/MarginContainer/Rows/Row/DragDropArea" to="." method="_on_drag_drop_area_drag_started"]

addons/block_code/ui/blocks/entry_block/entry_block.tscn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/entry_block/entry_block.gd" id="2_3ik8h"]
44
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/utilities/background/background.gd" id="2_yrw8l"]
5-
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="3_v0qw8"]
5+
[ext_resource type="PackedScene" uid="uid://c1l0mpkvmvwfd" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="3_swkpp"]
66
[ext_resource type="PackedScene" uid="uid://b1xvp3u11h41s" path="res://addons/block_code/ui/blocks/utilities/template_editor/template_editor.tscn" id="4_1gwsm"]
77
[ext_resource type="PackedScene" uid="uid://b1oge52xhjqnu" path="res://addons/block_code/ui/blocks/utilities/snap_point/snap_point.tscn" id="4_yj206"]
88

@@ -37,7 +37,7 @@ script = ExtResource("2_yrw8l")
3737
color = Color(1, 1, 1, 1)
3838
show_top = false
3939

40-
[node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_v0qw8")]
40+
[node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_swkpp")]
4141
layout_mode = 2
4242

4343
[node name="MarginContainer" type="MarginContainer" parent="VBoxContainer/TopMarginContainer"]
@@ -55,4 +55,4 @@ layout_mode = 2
5555
[node name="SnapPoint" parent="VBoxContainer" instance=ExtResource("4_yj206")]
5656
layout_mode = 2
5757

58-
[connection signal="mouse_down" from="VBoxContainer/TopMarginContainer/DragDropArea" to="." method="_on_drag_drop_area_mouse_down"]
58+
[connection signal="drag_started" from="VBoxContainer/TopMarginContainer/DragDropArea" to="." method="_on_drag_drop_area_drag_started"]

addons/block_code/ui/blocks/parameter_block/parameter_block.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func _ready():
3030
_panel.add_theme_stylebox_override("panel", _panel_normal)
3131

3232

33-
func _on_drag_drop_area_mouse_down():
33+
func _on_drag_drop_area_drag_started() -> void:
3434
_drag_started()
3535

3636

addons/block_code/ui/blocks/parameter_block/parameter_block.tscn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[gd_scene load_steps=5 format=3 uid="uid://clipm2dd28jde"]
22

33
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/parameter_block/parameter_block.gd" id="1_0hajy"]
4-
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_gy5co"]
4+
[ext_resource type="PackedScene" uid="uid://c1l0mpkvmvwfd" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_0eadx"]
55
[ext_resource type="PackedScene" uid="uid://b1xvp3u11h41s" path="res://addons/block_code/ui/blocks/utilities/template_editor/template_editor.tscn" id="3_shl1a"]
66

77
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_dbera"]
@@ -29,7 +29,7 @@ unique_name_in_owner = true
2929
layout_mode = 2
3030
theme_override_styles/panel = SubResource("StyleBoxFlat_dbera")
3131

32-
[node name="DragDropArea" parent="." instance=ExtResource("2_gy5co")]
32+
[node name="DragDropArea" parent="." instance=ExtResource("2_0eadx")]
3333
layout_mode = 2
3434

3535
[node name="MarginContainer" type="MarginContainer" parent="."]
@@ -47,4 +47,4 @@ theme_override_constants/margin_bottom = 8
4747

4848
[connection signal="focus_entered" from="." to="." method="_on_focus_entered"]
4949
[connection signal="focus_exited" from="." to="." method="_on_focus_exited"]
50-
[connection signal="mouse_down" from="DragDropArea" to="." method="_on_drag_drop_area_mouse_down"]
50+
[connection signal="drag_started" from="DragDropArea" to="." method="_on_drag_drop_area_drag_started"]

addons/block_code/ui/blocks/statement_block/statement_block.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func _ready():
1818
_background.color = color
1919

2020

21-
func _on_drag_drop_area_mouse_down():
21+
func _on_drag_drop_area_drag_started() -> void:
2222
_drag_started()
2323

2424

addons/block_code/ui/blocks/statement_block/statement_block.tscn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/statement_block/statement_block.gd" id="1_6wvlf"]
44
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/utilities/background/background.gd" id="2_lctqt"]
5-
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_owgdx"]
65
[ext_resource type="PackedScene" uid="uid://b1oge52xhjqnu" path="res://addons/block_code/ui/blocks/utilities/snap_point/snap_point.tscn" id="3_5vaov"]
6+
[ext_resource type="PackedScene" uid="uid://c1l0mpkvmvwfd" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="3_mbxhq"]
77
[ext_resource type="PackedScene" uid="uid://b1xvp3u11h41s" path="res://addons/block_code/ui/blocks/utilities/template_editor/template_editor.tscn" id="4_vky23"]
88

99
[node name="StatementBlock" type="MarginContainer" node_paths=PackedStringArray("bottom_snap", "template_editor")]
@@ -36,7 +36,7 @@ mouse_filter = 1
3636
script = ExtResource("2_lctqt")
3737
color = Color(1, 1, 1, 1)
3838

39-
[node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("2_owgdx")]
39+
[node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_mbxhq")]
4040
layout_mode = 2
4141

4242
[node name="MarginContainer" type="MarginContainer" parent="VBoxContainer/TopMarginContainer"]
@@ -54,4 +54,4 @@ layout_mode = 2
5454
[node name="SnapPoint" parent="VBoxContainer" instance=ExtResource("3_5vaov")]
5555
layout_mode = 2
5656

57-
[connection signal="mouse_down" from="VBoxContainer/TopMarginContainer/DragDropArea" to="." method="_on_drag_drop_area_mouse_down"]
57+
[connection signal="drag_started" from="VBoxContainer/TopMarginContainer/DragDropArea" to="." method="_on_drag_drop_area_drag_started"]
Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
11
@tool
2-
extends MarginContainer
2+
## Drag drop area.
3+
##
4+
## A Control which watches for click and drag gestures beginning from itself.
5+
## It propagates events up to its parent, so it is possible to place this
6+
## control inside a control which processes input events such as [LineEdit].
7+
## If a drag occurs, it emits [signal drag_started].
8+
extends Control
39

4-
signal mouse_down
5-
signal mouse_up
10+
const Constants = preload("res://addons/block_code/ui/constants.gd")
611

12+
signal drag_started
713

8-
func _on_gui_input(event):
9-
if event is InputEventMouseButton and get_global_rect().has_point(event.global_position):
10-
var mouse_event: InputEventMouseButton = event as InputEventMouseButton
11-
if mouse_event.button_index == MOUSE_BUTTON_LEFT and mouse_event.pressed:
12-
mouse_down.emit()
13-
get_viewport().set_input_as_handled()
14-
if mouse_event.button_index == MOUSE_BUTTON_LEFT and not mouse_event.pressed:
15-
mouse_up.emit()
16-
get_viewport().set_input_as_handled()
14+
## True to require that the mouse move outside of the component before
15+
## [signal drag_started] is emitted.
16+
@export var drag_outside: bool = false
17+
18+
var _drag_start_position: Vector2 = Vector2.INF
19+
20+
21+
func _gui_input(event: InputEvent) -> void:
22+
# Watch for mouse clicks using _gui_input, so events are filtered based on
23+
# rules of the GUI system.
24+
25+
if not event is InputEventMouseButton:
26+
return
27+
28+
var button_event: InputEventMouseButton = event as InputEventMouseButton
29+
30+
if button_event.button_index != MOUSE_BUTTON_LEFT:
31+
return
32+
33+
if button_event.double_click:
34+
# Double click event (with the mouse released) has both pressed=true
35+
# and double_click=true, so ignore it as a special case.
36+
pass
37+
elif button_event.pressed:
38+
# Keep track of where the mouse click originated, but allow this
39+
# event to propagate to other nodes.
40+
_drag_start_position = event.global_position
41+
else:
42+
_drag_start_position = Vector2.INF
43+
44+
45+
func _input(event: InputEvent) -> void:
46+
# Watch for mouse movements using _input. This way, we receive mouse
47+
# movement events that occur outside of the component.
48+
49+
if not event is InputEventMouseMotion:
50+
return
51+
52+
if _drag_start_position == Vector2.INF:
53+
return
54+
55+
var motion_event: InputEventMouseMotion = event as InputEventMouseMotion
56+
57+
if not (drag_outside and get_global_rect().has_point(event.global_position)) and _drag_start_position.distance_to(event.global_position) > Constants.MINIMUM_DRAG_THRESHOLD:
58+
get_viewport().set_input_as_handled()
59+
drag_started.emit()
60+
_drag_start_position = Vector2.INF
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
[gd_scene load_steps=2 format=3 uid="uid://c7puyxpqcq6xo"]
1+
[gd_scene load_steps=2 format=3 uid="uid://c1l0mpkvmvwfd"]
22

33
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.gd" id="1_5vdxp"]
44

5-
[node name="DragDropArea" type="MarginContainer"]
5+
[node name="DragDropArea" type="Control"]
6+
layout_mode = 3
67
anchors_preset = 15
78
anchor_right = 1.0
89
anchor_bottom = 1.0
910
grow_horizontal = 2
1011
grow_vertical = 2
12+
mouse_filter = 1
1113
mouse_default_cursor_shape = 2
1214
script = ExtResource("1_5vdxp")
13-
14-
[connection signal="gui_input" from="." to="." method="_on_gui_input"]

0 commit comments

Comments
 (0)