1
0
forked from liza/Deliveroo

Update drag & drop functionality

This commit is contained in:
Liza 2024-08-30 14:23:51 +02:00
parent 26991aa59b
commit 780a0cbdcd
Signed by: liza
GPG Key ID: 7199F8D727D55F67
2 changed files with 82 additions and 26 deletions

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Dalamud.Configuration; using Dalamud.Configuration;
using Dalamud.Game.ClientState.Keys; using Dalamud.Game.ClientState.Keys;
using Dalamud.Game.Text; using Dalamud.Game.Text;
@ -29,6 +31,9 @@ internal sealed class Configuration : IPluginConfiguration
internal sealed class PurchasePriority internal sealed class PurchasePriority
{ {
[JsonIgnore]
public Guid InternalId { get; } = Guid.NewGuid();
public uint ItemId { get; set; } public uint ItemId { get; set; }
public int Limit { get; set; } public int Limit { get; set; }
public bool Enabled { get; set; } = true; public bool Enabled { get; set; } = true;

View File

@ -56,6 +56,7 @@ internal sealed class TurnInWindow : LWindow, IPersistableWindowConfig
private readonly GameFunctions _gameFunctions; private readonly GameFunctions _gameFunctions;
private bool _state; private bool _state;
private Guid? _draggedItem;
public TurnInWindow(DeliverooPlugin plugin, IDalamudPluginInterface pluginInterface, Configuration configuration, public TurnInWindow(DeliverooPlugin plugin, IDalamudPluginInterface pluginInterface, Configuration configuration,
ICondition condition, IClientState clientState, GcRewardsCache gcRewardsCache, ConfigWindow configWindow, ICondition condition, IClientState clientState, GcRewardsCache gcRewardsCache, ConfigWindow configWindow,
@ -359,11 +360,17 @@ internal sealed class TurnInWindow : LWindow, IPersistableWindowConfig
itemsWrapper.Save(); itemsWrapper.Save();
} }
int? itemToRemove = null; Configuration.PurchasePriority? itemToRemove = null;
Configuration.PurchasePriority? itemToAdd = null; Configuration.PurchasePriority? itemToAdd = null;
int indexToAdd = 0; int indexToAdd = 0;
float width = ImGui.GetContentRegionAvail().X;
List<(Vector2 TopLeft, Vector2 BottomRight)> itemPositions = [];
for (int i = 0; i < itemsWrapper.GetItemsToPurchase().Count; ++i) for (int i = 0; i < itemsWrapper.GetItemsToPurchase().Count; ++i)
{ {
Vector2 topLeft = ImGui.GetCursorScreenPos() + new Vector2(0, -ImGui.GetStyle().ItemSpacing.Y / 2);
ImGui.PushID($"ItemToBuy{i}"); ImGui.PushID($"ItemToBuy{i}");
Configuration.PurchasePriority item = itemsWrapper.GetItemsToPurchase()[i]; Configuration.PurchasePriority item = itemsWrapper.GetItemsToPurchase()[i];
@ -437,6 +444,16 @@ internal sealed class TurnInWindow : LWindow, IPersistableWindowConfig
indentX = ImGui.GetCursorPosX() - indentX; indentX = ImGui.GetCursorPosX() - indentX;
ImGui.PushFont(UiBuilder.IconFont);
ImGui.SetNextItemWidth(width -
ImGui.GetStyle().WindowPadding.X -
ImGui.CalcTextSize(FontAwesomeIcon.Circle.ToIconString()).X -
ImGui.CalcTextSize(FontAwesomeIcon.ArrowsUpDown.ToIconString()).X -
ImGui.CalcTextSize(FontAwesomeIcon.Times.ToIconString()).X -
ImGui.GetStyle().FramePadding.X * 8 -
ImGui.GetStyle().ItemSpacing.X * 2 - 3 * 3);
ImGui.PopFont();
if (ImGui.Combo("", ref comboValueIndex, if (ImGui.Combo("", ref comboValueIndex,
comboValues.Select(x => item.CheckRetainerInventory ? x.NameWithRetainers : x.NameWithoutRetainers) comboValues.Select(x => item.CheckRetainerInventory ? x.NameWithRetainers : x.NameWithoutRetainers)
.ToArray(), comboValues.Count)) .ToArray(), comboValues.Count))
@ -450,30 +467,40 @@ internal sealed class TurnInWindow : LWindow, IPersistableWindowConfig
if (itemsWrapper.GetItemsToPurchase().Count >= 2) if (itemsWrapper.GetItemsToPurchase().Count >= 2)
{ {
ImGui.SameLine(); ImGui.PushFont(UiBuilder.IconFont);
if (ImGuiComponents.IconButton($"##Up{i}", FontAwesomeIcon.ArrowUp)) ImGui.SameLine(ImGui.GetContentRegionAvail().X +
{ ImGui.GetStyle().WindowPadding.X -
itemToAdd = item; ImGui.CalcTextSize(FontAwesomeIcon.ArrowsUpDown.ToIconString()).X -
if (i > 0) ImGui.CalcTextSize(FontAwesomeIcon.Times.ToIconString()).X -
indexToAdd = i - 1; ImGui.GetStyle().FramePadding.X * 4 -
else ImGui.GetStyle().ItemSpacing.X);
indexToAdd = itemsWrapper.GetItemsToPurchase().Count - 1; ImGui.PopFont();
}
ImGui.SameLine(0, 0); if (_draggedItem == item.InternalId)
if (ImGuiComponents.IconButton($"##Down{i}", FontAwesomeIcon.ArrowDown))
{ {
itemToAdd = item; ImGuiComponents.IconButton("##Move", FontAwesomeIcon.ArrowsUpDown,
if (i < itemsWrapper.GetItemsToPurchase().Count - 1) ImGui.ColorConvertU32ToFloat4(ImGui.GetColorU32(ImGuiCol.ButtonActive)));
indexToAdd = i + 1;
else
indexToAdd = 0;
} }
else
ImGuiComponents.IconButton("##Move", FontAwesomeIcon.ArrowsUpDown);
if (_draggedItem == null && ImGui.IsItemActive() && ImGui.IsMouseDragging(ImGuiMouseButton.Left))
_draggedItem = item.InternalId;
ImGui.SameLine(); ImGui.SameLine();
}
else
{
ImGui.PushFont(UiBuilder.IconFont);
ImGui.SameLine(ImGui.GetContentRegionAvail().X +
ImGui.GetStyle().WindowPadding.X -
ImGui.CalcTextSize(FontAwesomeIcon.Times.ToIconString()).X -
ImGui.GetStyle().FramePadding.X * 2);
ImGui.PopFont();
}
if (ImGuiComponents.IconButton($"###Remove{i}", FontAwesomeIcon.Times)) if (ImGuiComponents.IconButton($"###Remove{i}", FontAwesomeIcon.Times))
itemToRemove = i; itemToRemove = item;
}
if (enabled) if (enabled)
{ {
@ -516,6 +543,36 @@ internal sealed class TurnInWindow : LWindow, IPersistableWindowConfig
} }
ImGui.PopID(); ImGui.PopID();
Vector2 bottomRight = new Vector2(topLeft.X + width,
ImGui.GetCursorScreenPos().Y - ImGui.GetStyle().ItemSpacing.Y + 2);
itemPositions.Add((topLeft, bottomRight));
}
if (!ImGui.IsMouseDragging(ImGuiMouseButton.Left))
_draggedItem = null;
else if (_draggedItem != null)
{
var items = itemsWrapper.GetItemsToPurchase().ToList();
var draggedItem = items.Single(x => x.InternalId == _draggedItem);
int oldIndex = items.IndexOf(draggedItem);
var (topLeft, bottomRight) = itemPositions[oldIndex];
ImGui.GetWindowDrawList().AddRect(topLeft, bottomRight, ImGui.GetColorU32(ImGuiColors.DalamudGrey), 3f,
ImDrawFlags.RoundCornersAll);
int newIndex = itemPositions.FindIndex(x => ImGui.IsMouseHoveringRect(x.TopLeft, x.BottomRight, true));
if (newIndex >= 0 && oldIndex != newIndex)
{
itemToAdd = items.Single(x => x.InternalId == _draggedItem);
indexToAdd = newIndex;
}
}
if (itemToRemove != null)
{
itemsWrapper.Remove(itemToRemove);
itemsWrapper.Save();
} }
if (itemToAdd != null) if (itemToAdd != null)
@ -525,12 +582,6 @@ internal sealed class TurnInWindow : LWindow, IPersistableWindowConfig
itemsWrapper.Save(); itemsWrapper.Save();
} }
if (itemToRemove != null)
{
itemsWrapper.RemoveAt(itemToRemove.Value);
itemsWrapper.Save();
}
if (_configuration.ItemsAvailableForPurchase.Any(x => if (_configuration.ItemsAvailableForPurchase.Any(x =>
itemsWrapper.GetItemsToPurchase().All(y => x != y.ItemId))) itemsWrapper.GetItemsToPurchase().All(y => x != y.ItemId)))
{ {