From 2920d18afa8dd37a3b2281b0e888337500eb48d3 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Thu, 22 Feb 2024 03:36:55 +0900 Subject: [PATCH] Update IconBrowserWidget --- .../Windows/Data/Widgets/IconBrowserWidget.cs | 78 +++++++++---------- 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs b/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs index 06c691cc9..37fc958af 100644 --- a/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs +++ b/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs @@ -1,12 +1,10 @@ using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Numerics; -using Dalamud.Data; using Dalamud.Interface.Colors; using Dalamud.Interface.Utility; -using Dalamud.Utility; + using ImGuiNET; namespace Dalamud.Interface.Internal.Windows.Data.Widgets; @@ -17,22 +15,22 @@ namespace Dalamud.Interface.Internal.Windows.Data.Widgets; public class IconBrowserWidget : IDataWindowWidget { // Remove range 170,000 -> 180,000 by default, this specific range causes exceptions. - private readonly HashSet nullValues = Enumerable.Range(170000, 9999).ToHashSet(); - + private readonly HashSet nullValues = Enumerable.Range(170000, 9999).ToHashSet(); + private Vector2 iconSize = new(64.0f, 64.0f); private Vector2 editIconSize = new(64.0f, 64.0f); - + private List valueRange = Enumerable.Range(0, 200000).ToList(); - + private int lastNullValueCount; private int startRange; private int stopRange = 200000; private bool showTooltipImage; - + private Vector2 mouseDragStart; private bool dragStarted; private Vector2 lastWindowSize = Vector2.Zero; - + /// public string[]? CommandShortcuts { get; init; } = { "icon", "icons" }; @@ -46,7 +44,7 @@ public class IconBrowserWidget : IDataWindowWidget public void Load() { } - + /// public void Draw() { @@ -54,23 +52,24 @@ public class IconBrowserWidget : IDataWindowWidget 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; ImGuiClip.ClippedDraw(this.valueRange, this.DrawIcon, itemsPerRow, itemHeight); } - + ImGui.EndChild(); - + this.ProcessMouseDragging(); - + if (this.lastNullValueCount != this.nullValues.Count) { this.RecalculateIndexRange(); this.lastNullValueCount = this.nullValues.Count; } } - + // Limit the popup image to half our screen size. private static float GetImageScaleFactor(IDalamudTextureWrap texture) { @@ -84,10 +83,10 @@ public class IconBrowserWidget : IDataWindowWidget scale = MathF.Min(widthRatio, heightRatio); } - + return scale; } - + private void DrawOptions() { ImGui.Columns(2); @@ -101,48 +100,45 @@ public class IconBrowserWidget : IDataWindowWidget ImGui.NextColumn(); ImGui.Checkbox("Show Image in Tooltip", ref this.showTooltipImage); - + ImGui.NextColumn(); ImGui.InputFloat2("Icon Size", ref this.editIconSize); if (ImGui.IsItemDeactivatedAfterEdit()) { this.iconSize = this.editIconSize; } - + ImGui.Columns(1); } - + private void DrawIcon(int iconId) { + var texm = Service.Get(); try { var cursor = ImGui.GetCursorScreenPos(); - - if (!this.IsIconValid(iconId)) - { - this.nullValues.Add(iconId); - return; - } - - if (Service.Get().GetIcon((uint)iconId) is { } texture) + var texture = texm.ImmediateGetFromGameIcon(new((uint)iconId)); + + if (texm.ImmediateGetStateFromGameIcon(new((uint)iconId), out var exc) || exc is null) { 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 (ImGui.IsItemHovered() && this.showTooltipImage) { ImGui.BeginTooltip(); - + var scale = GetImageScaleFactor(texture); - + 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.Image(texture.ImGuiHandle, texture.Size * scale); ImGui.EndTooltip(); } - + // else, just draw the iconId. else if (ImGui.IsItemHovered()) { @@ -155,7 +151,10 @@ public class IconBrowserWidget : IDataWindowWidget 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) { @@ -182,7 +181,7 @@ public class IconBrowserWidget : IDataWindowWidget this.dragStarted = false; } } - + if (ImGui.IsMouseDragging(ImGuiMouseButton.Left) && this.dragStarted) { var delta = this.mouseDragStart - ImGui.GetMousePos(); @@ -194,13 +193,6 @@ public class IconBrowserWidget : IDataWindowWidget 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.Get().GetIconPath((uint)iconId); - return !filePath.IsNullOrEmpty() && Service.Get().FileExists(filePath); - } private void RecalculateIndexRange() {