Files
crystall-punk-14/Content.Client/Parallax/Managers/ParallaxManager.cs

134 lines
4.2 KiB
C#
Raw Normal View History

2018-11-30 21:54:30 +01:00
using System;
2022-05-04 17:55:21 +01:00
using System.Linq;
2019-08-29 14:36:31 +02:00
using System.Collections.Generic;
2018-11-30 21:54:30 +01:00
using System.IO;
2022-05-04 17:55:21 +01:00
using System.Threading;
2018-11-30 21:54:30 +01:00
using System.Threading.Tasks;
2022-05-04 17:55:21 +01:00
using Content.Client.Parallax.Data;
using Content.Shared;
using Content.Shared.CCVar;
2018-11-30 21:54:30 +01:00
using Nett;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
2022-05-04 17:55:21 +01:00
using Robust.Shared.Prototypes;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
2019-08-01 00:13:49 +02:00
using SixLabors.ImageSharp;
2019-08-29 14:36:31 +02:00
using SixLabors.ImageSharp.PixelFormats;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
namespace Content.Client.Parallax.Managers;
internal sealed class ParallaxManager : IParallaxManager
2018-11-30 21:54:30 +01:00
{
2022-05-04 17:55:21 +01:00
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
private string _parallaxName = "";
public string ParallaxName
{
get => _parallaxName;
set
{
LoadParallaxByName(value);
}
}
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
public Vector2 ParallaxAnchor { get; set; }
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
private CancellationTokenSource? _presentParallaxLoadCancel;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
private ParallaxLayerPrepared[] _parallaxLayersHQ = {};
private ParallaxLayerPrepared[] _parallaxLayersLQ = {};
2019-06-29 01:58:16 +02:00
2022-05-04 17:55:21 +01:00
public ParallaxLayerPrepared[] ParallaxLayers => _configurationManager.GetCVar(CCVars.ParallaxLowQuality) ? _parallaxLayersLQ : _parallaxLayersHQ;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
public async void LoadParallax()
{
await LoadParallaxByName("default");
}
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
private async Task LoadParallaxByName(string name)
{
// Update _parallaxName
if (_parallaxName == name)
{
return;
}
_parallaxName = name;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
// Cancel any existing load and setup the new cancellation token
_presentParallaxLoadCancel?.Cancel();
_presentParallaxLoadCancel = new CancellationTokenSource();
var cancel = _presentParallaxLoadCancel.Token;
2019-08-29 14:36:31 +02:00
2022-05-04 17:55:21 +01:00
// Empty parallax name = no layers (this is so that the initial "" parallax name is consistent)
if (_parallaxName == "")
{
_parallaxLayersHQ = _parallaxLayersLQ = new ParallaxLayerPrepared[] {};
return;
}
2022-05-04 17:55:21 +01:00
// Begin (for real)
Logger.InfoS("parallax", $"Loading parallax {name}");
try
{
2022-05-04 17:55:21 +01:00
var parallaxPrototype = _prototypeManager.Index<ParallaxPrototype>(name);
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
ParallaxLayerPrepared[] hq;
ParallaxLayerPrepared[] lq;
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
if (parallaxPrototype.LayersLQUseHQ)
{
lq = hq = await LoadParallaxLayers(parallaxPrototype.Layers, cancel);
}
else
{
var results = await Task.WhenAll(
LoadParallaxLayers(parallaxPrototype.Layers, cancel),
LoadParallaxLayers(parallaxPrototype.LayersLQ, cancel)
);
hq = results[0];
lq = results[1];
}
2018-11-30 21:54:30 +01:00
2022-05-04 17:55:21 +01:00
// Still keeping this check just in case.
if (_parallaxName == name)
2019-08-29 14:36:31 +02:00
{
2022-05-04 17:55:21 +01:00
_parallaxLayersHQ = hq;
_parallaxLayersLQ = lq;
Logger.InfoS("parallax", $"Loaded parallax {name}");
2019-08-29 14:36:31 +02:00
}
}
2022-05-04 17:55:21 +01:00
catch (Exception ex)
{
2022-05-04 17:55:21 +01:00
Logger.ErrorS("parallax", $"Failed to loaded parallax {name}: {ex}");
}
2022-05-04 17:55:21 +01:00
}
2019-08-28 12:08:10 +02:00
2022-05-04 17:55:21 +01:00
private async Task<ParallaxLayerPrepared[]> LoadParallaxLayers(List<ParallaxLayerConfig> layersIn, CancellationToken cancel = default)
{
// Because this is async, make sure it doesn't change (prototype reloads could muck this up)
// Since the tasks aren't awaited until the end, this should be fine
var tasks = new Task<ParallaxLayerPrepared>[layersIn.Count];
for (var i = 0; i < layersIn.Count; i++)
{
2022-05-04 17:55:21 +01:00
tasks[i] = LoadParallaxLayer(layersIn[i], cancel);
2018-11-30 21:54:30 +01:00
}
2022-05-04 17:55:21 +01:00
return await Task.WhenAll(tasks);
}
private async Task<ParallaxLayerPrepared> LoadParallaxLayer(ParallaxLayerConfig config, CancellationToken cancel = default)
{
return new ParallaxLayerPrepared()
{
Texture = await config.Texture.GenerateTexture(cancel),
Config = config
};
2018-11-30 21:54:30 +01:00
}
}
2022-05-04 17:55:21 +01:00