From 8fde0af0f976a230004e1dde37173aefe3d045b8 Mon Sep 17 00:00:00 2001 From: MidoriKami <9083275+MidoriKami@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:25:17 -0700 Subject: [PATCH 1/2] Add IconBrowser Widget --- .../Internal/Windows/Data/DataWindow.cs | 2 +- .../Windows/Data/Widgets/IconBrowserWidget.cs | 216 ++++++++++++++++++ 2 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs diff --git a/Dalamud/Interface/Internal/Windows/Data/DataWindow.cs b/Dalamud/Interface/Internal/Windows/Data/DataWindow.cs index 1363c6abe..ba47d2c8e 100644 --- a/Dalamud/Interface/Internal/Windows/Data/DataWindow.cs +++ b/Dalamud/Interface/Internal/Windows/Data/DataWindow.cs @@ -1,4 +1,3 @@ -using System; using System.Linq; using System.Numerics; @@ -50,6 +49,7 @@ internal class DataWindow : Window new UIColorWidget(), new DataShareWidget(), new NetworkMonitorWidget(), + new IconBrowserWidget(), }; private readonly IOrderedEnumerable orderedModules; diff --git a/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs b/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs new file mode 100644 index 000000000..b37878045 --- /dev/null +++ b/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs @@ -0,0 +1,216 @@ +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Numerics; + +using Dalamud.Data; +using Dalamud.Interface.Utility; +using Dalamud.Utility; +using ImGuiNET; + +namespace Dalamud.Interface.Internal.Windows.Data.Widgets; + +/// +/// Data widget for browsing in-game icons. +/// +public class IconBrowserWidget : IDataWindowWidget +{ + // Remove range 170,000 -> 180,000 by default, this specific range causes exceptions. + private readonly HashSet nullValues = Enumerable.Range(170000, 10000).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" }; + + /// + public string DisplayName { get; init; } = "Icon Browser"; + + /// + public bool Ready { get; set; } = true; + + /// + public void Load() + { + } + + /// + public void Draw() + { + this.DrawOptions(); + + 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 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) + { + var workArea = ImGui.GetMainViewport().Size / 2.0f; + var scale = 1.0f; + + if (texture.Width > workArea.X || texture.Height > workArea.Y) + { + var widthRatio = workArea.X / texture.Width; + var heightRatio = workArea.Y / texture.Height; + + scale = MathF.Min(widthRatio, heightRatio); + } + + return scale; + } + + private void DrawOptions() + { + ImGui.Columns(2); + + ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X); + if (ImGui.InputInt("##StartRange", ref this.startRange, 0, 0)) this.RecalculateIndexRange(); + + ImGui.NextColumn(); + ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X); + if (ImGui.InputInt("##StopRange", ref this.stopRange, 0, 0)) this.RecalculateIndexRange(); + + 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) + { + try + { + var cursor = ImGui.GetCursorScreenPos(); + + if (!this.IsIconValid(iconId)) + { + this.nullValues.Add(iconId); + return; + } + + if (Service.Get().GetIcon((uint)iconId) is { } texture) + { + 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.Text(iconId.ToString()); + + ImGui.Image(texture.ImGuiHandle, texture.Size * scale); + ImGui.EndTooltip(); + } + + // else, just draw the iconId. + else if (ImGui.IsItemHovered()) + { + ImGui.SetTooltip(iconId.ToString()); + } + } + else + { + // This texture was null, draw nothing, and prevent from trying to show it again. + this.nullValues.Add(iconId); + } + + ImGui.GetWindowDrawList().AddRect(cursor, cursor + this.iconSize, ImGui.GetColorU32(KnownColor.White.Vector())); + } + catch (Exception) + { + // If something went wrong, prevent from trying to show this icon again. + this.nullValues.Add(iconId); + } + } + + private void ProcessMouseDragging() + { + if (ImGui.IsItemHovered() || this.dragStarted) + { + if (ImGui.GetWindowSize() == this.lastWindowSize) + { + if (ImGui.IsItemClicked(ImGuiMouseButton.Left) && !this.dragStarted) + { + this.mouseDragStart = ImGui.GetMousePos(); + this.dragStarted = true; + } + } + else + { + this.lastWindowSize = ImGui.GetWindowSize(); + this.dragStarted = false; + } + } + + if (ImGui.IsMouseDragging(ImGuiMouseButton.Left) && this.dragStarted) + { + var delta = this.mouseDragStart - ImGui.GetMousePos(); + ImGui.GetIO().AddMouseWheelEvent(0.0f, -delta.Y / 85.0f); + this.mouseDragStart = ImGui.GetMousePos(); + } + else if (ImGui.IsMouseReleased(ImGuiMouseButton.Left)) + { + 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() + { + if (this.stopRange <= this.startRange || this.stopRange <= 0 || this.startRange < 0) + { + this.valueRange = new List(); + } + else + { + this.valueRange = Enumerable.Range(this.startRange, this.stopRange - this.startRange).ToList(); + this.valueRange.RemoveAll(value => this.nullValues.Contains(value)); + } + } +} From a1f1c169dce5ebe244826bb5df6e35f4fc5cdf17 Mon Sep 17 00:00:00 2001 From: MidoriKami <9083275+MidoriKami@users.noreply.github.com> Date: Sun, 1 Oct 2023 12:01:58 -0700 Subject: [PATCH 2/2] Allow icon 180000 --- .../Internal/Windows/Data/Widgets/IconBrowserWidget.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs b/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs index b37878045..dcae6e689 100644 --- a/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs +++ b/Dalamud/Interface/Internal/Windows/Data/Widgets/IconBrowserWidget.cs @@ -16,7 +16,7 @@ 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, 10000).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);