Hud refactor (#7202)

Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
Co-authored-by: Jezithyr <jmaster9999@gmail.com>
Co-authored-by: Jezithyr <Jezithyr@gmail.com>
Co-authored-by: Visne <39844191+Visne@users.noreply.github.com>
Co-authored-by: wrexbe <wrexbe@protonmail.com>
Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com>
This commit is contained in:
Jezithyr
2022-10-12 01:16:23 -07:00
committed by GitHub
parent d09fbc1849
commit 571dd4e6d5
168 changed files with 6940 additions and 7817 deletions

View File

@@ -0,0 +1,119 @@
using Content.Client.Resources;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
public sealed class ChannelFilterButton : ContainerButton
{
private static readonly Color ColorNormal = Color.FromHex("#7b7e9e");
private static readonly Color ColorHovered = Color.FromHex("#9699bb");
private static readonly Color ColorPressed = Color.FromHex("#789B8C");
private readonly TextureRect _textureRect;
public readonly ChannelFilterPopup ChatFilterPopup;
private readonly ChatUIController _chatUIController;
private const int FilterDropdownOffset = 120;
public ChannelFilterButton()
{
_chatUIController = UserInterfaceManager.GetUIController<ChatUIController>();
var filterTexture = IoCManager.Resolve<IResourceCache>()
.GetTexture("/Textures/Interface/Nano/filter.svg.96dpi.png");
// needed for same reason as ChannelSelectorButton
Mode = ActionMode.Press;
EnableAllKeybinds = true;
AddChild(
(_textureRect = new TextureRect
{
Texture = filterTexture,
HorizontalAlignment = HAlignment.Center,
VerticalAlignment = VAlignment.Center
})
);
ToggleMode = true;
OnToggled += OnFilterButtonToggled;
ChatFilterPopup = UserInterfaceManager.CreatePopup<ChannelFilterPopup>();
ChatFilterPopup.OnVisibilityChanged += PopupVisibilityChanged;
_chatUIController.FilterableChannelsChanged += ChatFilterPopup.SetChannels;
_chatUIController.UnreadMessageCountsUpdated += ChatFilterPopup.UpdateUnread;
ChatFilterPopup.SetChannels(_chatUIController.FilterableChannels);
}
private void PopupVisibilityChanged(Control control)
{
Pressed = control.Visible;
}
private void OnFilterButtonToggled(ButtonToggledEventArgs args)
{
if (args.Pressed)
{
var globalPos = GlobalPosition;
var (minX, minY) = ChatFilterPopup.MinSize;
var box = UIBox2.FromDimensions(globalPos - (FilterDropdownOffset, 0),
(Math.Max(minX, ChatFilterPopup.MinWidth), minY));
ChatFilterPopup.Open(box);
}
else
{
ChatFilterPopup.Close();
}
}
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
{
// needed since we need EnableAllKeybinds - don't double-send both UI click and Use
if (args.Function == EngineKeyFunctions.Use) return;
base.KeyBindDown(args);
}
private void UpdateChildColors()
{
if (_textureRect == null) return;
switch (DrawMode)
{
case DrawModeEnum.Normal:
_textureRect.ModulateSelfOverride = ColorNormal;
break;
case DrawModeEnum.Pressed:
_textureRect.ModulateSelfOverride = ColorPressed;
break;
case DrawModeEnum.Hover:
_textureRect.ModulateSelfOverride = ColorHovered;
break;
case DrawModeEnum.Disabled:
break;
}
}
protected override void DrawModeChanged()
{
base.DrawModeChanged();
UpdateChildColors();
}
protected override void StylePropertiesChanged()
{
base.StylePropertiesChanged();
UpdateChildColors();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_chatUIController.FilterableChannelsChanged -= ChatFilterPopup.SetChannels;
_chatUIController.UnreadMessageCountsUpdated -= ChatFilterPopup.UpdateUnread;
}
}

View File

@@ -0,0 +1,33 @@
using Content.Shared.Chat;
using Robust.Client.UserInterface.Controls;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
public sealed class ChannelFilterCheckbox : CheckBox
{
public readonly ChatChannel Channel;
public bool IsHidden => Parent == null;
public ChannelFilterCheckbox(ChatChannel channel)
{
Channel = channel;
Text = Loc.GetString($"hud-chatbox-channel-{Channel}");
}
private void UpdateText(int? unread)
{
var name = Loc.GetString($"hud-chatbox-channel-{Channel}");
if (unread > 0)
// todo: proper fluent stuff here.
name += " (" + (unread > 9 ? "9+" : unread) + ")";
Text = name;
}
public void UpdateUnreadCount(int? unread)
{
UpdateText(unread);
}
}

View File

@@ -0,0 +1,10 @@
<controls:ChannelFilterPopup
xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Systems.Chat.Controls">
<PanelContainer Name="FilterPopupPanel" StyleClasses="BorderedWindowPanel">
<BoxContainer Orientation="Horizontal">
<Control MinSize="4 0"/>
<BoxContainer Name="FilterVBox" MinWidth="110" Margin="0 10" Orientation="Vertical" SeparationOverride="4"/>
</BoxContainer>
</PanelContainer>
</controls:ChannelFilterPopup>

View File

@@ -0,0 +1,94 @@
using Content.Shared.Chat;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
[GenerateTypedNameReferences]
public sealed partial class ChannelFilterPopup : Popup
{
// order in which the available channel filters show up when available
private static readonly ChatChannel[] ChannelFilterOrder =
{
ChatChannel.Local,
ChatChannel.Whisper,
ChatChannel.Emotes,
ChatChannel.Radio,
ChatChannel.LOOC,
ChatChannel.OOC,
ChatChannel.Dead,
ChatChannel.Admin,
ChatChannel.Server
};
private readonly Dictionary<ChatChannel, ChannelFilterCheckbox> _filterStates = new();
public event Action<ChatChannel, bool>? OnChannelFilter;
public ChannelFilterPopup()
{
RobustXamlLoader.Load(this);
}
public bool IsActive(ChatChannel channel)
{
return _filterStates.TryGetValue(channel, out var checkbox) && checkbox.Pressed;
}
public ChatChannel GetActive()
{
ChatChannel active = 0;
foreach (var (key, value) in _filterStates)
{
if (value.IsHidden || !value.Pressed)
{
continue;
}
active |= key;
}
return active;
}
public void SetChannels(ChatChannel channels)
{
foreach (var channel in ChannelFilterOrder)
{
if (!_filterStates.TryGetValue(channel, out var checkbox))
{
checkbox = new ChannelFilterCheckbox(channel);
_filterStates.Add(channel, checkbox);
checkbox.OnPressed += CheckboxPressed;
checkbox.Pressed = true;
}
if ((channels & channel) == 0)
{
if (checkbox.Parent == FilterVBox)
{
FilterVBox.RemoveChild(checkbox);
}
}
else if (checkbox.IsHidden)
{
FilterVBox.AddChild(checkbox);
}
}
}
private void CheckboxPressed(ButtonEventArgs args)
{
var checkbox = (ChannelFilterCheckbox) args.Button;
OnChannelFilter?.Invoke(checkbox.Channel, checkbox.Pressed);
}
public void UpdateUnread(ChatChannel channel, int? unread)
{
if (_filterStates.TryGetValue(channel, out var checkbox))
checkbox.UpdateUnreadCount(unread);
}
}

View File

@@ -0,0 +1,110 @@
using Content.Shared.Chat;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
/// <summary>
/// Only needed to avoid the issue where right click on the button closes the popup
/// but leaves the button highlighted.
/// </summary>
public sealed class ChannelSelectorButton : Button
{
private readonly ChannelSelectorPopup _channelSelectorPopup;
public event Action<ChatSelectChannel>? OnChannelSelect;
public ChatSelectChannel SelectedChannel { get; private set; }
private const int SelectorDropdownOffset = 38;
public ChannelSelectorButton()
{
// needed so the popup is untoggled regardless of which key is pressed when hovering this button.
// If we don't have this, then right clicking the button while it's toggled on will hide
// the popup but keep the button toggled on
Name = "ChannelSelector";
Mode = ActionMode.Press;
EnableAllKeybinds = true;
ToggleMode = true;
OnToggled += OnSelectorButtonToggled;
_channelSelectorPopup = UserInterfaceManager.CreatePopup<ChannelSelectorPopup>();
_channelSelectorPopup.Selected += OnChannelSelected;
_channelSelectorPopup.OnVisibilityChanged += OnPopupVisibilityChanged;
if (_channelSelectorPopup.FirstChannel is { } firstSelector)
{
Select(firstSelector);
}
}
private void OnChannelSelected(ChatSelectChannel channel)
{
Select(channel);
}
private void OnPopupVisibilityChanged(Control control)
{
Pressed = control.Visible;
}
protected override void KeyBindDown(GUIBoundKeyEventArgs args)
{
// needed since we need EnableAllKeybinds - don't double-send both UI click and Use
if (args.Function == EngineKeyFunctions.Use) return;
base.KeyBindDown(args);
}
public void Select(ChatSelectChannel channel)
{
if (_channelSelectorPopup.Visible)
{
_channelSelectorPopup.Close();
}
if (SelectedChannel == channel) return;
SelectedChannel = channel;
UpdateChannelSelectButton(channel);
OnChannelSelect?.Invoke(channel);
}
public string ChannelSelectorName(ChatSelectChannel channel)
{
return Loc.GetString($"hud-chatbox-select-channel-{channel}");
}
public Color ChannelSelectColor(ChatSelectChannel channel)
{
return channel switch
{
ChatSelectChannel.Radio => Color.LimeGreen,
ChatSelectChannel.LOOC => Color.MediumTurquoise,
ChatSelectChannel.OOC => Color.LightSkyBlue,
ChatSelectChannel.Dead => Color.MediumPurple,
ChatSelectChannel.Admin => Color.Red,
_ => Color.DarkGray
};
}
public void UpdateChannelSelectButton(ChatSelectChannel channel)
{
Text = ChannelSelectorName(channel);
Modulate = ChannelSelectColor(channel);
}
private void OnSelectorButtonToggled(ButtonToggledEventArgs args)
{
if (args.Pressed)
{
var globalLeft = GlobalPosition.X;
var globalBot = GlobalPosition.Y + Height;
var box = UIBox2.FromDimensions((globalLeft, globalBot), (SizeBox.Width, SelectorDropdownOffset));
_channelSelectorPopup.Open(box);
}
else
{
_channelSelectorPopup.Close();
}
}
}

View File

@@ -0,0 +1,21 @@
using Content.Client.Stylesheets;
using Content.Shared.Chat;
using Robust.Client.UserInterface.Controls;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
public sealed class ChannelSelectorItemButton : Button
{
public readonly ChatSelectChannel Channel;
public bool IsHidden => Parent == null;
public ChannelSelectorItemButton(ChatSelectChannel selector)
{
Channel = selector;
AddStyleClass(StyleNano.StyleClassChatChannelSelectorButton);
Text = ChatUIController.GetChannelSelectorName(selector);
var prefix = ChatUIController.GetChannelSelectorPrefix(selector);
if (prefix != default) Text = Loc.GetString("hud-chatbox-select-name-prefixed", ("name", Text), ("prefix", prefix));
}
}

View File

@@ -0,0 +1,184 @@
using Content.Shared.Chat;
using Robust.Client.UserInterface.Controls;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
public sealed class ChannelSelectorPopup : Popup
{
// order in which the channels show up in the channel selector
public static readonly ChatSelectChannel[] ChannelSelectorOrder =
{
ChatSelectChannel.Local,
ChatSelectChannel.Whisper,
ChatSelectChannel.Emotes,
ChatSelectChannel.Radio,
ChatSelectChannel.LOOC,
ChatSelectChannel.OOC,
ChatSelectChannel.Dead,
ChatSelectChannel.Admin
// NOTE: Console is not in there and it can never be permanently selected.
// You can, however, still submit commands as console by prefixing with /.
};
private readonly BoxContainer _channelSelectorHBox;
private readonly Dictionary<ChatSelectChannel, ChannelSelectorItemButton> _selectorStates = new();
private readonly ChatUIController _chatUIController;
public event Action<ChatSelectChannel>? Selected;
public ChannelSelectorPopup()
{
_channelSelectorHBox = new BoxContainer
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
SeparationOverride = 1
};
_chatUIController = UserInterfaceManager.GetUIController<ChatUIController>();
_chatUIController.SelectableChannelsChanged += SetChannels;
SetChannels(_chatUIController.SelectableChannels);
AddChild(_channelSelectorHBox);
}
public ChatSelectChannel? FirstChannel
{
get
{
foreach (var selector in _selectorStates.Values)
{
if (!selector.IsHidden)
return selector.Channel;
}
return null;
}
}
/*public ChatSelectChannel NextChannel()
{
var nextChannel = ChatUIController.GetNextChannelSelector(_activeSelector);
var index = 0;
while (_selectorStates[(int)nextChannel].IsHidden && index <= _selectorStates.Count)
{
nextChannel = ChatUIController.GetNextChannelSelector(nextChannel);
index++;
}
_activeSelector = nextChannel;
return nextChannel;
}
private void SetupChannels(ChatUIController.ChannelSelectorSetup[] selectorData)
{
_channelSelectorHBox.DisposeAllChildren(); //cleanup old toggles
_selectorStates.Clear();
foreach (var channelSelectorData in selectorData)
{
var newSelectorButton = new ChannelSelectorItemButton(channelSelectorData);
_selectorStates.Add(newSelectorButton);
if (!newSelectorButton.IsHidden)
{
_channelSelectorHBox.AddChild(newSelectorButton);
}
newSelectorButton.OnPressed += OnSelectorPressed;
}
}
private void OnSelectorPressed(BaseButton.ButtonEventArgs args)
{
if (_selectorButton == null) return;
_selectorButton.SelectedChannel = ((ChannelSelectorItemButton) args.Button).Channel;
}
public void HideChannels(params ChatChannel[] channels)
{
foreach (var channel in channels)
{
if (!ChatUIController.ChannelToSelector.TryGetValue(channel, out var selector)) continue;
var selectorbutton = _selectorStates[(int)selector];
if (!selectorbutton.IsHidden)
{
_channelSelectorHBox.RemoveChild(selectorbutton);
if (_activeSelector != selector) continue; // do nothing
if (_channelSelectorHBox.Children.First() is ChannelSelectorItemButton button)
{
_activeSelector = button.Channel;
}
else
{
_activeSelector = ChatSelectChannel.None;
}
}
}
}
*/
private bool IsPreferredAvailable()
{
var preferred = _chatUIController.MapLocalIfGhost(_chatUIController.GetPreferredChannel());
return _selectorStates.TryGetValue(preferred, out var selector) &&
!selector.IsHidden;
}
public void SetChannels(ChatSelectChannel channels)
{
var wasPreferredAvailable = IsPreferredAvailable();
_channelSelectorHBox.RemoveAllChildren();
foreach (var channel in ChannelSelectorOrder)
{
if (!_selectorStates.TryGetValue(channel, out var selector))
{
selector = new ChannelSelectorItemButton(channel);
_selectorStates.Add(channel, selector);
selector.OnPressed += OnSelectorPressed;
}
if ((channels & channel) == 0)
{
if (selector.Parent == _channelSelectorHBox)
{
_channelSelectorHBox.RemoveChild(selector);
}
}
else if (selector.IsHidden)
{
_channelSelectorHBox.AddChild(selector);
}
}
var isPreferredAvailable = IsPreferredAvailable();
if (!wasPreferredAvailable && isPreferredAvailable)
{
Select(_chatUIController.GetPreferredChannel());
}
else if (wasPreferredAvailable && !isPreferredAvailable)
{
Select(ChatSelectChannel.OOC);
}
}
private void OnSelectorPressed(ButtonEventArgs args)
{
var button = (ChannelSelectorItemButton) args.Button;
Select(button.Channel);
}
private void Select(ChatSelectChannel channel)
{
Selected?.Invoke(channel);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_chatUIController.SelectableChannelsChanged -= SetChannels;
}
}

View File

@@ -0,0 +1,53 @@
using Content.Shared.Chat;
using Robust.Client.UserInterface.Controls;
namespace Content.Client.UserInterface.Systems.Chat.Controls;
[Virtual]
public class ChatInputBox : PanelContainer
{
public readonly ChannelSelectorButton ChannelSelector;
public readonly HistoryLineEdit Input;
public readonly ChannelFilterButton FilterButton;
protected readonly BoxContainer Container;
protected ChatChannel ActiveChannel { get; private set; } = ChatChannel.Local;
public ChatInputBox()
{
Container = new BoxContainer
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
SeparationOverride = 4
};
AddChild(Container);
ChannelSelector = new ChannelSelectorButton
{
Name = "ChannelSelector",
ToggleMode = true,
StyleClasses = {"chatSelectorOptionButton"},
MinWidth = 75
};
Container.AddChild(ChannelSelector);
Input = new HistoryLineEdit
{
Name = "Input",
PlaceHolder = Loc.GetString("hud-chatbox-info"),
HorizontalExpand = true,
StyleClasses = {"chatLineEdit"}
};
Container.AddChild(Input);
FilterButton = new ChannelFilterButton
{
Name = "FilterButton",
StyleClasses = {"chatFilterOptionButton"}
};
Container.AddChild(FilterButton);
ChannelSelector.OnChannelSelect += UpdateActiveChannel;
}
private void UpdateActiveChannel(ChatSelectChannel selectedChannel)
{
ActiveChannel = (ChatChannel) selectedChannel;
}
}