summaryrefslogtreecommitdiff
path: root/Game/Selection/selection_rectangle.gd
blob: 40619ba6ed79c81cba96224dbbb701a0266b145f (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
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) and Client.state is StateDefault

@onready var camera: Camera = get_viewport().get_camera_2d()


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"):
		var areas = get_overlapping_areas()
		for area in areas:
			if "is_selected" in area.get_parent():
				if area.get_parent().owner_id == multiplayer.get_unique_id():
					area.get_parent().is_selected = true
		
		if not areas.is_empty():
			Client.multi_select_finished.emit()
		
		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, 1.25 / max(camera.zoom.x, camera.zoom.y))


func _on_area_entered(area: Area2D) -> void:
	if area.get_parent().owner_id == multiplayer.get_unique_id():
		area.get_parent().is_hovered = true


func _on_area_exited(area: Area2D) -> void:
	if area.get_parent().owner_id == multiplayer.get_unique_id():
		area.get_parent().is_hovered = false