Merge pull request #1 from NotNite/fools23-yeshealmeh

[Fools23] YesHealMe
This commit is contained in:
Julian 2023-03-25 15:34:59 -04:00 committed by GitHub
commit 0d621bc944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 503 additions and 1 deletions

View file

@ -85,7 +85,7 @@ internal class FoolsManager : IDisposable, IServiceType
{
Name = "DailyLifeDuty",
InternalName = "DailyLifeDutyPlugin",
Description = "Easily Track Daily and Weekly tasks... in real life",
Description = "We were just informed there are these things called \"chores\" outside the game. No worries, though, we can track them!",
Author = "MidoriKami",
RealAuthor = "Berna",
Type = typeof(DailyLifeDutyPlugin),
@ -135,6 +135,15 @@ internal class FoolsManager : IDisposable, IServiceType
RealAuthor = "Chirp",
Type = typeof(GoodVibesPlugin),
},
new()
{
Name = "YesHealMe",
InternalName = "YesHealMePlugin",
Description = "As the saying goes: it's the first missing HP that matters. And the second. And the third...",
Author = "MidoriKami",
RealAuthor = "Berna",
Type = typeof(YesHealMePlugin),
},
};
}

View file

@ -0,0 +1,31 @@
using System.Numerics;
using ImGuiNET;
namespace Dalamud.Fools.Helper.YesHealMe;
public static class Colors
{
public static Vector4 Purple = new(176 / 255.0f, 38 / 255.0f, 236 / 255.0f, 1.0f);
public static Vector4 Blue = new(37 / 255.0f, 168 / 255.0f, 1.0f, 1.0f);
public static Vector4 ForestGreen = new(0.133f, 0.545f, 0.1333f, 1.0f);
public static Vector4 White = new(1.0f, 1.0f, 1.0f,1.0f);
public static Vector4 Red = new(1.0f, 0.0f, 0.0f, 1.0f);
public static Vector4 Green = new(0.0f, 1.0f, 0.0f, 1.0f);
public static Vector4 Black = new(0.0f, 0.0f, 0.0f, 1.0f);
public static Vector4 HealerGreen = new(33 / 255f, 193 / 255f, 0, 1.0f);
public static Vector4 DPSRed = new(210/255f, 42/255f, 43/255f, 1.0f);
public static Vector4 SoftRed = new(0.8f, 0.2f, 0.2f, 1.0f);
public static Vector4 Grey = new(0.6f, 0.6f, 0.6f, 1.0f);
public static Vector4 LightGrey = new(220/250.0f, 220/250.0f, 220/250f, 1.0f);
public static Vector4 Orange = new(1.0f, 165.0f / 255.0f, 0.0f, 1.0f);
public static Vector4 SoftGreen = new(0.2f, 0.8f, 0.2f, 1.0f);
public static Vector4 FatePink = new(0.58f, 0.388f, 0.827f, 0.33f);
public static Vector4 FateDarkPink = new(0.58f, 0.388f, 0.827f, 1.0f);
public static Vector4 MapTextBrown = new(0.655f, 0.396f, 0.149f, 1.0f);
public static Vector4 BabyBlue = new(0.537f, 0.812f, 0.941f, 1.0f);
}
public static class ColorExtensions
{
public static uint ToU32(this Vector4 color) => ImGui.GetColorU32(color);
}

View file

@ -0,0 +1,99 @@
using System;
using System.Numerics;
using ImGuiNET;
using KamiLib.Caching;
namespace Dalamud.Fools.Helper.YesHealMe;
internal static class DrawUtilities
{
public static void TextOutlined(FontManager fontManager, Vector2 startingPosition, string text, float scale, Vector4 color)
{
startingPosition = startingPosition.Ceil();
var outlineThickness = (int)MathF.Ceiling(1 * scale);
for (var x = -outlineThickness; x <= outlineThickness; ++x)
{
for (var y = -outlineThickness; y <= outlineThickness; ++y)
{
if (x == 0 && y == 0)
{
continue;
}
DrawText(fontManager, startingPosition + new Vector2(x, y), text, Colors.Black, scale);
}
}
DrawText(fontManager, startingPosition, text, color, scale);
}
public static void DrawIconWithName(FontManager fontManager, Vector2 drawPosition, uint iconID, string name, float iconScale, float textScale, bool drawText = true)
{
if (!fontManager.GameFont.Available) return;
var icon = IconCache.Instance.GetIcon(iconID);
if (icon != null)
{
var drawList = ImGui.GetBackgroundDrawList();
var imagePadding = new Vector2(20.0f, 10.0f) * iconScale;
var imageSize = new Vector2(50.0f, 50.0f) * iconScale;
drawPosition += imagePadding;
drawList.AddImage(icon.ImGuiHandle, drawPosition, drawPosition + imageSize);
if (drawText)
{
drawPosition.X += imageSize.X / 2.0f;
drawPosition.Y += imageSize.Y + 2.0f * iconScale;
var textSize = CalculateTextSize(fontManager, name, textScale / 2.75f);
var textOffset = new Vector2(0.0f, 5.0f) * iconScale;
drawPosition.X -= textSize.X / 2.0f;
TextOutlined(fontManager, drawPosition + textOffset, name, textScale / 2.75f, Colors.White);
}
}
}
public static Vector2 CalculateTextSize(FontManager fontManager, string text, float scale)
{
if(!fontManager.GameFont.Available) return Vector2.Zero;
var fontSize = fontManager.GameFont.ImFont.FontSize;
var textSize = ImGui.CalcTextSize(text);
var fontScalar = 62.0f / textSize.Y;
var textWidth = textSize.X * fontScalar;
return new Vector2(textWidth, fontSize) * scale;
}
private static void DrawText(FontManager fontManager, Vector2 drawPosition, string text, Vector4 color, float scale, bool debug = false)
{
if (!fontManager.GameFont.Available) return;
var font = fontManager.GameFont.ImFont;
var drawList = ImGui.GetBackgroundDrawList();
var stringSize = CalculateTextSize(fontManager, text, scale);
if (debug)
{
drawList.AddRect(drawPosition, drawPosition + stringSize, ImGui.GetColorU32(Colors.Green));
}
drawList.AddText(font, font.FontSize * scale, drawPosition, ImGui.GetColorU32(color), text);
}
}
public static class VectorExtensions
{
public static Vector2 Ceil(this Vector2 data)
{
return new Vector2(MathF.Ceiling(data.X), MathF.Ceiling(data.Y));
}
}

View file

@ -0,0 +1,20 @@
using System;
using Dalamud.Interface;
using Dalamud.Interface.GameFonts;
namespace Dalamud.Fools.Helper.YesHealMe;
public class FontManager : IDisposable
{
public GameFontHandle GameFont { get; }
public FontManager(UiBuilder uiBuilder)
{
this.GameFont = uiBuilder.GetGameFontHandle( new GameFontStyle( GameFontFamily.Axis, 52.0f ) );
}
public void Dispose()
{
this.GameFont.Dispose();
}
}

View file

@ -0,0 +1,41 @@
using Dalamud;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.SubKinds;
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
namespace NoTankYou.Utilities;
public static unsafe class HudHelper
{
private static AgentHUD* AgentHud => AgentModule.Instance()->GetAgentHUD();
public static PlayerCharacter? GetPlayerCharacter(int index)
{
// Sorta temporary, waiting for ClientStructs to merge a fixed size array for this element
var partyMemberList = AgentHud->PartyMemberList;
var targetOffset = index * sizeof(HudPartyMember);
var targetAddress = partyMemberList + targetOffset;
var hudData = (HudPartyMember*) targetAddress;
var targetPlayer = hudData->ObjectId;
return GetPlayer(targetPlayer);
}
public static PlayerCharacter? GetAllianceMember(int index)
{
var targetPlayer = AgentHud->RaidMemberIds[index];
return GetPlayer(targetPlayer);
}
private static PlayerCharacter? GetPlayer(uint objectId)
{
var result = Service<ObjectTable>.Get().SearchById(objectId);
if (result?.GetType() == typeof(PlayerCharacter))
return result as PlayerCharacter;
return null;
}
}

View file

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Dalamud;
using Dalamud.Data;
using Dalamud.Logging;
using Dalamud.Utility;
using ImGuiScene;
namespace KamiLib.Caching;
public class IconCache : IDisposable
{
private readonly Dictionary<uint, TextureWrap?> iconTextures = new();
private const string IconFilePath = "ui/icon/{0:D3}000/{1:D6}_hr1.tex";
private static IconCache? _instance;
public static IconCache Instance => _instance ??= new IconCache();
public static void Cleanup()
{
_instance?.Dispose();
}
public void Dispose()
{
foreach (var texture in iconTextures.Values)
{
texture?.Dispose();
}
iconTextures.Clear();
}
private void LoadIconTexture(uint iconId)
{
Task.Run(() =>
{
try
{
var path = IconFilePath.Format(iconId / 1000, iconId);
var tex = Service<DataManager>.Get().GetImGuiTexture(path);
if (tex is not null && tex.ImGuiHandle != nint.Zero)
{
iconTextures[iconId] = tex;
}
else
{
tex?.Dispose();
}
}
catch (Exception ex)
{
PluginLog.LogError($"Failed loading texture for icon {iconId} - {ex.Message}");
}
});
}
public TextureWrap? GetIcon(uint iconId)
{
if (iconTextures.TryGetValue(iconId, out var value)) return value;
iconTextures.Add(iconId, null);
LoadIconTexture(iconId);
return iconTextures[iconId];
}
}

View file

@ -0,0 +1,67 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Dalamud;
using Dalamud.Game;
using Dalamud.Game.Gui;
using FFXIVClientStructs.FFXIV.Client.UI;
using NoTankYou.DataModels;
using NoTankYou.Utilities;
namespace NoTankYou.System;
public unsafe class PartyListAddon : IEnumerable<PartyListAddonData>, IDisposable
{
public record PartyFramePositionInfo(Vector2 Position, Vector2 Size, Vector2 Scale);
public IEnumerator<PartyListAddonData> GetEnumerator() => addonData.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private static AddonPartyList* PartyList => (AddonPartyList*) Service<GameGui>.Get()?.GetAddonByName("_PartyList");
public static bool DataAvailable => PartyList != null && PartyList->AtkUnitBase.RootNode != null;
private readonly List<PartyListAddonData> addonData = new();
public PartyListAddon()
{
Service<Framework>.Get().Update += OnFrameworkUpdate;
}
public void Dispose()
{
Service<Framework>.Get().Update -= OnFrameworkUpdate;
}
private void OnFrameworkUpdate(Framework framework)
{
addonData.Clear();
if (!DataAvailable) return;
if (PartyList->MemberCount <= 0) return;
foreach (var index in Enumerable.Range(0, PartyList->MemberCount))
{
var playerCharacter = HudHelper.GetPlayerCharacter(index);
var userInterface = PartyList->PartyMember[index];
addonData.Add(new PartyListAddonData
{
PlayerCharacter = playerCharacter,
UserInterface = userInterface,
});
}
}
public static PartyFramePositionInfo GetPositionInfo()
{
// Resource Node (id 9) contains a weird offset for the actual list elements
var rootNode = PartyList->AtkUnitBase.RootNode;
var addonBasePosition = new Vector2(rootNode->X, rootNode->Y);
var scale = new Vector2(rootNode->ScaleX, rootNode->ScaleY);
var partyListNode = PartyList->AtkUnitBase.GetNodeById(9);
var partyListPositionOffset = new Vector2(partyListNode->X, partyListNode->Y) * scale;
var partyListSize = new Vector2(partyListNode->Width, partyListNode->Height);
return new PartyFramePositionInfo(addonBasePosition + partyListPositionOffset, partyListSize * scale, scale);
}
}

View file

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Dalamud.Game.ClientState.Objects.SubKinds;
using FFXIVClientStructs.FFXIV.Client.UI;
namespace NoTankYou.DataModels;
public readonly unsafe struct PartyListAddonData
{
private static readonly Dictionary<uint, Stopwatch> TimeSinceLastTargetable = new();
public AddonPartyList.PartyListMemberStruct UserInterface { get; init; }
public PlayerCharacter? PlayerCharacter { get; init; }
private bool Targetable => UserInterface.PartyMemberComponent->OwnerNode->AtkResNode.Color.A != 0x99;
public bool IsTargetable()
{
if (PlayerCharacter is null) return false;
TimeSinceLastTargetable.TryAdd(PlayerCharacter.ObjectId, Stopwatch.StartNew());
var stopwatch = TimeSinceLastTargetable[PlayerCharacter.ObjectId];
if (Targetable)
{
// Returns true if the party member has been targetable for 2second or more
return stopwatch.Elapsed >= TimeSpan.FromSeconds(2);
}
else
{
// Returns false, and continually resets the stopwatch
stopwatch.Restart();
return false;
}
}
}

View file

@ -0,0 +1,31 @@
using Dalamud.Fools.Helper.YesHealMe;
using Dalamud.Interface;
using NoTankYou.System;
namespace Dalamud.Fools.Plugins;
public class YesHealMePlugin : IFoolsPlugin
{
private readonly FontManager fontManager;
private readonly PartyListAddon partyListAddon = new();
private int iconId = 1;
public YesHealMePlugin()
{
const string nameSpace = "fools+YesHealMe";
var uiBuilder = new UiBuilder(nameSpace);
this.fontManager = new FontManager(uiBuilder);
}
/// <inheritdoc/>
public void Dispose()
{
this.fontManager.Dispose();
this.partyListAddon.Dispose();
}
public void DrawUi()
{
YesHealMePluginWindow.Draw(this.partyListAddon, this.fontManager, ref this.iconId);
}
}

View file

@ -0,0 +1,97 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Dalamud.Data;
using Dalamud.Fools.Helper.YesHealMe;
using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Interface.Components;
using Dalamud.Interface.Internal;
using ImGuiNET;
using NoTankYou.System;
namespace Dalamud.Fools.Plugins;
public static class YesHealMePluginWindow
{
private const string WindowName = "##foolsYesHealMeBanner";
private const string WarningText = "HEAL ME";
private const int Length = 300;
private const int SectionHeight = 100;
private const float Scale = 1f;
private static readonly Vector2 Position = new(200, 200);
private static readonly Vector2 Size = new(Length, SectionHeight);
private static IEnumerable<PlayerCharacter> Characters(PartyListAddon partyListAddon)
{
return partyListAddon.Any() ? partyListAddon.Select(pla => pla.PlayerCharacter) : new[] { Service<ClientState>.Get().LocalPlayer };
}
private static List<PlayerCharacter> HurtingCharacters(IEnumerable<PlayerCharacter> characters)
{
return characters
.Where(pc => pc.CurrentHp < pc.MaxHp ||
Service<DalamudInterface>.Get()
.IsDevMenuOpen)
.ToList();
}
public static void Draw(PartyListAddon partyListAddon, FontManager fontManager, ref int iconId)
{
ImGui.SetNextWindowPos(Position, ImGuiCond.FirstUseEver);
ImGui.SetNextWindowSize(Size);
ImGui.SetNextWindowSizeConstraints(Size, Size);
ImGui.Begin(WindowName, ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoBackground);
var playersDrawn = 0;
var hurtingCharacters = HurtingCharacters(Characters(partyListAddon));
if (hurtingCharacters.Count > 0)
{
var windowPos = ImGui.GetCursorScreenPos();
foreach (var hurtingCharacter in hurtingCharacters)
{
var position = windowPos + new Vector2(0, playersDrawn * SectionHeight);
var healMeTextSize = DrawUtilities.CalculateTextSize(fontManager, WarningText, Scale);
var healMePosition = position with
{
X = position.X + healMeTextSize.X,
};
DrawHealMeText(fontManager, position);
DrawPlayerName(fontManager, hurtingCharacter, healMePosition);
DrawIcon(fontManager, healMePosition);
playersDrawn += 1;
}
}
ImGui.End();
}
private static void DrawIcon(FontManager fontManager, Vector2 healMePosition)
{
DrawUtilities.DrawIconWithName(fontManager, healMePosition, 62582, "pls? owo", 1, 1);
}
private static void DrawHealMeText(FontManager fontManager, Vector2 position)
{
// HEAL ME text
DrawUtilities.TextOutlined(fontManager, position, WarningText, 1, Colors.White);
}
private static void DrawPlayerName(
FontManager fontManager, PlayerCharacter hurtingCharacter, Vector2 healMePosition)
{
var textSize = DrawUtilities.CalculateTextSize(fontManager, hurtingCharacter.Name.TextValue, Scale);
var namePosition = new Vector2
{
X = healMePosition.X - textSize.X / 2.0f,
Y = healMePosition.Y + textSize.Y,
};
DrawUtilities.TextOutlined(
fontManager,
namePosition,
hurtingCharacter.Name.TextValue,
Scale / 2f,
Colors.White);
}
}