Move Text handling to its own class

This commit is contained in:
teinarss
2021-06-03 19:33:32 +02:00
committed by reaperrr
parent 2a26ddc622
commit 82115c6bf7
25 changed files with 97 additions and 74 deletions

View File

@@ -474,11 +474,6 @@ namespace OpenRA
JoinLocal();
ChromeMetrics.TryGet("ChatMessageColor", out chatMessageColor);
ChromeMetrics.TryGet("SystemMessageColor", out systemMessageColor);
if (!ChromeMetrics.TryGet("SystemMessageLabel", out systemMessageLabel))
systemMessageLabel = "Battlefield Control";
ModData.LoadScreen.StartGame(args);
}
@@ -559,9 +554,6 @@ namespace OpenRA
// Note: These delayed actions should only be used by widgets or disposing objects
// - things that depend on a particular world should be queuing them on the world actor.
static volatile ActionQueue delayedActions = new ActionQueue();
static Color systemMessageColor = Color.White;
static Color chatMessageColor = Color.White;
static string systemMessageLabel;
public static void RunAfterTick(Action a) { delayedActions.Add(a, RunTime); }
public static void RunAfterDelay(int delayMilliseconds, Action a) { delayedActions.Add(a, RunTime + delayMilliseconds); }
@@ -579,7 +571,7 @@ namespace OpenRA
Log.Write("debug", "Taking screenshot " + path);
Renderer.SaveScreenshot(path);
Debug("Saved screenshot " + filename);
TextNotificationsManager.Debug("Saved screenshot " + filename);
}
}
@@ -911,26 +903,6 @@ namespace OpenRA
state = RunStatus.Success;
}
public static void AddSystemLine(string text)
{
AddSystemLine(systemMessageLabel, text);
}
public static void AddSystemLine(string name, string text)
{
OrderManager.AddChatLine(name, systemMessageColor, text, systemMessageColor);
}
public static void AddChatLine(string name, Color nameColor, string text)
{
OrderManager.AddChatLine(name, nameColor, text, chatMessageColor);
}
public static void Debug(string s, params object[] args)
{
AddSystemLine("Debug", string.Format(s, args));
}
public static void Disconnect()
{
OrderManager.World?.TraitDict.PrintReport();

View File

@@ -212,8 +212,8 @@ namespace OpenRA
Log.Write("debug", e.ToString());
// HACK: this can hopefully go away in the future
Game.Debug("Ignoring malformed order that would have crashed the game");
Game.Debug("Please file a bug report and include the replay from this match");
TextNotificationsManager.Debug("Ignoring malformed order that would have crashed the game");
TextNotificationsManager.Debug("Please file a bug report and include the replay from this match");
return null;
}

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Network
{
// Server message
case "Message":
Game.AddSystemLine(order.TargetString);
TextNotificationsManager.AddSystemLine(order.TargetString);
break;
// Reports that the target player disconnected
@@ -70,7 +70,7 @@ namespace OpenRA.Network
if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
suffix += " (Ally)";
Game.AddChatLine(client.Name + suffix, client.Color, message);
TextNotificationsManager.AddChatLine(client.Name + suffix, client.Color, message);
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)
Game.AddChatLine(prefix + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine(prefix + client.Name, client.Color, message);
break;
}
@@ -93,7 +93,7 @@ namespace OpenRA.Network
{
// Validate before adding the line
if (client.IsObserver || (player != null && player.WinState != WinState.Undefined))
Game.AddChatLine("[Spectators] " + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine("[Spectators] " + client.Name, client.Color, message);
break;
}
@@ -103,7 +103,7 @@ namespace OpenRA.Network
&& world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Undefined;
if (valid && (isSameTeam || world.IsReplay))
Game.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message);
break;
}
@@ -133,7 +133,7 @@ namespace OpenRA.Network
FieldLoader.GetValue<int>("SaveSyncFrame", saveSyncFrame.Value.Value);
}
else
Game.AddSystemLine("The game has started.");
TextNotificationsManager.AddSystemLine("The game has started.");
Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular);
break;
@@ -151,7 +151,7 @@ namespace OpenRA.Network
case "GameSaved":
if (!orderManager.World.IsReplay)
Game.AddSystemLine("Game saved");
TextNotificationsManager.AddSystemLine("Game saved");
foreach (var nsr in orderManager.World.WorldActor.TraitsImplementing<INotifyGameSaved>())
nsr.GameSaved(orderManager.World);
@@ -171,7 +171,7 @@ namespace OpenRA.Network
if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1)
{
var pausetext = $"The game is {(pause ? "paused" : "un-paused")} by {client.Name}";
Game.AddSystemLine(pausetext);
TextNotificationsManager.AddSystemLine(pausetext);
}
orderManager.World.Paused = pause;

View File

@@ -188,9 +188,9 @@ namespace OpenRA.Orders
static Order CheckSameOrder(IOrderTargeter iot, Order order)
{
if (order == null && iot.OrderID != null)
Game.Debug("BUG: in order targeter - decided on {0} but then didn't order", iot.OrderID);
TextNotificationsManager.Debug("BUG: in order targeter - decided on {0} but then didn't order", iot.OrderID);
else if (order != null && iot.OrderID != order.OrderString)
Game.Debug("BUG: in order targeter - decided on {0} but ordered {1}", iot.OrderID, order.OrderString);
TextNotificationsManager.Debug("BUG: in order targeter - decided on {0} but ordered {1}", iot.OrderID, order.OrderString);
return order;
}

View File

@@ -0,0 +1,51 @@
#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 OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA
{
public static class TextNotificationsManager
{
static Color systemMessageColor = Color.White;
static Color chatMessageColor = Color.White;
static string systemMessageLabel;
static TextNotificationsManager()
{
ChromeMetrics.TryGet("ChatMessageColor", out chatMessageColor);
ChromeMetrics.TryGet("SystemMessageColor", out systemMessageColor);
if (!ChromeMetrics.TryGet("SystemMessageLabel", out systemMessageLabel))
systemMessageLabel = "Battlefield Control";
}
public static void AddSystemLine(string text)
{
AddSystemLine(systemMessageLabel, text);
}
public static void AddSystemLine(string name, string text)
{
Game.OrderManager.AddChatLine(name, systemMessageColor, text, systemMessageColor);
}
public static void AddChatLine(string name, Color nameColor, string text)
{
Game.OrderManager.AddChatLine(name, nameColor, text, chatMessageColor);
}
public static void Debug(string s, params object[] args)
{
AddSystemLine("Debug", string.Format(s, args));
}
}
}

View File

@@ -226,7 +226,7 @@ namespace OpenRA
if (right.TryGetClrValue(out int c))
{
Game.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
TextNotificationsManager.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
return new LuaCustomClrObject(a + FromFacing(c));
}
@@ -243,7 +243,7 @@ namespace OpenRA
if (right.TryGetClrValue(out int c))
{
Game.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
TextNotificationsManager.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
return new LuaCustomClrObject(a - FromFacing(c));
}

View File

@@ -143,7 +143,7 @@ namespace OpenRA
switch (key.ToString())
{
case "Length": return Length;
case "Range": Game.Debug("WDist.Range is deprecated. Use WDist.Length instead"); return Length;
case "Range": TextNotificationsManager.Debug("WDist.Range is deprecated. Use WDist.Length instead"); return Length;
default: throw new LuaException($"WDist does not define a member '{key}'");
}
}

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Mods.Common
public static void BotDebug(string s, params object[] args)
{
if (Game.Settings.Debug.BotDebug)
Game.Debug(s, args);
TextNotificationsManager.Debug(s, args);
}
}
}

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Commands
if (command.Value != null)
command.Value.InvokeCommand(name.ToLowerInvariant(), message.Substring(1 + name.Length).Trim());
else
Game.Debug("{0} is not a valid command.", name);
TextNotificationsManager.Debug("{0} is not a valid command.", name);
return false;
}

View File

@@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Commands
if (!developerMode.Enabled)
{
Game.Debug("Cheats are disabled.");
TextNotificationsManager.Debug("Cheats are disabled.");
return;
}

View File

@@ -42,14 +42,14 @@ namespace OpenRA.Mods.Common.Commands
public void InvokeCommand(string name, string arg)
{
Game.Debug("Here are the available commands:");
TextNotificationsManager.Debug("Here are the available commands:");
foreach (var key in console.Commands.Keys)
{
if (!helpDescriptions.TryGetValue(key, out var description))
description = "no description available.";
Game.Debug("{0}: {1}", key, description);
TextNotificationsManager.Debug("{0}: {1}", key, description);
}
}

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Mods.Common.Scripting
if (value.TryGetClrValue(out int facing))
{
facingInit.Initialize(WAngle.FromFacing(facing));
Game.Debug("Initializing Facing with integers is deprecated. Use Angle instead.");
TextNotificationsManager.Debug("Initializing Facing with integers is deprecated. Use Angle instead.");
return facingInit;
}
}

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.Common.Scripting.Global
void Deprecated()
{
Game.Debug("The Facing table is deprecated. Use Angle instead.");
TextNotificationsManager.Debug("The Facing table is deprecated. Use Angle instead.");
}
public int North { get { Deprecated(); return 0; } }

View File

@@ -194,7 +194,7 @@ namespace OpenRA.Mods.Common.Scripting
return;
var c = color.HasValue ? color.Value : Color.White;
Game.AddChatLine(prefix, c, text);
TextNotificationsManager.AddChatLine(prefix, c, text);
}
[Desc("Display a system message to the player. If 'prefix' is nil the default system prefix is used.")]
@@ -204,9 +204,9 @@ namespace OpenRA.Mods.Common.Scripting
return;
if (string.IsNullOrEmpty(prefix))
Game.AddSystemLine(text);
TextNotificationsManager.AddSystemLine(text);
else
Game.AddSystemLine(prefix, text);
TextNotificationsManager.AddSystemLine(prefix, text);
}
[Desc("Displays a debug message to the player, if \"Show Map Debug Messages\" is checked in the settings.")]
@@ -215,7 +215,7 @@ namespace OpenRA.Mods.Common.Scripting
if (string.IsNullOrEmpty(text) || !Game.Settings.Debug.LuaDebug)
return;
Game.Debug(text);
TextNotificationsManager.Debug(text);
}
[Desc("Display a text message at the specified location.")]

View File

@@ -36,14 +36,14 @@ namespace OpenRA.Mods.Common.Scripting
[Desc("Activate the actor's Airstrike Power. DEPRECATED! Will be removed.")]
public void SendAirstrike(WPos target, bool randomize = true, int facing = 0)
{
Game.Debug("SendAirstrike is deprecated. Use TargetAirstrike instead.");
TextNotificationsManager.Debug("SendAirstrike is deprecated. Use TargetAirstrike instead.");
ap.SendAirstrike(Self, target, randomize ? (WAngle?)null : WAngle.FromFacing(facing));
}
[Desc("Activate the actor's Airstrike Power. DEPRECATED! Will be removed.")]
public void SendAirstrikeFrom(CPos from, CPos to)
{
Game.Debug("SendAirstrikeFrom is deprecated. Use TargetAirstrike instead.");
TextNotificationsManager.Debug("SendAirstrikeFrom is deprecated. Use TargetAirstrike instead.");
var i = Self.World.Map.CenterOfCell(from);
var j = Self.World.Map.CenterOfCell(to);

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Scripting
[Desc("Activate the actor's Paratroopers Power. Returns the aircraft that will drop the reinforcements. DEPRECATED! Will be removed.")]
public Actor[] ActivateParatroopers(WPos target, int facing = -1)
{
Game.Debug("SendParatroopersFrom is deprecated. Use TargetParatroopers instead.");
TextNotificationsManager.Debug("SendParatroopersFrom is deprecated. Use TargetParatroopers instead.");
var actors = pp.SendParatroopers(Self, target, facing == -1 ? (WAngle?)null : WAngle.FromFacing(facing));
return actors.Aircraft;
}

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits
if (info.SuppressNotifications)
return;
Game.AddSystemLine(player.PlayerName + " is defeated.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is defeated.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)
@@ -112,7 +112,7 @@ namespace OpenRA.Mods.Common.Traits
if (info.SuppressNotifications)
return;
Game.AddSystemLine(player.PlayerName + " is victorious.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is victorious.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)

View File

@@ -273,7 +273,7 @@ namespace OpenRA.Mods.Common.Traits
return;
}
Game.Debug("Cheat used: {0} by {1}{2}", order.OrderString, self.Owner.PlayerName, debugSuffix);
TextNotificationsManager.Debug("Cheat used: {0} by {1}{2}", order.OrderString, self.Owner.PlayerName, debugSuffix);
}
bool IUnlocksRenderPlayer.RenderPlayerUnlocked => Enabled;

View File

@@ -137,7 +137,7 @@ namespace OpenRA.Mods.Common.Traits
if (info.SuppressNotifications)
return;
Game.AddSystemLine(player.PlayerName + " is defeated.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is defeated.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)
@@ -150,7 +150,7 @@ namespace OpenRA.Mods.Common.Traits
if (info.SuppressNotifications)
return;
Game.AddSystemLine(player.PlayerName + " is victorious.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is victorious.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)

View File

@@ -158,7 +158,7 @@ namespace OpenRA.Mods.Common.Traits
{
if (ticksRemaining == m * 60 * ticksPerSecond)
{
Game.AddSystemLine(Notification.F(m, m > 1 ? "s" : null));
TextNotificationsManager.AddSystemLine(Notification.F(m, m > 1 ? "s" : null));
var faction = self.World.LocalPlayer == null ? null : self.World.LocalPlayer.Faction.InternalName;
Game.Sound.PlayNotification(self.World.Map.Rules, self.World.LocalPlayer, "Speech", info.TimeLimitWarnings[m], faction);
@@ -172,7 +172,7 @@ namespace OpenRA.Mods.Common.Traits
countdownLabel.GetText = () => null;
if (!info.SkipTimerExpiredNotification)
Game.AddSystemLine("Time limit has expired.");
TextNotificationsManager.AddSystemLine("Time limit has expired.");
}
}
}

View File

@@ -252,7 +252,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
catch (Exception ex)
{
Game.Debug("Failed to delete save file '{0}'. See the logs for details.", savePath);
TextNotificationsManager.Debug("Failed to delete save file '{0}'. See the logs for details.", savePath);
Log.Write("debug", ex.ToString());
return;
}

View File

@@ -320,7 +320,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
catch (Exception ex)
{
Game.Debug("Failed to delete map '{0}'. See the debug.log file for details.", map);
TextNotificationsManager.Debug("Failed to delete map '{0}'. See the debug.log file for details.", map);
Log.Write("debug", ex.ToString());
}

View File

@@ -29,12 +29,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (Game.Settings.Sound.Mute)
{
Game.Sound.MuteAudio();
Game.AddSystemLine("Audio muted");
TextNotificationsManager.AddSystemLine("Audio muted");
}
else
{
Game.Sound.UnmuteAudio();
Game.AddSystemLine("Audio unmuted");
TextNotificationsManager.AddSystemLine("Audio unmuted");
}
return true;

View File

@@ -499,7 +499,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
catch (Exception ex)
{
Game.Debug("Failed to delete replay file '{0}'. See the logs for details.", replay.FilePath);
TextNotificationsManager.Debug("Failed to delete replay file '{0}'. See the logs for details.", replay.FilePath);
Log.Write("debug", ex.ToString());
return;
}

View File

@@ -261,12 +261,12 @@ namespace OpenRA.Mods.Common.Widgets
// Check if selecting actors on the screen has selected new units
if (ownUnitsOnScreen.Count > World.Selection.Actors.Count())
Game.AddSystemLine("Selected across screen");
TextNotificationsManager.AddSystemLine("Selected across screen");
else
{
// Select actors in the world that have highest selection priority
ownUnitsOnScreen = SelectActorsInWorld(World, null, eligiblePlayers).SubsetWithHighestSelectionPriority(e.Modifiers).ToList();
Game.AddSystemLine("Selected across map");
TextNotificationsManager.AddSystemLine("Selected across map");
}
World.Selection.Combine(World, ownUnitsOnScreen, false, false);
@@ -293,12 +293,12 @@ namespace OpenRA.Mods.Common.Widgets
// Check if selecting actors on the screen has selected new units
if (newSelection.Count > World.Selection.Actors.Count())
Game.AddSystemLine("Selected across screen");
TextNotificationsManager.AddSystemLine("Selected across screen");
else
{
// Select actors in the world that have the same selection class as one of the already selected actors
newSelection = SelectActorsInWorld(World, selectedClasses, eligiblePlayers).ToList();
Game.AddSystemLine("Selected across map");
TextNotificationsManager.AddSystemLine("Selected across map");
}
World.Selection.Combine(World, newSelection, true, false);