diff options
Diffstat (limited to 'src/helpers/text_fade.gd')
-rw-r--r-- | src/helpers/text_fade.gd | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/helpers/text_fade.gd b/src/helpers/text_fade.gd new file mode 100644 index 0000000..e83d59f --- /dev/null +++ b/src/helpers/text_fade.gd @@ -0,0 +1,92 @@ +extends RichTextLabel + +export(PoolColorArray) var char_colors = PoolColorArray() +export var fade_step_time = 0.1 +export var do_autostart = true +export var char_delta = 1.0 +export var char_step = 0.01 +export var time_until_fadeout = 1.0 + +var fade_timer +var wait_timer +var delta +var current_max_index +var last_index_delta +var fadein + +func set_bbcode_opacity(): + var new_bbcode = str("") + + for i in min(char_colors.size(), text.length()): + if i <= current_max_index: + if fadein == true: + char_colors[i].a = min(char_colors[i].a + char_step, 1.0) + else: + char_colors[i].a = max(char_colors[i].a - char_step, 0.0) + new_bbcode += "[color=#" + char_colors[i].to_html(true) + "]" + text[i] + "[/color]" + + set_bbcode(new_bbcode) + + if char_colors[char_colors.size() - 1].a == 1.0 and fadein == true: + fade_timer.stop() + wait_timer.start() + +func set_next_char(): + last_index_delta = delta + current_max_index = min(current_max_index + 1, text.length()) + +func reset(_fadein = true): + delta = 0 + current_max_index = 0 + last_index_delta = delta + fadein = _fadein + +func _ready(): + if char_colors.size() > text.length() or char_colors.size() == 0: + push_error (str(self.name) + ": Size of character colors more than the text length: " + str(char_colors.size()) + " > " + str(text.length())) + get_tree().quit(-1) + + if char_colors.size() < text.length(): + var i = char_colors.size() - 1 + while i < text.length() - 1: + char_colors.append(char_colors[i]) + i += 1 + + for i in char_colors.size(): + char_colors[i].a = 0.0 + + current_max_index = -1 + set_bbcode_opacity() + reset(true) + + fade_timer = Timer.new() + fade_timer.set_one_shot(false) + fade_timer.set_wait_time(fade_step_time) + fade_timer.set_autostart(do_autostart) + fade_timer.connect("timeout", self, "_fade_timer_callback") + add_child(fade_timer) + + wait_timer = Timer.new() + wait_timer.set_one_shot(true) + wait_timer.set_wait_time(time_until_fadeout) + wait_timer.set_autostart(false) + wait_timer.connect("timeout", self, "_wait_timer_callback") + add_child(wait_timer) + +func _process(_delta): + delta += _delta + +func _fade_timer_callback(): + if current_max_index >= 0: + if delta > last_index_delta + char_delta: + set_next_char() + + set_bbcode_opacity() + +func _wait_timer_callback(): + reset(false) + fade_timer.start() + + +func _on_start_light_timeout(): + fade_timer.start() |