75 lines
1.9 KiB
GDScript
75 lines
1.9 KiB
GDScript
extends CanvasLayer
|
|
|
|
@onready var chat_log = $Control/ChatLog
|
|
@onready var chat_messages_container = $Control/ChatLog/ChatMessages
|
|
@onready var chat_input = $Control/InputContainer/ChatInput
|
|
|
|
const MAX_MESSAGES = 50
|
|
const TYPE_COLORS = {
|
|
"normal": Color.WHITE,
|
|
"system": Color(1, 0.8, 0),
|
|
"party": Color(0, 0.8, 1),
|
|
"whisper": Color(0.8, 0.4, 1)
|
|
}
|
|
|
|
func _ready():
|
|
chat_input.text_submitted.connect(_on_message_submitted)
|
|
|
|
visible = false
|
|
|
|
# Defer GameState connection
|
|
call_deferred("_connect_gamestate")
|
|
|
|
func _connect_gamestate():
|
|
if GameState:
|
|
GameState.chat_message_received.connect(_on_chat_message)
|
|
|
|
func _on_chat_message(sender: String, message: String, type: String):
|
|
_add_message(sender, message, type)
|
|
|
|
func _add_message(sender: String, message: String, type: String = "normal"):
|
|
var label = RichTextLabel.new()
|
|
label.bbcode_enabled = true
|
|
label.fit_content = true
|
|
label.scroll_active = false
|
|
|
|
var color = TYPE_COLORS.get(type, Color.WHITE)
|
|
var color_hex = color.to_html(false)
|
|
|
|
if type == "system":
|
|
label.text = "[color=#%s][System] %s[/color]" % [color_hex, message]
|
|
else:
|
|
label.text = "[color=#ffd700]%s:[/color] [color=#%s]%s[/color]" % [sender, color_hex, message]
|
|
|
|
chat_messages_container.add_child(label)
|
|
|
|
# Limit messages
|
|
while chat_messages_container.get_child_count() > MAX_MESSAGES:
|
|
chat_messages_container.get_child(0).queue_free()
|
|
|
|
# Scroll to bottom
|
|
await get_tree().process_frame
|
|
chat_log.scroll_vertical = chat_log.get_v_scroll_bar().max_value
|
|
|
|
func focus_input():
|
|
chat_input.grab_focus()
|
|
|
|
func _on_message_submitted(text: String):
|
|
if text.strip_edges().is_empty():
|
|
_close()
|
|
return
|
|
|
|
GameState.send_chat(text)
|
|
chat_input.text = ""
|
|
_close()
|
|
|
|
func _close():
|
|
visible = false
|
|
chat_input.release_focus()
|
|
|
|
func _input(event):
|
|
if visible:
|
|
if event.is_action_pressed("escape_menu"):
|
|
_close()
|
|
get_viewport().set_input_as_handled()
|