This commit is contained in:
2025-04-13 14:40:41 +08:00
parent 2201147ccc
commit a1e71a6a79
20 changed files with 1738 additions and 1794 deletions

View File

@@ -1,22 +1,35 @@
extends PanelContainer
@onready var label = $Label
var display_time = 4.0 # 显示的时间(秒)
var fade_duration = 1.0 # 渐隐时间(秒)
@export var display_time = 4.0
@export var fade_duration = 1.0
var label: Label # 显式声明类型
func _ready():
# 确保获取Label节点
label = find_child("Label") as Label
if not label:
push_error("找不到Label子节点请检查场景结构")
func setup(text: String, color: Color, duration: float, fade: float):
display_time = duration
fade_duration = fade
# 确保添加到场景树
Engine.get_main_loop().root.add_child(self)
Toast(text, color)
func Toast(text: String, text_color: Color = Color.WHITE):
if !label:
return
label.text = text
label.modulate = text_color
show()
modulate.a = 1 # 确保初始透明度为 1
await get_tree().create_timer(display_time).timeout # 等待显示时间
await fade_out() # 开始渐隐
func fade_out() -> void:
var fade_step = 1.0 / (fade_duration / 60) # 每帧减少的透明度
while modulate.a > 0:
modulate.a -= fade_step
if modulate.a < 0:
modulate.a = 0
await get_tree().create_timer(0).timeout # 等待下一帧
hide() # 完全透明时隐藏面板
modulate.a = 1
await get_tree().create_timer(display_time).timeout
var tween = create_tween()
tween.tween_property(self, "modulate:a", 0.0, fade_duration)
await tween.finished
queue_free()

12
components/toast.gd Normal file
View File

@@ -0,0 +1,12 @@
# toast.gd
extends Node
const ToastScene = preload("res://components/ToastShow.tscn")
static func show(text: String,
color: Color = Color.WHITE,
duration: float = 3.0,
fade: float = 1.0) -> void:
var toast = ToastScene.instantiate()
# 延迟设置参数确保节点初始化完成
toast.call_deferred("setup", text, color, duration, fade)

6
components/toast.tscn Normal file
View File

@@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cvip7owyfmqav"]
[ext_resource type="Script" path="res://components/toast.gd" id="1_rdgmi"]
[node name="Toast" type="Node"]
script = ExtResource("1_rdgmi")