ContextMenu (#3286)

* ContextMenu

* Updating to WPF.

* Updating to WPF.

* Margins
This commit is contained in:
Daniel Castro Razo
2021-02-25 19:42:16 -06:00
committed by GitHub
parent 51182c8469
commit f30a4d8a52
10 changed files with 1059 additions and 231 deletions

View File

@@ -0,0 +1,233 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Client.GameObjects.Components;
using Content.Client.UserInterface.Stylesheets;
using Content.Client.Utility;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Vector2 = Robust.Shared.Maths.Vector2;
namespace Content.Client.UserInterface.ContextMenu
{
public abstract class ContextMenuElement : Control
{
private static readonly Color HoverColor = Color.DarkSlateGray;
protected internal readonly ContextMenuPopup? ParentMenu;
protected ContextMenuElement(ContextMenuPopup? parentMenu)
{
ParentMenu = parentMenu;
MouseFilter = MouseFilterMode.Stop;
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
if (UserInterfaceManager.CurrentlyHovered == this)
{
handle.DrawRect(PixelSizeBox, HoverColor);
}
}
}
public sealed class SingleContextElement : ContextMenuElement
{
public event Action? OnMouseHovering;
public event Action? OnExitedTree;
public IEntity ContextEntity{ get; }
public readonly StackContextElement? Pre;
public ISpriteComponent? SpriteComp { get; }
public InteractionOutlineComponent? OutlineComponent { get; }
public int OriginalDrawDepth { get; }
public bool DrawOutline { get; set; }
public SingleContextElement(IEntity entity, StackContextElement? pre, ContextMenuPopup? parentMenu) : base(parentMenu)
{
Pre = pre;
ContextEntity = entity;
if (ContextEntity.TryGetComponent(out ISpriteComponent? sprite))
{
SpriteComp = sprite;
OriginalDrawDepth = SpriteComp.DrawDepth;
}
OutlineComponent = ContextEntity.GetComponentOrNull<InteractionOutlineComponent>();
AddChild(
new HBoxContainer
{
SeparationOverride = 6,
Children =
{
new LayoutContainer
{
Children = { new SpriteView { Sprite = SpriteComp } }
},
new Label
{
Text = Loc.GetString(UserInterfaceManager.DebugMonitors.Visible ? $"{ContextEntity.Name} ({ContextEntity.Uid})" : ContextEntity.Name)
}
}
}
);
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
if (UserInterfaceManager.CurrentlyHovered == this)
{
OnMouseHovering?.Invoke();
}
}
protected override void ExitedTree()
{
OnExitedTree?.Invoke();
base.ExitedTree();
}
}
public sealed class StackContextElement : ContextMenuElement
{
public event Action? OnExitedTree;
public readonly TimeSpan HoverDelay = TimeSpan.FromSeconds(0.2);
public HashSet<IEntity> ContextEntities { get; }
public readonly StackContextElement? Pre;
private readonly SpriteView _spriteView;
private readonly Label _label;
public int EntitiesCount => ContextEntities.Count;
public StackContextElement(IEnumerable<IEntity> entities, StackContextElement? pre, ContextMenuPopup? parentMenu)
: base(parentMenu)
{
Pre = pre;
ContextEntities = new(entities);
_spriteView = new SpriteView
{
Sprite = ContextEntities.First().GetComponent<ISpriteComponent>()
};
_label = new Label
{
Text = Loc.GetString(ContextEntities.Count.ToString()),
StyleClasses = { StyleNano.StyleClassContextMenuCount }
};
LayoutContainer.SetAnchorPreset(_label, LayoutContainer.LayoutPreset.BottomRight);
LayoutContainer.SetGrowHorizontal(_label, LayoutContainer.GrowDirection.Begin);
LayoutContainer.SetGrowVertical(_label, LayoutContainer.GrowDirection.Begin);
AddChild(
new HBoxContainer()
{
SeparationOverride = 6,
Children =
{
new LayoutContainer { Children = { _spriteView, _label } },
new HBoxContainer()
{
SeparationOverride = 6,
Children =
{
new Label
{
Text = Loc.GetString(ContextEntities.First().Name)
},
new TextureRect
{
Texture = IoCManager.Resolve<IResourceCache>().GetTexture("/Textures/Interface/VerbIcons/group.svg.96dpi.png"),
Stretch = TextureRect.StretchMode.KeepCentered,
}
},
}
},
}
);
}
protected override void ExitedTree()
{
OnExitedTree?.Invoke();
base.ExitedTree();
}
public void RemoveEntity(IEntity entity)
{
ContextEntities.Remove(entity);
_label.Text = Loc.GetString(ContextEntities.Count.ToString());
_spriteView.Sprite = ContextEntities.FirstOrDefault(e => !e.Deleted)?.GetComponent<ISpriteComponent>();
}
}
public sealed class ContextMenuPopup : Popup
{
private static readonly Color DefaultColor = Color.FromHex("#1116");
private static readonly Color MarginColor = Color.FromHex("#222E");
private const int MaxItemsBeforeScroll = 10;
public VBoxContainer List { get; }
public int Depth { get; }
public ContextMenuPopup(int depth = 0)
{
Depth = depth;
AddChild(new ScrollContainer
{
HScrollEnabled = false,
Children = { new PanelContainer
{
Children = { (List = new VBoxContainer()) },
PanelOverride = new StyleBoxFlat { BackgroundColor = MarginColor }
}}
});
}
public void AddToMenu(ContextMenuElement element)
{
List.AddChild(new PanelContainer
{
Children = { element },
Margin = new Thickness(0,0,0, 2),
PanelOverride = new StyleBoxFlat {BackgroundColor = DefaultColor}
});
}
public void RemoveFromMenu(ContextMenuElement element)
{
List.RemoveChild(element.Parent!);
InvalidateMeasure();
}
protected override Vector2 MeasureOverride(Vector2 availableSize)
{
if (List.ChildCount == 0)
{
return Vector2.Zero;
}
List.Measure(availableSize);
var listSize = List.DesiredSize;
if (List.ChildCount < MaxItemsBeforeScroll)
{
return listSize;
}
listSize.Y = MaxItemsBeforeScroll * 32 + MaxItemsBeforeScroll * 2;
return listSize;
}
}
}

View File

@@ -0,0 +1,265 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Client.UserInterface;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Client.UserInterface.ContextMenu
{
public interface IContextMenuView : IDisposable
{
Dictionary<IEntity, ContextMenuElement> Elements { get; set; }
Stack<ContextMenuPopup> Menus { get; }
event EventHandler<(GUIBoundKeyEventArgs, SingleContextElement)>? OnKeyBindDownSingle;
event EventHandler<SingleContextElement>? OnMouseEnteredSingle;
event EventHandler<SingleContextElement>? OnMouseExitedSingle;
event EventHandler<SingleContextElement>? OnMouseHoveringSingle;
event EventHandler<(GUIBoundKeyEventArgs, StackContextElement)>? OnKeyBindDownStack;
event EventHandler<StackContextElement>? OnMouseEnteredStack;
event EventHandler<ContextMenuElement>? OnExitedTree;
event EventHandler? OnCloseRootMenu;
event EventHandler<int>? OnCloseChildMenu;
void UpdateParents(ContextMenuElement element);
void RemoveEntity(IEntity element);
void AddRootMenu(List<IEntity> entities);
void AddChildMenu(IEnumerable<IEntity> entities, Vector2 position, StackContextElement? stack);
void CloseContextPopups(int depth);
void CloseContextPopups();
void OnGroupingContextMenuChanged(int obj);
}
public partial class ContextMenuView : IContextMenuView
{
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
public Stack<ContextMenuPopup> Menus { get; }
public Dictionary<IEntity, ContextMenuElement> Elements { get; set; }
public event EventHandler<(GUIBoundKeyEventArgs, SingleContextElement)>? OnKeyBindDownSingle;
public event EventHandler<SingleContextElement>? OnMouseEnteredSingle;
public event EventHandler<SingleContextElement>? OnMouseExitedSingle;
public event EventHandler<SingleContextElement>? OnMouseHoveringSingle;
public event EventHandler<(GUIBoundKeyEventArgs, StackContextElement)>? OnKeyBindDownStack;
public event EventHandler<StackContextElement>? OnMouseEnteredStack;
public event EventHandler<ContextMenuElement>? OnExitedTree;
public event EventHandler? OnCloseRootMenu;
public event EventHandler<int>? OnCloseChildMenu;
public ContextMenuView()
{
IoCManager.InjectDependencies(this);
Menus = new Stack<ContextMenuPopup>();
Elements = new Dictionary<IEntity, ContextMenuElement>();
}
public void AddRootMenu(List<IEntity> entities)
{
Elements = new Dictionary<IEntity, ContextMenuElement>(entities.Count);
var rootContextMenu = new ContextMenuPopup();
rootContextMenu.OnPopupHide += () => OnCloseRootMenu?.Invoke(this, EventArgs.Empty);
Menus.Push(rootContextMenu);
var entitySpriteStates = GroupEntities(entities);
var orderedStates = entitySpriteStates.ToList();
orderedStates.Sort((x, y) => string.CompareOrdinal(x.First().Prototype!.Name, y.First().Prototype!.Name));
AddToUI(orderedStates);
_userInterfaceManager.ModalRoot.AddChild(rootContextMenu);
var size = rootContextMenu.List.CombinedMinimumSize;
var box = UIBox2.FromDimensions(_userInterfaceManager.MousePositionScaled, size);
rootContextMenu.Open(box);
}
public void AddChildMenu(IEnumerable<IEntity> entities, Vector2 position, StackContextElement? stack)
{
if (stack == null) return;
var newDepth = stack.ParentMenu?.Depth + 1 ?? 1;
var childContextMenu = new ContextMenuPopup(newDepth);
Menus.Push(childContextMenu);
var orderedStates = GroupEntities(entities, newDepth);
AddToUI(orderedStates, stack);
_userInterfaceManager.ModalRoot.AddChild(childContextMenu);
var size = childContextMenu.List.CombinedMinimumSize;
childContextMenu.Open(UIBox2.FromDimensions(position + (stack.Width, 0), size));
}
private void AddToUI(List<List<IEntity>> entities, StackContextElement? stack = null)
{
if (entities.Count == 1)
{
foreach (var entity in entities[0])
{
AddSingleContextElement(entity, stack);
}
}
else
{
foreach (var entity in entities)
{
if (entity.Count == 1)
{
AddSingleContextElement(entity[0], stack);
}
else
{
AddStackContextElement(entity, stack);
}
}
}
}
private void AddSingleContextElement(IEntity entity, StackContextElement? pre)
{
if (Menus.TryPeek(out var menu))
{
var single = new SingleContextElement(entity, pre, menu);
single.OnKeyBindDown += args => OnKeyBindDownSingle?.Invoke(this, (args, single));
single.OnMouseEntered += _ => OnMouseEnteredSingle?.Invoke(this, single);
single.OnMouseExited += _ => OnMouseExitedSingle?.Invoke(this, single);
single.OnMouseHovering += () => OnMouseHoveringSingle?.Invoke(this, single);
single.OnExitedTree += () => OnExitedTree?.Invoke(this, single);
UpdateElements(entity, single);
menu.AddToMenu(single);
}
}
private void AddStackContextElement(IEnumerable<IEntity> entities, StackContextElement? pre)
{
if (Menus.TryPeek(out var menu))
{
var stack = new StackContextElement(entities, pre, menu);
stack.OnKeyBindDown += args => OnKeyBindDownStack?.Invoke(this, (args, stack));
stack.OnMouseEntered += _ => OnMouseEnteredStack?.Invoke(this, stack);
stack.OnExitedTree += () => OnExitedTree?.Invoke(this, stack);
foreach (var entity in entities)
{
UpdateElements(entity, stack);
}
menu.AddToMenu(stack);
}
}
private void UpdateElements(IEntity entity, ContextMenuElement element)
{
if (Elements.ContainsKey(entity))
{
Elements[entity] = element;
}
else
{
Elements.Add(entity, element);
}
}
private void RemoveFromUI(ContextMenuElement element)
{
var menu = element.ParentMenu;
if (menu != null)
{
menu.RemoveFromMenu(element);
if (menu.List.ChildCount == 0)
{
OnCloseChildMenu?.Invoke(this, menu.Depth - 1);
}
}
}
public void RemoveEntity(IEntity entity)
{
var element = Elements[entity];
switch (element)
{
case SingleContextElement singleContextElement:
RemoveFromUI(singleContextElement);
UpdateBranch(entity, singleContextElement.Pre);
break;
case StackContextElement stackContextElement:
stackContextElement.RemoveEntity(entity);
if (stackContextElement.EntitiesCount == 0)
{
RemoveFromUI(stackContextElement);
}
UpdateBranch(entity, stackContextElement.Pre);
break;
default:
throw new ArgumentOutOfRangeException(nameof(element));
}
Elements.Remove(entity);
}
private void UpdateBranch(IEntity entity, StackContextElement? stack)
{
while (stack != null)
{
stack.RemoveEntity(entity);
if (stack.EntitiesCount == 0)
{
RemoveFromUI(stack);
}
stack = stack.Pre;
}
}
public void UpdateParents(ContextMenuElement element)
{
switch (element)
{
case SingleContextElement singleContextElement:
if (singleContextElement.Pre != null)
{
Elements[singleContextElement.ContextEntity] = singleContextElement.Pre;
}
break;
case StackContextElement stackContextElement:
if (stackContextElement.Pre != null)
{
foreach (var entity in stackContextElement.ContextEntities)
{
Elements[entity] = stackContextElement.Pre;
}
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(element));
}
}
public void CloseContextPopups()
{
while (Menus.Count > 0)
{
Menus.Pop().Dispose();
}
Elements.Clear();
}
public void CloseContextPopups(int depth)
{
while (Menus.Count > 0 && Menus.Peek().Depth > depth)
{
Menus.Pop().Dispose();
}
}
public void Dispose()
{
CloseContextPopups();
}
}
}

View File

@@ -0,0 +1,118 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Client.UserInterface.ContextMenu
{
public partial class ContextMenuView
{
public const int GroupingTypesCount = 2;
private int GroupingContextMenuType { get; set; }
public void OnGroupingContextMenuChanged(int obj)
{
CloseContextPopups();
GroupingContextMenuType = obj;
}
private List<List<IEntity>> GroupEntities(IEnumerable<IEntity> entities, int depth = 0)
{
if (GroupingContextMenuType == 0)
{
var newEntities = entities.GroupBy(e => e, new PrototypeContextMenuComparer()).ToList();
return newEntities.Select(grp => grp.ToList()).ToList();
}
else
{
var newEntities = entities.GroupBy(e => e, new PrototypeAndStatesContextMenuComparer(depth)).ToList();
return newEntities.Select(grp => grp.ToList()).ToList();
}
}
private sealed class PrototypeAndStatesContextMenuComparer : IEqualityComparer<IEntity>
{
private static readonly List<Func<IEntity, IEntity, bool>> EqualsList = new()
{
(a, b) => a.Prototype!.ID == b.Prototype!.ID,
(a, b) =>
{
var xStates = a.GetComponent<ISpriteComponent>().AllLayers.Where(e => e.Visible).Select(s => s.RsiState.Name);
var yStates = b.GetComponent<ISpriteComponent>().AllLayers.Where(e => e.Visible).Select(s => s.RsiState.Name);
return xStates.OrderBy(t => t).SequenceEqual(yStates.OrderBy(t => t));
},
};
private static readonly List<Func<IEntity, int>> GetHashCodeList = new()
{
e => EqualityComparer<string>.Default.GetHashCode(e.Prototype!.ID),
e =>
{
var hash = 0;
foreach (var element in e.GetComponent<ISpriteComponent>().AllLayers.Where(obj => obj.Visible).Select(s => s.RsiState.Name))
{
hash ^= EqualityComparer<string>.Default.GetHashCode(element!);
}
return hash;
},
};
private static int Count => EqualsList.Count - 1;
private readonly int _depth;
public PrototypeAndStatesContextMenuComparer(int step = 0)
{
_depth = step > Count ? Count : step;
}
public bool Equals(IEntity? x, IEntity? y)
{
if (x == null)
{
return y == null;
}
return y != null && EqualsList[_depth](x, y);
}
public int GetHashCode(IEntity e)
{
return GetHashCodeList[_depth](e);
}
}
private sealed class PrototypeContextMenuComparer : IEqualityComparer<IEntity>
{
public bool Equals(IEntity? x, IEntity? y)
{
if (x == null)
{
return y == null;
}
if (y != null)
{
if (x.Prototype?.ID == y.Prototype?.ID)
{
var xStates = x.GetComponent<ISpriteComponent>().AllLayers.Where(e => e.Visible).Select(s => s.RsiState.Name);
var yStates = y.GetComponent<ISpriteComponent>().AllLayers.Where(e => e.Visible).Select(s => s.RsiState.Name);
return xStates.OrderBy(t => t).SequenceEqual(yStates.OrderBy(t => t));
}
}
return false;
}
public int GetHashCode(IEntity e)
{
var hash = EqualityComparer<string>.Default.GetHashCode(e.Prototype?.ID!);
foreach (var element in e.GetComponent<ISpriteComponent>().AllLayers.Where(obj => obj.Visible).Select(s => s.RsiState.Name))
{
hash ^= EqualityComparer<string>.Default.GetHashCode(element!);
}
return hash;
}
}
}
}