diary/user_interface/chat_panel/message_editable.gd

161 lines
4.0 KiB
GDScript3
Raw Normal View History

extends EditableTextBlock
class_name MessageEditable
const BUBBLE_BY_CURRENT = preload("res://user_interface/chat_panel/bubble_by_current.png")
const BUBBLE_BY_INACTIVE = preload("res://user_interface/chat_panel/bubble_by_inactive.png")
const FOLLOW_UP_BUBBLE_BY_CURRENT = preload("res://user_interface/chat_panel/follow_up_bubble_by_current.png")
const FOLLOW_UP_BUBBLE_BY_INACTIVE = preload("res://user_interface/chat_panel/follow_up_bubble_by_inactive.png")
onready var message_inserted_sound = get_node("MessageInsertedSound")
2022-06-04 13:33:00 +02:00
var associated_message: WeakRef
var follow_up: bool = false
# If the node was spawned by PingSystem.
var insert_mode: bool = false
var ping_id: int = -1
func get_user() -> User:
2022-06-04 13:33:00 +02:00
return associated_message.get_ref().get_user()
func _get_content_string() -> String: # Override.
2022-06-04 13:33:00 +02:00
return associated_message.get_ref().content
func _setup(): # Override.
if _get_content_string().empty():
_setup_insert_mode()
else:
_setup_content()
_setup_appearance()
func _setup_insert_mode():
# warning-ignore:return_value_discarded
PingSystem.connect("ping_handled", self, "_on_ping_handled")
ping_id = PingSystem.total_ping_count
2022-06-04 13:33:00 +02:00
PingSystem.unhandled_ping_count += 1
PingSystem.total_ping_count += 1
# warning-ignore:return_value_discarded
_check_if_next_ping()
insert_mode = true
var is_multiline = (randi() % 100) < 20
if is_multiline:
text_edit.min_width = 488
text_edit.min_height = 18 * (randi() % 2 + 2) + 19
else:
text_edit.min_width = randi() % 440 + 48
text_edit.min_height = 0
_enable_edit_mode()
func _setup_popup_menu_items(): # Override.
2022-06-04 13:33:00 +02:00
if associated_message.get_ref().get_user() == Users.get_current():
popup_menu.add_item("Edit", 0)
popup_menu.add_item("Copy", 1)
popup_menu.add_item("Delete", 2)
else:
popup_menu.add_item("Copy", 1)
func _adjust_appearance_to_user(): # Override.
if get_user().is_current():
_set_appearance_to_style_current()
else:
_set_appearance_to_style_inactive()
func _set_appearance_to_style_current():
if follow_up:
nine_patch.texture = FOLLOW_UP_BUBBLE_BY_CURRENT
else:
nine_patch.texture = BUBBLE_BY_CURRENT
nine_patch.patch_margin_left = 12
nine_patch.patch_margin_right = 21
margin_container.add_constant_override("margin_left", 8)
margin_container.add_constant_override("margin_right", 17)
size_flags_horizontal = SIZE_SHRINK_END
nine_patch.self_modulate = get_user().own_message_color
func _set_appearance_to_style_inactive():
if follow_up:
nine_patch.texture = FOLLOW_UP_BUBBLE_BY_INACTIVE
else:
nine_patch.texture = BUBBLE_BY_INACTIVE
nine_patch.patch_margin_left = 21
nine_patch.patch_margin_right = 12
margin_container.add_constant_override("margin_left", 17)
margin_container.add_constant_override("margin_right", 8)
size_flags_horizontal = 0
nine_patch.self_modulate = Users.get_current().others_message_color
func _check_if_next_ping() -> bool:
if ping_id == PingSystem.current_ping_id:
text_edit.readonly = false
text_edit.placeholder = "Insert Message"
return true
else:
text_edit.readonly = true
return false
func _post_enable_edit_mode(): # Override.
if !insert_mode:
text_edit.grab_focus()
text_edit.confirm_on_focus_exit = true
func _post_disable_edit_mode(): # Override.
if insert_mode:
_disable_insert_mode()
func _disable_insert_mode():
insert_mode = false
text_edit.min_width = 0
text_edit.min_height = 0
PingSystem.disconnect("ping_handled", self, "_on_ping_handled")
PingSystem.emit_signal("ping_handled")
message_inserted_sound.play()
func _delete():
2022-06-04 13:33:00 +02:00
associated_message.get_ref().delete()
func _on_popup_menu_id_pressed(id):
match id:
0: # Edit.
_enable_edit_mode()
1: # Copy.
2022-06-04 13:33:00 +02:00
OS.clipboard = associated_message.get_ref().content
2: # Delete.
_delete()
func _on_text_edit_confirmed(confirmed_content): # Override
var test_string = confirmed_content.strip_edges()
if test_string.empty():
if not insert_mode:
_delete()
else:
2022-06-04 13:33:00 +02:00
associated_message.get_ref().edit(confirmed_content)
_disable_edit_mode()
func _on_ping_handled():
if _check_if_next_ping():
text_edit.grab_focus()