Implemented group highlight on hover

It's not currently stable, but it's ok for now.
This commit is contained in:
Jack Case
2025-08-23 13:35:41 -04:00
parent 9bcafdff5c
commit 99e2dce2b6
3 changed files with 63 additions and 27 deletions

24
grid.gd
View File

@@ -1,4 +1,5 @@
extends Node2D
class_name Grid
@export var rows: int = 5
@export var cols: int = 5
@@ -8,6 +9,7 @@ extends Node2D
var grid: Array[Array]
var groups: Array
var hovered_group: Array
var debug_label: Label
@@ -25,6 +27,7 @@ func _ready():
token_node.set_type(randi_range(0,3) as Token.token_type)
token_node.set_debug_label(str(row) + "," + str(column))
token_node.token_clicked.connect(_on_token_clicked.bind([row,column]))
token_node.token_hovered.connect(_on_token_hovered.bind([row,column]))
grid[row].append(token_node)
calculate_token_groups()
@@ -42,8 +45,25 @@ func _on_token_clicked(token_coord):
update_grid()
func highlight_group(group: Array):
pass
func _on_token_hovered(enter: bool, coord: Array):
var group = get_group_of_token(coord)
if enter:
highlight_group(group, true)
else:
highlight_group(group, false)
func highlight_group(group: Array, enable: bool):
if group != hovered_group:
# Un-highlight current, update current
for coord in hovered_group:
var current_token = grid[coord[0]][coord[1]]
current_token.set_highlighted(false)
for coord in group:
var current_token = grid[coord[0]][coord[1]] as Token
current_token.set_highlighted(enable)
hovered_group = group
func update_grid():
pass

View File

@@ -2,6 +2,7 @@ extends Node2D
class_name Token
signal token_clicked
signal token_hovered(entered: bool)
enum token_type {TYPE_1, TYPE_2, TYPE_3, TYPE_4}
enum token_state {NONE, HIGHLIGHT}
@@ -41,3 +42,15 @@ func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int)
func set_highlighted(highlight: bool):
state = token_state.HIGHLIGHT if highlight else token_state.NONE
match state:
token_state.HIGHLIGHT:
highlight_polygon.visible = true
token_state.NONE:
highlight_polygon.visible = false
func _on_area_2d_mouse_entered() -> void:
token_hovered.emit(true)
func _on_area_2d_mouse_exited() -> void:
token_hovered.emit(false)

View File

@@ -43,9 +43,12 @@ color = Color(0.84101, 0.000705212, 0.866408, 1)
polygon = PackedVector2Array(0, 0, 40, 0, 40, 40, 0, 40)
[node name="highlight_indicator" type="Polygon2D" parent="."]
visible = false
position = Vector2(52.8333, 76.6667)
scale = Vector2(0.427083, 0.319444)
polygon = PackedVector2Array(-112, -96, -16, -96, -64, -168)
uv = PackedVector2Array(-112, -96, -16, -96, -72, -160)
[connection signal="input_event" from="Area2D" to="." method="_on_area_2d_input_event"]
[connection signal="mouse_entered" from="Area2D" to="." method="_on_area_2d_mouse_entered"]
[connection signal="mouse_exited" from="Area2D" to="." method="_on_area_2d_mouse_exited"]