Move scale command to content and turn it into a toolshed command (#39349)

* scale command

* fix namespaces
This commit is contained in:
slarticodefast
2025-08-06 14:33:21 +02:00
committed by GitHub
parent 87b0ec090f
commit 71f21d96f7
7 changed files with 224 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
using System.Numerics;
using Content.Shared.Sprite;
using Robust.Client.GameObjects;
namespace Content.Client.Sprite;
public sealed class ScaleVisualsSystem : SharedScaleVisualsSystem
{
[Dependency] private readonly SpriteSystem _sprite = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ScaleVisualsComponent, AppearanceChangeEvent>(OnChangeData);
}
private void OnChangeData(Entity<ScaleVisualsComponent> ent, ref AppearanceChangeEvent args)
{
if (!args.AppearanceData.TryGetValue(ScaleVisuals.Scale, out var scale) ||
args.Sprite == null) return;
// save the original scale
ent.Comp.OriginalScale ??= args.Sprite.Scale;
var vecScale = (Vector2)scale;
_sprite.SetScale((ent.Owner, args.Sprite), vecScale);
}
// revert to the original scale
protected override void ResetScale(Entity<ScaleVisualsComponent> ent)
{
base.ResetScale(ent);
if (ent.Comp.OriginalScale != null)
_sprite.SetScale(ent.Owner, ent.Comp.OriginalScale.Value);
}
}

View File

@@ -0,0 +1,5 @@
using Content.Shared.Sprite;
namespace Content.Server.Sprite;
public sealed class ScaleVisualsSystem : SharedScaleVisualsSystem;

View File

@@ -0,0 +1,69 @@
using System.Numerics;
using Content.Server.Administration;
using Content.Shared.Administration;
using Content.Shared.Sprite;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Toolshed;
namespace Content.Server.Toolshed.Commands.Misc;
/// <summary>
/// Used to change an entity's sprite scale.
/// </summary>
[ToolshedCommand, AdminCommand(AdminFlags.VarEdit)]
public sealed class ScaleCommand : ToolshedCommand
{
private SharedScaleVisualsSystem? _scaleVisuals;
private SharedPhysicsSystem? _physics;
[CommandImplementation("set")]
public IEnumerable<EntityUid> Set([PipedArgument] IEnumerable<EntityUid> input, Vector2 scale)
{
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
foreach (var ent in input)
{
_scaleVisuals.SetSpriteScale(ent, scale);
yield return ent;
}
}
[CommandImplementation("multiply")]
public IEnumerable<EntityUid> Multiply([PipedArgument] IEnumerable<EntityUid> input, float factor)
{
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
foreach (var ent in input)
{
var scale = _scaleVisuals.GetSpriteScale(ent) * factor;
_scaleVisuals.SetSpriteScale(ent, scale);
yield return ent;
}
}
[CommandImplementation("multiplywithfixture")]
public IEnumerable<EntityUid> MultiplyWithFixture([PipedArgument] IEnumerable<EntityUid> input, float factor)
{
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
_physics ??= GetSys<SharedPhysicsSystem>();
foreach (var ent in input)
{
var scale = _scaleVisuals.GetSpriteScale(ent) * factor;
_scaleVisuals.SetSpriteScale(ent, scale);
_physics.ScaleFixtures(ent, factor);
yield return ent;
}
}
[CommandImplementation("get")]
public IEnumerable<Vector2> Get([PipedArgument] IEnumerable<EntityUid> input)
{
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
foreach (var ent in input)
{
yield return _scaleVisuals.GetSpriteScale(ent);
}
}
}

View File

@@ -0,0 +1,27 @@
using System.Numerics;
using Robust.Shared.GameStates;
namespace Content.Shared.Sprite;
/// <summary>
/// Used to set the <see cref="Robust.Client.GameObjects.SpriteComponent.Scale"/> datafield to a certain value from the server.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(SharedScaleVisualsSystem))]
public sealed partial class ScaleVisualsComponent : Component
{
/// <summary>
/// The current sprite scale.
/// </summary>
[DataField, AutoNetworkedField]
[ViewVariables]
public Vector2 Scale = Vector2.One;
/// <summary>
/// The original sprite scale, which we revert to if this component is removed.
/// Only set on the client.
/// </summary>
[DataField]
[ViewVariables]
public Vector2? OriginalScale;
}

View File

@@ -0,0 +1,77 @@
using System.Numerics;
using Robust.Shared.Serialization;
namespace Content.Shared.Sprite;
public abstract class SharedScaleVisualsSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ScaleVisualsComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<ScaleVisualsComponent, ComponentShutdown>(OnComponentShutdown);
}
private void OnMapInit(Entity<ScaleVisualsComponent> ent, ref MapInitEvent args)
{
SetSpriteScale(ent.Owner, ent.Comp.Scale);
}
private void OnComponentShutdown(Entity<ScaleVisualsComponent> ent, ref ComponentShutdown args)
{
ResetScale(ent);
}
protected virtual void ResetScale(Entity<ScaleVisualsComponent> ent)
{
var ev = new ScaleEntityEvent(ent.Owner, Vector2.One);
RaiseLocalEvent(ent.Owner, ref ev);
}
/// <summary>
/// Used to set the <see cref="Robust.Client.GameObjects.SpriteComponent.Scale"/> datafield to a certain value from the server.
/// </summary>
public void SetSpriteScale(EntityUid uid, Vector2 scale)
{
var comp = EnsureComp<ScaleVisualsComponent>(uid);
comp.Scale = scale;
Dirty(uid, comp);
var appearanceComponent = EnsureComp<AppearanceComponent>(uid);
_appearance.SetData(uid, ScaleVisuals.Scale, scale, appearanceComponent);
// Raise an event for content use.
var ev = new ScaleEntityEvent(uid, scale);
RaiseLocalEvent(uid, ref ev);
}
/// <summary>
/// Gets the current scale set by <see cref="SetSpriteScale"/>.
/// This does not include any direct changes made to the SpriteComponent.
/// </summary>
public Vector2 GetSpriteScale(EntityUid uid)
{
if (!TryComp<AppearanceComponent>(uid, out var appearanceComponent))
return Vector2.One;
if (!_appearance.TryGetData<Vector2>(uid, ScaleVisuals.Scale, out var scale, appearanceComponent))
scale = Vector2.One;
return scale;
}
}
/// <summary>
/// Raised when a sprite scale is changed.
/// </summary>
[ByRefEvent]
public readonly record struct ScaleEntityEvent(EntityUid Uid, Vector2 Scale);
[Serializable, NetSerializable]
public enum ScaleVisuals : byte
{
Scale,
}

View File

@@ -96,3 +96,11 @@ command-description-xenoartifact-unlockAllNodes =
Unlocks all nodes of artifact. Unlocks all nodes of artifact.
command-description-jobboard-completeJob = command-description-jobboard-completeJob =
Completes a given salvage job board job for the station. Completes a given salvage job board job for the station.
command-description-scale-set =
Sets an entity's sprite size to a certain scale (without changing its fixture).
command-description-scale-get =
Get an entity's sprite scale as set by ScaleVisualsComponent. Does not include any changes directly made in the SpriteComponent.
command-description-scale-multiply =
Multiply an entity's sprite size with a certain factor (without changing its fixture).
command-description-scale-multiplywithfixture =
Multiply an entity's sprite size with a certain factor (including its fixture).

View File

@@ -8,7 +8,6 @@
- vvread - vvread
- vvwrite - vvwrite
- vvinvoke - vvinvoke
- scale
- spin - spin
- Flags: DEBUG - Flags: DEBUG