Files
wwdpublic/Content.Client/_White/RadialSelector/BaseRadialSelectorMenuBUI.cs
Fildrance d053e27974 Feature/make radial menu great again (#32653)
* 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

---------

Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
Co-authored-by: Eoin Mcloughlin <helloworld@eoinrul.es>

(cherry picked from commit 68b0dfbc026a07aad2bf3f07d1052f92f899ea31)
2025-10-04 12:50:48 +03:00

157 lines
4.8 KiB
C#

using System.Linq;
using System.Numerics;
using Content.Client.UserInterface.Controls;
using Content.Shared.Construction.Prototypes;
using Content.Shared.RadialSelector;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Prototypes;
// ReSharper disable InconsistentNaming
namespace Content.Client._White.RadialSelector;
public abstract class BasedRadialSelectorMenuBUI : BoundUserInterface
{
[Dependency] protected readonly IPrototypeManager ProtoManager = default!;
[Dependency] protected readonly IResourceCache Resources = default!;
protected readonly SpriteSystem _spriteSystem;
// Used to clearing on state changing
private readonly HashSet<RadialContainer> _cachedContainers = new();
private readonly Vector2 ItemSize = Vector2.One * 64;
protected BasedRadialSelectorMenuBUI(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
_spriteSystem = EntMan.System<SpriteSystem>();
}
protected void CreateMenu(List<RadialSelectorEntry> entries, RadialMenu menu, string parentCategory = "")
{
var container = new RadialContainer
{
Name = !string.IsNullOrEmpty(parentCategory) ? parentCategory : "Main",
};
menu.AddChild(container);
_cachedContainers.Add(container);
foreach (var entry in entries)
{
if (entry.Category != null)
{
var button = CreateButton(entry.Category.Name, _spriteSystem.Frame0(entry.Category.Icon));
button.TargetLayer = entry.Category.Name;
CreateMenu(entry.Category.Entries, menu, entry.Category.Name);
container.AddChild(button);
}
else if (entry.Prototype != null)
{
var name = GetName(entry.Prototype);
var icon = GetTextures(entry);
var button = CreateButton(name, icon);
button.OnButtonUp += _ =>
{
var msg = new RadialSelectorSelectedMessage(entry.Prototype);
SendPredictedMessage(msg);
};
container.AddChild(button);
}
}
}
private string GetName(string proto)
{
if (ProtoManager.TryIndex(proto, out var prototype))
return prototype.Name;
if (ProtoManager.TryIndex(proto, out ConstructionPrototype? constructionPrototype))
return constructionPrototype.Name;
return proto;
}
private List<Texture> GetTextures(RadialSelectorEntry entry)
{
var result = new List<Texture>();
if (entry.Icon is not null)
{
result.Add(_spriteSystem.Frame0(entry.Icon));
return result;
}
if (ProtoManager.TryIndex(entry.Prototype!, out var prototype))
{
result.AddRange(SpriteComponent.GetPrototypeTextures(prototype, Resources).Select(o => o.Default));
return result;
}
if (ProtoManager.TryIndex(entry.Prototype!, out ConstructionPrototype? constructionProto))
{
result.Add(_spriteSystem.Frame0(constructionProto.Icon));
return result;
}
// No icons provided and no icons found in prototypes. There's nothing we can do.
return result;
}
private RadialMenuTextureButton CreateButton(string name, Texture icon)
{
var button = new RadialMenuTextureButton
{
ToolTip = Loc.GetString(name),
StyleClasses = { "RadialMenuButton" },
SetSize = ItemSize
};
var iconScale = ItemSize / icon.Size;
var texture = new TextureRect
{
VerticalAlignment = Control.VAlignment.Center,
HorizontalAlignment = Control.HAlignment.Center,
Texture = icon,
TextureScale = iconScale
};
button.AddChild(texture);
return button;
}
private RadialMenuTextureButton CreateButton(string name, List<Texture> icons)
{
var button = new RadialMenuTextureButton
{
ToolTip = Loc.GetString(name),
StyleClasses = { "RadialMenuButton" },
SetSize = ItemSize
};
var iconScale = ItemSize / icons[0].Size;
var texture = new LayeredTextureRect
{
VerticalAlignment = Control.VAlignment.Center,
HorizontalAlignment = Control.HAlignment.Center,
Textures = icons,
TextureScale = iconScale
};
button.AddChild(texture);
return button;
}
protected void ClearExistingContainers(RadialMenu menu)
{
foreach (var container in _cachedContainers)
menu.RemoveChild(container);
_cachedContainers.Clear();
}
}