Find PlacementModes by attribute

This commit is contained in:
Tayrtahn
2025-06-02 13:34:25 -04:00
parent 44ea2cd396
commit 27efb6c5cf
15 changed files with 60 additions and 18 deletions

View File

@@ -48,6 +48,11 @@ namespace Robust.Client.Placement
void ToggleEraser();
void ToggleEraserHijacked(PlacementHijack hijack);
/// <summary>
/// Returns an array containing the names of all placement modes that can be used.
/// </summary>
string[] GetAllPlacementModes();
void FrameUpdate(FrameEventArgs e);
}
}

View File

@@ -9,6 +9,7 @@ using Robust.Shared.Prototypes;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class AlignSimilar : PlacementMode
{
private const uint SnapToRange = 50;

View File

@@ -7,6 +7,7 @@ using Robust.Shared.Maths;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class SnapgridBorder : SnapgridCenter
{
public override bool HasLineMode => true;

View File

@@ -9,6 +9,7 @@ using Robust.Shared.Maths;
namespace Robust.Client.Placement.Modes
{
[Virtual]
[PlacementMode]
public class SnapgridCenter : PlacementMode
{
protected MapGridComponent? Grid;

View File

@@ -5,6 +5,7 @@ using Robust.Shared.Map.Components;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class AlignTileAny : PlacementMode
{
public override bool HasLineMode => true;

View File

@@ -5,6 +5,7 @@ using Robust.Shared.Map.Components;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class AlignTileDense : PlacementMode
{
public override bool HasLineMode => true;

View File

@@ -6,6 +6,7 @@ using Robust.Shared.Maths;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class AlignTileEmpty : PlacementMode
{
public override bool HasLineMode => true;
@@ -23,7 +24,7 @@ namespace Robust.Client.Placement.Modes
if (gridIdOpt is { } gridId && gridId.IsValid())
{
var mapGrid = pManager.EntityManager.GetComponent<MapGridComponent>(gridId);
CurrentTile = pManager.EntityManager.System<SharedMapSystem>().GetTileRef(gridId, mapGrid ,MouseCoords);
CurrentTile = pManager.EntityManager.System<SharedMapSystem>().GetTileRef(gridId, mapGrid, MouseCoords);
tileSize = mapGrid.TileSize; //convert from ushort to float
}

View File

@@ -5,6 +5,7 @@ using Robust.Shared.Map.Components;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class AlignTileNonDense : PlacementMode
{
public override bool HasLineMode => true;

View File

@@ -5,6 +5,7 @@ using Vector2 = System.Numerics.Vector2;
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class AlignWall : PlacementMode
{
public AlignWall(PlacementManager pMan) : base(pMan) { }
@@ -35,8 +36,8 @@ namespace Robust.Client.Placement.Modes
}
var closestNode = (from Vector2 node in nodes
orderby (node - MouseCoords.Position).LengthSquared() ascending
select node).First();
orderby (node - MouseCoords.Position).LengthSquared() ascending
select node).First();
MouseCoords = new EntityCoordinates(MouseCoords.EntityId,
closestNode + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y));

View File

@@ -7,6 +7,7 @@ namespace Robust.Client.Placement.Modes
/// <summary>
/// Snaps in edge on one axis, center in the other.
/// </summary>
[PlacementMode]
public sealed class AlignWallProper : PlacementMode
{
public AlignWallProper(PlacementManager pMan) : base(pMan)

View File

@@ -2,6 +2,7 @@
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class PlaceFree : PlacementMode
{
public PlaceFree(PlacementManager pMan) : base(pMan) { }

View File

@@ -2,6 +2,7 @@
namespace Robust.Client.Placement.Modes
{
[PlacementMode]
public sealed class PlaceNearby : PlacementMode
{
public PlaceNearby(PlacementManager pMan) : base(pMan) { }

View File

@@ -0,0 +1,15 @@
using System;
using JetBrains.Annotations;
namespace Robust.Client.Placement.Modes;
[AttributeUsage(AttributeTargets.Class)]
[BaseTypeRequired(typeof(PlacementMode))]
[MeansImplicitUse]
public sealed class PlacementModeAttribute(string? name = null) : Attribute
{
/// <summary>
/// The display name of this placement mode.
/// </summary>
public string? Name { get; } = name;
}

View File

@@ -22,6 +22,8 @@ using Robust.Shared.Utility;
using Robust.Shared.Log;
using Direction = Robust.Shared.Maths.Direction;
using Robust.Shared.Map.Components;
using Robust.Client.Placement.Modes;
using Robust.Shared.IoC.Exceptions;
namespace Robust.Client.Placement
{
@@ -497,6 +499,21 @@ namespace Robust.Client.Placement
else Clear();
}
public string[] GetAllPlacementModes()
{
List<string> names = [];
var modes = _reflectionManager.FindTypesWithAttribute<PlacementModeAttribute>();
foreach (var type in modes)
{
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
names.Add(attribute.Name ?? type.Name);
}
return names.ToArray();
}
public void BeginPlacing(PlacementInformation info, PlacementHijack? hijack = null)
{
BeginHijackedPlacing(info, hijack);

View File

@@ -2,8 +2,10 @@ using System.Collections.Generic;
using System.Numerics;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.Placement;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Graphics;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
@@ -12,21 +14,9 @@ namespace Robust.Client.UserInterface.CustomControls
[GenerateTypedNameReferences]
public sealed partial class EntitySpawnWindow : DefaultWindow
{
public static readonly string[] InitOpts =
{
"Default",
"PlaceFree",
"PlaceNearby",
"SnapgridCenter",
"SnapgridBorder",
"AlignSimilar",
"AlignTileAny",
"AlignTileEmpty",
"AlignTileNonDense",
"AlignTileDense",
"AlignWall",
"AlignWallProper",
};
[Dependency] private readonly IPlacementManager _placementManager = default!;
public static string[] InitOpts { get; private set; } = ["Default"];
public EntitySpawnButton? SelectedButton;
public EntityPrototype? SelectedPrototype;
@@ -34,9 +24,13 @@ namespace Robust.Client.UserInterface.CustomControls
public EntitySpawnWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
MeasureButton.Measure(Vector2Helpers.Infinity);
List<string> modes = ["Default"];
modes.AddRange(_placementManager.GetAllPlacementModes());
InitOpts = modes.ToArray();
for (var i = 0; i < InitOpts.Length; i++)
{
OverrideMenu.AddItem(InitOpts[i], i);