Add context menu integration
This commit is contained in:
parent
9d255fea92
commit
158eda6d7f
@ -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>
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
75
ARDiscard/ContextMenuIntegration.cs
Normal file
75
ARDiscard/ContextMenuIntegration.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
@ -306,6 +329,16 @@ public sealed class ConfigWindow : Window
|
||||
if (string.IsNullOrEmpty(_itemName))
|
||||
_searchResults = new();
|
||||
else
|
||||
{
|
||||
_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)
|
||||
{
|
||||
@ -317,11 +350,7 @@ public sealed class ConfigWindow : Window
|
||||
.ToList();
|
||||
}
|
||||
|
||||
_searchResults = _allItems.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();
|
||||
}
|
||||
return _allItems;
|
||||
}
|
||||
|
||||
private void Save()
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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, )",
|
||||
|
Loading…
Reference in New Issue
Block a user