tool
extends RichTextLabel

signal text_changed

export(bool) var fit_content_size: bool = false setget set_fit_content_size


func _ready() -> void:
	connect("text_changed", self, "_on_text_changed")
	
	_fit_content_size(fit_content_size)


func set_text(t: String) -> void:
	.set_text(t)
	emit_signal("text_changed")


func set_fit_content_size(val: bool) -> void:
	fit_content_size = val
	_fit_content_size(fit_content_size)


func _fit_content_size(val: bool) -> void:
	if val:
		set_fit_content_height(false)
		_fit_content_width()
		set_fit_content_height(true)
	else:
		set_fit_content_height(false)
		set_custom_minimum_size(Vector2.ZERO)


func _fit_content_width() -> void:
	var minimum_size_y: float = get_minimum_size().y
	var text_width: float = \
			get_font("normal_font").get_string_size(get_text()).x
	set_custom_minimum_size(Vector2(text_width, minimum_size_y))


func _on_text_changed() -> void:
	_fit_content_size(fit_content_size)
