summaryrefslogtreecommitdiff
path: root/chat_window.gd
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-12-15 14:23:25 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-12-15 14:23:25 +0100
commita14e88ff4d0d87841a44254e2bff1784da6e8b48 (patch)
tree0507cb8117da7943805a6479aef4526edb2db4dd /chat_window.gd
parentca4d1945598863d0ce297f4272317e5dd5797f88 (diff)
next commit
Diffstat (limited to 'chat_window.gd')
-rw-r--r--chat_window.gd52
1 files changed, 48 insertions, 4 deletions
diff --git a/chat_window.gd b/chat_window.gd
index b8636c7..640a257 100644
--- a/chat_window.gd
+++ b/chat_window.gd
@@ -1,19 +1,63 @@
extends PanelContainer
-func add_message(message: String):
+@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)
- %ScrollContainer.set_deferred("scroll_vertical", %ScrollContainer.get_v_scroll_bar().max_value)
- # TODO: doesn't reach bottom?
+ 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 = ""
- #add_message(new_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