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

@@ -46,9 +46,9 @@ namespace OpenRA.Network
readonly List<Order> localOrders = new List<Order>();
readonly List<Order> localImmediateOrders = new List<Order>();
readonly List<ChatLine> chatCache = new List<ChatLine>();
readonly List<TextNotification> notificationsCache = new List<TextNotification>();
public IReadOnlyList<ChatLine> ChatCache => chatCache;
public IReadOnlyList<TextNotification> NotificationsCache => notificationsCache;
bool disposed;
bool generateSyncReport = false;
@@ -94,7 +94,7 @@ namespace OpenRA.Network
{
Connection = conn;
syncReport = new SyncReport(this);
AddChatLine += CacheChatLine;
AddTextNotification += CacheTextNotification;
}
public void IssueOrders(Order[] orders)
@@ -111,10 +111,10 @@ namespace OpenRA.Network
localOrders.Add(order);
}
public Action<string, Color, string, Color> AddChatLine = (n, nc, s, tc) => { };
void CacheChatLine(string name, Color nameColor, string text, Color textColor)
public Action<TextNotification> AddTextNotification = (notification) => { };
void CacheTextNotification(TextNotification notification)
{
chatCache.Add(new ChatLine(name, nameColor, text, textColor));
notificationsCache.Add(notification);
}
public void TickImmediate()
@@ -242,20 +242,4 @@ namespace OpenRA.Network
Connection?.Dispose();
}
}
public class ChatLine
{
public readonly Color Color;
public readonly string Name;
public readonly string Text;
public readonly Color TextColor;
public ChatLine(string name, Color nameColor, string text, Color textColor)
{
Color = nameColor;
Name = name;
Text = text;
TextColor = textColor;
}
}
}

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Network
if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
suffix += " (Ally)";
TextNotificationsManager.AddChatLine(client.Name + suffix, client.Color, message);
TextNotificationsManager.AddChatLine(client.Name + suffix, message, client.Color);
break;
}
@@ -79,7 +79,7 @@ namespace OpenRA.Network
{
var prefix = order.ExtraData == uint.MaxValue ? "[Spectators] " : "[Team] ";
if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
TextNotificationsManager.AddChatLine(prefix + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine(prefix + client.Name, message, client.Color);
break;
}
@@ -93,7 +93,7 @@ namespace OpenRA.Network
{
// Validate before adding the line
if (client.IsObserver || (player != null && player.WinState != WinState.Undefined))
TextNotificationsManager.AddChatLine("[Spectators] " + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine("[Spectators] " + client.Name, message, client.Color);
break;
}
@@ -103,7 +103,7 @@ namespace OpenRA.Network
&& world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Undefined;
if (valid && (isSameTeam || world.IsReplay))
TextNotificationsManager.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, message, client.Color);
break;
}

View File

@@ -33,6 +33,12 @@ namespace OpenRA
Incompatible = 16
}
[Flags]
public enum TextNotificationPoolFilters
{
None = 0
}
public enum WorldViewport { Native, Close, Medium, Far }
public class ServerSettings
@@ -265,6 +271,8 @@ namespace OpenRA
[Desc("Allow mods to enable the Discord service that can interact with a local Discord client.")]
public bool EnableDiscordService = true;
public TextNotificationPoolFilters TextNotificationPoolFilters = TextNotificationPoolFilters.None;
}
public class Settings

View File

@@ -0,0 +1,56 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using OpenRA.Primitives;
namespace OpenRA
{
public enum TextNotificationPool { System, Chat, Mission, Feedback }
public class TextNotification : IEquatable<TextNotification>
{
public readonly TextNotificationPool Pool;
public readonly string Prefix;
public readonly string Text;
public readonly Color PrefixColor;
public readonly Color TextColor;
public TextNotification(TextNotificationPool pool, string prefix, string text, Color prefixColor, Color textColor)
{
Pool = pool;
Prefix = prefix;
Text = text;
PrefixColor = prefixColor;
TextColor = textColor;
}
public bool CanIncrementOnDuplicate()
{
return Pool == TextNotificationPool.Feedback || Pool == TextNotificationPool.System;
}
public bool Equals(TextNotification other)
{
return other != null && other.GetHashCode() == GetHashCode();
}
public override bool Equals(object obj)
{
return obj is TextNotification && Equals((TextNotification)obj);
}
public override int GetHashCode()
{
return string.Format("{0}{1}{2}", Prefix, Text, Pool).GetHashCode();
}
}
}

View File

@@ -28,24 +28,43 @@ namespace OpenRA
systemMessageLabel = "Battlefield Control";
}
public static void AddFeedbackLine(string text)
{
AddTextNotification(TextNotificationPool.Feedback, systemMessageLabel, text, systemMessageColor, systemMessageColor);
}
public static void AddSystemLine(string text)
{
AddSystemLine(systemMessageLabel, text);
}
public static void AddSystemLine(string name, string text)
public static void AddSystemLine(string prefix, string text)
{
Game.OrderManager.AddChatLine(name, systemMessageColor, text, systemMessageColor);
AddTextNotification(TextNotificationPool.System, prefix, text, systemMessageColor, systemMessageColor);
}
public static void AddChatLine(string name, Color nameColor, string text)
public static void AddChatLine(string prefix, string text, Color? prefixColor = null, Color? textColor = null)
{
Game.OrderManager.AddChatLine(name, nameColor, text, chatMessageColor);
AddTextNotification(TextNotificationPool.Chat, prefix, text, prefixColor, textColor);
}
public static void Debug(string s, params object[] args)
{
AddSystemLine("Debug", string.Format(s, args));
}
static void AddTextNotification(TextNotificationPool pool, string prefix, string text, Color? prefixColor = null, Color? textColor = null)
{
if (IsPoolEnabled(pool))
Game.OrderManager.AddTextNotification(new TextNotification(pool, prefix, text, prefixColor ?? chatMessageColor, textColor ?? chatMessageColor));
}
static bool IsPoolEnabled(TextNotificationPool pool)
{
return pool == TextNotificationPool.Chat ||
pool == TextNotificationPool.System ||
pool == TextNotificationPool.Mission ||
pool == TextNotificationPool.Feedback;
}
}
}