summaryrefslogtreecommitdiff
path: root/Objects/BuilderObject.gd
blob: 91465bcb102ceffe0b4ee1efbe20b7f85a1bfbd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class_name BuilderObject
extends Node2D


var is_colliding = false
var is_dragging = false
var is_rotating = false

var mouse_offset = Vector2(0,0)
var last_position = null

var rotation_offset_angle = 0
var last_rotation = 0

# todo: if is focused via click, show rotation picker in edge

func _ready():
	$CollisionShape.texture.width = $Area2D/CollisionShape2D.shape.size.x


func _process(_delta):
	if is_dragging:
		position = get_global_mouse_position() + mouse_offset
	
	if is_rotating:
		var distance = position - get_global_mouse_position()
		rotation = atan2(distance.y, distance.x) - rotation_offset_angle


func _on_area_2d_input_event(_viewport, event: InputEvent, _shape_idx):
	if event.is_action_pressed("drag_start"):
		drag_start()
	if event.is_action_released("drag_start"):
		drag_end()


func drag_start():
	if get_viewport():
		mouse_offset = position - get_global_mouse_position()
		last_position = position
		
	is_dragging = true

func drag_end():
	if is_colliding:
		if last_position:
			position = last_position
		else:
			queue_free()
		
	is_dragging = false


func _on_rotate_gui_input(event: InputEvent):
	get_viewport().set_input_as_handled()
	
	if event.is_action_pressed("rotate_start"):
		rotate_start()
	if event.is_action_released("rotate_start"):
		rotate_end()


func rotate_start():
	var distance = position - get_global_mouse_position()
	rotation_offset_angle = atan2(distance.y, distance.x) - rotation
	last_rotation = rotation
	is_rotating = true

func rotate_end():
	if is_colliding:
		rotation = last_rotation
	
	is_rotating = false


func _on_area_2d_area_entered(_area):
	is_colliding = true
	modulate = Color("bb5a4b")


func _on_area_2d_area_exited(_area):
	#if $Area2D.get_overlapping_areas().size() == 0:
	is_colliding = false
	modulate = Color("fff")