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.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<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 editIconSize = new(64.0f, 64.0f);
private List<int> 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;
/// <inheritdoc/>
public string[]? CommandShortcuts { get; init; } = { "icon", "icons" };
@ -46,7 +44,7 @@ public class IconBrowserWidget : IDataWindowWidget
public void Load()
{
}
/// <inheritdoc/>
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<TextureManager>.Get();
try
{
var cursor = ImGui.GetCursorScreenPos();
if (!this.IsIconValid(iconId))
{
this.nullValues.Add(iconId);
return;
}
if (Service<TextureManager>.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<TextureManager>.Get().GetIconPath((uint)iconId);
return !filePath.IsNullOrEmpty() && Service<DataManager>.Get().FileExists(filePath);
}
private void RecalculateIndexRange()
{