DI: Support debug items in SimpleRenderer, remove IDrawDebugItems

rendering
Liza 2023-02-17 19:31:43 +01:00
parent 5419f51942
commit adddbc452c
11 changed files with 52 additions and 64 deletions

View File

@ -1,7 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@ -385,15 +384,6 @@ namespace Pal.Client.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Splatoon Test:.
/// </summary>
internal static string Config_Splatoon_Test {
get {
return ResourceManager.GetString("Config_Splatoon_Test", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Start Export.
/// </summary>

View File

@ -228,9 +228,6 @@ Il n'y a pas de synchronisation avec les autres joueurs ni de sauvegarde entre l
<data name="Config_Renderer_Simple_Hint" xml:space="preserve">
<value>Expérimental</value>
</data>
<data name="Config_Splatoon_Test" xml:space="preserve">
<value>Test de Splatoon :</value>
</data>
<data name="Config_Splatoon_DrawCircles" xml:space="preserve">
<value>Dessiner les marqueurs des pièges et coffres autour de soi</value>
<comment>To test the Splatoon integration, you can draw markers around yourself.</comment>

View File

@ -227,9 +227,6 @@
<data name="Config_Renderer_Simple_Hint" xml:space="preserve">
<value>試験的機能</value>
</data>
<data name="Config_Splatoon_Test" xml:space="preserve">
<value>Splatoonのテスト:</value>
</data>
<data name="Config_Splatoon_DrawCircles" xml:space="preserve">
<value>自分の周りにトラップと宝箱を表示する</value>
<comment>To test the Splatoon integration, you can draw markers around yourself.</comment>

View File

@ -239,9 +239,6 @@ This is not synchronized with other players and not saved between floors/runs.</
<data name="Config_Renderer_Simple_Hint" xml:space="preserve">
<value>experimental</value>
</data>
<data name="Config_Splatoon_Test" xml:space="preserve">
<value>Splatoon Test:</value>
</data>
<data name="Config_Splatoon_DrawCircles" xml:space="preserve">
<value>Draw trap &amp; coffer circles around self</value>
<comment>To test the Splatoon integration, you can draw markers around yourself.</comment>

View File

@ -1,9 +0,0 @@
using System.Numerics;
namespace Pal.Client.Rendering
{
internal interface IDrawDebugItems
{
void DrawDebugItems(Vector4 trapColor, Vector4 hoardColor);
}
}

View File

@ -13,5 +13,7 @@ namespace Pal.Client.Rendering
void ResetLayer(ELayer layer);
IRenderElement CreateElement(Marker.EType type, Vector3 pos, uint color, bool fill = false);
void DrawDebugItems(uint trapColor, uint hoardColor);
}
}

View File

@ -14,6 +14,7 @@ namespace Pal.Client.Rendering
private readonly IPalacePalConfiguration _configuration;
private IServiceScope? _renderScope;
private IRenderer _implementation;
public RenderAdapter(IServiceScopeFactory serviceScopeFactory, ILogger<RenderAdapter> logger,
IPalacePalConfiguration configuration)
@ -22,14 +23,14 @@ namespace Pal.Client.Rendering
_logger = logger;
_configuration = configuration;
Implementation = Recreate(null);
_implementation = Recreate(null);
}
private IRenderer Recreate(ERenderer? currentRenderer)
{
ERenderer targetRenderer = _configuration.Renderer.SelectedRenderer;
if (targetRenderer == currentRenderer)
return Implementation;
return _implementation;
_renderScope?.Dispose();
@ -43,30 +44,31 @@ namespace Pal.Client.Rendering
public void ConfigUpdated()
{
Implementation = Recreate(Implementation.GetConfigValue());
_implementation = Recreate(_implementation.GetConfigValue());
}
public void Dispose()
=> _renderScope?.Dispose();
public IRenderer Implementation { get; private set; }
public void SetLayer(ELayer layer, IReadOnlyList<IRenderElement> elements)
=> Implementation.SetLayer(layer, elements);
=> _implementation.SetLayer(layer, elements);
public void ResetLayer(ELayer layer)
=> Implementation.ResetLayer(layer);
=> _implementation.ResetLayer(layer);
public IRenderElement CreateElement(Marker.EType type, Vector3 pos, uint color, bool fill = false)
=> Implementation.CreateElement(type, pos, color, fill);
public void DrawLayers()
{
if (Implementation is SimpleRenderer sr)
sr.DrawLayers();
}
=> _implementation.CreateElement(type, pos, color, fill);
public ERenderer GetConfigValue()
=> throw new NotImplementedException();
public void DrawDebugItems(uint trapColor, uint hoardColor)
=> _implementation.DrawDebugItems(trapColor, hoardColor);
public void DrawLayers()
{
if (_implementation is SimpleRenderer sr)
sr.DrawLayers();
}
}
}

View File

@ -3,5 +3,6 @@
internal static class RenderData
{
public static readonly uint ColorInvisible = 0;
public static readonly long TestLayerTimeout = 10_000;
}
}

View File

@ -66,6 +66,22 @@ namespace Pal.Client.Rendering
};
}
public void DrawDebugItems(uint trapColor, uint hoardColor)
{
_layers[ELayer.Test] = new SimpleLayer
{
TerritoryType = _clientState.TerritoryType,
Elements = new List<SimpleElement>
{
(SimpleElement)CreateElement(Marker.EType.Trap, _clientState.LocalPlayer?.Position ?? default,
trapColor),
(SimpleElement)CreateElement(Marker.EType.Hoard, _clientState.LocalPlayer?.Position ?? default,
hoardColor)
},
ExpiresAt = Environment.TickCount64 + RenderData.TestLayerTimeout
};
}
public void DrawLayers()
{
if (_layers.Count == 0)
@ -80,15 +96,14 @@ namespace Pal.Client.Rendering
ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.NoSavedSettings |
ImGuiWindowFlags.AlwaysUseWindowPadding))
{
ushort territoryType = _clientState.TerritoryType;
foreach (var layer in _layers.Values.Where(l => l.TerritoryType == territoryType))
foreach (var layer in _layers.Values.Where(l => l.IsValid(_clientState)))
{
foreach (var e in layer.Elements)
Draw(e);
}
foreach (var key in _layers.Where(l => l.Value.TerritoryType != territoryType).Select(l => l.Key)
foreach (var key in _layers.Where(l => !l.Value.IsValid(_clientState))
.Select(l => l.Key)
.ToList())
ResetLayer(key);
@ -159,6 +174,10 @@ namespace Pal.Client.Rendering
{
public required ushort TerritoryType { get; init; }
public required IReadOnlyList<SimpleElement> Elements { get; init; }
public long ExpiresAt { get; init; } = long.MaxValue;
public bool IsValid(ClientState clientState) =>
TerritoryType == clientState.TerritoryType && ExpiresAt >= Environment.TickCount64;
public void Dispose()
{

View File

@ -1,10 +1,8 @@
using Dalamud.Logging;
using Dalamud.Plugin;
using Dalamud.Plugin;
using ECommons;
using ECommons.Reflection;
using ECommons.Schedulers;
using ECommons.SplatoonAPI;
using ImGuiNET;
using System;
using System.Collections;
using System.Collections.Generic;
@ -12,15 +10,13 @@ using System.Linq;
using System.Numerics;
using System.Reflection;
using Dalamud.Game.ClientState;
using Dalamud.Game.Gui;
using Microsoft.Extensions.Logging;
using Pal.Client.Configuration;
using Pal.Client.DependencyInjection;
using Pal.Client.Extensions;
namespace Pal.Client.Rendering
{
internal sealed class SplatoonRenderer : IRenderer, IDrawDebugItems, IDisposable
internal sealed class SplatoonRenderer : IRenderer, IDisposable
{
private const long OnTerritoryChange = -2;
@ -102,25 +98,24 @@ namespace Pal.Client.Rendering
return new SplatoonElement(this, element);
}
// TODO This should be handled differently
// - make SimpleRenderer implement this
// - return error (if any) instead of using chat here
public void DrawDebugItems(Vector4 trapColor, Vector4 hoardColor)
public void DrawDebugItems(uint trapColor, uint hoardColor)
{
try
{
Vector3? pos = _clientState.LocalPlayer?.Position;
if (pos != null)
{
ResetLayer(ELayer.Test);
var elements = new List<IRenderElement>
{
CreateElement(Marker.EType.Trap, pos.Value, ImGui.ColorConvertFloat4ToU32(trapColor)),
CreateElement(Marker.EType.Hoard, pos.Value, ImGui.ColorConvertFloat4ToU32(hoardColor)),
CreateElement(Marker.EType.Trap, pos.Value, trapColor),
CreateElement(Marker.EType.Hoard, pos.Value, hoardColor),
};
if (!Splatoon.AddDynamicElements(ToLayerName(ELayer.Test),
elements.Cast<SplatoonElement>().Select(x => x.Delegate).ToArray(),
new[] { Environment.TickCount64 + 10000 }))
new[] { Environment.TickCount64 + RenderData.TestLayerTimeout }))
{
_chat.Message("Could not draw markers :(");
}

View File

@ -363,12 +363,9 @@ namespace Pal.Client.Windows
saveAndClose = ImGui.Button(Localization.SaveAndClose);
ImGui.Separator();
ImGui.Text(Localization.Config_Splatoon_Test);
ImGui.BeginDisabled(!(_renderAdapter.Implementation is IDrawDebugItems));
if (ImGui.Button(Localization.Config_Splatoon_DrawCircles))
(_renderAdapter.Implementation as IDrawDebugItems)?.DrawDebugItems(_trapConfig.Color,
_hoardConfig.Color);
ImGui.EndDisabled();
_renderAdapter.DrawDebugItems(ImGui.ColorConvertFloat4ToU32(_trapConfig.Color),
ImGui.ColorConvertFloat4ToU32(_hoardConfig.Color));
ImGui.EndTabItem();
}