SpeechBubble.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System.Numerics;
  2. using Content.Client.Chat.Managers;
  3. using Content.Shared.CCVar;
  4. using Content.Shared.Chat;
  5. using Content.Shared.Speech;
  6. using Robust.Client.Graphics;
  7. using Robust.Client.UserInterface;
  8. using Robust.Client.UserInterface.Controls;
  9. using Robust.Shared.Configuration;
  10. using Robust.Shared.Timing;
  11. using Robust.Shared.Utility;
  12. namespace Content.Client.Chat.UI
  13. {
  14. public abstract class SpeechBubble : Control
  15. {
  16. [Dependency] private readonly IEyeManager _eyeManager = default!;
  17. [Dependency] private readonly IEntityManager _entityManager = default!;
  18. [Dependency] protected readonly IConfigurationManager ConfigManager = default!;
  19. private readonly SharedTransformSystem _transformSystem;
  20. public enum SpeechType : byte
  21. {
  22. Emote,
  23. Say,
  24. Whisper,
  25. Looc
  26. }
  27. /// <summary>
  28. /// The total time a speech bubble stays on screen.
  29. /// </summary>
  30. private const float TotalTime = 4;
  31. /// <summary>
  32. /// The amount of time at the end of the bubble's life at which it starts fading.
  33. /// </summary>
  34. private const float FadeTime = 0.25f;
  35. /// <summary>
  36. /// The distance in world space to offset the speech bubble from the center of the entity.
  37. /// i.e. greater -> higher above the mob's head.
  38. /// </summary>
  39. private const float EntityVerticalOffset = 0.5f;
  40. /// <summary>
  41. /// The default maximum width for speech bubbles.
  42. /// </summary>
  43. public const float SpeechMaxWidth = 256;
  44. private readonly EntityUid _senderEntity;
  45. private float _timeLeft = TotalTime;
  46. public float VerticalOffset { get; set; }
  47. private float _verticalOffsetAchieved;
  48. public Vector2 ContentSize { get; private set; }
  49. // man down
  50. public event Action<EntityUid, SpeechBubble>? OnDied;
  51. public static SpeechBubble CreateSpeechBubble(SpeechType type, ChatMessage message, EntityUid senderEntity)
  52. {
  53. switch (type)
  54. {
  55. case SpeechType.Emote:
  56. return new TextSpeechBubble(message, senderEntity, "emoteBox");
  57. case SpeechType.Say:
  58. return new FancyTextSpeechBubble(message, senderEntity, "sayBox");
  59. case SpeechType.Whisper:
  60. return new FancyTextSpeechBubble(message, senderEntity, "whisperBox");
  61. case SpeechType.Looc:
  62. return new TextSpeechBubble(message, senderEntity, "emoteBox", Color.FromHex("#48d1cc"));
  63. default:
  64. throw new ArgumentOutOfRangeException();
  65. }
  66. }
  67. public SpeechBubble(ChatMessage message, EntityUid senderEntity, string speechStyleClass, Color? fontColor = null)
  68. {
  69. IoCManager.InjectDependencies(this);
  70. _senderEntity = senderEntity;
  71. _transformSystem = _entityManager.System<SharedTransformSystem>();
  72. // Use text clipping so new messages don't overlap old ones being pushed up.
  73. RectClipContent = true;
  74. var bubble = BuildBubble(message, speechStyleClass, fontColor);
  75. AddChild(bubble);
  76. ForceRunStyleUpdate();
  77. bubble.Measure(Vector2Helpers.Infinity);
  78. ContentSize = bubble.DesiredSize;
  79. _verticalOffsetAchieved = -ContentSize.Y;
  80. }
  81. protected abstract Control BuildBubble(ChatMessage message, string speechStyleClass, Color? fontColor = null);
  82. protected override void FrameUpdate(FrameEventArgs args)
  83. {
  84. base.FrameUpdate(args);
  85. _timeLeft -= args.DeltaSeconds;
  86. if (_entityManager.Deleted(_senderEntity) || _timeLeft <= 0)
  87. {
  88. // Timer spawn to prevent concurrent modification exception.
  89. Timer.Spawn(0, Die);
  90. return;
  91. }
  92. // Lerp to our new vertical offset if it's been modified.
  93. if (MathHelper.CloseToPercent(_verticalOffsetAchieved - VerticalOffset, 0, 0.1))
  94. {
  95. _verticalOffsetAchieved = VerticalOffset;
  96. }
  97. else
  98. {
  99. _verticalOffsetAchieved = MathHelper.Lerp(_verticalOffsetAchieved, VerticalOffset, 10 * args.DeltaSeconds);
  100. }
  101. if (!_entityManager.TryGetComponent<TransformComponent>(_senderEntity, out var xform) || xform.MapID != _eyeManager.CurrentMap)
  102. {
  103. Modulate = Color.White.WithAlpha(0);
  104. return;
  105. }
  106. if (_timeLeft <= FadeTime)
  107. {
  108. // Update alpha if we're fading.
  109. Modulate = Color.White.WithAlpha(_timeLeft / FadeTime);
  110. }
  111. else
  112. {
  113. // Make opaque otherwise, because it might have been hidden before
  114. Modulate = Color.White;
  115. }
  116. var baseOffset = 0f;
  117. if (_entityManager.TryGetComponent<SpeechComponent>(_senderEntity, out var speech))
  118. baseOffset = speech.SpeechBubbleOffset;
  119. var offset = (-_eyeManager.CurrentEye.Rotation).ToWorldVec() * -(EntityVerticalOffset + baseOffset);
  120. var worldPos = _transformSystem.GetWorldPosition(xform) + offset;
  121. var lowerCenter = _eyeManager.WorldToScreen(worldPos) / UIScale;
  122. var screenPos = lowerCenter - new Vector2(ContentSize.X / 2, ContentSize.Y + _verticalOffsetAchieved);
  123. // Round to nearest 0.5
  124. screenPos = (screenPos * 2).Rounded() / 2;
  125. LayoutContainer.SetPosition(this, screenPos);
  126. var height = MathF.Ceiling(MathHelper.Clamp(lowerCenter.Y - screenPos.Y, 0, ContentSize.Y));
  127. SetHeight = height;
  128. }
  129. private void Die()
  130. {
  131. if (Disposed)
  132. {
  133. return;
  134. }
  135. OnDied?.Invoke(_senderEntity, this);
  136. }
  137. /// <summary>
  138. /// Causes the speech bubble to start fading IMMEDIATELY.
  139. /// </summary>
  140. public void FadeNow()
  141. {
  142. if (_timeLeft > FadeTime)
  143. {
  144. _timeLeft = FadeTime;
  145. }
  146. }
  147. protected FormattedMessage FormatSpeech(string message, Color? fontColor = null)
  148. {
  149. var msg = new FormattedMessage();
  150. if (fontColor != null)
  151. msg.PushColor(fontColor.Value);
  152. msg.AddMarkupOrThrow(message);
  153. return msg;
  154. }
  155. protected FormattedMessage ExtractAndFormatSpeechSubstring(ChatMessage message, string tag, Color? fontColor = null)
  156. {
  157. return FormatSpeech(SharedChatSystem.GetStringInsideTag(message, tag), fontColor);
  158. }
  159. }
  160. public sealed class TextSpeechBubble : SpeechBubble
  161. {
  162. public TextSpeechBubble(ChatMessage message, EntityUid senderEntity, string speechStyleClass, Color? fontColor = null)
  163. : base(message, senderEntity, speechStyleClass, fontColor)
  164. {
  165. }
  166. protected override Control BuildBubble(ChatMessage message, string speechStyleClass, Color? fontColor = null)
  167. {
  168. var label = new RichTextLabel
  169. {
  170. MaxWidth = SpeechMaxWidth,
  171. };
  172. label.SetMessage(FormatSpeech(message.WrappedMessage, fontColor));
  173. var panel = new PanelContainer
  174. {
  175. StyleClasses = { "speechBox", speechStyleClass },
  176. Children = { label },
  177. ModulateSelfOverride = Color.White.WithAlpha(ConfigManager.GetCVar(CCVars.SpeechBubbleBackgroundOpacity))
  178. };
  179. return panel;
  180. }
  181. }
  182. public sealed class FancyTextSpeechBubble : SpeechBubble
  183. {
  184. public FancyTextSpeechBubble(ChatMessage message, EntityUid senderEntity, string speechStyleClass, Color? fontColor = null)
  185. : base(message, senderEntity, speechStyleClass, fontColor)
  186. {
  187. }
  188. protected override Control BuildBubble(ChatMessage message, string speechStyleClass, Color? fontColor = null)
  189. {
  190. if (!ConfigManager.GetCVar(CCVars.ChatEnableFancyBubbles))
  191. {
  192. var label = new RichTextLabel
  193. {
  194. MaxWidth = SpeechMaxWidth
  195. };
  196. label.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleContent", fontColor));
  197. var unfanciedPanel = new PanelContainer
  198. {
  199. StyleClasses = { "speechBox", speechStyleClass },
  200. Children = { label },
  201. ModulateSelfOverride = Color.White.WithAlpha(ConfigManager.GetCVar(CCVars.SpeechBubbleBackgroundOpacity)),
  202. };
  203. return unfanciedPanel;
  204. }
  205. var bubbleHeader = new RichTextLabel
  206. {
  207. ModulateSelfOverride = Color.White.WithAlpha(ConfigManager.GetCVar(CCVars.SpeechBubbleSpeakerOpacity)),
  208. Margin = new Thickness(1, 1, 1, 1),
  209. };
  210. var bubbleContent = new RichTextLabel
  211. {
  212. ModulateSelfOverride = Color.White.WithAlpha(ConfigManager.GetCVar(CCVars.SpeechBubbleTextOpacity)),
  213. MaxWidth = SpeechMaxWidth,
  214. Margin = new Thickness(2, 6, 2, 2),
  215. StyleClasses = { "bubbleContent" },
  216. };
  217. //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
  218. bubbleHeader.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleHeader", fontColor));
  219. bubbleContent.SetMessage(ExtractAndFormatSpeechSubstring(message, "BubbleContent", fontColor));
  220. //As for below: Some day this could probably be converted to xaml. But that is not today. -Myr
  221. var mainPanel = new PanelContainer
  222. {
  223. StyleClasses = { "speechBox", speechStyleClass },
  224. Children = { bubbleContent },
  225. ModulateSelfOverride = Color.White.WithAlpha(ConfigManager.GetCVar(CCVars.SpeechBubbleBackgroundOpacity)),
  226. HorizontalAlignment = HAlignment.Center,
  227. VerticalAlignment = VAlignment.Bottom,
  228. Margin = new Thickness(4, 14, 4, 2)
  229. };
  230. var headerPanel = new PanelContainer
  231. {
  232. StyleClasses = { "speechBox", speechStyleClass },
  233. Children = { bubbleHeader },
  234. ModulateSelfOverride = Color.White.WithAlpha(ConfigManager.GetCVar(CCVars.ChatFancyNameBackground) ? ConfigManager.GetCVar(CCVars.SpeechBubbleBackgroundOpacity) : 0f),
  235. HorizontalAlignment = HAlignment.Center,
  236. VerticalAlignment = VAlignment.Top
  237. };
  238. var panel = new PanelContainer
  239. {
  240. Children = { mainPanel, headerPanel }
  241. };
  242. return panel;
  243. }
  244. }
  245. }