PalacePal/Pal.Client/Rendering/RenderAdapter.cs

79 lines
2.7 KiB
C#
Raw Permalink 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;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
2023-02-15 22:17:19 +00:00
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;
private IRenderer _implementation;
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);
}
public bool RequireRedraw { get; set; }
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());
RequireRedraw = true;
2023-02-15 22:17:19 +00:00
}
public void Dispose()
=> _renderScope?.Dispose();
2023-02-15 22:17:19 +00:00
public void SetLayer(ELayer layer, IReadOnlyList<IRenderElement> elements)
=> _implementation.SetLayer(layer, elements);
2023-02-15 22:17:19 +00:00
public void ResetLayer(ELayer layer)
=> _implementation.ResetLayer(layer);
2023-02-15 22:17:19 +00:00
2023-02-18 20:12:36 +00:00
public IRenderElement CreateElement(MemoryLocation.EType type, Vector3 pos, uint color, bool fill = false)
=> _implementation.CreateElement(type, pos, color, fill);
public ERenderer GetConfigValue()
=> throw new NotImplementedException();
public void DrawDebugItems(uint trapColor, uint hoardColor)
=> _implementation.DrawDebugItems(trapColor, hoardColor);
2023-02-16 12:17:55 +00:00
public void DrawLayers()
{
if (_implementation is SimpleRenderer sr)
2023-02-16 12:17:55 +00:00
sr.DrawLayers();
}
2023-02-15 22:17:19 +00:00
}
}