@tool
class_name RichTextLoading
extends RichTextEffect


var bbcode: String = "loading"


func _init() -> void:
	resource_name = "RichTextLoading"


func _process_custom_fx(char_fx: CharFXTransform) -> bool:
	_process_big_wave(char_fx)
	_process_small_wave(char_fx)
	_process_fade(char_fx)
	return true


func _process_big_wave(char_fx: CharFXTransform) -> void:
	var freq: float = char_fx.env.get("freq", 1.0)
	var height: float = char_fx.env.get("height", 25.0)
	var char_range: float = char_fx.env.get("range", 0.15)
	var curve: float = char_fx.env.get("curve", -13.0)

	var time_offset := float(-char_fx.relative_index) * char_range
	var sined_time: float = (sin((char_fx.elapsed_time + PI) * freq + time_offset) + 1.0) / 2.0
	sined_time = ease(sined_time, curve)
	var y_offset: float = sined_time * height
	char_fx.offset += Vector2.DOWN * y_offset


func _process_small_wave(char_fx: CharFXTransform) -> void:
	var freq: float = char_fx.env.get("freq", 4.0)
	var height: float = char_fx.env.get("height", 12.5)
	var char_range: float = char_fx.env.get("range", 0.5)
	var curve: float = char_fx.env.get("curve", 6.5)

	var time_offset := float(-char_fx.relative_index) * char_range
	var sined_time: float = (sin((char_fx.elapsed_time + PI) * freq + time_offset) + 1.0) / 2.0
	sined_time = ease(sined_time, curve)
	var y_offset: float = sined_time * height * (1.0 - char_fx.offset.y / 25.0)
	char_fx.offset += Vector2.UP * y_offset


func _process_fade(char_fx: CharFXTransform) -> void:
	var alpha: float = 1.0 - char_fx.offset.y / 25.0
	char_fx.color.a = alpha
