PalacePal/Pal.Client/Rendering/SimpleRenderer.cs

163 lines
5.7 KiB
C#
Raw Normal View History

2023-02-08 15:06:43 +00:00
using Dalamud.Game.Gui;
using Dalamud.Interface;
using Dalamud.Plugin;
using ECommons.ExcelServices.TerritoryEnumeration;
using ImGuiNET;
using Lumina.Excel.GeneratedSheets;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Xml.Linq;
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>
internal class SimpleRenderer : IRenderer, IDisposable
{
2023-02-11 20:10:45 +00:00
private readonly ConcurrentDictionary<ELayer, SimpleLayer> _layers = new();
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
{
TerritoryType = Service.ClientState.TerritoryType,
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();
}
public IRenderElement CreateElement(Marker.EType type, Vector3 pos, uint color, bool fill = false)
{
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 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);
if (ImGui.Begin("###PalacePalSimpleRender", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.AlwaysUseWindowPadding))
{
ushort territoryType = Service.ClientState.TerritoryType;
2023-02-11 20:10:45 +00:00
foreach (var layer in _layers.Values.Where(l => l.TerritoryType == territoryType))
2023-02-08 15:06:43 +00:00
layer.Draw();
2023-02-11 20:10:45 +00:00
foreach (var key in _layers.Where(l => l.Value.TerritoryType != territoryType).Select(l => l.Key).ToList())
2023-02-08 15:06:43 +00:00
ResetLayer(key);
ImGui.End();
}
ImGui.PopStyleVar();
}
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();
}
public class SimpleLayer : IDisposable
{
public required ushort TerritoryType { get; init; }
2023-02-11 20:10:45 +00:00
public required IReadOnlyList<SimpleElement> Elements { get; init; }
2023-02-08 15:06:43 +00:00
public void Draw()
{
foreach (var element in Elements)
element.Draw();
}
public void Dispose()
{
foreach (var e in Elements)
e.IsValid = false;
}
}
public class SimpleElement : IRenderElement
{
2023-02-11 20:10:45 +00:00
private const int SegmentCount = 20;
2023-02-08 15:06:43 +00:00
public bool IsValid { get; set; } = true;
2023-02-11 20:10:45 +00:00
public required Marker.EType Type { get; init; }
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
public void Draw()
{
2023-02-10 19:48:14 +00:00
if (Color == Plugin.ColorInvisible)
2023-02-08 15:06:43 +00:00
return;
switch (Type)
{
case Marker.EType.Hoard:
// ignore distance if this is a found hoard coffer
2023-02-15 01:38:04 +00:00
if (Service.Plugin.PomanderOfIntuition == Plugin.PomanderState.Active && Service.Configuration.DeepDungeons.HoardCoffers.OnlyVisibleAfterPomander)
2023-02-08 15:06:43 +00:00
break;
goto case Marker.EType.Trap;
case Marker.EType.Trap:
var playerPos = Service.ClientState.LocalPlayer?.Position;
if (playerPos == null)
return;
if ((playerPos.Value - Position).Length() > 65)
return;
break;
}
bool onScreen = false;
2023-02-11 20:10:45 +00:00
for (int index = 0; index < 2 * SegmentCount; ++index)
2023-02-08 15:06:43 +00:00
{
onScreen |= Service.GameGui.WorldToScreen(new Vector3(
2023-02-11 20:10:45 +00:00
Position.X + Radius * (float)Math.Sin(Math.PI / SegmentCount * index),
2023-02-08 15:06:43 +00:00
Position.Y,
2023-02-11 20:10:45 +00:00
Position.Z + Radius * (float)Math.Cos(Math.PI / SegmentCount * index)),
2023-02-08 15:06:43 +00:00
out Vector2 vector2);
ImGui.GetWindowDrawList().PathLineTo(vector2);
}
if (onScreen)
{
if (Fill)
ImGui.GetWindowDrawList().PathFillConvex(Color);
else
ImGui.GetWindowDrawList().PathStroke(Color, ImDrawFlags.Closed, 2);
}
else
ImGui.GetWindowDrawList().PathClear();
}
}
}
}