From 52419e2199aadf61ad9243852af58be88b584a06 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 5 Mar 2026 05:48:09 -0500 Subject: [PATCH] =?UTF-8?q?Chat=20module=20=E2=80=94=20overlay,=20friends,?= =?UTF-8?q?=20presence,=20DMs,=20context=20menus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Chat.gd autoload: data layer for channels, messages, social graph, presence, and notifications; wires to Network/Auth singletons - ChatOverlay.tscn + chat_overlay.gd: CanvasLayer (layer=100) that persists across all scenes; bubble toggle at bottom-right corner - Six tabs: Friends, Presence, Global, Lobby, Game, DMs - Message rows show [HH:MM] [source] Name: text with color-coded source badges (global/lobby/game/private/party/system) - Friends list: online/offline sections, right-click context menu (Private Message, Set Nickname, Block, Remove Friend) - Player name click: Add Friend, Invite to Party (stub), Mute/Unmute, Hi Scores (disabled placeholder) - Presence tab: own status selector (Online/Away/Offline) + friends presence overview with status dots - DMs tab: conversation sidebar + message pane, input bar shown when a conversation is selected - Unread notification badge on chat bubble - Per-session nickname overrides and muting Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Client/origin-client/autoloads/Chat.gd | 288 +++++ Client/origin-client/project.godot | 1 + .../scenes/chat/ChatOverlay.tscn | 51 + .../origin-client/scenes/chat/chat_overlay.gd | 1062 +++++++++++++++++ 4 files changed, 1402 insertions(+) create mode 100644 Client/origin-client/autoloads/Chat.gd create mode 100644 Client/origin-client/scenes/chat/ChatOverlay.tscn create mode 100644 Client/origin-client/scenes/chat/chat_overlay.gd diff --git a/Client/origin-client/autoloads/Chat.gd b/Client/origin-client/autoloads/Chat.gd new file mode 100644 index 0000000..703487c --- /dev/null +++ b/Client/origin-client/autoloads/Chat.gd @@ -0,0 +1,288 @@ +extends Node +## Chat autoload — manages all chat state and server communication. +## +## On auth_success, fetches channels, social graph, and notifications, +## then sets own presence to Online. Adds the ChatOverlay CanvasLayer to +## the viewport root so it persists across scene changes. +## +## Public signals let the overlay (and any other UI) react to changes. +## +## Usage: +## Chat.send_message(channel_id, "Hello!") +## Chat.open_dm(other_account_id) +## Chat.social_updated ← emit when friends / requests / blocked change + +# ── Signals ─────────────────────────────────────────────────────────────────── + +## Emitted when the channels array changes (initial load, DM opened, etc.) +signal channels_updated + +## Emitted for every server-pushed or locally echoed message. +## msg is a ChatMessageDto dictionary. +signal message_received(channel_id: String, msg: Dictionary) + +## Emitted after friends / pending_requests / blocked_users are refreshed. +signal social_updated + +## Emitted after the presence dictionary is updated. +signal presence_updated + +## Emitted after notifications array changes. +signal notifications_updated + +# ── State ───────────────────────────────────────────────────────────────────── + +## Array[Dictionary] — ChatChannelDto {id, name, channelType, createdAt} +var channels: Array = [] + +## Dictionary[channel_id → Array[Dictionary]] — messages newest-first +var messages: Dictionary = {} + +## Array[Dictionary] — FriendDto {accountId, displayName, friendsSince} +var friends: Array = [] + +## Array[Dictionary] — FriendRequestDto {requestId, otherAccountId, otherDisplayName, createdAt} +var pending_requests: Array = [] + +## Array[Dictionary] — BlockedUserDto {accountId, displayName, blockedAt} +var blocked_users: Array = [] + +## Dictionary[account_id → Dictionary] — PresenceDto +var presence: Dictionary = {} + +## Array[Dictionary] — NotificationDto +var notifications: Array = [] + +# ── Private ─────────────────────────────────────────────────────────────────── + +var _overlay: CanvasLayer = null + +# ── Lifecycle ───────────────────────────────────────────────────────────────── + +func _ready() -> void: + Auth.auth_success.connect(_on_auth_success) + Auth.session_expired.connect(_clear_state) + Network.message_received.connect(_on_server_push) + # Defer adding the overlay so the scene tree is fully ready. + call_deferred("_add_overlay") + + +func _add_overlay() -> void: + var scene := load("res://scenes/chat/ChatOverlay.tscn") as PackedScene + if scene == null: + push_error("Chat: could not load res://scenes/chat/ChatOverlay.tscn") + return + _overlay = scene.instantiate() as CanvasLayer + get_tree().get_root().add_child(_overlay) + +# ── Auth hooks ──────────────────────────────────────────────────────────────── + +func _on_auth_success(_uid: String, _dn: String) -> void: + _fetch_social() + _fetch_channels() + _fetch_notifications() + _set_own_presence("Online") + + +func _clear_state() -> void: + channels = [] + messages = {} + friends = [] + pending_requests = [] + blocked_users = [] + presence = {} + notifications = [] + channels_updated.emit() + social_updated.emit() + +# ── Public API ──────────────────────────────────────────────────────────────── + +func send_message(channel_id: String, content: String) -> void: + Network.send("chat.send", { "channelId": channel_id, "content": content.strip_edges() }) + + +func fetch_messages(channel_id: String, before_id: String = "") -> void: + var payload: Dictionary = { "channelId": channel_id } + if not before_id.is_empty(): + payload["beforeMessageId"] = before_id + Network.request("chat.get_messages", payload, + func(_t: String, p: Variant) -> void: + var msgs: Array = (p as Dictionary).get("messages", []) + if channel_id not in messages: + messages[channel_id] = [] + messages[channel_id].append_array(msgs) + message_received.emit(channel_id, {}) + , + func(_c: String, _m: String) -> void: pass + ) + + +## Open or retrieve the Direct channel with recipient_account_id. +## Emits channels_updated on success so the DMs tab can refresh. +func open_dm(recipient_account_id: String) -> void: + Network.request("chat.dm_open", { "recipientAccountId": recipient_account_id }, + func(_t: String, p: Variant) -> void: + var chan: Dictionary = (p as Dictionary).get("channel", {}) + if chan.is_empty(): + return + var cid: String = chan.get("id", "") + # Add to local list if not already present. + var found := false + for c: Dictionary in channels: + if c.get("id", "") == cid: + found = true + break + if not found: + channels.push_front(chan) + channels_updated.emit() + , + func(_c: String, _m: String) -> void: pass + ) + + +func send_friend_request(recipient_id: String) -> void: + Network.send("chat.friend_request", { "recipientAccountId": recipient_id }) + + +func respond_friend_request(request_id: String, accept: bool) -> void: + Network.request("chat.friend_respond", + { "requestId": request_id, "accept": accept }, + func(_t: String, _p: Variant) -> void: _fetch_social(), + func(_c: String, _m: String) -> void: pass + ) + + +func remove_friend(friend_account_id: String) -> void: + Network.request("chat.friend_remove", + { "friendAccountId": friend_account_id }, + func(_t: String, _p: Variant) -> void: _fetch_social(), + func(_c: String, _m: String) -> void: pass + ) + + +func block_user(target_account_id: String) -> void: + Network.request("chat.block", + { "targetAccountId": target_account_id }, + func(_t: String, _p: Variant) -> void: _fetch_social(), + func(_c: String, _m: String) -> void: pass + ) + + +func unblock_user(target_account_id: String) -> void: + Network.request("chat.unblock", + { "targetAccountId": target_account_id }, + func(_t: String, _p: Variant) -> void: _fetch_social(), + func(_c: String, _m: String) -> void: pass + ) + + +func set_presence(status: String) -> void: + _set_own_presence(status) + + +func fetch_presence(account_ids: Array) -> void: + if account_ids.is_empty(): + return + Network.request("chat.presence.get", { "accountIds": account_ids }, + func(_t: String, p: Variant) -> void: + for pr: Dictionary in (p as Dictionary).get("presences", []): + presence[pr.get("accountId", "")] = pr + presence_updated.emit() + , + func(_c: String, _m: String) -> void: pass + ) + + +func mark_notifications_read(ids: Array) -> void: + if ids.is_empty(): + return + Network.send("chat.notifications.read", { "notificationIds": ids }) + for n: Dictionary in notifications: + if n.get("id", "") in ids: + n["readAt"] = Time.get_datetime_string_from_system() + notifications_updated.emit() + + +func get_unread_notification_count() -> int: + return notifications.filter( + func(n: Dictionary) -> bool: return n.get("readAt") == null + ).size() + + +## Returns the first channel of the given ChannelType, or {} if none. +func get_channel_by_type(type_name: String) -> Dictionary: + for c: Dictionary in channels: + if c.get("channelType", "") == type_name: + return c + return {} + + +## Returns all Direct-type channels. +func get_dm_channels() -> Array: + return channels.filter( + func(c: Dictionary) -> bool: return c.get("channelType", "") == "Direct" + ) + +# ── Private helpers ─────────────────────────────────────────────────────────── + +func _set_own_presence(status: String) -> void: + Network.send("chat.presence.set", { "status": status }) + + +func _fetch_social() -> void: + Network.request("chat.get_social", {}, + func(_t: String, p: Variant) -> void: + var d := p as Dictionary + friends = d.get("friends", []) + pending_requests = d.get("pendingRequests", []) + blocked_users = d.get("blockedUsers", []) + social_updated.emit() + # Refresh presence for all friends. + var ids: Array = friends.map(func(f: Dictionary) -> String: + return f.get("accountId", "")) + fetch_presence(ids) + , + func(_c: String, _m: String) -> void: pass + ) + + +func _fetch_channels() -> void: + Network.request("chat.get_channels", {}, + func(_t: String, p: Variant) -> void: + channels = (p as Dictionary).get("channels", []) + channels_updated.emit() + , + func(_c: String, _m: String) -> void: pass + ) + + +func _fetch_notifications() -> void: + Network.request("chat.notifications.get", { "unreadOnly": false }, + func(_t: String, p: Variant) -> void: + notifications = (p as Dictionary).get("notifications", []) + notifications_updated.emit() + , + func(_c: String, _m: String) -> void: pass + ) + + +func _on_server_push(type: String, payload: Variant) -> void: + match type: + "chat.message.push": + var msg: Dictionary = (payload as Dictionary).get("message", {}) + var cid: String = msg.get("channelId", "") + if cid.is_empty(): + return + if cid not in messages: + messages[cid] = [] + messages[cid].push_front(msg) + message_received.emit(cid, msg) + + "chat.friend_request.push": + _fetch_social() + + "chat.notification.push": + var notif: Dictionary = (payload as Dictionary).get("notification", {}) + if not notif.is_empty(): + notifications.push_front(notif) + notifications_updated.emit() diff --git a/Client/origin-client/project.godot b/Client/origin-client/project.godot index 02fc691..f479882 100644 --- a/Client/origin-client/project.godot +++ b/Client/origin-client/project.godot @@ -21,6 +21,7 @@ Network="*res://autoloads/Network.gd" ContentCache="*res://autoloads/ContentCache.gd" Auth="*res://autoloads/Auth.gd" CollectionCache="*res://autoloads/CollectionCache.gd" +Chat="*res://autoloads/Chat.gd" [display] diff --git a/Client/origin-client/scenes/chat/ChatOverlay.tscn b/Client/origin-client/scenes/chat/ChatOverlay.tscn new file mode 100644 index 0000000..acf9874 --- /dev/null +++ b/Client/origin-client/scenes/chat/ChatOverlay.tscn @@ -0,0 +1,51 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://scenes/chat/chat_overlay.gd" id="1_overlay"] + +[node name="ChatOverlay" type="CanvasLayer"] +layer = 100 +script = ExtResource("1_overlay") + +[node name="ChatBubble" type="Button" parent="."] +layout_mode = 0 +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -76.0 +offset_top = -76.0 +offset_right = -16.0 +offset_bottom = -16.0 +grow_horizontal = 0 +grow_vertical = 0 +text = "Chat" +theme_override_font_sizes/font_size = 12 + +[node name="BadgeLabel" type="Label" parent="ChatBubble"] +layout_mode = 0 +anchor_left = 1.0 +anchor_top = 0.0 +anchor_right = 1.0 +anchor_bottom = 0.0 +offset_left = -18.0 +offset_top = -8.0 +offset_right = 8.0 +offset_bottom = 12.0 +visible = false +text = "" +horizontal_alignment = 1 +theme_override_font_sizes/font_size = 10 + +[node name="ChatPanel" type="PanelContainer" parent="."] +visible = false +layout_mode = 0 +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -356.0 +offset_top = -600.0 +offset_right = -16.0 +offset_bottom = -84.0 +grow_horizontal = 0 +grow_vertical = 0 diff --git a/Client/origin-client/scenes/chat/chat_overlay.gd b/Client/origin-client/scenes/chat/chat_overlay.gd new file mode 100644 index 0000000..0e03da5 --- /dev/null +++ b/Client/origin-client/scenes/chat/chat_overlay.gd @@ -0,0 +1,1062 @@ +extends CanvasLayer +## Chat overlay — persists across all scenes as a child of the viewport root. +## +## Structure (built programmatically in _build_panel): +## ChatBubble (Button, bottom-right) — toggles the panel +## ChatPanel (PanelContainer) +## VBox +## Header — title + close button +## TabBar — Friends | Presence | Global | Lobby | Game | DMs +## ContentArea — one VBox per tab, only active one is visible +## InputBar — LineEdit + Send (hidden on Friends/Presence tabs) +## +## Message row format: [HH:MM] [source] SenderName: message text +## Player name is clickable → context menu (Add Friend, Invite, Mute, Hi Scores) +## Friends list supports right-click → (Private Msg, Nickname, Block, Remove) + +# ── Tab enum ────────────────────────────────────────────────────────────────── + +enum Tab { FRIENDS, PRESENCE, GLOBAL, LOBBY, GAME, DMS } + +const TAB_LABELS := ["Friends", "Presence", "Global", "Lobby", "Game", "DMs"] + +# ── Colors ──────────────────────────────────────────────────────────────────── + +const ACCENT_COLOR := Color(0.27, 0.47, 0.80, 1.0) +const MUTED_COLOR := Color(0.52, 0.52, 0.58, 1.0) +const ONLINE_COLOR := Color(0.30, 0.88, 0.42, 1.0) +const AWAY_COLOR := Color(1.00, 0.76, 0.20, 1.0) +const INMATCH_COLOR := Color(0.40, 0.65, 1.00, 1.0) +const OFFLINE_COLOR := Color(0.38, 0.38, 0.43, 1.0) +const DANGER_COLOR := Color(1.00, 0.38, 0.38, 1.0) + +const SOURCE_COLORS := { + "global": Color(0.55, 0.82, 1.00), + "lobby": Color(0.90, 0.78, 0.40), + "game": Color(1.00, 0.55, 0.32), + "private": Color(0.88, 0.55, 1.00), + "party": Color(0.42, 1.00, 0.58), + "system": Color(0.82, 0.82, 0.88), +} + +# ── Node refs (from tscn) ───────────────────────────────────────────────────── + +@onready var _bubble : Button = $ChatBubble +@onready var _badge : Label = $ChatBubble/BadgeLabel +@onready var _panel : PanelContainer = $ChatPanel + +# ── Built programmatically ──────────────────────────────────────────────────── + +var _tab_buttons : Array[Button] = [] +var _content_nodes : Array[Control] = [] # one entry per Tab enum value + +var _input_bar : HBoxContainer = null +var _message_input : LineEdit = null + +# Per-tab content refs +var _friends_list : VBoxContainer = null +var _requests_section : Control = null +var _requests_list : VBoxContainer = null +var _presence_list : VBoxContainer = null +var _presence_option : OptionButton = null +var _global_list : VBoxContainer = null +var _lobby_list : VBoxContainer = null +var _game_list : VBoxContainer = null +var _dm_conv_list : VBoxContainer = null +var _dm_msg_list : VBoxContainer = null + +# ── State ───────────────────────────────────────────────────────────────────── + +var _current_tab : Tab = Tab.FRIENDS +var _active_channel : String = "" # channel_id used by the input bar +var _active_dm : String = "" # channel_id of the open DM conversation + +var _nicknames : Dictionary = {} # account_id → custom display name +var _muted : Dictionary = {} # account_id → true + +# ── Lifecycle ───────────────────────────────────────────────────────────────── + +func _ready() -> void: + _build_panel() + _style_bubble() + _connect_chat_signals() + _bubble.pressed.connect(_toggle_panel) + + +func _connect_chat_signals() -> void: + Chat.channels_updated.connect(_on_channels_updated) + Chat.message_received.connect(_on_message_received) + Chat.social_updated.connect(_on_social_updated) + Chat.presence_updated.connect(_on_presence_updated) + Chat.notifications_updated.connect(_refresh_badge) + + +func _style_bubble() -> void: + var s := StyleBoxFlat.new() + s.bg_color = ACCENT_COLOR + s.corner_radius_top_left = 8 + s.corner_radius_top_right = 8 + s.corner_radius_bottom_left = 8 + s.corner_radius_bottom_right = 8 + _bubble.add_theme_stylebox_override("normal", s) + + var h := s.duplicate() as StyleBoxFlat + h.bg_color = Color(ACCENT_COLOR.r + 0.08, ACCENT_COLOR.g + 0.08, ACCENT_COLOR.b + 0.08) + _bubble.add_theme_stylebox_override("hover", h) + _bubble.add_theme_stylebox_override("pressed", h) + + # Badge background + var badge_style := StyleBoxFlat.new() + badge_style.bg_color = DANGER_COLOR + badge_style.corner_radius_top_left = 6 + badge_style.corner_radius_top_right = 6 + badge_style.corner_radius_bottom_left = 6 + badge_style.corner_radius_bottom_right = 6 + _badge.add_theme_stylebox_override("normal", badge_style) + +# ── Build panel ─────────────────────────────────────────────────────────────── + +func _build_panel() -> void: + var root_vbox := VBoxContainer.new() + root_vbox.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) + root_vbox.add_theme_constant_override("separation", 0) + _panel.add_child(root_vbox) + + root_vbox.add_child(_make_header()) + root_vbox.add_child(_make_tab_bar()) + root_vbox.add_child(_make_separator()) + + # Content area — child controls fill it; only one is visible at a time. + var content_area := Control.new() + content_area.size_flags_vertical = Control.SIZE_EXPAND_FILL + root_vbox.add_child(content_area) + + for i in TAB_LABELS.size(): + var tab_node := _build_tab_content(i as Tab) + tab_node.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) + tab_node.visible = false + content_area.add_child(tab_node) + _content_nodes.append(tab_node) + + root_vbox.add_child(_make_separator()) + root_vbox.add_child(_make_input_bar()) + + _show_tab(Tab.FRIENDS) + + +func _make_separator() -> HSeparator: + return HSeparator.new() + + +func _make_header() -> Control: + var margin := MarginContainer.new() + margin.add_theme_constant_override("margin_left", 12) + margin.add_theme_constant_override("margin_right", 8) + margin.add_theme_constant_override("margin_top", 6) + margin.add_theme_constant_override("margin_bottom", 6) + + var hbox := HBoxContainer.new() + hbox.add_theme_constant_override("separation", 8) + hbox.size_flags_vertical = Control.SIZE_SHRINK_CENTER + margin.add_child(hbox) + + var title := Label.new() + title.text = "Chat" + title.add_theme_font_size_override("font_size", 15) + title.size_flags_horizontal = Control.SIZE_EXPAND_FILL + hbox.add_child(title) + + var close_btn := Button.new() + close_btn.text = "✕" + close_btn.flat = true + close_btn.add_theme_font_size_override("font_size", 14) + close_btn.pressed.connect(func() -> void: _panel.visible = false) + hbox.add_child(close_btn) + + return margin + + +func _make_tab_bar() -> Control: + var hbox := HBoxContainer.new() + hbox.add_theme_constant_override("separation", 0) + hbox.custom_minimum_size = Vector2(0, 28) + + for i in TAB_LABELS.size(): + var btn := Button.new() + btn.text = TAB_LABELS[i] + btn.size_flags_horizontal = Control.SIZE_EXPAND_FILL + btn.flat = true + btn.add_theme_font_size_override("font_size", 11) + var idx := i + btn.pressed.connect(func() -> void: _show_tab(idx as Tab)) + _tab_buttons.append(btn) + hbox.add_child(btn) + + return hbox + + +func _make_input_bar() -> Control: + _input_bar = HBoxContainer.new() + _input_bar.add_theme_constant_override("separation", 0) + _input_bar.visible = false + + var margin := MarginContainer.new() + margin.add_theme_constant_override("margin_left", 8) + margin.add_theme_constant_override("margin_right", 8) + margin.add_theme_constant_override("margin_top", 6) + margin.add_theme_constant_override("margin_bottom", 6) + margin.size_flags_horizontal = Control.SIZE_EXPAND_FILL + _input_bar.add_child(margin) + + var inner := HBoxContainer.new() + inner.add_theme_constant_override("separation", 6) + inner.size_flags_horizontal = Control.SIZE_EXPAND_FILL + margin.add_child(inner) + + _message_input = LineEdit.new() + _message_input.placeholder_text = "Type a message…" + _message_input.size_flags_horizontal = Control.SIZE_EXPAND_FILL + _message_input.add_theme_font_size_override("font_size", 13) + _message_input.max_length = 500 + _message_input.text_submitted.connect(_on_send) + inner.add_child(_message_input) + + var send_btn := Button.new() + send_btn.text = "Send" + send_btn.custom_minimum_size = Vector2(52, 0) + send_btn.add_theme_font_size_override("font_size", 12) + send_btn.pressed.connect(func() -> void: _on_send(_message_input.text)) + inner.add_child(send_btn) + + return _input_bar + +# ── Tab builders ────────────────────────────────────────────────────────────── + +func _build_tab_content(tab: Tab) -> Control: + match tab: + Tab.FRIENDS: return _build_friends_tab() + Tab.PRESENCE: return _build_presence_tab() + Tab.GLOBAL: return _build_channel_tab(Tab.GLOBAL) + Tab.LOBBY: return _build_channel_tab(Tab.LOBBY) + Tab.GAME: return _build_channel_tab(Tab.GAME) + Tab.DMS: return _build_dms_tab() + return Control.new() + + +func _build_friends_tab() -> Control: + var vbox := VBoxContainer.new() + vbox.add_theme_constant_override("separation", 0) + + # ── Pending requests (hidden when empty) ────────────────────────────────── + _requests_section = VBoxContainer.new() + _requests_section.visible = false + vbox.add_child(_requests_section) + + var req_margin := MarginContainer.new() + req_margin.add_theme_constant_override("margin_left", 10) + req_margin.add_theme_constant_override("margin_top", 6) + req_margin.add_theme_constant_override("margin_bottom", 2) + _requests_section.add_child(req_margin) + + var req_header := Label.new() + req_header.text = "FRIEND REQUESTS" + req_header.add_theme_color_override("font_color", MUTED_COLOR) + req_header.add_theme_font_size_override("font_size", 10) + req_margin.add_child(req_header) + + _requests_list = VBoxContainer.new() + _requests_list.add_theme_constant_override("separation", 0) + _requests_section.add_child(_requests_list) + + _requests_section.add_child(_make_separator()) + + # ── Friends scroll ──────────────────────────────────────────────────────── + var scroll := ScrollContainer.new() + scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL + scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + vbox.add_child(scroll) + + _friends_list = VBoxContainer.new() + _friends_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL + _friends_list.add_theme_constant_override("separation", 0) + scroll.add_child(_friends_list) + + return vbox + + +func _build_presence_tab() -> Control: + var vbox := VBoxContainer.new() + vbox.add_theme_constant_override("separation", 0) + + # ── Own status selector ─────────────────────────────────────────────────── + var sm := MarginContainer.new() + sm.add_theme_constant_override("margin_left", 12) + sm.add_theme_constant_override("margin_right", 12) + sm.add_theme_constant_override("margin_top", 8) + sm.add_theme_constant_override("margin_bottom", 8) + vbox.add_child(sm) + + var shbox := HBoxContainer.new() + shbox.add_theme_constant_override("separation", 8) + shbox.size_flags_vertical = Control.SIZE_SHRINK_CENTER + sm.add_child(shbox) + + var slbl := Label.new() + slbl.text = "My Status:" + slbl.add_theme_font_size_override("font_size", 13) + slbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL + shbox.add_child(slbl) + + _presence_option = OptionButton.new() + _presence_option.add_item("● Online", 0) + _presence_option.add_item("◐ Away", 1) + _presence_option.add_item("○ Offline", 2) + _presence_option.selected = 0 + _presence_option.add_theme_font_size_override("font_size", 12) + _presence_option.item_selected.connect(_on_own_presence_changed) + shbox.add_child(_presence_option) + + vbox.add_child(_make_separator()) + + # ── Online friends list ─────────────────────────────────────────────────── + var lm := MarginContainer.new() + lm.add_theme_constant_override("margin_left", 10) + lm.add_theme_constant_override("margin_top", 6) + lm.add_theme_constant_override("margin_bottom", 2) + vbox.add_child(lm) + + var llbl := Label.new() + llbl.text = "FRIENDS PRESENCE" + llbl.add_theme_color_override("font_color", MUTED_COLOR) + llbl.add_theme_font_size_override("font_size", 10) + lm.add_child(llbl) + + var scroll := ScrollContainer.new() + scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL + scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + vbox.add_child(scroll) + + _presence_list = VBoxContainer.new() + _presence_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL + _presence_list.add_theme_constant_override("separation", 0) + scroll.add_child(_presence_list) + + return vbox + + +func _build_channel_tab(tab: Tab) -> Control: + var vbox := VBoxContainer.new() + vbox.add_theme_constant_override("separation", 0) + + var scroll := ScrollContainer.new() + scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL + scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + vbox.add_child(scroll) + + var outer_margin := MarginContainer.new() + outer_margin.size_flags_horizontal = Control.SIZE_EXPAND_FILL + outer_margin.add_theme_constant_override("margin_left", 8) + outer_margin.add_theme_constant_override("margin_right", 8) + outer_margin.add_theme_constant_override("margin_top", 4) + outer_margin.add_theme_constant_override("margin_bottom", 4) + scroll.add_child(outer_margin) + + var msg_list := VBoxContainer.new() + msg_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL + msg_list.add_theme_constant_override("separation", 2) + outer_margin.add_child(msg_list) + + match tab: + Tab.GLOBAL: _global_list = msg_list + Tab.LOBBY: _lobby_list = msg_list + Tab.GAME: _game_list = msg_list + + return vbox + + +func _build_dms_tab() -> Control: + var hbox := HBoxContainer.new() + hbox.add_theme_constant_override("separation", 0) + + # ── Left: conversation list ─────────────────────────────────────────────── + var left_vbox := VBoxContainer.new() + left_vbox.custom_minimum_size = Vector2(110, 0) + left_vbox.add_theme_constant_override("separation", 0) + hbox.add_child(left_vbox) + + var conv_scroll := ScrollContainer.new() + conv_scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL + conv_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + left_vbox.add_child(conv_scroll) + + _dm_conv_list = VBoxContainer.new() + _dm_conv_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL + _dm_conv_list.add_theme_constant_override("separation", 0) + conv_scroll.add_child(_dm_conv_list) + + # ── Vertical divider ────────────────────────────────────────────────────── + hbox.add_child(VSeparator.new()) + + # ── Right: message area ─────────────────────────────────────────────────── + var right_vbox := VBoxContainer.new() + right_vbox.size_flags_horizontal = Control.SIZE_EXPAND_FILL + right_vbox.add_theme_constant_override("separation", 0) + hbox.add_child(right_vbox) + + var msg_scroll := ScrollContainer.new() + msg_scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL + msg_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED + right_vbox.add_child(msg_scroll) + + var msg_margin := MarginContainer.new() + msg_margin.size_flags_horizontal = Control.SIZE_EXPAND_FILL + msg_margin.add_theme_constant_override("margin_left", 8) + msg_margin.add_theme_constant_override("margin_right", 8) + msg_margin.add_theme_constant_override("margin_top", 4) + msg_margin.add_theme_constant_override("margin_bottom", 4) + msg_scroll.add_child(msg_margin) + + _dm_msg_list = VBoxContainer.new() + _dm_msg_list.size_flags_horizontal = Control.SIZE_EXPAND_FILL + _dm_msg_list.add_theme_constant_override("separation", 2) + msg_margin.add_child(_dm_msg_list) + + return hbox + +# ── Tab switching ───────────────────────────────────────────────────────────── + +func _show_tab(tab: Tab) -> void: + _current_tab = tab + + for i in _content_nodes.size(): + _content_nodes[i].visible = (i == tab) + + for i in _tab_buttons.size(): + _apply_tab_btn_style(_tab_buttons[i], i == tab) + + # Input bar visibility: show on chat tabs, or DMs when a convo is selected. + match tab: + Tab.FRIENDS, Tab.PRESENCE: + _input_bar.visible = false + Tab.GLOBAL, Tab.LOBBY, Tab.GAME: + _input_bar.visible = true + Tab.DMS: + _input_bar.visible = not _active_dm.is_empty() + + # Populate content. + match tab: + Tab.FRIENDS: + _refresh_friends() + Tab.PRESENCE: + _refresh_presence() + Tab.GLOBAL: + _activate_channel_tab("Global", Tab.GLOBAL) + Tab.LOBBY: + _activate_channel_tab("Guild", Tab.LOBBY) + Tab.GAME: + _activate_channel_tab("Match", Tab.GAME) + Tab.DMS: + _refresh_dm_list() + if not _active_dm.is_empty(): + _refresh_dm_messages() + + +func _apply_tab_btn_style(btn: Button, active: bool) -> void: + var s := StyleBoxFlat.new() + s.bg_color = Color(ACCENT_COLOR.r, ACCENT_COLOR.g, ACCENT_COLOR.b, 0.25) \ + if active else Color(0, 0, 0, 0) + s.border_width_bottom = 2 if active else 0 + s.border_color = Color(0.5, 0.72, 1.0) + btn.add_theme_stylebox_override("normal", s) + btn.add_theme_stylebox_override("hover", s) + btn.add_theme_stylebox_override("pressed", s) + btn.add_theme_color_override("font_color", + Color(1, 1, 1) if active else MUTED_COLOR) + + +func _activate_channel_tab(channel_type: String, tab: Tab) -> void: + var chan := Chat.get_channel_by_type(channel_type) + var cid : String = chan.get("id", "") + _active_channel = cid + + if not cid.is_empty() and cid not in Chat.messages: + Chat.fetch_messages(cid) + + _refresh_channel_messages(tab) + +# ── Toggle ──────────────────────────────────────────────────────────────────── + +func _toggle_panel() -> void: + _panel.visible = not _panel.visible + if _panel.visible: + _show_tab(_current_tab) + _refresh_badge() + +# ── Refresh: Friends ────────────────────────────────────────────────────────── + +func _refresh_friends() -> void: + if not is_instance_valid(_friends_list): + return + + for c in _friends_list.get_children(): + c.queue_free() + + # Pending requests section. + if is_instance_valid(_requests_section): + _requests_section.visible = not Chat.pending_requests.is_empty() + if is_instance_valid(_requests_list): + for c in _requests_list.get_children(): + c.queue_free() + for req: Dictionary in Chat.pending_requests: + _requests_list.add_child(_make_request_row(req)) + + if Chat.friends.is_empty(): + _friends_list.add_child(_make_placeholder("No friends yet.")) + return + + var online_friends := Chat.friends.filter(func(f: Dictionary) -> bool: + return Chat.presence.get(f.get("accountId", ""), {}).get("status", "Offline") != "Offline") + var offline_friends := Chat.friends.filter(func(f: Dictionary) -> bool: + return Chat.presence.get(f.get("accountId", ""), {}).get("status", "Offline") == "Offline") + + if not online_friends.is_empty(): + _friends_list.add_child(_make_section_label("ONLINE — %d" % online_friends.size())) + for f: Dictionary in online_friends: + _friends_list.add_child(_make_friend_row(f)) + + if not offline_friends.is_empty(): + _friends_list.add_child(_make_section_label("OFFLINE — %d" % offline_friends.size())) + for f: Dictionary in offline_friends: + _friends_list.add_child(_make_friend_row(f)) + + +func _make_friend_row(f: Dictionary) -> Control: + var account_id : String = f.get("accountId", "") + var raw_name : String = f.get("displayName", "Unknown") + var label_name : String = _nicknames.get(account_id, raw_name) + var pr : Dictionary = Chat.presence.get(account_id, {}) + var status : String = pr.get("status", "Offline") + + var btn := Button.new() + btn.flat = true + btn.alignment = HORIZONTAL_ALIGNMENT_LEFT + btn.custom_minimum_size = Vector2(0, 34) + btn.add_theme_font_size_override("font_size", 13) + btn.text = "%s %s" % [_presence_dot(status), label_name] + btn.add_theme_color_override("font_color", _presence_color(status)) + + # Left-click → open DM and jump to DMs tab. + btn.pressed.connect(func() -> void: + Chat.open_dm(account_id) + _show_tab(Tab.DMS)) + + # Right-click → context menu. + btn.gui_input.connect(func(ev: InputEvent) -> void: + if ev is InputEventMouseButton: + var mb := ev as InputEventMouseButton + if mb.button_index == MOUSE_BUTTON_RIGHT and mb.pressed: + _show_friend_context_menu(f)) + + return btn + + +func _make_request_row(req: Dictionary) -> Control: + var dn : String = req.get("otherDisplayName", "Unknown") + var rid : String = req.get("requestId", "") + + var hbox := HBoxContainer.new() + hbox.add_theme_constant_override("separation", 4) + hbox.custom_minimum_size = Vector2(0, 36) + + var m := MarginContainer.new() + m.add_theme_constant_override("margin_left", 10) + m.add_theme_constant_override("margin_right", 4) + m.size_flags_horizontal = Control.SIZE_EXPAND_FILL + hbox.add_child(m) + + var lbl := Label.new() + lbl.text = dn + lbl.size_flags_vertical = Control.SIZE_SHRINK_CENTER + lbl.add_theme_font_size_override("font_size", 12) + m.add_child(lbl) + + var acc := Button.new() + acc.text = "✓" + acc.custom_minimum_size = Vector2(30, 0) + acc.add_theme_color_override("font_color", ONLINE_COLOR) + acc.pressed.connect(func() -> void: Chat.respond_friend_request(rid, true)) + hbox.add_child(acc) + + var dec := Button.new() + dec.text = "✕" + dec.custom_minimum_size = Vector2(30, 0) + dec.add_theme_color_override("font_color", DANGER_COLOR) + dec.pressed.connect(func() -> void: Chat.respond_friend_request(rid, false)) + hbox.add_child(dec) + + return hbox + +# ── Refresh: Presence ───────────────────────────────────────────────────────── + +func _refresh_presence() -> void: + if not is_instance_valid(_presence_list): + return + + for c in _presence_list.get_children(): + c.queue_free() + + if Chat.friends.is_empty(): + _presence_list.add_child(_make_placeholder("No friends to show.")) + return + + for f: Dictionary in Chat.friends: + var aid : String = f.get("accountId", "") + var dn : String = _nicknames.get(aid, f.get("displayName", "Unknown")) + var pr : Dictionary = Chat.presence.get(aid, {}) + var status : String = pr.get("status", "Offline") + + var row := HBoxContainer.new() + row.add_theme_constant_override("separation", 8) + row.custom_minimum_size = Vector2(0, 30) + + var m := MarginContainer.new() + m.add_theme_constant_override("margin_left", 10) + m.size_flags_horizontal = Control.SIZE_EXPAND_FILL + row.add_child(m) + + var inner := HBoxContainer.new() + inner.add_theme_constant_override("separation", 6) + inner.size_flags_vertical = Control.SIZE_SHRINK_CENTER + m.add_child(inner) + + var dot := Label.new() + dot.text = _presence_dot(status) + dot.add_theme_color_override("font_color", _presence_color(status)) + dot.add_theme_font_size_override("font_size", 12) + inner.add_child(dot) + + var name_lbl := Label.new() + name_lbl.text = dn + name_lbl.add_theme_font_size_override("font_size", 12) + name_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL + inner.add_child(name_lbl) + + var status_lbl := Label.new() + status_lbl.text = status + status_lbl.add_theme_color_override("font_color", _presence_color(status)) + status_lbl.add_theme_font_size_override("font_size", 10) + row.add_child(status_lbl) + + var rm := MarginContainer.new() + rm.add_theme_constant_override("margin_right", 10) + rm.add_child(status_lbl) + row.add_child(rm) + + _presence_list.add_child(row) + +# ── Refresh: Channel messages ───────────────────────────────────────────────── + +func _refresh_channel_messages(tab: Tab) -> void: + var msg_list : VBoxContainer + match tab: + Tab.GLOBAL: msg_list = _global_list + Tab.LOBBY: msg_list = _lobby_list + Tab.GAME: msg_list = _game_list + _: return + + if not is_instance_valid(msg_list): + return + + for c in msg_list.get_children(): + c.queue_free() + + if _active_channel.is_empty(): + msg_list.add_child(_make_placeholder( + "No %s channel available." % TAB_LABELS[tab])) + _input_bar.visible = false + return + + _input_bar.visible = true + var source := _tab_source(tab) + var msgs : Array = Chat.messages.get(_active_channel, []) + + # Server returns newest-first → display oldest-first. + for i in range(msgs.size() - 1, -1, -1): + var row := _make_message_row(msgs[i] as Dictionary, source) + if row != null: + msg_list.add_child(row) + + +func _tab_source(tab: Tab) -> String: + match tab: + Tab.GLOBAL: return "global" + Tab.LOBBY: return "lobby" + Tab.GAME: return "game" + return "" + +# ── Refresh: DMs ────────────────────────────────────────────────────────────── + +func _refresh_dm_list() -> void: + if not is_instance_valid(_dm_conv_list): + return + + for c in _dm_conv_list.get_children(): + c.queue_free() + + var dms := Chat.get_dm_channels() + if dms.is_empty(): + _dm_conv_list.add_child(_make_placeholder("No conversations.")) + return + + for chan: Dictionary in dms: + var cid : String = chan.get("id", "") + var name : String = chan.get("name", "DM") + + var btn := Button.new() + btn.text = name + btn.flat = true + btn.alignment = HORIZONTAL_ALIGNMENT_LEFT + btn.custom_minimum_size = Vector2(0, 36) + btn.add_theme_font_size_override("font_size", 11) + btn.clip_text = true + + if cid == _active_dm: + _apply_tab_btn_style(btn, true) + + btn.pressed.connect(func() -> void: _select_dm(cid)) + _dm_conv_list.add_child(btn) + + +func _select_dm(channel_id: String) -> void: + _active_dm = channel_id + _active_channel = channel_id + _input_bar.visible = true + + if channel_id not in Chat.messages: + Chat.fetch_messages(channel_id) + + _refresh_dm_messages() + _refresh_dm_list() + + +func _refresh_dm_messages() -> void: + if not is_instance_valid(_dm_msg_list): + return + + for c in _dm_msg_list.get_children(): + c.queue_free() + + if _active_dm.is_empty(): + return + + var msgs : Array = Chat.messages.get(_active_dm, []) + for i in range(msgs.size() - 1, -1, -1): + var row := _make_message_row(msgs[i] as Dictionary, "private") + if row != null: + _dm_msg_list.add_child(row) + +# ── Badge ───────────────────────────────────────────────────────────────────── + +func _refresh_badge() -> void: + var count := Chat.get_unread_notification_count() + _badge.visible = count > 0 + _badge.text = str(count) if count < 100 else "99+" + +# ── Message row ─────────────────────────────────────────────────────────────── + +## Returns a Control row for a single chat message, or null if the sender is muted. +## Format: [HH:MM] [source] SenderName: message text +func _make_message_row(msg: Dictionary, source: String) -> Control: + var account_id : String = msg.get("senderAccountId", "") + + if _muted.has(account_id): + return null + + var outer := MarginContainer.new() + outer.add_theme_constant_override("margin_top", 1) + outer.add_theme_constant_override("margin_bottom", 1) + + var hbox := HBoxContainer.new() + hbox.add_theme_constant_override("separation", 4) + outer.add_child(hbox) + + # Timestamp + var ts := Label.new() + ts.text = _format_time(msg.get("sentAt", "")) + ts.add_theme_color_override("font_color", Color(0.42, 0.42, 0.48)) + ts.add_theme_font_size_override("font_size", 10) + ts.custom_minimum_size = Vector2(32, 0) + ts.size_flags_vertical = Control.SIZE_SHRINK_BEGIN + hbox.add_child(ts) + + # Source badge + if not source.is_empty(): + var src := Label.new() + src.text = "[%s]" % source + src.add_theme_color_override("font_color", + SOURCE_COLORS.get(source, MUTED_COLOR)) + src.add_theme_font_size_override("font_size", 10) + src.size_flags_vertical = Control.SIZE_SHRINK_BEGIN + hbox.add_child(src) + + # Sender name (clickable button) + var raw_name : String = msg.get("senderDisplayName", "Unknown") + var show_name : String = _nicknames.get(account_id, raw_name) + var name_btn := Button.new() + name_btn.text = show_name + name_btn.flat = true + name_btn.add_theme_color_override("font_color", Color(0.75, 0.85, 1.0)) + name_btn.add_theme_font_size_override("font_size", 13) + name_btn.size_flags_vertical = Control.SIZE_SHRINK_BEGIN + name_btn.pressed.connect(func() -> void: + _show_player_menu(account_id, raw_name)) + hbox.add_child(name_btn) + + # Colon separator + var colon := Label.new() + colon.text = ":" + colon.add_theme_color_override("font_color", MUTED_COLOR) + colon.size_flags_vertical = Control.SIZE_SHRINK_BEGIN + hbox.add_child(colon) + + # Message content + var body := Label.new() + body.text = msg.get("content", "") + body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + body.size_flags_horizontal = Control.SIZE_EXPAND_FILL + body.add_theme_font_size_override("font_size", 13) + hbox.add_child(body) + + return outer + +# ── Context menus ───────────────────────────────────────────────────────────── + +func _show_friend_context_menu(friend: Dictionary) -> void: + var account_id : String = friend.get("accountId", "") + var dn : String = friend.get("displayName", "Unknown") + + var menu := PopupMenu.new() + add_child(menu) + menu.add_item("Private Message", 0) + menu.add_item("Set Nickname", 1) + menu.add_separator() + menu.add_item("Block", 2) + menu.add_item("Remove Friend", 3) + + menu.id_pressed.connect(func(id: int) -> void: + _on_friend_menu_action(id, account_id, dn) + menu.queue_free()) + menu.popup_hide.connect(func() -> void: menu.queue_free()) + + menu.position = DisplayServer.mouse_get_position() + menu.reset_size() + menu.popup() + + +func _on_friend_menu_action(id: int, account_id: String, dn: String) -> void: + match id: + 0: # Private Message + Chat.open_dm(account_id) + _show_tab(Tab.DMS) + 1: # Set Nickname + _prompt_nickname(account_id, dn) + 2: # Block + Chat.block_user(account_id) + 3: # Remove Friend + Chat.remove_friend(account_id) + + +func _show_player_menu(account_id: String, display_name: String) -> void: + if account_id == Auth.user_id: + return + + var is_friend := Chat.friends.any(func(f: Dictionary) -> bool: + return f.get("accountId", "") == account_id) + + var menu := PopupMenu.new() + add_child(menu) + + if not is_friend: + menu.add_item("Add Friend", 0) + menu.add_item("Invite to Party", 1) + menu.add_item("Private Message", 2) + if not _muted.has(account_id): + menu.add_item("Mute", 3) + else: + menu.add_item("Unmute", 4) + menu.add_separator() + menu.add_item("Hi Scores", 5) + menu.set_item_disabled(menu.item_count - 1, true) + + menu.id_pressed.connect(func(id: int) -> void: + _on_player_menu_action(id, account_id, display_name) + menu.queue_free()) + menu.popup_hide.connect(func() -> void: menu.queue_free()) + + menu.position = DisplayServer.mouse_get_position() + menu.reset_size() + menu.popup() + + +func _on_player_menu_action(id: int, account_id: String, _dn: String) -> void: + match id: + 0: # Add Friend + Chat.send_friend_request(account_id) + 1: # Invite to Party (stub — party system not yet implemented) + pass + 2: # Private Message + Chat.open_dm(account_id) + _show_tab(Tab.DMS) + 3: # Mute + _muted[account_id] = true + 4: # Unmute + _muted.erase(account_id) + + +func _prompt_nickname(account_id: String, display_name: String) -> void: + var dialog := AcceptDialog.new() + dialog.title = "Nickname for %s" % display_name + dialog.min_size = Vector2(280, 100) + + var vbox := VBoxContainer.new() + vbox.add_theme_constant_override("separation", 8) + var input := LineEdit.new() + input.placeholder_text = "Leave blank to clear nickname" + input.text = _nicknames.get(account_id, "") + input.max_length = 32 + vbox.add_child(input) + dialog.add_child(vbox) + + add_child(dialog) + dialog.popup_centered() + + dialog.confirmed.connect(func() -> void: + var nick := input.text.strip_edges() + if nick.is_empty(): + _nicknames.erase(account_id) + else: + _nicknames[account_id] = nick + dialog.queue_free() + if _current_tab == Tab.FRIENDS: + _refresh_friends()) + + dialog.canceled.connect(func() -> void: dialog.queue_free()) + +# ── Send ────────────────────────────────────────────────────────────────────── + +func _on_send(text: String) -> void: + var trimmed := text.strip_edges() + if trimmed.is_empty() or _active_channel.is_empty(): + return + Chat.send_message(_active_channel, trimmed) + _message_input.clear() + +# ── Signal handlers (Chat autoload) ─────────────────────────────────────────── + +func _on_channels_updated() -> void: + if not _panel.visible: + return + match _current_tab: + Tab.GLOBAL: _activate_channel_tab("Global", Tab.GLOBAL) + Tab.LOBBY: _activate_channel_tab("Guild", Tab.LOBBY) + Tab.GAME: _activate_channel_tab("Match", Tab.GAME) + Tab.DMS: _refresh_dm_list() + + +func _on_message_received(channel_id: String, _msg: Dictionary) -> void: + if not _panel.visible: + _refresh_badge() + return + + match _current_tab: + Tab.GLOBAL: + if Chat.get_channel_by_type("Global").get("id", "") == channel_id: + _refresh_channel_messages(Tab.GLOBAL) + Tab.LOBBY: + if Chat.get_channel_by_type("Guild").get("id", "") == channel_id: + _refresh_channel_messages(Tab.LOBBY) + Tab.GAME: + if Chat.get_channel_by_type("Match").get("id", "") == channel_id: + _refresh_channel_messages(Tab.GAME) + Tab.DMS: + if channel_id == _active_dm: + _refresh_dm_messages() + + +func _on_social_updated() -> void: + if not _panel.visible: + return + match _current_tab: + Tab.FRIENDS: _refresh_friends() + Tab.PRESENCE: _refresh_presence() + + +func _on_presence_updated() -> void: + if not _panel.visible: + return + match _current_tab: + Tab.FRIENDS: _refresh_friends() + Tab.PRESENCE: _refresh_presence() + + +func _on_own_presence_changed(index: int) -> void: + const STATUS_MAP := ["Online", "Away", "Offline"] + Chat.set_presence(STATUS_MAP[index]) + +# ── Helpers ─────────────────────────────────────────────────────────────────── + +func _presence_dot(status: String) -> String: + match status: + "Online": return "●" + "InMatch": return "◆" + "Away": return "◐" + return "○" + + +func _presence_color(status: String) -> Color: + match status: + "Online": return ONLINE_COLOR + "InMatch": return INMATCH_COLOR + "Away": return AWAY_COLOR + return OFFLINE_COLOR + + +## Parse "YYYY-MM-DDTHH:MM:SS..." → "HH:MM" +func _format_time(iso: String) -> String: + var t_idx := iso.find("T") + if t_idx < 0: + return "" + var time_part := iso.substr(t_idx + 1) + var colon_idx := time_part.find(":") + if colon_idx < 0: + return "" + # HH:MM + return time_part.substr(0, colon_idx + 3) + + +func _make_placeholder(text: String) -> Control: + var m := MarginContainer.new() + m.add_theme_constant_override("margin_top", 12) + m.add_theme_constant_override("margin_left", 12) + m.add_theme_constant_override("margin_right", 12) + var lbl := Label.new() + lbl.text = text + lbl.add_theme_color_override("font_color", MUTED_COLOR) + lbl.add_theme_font_size_override("font_size", 12) + lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + lbl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + m.add_child(lbl) + return m + + +func _make_section_label(text: String) -> Control: + var m := MarginContainer.new() + m.add_theme_constant_override("margin_left", 10) + m.add_theme_constant_override("margin_top", 8) + m.add_theme_constant_override("margin_bottom", 2) + var lbl := Label.new() + lbl.text = text + lbl.add_theme_color_override("font_color", MUTED_COLOR) + lbl.add_theme_font_size_override("font_size", 10) + m.add_child(lbl) + return m