Skip to content

Commit

Permalink
Split 'SelectionHandler' into 'Areal' and 'DoubleClick' selection han…
Browse files Browse the repository at this point in the history
…dlers
  • Loading branch information
Scony committed Jan 6, 2024
1 parent 06dfd98 commit fca7c56
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 48 deletions.
9 changes: 6 additions & 3 deletions source/match/Match.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=43 format=3 uid="uid://camns8fqod8d4"]
[gd_scene load_steps=44 format=3 uid="uid://camns8fqod8d4"]

[ext_resource type="Script" path="res://source/match/Match.gd" id="1_1555u"]
[ext_resource type="Script" path="res://source/match/IsometricCamera3D.gd" id="1_qb2ry"]
Expand All @@ -20,11 +20,12 @@
[ext_resource type="Script" path="res://source/match/hud/Minimap.gd" id="17_0bfte"]
[ext_resource type="Script" path="res://source/match/TerrainNavigation.gd" id="17_6fprk"]
[ext_resource type="Shader" path="res://source/shaders/2d/white_transparent.gdshader" id="19_m1b2v"]
[ext_resource type="PackedScene" uid="uid://bocb7hjilvri5" path="res://source/match/handlers/SelectionHandler.tscn" id="20_tw1wp"]
[ext_resource type="PackedScene" uid="uid://c0uxy26e4qs4w" path="res://source/match/handlers/MouseClickAnimationsHandler.tscn" id="22_438pb"]
[ext_resource type="PackedScene" uid="uid://1rby73ckmk7b" path="res://source/match/Menu.tscn" id="22_ofmlu"]
[ext_resource type="PackedScene" uid="uid://b83l4dny3gly0" path="res://source/match/debug/FrameIncrementer.tscn" id="23_exhtd"]
[ext_resource type="PackedScene" uid="uid://c3xjdp2yrr1fu" path="res://source/match/debug/DiagnosticHud.tscn" id="23_pocq6"]
[ext_resource type="PackedScene" uid="uid://bocb7hjilvri5" path="res://source/match/handlers/ArealUnitSelectionHandler.tscn" id="24_aug7m"]
[ext_resource type="PackedScene" uid="uid://pi813oou7xim" path="res://source/match/handlers/DoubleClickUnitSelectionHandler.tscn" id="25_ldkhw"]
[ext_resource type="PackedScene" uid="uid://yn470qvc3eak" path="res://source/match/handlers/MatchEndHandler.tscn" id="26_4d7im"]
[ext_resource type="PackedScene" uid="uid://b8p6lcwubx1tp" path="res://source/match/handlers/UnitVisibilityHandler.tscn" id="32_fci1c"]

Expand Down Expand Up @@ -410,9 +411,11 @@ visible = false

[node name="Handlers" type="Node3D" parent="."]

[node name="UnitSelectionHandler" parent="Handlers" instance=ExtResource("20_tw1wp")]
[node name="ArealUnitSelectionHandler" parent="Handlers" instance=ExtResource("24_aug7m")]
rectangular_selection_3d = NodePath("../../RectangularSelection3D")

[node name="DoubleClickUnitSelectionHandler" parent="Handlers" instance=ExtResource("25_ldkhw")]

[node name="MouseClickAnimationsHandler" parent="Handlers" instance=ExtResource("22_438pb")]

[node name="MatchEndHandler" parent="Handlers" instance=ExtResource("26_4d7im")]
Expand Down
9 changes: 9 additions & 0 deletions source/match/MatchUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ static func traverse_node_tree_and_replace_materials_matching_albedo(
)
):
child.set("surface_material_override/{0}".format([surface_id]), material_to_set)


static func select_units(units_to_select):
if not units_to_select.empty() and not Input.is_action_pressed("shift_selecting"):
MatchSignals.deselect_all_units.emit()
for unit in units_to_select.iterate():
var selection = unit.find_child("Selection")
if selection != null:
selection.select()
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ extends Node3D
var _rectangular_selection_3d = null
var _highlighted_units = Utils.Set.new()

var _double_click_last_unit = null
var _double_click_timer = 0


func _ready():
_rectangular_selection_3d = get_node_or_null(rectangular_selection_3d)
Expand All @@ -16,7 +13,6 @@ func _ready():
_rectangular_selection_3d.started.connect(_on_selection_started)
_rectangular_selection_3d.interrupted.connect(_on_selection_interrupted)
_rectangular_selection_3d.finished.connect(_on_selection_finished)
MatchSignals.unit_selected.connect(_on_unit_selected)


func _force_highlight(units_to_highlight):
Expand Down Expand Up @@ -50,15 +46,6 @@ func _get_controlled_units_from_navigation_domain_within_topdown_polygon_2d(
return units_within_polygon


func _select_units(units_to_select):
if not units_to_select.empty() && not Input.is_action_pressed("shift_selecting"):
MatchSignals.deselect_all_units.emit()
for unit in units_to_select.iterate():
var selection = unit.find_child("Selection")
if selection != null:
selection.select()


func _rebase_topdown_polygon_2d_to_different_plane(topdown_polygon_2d, plane):
var rebased_topdown_polygon_2d = []
var camera = get_viewport().get_camera_3d()
Expand Down Expand Up @@ -116,29 +103,4 @@ func _on_selection_finished(topdown_polygon_2d):
)
)
)
_select_units(units_to_select)


func _on_unit_selected(unit):
if Time.get_ticks_msec() <= _double_click_timer + 50:
return
if not unit.is_in_group("controlled_units"):
return
if unit == _double_click_last_unit and Time.get_ticks_msec() <= _double_click_timer + 600:
_double_click_last_unit = unit
_double_click_timer = Time.get_ticks_msec()
_double_click(unit)
else:
_double_click_last_unit = unit
_double_click_timer = Time.get_ticks_msec()


func _double_click(unit):
var units_to_select = Utils.Set.new()
var camera = get_viewport().get_camera_3d()
for u in get_tree().get_nodes_in_group("controlled_units"):
if not u.visible or not camera.is_position_in_frustum(u.global_position):
continue
if u.type == unit.type:
units_to_select.add(u)
_select_units(units_to_select)
Utils.Match.select_units(units_to_select)
6 changes: 6 additions & 0 deletions source/match/handlers/ArealUnitSelectionHandler.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bocb7hjilvri5"]

[ext_resource type="Script" path="res://source/match/handlers/ArealUnitSelectionHandler.gd" id="1_xmfnv"]

[node name="ArealUnitSelectionHandler" type="Node3D"]
script = ExtResource("1_xmfnv")
39 changes: 39 additions & 0 deletions source/match/handlers/DoubleClickUnitSelectionHandler.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extends Node3D

const DOUBLE_CLICK_LB_MS = 50
const DOUBLE_CLICK_UB_MS = 600

var _last_unit_selected = null
var _last_unit_selected_timestamp = 0


func _ready():
MatchSignals.unit_selected.connect(_on_unit_selected)


func _handle_double_click(unit_type):
var units_to_select = Utils.Set.new()
var camera = get_viewport().get_camera_3d()
for unit in get_tree().get_nodes_in_group("controlled_units"):
if not unit.visible or not camera.is_position_in_frustum(unit.global_position):
continue
if unit.type == unit_type:
units_to_select.add(unit)
Utils.Match.select_units(units_to_select)


func _on_unit_selected(unit):
if not unit.is_in_group("controlled_units"):
return
if Time.get_ticks_msec() < _last_unit_selected_timestamp + DOUBLE_CLICK_LB_MS:
return
if (
unit == _last_unit_selected
and Time.get_ticks_msec() <= _last_unit_selected_timestamp + DOUBLE_CLICK_UB_MS
):
_last_unit_selected = null
_last_unit_selected_timestamp = 0
_handle_double_click(unit.type)
else:
_last_unit_selected = unit
_last_unit_selected_timestamp = Time.get_ticks_msec()
6 changes: 6 additions & 0 deletions source/match/handlers/DoubleClickUnitSelectionHandler.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://pi813oou7xim"]

[ext_resource type="Script" path="res://source/match/handlers/DoubleClickUnitSelectionHandler.gd" id="1_myskc"]

[node name="DoubleClickUnitSelectionHandler" type="Node3D"]
script = ExtResource("1_myskc")
6 changes: 0 additions & 6 deletions source/match/handlers/SelectionHandler.tscn

This file was deleted.

0 comments on commit fca7c56

Please sign in to comment.