xiandie/scene/dialog/dialog.gd

75 lines
1.6 KiB
GDScript

extends Control
@onready var label = %RichTextLabel as RichTextLabel
var pending_bbcode = []
var tweening := false
func _ready():
label.clear()
modulate = Color(1, 1, 1, 0)
# test notification in editor
if GlobalConfig.DEBUG:
call_deferred("test")
func test():
# append_dialog("吕萍", "Hello, 2! very very long message, very very loooonnnggggggg!")
# append_note("Hello, 3!")
# append_note("Hello, blue!", "blue", 1)
# append_dialog("车夫", "你好!", "green")
pass
func append_note(note: String, note_color := "white", duration := 2.5) -> void:
pending_bbcode.append(
["[center][color=" + note_color + "]" + note + "[/color][/center]", duration]
)
_show()
func append_dialog(
character: String,
content: String,
character_color := "orange",
content_color := "white",
duration := 2.5
) -> void:
pending_bbcode.append(
[
(
"[center][color="
+ character_color
+ "][b]"
+ character
+ ":[/b][/color][color="
+ content_color
+ "]"
+ content
+ "[/color][/center]"
),
duration
]
)
_show()
func _show() -> void:
if not tweening and pending_bbcode.size() > 0:
var tuple = pending_bbcode.pop_front()
label.parse_bbcode(tuple[0])
tweening = true
var tween = create_tween()
# 0.5s to show the notification
tween.tween_property(self, "modulate:a", 1, 0.2).set_trans(Tween.TRANS_CUBIC)
# keep the notification
tween.tween_interval(max(tuple[1] - 0.4, 0))
# 0.5s to hide the notification
tween.tween_property(self, "modulate:a", 0, 0.2).set_trans(Tween.TRANS_CUBIC)
# callback
tween.tween_callback(_check_next)
func _check_next():
tweening = false
_show()