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 var _send_button : Button = 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.GLOBAL 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.GLOBAL) 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(func(_text: String) -> void: _send_current_input()) inner.add_child(_message_input) _send_button = Button.new() _send_button.text = "Send" _send_button.custom_minimum_size = Vector2(52, 0) _send_button.add_theme_font_size_override("font_size", 12) _send_button.pressed.connect(_send_current_input) inner.add_child(_send_button) return _input_bar func _unhandled_input(event: InputEvent) -> void: if not _panel.visible or not _input_bar.visible: return if not is_instance_valid(_message_input) or not _message_input.has_focus(): return if event is InputEventKey: var key := event as InputEventKey if key.pressed and not key.echo \ and (key.keycode == KEY_ENTER or key.keycode == KEY_KP_ENTER): _send_current_input() get_viewport().set_input_as_handled() # ── 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 _send_current_input() -> void: if not is_instance_valid(_message_input): return _on_send(_message_input.text) 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