class_name Notification extends Control @onready var top_right_label := %RichTextLabel as RichTextLabel @onready var top_center_label := %CenterLabel as Label @onready var center_texture := %CenterTexture as TextureRect var pending_notifications = [] var tweening_top_left := false func _ready() -> void: top_right_label.modulate.a = 0 top_center_label.modulate.a = 0 center_texture.modulate.a = 0 # # 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) 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) func show_notification(msg, number): if not top_right_label: return if tweening_top_left: pending_notifications.append([msg, number]) else: top_right_label.text = msg tweening_top_left = true 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) # 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) # callback tween.tween_callback(_check_next) func _check_next(): tweening_top_left = false if pending_notifications.size() > 0: var n = pending_notifications.pop_front() show_notification(n[0], n[1])