xiandie/scene/entity/ux/hover_light_click_area.gd
2025-07-14 18:50:05 +08:00

70 lines
1.6 KiB
GDScript

class_name HoverLightClickArea extends Area2D
signal clicked
@export var freezing := false:
set(val):
freezing = val
if freezing and touching:
_on_mouse_exited()
var touching = false
static var current_focusing_item = "":
set(val):
current_focusing_item = val
if GlobalConfig.DEBUG:
print("HoverLightClickArea current_focusing_item=", current_focusing_item)
static var pending_enter_callables := [] as Array[Callable]
# to pointlight2d
var lights: Array[NodePath] = []
func _ready() -> void:
for c in get_children():
if c is PointLight2D:
lights.append(c)
if lights.is_empty():
printerr("HoverLightButton has no PointLight2D children")
mouse_entered.connect(_on_mouse_entered)
mouse_exited.connect(_on_mouse_exited)
func is_focused() -> bool:
return current_focusing_item == name
func _on_mouse_entered() -> bool:
touching = true
if freezing or not is_visible_in_tree():
return false
if is_focused():
return true
# 尝试获得 current_focusing_item
if current_focusing_item != "":
if not pending_enter_callables.has(_on_mouse_entered):
pending_enter_callables.append(_on_mouse_entered)
return false
current_focusing_item = name
_toggle_light(true)
return true
func _on_mouse_exited() -> void:
touching = false
pending_enter_callables.erase(_on_mouse_entered)
# frezzing 不影响 mouse exited
if is_focused():
current_focusing_item = ""
for c in pending_enter_callables:
if c.call():
break
_toggle_light(false)
func _toggle_light(on: bool) -> void:
for l in lights:
var light = get_node(l) as PointLight2D
if light:
light.enabled = on