diary/user_interface/utility_controls/editable_text_block.gd

170 lines
4.0 KiB
GDScript3
Raw Normal View History

extends MarginContainer
class_name EditableTextBlock
const EMOTE_LIST = preload("res://configuration/emotes/emote_list.tres")
onready var nine_patch = get_node("NinePatch")
onready var margin_container = get_node("VBoxContainer/MarginContainer")
onready var content = get_node("VBoxContainer/MarginContainer/Content")
onready var reference_label = get_node("VBoxContainer/MarginContainer/ReferenceLabel")
onready var text_edit = get_node("VBoxContainer/MarginContainer/ContentFitTextEdit")
onready var popup_menu = get_node("PopupMenu")
var edit_mode: bool = false
func _ready():
margin_container.remove_child(text_edit)
popup_menu.rect_size.x = 64
2022-06-04 13:33:00 +02:00
content.text = _get_content_string()
# warning-ignore:return_value_discarded
Users.connect("switched", self, "_on_user_switched")
_setup()
func _gui_input(event):
if event is InputEventMouseButton:
if event.is_pressed() and event.button_index == BUTTON_RIGHT and !edit_mode:
popup_menu.clear()
popup_menu.rect_size.y = 1
_setup_popup_menu_items()
popup_menu.popup(Rect2(
event.global_position,
popup_menu.rect_size
))
func _get_content_string() -> String: # Virtual.
return ""
func _setup(): # Virtual.
_setup_content()
_setup_appearance()
func _setup_content():
var emote_placeholder = "miii"
var emote_size = 21
if _check_if_string_only_contains_emote_tags(_get_content_string()):
emote_placeholder = "mMiii"
emote_size = 32
reference_label.text = _resolve_emotes_to_placeholder(
_get_content_string(),
emote_placeholder
)
content.bbcode_text = _resolve_emotes_in_string(
_get_content_string(),
emote_size
)
func _setup_appearance():
_adjust_appearance_to_user()
if text_edit.max_width > 0:
yield(get_tree(), "idle_frame")
if rect_size.x >= 488.0:
margin_container.remove_child(reference_label)
content.rect_min_size.x = 488.0
func _setup_popup_menu_items(): # Virtual.
pass
func _adjust_appearance_to_user(): # Virtual.
pass
func _check_if_string_only_contains_emote_tags(string: String):
var test_string = string
for entry in EMOTE_LIST.list:
test_string = test_string.replace(":" + entry + ":", "")
test_string = test_string.strip_edges()
return test_string.empty()
func _resolve_emotes_in_string(string: String, size: int) -> String:
var result := string
var str_size = str(size)
var img_tag = "[img=<" + str_size + ">x<" + str_size + ">]"
for entry in EMOTE_LIST.list:
result = result.replace(
":" + entry + ":",
img_tag + "res://configuration/emotes/" + entry + ".png[/img]"
)
return result
# We replace the emote_tag in ReferenceLabel with some placeholder string,
# that has about the same width, as an emote, to fit container sizing.
func _resolve_emotes_to_placeholder(
string: String, placeholder: String) -> String:
var result := string
for entry in EMOTE_LIST.list:
result = result.replace(":" + entry + ":", placeholder)
return result
func _enable_edit_mode():
edit_mode = true
if margin_container.is_a_parent_of(content):
content.rect_min_size.x = 0.0
margin_container.remove_child(content)
if margin_container.is_a_parent_of(reference_label):
margin_container.remove_child(reference_label)
margin_container.add_child(text_edit)
yield(get_tree(), "idle_frame")
text_edit.text = _get_content_string()
text_edit.confirm_on_focus_exit = false
_post_enable_edit_mode()
text_edit.adjust_size()
2022-06-05 14:22:08 +02:00
text_edit.grab_focus()
yield(get_tree(), "idle_frame")
text_edit.select_all()
func _post_enable_edit_mode(): # Virtual.
pass
func _disable_edit_mode():
edit_mode = false
if margin_container.is_a_parent_of(text_edit):
margin_container.remove_child(text_edit)
margin_container.add_child(content)
margin_container.add_child(reference_label)
_post_disable_edit_mode()
_setup()
func _post_disable_edit_mode(): # Virtual.
pass
func _on_user_switched():
_adjust_appearance_to_user()
func _on_popup_menu_id_pressed(_id): # Virtual.
pass
func _on_text_edit_confirmed(_confirmed_content): # Virtual.
pass