Parallax refactors (#7654)
This commit is contained in:
@@ -6,42 +6,60 @@ using Robust.Shared.Maths;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Client.Parallax
|
||||
namespace Content.Client.Parallax;
|
||||
|
||||
/// <summary>
|
||||
/// Renders the parallax background as a UI control.
|
||||
/// </summary>
|
||||
public sealed class ParallaxControl : Control
|
||||
{
|
||||
/// <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()
|
||||
{
|
||||
[Dependency] private readonly IParallaxManager _parallaxManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] public Vector2i Offset { get; set; }
|
||||
Offset = (_random.Next(0, 1000), _random.Next(0, 1000));
|
||||
RectClipContent = true;
|
||||
}
|
||||
|
||||
public ParallaxControl()
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
foreach (var layer in _parallaxManager.ParallaxLayers)
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
Offset = (_random.Next(0, 1000), _random.Next(0, 1000));
|
||||
RectClipContent = true;
|
||||
}
|
||||
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
var tex = _parallaxManager.ParallaxTexture;
|
||||
if (tex == null)
|
||||
return;
|
||||
|
||||
var size = tex.Size;
|
||||
var tex = layer.Texture;
|
||||
var texSize = tex.Size * layer.Config.Scale.Floored();
|
||||
var ourSize = PixelSize;
|
||||
|
||||
for (var x = -size.X + Offset.X; x < ourSize.X; x += size.X)
|
||||
if (layer.Config.Tiled)
|
||||
{
|
||||
for (var y = -size.Y + Offset.Y; y < ourSize.Y; y += size.Y)
|
||||
// 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)
|
||||
{
|
||||
handle.DrawTexture(tex, (x, y));
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user