Skip to content

Commit

Permalink
a (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhailrake authored Dec 26, 2024
1 parent 01bc225 commit 3239865
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
40 changes: 31 additions & 9 deletions Content.Client/Chat/UI/SpeechBubble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public enum SpeechType : byte
Looc
}

protected RichTextLabel? ContentLabel;

public void UpdateText(ChatMessage message, int repeat)
{
if (ContentLabel == null)
return;

if (this is TextSpeechBubble)
{
var updatedMessage = $"{message.WrappedMessage} x{repeat}";
ContentLabel.SetMessage(FormatSpeech(updatedMessage));
}
else if (this is FancyTextSpeechBubble)
{
var bubbleContent = SharedChatSystem.GetStringInsideTag(message, "BubbleContent");
var updatedMessage = $"{bubbleContent} x{repeat}";
ContentLabel.SetMessage(FormatSpeech(updatedMessage));
}

_timeLeft = TotalTime;
}

/// <summary>
/// The total time a speech bubble stays on screen.
/// </summary>
Expand Down Expand Up @@ -206,17 +228,17 @@ public TextSpeechBubble(ChatMessage message, EntityUid senderEntity, string spee

protected override Control BuildBubble(ChatMessage message, string speechStyleClass, Color? fontColor = null)
{
var label = new RichTextLabel
ContentLabel = new RichTextLabel
{
MaxWidth = SpeechMaxWidth,
};

label.SetMessage(FormatSpeech(message.WrappedMessage, fontColor));
ContentLabel.SetMessage(FormatSpeech(message.WrappedMessage, fontColor));

var panel = new PanelContainer
{
StyleClasses = { "speechBox", speechStyleClass },
Children = { label },
Children = { ContentLabel },
ModulateSelfOverride = Color.White.WithAlpha(0.75f)
};

Expand All @@ -236,17 +258,17 @@ protected override Control BuildBubble(ChatMessage message, string speechStyleCl
{
if (!ConfigManager.GetCVar(CCVars.ChatEnableFancyBubbles))
{
var label = new RichTextLabel
ContentLabel = new RichTextLabel
{
MaxWidth = SpeechMaxWidth
};

label.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleContent", fontColor));
ContentLabel.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleContent", fontColor));

var unfanciedPanel = new PanelContainer
{
StyleClasses = { "speechBox", speechStyleClass },
Children = { label },
Children = { ContentLabel },
ModulateSelfOverride = Color.White.WithAlpha(0.75f)
};
return unfanciedPanel;
Expand All @@ -257,7 +279,7 @@ protected override Control BuildBubble(ChatMessage message, string speechStyleCl
Margin = new Thickness(1, 1, 1, 1)
};

var bubbleContent = new RichTextLabel
ContentLabel = new RichTextLabel
{
MaxWidth = SpeechMaxWidth,
Margin = new Thickness(2, 6, 2, 2),
Expand All @@ -266,13 +288,13 @@ protected override Control BuildBubble(ChatMessage message, string speechStyleCl

//We'll be honest. *Yes* this is hacky. Doing this in a cleaner way would require a bottom-up refactor of how saycode handles sending chat messages. -Myr
bubbleHeader.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleHeader", fontColor));
bubbleContent.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleContent", fontColor));
ContentLabel.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleContent", fontColor));

//As for below: Some day this could probably be converted to xaml. But that is not today. -Myr
var mainPanel = new PanelContainer
{
StyleClasses = { "speechBox", speechStyleClass },
Children = { bubbleContent },
Children = { ContentLabel },
ModulateSelfOverride = Color.White.WithAlpha(0.75f),
HorizontalAlignment = HAlignment.Center,
VerticalAlignment = VAlignment.Bottom,
Expand Down
43 changes: 39 additions & 4 deletions Content.Client/UserInterface/Systems/Chat/ChatUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@

namespace Content.Client.UserInterface.Systems.Chat;

public class SpeechBubbleStack
{
public required SpeechBubble Bubble { get; set; }
public required string Message { get; set; }
public int RepeatCount { get; set; } = 1;
public SpeechBubble.SpeechType Type { get; set; }
}

public sealed class ChatUIController : UIController
{
[Dependency] private readonly IClientAdminManager _admin = default!;
Expand Down Expand Up @@ -174,6 +182,8 @@ private readonly Dictionary<EntityUid, SpeechBubbleQueueData> _queuedSpeechBubbl

public ChatMessage? LastMessage = null;

private readonly Dictionary<EntityUid, SpeechBubbleStack> _lastBubbles = new();

public event Action<ChatSelectChannel>? CanSendChannelsChanged;
public event Action<ChatChannel>? FilterableChannelsChanged;
public event Action<ChatSelectChannel>? SelectableChannelsChanged;
Expand Down Expand Up @@ -456,9 +466,19 @@ private void AddSpeechBubble(ChatMessage msg, SpeechBubble.SpeechType speechType

private void CreateSpeechBubble(EntityUid entity, SpeechBubbleData speechData)
{
var bubble =
SpeechBubble.CreateSpeechBubble(speechData.Type, speechData.Message, entity);
var message = speechData.Message;
var type = speechData.Type;

if (_lastBubbles.TryGetValue(entity, out var stack) &&
stack.Message == message.WrappedMessage &&
stack.Type == type)
{
stack.RepeatCount++;
stack.Bubble.UpdateText(message, stack.RepeatCount);
return;
}

var bubble = SpeechBubble.CreateSpeechBubble(type, message, entity);
bubble.OnDied += SpeechBubbleDied;

if (_activeSpeechBubbles.TryGetValue(entity, out var existing))
Expand All @@ -478,6 +498,13 @@ private void CreateSpeechBubble(EntityUid entity, SpeechBubbleData speechData)
existing.Add(bubble);
_speechBubbleRoot.AddChild(bubble);

_lastBubbles[entity] = new SpeechBubbleStack
{
Bubble = bubble,
Message = message.WrappedMessage,
Type = type
};

if (existing.Count > SpeechBubbleCap)
{
// Get the oldest to start fading fast.
Expand Down Expand Up @@ -513,6 +540,9 @@ public void RemoveSpeechBubble(EntityUid entityUid, SpeechBubble bubble)
var list = _activeSpeechBubbles[entityUid];
list.Remove(bubble);

if (_lastBubbles.TryGetValue(entityUid, out var stack) && stack.Bubble == bubble)
_lastBubbles.Remove(entityUid);

if (list.Count == 0)
{
_activeSpeechBubbles.Remove(entityUid);
Expand Down Expand Up @@ -889,9 +919,10 @@ public void ProcessChatMessage(ChatMessage msg, bool speechBubble = true)

foreach (var chat in _chats)
{
// chat._controller.History[index].Item2
chat.UpdateMessage(chat.GetHistoryLength() - 1, LastMessage);
}

TryBuble(msg, speechBubble);
return;
}

Expand All @@ -915,7 +946,11 @@ public void ProcessChatMessage(ChatMessage msg, bool speechBubble = true)
}
}

// Local messages that have an entity attached get a speech bubble.
TryBuble(msg, speechBubble);
}

private void TryBuble(ChatMessage msg, bool speechBubble)
{
if (!speechBubble || msg.SenderEntity == default)
return;

Expand Down

0 comments on commit 3239865

Please sign in to comment.