├── DragDropController.gd ├── DragDropController.tscn ├── LICENSE.txt ├── README.md ├── icon.png ├── icon.png.import └── project.godot /DragDropController.gd: -------------------------------------------------------------------------------- 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()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) 32 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/programaths/dragdrop/620b2995b131d82359c039d27c862910be1dadba/icon.png -------------------------------------------------------------------------------- /icon.png.import: -------------------------------------------------------------------------------- 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 35 | -------------------------------------------------------------------------------- /project.godot: -------------------------------------------------------------------------------- 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" 20 | --------------------------------------------------------------------------------