summaryrefslogtreecommitdiff
path: root/Game/Selection/selection_rectangle.gd
blob: a57f25b2122b99978d01580c185497cee84b4d0a (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
extends Area2D


@export_group("Color", "color")
@export var color_background: Color
@export var color_border: Color

var anchor: Vector2 = Vector2.ZERO
var size: Vector2 = Vector2.ZERO :
	set(value):
		size = value
		$CollisionShape2D.position = size / 2
		$CollisionShape2D.shape.size = abs(size)

var is_active: bool :
	get():
		return abs(size) > Vector2(1, 1)


func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("select"):
		anchor = get_global_mouse_position()
		global_position = anchor
	
	if Input.is_action_pressed("select"):
		size = get_global_mouse_position() - anchor
		if is_active:
			queue_redraw()
	
	if is_active and Input.is_action_just_released("select"):
		for area in get_overlapping_areas():
			if "is_selected" in area.get_parent():
				area.get_parent().is_selected = true
		
		size = Vector2.ZERO
		queue_redraw()


func _draw():
	if is_active:
		var rect = Rect2(Vector2.ZERO, size)
		draw_rect(rect, color_background)
		draw_rect(rect, color_border, false, 2.0)


func _on_area_entered(area: Area2D) -> void:
	area.get_parent().is_hovered = true


func _on_area_exited(area: Area2D) -> void:
	area.get_parent().is_hovered = false