From 32398653645e26771070d7a8df8c5053b135d562 Mon Sep 17 00:00:00 2001 From: haiwwkes <49613070+rhailrake@users.noreply.github.com> Date: Fri, 27 Dec 2024 04:41:35 +0500 Subject: [PATCH] a (#939) --- Content.Client/Chat/UI/SpeechBubble.cs | 40 +++++++++++++---- .../Systems/Chat/ChatUIController.cs | 43 +++++++++++++++++-- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/Content.Client/Chat/UI/SpeechBubble.cs b/Content.Client/Chat/UI/SpeechBubble.cs index aa61e73e31c..cbd22739cec 100644 --- a/Content.Client/Chat/UI/SpeechBubble.cs +++ b/Content.Client/Chat/UI/SpeechBubble.cs @@ -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; + } + /// /// The total time a speech bubble stays on screen. /// @@ -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) }; @@ -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; @@ -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), @@ -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, diff --git a/Content.Client/UserInterface/Systems/Chat/ChatUIController.cs b/Content.Client/UserInterface/Systems/Chat/ChatUIController.cs index af37d1e4df7..a12fc5a7bf9 100644 --- a/Content.Client/UserInterface/Systems/Chat/ChatUIController.cs +++ b/Content.Client/UserInterface/Systems/Chat/ChatUIController.cs @@ -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!; @@ -174,6 +182,8 @@ private readonly Dictionary _queuedSpeechBubbl public ChatMessage? LastMessage = null; + private readonly Dictionary _lastBubbles = new(); + public event Action? CanSendChannelsChanged; public event Action? FilterableChannelsChanged; public event Action? SelectableChannelsChanged; @@ -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)) @@ -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. @@ -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); @@ -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; } @@ -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;