Skip to content

Commit

Permalink
Remove obsoletion warnings from StackVisualizer
Browse files Browse the repository at this point in the history
 This is a combination of 4 commits.
 This is the 1st commit message:

Move StackVisualizer to Content.Shared.StackVisualizerComponent (temporarily), update content. Visualizer disabled for now.

 This is the commit message space-wizards#2:

Convert StackVisualizer from AppearanceVisualizer to VisualizerSystem

 This is the commit message space-wizards#3:

Split StackVisualizerComponent into seperate file, add some comments
    Intention had been to merge StackVisualizerComponent and
    StackComponent, but there are some prototypes which use
    StackVisualizerComponent without StackComponent (to great effect)

 This is the commit message space-wizards#4:

Don't netsync stack visualizer components
  • Loading branch information
eoineoineoin committed Jan 6, 2023
1 parent a0f18e3 commit b1b3382
Show file tree
Hide file tree
Showing 19 changed files with 442 additions and 390 deletions.
162 changes: 0 additions & 162 deletions Content.Client/Stack/StackVisualizer.cs

This file was deleted.

133 changes: 133 additions & 0 deletions Content.Client/Stack/StackVisualizerSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Content.Shared.Rounding;
using Content.Shared.Stacks;
using Robust.Client.GameObjects;
using Robust.Shared.Utility;

namespace Content.Client.Stack
{
/// <summary>
/// Visualizer for items that come in stacks and have different appearance
/// depending on the size of the stack. Visualizer can work by switching between different
/// icons in <c>_spriteLayers</c> or if the sprite layers are supposed to be composed as transparent layers.
/// The former behavior is default and the latter behavior can be defined in prototypes.
///
/// <example>
/// <para>To define a Stack Visualizer prototype insert the following
/// snippet (you can skip Appearance if already defined)
/// </para>
/// <code>
/// - type: StackVisualizer
/// stackLayers:
/// - goldbar_10
/// - goldbar_20
/// - goldbar_30
/// </code>
/// </example>
/// <example>
/// <para>Defining a stack visualizer with composable transparent layers</para>
/// <code>
/// - type: StackVisualizer
/// composite: true
/// stackLayers:
/// - cigarette_1
/// - cigarette_2
/// - cigarette_3
/// - cigarette_4
/// - cigarette_5
/// - cigarette_6
/// </code>
/// </example>
/// <seealso cref="_spriteLayers"/>
/// </summary>
public sealed class StackVisualizerSystem : VisualizerSystem<StackVisualizerComponent>
{
/// <summary>
/// Default IconLayer stack.
/// </summary>
private const int IconLayer = 0;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<StackVisualizerComponent, ComponentInit>(OnComponentInit);
}

private void OnComponentInit(EntityUid uid, StackVisualizerComponent stackComponent, ComponentInit args)
{
if (stackComponent._isComposite
&& stackComponent._spriteLayers.Count > 0
&& IoCManager.Resolve<IEntityManager>().TryGetComponent<ISpriteComponent?>(uid,
out var spriteComponent))
{
var spritePath = stackComponent._spritePath ?? spriteComponent.BaseRSI!.Path!;

foreach (var sprite in stackComponent._spriteLayers)
{
spriteComponent.LayerMapReserveBlank(sprite);
spriteComponent.LayerSetSprite(sprite, new SpriteSpecifier.Rsi(spritePath, sprite));
spriteComponent.LayerSetVisible(sprite, false);
}
}
}

protected override void OnAppearanceChange(EntityUid uid, StackVisualizerComponent stackComponent, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
{
return;
}

if (stackComponent._isComposite)
{
ProcessCompositeSprites(uid, stackComponent, args.Sprite);
}
else
{
ProcessOpaqueSprites(uid, stackComponent, args.Sprite);
}
}

private void ProcessOpaqueSprites(EntityUid uid, StackVisualizerComponent stackComponent, ISpriteComponent spriteComponent)
{
// Skip processing if no actual
if (!AppearanceSystem.TryGetData<int>(uid, StackVisuals.Actual, out var actual)) return;
if (!AppearanceSystem.TryGetData<int>(uid, StackVisuals.MaxCount, out var maxCount))
{
maxCount = stackComponent._spriteLayers.Count;
}

var activeLayer = ContentHelpers.RoundToEqualLevels(actual, maxCount, stackComponent._spriteLayers.Count);
spriteComponent.LayerSetState(IconLayer, stackComponent._spriteLayers[activeLayer]);
}

private void ProcessCompositeSprites(EntityUid uid, StackVisualizerComponent stackComponent, ISpriteComponent spriteComponent)
{
// If hidden, don't render any sprites
if (AppearanceSystem.TryGetData<bool>(uid, StackVisuals.Hide, out var hide)
&& hide)
{
foreach (var transparentSprite in stackComponent._spriteLayers)
{
spriteComponent.LayerSetVisible(transparentSprite, false);
}

return;
}

// Skip processing if no actual/maxCount
if (!AppearanceSystem.TryGetData<int>(uid, StackVisuals.Actual, out var actual)) return;
if (!AppearanceSystem.TryGetData<int>(uid, StackVisuals.MaxCount, out var maxCount))
{
maxCount = stackComponent._spriteLayers.Count;
}


var activeTill = ContentHelpers.RoundToNearestLevels(actual, maxCount, stackComponent._spriteLayers.Count);
for (var i = 0; i < stackComponent._spriteLayers.Count; i++)
{
spriteComponent.LayerSetVisible(stackComponent._spriteLayers[i], i < activeTill);
}
}
}
}
1 change: 1 addition & 0 deletions Content.Shared/Stacks/StackComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Utility;

namespace Content.Shared.Stacks
{
Expand Down
74 changes: 74 additions & 0 deletions Content.Shared/Stacks/StackVisualizerComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Robust.Shared.Utility;

namespace Content.Shared.Stacks
{
///
/// <summary>
/// Visualizer for items that come in stacks and have different appearance
/// depending on the size of the stack. The visualizer can work by switching between different icons
/// in <c>_spriteLayers</c> or if the sprite layers are supposed to be composed as transparent layers.
/// The former behavior is default and the latter behavior can be defined in prototypes.
///
/// Note, the entity doesn't necessarily need to have a StackComponent for this to work.
/// Some prototypes (e.g. CigPackBase) use this to visualize how full a Storage is.
///
/// <example>
/// <para>To define a Stack Visualizer prototype insert the following
/// snippet (you can skip Appearance if already defined)
/// </para>
/// <code>
/// - type: Appearance
/// visuals:
/// - type: StackVisualizer
/// stackLayers:
/// - goldbar_10
/// - goldbar_20
/// - goldbar_30
/// </code>
/// </example>
/// <example>
/// <para>Defining a stack visualizer with composable transparent layers</para>
/// <code>
/// - type: StackVisualizer
/// composite: true
/// stackLayers:
/// - cigarette_1
/// - cigarette_2
/// - cigarette_3
/// - cigarette_4
/// - cigarette_5
/// - cigarette_6
/// </code>
/// </example>
/// <seealso cref="_spriteLayers"/>
/// </summary>
[RegisterComponent]
public sealed class StackVisualizerComponent : Component
{
/// <summary>
/// Sprite layers used in stack visualizer. Sprites first in layer correspond to lower stack states
/// e.g. <code>_spriteLayers[0]</code> is lower stack level than <code>_spriteLayers[1]</code>.
/// </summary>
[DataField("stackLayers")] public readonly List<string> _spriteLayers = new();

/// <summary>
/// Determines if the visualizer uses composite or non-composite layers for icons. Defaults to false.
///
/// <list type="bullet">
/// <item>
/// <description>false: they are opaque and mutually exclusive (e.g. sprites in a cable coil). <b>Default value</b></description>
/// </item>
/// <item>
/// <description>true: they are transparent and thus layered one over another in ascending order first</description>
/// </item>
/// </list>
///
/// </summary>
[DataField("composite")] public bool _isComposite;

/// <summary>
/// Optional RSI path to use for the <code>stackLayers<code>. If not specified, will look in the entity's sprite.
/// </summary>
[DataField("sprite")] public ResourcePath? _spritePath;
}
}
Loading

0 comments on commit b1b3382

Please sign in to comment.