Files
agame/ui/crosshair.gd
T
2026-10-09 15:03:52 +08:00

58 lines
1.5 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@tool
extends Control
## 简易准心:屏幕正中央的一个点,用 _draw() 实时绘制,不依赖任何贴图。
##
## 挂法:CanvasLayer > Crosshair(Control),Control 的锚点设为「Full Rect」
## (anchor 全 0/1),这样 size 就等于视口尺寸,中心即屏幕中心。
## 因为是 CanvasLayer 子节点,在 window/stretch/mode=canvas_items 下会自动跟随缩放。
## 点的颜色
@export var dot_color: Color = Color(1, 1, 1, 0.9):
set(v):
dot_color = v
queue_redraw()
## 点的半径(像素)
@export_range(0.5, 12.0, 0.5) var dot_radius: float = 2.5:
set(v):
dot_radius = v
queue_redraw()
## 是否给点画一圈描边(浅色背景上也看得清)
@export var outline: bool = true:
set(v):
outline = v
queue_redraw()
## 描边颜色
@export var outline_color: Color = Color(0, 0, 0, 0.6):
set(v):
outline_color = v
queue_redraw()
## 描边宽度(像素)
@export_range(0.5, 4.0, 0.5) var outline_width: float = 1.0:
set(v):
outline_width = v
queue_redraw()
func _ready() -> void:
# 准心不吃鼠标事件,否则会挡住后面的 UI / 拖拽
mouse_filter = Control.MOUSE_FILTER_IGNORE
queue_redraw()
func _notification(what: int) -> void:
# 窗口尺寸变化时重画(stretch 模式下实时跟着变)
if what == NOTIFICATION_RESIZED:
queue_redraw()
func _draw() -> void:
var c := size * 0.5 # 控件中心 = 屏幕中心
if outline and outline_width > 0.0:
draw_circle(c, dot_radius + outline_width, outline_color)
draw_circle(c, dot_radius, dot_color)