Skip to content

Commit 620b299

Browse files
author
Christian
committed
Initial
0 parents  commit 620b299

7 files changed

+162
-0
lines changed

DragDropController.gd

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
extends Node
2+
3+
var current = null
4+
var drag_offset = Vector2()
5+
6+
var candidates = []
7+
8+
export var drag_group = "draggable"
9+
10+
11+
func _ready():
12+
var draggables = get_tree().get_nodes_in_group(drag_group)
13+
for dragable in draggables:
14+
if dragable is CollisionObject2D:
15+
dragable.connect("mouse_entered",self,"mouse_entered",[dragable])
16+
dragable.connect("mouse_exited",self,"mouse_exited",[dragable])
17+
dragable.connect("input_event",self,"input_event",[dragable])
18+
19+
func _process(delta):
20+
if current is Node2D:
21+
current.global_position = current.get_global_mouse_position() - drag_offset
22+
23+
func mouse_entered(which):
24+
candidates.append(which)
25+
pass
26+
27+
func mouse_exited(which):
28+
candidates.erase(which)
29+
pass
30+
31+
func input_event(viewport: Node, event: InputEvent, shape_idx: int,which:Node2D):
32+
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
33+
if event.is_pressed():
34+
candidates.sort_custom(self,"depth_sort")
35+
var last = candidates.back()
36+
if last:
37+
last.raise()
38+
current = last
39+
drag_offset = current.get_global_mouse_position() - current.global_position
40+
if current.has_method("on_drag_start"):
41+
current.on_drag_start()
42+
else:
43+
var can_drop = true
44+
if current:
45+
if current.has_method("on_drop"):
46+
var on_drop_result = current.on_drop()
47+
can_drop = on_drop_result == null || on_drop_result
48+
if can_drop:
49+
current = null
50+
51+
func depth_sort(a,b):
52+
return b.get_index()<a.get_index()

DragDropController.tscn

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[gd_scene load_steps=2 format=2]
2+
3+
[ext_resource path="res://DragDropController.gd" type="Script" id=1]
4+
5+
[node name="DragDropController" type="Node"]
6+
script = ExtResource( 1 )

LICENSE.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Copyright (c) 2019 Christian Baune.
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Use
2+
3+
1. Add the `DragDropController` node in your scene.
4+
2. In the inspector, fill in Drag Group with the name of the group containing nodes you want to be draggable, by default, the group is named "draggable".
5+
3. (optional) In the node that can be dragged, you can create two methods : `on_drop` and `on_drag_start`
6+
7+
## on_drop
8+
9+
Called when the node is dropped.
10+
Return "false" to prevent drop.
11+
Do not return anything (no `return` in your function) or return `true` to allow drop.
12+
13+
Here is an example of implementation to check if you are dropping in a free space:
14+
15+
```godot
16+
onready var previous_position = position
17+
func on_drop():
18+
position = position.snapped(Vector2(64,64))
19+
yield(get_tree(),"idle_frame")
20+
yield(get_tree(),"idle_frame")
21+
if get_overlapping_areas().size()>0:
22+
print("col!")
23+
position = previous_position
24+
else:
25+
print("no col")
26+
previous_position = position
27+
```
28+
29+
## on_drag_start
30+
31+
Called when the node begin to be dragged. (if multiple nodes are stacked, only the topmost node will be dragged)

icon.png

7.61 KB
Loading

icon.png.import

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://icon.png"
13+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

project.godot

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
14+
}
15+
16+
[application]
17+
18+
config/name="DragDrop"
19+
config/icon="res://icon.png"

0 commit comments

Comments
 (0)