Files
tbd-station-14/Content.Client/UserInterface/Systems/Chat/Widgets/ChatBox.xaml.cs
Flipp Syder a3dafd88dc Oldchat (#11913)
* attempt at moving MainViewport to UIWidget

* oldchat (prototype)

* separate oldchat and default ss14 HUD into their own files

* restores original default game screen logic and adds that logic into separated chat game screen

* hand reloading, several tweaks to port oldchat to main ss14 branch

oldchat is currently not selectable

* screen type cvar, gameplay state screen reloading/loading

* reload screen on ui layout cvar change

* fixes up basic reloading (HUD switching is still very bad)

* some UI widget reloading for main UI screen switching

* alert sync on screen change

* inventory reload

* hotbar margin fix

* chat bubbles above viewport

* whoops

* fixes ordering of speech bubble root

* should fix the chat focus issue (at least in-game, not lobby yet)

* should fix up the lobby/game chat focus

* fixes chat for lobby, turns lobby into a UI state

* viewport UI controller

* viewport ratio selection

* whoops

* adds the /tg/ widescreen ratio

* removes warning from inventory UI controller, adds background to separated chat game screen's chat portion

* menu button reload

* unload menu buttons only from gameplay state shutdown

* bugfix

* character button fix

* adds config options for viewport width/UI layout

* variable naming changes, get or null instead of get to avoid exceptions

* moves entity system get into controller
2022-10-17 17:13:41 -05:00

216 lines
6.6 KiB
C#

using Content.Client.Chat;
using Content.Client.Chat.TypingIndicator;
using Content.Client.UserInterface.Systems.Chat.Controls;
using Content.Shared.Chat;
using Content.Shared.Input;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Input;
using Robust.Shared.Utility;
using static Robust.Client.UserInterface.Controls.LineEdit;
namespace Content.Client.UserInterface.Systems.Chat.Widgets;
[GenerateTypedNameReferences]
#pragma warning disable RA0003
public partial class ChatBox : UIWidget
#pragma warning restore RA0003
{
private readonly ChatUIController _controller;
public bool Main { get; set; }
public ChatSelectChannel SelectedChannel => ChatInput.ChannelSelector.SelectedChannel;
public ChatBox()
{
RobustXamlLoader.Load(this);
ChatInput.Input.OnTextEntered += OnTextEntered;
ChatInput.Input.OnKeyBindDown += OnKeyBindDown;
ChatInput.Input.OnTextChanged += OnTextChanged;
ChatInput.ChannelSelector.OnChannelSelect += OnChannelSelect;
ChatInput.FilterButton.ChatFilterPopup.OnChannelFilter += OnChannelFilter;
_controller = UserInterfaceManager.GetUIController<ChatUIController>();
_controller.MessageAdded += OnMessageAdded;
_controller.RegisterChat(this);
}
private void OnTextEntered(LineEditEventArgs args)
{
_controller.SendMessage(this, SelectedChannel);
}
private void OnMessageAdded(StoredChatMessage msg)
{
var text = FormattedMessage.EscapeText(msg.Message);
if (!string.IsNullOrEmpty(msg.MessageWrap))
{
text = string.Format(msg.MessageWrap, text);
}
Logger.DebugS("chat", $"{msg.Channel}: {text}");
if (!ChatInput.FilterButton.ChatFilterPopup.IsActive(msg.Channel))
{
return;
}
msg.Read = true;
var color = msg.MessageColorOverride != Color.Transparent
? msg.MessageColorOverride
: msg.Channel.TextColor();
AddLine(text, color);
}
private void OnChannelSelect(ChatSelectChannel channel)
{
UpdateSelectedChannel();
}
private void OnChannelFilter(ChatChannel channel, bool active)
{
Contents.Clear();
foreach (var message in _controller.History)
{
OnMessageAdded(message);
}
if (active)
{
_controller.ClearUnfilteredUnreads(channel);
}
}
public void AddLine(string message, Color color)
{
var formatted = new FormattedMessage(3);
formatted.PushColor(color);
formatted.AddMarkup(message);
formatted.Pop();
Contents.AddMessage(formatted);
}
public void UpdateSelectedChannel()
{
var (prefixChannel, _) = _controller.SplitInputContents(ChatInput.Input.Text);
var channel = prefixChannel == 0 ? SelectedChannel : prefixChannel;
ChatInput.ChannelSelector.UpdateChannelSelectButton(channel);
}
public void Focus(ChatSelectChannel? channel = null)
{
var input = ChatInput.Input;
var selectStart = Index.End;
if (channel != null)
{
channel = _controller.MapLocalIfGhost(channel.Value);
// Channel not selectable, just do NOTHING (not even focus).
if ((_controller.SelectableChannels & channel.Value) == 0)
return;
var (_, text) = _controller.SplitInputContents(input.Text);
var newPrefix = _controller.GetPrefixFromChannel(channel.Value);
DebugTools.Assert(newPrefix != default, "Focus channel must have prefix!");
if (channel == SelectedChannel)
{
// New selected channel is just the selected channel,
// just remove prefix (if any) and leave text unchanged.
input.Text = text.ToString();
selectStart = Index.Start;
}
else
{
// Change prefix to new focused channel prefix and leave text unchanged.
input.Text = string.Concat(newPrefix.ToString(), " ", text.Span);
selectStart = Index.FromStart(2);
}
ChatInput.ChannelSelector.Select(channel.Value);
}
input.IgnoreNext = true;
input.GrabKeyboardFocus();
input.CursorPosition = input.Text.Length;
input.SelectionStart = selectStart.GetOffset(input.Text.Length);
}
public void CycleChatChannel(bool forward)
{
var idx = Array.IndexOf(ChannelSelectorPopup.ChannelSelectorOrder, SelectedChannel);
do
{
// go over every channel until we find one we can actually select.
idx += forward ? 1 : -1;
idx = MathHelper.Mod(idx, ChannelSelectorPopup.ChannelSelectorOrder.Length);
} while ((_controller.SelectableChannels & ChannelSelectorPopup.ChannelSelectorOrder[idx]) == 0);
SafelySelectChannel(ChannelSelectorPopup.ChannelSelectorOrder[idx]);
}
public void SafelySelectChannel(ChatSelectChannel toSelect)
{
toSelect = _controller.MapLocalIfGhost(toSelect);
if ((_controller.SelectableChannels & toSelect) == 0)
return;
ChatInput.ChannelSelector.Select(toSelect);
}
private void OnKeyBindDown(GUIBoundKeyEventArgs args)
{
if (args.Function == EngineKeyFunctions.TextReleaseFocus)
{
ChatInput.Input.ReleaseKeyboardFocus();
args.Handle();
return;
}
if (args.Function == ContentKeyFunctions.CycleChatChannelForward)
{
CycleChatChannel(true);
args.Handle();
return;
}
if (args.Function == ContentKeyFunctions.CycleChatChannelBackward)
{
CycleChatChannel(false);
args.Handle();
}
}
private void OnTextChanged(LineEditEventArgs args)
{
// Update channel select button to correct channel if we have a prefix.
UpdateSelectedChannel();
// Warn typing indicator about change
_controller.NotifyChatTextChange();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
_controller.UnregisterChat(this);
ChatInput.Input.OnTextEntered -= OnTextEntered;
ChatInput.Input.OnKeyBindDown -= OnKeyBindDown;
ChatInput.Input.OnTextChanged -= OnTextChanged;
ChatInput.ChannelSelector.OnChannelSelect -= OnChannelSelect;
}
}