Add context menu integration

This commit is contained in:
Liza 2023-09-16 14:42:15 +02:00
parent 9d255fea92
commit e987f504e2
Signed by: liza
GPG Key ID: 7199F8D727D55F67
6 changed files with 155 additions and 13 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<Version>2.1</Version>
<Version>2.2</Version>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
@ -25,6 +25,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dalamud.ContextMenu" Version="1.2.3"/>
<PackageReference Include="DalamudPackager" Version="2.1.11"/>
</ItemGroup>

View File

@ -38,6 +38,7 @@ public class AutoDiscardPlogon : IDalamudPlugin
private readonly InventoryUtils _inventoryUtils;
private readonly AutoRetainerApi _autoRetainerApi;
private readonly TaskManager _taskManager;
private readonly ContextMenuIntegration _contextMenuIntegration;
private DateTime _cancelDiscardAfter = DateTime.MaxValue;
@ -83,6 +84,7 @@ public class AutoDiscardPlogon : IDalamudPlugin
ECommonsMain.Init(_pluginInterface, this);
_autoRetainerApi = new();
_taskManager = new();
_contextMenuIntegration = new(_configuration, _configWindow);
_clientState.Login += _discardWindow.Login;
_clientState.Logout += _discardWindow.Logout;
@ -271,6 +273,7 @@ public class AutoDiscardPlogon : IDalamudPlugin
_clientState.Login -= _discardWindow.Login;
_clientState.Logout -= _discardWindow.Logout;
_contextMenuIntegration.Dispose();
_autoRetainerApi.Dispose();
ECommonsMain.Dispose();

View File

@ -12,6 +12,7 @@ public sealed class Configuration : IPluginConfiguration
public List<CharacterInfo> ExcludedCharacters { get; set; } = new();
public ArmouryConfiguration Armoury { get; set; } = new();
public ContextMenuConfiguration ContextMenu { get; set; } = new();
public sealed class CharacterInfo
{
@ -27,4 +28,10 @@ public sealed class Configuration : IPluginConfiguration
public bool CheckRightSideGear { get; set; } = false;
public int MaximumGearItemLevel { get; set; } = 45;
}
public sealed class ContextMenuConfiguration
{
public bool Enabled { get; set; } = false;
public bool OnlyWhenConfigIsOpen { get; set; } = true;
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Linq;
using ARDiscard.GameData;
using ARDiscard.Windows;
using Dalamud.ContextMenu;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Logging;
namespace ARDiscard;
public sealed class ContextMenuIntegration : IDisposable
{
private readonly Configuration _configuration;
private readonly ConfigWindow _configWindow;
private readonly InventoryContextMenuItem _addItem;
private readonly InventoryContextMenuItem _removeItem;
private readonly DalamudContextMenu _dalamudContextMenu;
public ContextMenuIntegration(Configuration configuration, ConfigWindow configWindow)
{
_configuration = configuration;
_configWindow = configWindow;
_addItem = new InventoryContextMenuItem(
new SeString(new UIForegroundPayload(52))
.Append($"\ue05f ")
.Append(new UIForegroundPayload(0)).Append("Add to Auto Discard List"),
AddToDiscardList);
_removeItem = new InventoryContextMenuItem(
new SeString(new UIForegroundPayload(52))
.Append($"\ue05f ")
.Append(new UIForegroundPayload(0)).Append("Remove from Auto Discard List"),
RemoveFromDiscardList);
_dalamudContextMenu = new();
_dalamudContextMenu.OnOpenInventoryContextMenu += OpenInventoryContextMenu;
}
private void OpenInventoryContextMenu(InventoryContextMenuOpenArgs args)
{
if (!_configuration.ContextMenu.Enabled)
return;
if (_configuration.ContextMenu.OnlyWhenConfigIsOpen && !_configWindow.IsOpen)
return;
if (!(args.ParentAddonName is "Inventory" or "InventoryExpansion" or "InventoryLarge"))
return;
if (!_configWindow.CanItemBeConfigured(args.ItemId))
return;
if (_configuration.DiscardingItems.Contains(args.ItemId))
args.AddCustomItem(_removeItem);
else if (!InternalConfiguration.BlacklistedItems.Contains(args.ItemId))
args.AddCustomItem(_addItem);
}
private void AddToDiscardList(InventoryContextMenuItemSelectedArgs args)
{
_configWindow.AddToDiscardList(args.ItemId);
}
private void RemoveFromDiscardList(InventoryContextMenuItemSelectedArgs args)
{
_configWindow.RemoveFromDiscardList(args.ItemId);
}
public void Dispose()
{
_dalamudContextMenu.OnOpenInventoryContextMenu -= OpenInventoryContextMenu;
_dalamudContextMenu.Dispose();
}
}

View File

@ -297,6 +297,29 @@ public sealed class ConfigWindow : Window
ImGui.Unindent(30);
ImGui.EndDisabled();
ImGui.Separator();
bool contextMenuEnabled = _configuration.ContextMenu.Enabled;
if (ImGui.Checkbox("Inventory context menu integration", ref contextMenuEnabled))
{
_configuration.ContextMenu.Enabled = contextMenuEnabled;
Save();
}
ImGui.BeginDisabled(!contextMenuEnabled);
ImGui.Indent(30);
bool contextMenuOnlyWhenConfigIsOpen = _configuration.ContextMenu.OnlyWhenConfigIsOpen;
if (ImGui.Checkbox("Only add menu entries while config window is open",
ref contextMenuOnlyWhenConfigIsOpen))
{
_configuration.ContextMenu.OnlyWhenConfigIsOpen = contextMenuOnlyWhenConfigIsOpen;
Save();
}
ImGui.Unindent(30);
ImGui.EndDisabled();
ImGui.EndTabItem();
}
}
@ -307,23 +330,29 @@ public sealed class ConfigWindow : Window
_searchResults = new();
else
{
if (_allItems == null)
{
_allItems = _itemCache.AllItems
.Where(x => !x.IsUnique && !x.IsUntradable)
.Where(x => x.UiCategory != UiCategories.Currency && x.UiCategory != UiCategories.Crystals &&
x.UiCategory != UiCategories.Unobtainable)
.Select(x => (x.ItemId, x.Name.ToString()))
.ToList();
}
_searchResults = _allItems.Where(x => x.Name.Contains(_itemName, StringComparison.CurrentCultureIgnoreCase)
|| (uint.TryParse(_itemName, out uint itemId) && x.ItemId == itemId))
_searchResults = EnsureAllItemsLoaded().Where(x =>
x.Name.Contains(_itemName, StringComparison.CurrentCultureIgnoreCase)
|| (uint.TryParse(_itemName, out uint itemId) && x.ItemId == itemId))
.OrderBy(x => _itemName.EqualsIgnoreCase(x.Name) ? string.Empty : x.Name)
.ToList();
}
}
private List<(uint ItemId, string Name)> EnsureAllItemsLoaded()
{
if (_allItems == null)
{
_allItems = _itemCache.AllItems
.Where(x => !x.IsUnique && !x.IsUntradable)
.Where(x => x.UiCategory != UiCategories.Currency && x.UiCategory != UiCategories.Crystals &&
x.UiCategory != UiCategories.Unobtainable)
.Select(x => (x.ItemId, x.Name.ToString()))
.ToList();
}
return _allItems;
}
private void Save()
{
_configuration.DiscardingItems = _discarding.Select(x => x.ItemId).ToList();
@ -331,4 +360,25 @@ public sealed class ConfigWindow : Window
ConfigSaved?.Invoke(this, EventArgs.Empty);
}
internal void AddToDiscardList(uint itemId)
{
var item = EnsureAllItemsLoaded().SingleOrDefault(x => x.ItemId == itemId);
if (item.ItemId != 0)
{
_discarding.Add(item);
Save();
}
}
internal void RemoveFromDiscardList(uint itemId)
{
if (_discarding.RemoveAll(x => x.ItemId == itemId) > 0)
Save();
}
public bool CanItemBeConfigured(uint itemId)
{
return EnsureAllItemsLoaded().SingleOrDefault(x => x.ItemId == itemId).ItemId == itemId;
}
}

View File

@ -2,6 +2,12 @@
"version": 1,
"dependencies": {
"net7.0-windows7.0": {
"Dalamud.ContextMenu": {
"type": "Direct",
"requested": "[1.2.3, )",
"resolved": "1.2.3",
"contentHash": "ydemplF7DNcA/LLeongDVzWUD/JV0Fw3EwA2+P0jYq3Le2ZYSt4U8qyJq4FyoChqt0lFG8BxYCAzfeWp4Jmnqw=="
},
"DalamudPackager": {
"type": "Direct",
"requested": "[2.1.11, )",