diff options
Diffstat (limited to 'Game/Selection')
-rw-r--r-- | Game/Selection/MultiSelectArea.tscn | 13 | ||||
-rw-r--r-- | Game/Selection/SelectableArea.tscn | 14 | ||||
-rw-r--r-- | Game/Selection/SelectionRectangle.tscn | 20 | ||||
-rw-r--r-- | Game/Selection/multi_select_area.gd | 19 | ||||
-rw-r--r-- | Game/Selection/selectable_area.gd | 29 | ||||
-rw-r--r-- | Game/Selection/selection_rectangle.gd | 51 |
6 files changed, 146 insertions, 0 deletions
diff --git a/Game/Selection/MultiSelectArea.tscn b/Game/Selection/MultiSelectArea.tscn new file mode 100644 index 0000000..9e6bd43 --- /dev/null +++ b/Game/Selection/MultiSelectArea.tscn @@ -0,0 +1,13 @@ +[gd_scene load_steps=3 format=3 uid="uid://bmi8eb80wghjs"] + +[ext_resource type="Script" path="res://Game/Selection/multi_select_area.gd" id="1_g76x3"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_8io84"] + +[node name="MultiSelectArea" type="Area2D"] +collision_layer = 0 +collision_mask = 0 +script = ExtResource("1_g76x3") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_8io84") diff --git a/Game/Selection/SelectableArea.tscn b/Game/Selection/SelectableArea.tscn new file mode 100644 index 0000000..3f64dec --- /dev/null +++ b/Game/Selection/SelectableArea.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=2 format=3 uid="uid://cqktpc8c7ecn3"] + +[ext_resource type="Script" path="res://Game/Selection/selectable_area.gd" id="1_8w2y0"] + +[node name="SelectableArea" type="Area2D"] +collision_layer = 32 +collision_mask = 16 +script = ExtResource("1_8w2y0") + +[connection signal="area_entered" from="." to="." method="_on_area_entered"] +[connection signal="area_exited" from="." to="." method="_on_area_exited"] +[connection signal="input_event" from="." to="." method="_on_input_event"] +[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"] +[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"] diff --git a/Game/Selection/SelectionRectangle.tscn b/Game/Selection/SelectionRectangle.tscn new file mode 100644 index 0000000..61517d0 --- /dev/null +++ b/Game/Selection/SelectionRectangle.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=3 format=3 uid="uid://ic2hc7gr27p3"] + +[ext_resource type="Script" path="res://Game/Selection/selection_rectangle.gd" id="1_on0pa"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_nq6xv"] +size = Vector2(1, 1) + +[node name="SelectionRectangle" type="Area2D" groups=["selection_rectangle"]] +z_index = 1 +collision_layer = 16 +collision_mask = 32 +script = ExtResource("1_on0pa") +color_background = Color(0.0823529, 0.392157, 0.203922, 0.392157) +color_border = Color(0.0823529, 1, 0.203922, 0.501961) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_nq6xv") + +[connection signal="area_entered" from="." to="." method="_on_area_entered"] +[connection signal="area_exited" from="." to="." method="_on_area_exited"] diff --git a/Game/Selection/multi_select_area.gd b/Game/Selection/multi_select_area.gd new file mode 100644 index 0000000..9b2e8ce --- /dev/null +++ b/Game/Selection/multi_select_area.gd @@ -0,0 +1,19 @@ +extends Area2D + + +signal select(nodes: Array) + + +func _ready() -> void: + var camera_rect = Client.stage.get_node("Camera").get_rect() + + global_position = camera_rect.position + camera_rect.size / 2 + + $CollisionShape2D.shape.size = camera_rect.size + + +func _process(_delta: float) -> void: + var nodes = get_overlapping_areas() + get_overlapping_bodies() + if nodes.size() > 0: + select.emit(nodes) + queue_free() diff --git a/Game/Selection/selectable_area.gd b/Game/Selection/selectable_area.gd new file mode 100644 index 0000000..59d4be7 --- /dev/null +++ b/Game/Selection/selectable_area.gd @@ -0,0 +1,29 @@ +extends Area2D + + +signal hover_enter +signal hover_exit +signal select(event: InputEvent) + + +func _on_area_entered(_area: Area2D) -> void: + hover_enter.emit() + + +func _on_area_exited(_area: Area2D) -> void: + hover_exit.emit() + + +func _on_mouse_entered() -> void: + if not get_tree().get_first_node_in_group("selection_rectangle").is_active: + hover_enter.emit() + + +func _on_mouse_exited() -> void: + if not get_tree().get_first_node_in_group("selection_rectangle").is_active: + hover_exit.emit() + + +func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void: + if event.is_action_pressed("select"): + select.emit(event) diff --git a/Game/Selection/selection_rectangle.gd b/Game/Selection/selection_rectangle.gd new file mode 100644 index 0000000..a57f25b --- /dev/null +++ b/Game/Selection/selection_rectangle.gd @@ -0,0 +1,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 |