refactor(context): use generic delegates for items

This commit is contained in:
Anna 2021-04-30 15:07:54 -04:00
parent a034beb937
commit 90c91c09d4
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
5 changed files with 65 additions and 48 deletions

View File

@ -85,7 +85,12 @@ namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// The delegate that is run when a context menu item is selected.
/// </summary>
public delegate void ContextMenuItemSelectedDelegate(ContextMenuItemSelectedArgsContainer args);
public delegate void ContextMenuItemSelectedDelegate(ContextMenuItemSelectedArgs args);
/// <summary>
/// The delegate that is run when an inventory context menu item is selected.
/// </summary>
public delegate void InventoryContextMenuItemSelectedDelegate(InventoryContextMenuItemSelectedArgs args);
private unsafe delegate byte ContextMenuOpenDelegate(IntPtr addon, int menuSize, AtkValue* atkValueArgs);
@ -302,7 +307,14 @@ namespace XivCommon.Functions.ContextMenu {
this._atkValueChangeType(newItem, ValueType.String);
var name = item switch {
ContextMenuItem custom => this.Language switch {
NormalContextMenuItem custom => this.Language switch {
ClientLanguage.Japanese => custom.NameJapanese,
ClientLanguage.English => custom.NameEnglish,
ClientLanguage.German => custom.NameGerman,
ClientLanguage.French => custom.NameFrench,
_ => custom.NameEnglish,
},
InventoryContextMenuItem custom => this.Language switch {
ClientLanguage.Japanese => custom.NameJapanese,
ClientLanguage.English => custom.NameEnglish,
ClientLanguage.German => custom.NameGerman,
@ -338,36 +350,42 @@ namespace XivCommon.Functions.ContextMenu {
var item = this.Items[index];
// a custom item is being clicked
if (item is ContextMenuItem custom) {
var (inventory, agent) = this.GetContextMenuAgent();
if (item is CustomContextMenuItem<ContextMenuItemSelectedDelegate> custom) {
var (_, agent) = this.GetContextMenuAgent();
var addonName = this.GetParentAddonName(addon);
var info = GetAgentInfo(agent);
ContextMenuItemSelectedArgsContainer container;
if (inventory) {
var info = GetInventoryAgentInfo(agent);
container = new ContextMenuItemSelectedArgsContainer(new InventoryContextMenuItemSelectedArgs(
addon,
agent,
addonName,
info.itemId,
info.itemAmount,
info.itemHq
));
} else {
var info = GetAgentInfo(agent);
container = new ContextMenuItemSelectedArgsContainer(new ContextMenuItemSelectedArgs(
addon,
agent,
addonName,
info.actorId,
info.contentIdLower,
info.text,
info.actorWorld
));
}
var args = new ContextMenuItemSelectedArgs(
addon,
agent,
addonName,
info.actorId,
info.contentIdLower,
info.text,
info.actorWorld
);
try {
custom.Action(container);
custom.Action(args);
} catch (Exception ex) {
Logger.LogError(ex, "Exception in custom context menu item");
}
} else if (item is CustomContextMenuItem<InventoryContextMenuItemSelectedDelegate> invCustom) {
var (_, agent) = this.GetContextMenuAgent();
var addonName = this.GetParentAddonName(addon);
var info = GetInventoryAgentInfo(agent);
var args = new InventoryContextMenuItemSelectedArgs(
addon,
agent,
addonName,
info.itemId,
info.itemAmount,
info.itemHq
);
try {
invCustom.Action(args);
} catch (Exception ex) {
Logger.LogError(ex, "Exception in custom context menu item");
}

View File

@ -1,16 +0,0 @@
using XivCommon.Functions.ContextMenu.Inventory;
namespace XivCommon.Functions.ContextMenu {
public class ContextMenuItemSelectedArgsContainer {
public ContextMenuItemSelectedArgs? ItemSelectedArgs { get; }
public InventoryContextMenuItemSelectedArgs? InventoryItemSelectedArgs { get; }
internal ContextMenuItemSelectedArgsContainer(ContextMenuItemSelectedArgs args) {
this.ItemSelectedArgs = args;
}
internal ContextMenuItemSelectedArgsContainer(InventoryContextMenuItemSelectedArgs args) {
this.InventoryItemSelectedArgs = args;
}
}
}

View File

@ -1,8 +1,11 @@
namespace XivCommon.Functions.ContextMenu {
using System;
namespace XivCommon.Functions.ContextMenu {
/// <summary>
/// A custom context menu item
/// </summary>
public class ContextMenuItem : BaseContextMenuItem {
public abstract class CustomContextMenuItem<T> : BaseContextMenuItem
where T : Delegate {
/// <summary>
/// The name of the context item to be shown for English clients.
/// </summary>
@ -26,14 +29,14 @@
/// <summary>
/// The action to perform when this item is clicked.
/// </summary>
public ContextMenu.ContextMenuItemSelectedDelegate Action { get; set; }
public T Action { get; set; }
/// <summary>
/// Create a new context menu item.
/// </summary>
/// <param name="name">the English name of the item, copied to other languages</param>
/// <param name="action">the action to perform on click</param>
public ContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) {
public CustomContextMenuItem(string name, T action) {
this.NameEnglish = name;
this.NameJapanese = name;
this.NameFrench = name;

View File

@ -0,0 +1,6 @@
namespace XivCommon.Functions.ContextMenu.Inventory {
public class InventoryContextMenuItem : CustomContextMenuItem<ContextMenu.InventoryContextMenuItemSelectedDelegate> {
public InventoryContextMenuItem(string name, ContextMenu.InventoryContextMenuItemSelectedDelegate action) : base(name, action) {
}
}
}

View File

@ -0,0 +1,6 @@
namespace XivCommon.Functions.ContextMenu {
public class NormalContextMenuItem : CustomContextMenuItem<ContextMenu.ContextMenuItemSelectedDelegate> {
public NormalContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) : base(name, action) {
}
}
}