Split chat lines into pools

- Add a common class for passing around chat lines
- Add wrapper methods for adding chat lines
- Combine repeated chat lines in the display widget
This commit is contained in:
Ivaylo Draganov
2020-02-29 22:31:29 +02:00
committed by Paul Chote
parent 0a02bd524a
commit 6af354ff99
11 changed files with 175 additions and 88 deletions

View File

@@ -41,6 +41,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly string chatLineSound = ChromeMetrics.Get<string>("ChatLineSound");
TextNotification lastLine;
int repetitions;
[ObjectCreator.UseCtor]
public IngameChatLogic(Widget widget, OrderManager orderManager, World world, ModData modData, bool isMenuChat, Dictionary<string, MiniYaml> logicArgs)
{
@@ -195,10 +198,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
chatScrollPanel.RemoveChildren();
chatScrollPanel.ScrollToBottom();
foreach (var chatLine in orderManager.ChatCache)
AddChatLine(chatLine.Name, chatLine.Color, chatLine.Text, chatLine.TextColor, true);
foreach (var chatLine in orderManager.NotificationsCache)
AddChatLine(chatLine, true);
orderManager.AddChatLine += AddChatLineWrapper;
orderManager.AddTextNotification += AddChatLineWrapper;
chatText.IsDisabled = () => world.IsReplay && !Game.Settings.Debug.EnableDebugCommandsInReplays;
@@ -245,38 +248,58 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Ui.ResetTooltips();
}
public void AddChatLineWrapper(string name, Color nameColor, string text, Color textColor)
public void AddChatLineWrapper(TextNotification chatLine)
{
chatOverlayDisplay?.AddLine(name, nameColor, text, textColor);
var chatLineToDisplay = chatLine;
if (chatLine.CanIncrementOnDuplicate() && chatLine.Equals(lastLine))
{
repetitions++;
chatLineToDisplay = new TextNotification(
chatLine.Pool,
chatLine.Prefix,
$"{chatLine.Text} ({repetitions + 1})",
chatLine.PrefixColor,
chatLine.TextColor);
chatScrollPanel.RemoveChild(chatScrollPanel.Children[chatScrollPanel.Children.Count - 1]);
chatOverlayDisplay?.RemoveMostRecentLine();
}
else
repetitions = 0;
lastLine = chatLine;
chatOverlayDisplay?.AddLine(chatLineToDisplay);
// HACK: Force disable the chat notification sound for the in-menu chat dialog
// This works around our inability to disable the sounds for the in-game dialog when it is hidden
AddChatLine(name, nameColor, text, textColor, chatOverlay == null);
AddChatLine(chatLineToDisplay, chatOverlay == null);
}
void AddChatLine(string @from, Color nameColor, string text, Color textColor, bool suppressSound)
void AddChatLine(TextNotification chatLine, bool suppressSound)
{
var template = chatTemplate.Clone();
var nameLabel = template.Get<LabelWidget>("NAME");
var textLabel = template.Get<LabelWidget>("TEXT");
var name = "";
if (!string.IsNullOrEmpty(from))
name = from + ":";
if (!string.IsNullOrEmpty(chatLine.Prefix))
name = chatLine.Prefix + ":";
var font = Game.Renderer.Fonts[nameLabel.Font];
var nameSize = font.Measure(from);
var nameSize = font.Measure(chatLine.Prefix);
nameLabel.GetColor = () => nameColor;
nameLabel.GetColor = () => chatLine.PrefixColor;
nameLabel.GetText = () => name;
nameLabel.Bounds.Width = nameSize.X;
textLabel.GetColor = () => textColor;
textLabel.GetColor = () => chatLine.TextColor;
textLabel.Bounds.X += nameSize.X;
textLabel.Bounds.Width -= nameSize.X;
// Hack around our hacky wordwrap behavior: need to resize the widget to fit the text
text = WidgetUtils.WrapText(text, textLabel.Bounds.Width, font);
var text = WidgetUtils.WrapText(chatLine.Text, textLabel.Bounds.Width, font);
textLabel.GetText = () => text;
var dh = font.Measure(text).Y - textLabel.Bounds.Height;
if (dh > 0)
@@ -299,7 +322,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
if (!disposed)
{
orderManager.AddChatLine -= AddChatLineWrapper;
orderManager.AddTextNotification -= AddChatLineWrapper;
disposed = true;
}

View File

@@ -121,7 +121,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
services = modData.Manifest.Get<WebServices>();
orderManager.AddChatLine += AddChatLine;
orderManager.AddTextNotification += AddChatLine;
Game.LobbyInfoChanged += UpdateCurrentMap;
Game.LobbyInfoChanged += UpdatePlayerList;
Game.LobbyInfoChanged += UpdateDiscordStatus;
@@ -466,7 +466,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (disposing && !disposed)
{
disposed = true;
orderManager.AddChatLine -= AddChatLine;
orderManager.AddTextNotification -= AddChatLine;
Game.LobbyInfoChanged -= UpdateCurrentMap;
Game.LobbyInfoChanged -= UpdatePlayerList;
Game.LobbyInfoChanged -= UpdateDiscordStatus;
@@ -489,10 +489,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
panel = PanelType.Players;
}
void AddChatLine(string name, Color nameColor, string text, Color textColor)
void AddChatLine(TextNotification chatLine)
{
var template = (ContainerWidget)chatTemplate.Clone();
LobbyUtils.SetupChatLine(template, DateTime.Now, name, nameColor, text, textColor);
LobbyUtils.SetupChatLine(template, DateTime.Now, chatLine);
var scrolledToBottom = lobbyChatPanel.ScrolledToBottom;
lobbyChatPanel.AddChild(template);

View File

@@ -651,28 +651,28 @@ namespace OpenRA.Mods.Common.Widgets.Logic
HideChildWidget(parent, "STATUS_IMAGE");
}
public static void SetupChatLine(ContainerWidget template, DateTime time, string name, Color nameColor, string text, Color textColor)
public static void SetupChatLine(ContainerWidget template, DateTime time, TextNotification chatLine)
{
var nameLabel = template.Get<LabelWidget>("NAME");
var timeLabel = template.Get<LabelWidget>("TIME");
var textLabel = template.Get<LabelWidget>("TEXT");
var nameText = name + ":";
var nameText = chatLine.Prefix + ":";
var font = Game.Renderer.Fonts[nameLabel.Font];
var nameSize = font.Measure(nameText);
timeLabel.GetText = () => $"{time.Hour:D2}:{time.Minute:D2}";
nameLabel.GetColor = () => nameColor;
nameLabel.GetColor = () => chatLine.PrefixColor;
nameLabel.GetText = () => nameText;
nameLabel.Bounds.Width = nameSize.X;
textLabel.GetColor = () => textColor;
textLabel.GetColor = () => chatLine.TextColor;
textLabel.Bounds.X += nameSize.X;
textLabel.Bounds.Width -= nameSize.X;
// Hack around our hacky wordwrap behavior: need to resize the widget to fit the text
text = WidgetUtils.WrapText(text, textLabel.Bounds.Width, font);
var text = WidgetUtils.WrapText(chatLine.Text, textLabel.Bounds.Width, font);
textLabel.GetText = () => text;
var dh = font.Measure(text).Y - textLabel.Bounds.Height;
if (dh > 0)