Add ITitleScreenMenu and Scoped Service. (#1379)

This commit is contained in:
MidoriKami 2023-09-11 22:06:40 -07:00 committed by GitHub
parent 0f3b9eab8c
commit af63217564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 195 additions and 114 deletions

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -230,7 +229,7 @@ internal class TitleScreenMenuWindow : Window, IDisposable
}
private bool DrawEntry(
TitleScreenMenu.TitleScreenMenuEntry entry, bool inhibitFadeout, bool showText, bool isFirst, bool overrideAlpha, bool interactable)
TitleScreenMenuEntry entry, bool inhibitFadeout, bool showText, bool isFirst, bool overrideAlpha, bool interactable)
{
InterfaceManager.SpecialGlyphRequest fontHandle;
if (this.specialGlyphRequests.TryGetValue(entry.Name, out fontHandle) && fontHandle.Size != TargetFontSizePx)

View file

@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using Dalamud.Plugin.Services;
using ImGuiScene;
namespace Dalamud.Interface;
@ -12,10 +12,9 @@ namespace Dalamud.Interface;
/// <summary>
/// Class responsible for managing elements in the title screen menu.
/// </summary>
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public class TitleScreenMenu : IServiceType
internal class TitleScreenMenu : IServiceType, ITitleScreenMenu
{
/// <summary>
/// Gets the texture size needed for title screen menu logos.
@ -29,19 +28,10 @@ public class TitleScreenMenu : IServiceType
{
}
/// <summary>
/// Gets the list of entries in the title screen menu.
/// </summary>
/// <inheritdoc/>
public IReadOnlyList<TitleScreenMenuEntry> Entries => this.entries;
/// <summary>
/// Adds a new entry to the title screen menu.
/// </summary>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
/// <returns>A <see cref="TitleScreenMenu"/> object that can be used to manage the entry.</returns>
/// <exception cref="ArgumentException">Thrown when the texture provided does not match the required resolution(64x64).</exception>
/// <inheritdoc/>
public TitleScreenMenuEntry AddEntry(string text, TextureWrap texture, Action onTriggered)
{
if (texture.Height != TextureSize || texture.Width != TextureSize)
@ -64,15 +54,7 @@ public class TitleScreenMenu : IServiceType
}
}
/// <summary>
/// Adds a new entry to the title screen menu.
/// </summary>
/// <param name="priority">Priority of the entry.</param>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
/// <returns>A <see cref="TitleScreenMenu"/> object that can be used to manage the entry.</returns>
/// <exception cref="ArgumentException">Thrown when the texture provided does not match the required resolution(64x64).</exception>
/// <inheritdoc/>
public TitleScreenMenuEntry AddEntry(ulong priority, string text, TextureWrap texture, Action onTriggered)
{
if (texture.Height != TextureSize || texture.Width != TextureSize)
@ -91,10 +73,7 @@ public class TitleScreenMenu : IServiceType
}
}
/// <summary>
/// Remove an entry from the title screen menu.
/// </summary>
/// <param name="entry">The entry to remove.</param>
/// <inheritdoc/>
public void RemoveEntry(TitleScreenMenuEntry entry)
{
lock (this.entries)
@ -159,93 +138,58 @@ public class TitleScreenMenu : IServiceType
return entry;
}
}
}
/// <summary>
/// Class representing an entry in the title screen menu.
/// </summary>
public class TitleScreenMenuEntry : IComparable<TitleScreenMenuEntry>
/// <summary>
/// Plugin-scoped version of a TitleScreenMenu service.
/// </summary>
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.ScopedService]
#pragma warning disable SA1015
[ResolveVia<ITitleScreenMenu>]
#pragma warning restore SA1015
internal class TitleScreenMenuPluginScoped : IDisposable, IServiceType, ITitleScreenMenu
{
[ServiceManager.ServiceDependency]
private readonly TitleScreenMenu titleScreenMenuService = Service<TitleScreenMenu>.Get();
private readonly List<TitleScreenMenuEntry> pluginEntries = new();
/// <inheritdoc/>
public IReadOnlyList<TitleScreenMenuEntry>? Entries => this.titleScreenMenuService.Entries;
/// <inheritdoc/>
public void Dispose()
{
private readonly Action onTriggered;
/// <summary>
/// Initializes a new instance of the <see cref="TitleScreenMenuEntry"/> class.
/// </summary>
/// <param name="callingAssembly">The calling assembly.</param>
/// <param name="priority">The priority of this entry.</param>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
internal TitleScreenMenuEntry(Assembly? callingAssembly, ulong priority, string text, TextureWrap texture, Action onTriggered)
foreach (var entry in this.pluginEntries)
{
this.CallingAssembly = callingAssembly;
this.Priority = priority;
this.Name = text;
this.Texture = texture;
this.onTriggered = onTriggered;
}
/// <summary>
/// Gets the priority of this entry.
/// </summary>
public ulong Priority { get; init; }
/// <summary>
/// Gets or sets the name of this entry.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the texture of this entry.
/// </summary>
public TextureWrap Texture { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not this entry is internal.
/// </summary>
internal bool IsInternal { get; set; }
/// <summary>
/// Gets the calling assembly of this entry.
/// </summary>
internal Assembly? CallingAssembly { get; init; }
/// <summary>
/// Gets the internal ID of this entry.
/// </summary>
internal Guid Id { get; init; } = Guid.NewGuid();
/// <inheritdoc/>
public int CompareTo(TitleScreenMenuEntry? other)
{
if (other == null)
return 1;
if (this.CallingAssembly != other.CallingAssembly)
{
if (this.CallingAssembly == null && other.CallingAssembly == null)
return 0;
if (this.CallingAssembly == null && other.CallingAssembly != null)
return -1;
if (this.CallingAssembly != null && other.CallingAssembly == null)
return 1;
return string.Compare(
this.CallingAssembly!.FullName!,
other.CallingAssembly!.FullName!,
StringComparison.CurrentCultureIgnoreCase);
}
if (this.Priority != other.Priority)
return this.Priority.CompareTo(other.Priority);
if (this.Name != other.Name)
return string.Compare(this.Name, other.Name, StringComparison.InvariantCultureIgnoreCase);
return string.Compare(this.Name, other.Name, StringComparison.InvariantCulture);
}
/// <summary>
/// Trigger the action associated with this entry.
/// </summary>
internal void Trigger()
{
this.onTriggered();
this.titleScreenMenuService.RemoveEntry(entry);
}
}
/// <inheritdoc/>
public TitleScreenMenuEntry AddEntry(string text, TextureWrap texture, Action onTriggered)
{
var entry = this.titleScreenMenuService.AddEntry(text, texture, onTriggered);
this.pluginEntries.Add(entry);
return entry;
}
/// <inheritdoc/>
public TitleScreenMenuEntry AddEntry(ulong priority, string text, TextureWrap texture, Action onTriggered)
{
var entry = this.titleScreenMenuService.AddEntry(priority, text, texture, onTriggered);
this.pluginEntries.Add(entry);
return entry;
}
/// <inheritdoc/>
public void RemoveEntry(TitleScreenMenuEntry entry)
{
this.pluginEntries.Remove(entry);
this.titleScreenMenuService.RemoveEntry(entry);
}
}

View file

@ -0,0 +1,94 @@
using System.Reflection;
using ImGuiScene;
namespace Dalamud.Interface;
/// <summary>
/// Class representing an entry in the title screen menu.
/// </summary>
public class TitleScreenMenuEntry : IComparable<TitleScreenMenuEntry>
{
private readonly Action onTriggered;
/// <summary>
/// Initializes a new instance of the <see cref="TitleScreenMenuEntry"/> class.
/// </summary>
/// <param name="callingAssembly">The calling assembly.</param>
/// <param name="priority">The priority of this entry.</param>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
internal TitleScreenMenuEntry(Assembly? callingAssembly, ulong priority, string text, TextureWrap texture, Action onTriggered)
{
this.CallingAssembly = callingAssembly;
this.Priority = priority;
this.Name = text;
this.Texture = texture;
this.onTriggered = onTriggered;
}
/// <summary>
/// Gets the priority of this entry.
/// </summary>
public ulong Priority { get; init; }
/// <summary>
/// Gets or sets the name of this entry.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the texture of this entry.
/// </summary>
public TextureWrap Texture { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not this entry is internal.
/// </summary>
internal bool IsInternal { get; set; }
/// <summary>
/// Gets the calling assembly of this entry.
/// </summary>
internal Assembly? CallingAssembly { get; init; }
/// <summary>
/// Gets the internal ID of this entry.
/// </summary>
internal Guid Id { get; init; } = Guid.NewGuid();
/// <inheritdoc/>
public int CompareTo(TitleScreenMenuEntry? other)
{
if (other == null)
return 1;
if (this.CallingAssembly != other.CallingAssembly)
{
if (this.CallingAssembly == null && other.CallingAssembly == null)
return 0;
if (this.CallingAssembly == null && other.CallingAssembly != null)
return -1;
if (this.CallingAssembly != null && other.CallingAssembly == null)
return 1;
return string.Compare(
this.CallingAssembly!.FullName!,
other.CallingAssembly!.FullName!,
StringComparison.CurrentCultureIgnoreCase);
}
if (this.Priority != other.Priority)
return this.Priority.CompareTo(other.Priority);
if (this.Name != other.Name)
return string.Compare(this.Name, other.Name, StringComparison.InvariantCultureIgnoreCase);
return 0;
}
/// <summary>
/// Trigger the action associated with this entry.
/// </summary>
internal void Trigger()
{
this.onTriggered();
}
}

View file

@ -0,0 +1,44 @@
using System.Collections.Generic;
using Dalamud.Interface;
using ImGuiScene;
namespace Dalamud.Plugin.Services;
/// <summary>
/// Interface for class responsible for managing elements in the title screen menu.
/// </summary>
public interface ITitleScreenMenu
{
/// <summary>
/// Gets the list of entries in the title screen menu.
/// </summary>
public IReadOnlyList<TitleScreenMenuEntry> Entries { get; }
/// <summary>
/// Adds a new entry to the title screen menu.
/// </summary>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
/// <returns>A <see cref="TitleScreenMenu"/> object that can be used to manage the entry.</returns>
/// <exception cref="ArgumentException">Thrown when the texture provided does not match the required resolution(64x64).</exception>
public TitleScreenMenuEntry AddEntry(string text, TextureWrap texture, Action onTriggered);
/// <summary>
/// Adds a new entry to the title screen menu.
/// </summary>
/// <param name="priority">Priority of the entry.</param>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
/// <returns>A <see cref="TitleScreenMenu"/> object that can be used to manage the entry.</returns>
/// <exception cref="ArgumentException">Thrown when the texture provided does not match the required resolution(64x64).</exception>
public TitleScreenMenuEntry AddEntry(ulong priority, string text, TextureWrap texture, Action onTriggered);
/// <summary>
/// Remove an entry from the title screen menu.
/// </summary>
/// <param name="entry">The entry to remove.</param>
public void RemoveEntry(TitleScreenMenuEntry entry);
}