xiandie/scene/notification/notification.gd

79 lines
2.3 KiB
GDScript3
Raw Permalink Normal View History

2025-01-29 14:05:27 +00:00
class_name Notification extends Control
2024-12-23 01:29:31 +00:00
@onready var top_right_label := %RichTextLabel as RichTextLabel
@onready var top_center_label := %CenterLabel as Label
2025-01-29 14:05:27 +00:00
@onready var center_texture := %CenterTexture as TextureRect
2024-12-24 01:16:06 +00:00
var pending_notifications = []
var tweening_top_left := false
2024-12-24 01:16:06 +00:00
func _ready() -> void:
top_right_label.modulate.a = 0
top_center_label.modulate.a = 0
2025-01-29 14:05:27 +00:00
center_texture.modulate.a = 0
2024-12-24 01:16:06 +00:00
# # test notification in editor
# if GlobalConfig.DEBUG:
# call_deferred("test")
# func test():
# # return
# show_center_notification(tr("ui_press_e"))
# # show_center_notification(tr("ui_press_b"))
# # show_center_notification(tr("ui_press_shift"))
# #show_notification("Hello, 1!", 42)
# #show_notification("Hello, 2! very very long message, very very loooonnnggggggg!", 2)
# #show_notification("Hello, 3!", 24)
# show_notification(tr("ui_saved_all"), 1)
2025-01-29 14:05:27 +00:00
func show_center_texture(texture, duration := 3.0):
if not center_texture:
return
center_texture.texture = texture
var tween = create_tween()
tween.tween_property(center_texture, "modulate:a", 1, 0.4)
tween.tween_interval(max(0.1, duration - .8))
tween.tween_property(center_texture, "modulate:a", 0, 0.4)
func show_center_notification(msg, duration := 3.0):
if not top_center_label:
return
top_center_label.text = msg
var tween = create_tween()
tween.tween_property(top_center_label, "modulate:a", 1, 0.4)
tween.tween_interval(max(0.1, duration - .8))
tween.tween_property(top_center_label, "modulate:a", 0, 0.4)
2024-12-24 01:16:06 +00:00
2024-12-23 01:29:31 +00:00
func show_notification(msg, number):
if not top_right_label:
return
if tweening_top_left:
2024-12-24 01:16:06 +00:00
pending_notifications.append([msg, number])
else:
top_right_label.text = msg
tweening_top_left = true
2024-12-24 01:16:06 +00:00
var tween = create_tween()
# 0.5s to show the notification
tween.tween_property(top_right_label, "modulate:a", 1, 0.5).set_trans(Tween.TRANS_CUBIC)
2024-12-24 01:16:06 +00:00
# keep the notification
if pending_notifications.size() > 0:
tween.tween_interval(2)
else:
tween.tween_interval(3)
# 0.5s to hide the notification
tween.tween_property(top_right_label, "modulate:a", 0, 0.5).set_trans(Tween.TRANS_CUBIC)
2024-12-24 01:16:06 +00:00
# callback
tween.tween_callback(_check_next)
2024-12-24 01:16:06 +00:00
func _check_next():
tweening_top_left = false
2024-12-24 01:16:06 +00:00
if pending_notifications.size() > 0:
var n = pending_notifications.pop_front()
show_notification(n[0], n[1])