Files

92 lines
2.7 KiB
GDScript

extends CanvasLayer
@onready var chat_log = $Control/VBoxContainer/ChatLog
@onready var chat_messages_container = $Control/VBoxContainer/ChatLog/ChatMessages
@onready var chat_input = $Control/VBoxContainer/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():
print("[ChatUI] _ready called")
chat_input.text_submitted.connect(_on_message_submitted)
visible = false
# Defer GameState connection
call_deferred("_connect_gamestate")
func _connect_gamestate():
print("[ChatUI] _connect_gamestate called")
if GameState:
print("[ChatUI] Connecting to GameState.chat_message_received signal")
GameState.chat_message_received.connect(_on_chat_message)
print("[ChatUI] Signal connected successfully")
else:
print("[ChatUI] ERROR: GameState not found!")
func _on_chat_message(sender: String, message: String, type: String):
print("[ChatUI] _on_chat_message received - Sender: '%s', Message: '%s', Type: '%s'" % [sender, message, type])
_add_message(sender, message, type)
# Show chat panel when a message is received
visible = true
func _add_message(sender: String, message: String, type: String = "normal"):
print("[ChatUI] Adding message to chat log")
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)
print("[ChatUI] set type")
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]
print("[ChatUI] Bout to add")
chat_messages_container.add_child(label)
print("[ChatUI] Message added: %s" % label.text)
# 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 is_typing() -> bool:
return chat_input != null and chat_input.has_focus()
func _on_message_submitted(text: String):
if text.strip_edges().is_empty():
_close()
return
#_add_message(GameState.character.get("name"),text,"normal")
print("[ChatUI] Sending chat message: %s" % text)
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()