refactor: simple radial menu for easier creation (#34639)
* it works! kinda * so it works now * minor cleanup * central button now is useful too * more cleanup * minor cleanup * more cleanup * refactor: migrated code from toolbox (as it was rejected as too specific) * feat: moved border drawing for radial menu into RadialMenuTextureButton. Radial menu position setting into was moved to OverrideArrange to not being called on every frame * refactor: major reworks! * renamed DrawBagleSector to DrawAnnulusSector * Remove strange indexing * Regularize math * refactor: re-orienting segment elements to be Y-mirrored * refactor: extracted radial menu radius multiplier property, changed color pallet for radial menu button * refactor: removed icon backgrounds on textures used in current radial menu buttons with sectors, RadialContainer Radius renamed and now actually changed control radius. * refactor: in RadialMenuTextureButtonWithSector all sector colors are converted to and from sRGB in property getter-setters * refactor: renamed srgb to include Srgb suffix so devs gonna see that its srgb clearly * fix: enabled any functional keys pressed when pushing radial menu buttons * fix: radial menu sector now scales with UIScale * fix: accept only one event when clicking on radial menu ContextualButton * fix: now radial menu buttons accepts only click/alt-click, now clicks outside menu closes menu always * feat: simple radial menu prototype for easier creation * refactor: cleanup, restored emote filtering, button models now have class hierarchy * refactor: remove usage of closure from 'outside code' * refactor: remove non existing type from UiControlTest * refactor: remove unused using * refactor: revert ability to declare radial menu layers in xaml, scale 32px sprites using scale in radial menu * refactor: whitespaces * refactor: subscribe for dispose on existing radial menus * feat: now simple radial menu button models can have custom color for each sector background (and hover background color). Also added OpenOverMouseScreenPosition inside SimpleRadialMenu * fix: AI door menu now can be closed by verb if it gets unpowered * refactor: simplify hiding border, extended xml-doc for simple radial menu settings * refactor: remove linq * fix: fix AI radial action serialization using invalid type * refactor: fix duplicate ShowDeviceNotRespondingPopup for AI by properly checking if it can interact * refactor: whitespaces, changed list to array in simple radial button preparing methods --------- Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru> Co-authored-by: Eoin Mcloughlin <helloworld@eoinrul.es>
This commit is contained in:
@@ -1,20 +1,32 @@
|
||||
using Content.Client.Popups;
|
||||
using Content.Client.UserInterface.Controls;
|
||||
using Content.Shared.RCD;
|
||||
using Content.Shared.RCD.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.RCD;
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class RCDMenuBoundUserInterface : BoundUserInterface
|
||||
{
|
||||
[Dependency] private readonly IClyde _displayManager = default!;
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
private static readonly Dictionary<string, (string Tooltip, SpriteSpecifier Sprite)> PrototypesGroupingInfo
|
||||
= new Dictionary<string, (string Tooltip, SpriteSpecifier Sprite)>
|
||||
{
|
||||
["WallsAndFlooring"] = ("rcd-component-walls-and-flooring", new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/Radial/RCD/walls_and_flooring.png"))),
|
||||
["WindowsAndGrilles"] = ("rcd-component-windows-and-grilles", new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/Radial/RCD/windows_and_grilles.png"))),
|
||||
["Airlocks"] = ("rcd-component-airlocks", new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/Radial/RCD/airlocks.png"))),
|
||||
["Electrical"] = ("rcd-component-electrical", new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/Radial/RCD/multicoil.png"))),
|
||||
["Lighting"] = ("rcd-component-lighting", new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/Radial/RCD/lighting.png"))),
|
||||
};
|
||||
|
||||
private RCDMenu? _menu;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly ISharedPlayerManager _playerManager = default!;
|
||||
|
||||
private SimpleRadialMenu? _menu;
|
||||
|
||||
public RCDMenuBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
|
||||
{
|
||||
@@ -25,19 +37,107 @@ public sealed class RCDMenuBoundUserInterface : BoundUserInterface
|
||||
{
|
||||
base.Open();
|
||||
|
||||
_menu = this.CreateWindow<RCDMenu>();
|
||||
_menu.SetEntity(Owner);
|
||||
_menu.SendRCDSystemMessageAction += SendRCDSystemMessage;
|
||||
if (!EntMan.TryGetComponent<RCDComponent>(Owner, out var rcd))
|
||||
return;
|
||||
|
||||
// Open the menu, centered on the mouse
|
||||
var vpSize = _displayManager.ScreenSize;
|
||||
_menu.OpenCenteredAt(_inputManager.MouseScreenPosition.Position / vpSize);
|
||||
_menu = this.CreateWindow<SimpleRadialMenu>();
|
||||
_menu.Track(Owner);
|
||||
var models = ConvertToButtons(rcd.AvailablePrototypes);
|
||||
_menu.SetButtons(models);
|
||||
|
||||
_menu.OpenOverMouseScreenPosition();
|
||||
}
|
||||
|
||||
public void SendRCDSystemMessage(ProtoId<RCDPrototype> protoId)
|
||||
private IEnumerable<RadialMenuNestedLayerOption> ConvertToButtons(HashSet<ProtoId<RCDPrototype>> prototypes)
|
||||
{
|
||||
Dictionary<string, List<RadialMenuActionOption>> buttonsByCategory = new();
|
||||
foreach (var protoId in prototypes)
|
||||
{
|
||||
var prototype = _prototypeManager.Index(protoId);
|
||||
if (!PrototypesGroupingInfo.TryGetValue(prototype.Category, out var groupInfo))
|
||||
continue;
|
||||
|
||||
if (!buttonsByCategory.TryGetValue(prototype.Category, out var list))
|
||||
{
|
||||
list = new List<RadialMenuActionOption>();
|
||||
buttonsByCategory.Add(prototype.Category, list);
|
||||
}
|
||||
|
||||
var actionOption = new RadialMenuActionOption<RCDPrototype>(HandleMenuOptionClick, prototype)
|
||||
{
|
||||
Sprite = prototype.Sprite,
|
||||
ToolTip = GetTooltip(prototype)
|
||||
};
|
||||
list.Add(actionOption);
|
||||
}
|
||||
|
||||
var models = new RadialMenuNestedLayerOption[buttonsByCategory.Count];
|
||||
var i = 0;
|
||||
foreach (var (key, list) in buttonsByCategory)
|
||||
{
|
||||
var groupInfo = PrototypesGroupingInfo[key];
|
||||
models[i] = new RadialMenuNestedLayerOption(list)
|
||||
{
|
||||
Sprite = groupInfo.Sprite,
|
||||
ToolTip = Loc.GetString(groupInfo.Tooltip)
|
||||
};
|
||||
i++;
|
||||
}
|
||||
|
||||
return models;
|
||||
}
|
||||
|
||||
private void HandleMenuOptionClick(RCDPrototype proto)
|
||||
{
|
||||
// A predicted message cannot be used here as the RCD UI is closed immediately
|
||||
// after this message is sent, which will stop the server from receiving it
|
||||
SendMessage(new RCDSystemMessage(protoId));
|
||||
SendMessage(new RCDSystemMessage(proto.ID));
|
||||
|
||||
|
||||
if (_playerManager.LocalSession?.AttachedEntity == null)
|
||||
return;
|
||||
|
||||
var msg = Loc.GetString("rcd-component-change-mode", ("mode", Loc.GetString(proto.SetName)));
|
||||
|
||||
if (proto.Mode is RcdMode.ConstructTile or RcdMode.ConstructObject)
|
||||
{
|
||||
var name = Loc.GetString(proto.SetName);
|
||||
|
||||
if (proto.Prototype != null &&
|
||||
_prototypeManager.TryIndex(proto.Prototype, out var entProto, logError: false))
|
||||
name = entProto.Name;
|
||||
|
||||
msg = Loc.GetString("rcd-component-change-build-mode", ("name", name));
|
||||
}
|
||||
|
||||
// Popup message
|
||||
var popup = EntMan.System<PopupSystem>();
|
||||
popup.PopupClient(msg, Owner, _playerManager.LocalSession.AttachedEntity);
|
||||
}
|
||||
|
||||
private string GetTooltip(RCDPrototype proto)
|
||||
{
|
||||
string tooltip;
|
||||
|
||||
if (proto.Mode is RcdMode.ConstructTile or RcdMode.ConstructObject
|
||||
&& proto.Prototype != null
|
||||
&& _prototypeManager.TryIndex(proto.Prototype, out var entProto, logError: false))
|
||||
{
|
||||
tooltip = Loc.GetString(entProto.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
tooltip = Loc.GetString(proto.SetName);
|
||||
}
|
||||
|
||||
tooltip = OopsConcat(char.ToUpper(tooltip[0]).ToString(), tooltip.Remove(0, 1));
|
||||
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
private static string OopsConcat(string a, string b)
|
||||
{
|
||||
// This exists to prevent Roslyn being clever and compiling something that fails sandbox checks.
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user