Skip to content

Commit

Permalink
Font shenanigans.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurochi51 committed Mar 25, 2024
1 parent b26805f commit 973082c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 40 deletions.
41 changes: 41 additions & 0 deletions PetScale/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using Dalamud;
using Lumina.Excel;
using Dalamud.Memory;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Dalamud.Interface.Utility;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Game.ClientState.Objects.Enums;
using FFXIVClientStructs.FFXIV.Client.Game.Character;
using FFXIVClientStructs.Interop;
Expand Down Expand Up @@ -99,4 +102,42 @@ public unsafe bool PetVisible(BattleChara* pet)
}
return pet->Character.GameObject.GetDrawObject()->IsVisible;
}

public static IFontHandle CreateIconFont(DalamudPluginInterface pi)
=> pi.UiBuilder.FontAtlas.NewDelegateFontHandle(e =>
{
e.OnPreBuild(tk => tk.AddFontAwesomeIconFont(new()
{
SizePx = pi.UiBuilder.DefaultFontSpec.SizePx,
GlyphMinAdvanceX = pi.UiBuilder.DefaultFontSpec.SizePx,
GlyphMaxAdvanceX = pi.UiBuilder.DefaultFontSpec.SizePx,
}));
e.OnPostBuild(tk =>
{
var font = tk.Font;
var fontSize = font.FontSize;
var glyphs = font.GlyphsWrapped();
foreach (ref var glyph in glyphs.DataSpan)
{
var ratio = 1f;
if (glyph.X1 - glyph.X0 > fontSize)
{
ratio = Math.Max(ratio, (glyph.X1 - glyph.X0) / fontSize);
}
if (glyph.Y1 - glyph.Y0 > fontSize)
{
ratio = Math.Max(ratio, (glyph.Y1 - glyph.Y0) / fontSize);
}
var width = MathF.Round((glyph.X1 - glyph.X0) / ratio, MidpointRounding.ToZero);
var height = MathF.Round((glyph.Y1 - glyph.Y0) / ratio, MidpointRounding.AwayFromZero);
glyph.X0 = MathF.Round((fontSize - width) / 2f, MidpointRounding.ToZero);
glyph.Y0 = MathF.Round((fontSize - height) / 2f, MidpointRounding.AwayFromZero);
glyph.X1 = glyph.X0 + width;
glyph.Y1 = glyph.Y0 + height;
glyph.AdvanceX = fontSize;
}
tk.BuildLookupTable(font);
});
});
}
4 changes: 4 additions & 0 deletions PetScale/PetScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Dalamud.Plugin.Services;
using Dalamud.Interface.Windowing;
using Lumina.Excel.GeneratedSheets2;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Game.ClientState.Objects.SubKinds;
using BattleChara = FFXIVClientStructs.FFXIV.Client.Game.Character.BattleChara;
Expand Down Expand Up @@ -56,6 +57,8 @@ public sealed class PetScale : IDalamudPlugin
public Queue<string> players { get; } = new(101); // 100 players + Others entry
public bool requestedCache { get; set; } = true;
public int lastIndexOfOthers { get; set; } = -1;
[Obsolete("Replace with Dalamud provided font once merged")]
public IFontHandle IconFont { get; set; }

private ConfigWindow ConfigWindow { get; init; }
#if DEBUG
Expand Down Expand Up @@ -104,6 +107,7 @@ public sealed class PetScale : IDalamudPlugin
stopwatch.Start();

_ = Task.Run(InitSheet);
IconFont = Utilities.CreateIconFont(pluginInterface);

Check warning on line 110 in PetScale/PetScale.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'

Check warning on line 110 in PetScale/PetScale.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'
ConfigWindow.ProcessPetData(save: false);
QueueOnlyExistingData();
}
Expand Down
60 changes: 20 additions & 40 deletions PetScale/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void GeneralTab()
ImGui.SameLine();
DrawComboBox("Sizes", sizeSelection, sizesWidth, out sizeSelection, sizeMap.Values, filter: false);
ImGui.SameLine();
if (DrawIconButton(fontHandle: null, addButtonIcon, "AddButton", 1))
if (IconButton(plugin.IconFont, addButtonIcon, "AddButton", 1))

Check warning on line 152 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'

Check warning on line 152 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'
{
buttonPressed = true;
}
Expand Down Expand Up @@ -193,10 +193,10 @@ private unsafe void DisplayEntries()
ImGui.TableSetupColumn("Character", ImGuiTableColumnFlags.WidthFixed, charaWidth);
ImGui.TableSetupColumn("Pet", ImGuiTableColumnFlags.WidthFixed, petWidth);
ImGui.TableSetupColumn("PetSize", ImGuiTableColumnFlags.WidthFixed, sizesWidth);
ImGui.TableSetupColumn("DeleteButton", ImGuiTableColumnFlags.WidthFixed, GetIconButtonSize(fontHandle: null, deleteButtonIcon).X);
ImGui.TableSetupColumn("DeleteButton", ImGuiTableColumnFlags.WidthFixed, IconButtonSize(plugin.IconFont, deleteButtonIcon).X);

Check warning on line 196 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'

Check warning on line 196 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'
var itemRemoved = false;
var clipper = new ImGuiListClipperPtr(ImGuiNative.ImGuiListClipper_ImGuiListClipper());
var clipperHeight = GetIconButtonSize(fontHandle: null, deleteButtonIcon).Y + (ImGui.GetStyle().FramePadding.Y * 2);
var clipperHeight = IconButtonSize(plugin.IconFont, deleteButtonIcon).Y + (ImGui.GetStyle().FramePadding.Y * 2);

Check warning on line 199 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'

Check warning on line 199 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'
clipper.Begin(petData.Count, clipperHeight);

var clipperBreak = false;
Expand Down Expand Up @@ -225,7 +225,7 @@ private unsafe void DisplayEntries()
ImGui.TextUnformatted(sizeMap[item.PetSize]);
ImGui.TableSetColumnIndex(3);
ImGui.SetCursorPosX(tableButtonAlignmentOffset);
if (DrawIconButton(fontHandle: null, deleteButtonIcon, buttonId + deleteButtonIcon))
if (IconButton(plugin.IconFont, deleteButtonIcon, buttonId + deleteButtonIcon, 1))

Check warning on line 228 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'
{
petData.RemoveAt(i);
CreateNotification("Entry " + item.CharacterName + ", " + petSelection + ", " + sizeMap[item.PetSize] + " was removed.", "Entry removed");
Expand Down Expand Up @@ -307,47 +307,27 @@ private static unsafe void DrawClippedList(int itemCount, string? filter, string
clipper.Destroy();
}

private static Vector2 GetIconButtonSize(IFontHandle? fontHandle, string icon)
private static Vector2 IconButtonSize(IFontHandle fontHandle, string icon)
{
using var currentFont = fontHandle == null ? ImRaii.PushFont(UiBuilder.IconFont) : fontHandle.Push();
var iconSize = ImGui.CalcTextSize(icon);
var iconscaling = (iconSize.X < iconSize.Y ? (iconSize.Y - iconSize.X) / 2f : 0f, iconSize.X > iconSize.Y ? 1f / (iconSize.X / iconSize.Y) : 1f);
var normalized = iconscaling.Item2 == 1f ?
new Vector2(iconSize.Y, iconSize.Y)
: new((iconSize.X * iconscaling.Item2) + (iconscaling.Item1 * 2), (iconSize.X * iconscaling.Item2) + (iconscaling.Item1 * 2));
var padding = ImGui.GetStyle().FramePadding;
return normalized with { X = normalized.X + (padding.X * 2), Y = normalized.Y + (padding.Y * 2) };
using (fontHandle.Push())
{
return new Vector2(ImGuiHelpers.GetButtonSize(icon).X, ImGui.GetFrameHeight());
}
}

// Shamelessly ripped off mare
private static bool DrawIconButton(IFontHandle? fontHandle, string icon, string buttonIDLabel, float widthOffset = 0f)
private static bool IconButton(IFontHandle fontHandle, string icon, string buttonIDLabel, float widthOffset = 0f)
{
var clicked = false;
using var currentFont = fontHandle == null ? ImRaii.PushFont(UiBuilder.IconFont) : fontHandle.Push();
var iconSize = ImGui.CalcTextSize(icon);
var iconscaling = (iconSize.X < iconSize.Y ? (iconSize.Y - iconSize.X) / 2f : 0f, iconSize.X > iconSize.Y ? 1f / (iconSize.X / iconSize.Y) : 1f);
var normalized = iconscaling.Item2 == 1f ?
new Vector2(iconSize.Y, iconSize.Y)
: new((iconSize.X * iconscaling.Item2) + (iconscaling.Item1 * 2), (iconSize.X * iconscaling.Item2) + (iconscaling.Item1 * 2));
var padding = ImGui.GetStyle().FramePadding;
var cursor = ImGui.GetCursorPos();
var drawList = ImGui.GetWindowDrawList();
var pos = ImGui.GetWindowPos();
var scrollPosY = ImGui.GetScrollY();
var scrollPosX = ImGui.GetScrollX();
var buttonSize = normalized with { X = normalized.X + (padding.X * 2), Y = normalized.Y + (padding.Y * 2) };

if (ImGui.Button("##" + buttonIDLabel, buttonSize))
using (fontHandle.Push())
{
clicked = true;
var cursorScreenPos = ImGui.GetCursorScreenPos();
var frameHeight = ImGui.GetFrameHeight();
var result = ImGui.Button("##" + buttonIDLabel, new Vector2(ImGuiHelpers.GetButtonSize(icon).X, frameHeight));
var pos = new Vector2(cursorScreenPos.X + ImGui.GetStyle().FramePadding.X + widthOffset,
cursorScreenPos.Y + (frameHeight / 2f) - (ImGui.CalcTextSize(icon).Y / 2f));
ImGui.GetWindowDrawList().AddText(pos, ImGui.GetColorU32(ImGuiCol.Text), icon);

return result;
}

drawList.AddText(ImGui.GetFont(), ImGui.GetFontSize() * iconscaling.Item2,
new(pos.X - scrollPosX + cursor.X + iconscaling.Item1 + padding.X + widthOffset,
pos.Y - scrollPosY + cursor.Y + ((buttonSize.Y - (iconSize.Y * iconscaling.Item2)) / 2f)),
ImGui.GetColorU32(ImGuiCol.Text), icon);

return clicked;
}

private void CheckPossibleEntry()
Expand Down Expand Up @@ -437,7 +417,7 @@ private void ResizeIfNeeded()
tableButtonAlignmentOffset = charaWidth + petWidth + sizesWidth + (ImGui.GetStyle().ItemSpacing.X * 3);
if (SizeConstraints.HasValue)
{
var newWidth = tableButtonAlignmentOffset + GetIconButtonSize(fontHandle: null, deleteButtonIcon).X + (ImGui.GetStyle().WindowPadding.X * 2) + ImGui.GetStyle().ScrollbarSize;
var newWidth = tableButtonAlignmentOffset + IconButtonSize(plugin.IconFont, deleteButtonIcon).X + (ImGui.GetStyle().WindowPadding.X * 2) + ImGui.GetStyle().ScrollbarSize;

Check warning on line 420 in PetScale/Windows/ConfigWindow.cs

View workflow job for this annotation

GitHub Actions / build

'PetScale.IconFont' is obsolete: 'Replace with Dalamud provided font once merged'
SizeConstraints = new WindowSizeConstraints()
{
MinimumSize = new Vector2(newWidth / ImGuiHelpers.GlobalScale, SizeConstraints.Value.MinimumSize.Y),
Expand Down

0 comments on commit 973082c

Please sign in to comment.