From 75793bd23d275d10d6a0bd8024a7e412b64557ce Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 15 Dec 2024 16:53:12 +0100 Subject: next commit --- ui/chat_window.gd | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 ui/chat_window.gd (limited to 'ui/chat_window.gd') diff --git a/ui/chat_window.gd b/ui/chat_window.gd new file mode 100644 index 0000000..640a257 --- /dev/null +++ b/ui/chat_window.gd @@ -0,0 +1,63 @@ +extends PanelContainer + + +@export var player_color: Color = Color8(255, 255, 255) + + +func _ready() -> void: + # clear test label + %ChatWindow.remove_child(%ChatWindow.get_child(0)) + + if Network.map_server: + initialize() + + +func initialize() -> void: + Network.map_server.received_server_message.connect(_on_server_message_packet_received) + Network.map_server.received_broadcast_formatted_message.connect(_on_broadcast_formatted_message_packet_received) + + +func add_message(message: String, format: ChatMessageFormat): + var label = Label.new() + label.text = message + + if format.color: + label.add_theme_color_override("font_color", format.color) + + if format.size: + label.add_theme_font_size_override("font_size", format.size) + + %ChatWindow.add_child(label) + + await get_tree().process_frame # necessary to reach bottom correctly + %ScrollContainer.scroll_vertical = %ScrollContainer.get_v_scroll_bar().max_value + + +func _on_line_edit_text_submitted(new_text: String) -> void: + %LineEdit.text = "" + + var send_chat_message_packet := SendChatMessagePacket.new() + send_chat_message_packet.message = "%s : %s" % [Client.character.name, new_text] + Network.map_server.send(send_chat_message_packet) + + +func _on_server_message_packet_received(packet: ServerMessagePacket): + var format := ChatMessageFormat.new() + + if packet.message.begins_with(Client.character.name): + format.color = player_color + + add_message(packet.message, format) + + +func _on_broadcast_formatted_message_packet_received(packet: BroadcastFormattedMessagePacket): + var format := ChatMessageFormat.new() + format.color = packet.get_font_color() + format.size = packet.font_size + + add_message(packet.message, format) + + +class ChatMessageFormat extends Resource: + var color: Color + var size: int -- cgit v1.2.3