Update TextNotificationsManager to use Ui.Send

This commit is contained in:
tomas
2022-07-12 17:46:20 +02:00
committed by Gustas
parent b0329aad35
commit 5f4ed5f16b
6 changed files with 25 additions and 55 deletions

View File

@@ -83,6 +83,9 @@ namespace OpenRA
static void JoinInner(OrderManager om) static void JoinInner(OrderManager om)
{ {
// Refresh TextNotificationsManager before the game starts.
TextNotificationsManager.Clear();
// HACK: The shellmap World and OrderManager are owned by the main menu's WorldRenderer instead of Game. // HACK: The shellmap World and OrderManager are owned by the main menu's WorldRenderer instead of Game.
// This allows us to switch Game.OrderManager from the shellmap to the new network connection when joining // This allows us to switch Game.OrderManager from the shellmap to the new network connection when joining
// a lobby, while keeping the OrderManager that runs the shellmap intact. // a lobby, while keeping the OrderManager that runs the shellmap intact.

View File

@@ -51,10 +51,6 @@ namespace OpenRA.Network
readonly List<ClientOrder> processClientOrders = new List<ClientOrder>(); readonly List<ClientOrder> processClientOrders = new List<ClientOrder>();
readonly List<int> processClientsToRemove = new List<int>(); readonly List<int> processClientsToRemove = new List<int>();
readonly List<TextNotification> notificationsCache = new List<TextNotification>();
public IReadOnlyList<TextNotification> NotificationsCache => notificationsCache;
bool disposed; bool disposed;
bool generateSyncReport = false; bool generateSyncReport = false;
int sentOrdersFrame = 0; int sentOrdersFrame = 0;
@@ -108,7 +104,6 @@ namespace OpenRA.Network
{ {
Connection = conn; Connection = conn;
syncReport = new SyncReport(this); syncReport = new SyncReport(this);
AddTextNotification += CacheTextNotification;
LastTickTime = new TickTime(() => SuggestedTimestep, Game.RunTime); LastTickTime = new TickTime(() => SuggestedTimestep, Game.RunTime);
} }
@@ -127,12 +122,6 @@ namespace OpenRA.Network
localOrders.Add(order); localOrders.Add(order);
} }
public Action<TextNotification> AddTextNotification = (notification) => { };
void CacheTextNotification(TextNotification notification)
{
notificationsCache.Add(notification);
}
void SendImmediateOrders() void SendImmediateOrders()
{ {
if (localImmediateOrders.Count != 0 && GameSaveLastFrame < NetFrameNumber) if (localImmediateOrders.Count != 0 && GameSaveLastFrame < NetFrameNumber)

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -20,6 +21,8 @@ namespace OpenRA
static readonly string SystemMessageLabel; static readonly string SystemMessageLabel;
public static long ChatDisabledUntil { get; internal set; } public static long ChatDisabledUntil { get; internal set; }
static readonly List<TextNotification> NotificationsCache = new List<TextNotification>();
public static IReadOnlyList<TextNotification> Notifications => NotificationsCache;
static TextNotificationsManager() static TextNotificationsManager()
{ {
@@ -69,7 +72,12 @@ namespace OpenRA
static void AddTextNotification(TextNotificationPool pool, int clientId, string prefix, string text, Color? prefixColor = null, Color? textColor = null) static void AddTextNotification(TextNotificationPool pool, int clientId, string prefix, string text, Color? prefixColor = null, Color? textColor = null)
{ {
if (IsPoolEnabled(pool)) if (IsPoolEnabled(pool))
Game.OrderManager.AddTextNotification(new TextNotification(pool, clientId, prefix, text, prefixColor, textColor)); {
var textNotification = new TextNotification(pool, clientId, prefix, text, prefixColor, textColor);
NotificationsCache.Add(textNotification);
Ui.Send(textNotification);
}
} }
static bool IsPoolEnabled(TextNotificationPool pool) static bool IsPoolEnabled(TextNotificationPool pool)
@@ -82,5 +90,10 @@ namespace OpenRA
(pool == TextNotificationPool.Transients && filters.HasFlag(TextNotificationPoolFilters.Transients)) || (pool == TextNotificationPool.Transients && filters.HasFlag(TextNotificationPoolFilters.Transients)) ||
(pool == TextNotificationPool.Feedback && filters.HasFlag(TextNotificationPoolFilters.Feedback)); (pool == TextNotificationPool.Feedback && filters.HasFlag(TextNotificationPoolFilters.Feedback));
} }
public static void Clear()
{
NotificationsCache.Clear();
}
} }
} }

View File

@@ -21,9 +21,8 @@ using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic namespace OpenRA.Mods.Common.Widgets.Logic
{ {
[ChromeLogicArgsHotkeys("OpenTeamChat", "OpenGeneralChat")] [ChromeLogicArgsHotkeys("OpenTeamChat", "OpenGeneralChat")]
public class IngameChatLogic : ChromeLogic public class IngameChatLogic : ChromeLogic, INotificationHandler<TextNotification>
{ {
readonly OrderManager orderManager;
readonly Ruleset modRules; readonly Ruleset modRules;
readonly World world; readonly World world;
@@ -47,7 +46,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public IngameChatLogic(Widget widget, OrderManager orderManager, World world, ModData modData, bool isMenuChat, Dictionary<string, MiniYaml> logicArgs) public IngameChatLogic(Widget widget, OrderManager orderManager, World world, ModData modData, bool isMenuChat, Dictionary<string, MiniYaml> logicArgs)
{ {
this.orderManager = orderManager;
modRules = modData.DefaultRules; modRules = modData.DefaultRules;
this.isMenuChat = isMenuChat; this.isMenuChat = isMenuChat;
this.world = world; this.world = world;
@@ -209,12 +207,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
chatScrollPanel.RemoveChildren(); chatScrollPanel.RemoveChildren();
chatScrollPanel.ScrollToBottom(); chatScrollPanel.ScrollToBottom();
foreach (var notification in orderManager.NotificationsCache) foreach (var notification in TextNotificationsManager.Notifications)
if (IsNotificationEligible(notification)) if (IsNotificationEligible(notification))
AddNotification(notification, true); AddNotification(notification, true);
orderManager.AddTextNotification += AddNotificationWrapper;
chatText.IsDisabled = () => !chatEnabled || (world.IsReplay && !Game.Settings.Debug.EnableDebugCommandsInReplays); chatText.IsDisabled = () => !chatEnabled || (world.IsReplay && !Game.Settings.Debug.EnableDebugCommandsInReplays);
if (!isMenuChat) if (!isMenuChat)
@@ -260,7 +256,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Ui.ResetTooltips(); Ui.ResetTooltips();
} }
public void AddNotificationWrapper(TextNotification notification) void INotificationHandler<TextNotification>.Handle(TextNotification notification)
{ {
if (!IsNotificationEligible(notification)) if (!IsNotificationEligible(notification))
return; return;
@@ -315,18 +311,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
notification.Pool == TextNotificationPool.Mission; notification.Pool == TextNotificationPool.Mission;
} }
bool disposed = false;
protected override void Dispose(bool disposing)
{
if (!disposed)
{
orderManager.AddTextNotification -= AddNotificationWrapper;
disposed = true;
}
base.Dispose(disposing);
}
bool IsNotificationMuted(TextNotification notification) bool IsNotificationMuted(TextNotification notification)
{ {
return Game.Settings.Game.HideReplayChat && world.IsReplay && notification.ClientId != TextNotificationsManager.SystemClientId; return Game.Settings.Game.HideReplayChat && world.IsReplay && notification.ClientId != TextNotificationsManager.SystemClientId;

View File

@@ -10,14 +10,12 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Network;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic namespace OpenRA.Mods.Common.Widgets.Logic
{ {
public class IngameTransientNotificationsLogic : ChromeLogic public class IngameTransientNotificationsLogic : ChromeLogic, INotificationHandler<TextNotification>
{ {
readonly OrderManager orderManager;
readonly Ruleset modRules; readonly Ruleset modRules;
readonly TextNotificationsDisplayWidget displayWidget; readonly TextNotificationsDisplayWidget displayWidget;
@@ -28,22 +26,19 @@ namespace OpenRA.Mods.Common.Widgets.Logic
int repetitions; int repetitions;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public IngameTransientNotificationsLogic(Widget widget, OrderManager orderManager, ModData modData, Dictionary<string, MiniYaml> logicArgs) public IngameTransientNotificationsLogic(Widget widget, ModData modData, Dictionary<string, MiniYaml> logicArgs)
{ {
this.orderManager = orderManager;
modRules = modData.DefaultRules; modRules = modData.DefaultRules;
displayWidget = widget.Get<TextNotificationsDisplayWidget>("TRANSIENTS_DISPLAY"); displayWidget = widget.Get<TextNotificationsDisplayWidget>("TRANSIENTS_DISPLAY");
orderManager.AddTextNotification += AddNotificationWrapper;
if (logicArgs.TryGetValue("TransientLineSound", out var yaml)) if (logicArgs.TryGetValue("TransientLineSound", out var yaml))
transientLineSound = yaml.Value; transientLineSound = yaml.Value;
else else
ChromeMetrics.TryGet("TransientLineSound", out transientLineSound); ChromeMetrics.TryGet("TransientLineSound", out transientLineSound);
} }
public void AddNotificationWrapper(TextNotification notification) void INotificationHandler<TextNotification>.Handle(TextNotification notification)
{ {
if (!IsNotificationEligible(notification)) if (!IsNotificationEligible(notification))
return; return;
@@ -83,17 +78,5 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
return notification.Pool == TextNotificationPool.Transients || notification.Pool == TextNotificationPool.Feedback; return notification.Pool == TextNotificationPool.Transients || notification.Pool == TextNotificationPool.Feedback;
} }
bool disposed = false;
protected override void Dispose(bool disposing)
{
if (!disposed)
{
orderManager.AddTextNotification -= AddNotificationWrapper;
disposed = true;
}
base.Dispose(disposing);
}
} }
} }

View File

@@ -20,7 +20,7 @@ using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic namespace OpenRA.Mods.Common.Widgets.Logic
{ {
public class LobbyLogic : ChromeLogic public class LobbyLogic : ChromeLogic, INotificationHandler<TextNotification>
{ {
static readonly Action DoNothing = () => { }; static readonly Action DoNothing = () => { };
@@ -125,7 +125,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
services = modData.Manifest.Get<WebServices>(); services = modData.Manifest.Get<WebServices>();
orderManager.AddTextNotification += AddChatLine;
Game.LobbyInfoChanged += UpdateCurrentMap; Game.LobbyInfoChanged += UpdateCurrentMap;
Game.LobbyInfoChanged += UpdatePlayerList; Game.LobbyInfoChanged += UpdatePlayerList;
Game.LobbyInfoChanged += UpdateDiscordStatus; Game.LobbyInfoChanged += UpdateDiscordStatus;
@@ -493,7 +492,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (disposing && !disposed) if (disposing && !disposed)
{ {
disposed = true; disposed = true;
orderManager.AddTextNotification -= AddChatLine;
Game.LobbyInfoChanged -= UpdateCurrentMap; Game.LobbyInfoChanged -= UpdateCurrentMap;
Game.LobbyInfoChanged -= UpdatePlayerList; Game.LobbyInfoChanged -= UpdatePlayerList;
Game.LobbyInfoChanged -= UpdateDiscordStatus; Game.LobbyInfoChanged -= UpdateDiscordStatus;
@@ -535,7 +533,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
} }
void AddChatLine(TextNotification notification) void INotificationHandler<TextNotification>.Handle(TextNotification notification)
{ {
var chatLine = chatTemplates[notification.Pool].Clone(); var chatLine = chatTemplates[notification.Pool].Clone();
WidgetUtils.SetupTextNotification(chatLine, notification, lobbyChatPanel.Bounds.Width - lobbyChatPanel.ScrollbarWidth, true); WidgetUtils.SetupTextNotification(chatLine, notification, lobbyChatPanel.Bounds.Width - lobbyChatPanel.ScrollbarWidth, true);