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:
144
Content.Client/UserInterface/Controls/MenuButton.cs
Normal file
144
Content.Client/UserInterface/Controls/MenuButton.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.UserInterface.Controls;
|
||||
|
||||
public sealed class MenuButton : ContainerButton
|
||||
{
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
public const string StyleClassLabelTopButton = "topButtonLabel";
|
||||
public const string StyleClassRedTopButton = "topButtonLabel";
|
||||
private const float CustomTooltipDelay = 0.4f;
|
||||
|
||||
private static readonly Color ColorNormal = Color.FromHex("#7b7e9e");
|
||||
private static readonly Color ColorRedNormal = Color.FromHex("#FEFEFE");
|
||||
private static readonly Color ColorHovered = Color.FromHex("#9699bb");
|
||||
private static readonly Color ColorRedHovered = Color.FromHex("#FFFFFF");
|
||||
private static readonly Color ColorPressed = Color.FromHex("#789B8C");
|
||||
|
||||
private const float VertPad = 8f;
|
||||
private Color NormalColor => HasStyleClass(StyleClassRedTopButton) ? ColorRedNormal : ColorNormal;
|
||||
private Color HoveredColor => HasStyleClass(StyleClassRedTopButton) ? ColorRedHovered : ColorHovered;
|
||||
|
||||
private BoundKeyFunction _function;
|
||||
private readonly BoxContainer _root;
|
||||
private readonly TextureRect _buttonIcon;
|
||||
private readonly Label _buttonLabel;
|
||||
|
||||
public string AppendStyleClass { set => AddStyleClass(value); }
|
||||
public Texture? Icon { get => _buttonIcon.Texture; set => _buttonIcon.Texture = value; }
|
||||
|
||||
public BoundKeyFunction BoundKey
|
||||
{
|
||||
get => _function;
|
||||
set
|
||||
{
|
||||
_function = value;
|
||||
_buttonLabel.Text = BoundKeyHelper.ShortKeyName(value);
|
||||
}
|
||||
}
|
||||
|
||||
public BoxContainer ButtonRoot => _root;
|
||||
|
||||
public MenuButton()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
TooltipDelay = CustomTooltipDelay;
|
||||
_buttonIcon = new TextureRect()
|
||||
{
|
||||
TextureScale = (0.5f, 0.5f),
|
||||
HorizontalAlignment = HAlignment.Center,
|
||||
VerticalAlignment = VAlignment.Center,
|
||||
VerticalExpand = true,
|
||||
Margin = new Thickness(0, VertPad),
|
||||
ModulateSelfOverride = NormalColor,
|
||||
Stretch = TextureRect.StretchMode.KeepCentered
|
||||
};
|
||||
_buttonLabel = new Label
|
||||
{
|
||||
Text = "",
|
||||
HorizontalAlignment = HAlignment.Center,
|
||||
ModulateSelfOverride = NormalColor,
|
||||
StyleClasses = {StyleClassLabelTopButton}
|
||||
};
|
||||
_root = new BoxContainer
|
||||
{
|
||||
Orientation = BoxContainer.LayoutOrientation.Vertical,
|
||||
Children =
|
||||
{
|
||||
_buttonIcon,
|
||||
_buttonLabel
|
||||
}
|
||||
};
|
||||
AddChild(_root);
|
||||
ToggleMode = true;
|
||||
}
|
||||
|
||||
protected override void EnteredTree()
|
||||
{
|
||||
_inputManager.OnKeyBindingAdded += OnKeyBindingChanged;
|
||||
_inputManager.OnKeyBindingRemoved += OnKeyBindingChanged;
|
||||
_inputManager.OnInputModeChanged += OnKeyBindingChanged;
|
||||
}
|
||||
|
||||
protected override void ExitedTree()
|
||||
{
|
||||
_inputManager.OnKeyBindingAdded -= OnKeyBindingChanged;
|
||||
_inputManager.OnKeyBindingRemoved -= OnKeyBindingChanged;
|
||||
_inputManager.OnInputModeChanged -= OnKeyBindingChanged;
|
||||
}
|
||||
|
||||
|
||||
private void OnKeyBindingChanged(IKeyBinding obj)
|
||||
{
|
||||
_buttonLabel.Text = BoundKeyHelper.ShortKeyName(_function);
|
||||
}
|
||||
|
||||
private void OnKeyBindingChanged()
|
||||
{
|
||||
_buttonLabel.Text = BoundKeyHelper.ShortKeyName(_function);
|
||||
}
|
||||
|
||||
protected override void StylePropertiesChanged()
|
||||
{
|
||||
// colors of children depend on style, so ensure we update when style is changed
|
||||
base.StylePropertiesChanged();
|
||||
UpdateChildColors();
|
||||
}
|
||||
|
||||
private void UpdateChildColors()
|
||||
{
|
||||
if (_buttonIcon == null || _buttonLabel == null) return;
|
||||
switch (DrawMode)
|
||||
{
|
||||
case DrawModeEnum.Normal:
|
||||
_buttonIcon.ModulateSelfOverride = NormalColor;
|
||||
_buttonLabel.ModulateSelfOverride = NormalColor;
|
||||
break;
|
||||
|
||||
case DrawModeEnum.Pressed:
|
||||
_buttonIcon.ModulateSelfOverride = ColorPressed;
|
||||
_buttonLabel.ModulateSelfOverride = ColorPressed;
|
||||
break;
|
||||
|
||||
case DrawModeEnum.Hover:
|
||||
_buttonIcon.ModulateSelfOverride = HoveredColor;
|
||||
_buttonLabel.ModulateSelfOverride = HoveredColor;
|
||||
break;
|
||||
|
||||
case DrawModeEnum.Disabled:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override void DrawModeChanged()
|
||||
{
|
||||
base.DrawModeChanged();
|
||||
UpdateChildColors();
|
||||
}
|
||||
}
|
||||
18
Content.Client/UserInterface/Controls/SlotButton.cs
Normal file
18
Content.Client/UserInterface/Controls/SlotButton.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Content.Client.Inventory;
|
||||
|
||||
namespace Content.Client.UserInterface.Controls
|
||||
{
|
||||
public sealed class SlotButton : SlotControl
|
||||
{
|
||||
public SlotButton(){}
|
||||
|
||||
public SlotButton(ClientInventorySystem.SlotData slotData)
|
||||
{
|
||||
ButtonTexturePath = slotData.TextureName;
|
||||
Blocked = slotData.Blocked;
|
||||
Highlight = slotData.Highlighted;
|
||||
StorageTexturePath = "Slots/back";
|
||||
SlotName = slotData.SlotName;
|
||||
}
|
||||
}
|
||||
}
|
||||
236
Content.Client/UserInterface/Controls/SlotControl.cs
Normal file
236
Content.Client/UserInterface/Controls/SlotControl.cs
Normal file
@@ -0,0 +1,236 @@
|
||||
using Content.Client.Cooldown;
|
||||
using Content.Client.UserInterface.Systems.Inventory.Controls;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Input;
|
||||
|
||||
namespace Content.Client.UserInterface.Controls
|
||||
{
|
||||
[Virtual]
|
||||
public abstract class SlotControl : Control
|
||||
{
|
||||
private const string HighlightShader = "SelectionOutlineInrange";
|
||||
|
||||
public TextureRect ButtonRect { get; }
|
||||
public TextureRect BlockedRect { get; }
|
||||
public TextureRect HighlightRect { get; }
|
||||
public SpriteView SpriteView { get; }
|
||||
public SpriteView HoverSpriteView { get; }
|
||||
public TextureButton StorageButton { get; }
|
||||
public CooldownGraphic CooldownDisplay { get; }
|
||||
|
||||
public EntityUid? Entity => SpriteView.Sprite?.Owner;
|
||||
|
||||
private bool _slotNameSet;
|
||||
|
||||
private string _slotName = "";
|
||||
public string SlotName
|
||||
{
|
||||
get => _slotName;
|
||||
set
|
||||
{
|
||||
//this auto registers the button with it's parent container when it's set
|
||||
if (_slotNameSet)
|
||||
{
|
||||
Logger.Warning("Tried to set slotName after init for:" + Name);
|
||||
return;
|
||||
}
|
||||
_slotNameSet = true;
|
||||
if (Parent is IItemslotUIContainer container)
|
||||
{
|
||||
container.TryRegisterButton(this, value);
|
||||
}
|
||||
Name = "SlotButton_" + value;
|
||||
_slotName = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Highlight { get => HighlightRect.Visible; set => HighlightRect.Visible = value;}
|
||||
|
||||
public bool Blocked { get => BlockedRect.Visible; set => BlockedRect.Visible = value;}
|
||||
|
||||
public Texture BlockedTexture => Theme.ResolveTexture(BlockedTexturePath);
|
||||
|
||||
private string _blockedTexturePath = "";
|
||||
public string BlockedTexturePath
|
||||
{
|
||||
get => _blockedTexturePath;
|
||||
set
|
||||
{
|
||||
_blockedTexturePath = value;
|
||||
BlockedRect.Texture = Theme.ResolveTexture(_blockedTexturePath);
|
||||
}
|
||||
}
|
||||
|
||||
public Texture ButtonTexture => Theme.ResolveTexture(ButtonTexturePath);
|
||||
|
||||
private string _buttonTexturePath = "";
|
||||
public string ButtonTexturePath {
|
||||
get => _buttonTexturePath;
|
||||
set
|
||||
{
|
||||
_buttonTexturePath = value;
|
||||
ButtonRect.Texture = Theme.ResolveTexture(_buttonTexturePath);
|
||||
}
|
||||
}
|
||||
|
||||
public Texture StorageTexture => Theme.ResolveTexture(StorageTexturePath);
|
||||
|
||||
private string _storageTexturePath = "";
|
||||
public string StorageTexturePath
|
||||
{
|
||||
get => _buttonTexturePath;
|
||||
set
|
||||
{
|
||||
_storageTexturePath = value;
|
||||
StorageButton.TextureNormal = Theme.ResolveTexture(_storageTexturePath);
|
||||
}
|
||||
}
|
||||
|
||||
private string _highlightTexturePath = "";
|
||||
public string HighlightTexturePath
|
||||
{
|
||||
get => _highlightTexturePath;
|
||||
set
|
||||
{
|
||||
_highlightTexturePath = value;
|
||||
HighlightRect.Texture = Theme.ResolveTexture(_highlightTexturePath);
|
||||
}
|
||||
}
|
||||
|
||||
public event Action<GUIBoundKeyEventArgs, SlotControl>? Pressed;
|
||||
public event Action<GUIBoundKeyEventArgs, SlotControl>? Unpressed;
|
||||
public event Action<GUIBoundKeyEventArgs, SlotControl>? StoragePressed;
|
||||
public event Action<GUIMouseHoverEventArgs, SlotControl>? Hover;
|
||||
|
||||
public bool EntityHover => HoverSpriteView.Sprite != null;
|
||||
public bool MouseIsHovering;
|
||||
|
||||
public SlotControl()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
Name = "SlotButton_null";
|
||||
MinSize = (64, 64);
|
||||
AddChild(ButtonRect = new TextureRect
|
||||
{
|
||||
TextureScale = (2, 2),
|
||||
MouseFilter = MouseFilterMode.Stop
|
||||
});
|
||||
AddChild(HighlightRect = new TextureRect
|
||||
{
|
||||
Visible = false,
|
||||
TextureScale = (2, 2),
|
||||
MouseFilter = MouseFilterMode.Ignore
|
||||
});
|
||||
|
||||
ButtonRect.OnKeyBindDown += OnButtonPressed;
|
||||
ButtonRect.OnKeyBindUp += OnButtonUnpressed;
|
||||
|
||||
AddChild(SpriteView = new SpriteView
|
||||
{
|
||||
Scale = (2, 2),
|
||||
OverrideDirection = Direction.South
|
||||
});
|
||||
|
||||
AddChild(HoverSpriteView = new SpriteView
|
||||
{
|
||||
Scale = (2, 2),
|
||||
OverrideDirection = Direction.South
|
||||
});
|
||||
|
||||
AddChild(StorageButton = new TextureButton
|
||||
{
|
||||
Scale = (0.75f, 0.75f),
|
||||
HorizontalAlignment = HAlignment.Right,
|
||||
VerticalAlignment = VAlignment.Bottom,
|
||||
Visible = false,
|
||||
});
|
||||
|
||||
StorageButton.OnKeyBindDown += args =>
|
||||
{
|
||||
if (args.Function != EngineKeyFunctions.UIClick)
|
||||
{
|
||||
OnButtonPressed(args);
|
||||
}
|
||||
};
|
||||
|
||||
StorageButton.OnPressed += OnStorageButtonPressed;
|
||||
|
||||
ButtonRect.OnMouseEntered += _ =>
|
||||
{
|
||||
MouseIsHovering = true;
|
||||
};
|
||||
ButtonRect.OnMouseEntered += OnButtonHover;
|
||||
|
||||
ButtonRect.OnMouseExited += _ =>
|
||||
{
|
||||
MouseIsHovering = false;
|
||||
ClearHover();
|
||||
};
|
||||
|
||||
AddChild(CooldownDisplay = new CooldownGraphic
|
||||
{
|
||||
Visible = false,
|
||||
});
|
||||
|
||||
AddChild(BlockedRect = new TextureRect
|
||||
{
|
||||
TextureScale = (2, 2),
|
||||
MouseFilter = MouseFilterMode.Stop,
|
||||
Visible = false
|
||||
});
|
||||
|
||||
HighlightTexturePath = "slot_highlight";
|
||||
BlockedTexturePath = "blocked";
|
||||
}
|
||||
|
||||
public void ClearHover()
|
||||
{
|
||||
if (!EntityHover)
|
||||
return;
|
||||
|
||||
var tempQualifier = HoverSpriteView.Sprite;
|
||||
if (tempQualifier != null)
|
||||
{
|
||||
IoCManager.Resolve<IEntityManager>().DeleteEntity(tempQualifier.Owner);
|
||||
}
|
||||
|
||||
HoverSpriteView.Sprite = null;
|
||||
}
|
||||
|
||||
private void OnButtonPressed(GUIBoundKeyEventArgs args)
|
||||
{
|
||||
Pressed?.Invoke(args, this);
|
||||
}
|
||||
|
||||
private void OnButtonUnpressed(GUIBoundKeyEventArgs args)
|
||||
{
|
||||
Unpressed?.Invoke(args, this);
|
||||
}
|
||||
|
||||
private void OnStorageButtonPressed(BaseButton.ButtonEventArgs args)
|
||||
{
|
||||
if (args.Event.Function == EngineKeyFunctions.UIClick)
|
||||
{
|
||||
StoragePressed?.Invoke(args.Event, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
Pressed?.Invoke(args.Event, this);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnButtonHover(GUIMouseHoverEventArgs args)
|
||||
{
|
||||
Hover?.Invoke(args, this);
|
||||
}
|
||||
|
||||
protected override void OnThemeUpdated()
|
||||
{
|
||||
StorageButton.TextureNormal = Theme.ResolveTexture(_storageTexturePath);
|
||||
ButtonRect.Texture = Theme.ResolveTexture(_buttonTexturePath);
|
||||
HighlightRect.Texture = Theme.ResolveTexture(_highlightTexturePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user