PalacePal/Pal.Client/Rendering/SimpleRenderer.cs

205 lines
7.2 KiB
C#
Raw Normal View History

2023-02-15 22:17:19 +00:00
using Dalamud.Interface;
2023-02-08 15:06:43 +00:00
using ImGuiNET;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
2023-02-15 22:17:19 +00:00
using Dalamud.Game.ClientState;
using Dalamud.Game.Gui;
using Pal.Client.Configuration;
using Pal.Client.DependencyInjection;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
2023-02-08 15:06:43 +00:00
namespace Pal.Client.Rendering
{
/// <summary>
/// Simple renderer that only draws basic stuff.
///
/// This is based on what SliceIsRight uses, and what PalacePal used before it was
/// remade into PalacePal (which is the third or fourth iteration on the same idea
/// I made, just with a clear vision).
/// </summary>
2023-02-15 22:17:19 +00:00
internal sealed class SimpleRenderer : IRenderer, IDisposable
2023-02-08 15:06:43 +00:00
{
2023-02-15 22:17:19 +00:00
private const int SegmentCount = 20;
private readonly ClientState _clientState;
private readonly GameGui _gameGui;
private readonly IPalacePalConfiguration _configuration;
private readonly TerritoryState _territoryState;
2023-02-11 20:10:45 +00:00
private readonly ConcurrentDictionary<ELayer, SimpleLayer> _layers = new();
2023-02-08 15:06:43 +00:00
2023-02-15 22:17:19 +00:00
public SimpleRenderer(ClientState clientState, GameGui gameGui, IPalacePalConfiguration configuration,
TerritoryState territoryState)
{
_clientState = clientState;
_gameGui = gameGui;
_configuration = configuration;
_territoryState = territoryState;
}
2023-02-08 15:06:43 +00:00
public void SetLayer(ELayer layer, IReadOnlyList<IRenderElement> elements)
{
2023-02-11 20:10:45 +00:00
_layers[layer] = new SimpleLayer
2023-02-08 15:06:43 +00:00
{
2023-02-15 22:17:19 +00:00
TerritoryType = _clientState.TerritoryType,
2023-02-08 15:06:43 +00:00
Elements = elements.Cast<SimpleElement>().ToList()
};
}
public void ResetLayer(ELayer layer)
{
2023-02-11 20:10:45 +00:00
if (_layers.Remove(layer, out var l))
2023-02-08 15:06:43 +00:00
l.Dispose();
}
2023-02-18 20:12:36 +00:00
public IRenderElement CreateElement(MemoryLocation.EType type, Vector3 pos, uint color, bool fill = false)
2023-02-08 15:06:43 +00:00
{
var config = MarkerConfig.ForType(type);
return new SimpleElement
{
Type = type,
Position = pos + new Vector3(0, config.OffsetY, 0),
Color = color,
Radius = config.Radius,
Fill = fill,
};
}
public void DrawDebugItems(uint trapColor, uint hoardColor)
{
_layers[ELayer.Test] = new SimpleLayer
{
TerritoryType = _clientState.TerritoryType,
Elements = new List<SimpleElement>
{
2023-02-18 20:12:36 +00:00
(SimpleElement)CreateElement(
MemoryLocation.EType.Trap,
_clientState.LocalPlayer?.Position ?? default,
trapColor),
2023-02-18 20:12:36 +00:00
(SimpleElement)CreateElement(
MemoryLocation.EType.Hoard,
_clientState.LocalPlayer?.Position ?? default,
hoardColor)
},
ExpiresAt = Environment.TickCount64 + RenderData.TestLayerTimeout
};
}
2023-02-08 15:06:43 +00:00
public void DrawLayers()
{
2023-02-11 20:10:45 +00:00
if (_layers.Count == 0)
2023-02-08 15:06:43 +00:00
return;
ImGuiHelpers.ForceNextWindowMainViewport();
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, Vector2.Zero);
ImGuiHelpers.SetNextWindowPosRelativeMainViewport(Vector2.Zero, ImGuiCond.None, Vector2.Zero);
ImGui.SetNextWindowSize(ImGuiHelpers.MainViewport.Size);
2023-02-15 22:17:19 +00:00
if (ImGui.Begin("###PalacePalSimpleRender",
ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoBackground |
ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.NoSavedSettings |
ImGuiWindowFlags.AlwaysUseWindowPadding))
2023-02-08 15:06:43 +00:00
{
foreach (var layer in _layers.Values.Where(l => l.IsValid(_clientState)))
2023-02-15 22:17:19 +00:00
{
foreach (var e in layer.Elements)
Draw(e);
}
2023-02-08 15:06:43 +00:00
foreach (var key in _layers.Where(l => !l.Value.IsValid(_clientState))
.Select(l => l.Key)
2023-02-15 22:17:19 +00:00
.ToList())
2023-02-08 15:06:43 +00:00
ResetLayer(key);
ImGui.End();
}
2023-02-15 22:17:19 +00:00
2023-02-08 15:06:43 +00:00
ImGui.PopStyleVar();
}
2023-02-15 22:17:19 +00:00
private void Draw(SimpleElement e)
{
if (e.Color == RenderData.ColorInvisible)
return;
switch (e.Type)
{
2023-02-18 20:12:36 +00:00
case MemoryLocation.EType.Hoard:
2023-02-15 22:17:19 +00:00
// ignore distance if this is a found hoard coffer
if (_territoryState.PomanderOfIntuition == PomanderState.Active &&
_configuration.DeepDungeons.HoardCoffers.OnlyVisibleAfterPomander)
break;
2023-02-18 20:12:36 +00:00
goto case MemoryLocation.EType.Trap;
2023-02-15 22:17:19 +00:00
2023-02-18 20:12:36 +00:00
case MemoryLocation.EType.Trap:
2023-02-15 22:17:19 +00:00
var playerPos = _clientState.LocalPlayer?.Position;
if (playerPos == null)
return;
if ((playerPos.Value - e.Position).Length() > 65)
return;
break;
}
bool onScreen = false;
for (int index = 0; index < 2 * SegmentCount; ++index)
{
onScreen |= _gameGui.WorldToScreen(new Vector3(
e.Position.X + e.Radius * (float)Math.Sin(Math.PI / SegmentCount * index),
e.Position.Y,
e.Position.Z + e.Radius * (float)Math.Cos(Math.PI / SegmentCount * index)),
out Vector2 vector2);
ImGui.GetWindowDrawList().PathLineTo(vector2);
}
if (onScreen)
{
if (e.Fill)
ImGui.GetWindowDrawList().PathFillConvex(e.Color);
else
ImGui.GetWindowDrawList().PathStroke(e.Color, ImDrawFlags.Closed, 2);
}
else
ImGui.GetWindowDrawList().PathClear();
}
public ERenderer GetConfigValue()
=> ERenderer.Simple;
2023-02-08 15:06:43 +00:00
public void Dispose()
{
2023-02-11 20:10:45 +00:00
foreach (var l in _layers.Values)
2023-02-08 15:06:43 +00:00
l.Dispose();
}
2023-02-15 22:17:19 +00:00
public sealed class SimpleLayer : IDisposable
2023-02-08 15:06:43 +00:00
{
public required ushort TerritoryType { get; init; }
2023-02-11 20:10:45 +00:00
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;
2023-02-08 15:06:43 +00:00
public void Dispose()
{
foreach (var e in Elements)
e.IsValid = false;
}
}
2023-02-15 22:17:19 +00:00
public sealed class SimpleElement : IRenderElement
2023-02-08 15:06:43 +00:00
{
public bool IsValid { get; set; } = true;
2023-02-18 20:12:36 +00:00
public required MemoryLocation.EType Type { get; init; }
2023-02-11 20:10:45 +00:00
public required Vector3 Position { get; init; }
2023-02-08 15:06:43 +00:00
public required uint Color { get; set; }
2023-02-11 20:10:45 +00:00
public required float Radius { get; init; }
public required bool Fill { get; init; }
2023-02-08 15:06:43 +00:00
}
}
}