Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nanochat lookup #2794

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,33 @@
Margin="0 0 4 0"
StyleClasses="OpenBoth"
ToolTip="{Loc nano-chat-new-chat}" />
<Button Name="LookupButton"
MaxSize="32 32"
StyleClasses="OpenBoth"
Margin="0 0 4 0"
ToolTip="{Loc nano-chat-look-up}">
<TextureRect StyleClasses="ButtonSquare"
TexturePath="/Textures/_DV/Interface/VerbIcons/magnifying_glass.svg.png"
Stretch="KeepAspectCentered"
MinSize="18 18" />
</Button>
<Button Name="ListNumberButton"
MaxSize="32 32"
StyleClasses="OpenBoth"
Margin="0 0 4 0"
ToolTip="{Loc nano-chat-list-number}"
ToggleMode="True">
<TextureRect StyleClasses="ButtonSquare"
TexturePath="/Textures/_DV/Interface/VerbIcons/hamburger_icon.svg.png"
Stretch="KeepAspectCentered"
MinSize="18 18" />
</Button>
</BoxContainer>
</controls:StripeBack>

<!-- Main content split -->
<BoxContainer Orientation="Horizontal"
<BoxContainer Name="ChatView"
Orientation="Horizontal"
VerticalExpand="True"
HorizontalExpand="True"
Margin="0 5 0 0">
Expand Down Expand Up @@ -163,5 +185,19 @@
</BoxContainer>
</PanelContainer>
</BoxContainer>
<PanelContainer StyleClasses="AngleRect"
Name="LookupView"
Visible="False"
VerticalExpand="True">
<ScrollContainer HorizontalExpand="True"
VerticalExpand="True"
Margin="-10">
<BoxContainer Name="ContactsList"
Orientation="Vertical"
VerticalExpand="True"
HorizontalExpand="True"
Margin="0" />
</ScrollContainer>
</PanelContainer>
</BoxContainer>
</cartridges:NanoChatUiFragment>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed partial class NanoChatUiFragment : BoxContainer
private uint? _pendingChat;
private uint _ownNumber;
private bool _notificationsMuted;
private bool _listNumber = true;
private Dictionary<uint, NanoChatRecipient> _recipients = new();
private Dictionary<uint, List<NanoChatMessage>> _messages = new();

Expand Down Expand Up @@ -75,10 +76,25 @@ private void SetupEventHandlers()
}
};

LookupButton.OnPressed += _ => ToggleView();
ListNumberButton.OnPressed += _ =>
{
_listNumber = !_listNumber;
UpdateListNumber();
OnMessageSent?.Invoke(NanoChatUiMessageType.ToggleListNumber, null, null, null);
};

SendButton.OnPressed += _ => SendMessage();
DeleteChatButton.OnPressed += _ => DeleteCurrentChat();
}

private void ToggleView()
{
ChatView.Visible = !ChatView.Visible;
LookupView.Visible = !ChatView.Visible;
LookupButton.Pressed = LookupView.Visible;
}

private void SendMessage()
{
var activeChat = _pendingChat ?? _currentChat;
Expand Down Expand Up @@ -214,18 +230,88 @@ private void UpdateMessages(Dictionary<uint, List<NanoChatMessage>> messages)
scroll.SetScrollValue(new Vector2(0, float.MaxValue));
}

private void UpdateContactList(List<NanoChatRecipient>? contacts)
{
ContactsList.RemoveAllChildren();
if (contacts is null)
{
ContactsList.AddChild(new Label() { Text = Loc.GetString("nano-chat-look-up-no-server") });
return;
}
for (var idx = 0; idx < contacts.Count; idx++)
{
var contact = contacts[idx];
var nameLabel = new Label()
{
Text = contact.Name,
HorizontalAlignment = HAlignment.Left,
HorizontalExpand = true
};
var numberLabel = new Label()
{
Text = $"#{contacts[idx].Number:D4}",
HorizontalAlignment = HAlignment.Right,
Margin = new Thickness(0, 0, 36, 0),
};
var startChatButton = new Button()
{
Text = "+",
HorizontalAlignment = HAlignment.Right,
MinSize = new Vector2(32, 32),
MaxSize = new Vector2(32, 32),
ToolTip = Loc.GetString("nano-chat-new-chat"),
};
startChatButton.AddStyleClass("OpenBoth");
if (contact.Number == _ownNumber || _recipients.ContainsKey(contact.Number))
{
startChatButton.Disabled = true;
}
startChatButton.OnPressed += _ =>
{
if (OnMessageSent is { } handler)
{
handler(NanoChatUiMessageType.NewChat, contact.Number, contact.Name, contact.JobTitle);
SelectChat(contact.Number);
ToggleView();
}
};

var panel = new PanelContainer()
{
HorizontalExpand = true,
};

panel.AddChild(nameLabel);
panel.AddChild(numberLabel);
panel.AddChild(startChatButton);
Toby222 marked this conversation as resolved.
Show resolved Hide resolved

var styleClass = idx % 2 == 0 ? "PanelBackgroundBaseDark" : "PanelBackgroundLight";
panel.StyleClasses.Add(styleClass);

ContactsList.AddChild(panel);
}
}

private void UpdateMuteButton()
{
if (BellMutedIcon != null)
BellMutedIcon.Visible = _notificationsMuted;
}

private void UpdateListNumber()
{
if (ListNumberButton != null)
ListNumberButton.Pressed = _listNumber;
}

public void UpdateState(NanoChatUiState state)
{
_ownNumber = state.OwnNumber;
_notificationsMuted = state.NotificationsMuted;
_listNumber = state.ListNumber;
OwnNumberLabel.Text = $"#{state.OwnNumber:D4}";
UpdateMuteButton();
UpdateListNumber();

// Update new chat button state based on recipient limit
var atLimit = state.Recipients.Count >= state.MaxRecipients;
Expand All @@ -250,5 +336,6 @@ public void UpdateState(NanoChatUiState state)
UpdateCurrentChat();
UpdateChatList(state.Recipients);
UpdateMessages(state.Messages);
UpdateContactList(state.Contacts);
}
}
3 changes: 3 additions & 0 deletions Content.Server/PDA/PdaSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Content.Server.Store.Components;
using Content.Server.Store.Systems;
using Content.Server.Traitor.Uplink;
using Content.Shared._DV.NanoChat;
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
using Content.Shared.Access.Components;
using Content.Shared.CartridgeLoader;
using Content.Shared.Chat;
Expand Down Expand Up @@ -180,6 +181,7 @@ public void UpdatePdaUi(EntityUid uid, PdaComponent? pda = null)

var address = GetDeviceNetAddress(uid);
var hasInstrument = HasComp<InstrumentComponent>(uid);
var hasNanoChatCard = HasComp<NanoChatCardComponent>(pda.ContainedId);
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
var showUplink = HasComp<UplinkComponent>(uid) && IsUnlocked(uid);

UpdateStationName(uid, pda);
Expand Down Expand Up @@ -210,6 +212,7 @@ public void UpdatePdaUi(EntityUid uid, PdaComponent? pda = null)
pda.StationName,
showUplink,
hasInstrument,
hasNanoChatCard,
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
address);

_ui.SetUiState(uid, PdaUiKey.Key, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Initialize()
private void UpdateClosed(Entity<NanoChatCartridgeComponent> ent)
{
if (!TryComp<CartridgeComponent>(ent, out var cartridge) ||
cartridge.LoaderUid is not {} pda ||
cartridge.LoaderUid is not { } pda ||
!TryComp<CartridgeLoaderComponent>(pda, out var loader) ||
!GetCardEntity(pda, out var card))
{
Expand Down Expand Up @@ -117,6 +117,9 @@ private void OnMessage(Entity<NanoChatCartridgeComponent> ent, ref CartridgeMess
case NanoChatUiMessageType.SendMessage:
HandleSendMessage(ent, card, msg);
break;
case NanoChatUiMessageType.ToggleListNumber:
HandleToggleListNumber(card);
break;
}

UpdateUI(ent, GetEntity(args.LoaderUid));
Expand Down Expand Up @@ -226,6 +229,12 @@ private void HandleToggleMute(Entity<NanoChatCardComponent> card)
UpdateUIForCard(card);
}

private void HandleToggleListNumber(Entity<NanoChatCardComponent> card)
{
_nanoChat.SetListNumber((card, card.Comp), !_nanoChat.GetListNumber((card, card.Comp)));
UpdateUIForAllCards();
}

/// <summary>
/// Handles sending a new message in a chat conversation.
/// </summary>
Expand Down Expand Up @@ -455,6 +464,22 @@ private void UpdateUIForCard(EntityUid cardUid)
}
}

/// <summary>
/// Updates the UI for all PDAs containing a NanoChat cartridge.
/// </summary>
private void UpdateUIForAllCards()
{
// Find any PDA containing this card and update its UI
var query = EntityQueryEnumerator<NanoChatCartridgeComponent, CartridgeComponent>();
while (query.MoveNext(out var uid, out var comp, out var cartridge))
{
if (cartridge.LoaderUid == null)
continue;

UpdateUI((uid, comp), cartridge.LoaderUid.Value);
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <summary>
/// Gets the <see cref="NanoChatRecipient" /> for a given NanoChat number.
/// </summary>
Expand Down Expand Up @@ -500,15 +525,35 @@ private void OnUiReady(Entity<NanoChatCartridgeComponent> ent, ref CartridgeUiRe

private void UpdateUI(Entity<NanoChatCartridgeComponent> ent, EntityUid loader)
{
List<NanoChatRecipient>? contacts;
if (_station.GetOwningStation(loader) is { } station)
{
ent.Comp.Station = station;

contacts = [];

var query = AllEntityQuery<NanoChatCardComponent, IdCardComponent>();
while (query.MoveNext(out var entityId, out var nanoChatCard, out var idCardComponent))
{
if (nanoChatCard.ListNumber && nanoChatCard.Number is uint nanoChatNumber && idCardComponent.FullName is string fullName && _station.GetOwningStation(entityId) == station)
{
contacts.Add(new NanoChatRecipient(nanoChatNumber, fullName));
}
}
contacts.Sort((contactA, contactB) => string.CompareOrdinal(contactA.Name, contactB.Name));
}
else
{
contacts = null;
}

var recipients = new Dictionary<uint, NanoChatRecipient>();
var messages = new Dictionary<uint, List<NanoChatMessage>>();
uint? currentChat = null;
uint ownNumber = 0;
var maxRecipients = 50;
var notificationsMuted = false;
var listNumber = false;

if (ent.Comp.Card != null && TryComp<NanoChatCardComponent>(ent.Comp.Card, out var card))
{
Expand All @@ -518,14 +563,17 @@ private void UpdateUI(Entity<NanoChatCartridgeComponent> ent, EntityUid loader)
ownNumber = card.Number ?? 0;
maxRecipients = card.MaxRecipients;
notificationsMuted = card.NotificationsMuted;
listNumber = card.ListNumber;
}

var state = new NanoChatUiState(recipients,
messages,
contacts,
currentChat,
ownNumber,
maxRecipients,
notificationsMuted);
notificationsMuted,
listNumber);
_cartridge.UpdateCartridgeUiState(loader, state);
}
}
3 changes: 3 additions & 0 deletions Content.Shared/PDA/PdaUpdateState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public sealed class PdaUpdateState : CartridgeLoaderUiState // WTF is this. what
public string? StationName;
public bool HasUplink;
public bool CanPlayMusic;
public bool HasNanoChatCard;
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
public string? Address;

public PdaUpdateState(
Expand All @@ -27,6 +28,7 @@ public PdaUpdateState(
string? stationName,
bool hasUplink = false,
bool canPlayMusic = false,
bool hasNanoChatCard = false,
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
string? address = null)
: base(programs, activeUI)
{
Expand All @@ -36,6 +38,7 @@ public PdaUpdateState(
PdaOwnerInfo = pdaOwnerInfo;
HasUplink = hasUplink;
CanPlayMusic = canPlayMusic;
HasNanoChatCard = hasNanoChatCard;
Toby222 marked this conversation as resolved.
Show resolved Hide resolved
StationName = stationName;
Address = address;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public enum NanoChatUiMessageType : byte
SendMessage,
DeleteChat,
ToggleMute,
ToggleListNumber,
}

// putting this here because i can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@ public sealed class NanoChatUiState : BoundUserInterfaceState
{
public readonly Dictionary<uint, NanoChatRecipient> Recipients = new();
public readonly Dictionary<uint, List<NanoChatMessage>> Messages = new();
public readonly List<NanoChatRecipient>? Contacts;
public readonly uint? CurrentChat;
public readonly uint OwnNumber;
public readonly int MaxRecipients;
public readonly bool NotificationsMuted;
public readonly bool ListNumber;

public NanoChatUiState(
Dictionary<uint, NanoChatRecipient> recipients,
Dictionary<uint, List<NanoChatMessage>> messages,
List<NanoChatRecipient>? contacts,
uint? currentChat,
uint ownNumber,
int maxRecipients,
bool notificationsMuted)
bool notificationsMuted,
bool listNumber)
{
Recipients = recipients;
Messages = messages;
Contacts = contacts;
CurrentChat = currentChat;
OwnNumber = ownNumber;
MaxRecipients = maxRecipients;
NotificationsMuted = notificationsMuted;
ListNumber = listNumber;
}
}
6 changes: 6 additions & 0 deletions Content.Shared/_DV/NanoChat/NanoChatCardComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public sealed partial class NanoChatCardComponent : Component
/// </summary>
[DataField]
public bool NotificationsMuted;

/// <summary>
/// Whether the card's number should be listed in NanoChat's lookup
/// </summary>
[DataField]
public bool ListNumber = true;
}
Loading
Loading