From 99e2dce2b6e7c160831747e9ed30e6cd4c5d09eb Mon Sep 17 00:00:00 2001 From: Jack Case Date: Sat, 23 Aug 2025 13:35:41 -0400 Subject: [PATCH] Implemented group highlight on hover It's not currently stable, but it's ok for now. --- grid.gd | 68 +++++++++++++++++++++++++++++++++++------------------- token.gd | 19 ++++++++++++--- token.tscn | 3 +++ 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/grid.gd b/grid.gd index a2f5c5a..3d49c17 100644 --- a/grid.gd +++ b/grid.gd @@ -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 @@ -15,7 +17,7 @@ var token = preload("res://token.tscn") func _ready(): debug_label = $"Debug Label" - + for row in rows: grid.append([]) for column in cols: @@ -25,13 +27,14 @@ 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() - + func _process(_delta: float) -> void: pass - + func _on_token_clicked(token_coord): var group = get_group_of_token(token_coord) if group.size() >= min_group_size: @@ -39,24 +42,41 @@ func _on_token_clicked(token_coord): var current_token = grid[coord[0]][coord[1]] as Token current_token.queue_free() grid[coord[0]][coord[1]] = null # do I actually want a null value or should there be some other placeholder? - + 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 - + func populate_grid(): pass - + func to_grid_coord(idx): pass - + func to_index(idx): pass - + func calculate_token_groups(): groups = [] var visited_nodes = [] @@ -65,45 +85,45 @@ func calculate_token_groups(): for col in cols: if [row, col] in visited_nodes: continue - + group_queue.append([row,col]) - + var new_group = [] while not group_queue.is_empty(): var current_coord = group_queue.pop_back() var current_token = grid[current_coord[0]][current_coord[1]] new_group.append(current_coord) visited_nodes.append(current_coord) - + var adjacent_token_coords = [ [current_coord[0] - 1, current_coord[1]], [current_coord[0], current_coord[1] + 1], [current_coord[0] + 1, current_coord[1]], [current_coord[0], current_coord[1] - 1] ] - + var valid_coords = adjacent_token_coords.filter( - func(coord_pair): + func(coord_pair): return ( - coord_pair[0] >= 0 and - coord_pair[0] < rows and - coord_pair[1] >= 0 and + coord_pair[0] >= 0 and + coord_pair[0] < rows and + coord_pair[1] >= 0 and coord_pair[1] < cols ) ) - + for coord in valid_coords: if (coord not in visited_nodes and grid[coord[0]][coord[1]].type == current_token.type and coord not in group_queue): group_queue.append(coord) groups.append(new_group) - + debug_label.text = str(groups) func get_group_of_token(token_coord) -> Array: for group in groups: if token_coord in group: return group - + return [] diff --git a/token.gd b/token.gd index e311e13..67e4934 100644 --- a/token.gd +++ b/token.gd @@ -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} @@ -17,7 +18,7 @@ func _ready(): color_polygon = $Color highlight_polygon = $highlight_indicator debug_label = $"Debug Label" - + func set_type(type: token_type): self.type = type match self.type: @@ -29,7 +30,7 @@ func set_type(type: token_type): color_polygon.color = Color.BLUE token_type.TYPE_4: color_polygon.color = Color.GOLD - + func set_debug_label(text: String): debug_label.text = text @@ -38,6 +39,18 @@ func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) # emit event up to parent on click if Input.is_action_just_pressed("select"): token_clicked.emit() - + 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) diff --git a/token.tscn b/token.tscn index eced291..9df16ec 100644 --- a/token.tscn +++ b/token.tscn @@ -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"]