Rework chat line templates and logic

- Extract chat line templates and logic so they can be reused across widgets
- Make text notification styling entirely template driven (by removing chat color configuration and making color optional for `TextNotification`)
- Add a new TextNotificationsDisplay widget (based on and replacing ChatDisplayWidget)
- Add timestamp support to text notifications
This commit is contained in:
Ivaylo Draganov
2021-06-15 17:32:21 +03:00
committed by Matthias Mailänder
parent 8416dc3f2d
commit 9e92340ea7
20 changed files with 330 additions and 363 deletions

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly Widget newSpectatorTemplate;
readonly ScrollPanelWidget lobbyChatPanel;
readonly Widget chatTemplate;
readonly Dictionary<TextNotificationPool, Widget> chatTemplates = new Dictionary<TextNotificationPool, Widget>();
readonly TextFieldWidget chatTextField;
readonly CachedTransform<int, string> chatDisabledLabel;
@@ -398,6 +398,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (skirmishMode)
disconnectButton.Text = "Back";
if (logicArgs.TryGetValue("ChatTemplates", out var templateIds))
{
foreach (var item in templateIds.Nodes)
{
var key = FieldLoader.GetValue<TextNotificationPool>("key", item.Key);
chatTemplates[key] = Ui.LoadWidget(item.Value.Value, null, new WidgetArgs());
}
}
var chatMode = lobby.Get<ButtonWidget>("CHAT_MODE");
chatMode.GetText = () => teamChat ? "Team" : "All";
chatMode.OnClick = () => teamChat ^= true;
@@ -442,7 +451,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
chatDisabledLabel = new CachedTransform<int, string>(x => x > 0 ? $"Chat available in {x} seconds..." : "Chat Disabled");
lobbyChatPanel = lobby.Get<ScrollPanelWidget>("CHAT_DISPLAY");
chatTemplate = lobbyChatPanel.Get("CHAT_TEMPLATE");
lobbyChatPanel.RemoveChildren();
var settingsButton = lobby.GetOrNull<ButtonWidget>("SETTINGS_BUTTON");
@@ -510,13 +518,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
}
void AddChatLine(TextNotification chatLine)
void AddChatLine(TextNotification notification)
{
var template = (ContainerWidget)chatTemplate.Clone();
LobbyUtils.SetupChatLine(template, DateTime.Now, chatLine);
var chatLine = chatTemplates[notification.Pool].Clone();
WidgetUtils.SetupTextNotification(chatLine, notification, lobbyChatPanel.Bounds.Width - lobbyChatPanel.ScrollbarWidth, true);
var scrolledToBottom = lobbyChatPanel.ScrolledToBottom;
lobbyChatPanel.AddChild(template);
lobbyChatPanel.AddChild(chatLine);
if (scrolledToBottom)
lobbyChatPanel.ScrollToBottom(smooth: true);

View File

@@ -648,37 +648,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
HideChildWidget(parent, "STATUS_IMAGE");
}
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 = chatLine.Prefix + ":";
var font = Game.Renderer.Fonts[nameLabel.Font];
var nameSize = font.Measure(nameText);
timeLabel.GetText = () => $"{time.Hour:D2}:{time.Minute:D2}";
nameLabel.GetColor = () => chatLine.PrefixColor;
nameLabel.GetText = () => nameText;
nameLabel.Bounds.Width = nameSize.X;
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
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)
{
textLabel.Bounds.Height += dh;
template.Bounds.Height += dh;
}
}
static void HideChildWidget(Widget parent, string widgetId)
{
var widget = parent.GetOrNull(widgetId);