Files
tbd-station-14/Content.Client/Parallax/ParallaxControl.cs
2022-05-04 09:55:21 -07:00

66 lines
2.2 KiB
C#

using Content.Client.Parallax.Managers;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.ViewVariables;
namespace Content.Client.Parallax;
/// <summary>
/// Renders the parallax background as a UI control.
/// </summary>
public sealed class ParallaxControl : Control
{
[Dependency] private readonly IParallaxManager _parallaxManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[ViewVariables(VVAccess.ReadWrite)] public Vector2i Offset { get; set; }
public ParallaxControl()
{
IoCManager.InjectDependencies(this);
Offset = (_random.Next(0, 1000), _random.Next(0, 1000));
RectClipContent = true;
}
protected override void Draw(DrawingHandleScreen handle)
{
foreach (var layer in _parallaxManager.ParallaxLayers)
{
var tex = layer.Texture;
var texSize = tex.Size * layer.Config.Scale.Floored();
var ourSize = PixelSize;
if (layer.Config.Tiled)
{
// Multiply offset by slowness to match normal parallax
var scaledOffset = (Offset * layer.Config.Slowness).Floored();
// Then modulo the scaled offset by the size to prevent drawing a bunch of offscreen tiles for really small images.
scaledOffset.X %= texSize.X;
scaledOffset.Y %= texSize.Y;
// Note: scaledOffset must never be below 0 or there will be visual issues.
// It could be allowed to be >= texSize on a given axis but that would be wasteful.
for (var x = -scaledOffset.X; x < ourSize.X; x += texSize.X)
{
for (var y = -scaledOffset.Y; y < ourSize.Y; y += texSize.Y)
{
handle.DrawTextureRect(tex, UIBox2.FromDimensions((x, y), texSize));
}
}
}
else
{
var origin = ((ourSize - texSize) / 2) + layer.Config.ControlHomePosition;
handle.DrawTextureRect(tex, UIBox2.FromDimensions(origin, texSize));
}
}
}
}