Skip to content

Commit 2fee849

Browse files
slarticodefasttaydeo
authored andcommitted
Move scale command to content and turn it into a toolshed command (#39349)
* scale command * fix namespaces
1 parent 71e5d02 commit 2fee849

7 files changed

Lines changed: 227 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Numerics;
2+
using Content.Shared.Sprite;
3+
using Robust.Client.GameObjects;
4+
5+
namespace Content.Client.Sprite;
6+
7+
public sealed class ScaleVisualsSystem : SharedScaleVisualsSystem
8+
{
9+
[Dependency] private readonly SpriteSystem _sprite = default!;
10+
11+
public override void Initialize()
12+
{
13+
base.Initialize();
14+
15+
SubscribeLocalEvent<ScaleVisualsComponent, AppearanceChangeEvent>(OnChangeData);
16+
}
17+
18+
private void OnChangeData(Entity<ScaleVisualsComponent> ent, ref AppearanceChangeEvent args)
19+
{
20+
if (!args.AppearanceData.TryGetValue(ScaleVisuals.Scale, out var scale) ||
21+
args.Sprite == null) return;
22+
23+
// save the original scale
24+
ent.Comp.OriginalScale ??= args.Sprite.Scale;
25+
26+
var vecScale = (Vector2)scale;
27+
_sprite.SetScale((ent.Owner, args.Sprite), vecScale);
28+
}
29+
30+
// revert to the original scale
31+
protected override void ResetScale(Entity<ScaleVisualsComponent> ent)
32+
{
33+
base.ResetScale(ent);
34+
35+
if (ent.Comp.OriginalScale != null)
36+
_sprite.SetScale(ent.Owner, ent.Comp.OriginalScale.Value);
37+
}
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using Content.Shared.Sprite;
2+
3+
namespace Content.Server.Sprite;
4+
5+
public sealed class ScaleVisualsSystem : SharedScaleVisualsSystem;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Numerics;
2+
using Content.Server.Administration;
3+
using Content.Shared.Administration;
4+
using Content.Shared.Sprite;
5+
using Robust.Shared.Physics.Systems;
6+
using Robust.Shared.Toolshed;
7+
8+
namespace Content.Server.Toolshed.Commands.Misc;
9+
10+
/// <summary>
11+
/// Used to change an entity's sprite scale.
12+
/// </summary>
13+
[ToolshedCommand, AdminCommand(AdminFlags.VarEdit)]
14+
public sealed class ScaleCommand : ToolshedCommand
15+
{
16+
private SharedScaleVisualsSystem? _scaleVisuals;
17+
private SharedPhysicsSystem? _physics;
18+
19+
[CommandImplementation("set")]
20+
public IEnumerable<EntityUid> Set([PipedArgument] IEnumerable<EntityUid> input, Vector2 scale)
21+
{
22+
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
23+
24+
foreach (var ent in input)
25+
{
26+
_scaleVisuals.SetSpriteScale(ent, scale);
27+
yield return ent;
28+
}
29+
}
30+
31+
[CommandImplementation("multiply")]
32+
public IEnumerable<EntityUid> Multiply([PipedArgument] IEnumerable<EntityUid> input, float factor)
33+
{
34+
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
35+
36+
foreach (var ent in input)
37+
{
38+
var scale = _scaleVisuals.GetSpriteScale(ent) * factor;
39+
_scaleVisuals.SetSpriteScale(ent, scale);
40+
yield return ent;
41+
}
42+
}
43+
44+
[CommandImplementation("multiplywithfixture")]
45+
public IEnumerable<EntityUid> MultiplyWithFixture([PipedArgument] IEnumerable<EntityUid> input, float factor)
46+
{
47+
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
48+
_physics ??= GetSys<SharedPhysicsSystem>();
49+
50+
foreach (var ent in input)
51+
{
52+
var scale = _scaleVisuals.GetSpriteScale(ent) * factor;
53+
_scaleVisuals.SetSpriteScale(ent, scale);
54+
_physics.ScaleFixtures(ent, factor);
55+
yield return ent;
56+
}
57+
}
58+
59+
[CommandImplementation("get")]
60+
public IEnumerable<Vector2> Get([PipedArgument] IEnumerable<EntityUid> input)
61+
{
62+
_scaleVisuals ??= GetSys<SharedScaleVisualsSystem>();
63+
64+
foreach (var ent in input)
65+
{
66+
yield return _scaleVisuals.GetSpriteScale(ent);
67+
}
68+
}
69+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Numerics;
2+
using Robust.Shared.GameStates;
3+
4+
namespace Content.Shared.Sprite;
5+
6+
/// <summary>
7+
/// Used to set the <see cref="Robust.Client.GameObjects.SpriteComponent.Scale"/> datafield to a certain value from the server.
8+
/// </summary>
9+
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
10+
[Access(typeof(SharedScaleVisualsSystem))]
11+
public sealed partial class ScaleVisualsComponent : Component
12+
{
13+
/// <summary>
14+
/// The current sprite scale.
15+
/// </summary>
16+
[DataField, AutoNetworkedField]
17+
[ViewVariables]
18+
public Vector2 Scale = Vector2.One;
19+
20+
/// <summary>
21+
/// The original sprite scale, which we revert to if this component is removed.
22+
/// Only set on the client.
23+
/// </summary>
24+
[DataField]
25+
[ViewVariables]
26+
public Vector2? OriginalScale;
27+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Numerics;
2+
using Robust.Shared.Serialization;
3+
4+
namespace Content.Shared.Sprite;
5+
6+
public abstract class SharedScaleVisualsSystem : EntitySystem
7+
{
8+
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
9+
10+
public override void Initialize()
11+
{
12+
base.Initialize();
13+
14+
SubscribeLocalEvent<ScaleVisualsComponent, MapInitEvent>(OnMapInit);
15+
SubscribeLocalEvent<ScaleVisualsComponent, ComponentShutdown>(OnComponentShutdown);
16+
}
17+
18+
private void OnMapInit(Entity<ScaleVisualsComponent> ent, ref MapInitEvent args)
19+
{
20+
SetSpriteScale(ent.Owner, ent.Comp.Scale);
21+
}
22+
23+
private void OnComponentShutdown(Entity<ScaleVisualsComponent> ent, ref ComponentShutdown args)
24+
{
25+
ResetScale(ent);
26+
}
27+
28+
protected virtual void ResetScale(Entity<ScaleVisualsComponent> ent)
29+
{
30+
var ev = new ScaleEntityEvent(ent.Owner, Vector2.One);
31+
RaiseLocalEvent(ent.Owner, ref ev);
32+
}
33+
34+
/// <summary>
35+
/// Used to set the <see cref="Robust.Client.GameObjects.SpriteComponent.Scale"/> datafield to a certain value from the server.
36+
/// </summary>
37+
public void SetSpriteScale(EntityUid uid, Vector2 scale)
38+
{
39+
var comp = EnsureComp<ScaleVisualsComponent>(uid);
40+
comp.Scale = scale;
41+
Dirty(uid, comp);
42+
43+
var appearanceComponent = EnsureComp<AppearanceComponent>(uid);
44+
_appearance.SetData(uid, ScaleVisuals.Scale, scale, appearanceComponent);
45+
46+
// Raise an event for content use.
47+
var ev = new ScaleEntityEvent(uid, scale);
48+
RaiseLocalEvent(uid, ref ev);
49+
}
50+
51+
/// <summary>
52+
/// Gets the current scale set by <see cref="SetSpriteScale"/>.
53+
/// This does not include any direct changes made to the SpriteComponent.
54+
/// </summary>
55+
public Vector2 GetSpriteScale(EntityUid uid)
56+
{
57+
if (!TryComp<AppearanceComponent>(uid, out var appearanceComponent))
58+
return Vector2.One;
59+
60+
if (!_appearance.TryGetData<Vector2>(uid, ScaleVisuals.Scale, out var scale, appearanceComponent))
61+
scale = Vector2.One;
62+
63+
return scale;
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Raised when a sprite scale is changed.
69+
/// </summary>
70+
[ByRefEvent]
71+
public readonly record struct ScaleEntityEvent(EntityUid Uid, Vector2 Scale);
72+
73+
[Serializable, NetSerializable]
74+
public enum ScaleVisuals : byte
75+
{
76+
Scale,
77+
}

Resources/Locale/en-US/commands/toolshed-commands.ftl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,14 @@ command-description-xenoartifact-averageResearch =
107107
Calculates amount of research points average generated xeno artifact will output when fully activated.
108108
command-description-xenoartifact-unlockAllNodes =
109109
Unlocks all nodes of artifact.
110+
command-description-jobboard-completeJob =
111+
Completes a given salvage job board job for the station.
112+
command-description-scale-set =
113+
Sets an entity's sprite size to a certain scale (without changing its fixture).
114+
command-description-scale-get =
115+
Get an entity's sprite scale as set by ScaleVisualsComponent. Does not include any changes directly made in the SpriteComponent.
116+
command-description-scale-multiply =
117+
Multiply an entity's sprite size with a certain factor (without changing its fixture).
118+
command-description-scale-multiplywithfixture =
119+
Multiply an entity's sprite size with a certain factor (including its fixture).
120+

Resources/engineCommandPerms.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
- vvread
3030
- vvwrite
3131
- vvinvoke
32-
- scale
3332
- spin
3433

3534
- Flags: DEBUG

0 commit comments

Comments
 (0)