PalacePal/Pal.Client/Rendering/RenderAdapter.cs

73 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2023-02-15 22:17:19 +00:00
using System.Numerics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2023-02-15 22:17:19 +00:00
using Pal.Client.Configuration;
namespace Pal.Client.Rendering
{
internal sealed class RenderAdapter : IRenderer, IDisposable
2023-02-15 22:17:19 +00:00
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILogger<RenderAdapter> _logger;
2023-02-15 22:17:19 +00:00
private readonly IPalacePalConfiguration _configuration;
private IServiceScope? _renderScope;
public RenderAdapter(IServiceScopeFactory serviceScopeFactory, ILogger<RenderAdapter> logger,
IPalacePalConfiguration configuration)
2023-02-15 22:17:19 +00:00
{
_serviceScopeFactory = serviceScopeFactory;
_logger = logger;
2023-02-15 22:17:19 +00:00
_configuration = configuration;
Implementation = Recreate(null);
}
private IRenderer Recreate(ERenderer? currentRenderer)
{
ERenderer targetRenderer = _configuration.Renderer.SelectedRenderer;
if (targetRenderer == currentRenderer)
return Implementation;
_renderScope?.Dispose();
_logger.LogInformation("Selected new renderer: {Renderer}", _configuration.Renderer.SelectedRenderer);
_renderScope = _serviceScopeFactory.CreateScope();
if (targetRenderer == ERenderer.Splatoon)
return _renderScope.ServiceProvider.GetRequiredService<SplatoonRenderer>();
else
return _renderScope.ServiceProvider.GetRequiredService<SimpleRenderer>();
}
public void ConfigUpdated()
{
Implementation = Recreate(Implementation.GetConfigValue());
2023-02-15 22:17:19 +00:00
}
public void Dispose()
=> _renderScope?.Dispose();
public IRenderer Implementation { get; private set; }
2023-02-15 22:17:19 +00:00
public void SetLayer(ELayer layer, IReadOnlyList<IRenderElement> elements)
=> Implementation.SetLayer(layer, elements);
public void ResetLayer(ELayer layer)
=> Implementation.ResetLayer(layer);
public IRenderElement CreateElement(Marker.EType type, Vector3 pos, uint color, bool fill = false)
=> Implementation.CreateElement(type, pos, color, fill);
2023-02-16 12:17:55 +00:00
public void DrawLayers()
{
if (Implementation is SimpleRenderer sr)
sr.DrawLayers();
}
public ERenderer GetConfigValue()
=> throw new NotImplementedException();
2023-02-15 22:17:19 +00:00
}
}