Files
RobustToolbox/Robust.Client/UserInterface/CustomControls/EntitySpawnWindow.xaml.cs
T
2025-06-02 14:11:45 -04:00

88 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.Placement;
using Robust.Client.Placement.Modes;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Graphics;
using Robust.Shared.IoC;
using Robust.Shared.IoC.Exceptions;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
namespace Robust.Client.UserInterface.CustomControls
{
[GenerateTypedNameReferences]
public sealed partial class EntitySpawnWindow : DefaultWindow
{
[Dependency] private readonly IPlacementManager _placementManager = default!;
public static string[] InitOpts { get; private set; } = ["Default"];
public EntitySpawnButton? SelectedButton;
public EntityPrototype? SelectedPrototype;
public EntitySpawnWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
MeasureButton.Measure(Vector2Helpers.Infinity);
List<(string Name, int Priority)> modes = [("Default", int.MaxValue)];
foreach (var type in _placementManager.GetAllPlacementModes())
{
if (Attribute.GetCustomAttribute(type, typeof(PlacementModeAttribute)) is not PlacementModeAttribute attribute)
throw new InvalidImplementationException(type, typeof(PlacementMode), $"No {nameof(PlacementModeAttribute)}!");
// Use the override if specified, falling back to the Type name
modes.Add((attribute.Name ?? type.Name, attribute.Priority));
}
InitOpts = modes.OrderByDescending(m => m.Priority).Select(m => m.Name).ToArray();
for (var i = 0; i < InitOpts.Length; i++)
{
OverrideMenu.AddItem(InitOpts[i], i);
}
}
// Create a spawn button and insert it into the start or end of the list.
public EntitySpawnButton InsertEntityButton(EntityPrototype prototype, bool insertFirst, int index)
{
var button = new EntitySpawnButton
{
Prototype = prototype,
Index = index // We track this index purely for debugging.
};
var entityLabelText = string.IsNullOrEmpty(prototype.Name) ? prototype.ID : prototype.Name;
if (!string.IsNullOrWhiteSpace(prototype.EditorSuffix))
{
entityLabelText += $" [{prototype.EditorSuffix}]";
}
button.EntityLabel.Text = entityLabelText;
button.ActualButton.ToolTip = prototype.Description;
if (prototype == SelectedPrototype)
{
SelectedButton = button;
SelectedButton.ActualButton.Pressed = true;
}
var rect = button.EntityTextureRects;
rect.SetPrototype(prototype.ID);
PrototypeList.AddChild(button);
if (insertFirst)
{
button.SetPositionInParent(0);
}
return button;
}
}
}