xiandie/scene/dialog/dialog.gd

88 lines
2.0 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, false]
)
_show()
func append_dialog(
character: String,
content: String,
character_color := "orange",
content_color := "white",
duration := 2.5
) -> void:
# remove non-dialog notifications
for _i in range(pending_bbcode.size()):
var triple = pending_bbcode.pop_front()
if not triple[2]:
continue
pending_bbcode.append(triple)
pending_bbcode.append(
[
(
"[center][color="
+ character_color
+ "][b]"
+ character
+ ":[/b][/color][color="
+ content_color
+ "]"
+ content
+ "[/color][/center]"
),
duration,
true
]
)
_show()
func _show() -> void:
if not tweening and pending_bbcode.size() > 0:
var triple = pending_bbcode.pop_front()
label.parse_bbcode(triple[0])
if triple[2]:
label.add_theme_color_override("font_shadow_color", Color(0.2, 0.2, 0.2, 0.6))
else:
label.remove_theme_color_override("font_shadow_color")
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(triple[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()