Support SetIconTexture(Task<IDalamudTextureWrap?>?)

This commit is contained in:
Soreepeong 2024-02-28 17:27:19 +09:00
parent 2a2fded520
commit 6b875bbcb5
5 changed files with 60 additions and 14 deletions

View file

@ -1,4 +1,5 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Dalamud.Interface.ImGuiNotification.EventArgs; using Dalamud.Interface.ImGuiNotification.EventArgs;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
@ -50,7 +51,7 @@ public interface IActiveNotification : INotification
/// <remarks>This does not override <see cref="INotification.HardExpiry"/>.</remarks> /// <remarks>This does not override <see cref="INotification.HardExpiry"/>.</remarks>
void ExtendBy(TimeSpan extension); void ExtendBy(TimeSpan extension);
/// <summary>Sets the icon from <see cref="IDalamudTextureWrap"/>, overriding the icon .</summary> /// <summary>Sets the icon from <see cref="IDalamudTextureWrap"/>, overriding the icon.</summary>
/// <param name="textureWrap">The new texture wrap to use, or null to clear and revert back to the icon specified /// <param name="textureWrap">The new texture wrap to use, or null to clear and revert back to the icon specified
/// from <see cref="INotification.Icon"/>.</param> /// from <see cref="INotification.Icon"/>.</param>
/// <remarks> /// <remarks>
@ -61,6 +62,21 @@ public interface IActiveNotification : INotification
/// </remarks> /// </remarks>
void SetIconTexture(IDalamudTextureWrap? textureWrap); void SetIconTexture(IDalamudTextureWrap? textureWrap);
/// <summary>Sets the icon from <see cref="IDalamudTextureWrap"/>, overriding the icon, once the given task
/// completes.</summary>
/// <param name="textureWrapTask">The task that will result in a new texture wrap to use, or null to clear and
/// revert back to the icon specified from <see cref="INotification.Icon"/>.</param>
/// <remarks>
/// <para>The texture resulted from the passed <see cref="Task{TResult}"/> will be disposed when the notification
/// is dismissed or a new different texture is set via another call to this function. You do not have to dispose the
/// resulted instance of <see cref="IDalamudTextureWrap"/> yourself.</para>
/// <para>If the task fails for any reason, the exception will be silently ignored and the icon specified from
/// <see cref="INotification.Icon"/> will be used instead.</para>
/// <para>If <see cref="DismissReason"/> is not <c>null</c>, then calling this function will simply dispose the
/// result of the passed <paramref name="textureWrapTask"/> without actually updating the icon.</para>
/// </remarks>
void SetIconTexture(Task<IDalamudTextureWrap?>? textureWrapTask);
/// <summary>Generates a new value to use for <see cref="Id"/>.</summary> /// <summary>Generates a new value to use for <see cref="Id"/>.</summary>
/// <returns>The new value.</returns> /// <returns>The new value.</returns>
internal static long CreateNewId() => Interlocked.Increment(ref idCounter); internal static long CreateNewId() => Interlocked.Increment(ref idCounter);

View file

@ -1,3 +1,6 @@
using System.Threading.Tasks;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.Internal.Notifications;
using Dalamud.Plugin.Services; using Dalamud.Plugin.Services;
@ -20,8 +23,10 @@ public interface INotification
NotificationType Type { get; set; } NotificationType Type { get; set; }
/// <summary>Gets or sets the icon source.</summary> /// <summary>Gets or sets the icon source.</summary>
/// <remarks>Use <see cref="IActiveNotification.SetIconTexture"/> to use a texture, after calling /// <remarks>Use <see cref="IActiveNotification.SetIconTexture(IDalamudTextureWrap?)"/> or
/// <see cref="INotificationManager.AddNotification"/>.</remarks> /// <see cref="IActiveNotification.SetIconTexture(Task{IDalamudTextureWrap?}?)"/> to use a texture, after calling
/// <see cref="INotificationManager.AddNotification"/>. Call either of those functions with <c>null</c> to revert
/// the effective icon back to this property.</remarks>
INotificationIcon? Icon { get; set; } INotificationIcon? Icon { get; set; }
/// <summary>Gets or sets the hard expiry.</summary> /// <summary>Gets or sets the hard expiry.</summary>

View file

@ -1,5 +1,6 @@
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Dalamud.Interface.Animation; using Dalamud.Interface.Animation;
using Dalamud.Interface.Animation.EasingFunctions; using Dalamud.Interface.Animation.EasingFunctions;
@ -29,7 +30,7 @@ internal sealed partial class ActiveNotification : IActiveNotification
private DateTime extendedExpiry; private DateTime extendedExpiry;
/// <summary>The icon texture to use if specified; otherwise, icon will be used from <see cref="Icon"/>.</summary> /// <summary>The icon texture to use if specified; otherwise, icon will be used from <see cref="Icon"/>.</summary>
private IDalamudTextureWrap? iconTextureWrap; private Task<IDalamudTextureWrap>? iconTextureWrap;
/// <summary>The plugin that initiated this notification.</summary> /// <summary>The plugin that initiated this notification.</summary>
private LocalPlugin? initiatorPlugin; private LocalPlugin? initiatorPlugin;
@ -229,18 +230,24 @@ internal sealed partial class ActiveNotification : IActiveNotification
/// <inheritdoc/> /// <inheritdoc/>
public void SetIconTexture(IDalamudTextureWrap? textureWrap) public void SetIconTexture(IDalamudTextureWrap? textureWrap)
{
this.SetIconTexture(textureWrap is null ? null : Task.FromResult(textureWrap));
}
/// <inheritdoc/>
public void SetIconTexture(Task<IDalamudTextureWrap?>? textureWrapTask)
{ {
if (this.DismissReason is not null) if (this.DismissReason is not null)
{ {
textureWrap?.Dispose(); textureWrapTask?.ToContentDisposedTask(true);
return; return;
} }
// After replacing, if the old texture is not the old texture, then dispose the old texture. // After replacing, if the old texture is not the old texture, then dispose the old texture.
if (Interlocked.Exchange(ref this.iconTextureWrap, textureWrap) is { } wrapToDispose && if (Interlocked.Exchange(ref this.iconTextureWrap, textureWrapTask) is { } wrapTaskToDispose &&
wrapToDispose != textureWrap) wrapTaskToDispose != textureWrapTask)
{ {
wrapToDispose.Dispose(); wrapTaskToDispose.ToContentDisposedTask(true);
} }
} }
@ -333,8 +340,8 @@ internal sealed partial class ActiveNotification : IActiveNotification
/// <summary>Clears the resources associated with this instance of <see cref="ActiveNotification"/>.</summary> /// <summary>Clears the resources associated with this instance of <see cref="ActiveNotification"/>.</summary>
internal void DisposeInternal() internal void DisposeInternal()
{ {
if (Interlocked.Exchange(ref this.iconTextureWrap, null) is { } wrapToDispose) if (Interlocked.Exchange(ref this.iconTextureWrap, null) is { } wrapTaskToDispose)
wrapToDispose.Dispose(); wrapTaskToDispose.ToContentDisposedTask(true);
this.Dismiss = null; this.Dismiss = null;
this.Click = null; this.Click = null;
this.DrawActions = null; this.DrawActions = null;

View file

@ -1,6 +1,7 @@
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Dalamud.Game.Text; using Dalamud.Game.Text;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
@ -103,6 +104,16 @@ public static class NotificationUtilities
} }
} }
/// <summary>Draws an icon from an instance of <see cref="Task{TResult}"/> that results in an
/// <see cref="IDalamudTextureWrap"/>.</summary>
/// <param name="minCoord">The coordinates of the top left of the icon area.</param>
/// <param name="maxCoord">The coordinates of the bottom right of the icon area.</param>
/// <param name="textureTask">The task that results in a texture.</param>
/// <returns><c>true</c> if anything has been drawn.</returns>
/// <remarks>Exceptions from the task will be treated as if no texture is provided.</remarks>
internal static bool DrawIconFrom(Vector2 minCoord, Vector2 maxCoord, Task<IDalamudTextureWrap?>? textureTask) =>
textureTask?.IsCompletedSuccessfully is true && DrawIconFrom(minCoord, maxCoord, textureTask.Result);
/// <summary>Draws an icon from an instance of <see cref="LocalPlugin"/>.</summary> /// <summary>Draws an icon from an instance of <see cref="LocalPlugin"/>.</summary>
/// <param name="minCoord">The coordinates of the top left of the icon area.</param> /// <param name="minCoord">The coordinates of the top left of the icon area.</param>
/// <param name="maxCoord">The coordinates of the bottom right of the icon area.</param> /// <param name="maxCoord">The coordinates of the bottom right of the icon area.</param>

View file

@ -88,20 +88,20 @@ internal class ImGuiWidget : IDataWindowWidget
ref this.notificationTemplate.IconText, ref this.notificationTemplate.IconText,
255); 255);
break; break;
case 3 or 4: case 5 or 6:
ImGui.Combo( ImGui.Combo(
"Asset##iconAssetCombo", "Asset##iconAssetCombo",
ref this.notificationTemplate.IconAssetInt, ref this.notificationTemplate.IconAssetInt,
NotificationTemplate.AssetSources, NotificationTemplate.AssetSources,
NotificationTemplate.AssetSources.Length); NotificationTemplate.AssetSources.Length);
break; break;
case 5 or 7: case 3 or 7:
ImGui.InputText( ImGui.InputText(
"Game Path##iconText", "Game Path##iconText",
ref this.notificationTemplate.IconText, ref this.notificationTemplate.IconText,
255); 255);
break; break;
case 6 or 8: case 4 or 8:
ImGui.InputText( ImGui.InputText(
"File Path##iconText", "File Path##iconText",
ref this.notificationTemplate.IconText, ref this.notificationTemplate.IconText,
@ -206,9 +206,15 @@ internal class ImGuiWidget : IDataWindowWidget
NotificationTemplate.AssetSources[this.notificationTemplate.IconAssetInt]))); NotificationTemplate.AssetSources[this.notificationTemplate.IconAssetInt])));
break; break;
case 6: case 6:
n.SetIconTexture(tm.GetTextureFromGame(this.notificationTemplate.IconText)); n.SetIconTexture(
dam.GetDalamudTextureWrapAsync(
Enum.Parse<DalamudAsset>(
NotificationTemplate.AssetSources[this.notificationTemplate.IconAssetInt])));
break; break;
case 7: case 7:
n.SetIconTexture(tm.GetTextureFromGame(this.notificationTemplate.IconText));
break;
case 8:
n.SetIconTexture(tm.GetTextureFromFile(new(this.notificationTemplate.IconText))); n.SetIconTexture(tm.GetTextureFromFile(new(this.notificationTemplate.IconText)));
break; break;
} }
@ -315,6 +321,7 @@ internal class ImGuiWidget : IDataWindowWidget
"GamePath", "GamePath",
"FilePath", "FilePath",
"TextureWrap from DalamudAssets", "TextureWrap from DalamudAssets",
"TextureWrap from DalamudAssets(Async)",
"TextureWrap from GamePath", "TextureWrap from GamePath",
"TextureWrap from FilePath", "TextureWrap from FilePath",
}; };