Files
2026-04-14 16:02:24 -04:00

1063 lines
35 KiB
GDScript

extends CanvasLayer
## Chat overlay — persists across all scenes as a child of the viewport root.
##
## Structure (defined in ChatOverlay.tscn):
## ChatBubble (Button, bottom-right) — toggles the panel
## ChatPanel (PanelContainer)
## RootVBox
## Header — title + close button
## TabBar — Friends | Presence | Global | Lobby | Game | Party | DMs
## ContentArea — one pane 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, PARTY, DMS }
const TAB_LABELS := ["Friends", "Presence", "Global", "Lobby", "Game", "Party", "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
@onready var _input_bar : HBoxContainer = %InputBar
@onready var _message_input : LineEdit = %MessageInput
# Per-tab content refs
@onready var _friends_list : VBoxContainer = %FriendsList
@onready var _requests_section : VBoxContainer = %RequestsSection
@onready var _requests_list : VBoxContainer = %RequestsList
@onready var _presence_list : VBoxContainer = %PresenceList
@onready var _presence_option : OptionButton = %PresenceOption
@onready var _global_list : VBoxContainer = %GlobalList
@onready var _lobby_list : VBoxContainer = %LobbyList
@onready var _game_list : VBoxContainer = %GameList
@onready var _dm_conv_list : VBoxContainer = %DmConvList
@onready var _dm_msg_list : VBoxContainer = %DmMsgList
@onready var _party_msg_list : VBoxContainer = %PartyList
@onready var _party_action_bar : HBoxContainer = %PartyActionBar
@onready var _party_name_lbl : Label = %PartyNameLabel
# ── State ─────────────────────────────────────────────────────────────────────
var _tab_buttons : Array[Button] = []
var _content_nodes : Array[Control] = [] # one entry per Tab enum value
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
const _BUBBLE_DEFAULT_LEFT := -58.0
const _BUBBLE_DEFAULT_TOP := -219.0
const _BUBBLE_DEFAULT_RIGHT := 2.0
const _BUBBLE_DEFAULT_BOTTOM := -159.0
var _nicknames : Dictionary = {} # account_id → custom display name
var _muted : Dictionary = {} # account_id → true
# ── Lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
# Populate tab button array from scene TabBar children (order matches Tab enum).
for btn in %TabBar.get_children():
_tab_buttons.append(btn as Button)
for i in _tab_buttons.size():
var idx := i
_tab_buttons[i].pressed.connect(func() -> void: _show_tab(idx as Tab))
# Populate content pane array from scene ContentArea children (order matches Tab enum).
for pane in %ContentArea.get_children():
_content_nodes.append(pane as Control)
# Wire scene node signals.
%CloseBtn.pressed.connect(func() -> void: _panel.visible = false)
%RenameBtn.pressed.connect(_on_party_rename_pressed)
%LeaveBtn.pressed.connect(_on_party_leave_pressed)
%SendBtn.pressed.connect(_send_current_input)
_message_input.text_submitted.connect(func(_text: String) -> void: _send_current_input())
# Populate presence option items.
_presence_option.add_item("● Online", 0)
_presence_option.add_item("◐ Away", 1)
_presence_option.add_item("○ Offline", 2)
_presence_option.selected = 0
_presence_option.item_selected.connect(_on_own_presence_changed)
_style_bubble()
_connect_chat_signals()
_bubble.pressed.connect(_toggle_panel)
_show_tab(Tab.GLOBAL)
func set_overlay_visible(v: bool) -> void:
visible = v
var bubble := get_node_or_null("ChatBubble") as Control
if is_instance_valid(bubble):
bubble.visible = v
if not v:
var panel := get_node_or_null("ChatPanel") as Control
if is_instance_valid(panel):
panel.visible = false
func set_bubble_host(host: Control) -> void:
var bubble := get_node_or_null("ChatBubble") as Control
if not is_instance_valid(bubble):
return
var target: Node = self
if is_instance_valid(host):
target = host
if bubble.get_parent() != target:
bubble.reparent(target)
if target == self:
bubble.set_anchors_preset(Control.PRESET_BOTTOM_RIGHT)
bubble.offset_left = _BUBBLE_DEFAULT_LEFT
bubble.offset_top = _BUBBLE_DEFAULT_TOP
bubble.offset_right = _BUBBLE_DEFAULT_RIGHT
bubble.offset_bottom = _BUBBLE_DEFAULT_BOTTOM
bubble.custom_minimum_size = Vector2.ZERO
else:
bubble.set_anchors_preset(Control.PRESET_FULL_RECT)
bubble.offset_left = 0.0
bubble.offset_top = 0.0
bubble.offset_right = 0.0
bubble.offset_bottom = 0.0
bubble.custom_minimum_size = Vector2(0, 36)
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(_on_notifications_updated)
Chat.party_updated.connect(_on_party_updated)
Chat.party_invite_received.connect(_on_party_invite_received)
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)
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 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.PARTY:
_input_bar.visible = not Chat.get_party_channel().is_empty()
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.PARTY:
_refresh_party_tab()
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()
# Notification preview section (unread only).
var unread_notifications := Chat.notifications.filter(func(n: Dictionary) -> bool:
return n.get("readAt") == null)
if not unread_notifications.is_empty():
_friends_list.add_child(_make_section_label("NOTIFICATIONS — %d" % unread_notifications.size()))
for n: Dictionary in unread_notifications:
_friends_list.add_child(_make_notification_row(n))
var mark_all_btn := Button.new()
mark_all_btn.text = "Mark all notifications read"
mark_all_btn.flat = true
mark_all_btn.alignment = HORIZONTAL_ALIGNMENT_LEFT
mark_all_btn.custom_minimum_size = Vector2(0, 30)
mark_all_btn.add_theme_color_override("font_color", ACCENT_COLOR)
mark_all_btn.pressed.connect(func() -> void:
var ids: Array = unread_notifications.map(func(n: Dictionary) -> String:
return str(n.get("id", "")))
Chat.mark_notifications_read(ids))
_friends_list.add_child(mark_all_btn)
var notif_sep := HSeparator.new()
_friends_list.add_child(notif_sep)
# 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_notification_row(n: Dictionary) -> Control:
var row := VBoxContainer.new()
row.custom_minimum_size = Vector2(0, 42)
row.add_theme_constant_override("separation", 2)
var title := Label.new()
title.text = n.get("title", "Notification")
title.add_theme_font_size_override("font_size", 12)
title.add_theme_color_override("font_color", Color(0.92, 0.92, 0.98))
row.add_child(title)
var body := Label.new()
body.text = n.get("body", "")
body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
body.add_theme_font_size_override("font_size", 11)
body.add_theme_color_override("font_color", MUTED_COLOR)
row.add_child(body)
var footer := HBoxContainer.new()
footer.add_theme_constant_override("separation", 8)
row.add_child(footer)
var ts := Label.new()
ts.text = _format_time(str(n.get("createdAt", "")))
ts.add_theme_font_size_override("font_size", 10)
ts.add_theme_color_override("font_color", MUTED_COLOR)
ts.size_flags_horizontal = Control.SIZE_EXPAND_FILL
footer.add_child(ts)
var mark_btn := Button.new()
mark_btn.text = "Mark read"
mark_btn.flat = true
mark_btn.add_theme_font_size_override("font_size", 10)
mark_btn.add_theme_color_override("font_color", ACCENT_COLOR)
mark_btn.pressed.connect(func() -> void:
var id := str(n.get("id", ""))
if not id.is_empty():
Chat.mark_notifications_read([id]))
footer.add_child(mark_btn)
var wrap := MarginContainer.new()
wrap.add_theme_constant_override("margin_left", 10)
wrap.add_theme_constant_override("margin_right", 10)
wrap.add_theme_constant_override("margin_top", 4)
wrap.add_theme_constant_override("margin_bottom", 4)
wrap.add_child(row)
return wrap
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)
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 row := HBoxContainer.new()
row.add_theme_constant_override("separation", 0)
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
btn.size_flags_horizontal = Control.SIZE_EXPAND_FILL
if cid == _active_dm:
_apply_tab_btn_style(btn, true)
btn.pressed.connect(func() -> void: _select_dm(cid))
row.add_child(btn)
var close_btn := Button.new()
close_btn.text = "✕"
close_btn.flat = true
close_btn.custom_minimum_size = Vector2(24, 36)
close_btn.add_theme_font_size_override("font_size", 10)
close_btn.add_theme_color_override("font_color", MUTED_COLOR)
close_btn.pressed.connect(func() -> void: _close_dm(cid))
row.add_child(close_btn)
_dm_conv_list.add_child(row)
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 _close_dm(channel_id: String) -> void:
if _active_dm == channel_id:
_active_dm = ""
_active_channel = ""
_input_bar.visible = false
if is_instance_valid(_dm_msg_list):
for c in _dm_msg_list.get_children():
c.queue_free()
Chat.close_dm(channel_id)
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)
# ── Refresh: Party ────────────────────────────────────────────────────────────
func _refresh_party_tab() -> void:
var party_chan := Chat.get_party_channel()
var in_party := not party_chan.is_empty()
_party_action_bar.visible = in_party
if in_party:
_party_name_lbl.text = party_chan.get("name", "Party")
_input_bar.visible = in_party
if in_party:
var cid: String = party_chan.get("id", "")
_active_channel = cid
if cid not in Chat.messages:
Chat.fetch_messages(cid)
_refresh_party_messages()
func _refresh_party_messages() -> void:
if not is_instance_valid(_party_msg_list):
return
for c in _party_msg_list.get_children():
c.queue_free()
var party_chan := Chat.get_party_channel()
if party_chan.is_empty():
_party_msg_list.add_child(_make_placeholder("You are not in a party.\nAccept an invite from a friend to join one."))
return
var cid : String = party_chan.get("id", "")
var msgs : Array = Chat.messages.get(cid, [])
for i in range(msgs.size() - 1, -1, -1):
var row := _make_message_row(msgs[i] as Dictionary, "party")
if row != null:
_party_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("Invite to Party", 4)
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)
4: # Invite to Party
Chat.invite_to_party(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
Chat.invite_to_party(account_id)
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.PARTY: _refresh_party_tab()
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.PARTY:
if Chat.get_party_channel().get("id", "") == channel_id:
_refresh_party_messages()
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_notifications_updated() -> void:
_refresh_badge()
if _panel.visible and _current_tab == Tab.FRIENDS:
_refresh_friends()
func _on_own_presence_changed(index: int) -> void:
const STATUS_MAP := ["Online", "Away", "Offline"]
Chat.set_presence(STATUS_MAP[index])
func _on_party_updated() -> void:
if not _panel.visible:
return
if _current_tab == Tab.PARTY:
_refresh_party_tab()
# Keep the tab button highlighted if a party exists.
_apply_tab_btn_style(
_tab_buttons[Tab.PARTY],
_current_tab == Tab.PARTY)
func _on_party_invite_received(channel_id: String, inviter_name: String) -> void:
var dialog := ConfirmationDialog.new()
dialog.title = "Party Invite"
dialog.dialog_text = "%s invited you to their party!" % inviter_name
dialog.ok_button_text = "Accept"
dialog.cancel_button_text = "Decline"
dialog.min_size = Vector2(280, 100)
add_child(dialog)
dialog.popup_centered()
dialog.confirmed.connect(func() -> void:
Chat.respond_party_invite(channel_id, true)
dialog.queue_free()
_show_tab(Tab.PARTY))
dialog.canceled.connect(func() -> void:
Chat.respond_party_invite(channel_id, false)
dialog.queue_free())
func _on_party_rename_pressed() -> void:
var party_chan := Chat.get_party_channel()
if party_chan.is_empty():
return
var dialog := AcceptDialog.new()
dialog.title = "Rename Party"
dialog.min_size = Vector2(280, 100)
var vbox := VBoxContainer.new()
vbox.add_theme_constant_override("separation", 8)
var input := LineEdit.new()
input.placeholder_text = "Party name"
input.text = party_chan.get("name", "")
input.max_length = 64
vbox.add_child(input)
dialog.add_child(vbox)
add_child(dialog)
dialog.popup_centered()
dialog.confirmed.connect(func() -> void:
var n := input.text.strip_edges()
if not n.is_empty():
Chat.rename_party(n)
dialog.queue_free())
dialog.canceled.connect(func() -> void: dialog.queue_free())
func _on_party_leave_pressed() -> void:
Chat.leave_party()
if _current_tab == Tab.PARTY:
_refresh_party_tab()
# ── 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