Update IconBrowserWidget

This commit is contained in:
Soreepeong 2024-02-22 03:36:55 +09:00
parent 5eadfc1b4d
commit 2920d18afa

View file

@ -1,12 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using Dalamud.Data;
using Dalamud.Interface.Colors; using Dalamud.Interface.Colors;
using Dalamud.Interface.Utility; using Dalamud.Interface.Utility;
using Dalamud.Utility;
using ImGuiNET; using ImGuiNET;
namespace Dalamud.Interface.Internal.Windows.Data.Widgets; namespace Dalamud.Interface.Internal.Windows.Data.Widgets;
@ -17,22 +15,22 @@ namespace Dalamud.Interface.Internal.Windows.Data.Widgets;
public class IconBrowserWidget : IDataWindowWidget public class IconBrowserWidget : IDataWindowWidget
{ {
// Remove range 170,000 -> 180,000 by default, this specific range causes exceptions. // Remove range 170,000 -> 180,000 by default, this specific range causes exceptions.
private readonly HashSet<int> nullValues = Enumerable.Range(170000, 9999).ToHashSet(); private readonly HashSet<int> nullValues = Enumerable.Range(170000, 9999).ToHashSet();
private Vector2 iconSize = new(64.0f, 64.0f); private Vector2 iconSize = new(64.0f, 64.0f);
private Vector2 editIconSize = new(64.0f, 64.0f); private Vector2 editIconSize = new(64.0f, 64.0f);
private List<int> valueRange = Enumerable.Range(0, 200000).ToList(); private List<int> valueRange = Enumerable.Range(0, 200000).ToList();
private int lastNullValueCount; private int lastNullValueCount;
private int startRange; private int startRange;
private int stopRange = 200000; private int stopRange = 200000;
private bool showTooltipImage; private bool showTooltipImage;
private Vector2 mouseDragStart; private Vector2 mouseDragStart;
private bool dragStarted; private bool dragStarted;
private Vector2 lastWindowSize = Vector2.Zero; private Vector2 lastWindowSize = Vector2.Zero;
/// <inheritdoc/> /// <inheritdoc/>
public string[]? CommandShortcuts { get; init; } = { "icon", "icons" }; public string[]? CommandShortcuts { get; init; } = { "icon", "icons" };
@ -46,7 +44,7 @@ public class IconBrowserWidget : IDataWindowWidget
public void Load() public void Load()
{ {
} }
/// <inheritdoc/> /// <inheritdoc/>
public void Draw() public void Draw()
{ {
@ -54,23 +52,24 @@ public class IconBrowserWidget : IDataWindowWidget
if (ImGui.BeginChild("ScrollableSection", ImGui.GetContentRegionAvail(), false, ImGuiWindowFlags.NoMove)) if (ImGui.BeginChild("ScrollableSection", ImGui.GetContentRegionAvail(), false, ImGuiWindowFlags.NoMove))
{ {
var itemsPerRow = (int)MathF.Floor(ImGui.GetContentRegionMax().X / (this.iconSize.X + ImGui.GetStyle().ItemSpacing.X)); var itemsPerRow = (int)MathF.Floor(
ImGui.GetContentRegionMax().X / (this.iconSize.X + ImGui.GetStyle().ItemSpacing.X));
var itemHeight = this.iconSize.Y + ImGui.GetStyle().ItemSpacing.Y; var itemHeight = this.iconSize.Y + ImGui.GetStyle().ItemSpacing.Y;
ImGuiClip.ClippedDraw(this.valueRange, this.DrawIcon, itemsPerRow, itemHeight); ImGuiClip.ClippedDraw(this.valueRange, this.DrawIcon, itemsPerRow, itemHeight);
} }
ImGui.EndChild(); ImGui.EndChild();
this.ProcessMouseDragging(); this.ProcessMouseDragging();
if (this.lastNullValueCount != this.nullValues.Count) if (this.lastNullValueCount != this.nullValues.Count)
{ {
this.RecalculateIndexRange(); this.RecalculateIndexRange();
this.lastNullValueCount = this.nullValues.Count; this.lastNullValueCount = this.nullValues.Count;
} }
} }
// Limit the popup image to half our screen size. // Limit the popup image to half our screen size.
private static float GetImageScaleFactor(IDalamudTextureWrap texture) private static float GetImageScaleFactor(IDalamudTextureWrap texture)
{ {
@ -84,10 +83,10 @@ public class IconBrowserWidget : IDataWindowWidget
scale = MathF.Min(widthRatio, heightRatio); scale = MathF.Min(widthRatio, heightRatio);
} }
return scale; return scale;
} }
private void DrawOptions() private void DrawOptions()
{ {
ImGui.Columns(2); ImGui.Columns(2);
@ -101,48 +100,45 @@ public class IconBrowserWidget : IDataWindowWidget
ImGui.NextColumn(); ImGui.NextColumn();
ImGui.Checkbox("Show Image in Tooltip", ref this.showTooltipImage); ImGui.Checkbox("Show Image in Tooltip", ref this.showTooltipImage);
ImGui.NextColumn(); ImGui.NextColumn();
ImGui.InputFloat2("Icon Size", ref this.editIconSize); ImGui.InputFloat2("Icon Size", ref this.editIconSize);
if (ImGui.IsItemDeactivatedAfterEdit()) if (ImGui.IsItemDeactivatedAfterEdit())
{ {
this.iconSize = this.editIconSize; this.iconSize = this.editIconSize;
} }
ImGui.Columns(1); ImGui.Columns(1);
} }
private void DrawIcon(int iconId) private void DrawIcon(int iconId)
{ {
var texm = Service<TextureManager>.Get();
try try
{ {
var cursor = ImGui.GetCursorScreenPos(); var cursor = ImGui.GetCursorScreenPos();
var texture = texm.ImmediateGetFromGameIcon(new((uint)iconId));
if (!this.IsIconValid(iconId))
{ if (texm.ImmediateGetStateFromGameIcon(new((uint)iconId), out var exc) || exc is null)
this.nullValues.Add(iconId);
return;
}
if (Service<TextureManager>.Get().GetIcon((uint)iconId) is { } texture)
{ {
ImGui.Image(texture.ImGuiHandle, this.iconSize); ImGui.Image(texture.ImGuiHandle, this.iconSize);
// If we have the option to show a tooltip image, draw the image, but make sure it's not too big. // If we have the option to show a tooltip image, draw the image, but make sure it's not too big.
if (ImGui.IsItemHovered() && this.showTooltipImage) if (ImGui.IsItemHovered() && this.showTooltipImage)
{ {
ImGui.BeginTooltip(); ImGui.BeginTooltip();
var scale = GetImageScaleFactor(texture); var scale = GetImageScaleFactor(texture);
var textSize = ImGui.CalcTextSize(iconId.ToString()); var textSize = ImGui.CalcTextSize(iconId.ToString());
ImGui.SetCursorPosX(texture.Size.X * scale / 2.0f - textSize.X / 2.0f + ImGui.GetStyle().FramePadding.X * 2.0f); ImGui.SetCursorPosX(
texture.Size.X * scale / 2.0f - textSize.X / 2.0f + ImGui.GetStyle().FramePadding.X * 2.0f);
ImGui.Text(iconId.ToString()); ImGui.Text(iconId.ToString());
ImGui.Image(texture.ImGuiHandle, texture.Size * scale); ImGui.Image(texture.ImGuiHandle, texture.Size * scale);
ImGui.EndTooltip(); ImGui.EndTooltip();
} }
// else, just draw the iconId. // else, just draw the iconId.
else if (ImGui.IsItemHovered()) else if (ImGui.IsItemHovered())
{ {
@ -155,7 +151,10 @@ public class IconBrowserWidget : IDataWindowWidget
this.nullValues.Add(iconId); this.nullValues.Add(iconId);
} }
ImGui.GetWindowDrawList().AddRect(cursor, cursor + this.iconSize, ImGui.GetColorU32(ImGuiColors.DalamudWhite)); ImGui.GetWindowDrawList().AddRect(
cursor,
cursor + this.iconSize,
ImGui.GetColorU32(ImGuiColors.DalamudWhite));
} }
catch (Exception) catch (Exception)
{ {
@ -182,7 +181,7 @@ public class IconBrowserWidget : IDataWindowWidget
this.dragStarted = false; this.dragStarted = false;
} }
} }
if (ImGui.IsMouseDragging(ImGuiMouseButton.Left) && this.dragStarted) if (ImGui.IsMouseDragging(ImGuiMouseButton.Left) && this.dragStarted)
{ {
var delta = this.mouseDragStart - ImGui.GetMousePos(); var delta = this.mouseDragStart - ImGui.GetMousePos();
@ -194,13 +193,6 @@ public class IconBrowserWidget : IDataWindowWidget
this.dragStarted = false; this.dragStarted = false;
} }
} }
// Check if the icon has a valid filepath, and exists in the game data.
private bool IsIconValid(int iconId)
{
var filePath = Service<TextureManager>.Get().GetIconPath((uint)iconId);
return !filePath.IsNullOrEmpty() && Service<DataManager>.Get().FileExists(filePath);
}
private void RecalculateIndexRange() private void RecalculateIndexRange()
{ {