diff --git a/Dalamud/Game/AddonEventManager/AddonCursorType.cs b/Dalamud/Game/AddonEventManager/AddonCursorType.cs
new file mode 100644
index 0000000000..8ba3a901bf
--- /dev/null
+++ b/Dalamud/Game/AddonEventManager/AddonCursorType.cs
@@ -0,0 +1,97 @@
+namespace Dalamud.Game.AddonEventManager;
+
+///
+/// Reimplementation of CursorType.
+///
+public enum AddonCursorType
+{
+ ///
+ /// Arrow.
+ ///
+ Arrow,
+
+ ///
+ /// Boot.
+ ///
+ Boot,
+
+ ///
+ /// Search.
+ ///
+ Search,
+
+ ///
+ /// Chat Pointer.
+ ///
+ ChatPointer,
+
+ ///
+ /// Interact.
+ ///
+ Interact,
+
+ ///
+ /// Attack.
+ ///
+ Attack,
+
+ ///
+ /// Hand.
+ ///
+ Hand,
+
+ ///
+ /// Resizeable Left-Right.
+ ///
+ ResizeWE,
+
+ ///
+ /// Resizeable Up-Down.
+ ///
+ ResizeNS,
+
+ ///
+ /// Resizeable.
+ ///
+ ResizeNWSR,
+
+ ///
+ /// Resizeable 4-way.
+ ///
+ ResizeNESW,
+
+ ///
+ /// Clickable.
+ ///
+ Clickable,
+
+ ///
+ /// Text Input.
+ ///
+ TextInput,
+
+ ///
+ /// Text Click.
+ ///
+ TextClick,
+
+ ///
+ /// Grab.
+ ///
+ Grab,
+
+ ///
+ /// Chat Bubble.
+ ///
+ ChatBubble,
+
+ ///
+ /// No Access.
+ ///
+ NoAccess,
+
+ ///
+ /// Hidden.
+ ///
+ Hidden,
+}
diff --git a/Dalamud/Game/AddonEventManager/AddonEventListener.cs b/Dalamud/Game/AddonEventManager/AddonEventListener.cs
new file mode 100644
index 0000000000..cb0aa1502c
--- /dev/null
+++ b/Dalamud/Game/AddonEventManager/AddonEventListener.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Runtime.InteropServices;
+
+using FFXIVClientStructs.FFXIV.Component.GUI;
+
+namespace Dalamud.Game.AddonEventManager;
+
+///
+/// Event listener class for managing custom events.
+///
+// Custom event handler tech provided by Pohky, implemented by MidoriKami
+internal unsafe class AddonEventListener : IDisposable
+{
+ private ReceiveEventDelegate? receiveEventDelegate;
+
+ private AtkEventListener* eventListener;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The managed handler to send events to.
+ public AddonEventListener(ReceiveEventDelegate eventHandler)
+ {
+ this.receiveEventDelegate = eventHandler;
+
+ this.eventListener = (AtkEventListener*)Marshal.AllocHGlobal(sizeof(AtkEventListener));
+ this.eventListener->vtbl = (void*)Marshal.AllocHGlobal(sizeof(void*) * 3);
+ this.eventListener->vfunc[0] = (delegate* unmanaged)&NullSub;
+ this.eventListener->vfunc[1] = (delegate* unmanaged)&NullSub;
+ this.eventListener->vfunc[2] = (void*)Marshal.GetFunctionPointerForDelegate(this.receiveEventDelegate);
+ }
+
+ ///
+ /// Delegate for receiving custom events.
+ ///
+ /// Pointer to the event listener.
+ /// Event type.
+ /// Unique Id for this event.
+ /// Event Data.
+ /// Unknown Parameter.
+ public delegate void ReceiveEventDelegate(AtkEventListener* self, AtkEventType eventType, uint eventParam, AtkEvent* eventData, nint unknown);
+
+ ///
+ public void Dispose()
+ {
+ if (this.eventListener is null) return;
+
+ Marshal.FreeHGlobal((nint)this.eventListener->vtbl);
+ Marshal.FreeHGlobal((nint)this.eventListener);
+
+ this.eventListener = null;
+ this.receiveEventDelegate = null;
+ }
+
+ ///
+ /// Register an event to this event handler.
+ ///
+ /// Addon that triggers this event.
+ /// Node to attach event to.
+ /// Event type to trigger this event.
+ /// Unique id for this event.
+ public void RegisterEvent(AtkUnitBase* addon, AtkResNode* node, AtkEventType eventType, uint param)
+ {
+ if (node is null) return;
+
+ node->AddEvent(eventType, param, this.eventListener, (AtkResNode*)addon, false);
+ }
+
+ ///
+ /// Unregister an event from this event handler.
+ ///
+ /// Node to remove the event from.
+ /// Event type that this event is for.
+ /// Unique id for this event.
+ public void UnregisterEvent(AtkResNode* node, AtkEventType eventType, uint param)
+ {
+ if (node is null) return;
+
+ node->RemoveEvent(eventType, param, this.eventListener, false);
+ }
+
+ [UnmanagedCallersOnly]
+ private static void NullSub()
+ {
+ /* do nothing */
+ }
+}
diff --git a/Dalamud/Game/AddonEventManager/AddonEventManager.cs b/Dalamud/Game/AddonEventManager/AddonEventManager.cs
new file mode 100644
index 0000000000..4718d48003
--- /dev/null
+++ b/Dalamud/Game/AddonEventManager/AddonEventManager.cs
@@ -0,0 +1,253 @@
+using System;
+using System.Collections.Generic;
+
+using Dalamud.Hooking;
+using Dalamud.IoC;
+using Dalamud.IoC.Internal;
+using Dalamud.Logging.Internal;
+using Dalamud.Plugin.Services;
+using FFXIVClientStructs.FFXIV.Client.UI;
+using FFXIVClientStructs.FFXIV.Component.GUI;
+
+namespace Dalamud.Game.AddonEventManager;
+
+///
+/// Service provider for addon event management.
+///
+[InterfaceVersion("1.0")]
+[ServiceManager.EarlyLoadedService]
+internal unsafe class AddonEventManager : IDisposable, IServiceType, IAddonEventManager
+{
+ private static readonly ModuleLog Log = new("AddonEventManager");
+
+ private readonly AddonEventManagerAddressResolver address;
+ private readonly Hook onUpdateCursor;
+
+ private readonly AddonEventListener eventListener;
+ private readonly Dictionary eventHandlers;
+
+ private AddonCursorType? cursorOverride;
+
+ [ServiceManager.ServiceConstructor]
+ private AddonEventManager(SigScanner sigScanner)
+ {
+ this.address = new AddonEventManagerAddressResolver();
+ this.address.Setup(sigScanner);
+
+ this.eventHandlers = new Dictionary();
+ this.eventListener = new AddonEventListener(this.DalamudAddonEventHandler);
+
+ this.cursorOverride = null;
+
+ this.onUpdateCursor = Hook.FromAddress(this.address.UpdateCursor, this.UpdateCursorDetour);
+ }
+
+ private delegate nint UpdateCursorDelegate(RaptureAtkModule* module);
+
+ ///
+ public void AddEvent(uint eventId, IntPtr atkUnitBase, IntPtr atkResNode, AddonEventType eventType, IAddonEventManager.AddonEventHandler eventHandler)
+ {
+ if (!this.eventHandlers.ContainsKey(eventId))
+ {
+ var type = (AtkEventType)eventType;
+ var node = (AtkResNode*)atkResNode;
+ var addon = (AtkUnitBase*)atkUnitBase;
+
+ this.eventHandlers.Add(eventId, eventHandler);
+ this.eventListener.RegisterEvent(addon, node, type, eventId);
+ }
+ else
+ {
+ Log.Warning($"Attempted to register already registered eventId: {eventId}");
+ }
+ }
+
+ ///
+ public void RemoveEvent(uint eventId, IntPtr atkResNode, AddonEventType eventType)
+ {
+ if (this.eventHandlers.ContainsKey(eventId))
+ {
+ var type = (AtkEventType)eventType;
+ var node = (AtkResNode*)atkResNode;
+
+ this.eventListener.UnregisterEvent(node, type, eventId);
+ this.eventHandlers.Remove(eventId);
+ }
+ else
+ {
+ Log.Warning($"Attempted to unregister already unregistered eventId: {eventId}");
+ }
+ }
+
+ ///
+ public void Dispose()
+ {
+ this.onUpdateCursor.Dispose();
+ this.eventListener.Dispose();
+ this.eventHandlers.Clear();
+ }
+
+ ///
+ public void SetCursor(AddonCursorType cursor) => this.cursorOverride = cursor;
+
+ ///
+ public void ResetCursor() => this.cursorOverride = null;
+
+ [ServiceManager.CallWhenServicesReady]
+ private void ContinueConstruction()
+ {
+ this.onUpdateCursor.Enable();
+ }
+
+ private nint UpdateCursorDetour(RaptureAtkModule* module)
+ {
+ try
+ {
+ var atkStage = AtkStage.GetSingleton();
+
+ if (this.cursorOverride is not null && atkStage is not null)
+ {
+ var cursor = (AddonCursorType)atkStage->AtkCursor.Type;
+ if (cursor != this.cursorOverride)
+ {
+ AtkStage.GetSingleton()->AtkCursor.SetCursorType((AtkCursor.CursorType)this.cursorOverride, 1);
+ }
+
+ return nint.Zero;
+ }
+ }
+ catch (Exception e)
+ {
+ Log.Error(e, "Exception in UpdateCursorDetour.");
+ }
+
+ return this.onUpdateCursor!.Original(module);
+ }
+
+ private void DalamudAddonEventHandler(AtkEventListener* self, AtkEventType eventType, uint eventParam, AtkEvent* eventData, IntPtr unknown)
+ {
+ if (this.eventHandlers.TryGetValue(eventParam, out var handler) && eventData is not null)
+ {
+ try
+ {
+ // We passed the AtkUnitBase into the EventData.Node field from our AddonEventHandler
+ handler?.Invoke((AddonEventType)eventType, (nint)eventData->Node, (nint)eventData->Target);
+ }
+ catch (Exception exception)
+ {
+ Log.Error(exception, "Exception in DalamudAddonEventHandler custom event invoke.");
+ }
+ }
+ }
+}
+
+///
+/// Plugin-scoped version of a AddonEventManager service.
+///
+[PluginInterface]
+[InterfaceVersion("1.0")]
+[ServiceManager.ScopedService]
+#pragma warning disable SA1015
+[ResolveVia]
+#pragma warning restore SA1015
+internal unsafe class AddonEventManagerPluginScoped : IDisposable, IServiceType, IAddonEventManager
+{
+ private static readonly ModuleLog Log = new("AddonEventManager");
+
+ [ServiceManager.ServiceDependency]
+ private readonly AddonEventManager baseEventManager = Service.Get();
+
+ private readonly AddonEventListener eventListener;
+ private readonly Dictionary eventHandlers;
+
+ private bool isForcingCursor;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public AddonEventManagerPluginScoped()
+ {
+ this.eventHandlers = new Dictionary();
+ this.eventListener = new AddonEventListener(this.PluginAddonEventHandler);
+ }
+
+ ///
+ public void Dispose()
+ {
+ // if multiple plugins force cursors and dispose without un-forcing them then all forces will be cleared.
+ if (this.isForcingCursor)
+ {
+ this.baseEventManager.ResetCursor();
+ }
+
+ this.eventListener.Dispose();
+ this.eventHandlers.Clear();
+ }
+
+ ///
+ public void AddEvent(uint eventId, IntPtr atkUnitBase, IntPtr atkResNode, AddonEventType eventType, IAddonEventManager.AddonEventHandler eventHandler)
+ {
+ if (!this.eventHandlers.ContainsKey(eventId))
+ {
+ var type = (AtkEventType)eventType;
+ var node = (AtkResNode*)atkResNode;
+ var addon = (AtkUnitBase*)atkUnitBase;
+
+ this.eventHandlers.Add(eventId, eventHandler);
+ this.eventListener.RegisterEvent(addon, node, type, eventId);
+ }
+ else
+ {
+ Log.Warning($"Attempted to register already registered eventId: {eventId}");
+ }
+ }
+
+ ///
+ public void RemoveEvent(uint eventId, IntPtr atkResNode, AddonEventType eventType)
+ {
+ if (this.eventHandlers.ContainsKey(eventId))
+ {
+ var type = (AtkEventType)eventType;
+ var node = (AtkResNode*)atkResNode;
+
+ this.eventListener.UnregisterEvent(node, type, eventId);
+ this.eventHandlers.Remove(eventId);
+ }
+ else
+ {
+ Log.Warning($"Attempted to unregister already unregistered eventId: {eventId}");
+ }
+ }
+
+ ///
+ public void SetCursor(AddonCursorType cursor)
+ {
+ this.isForcingCursor = true;
+
+ this.baseEventManager.SetCursor(cursor);
+ }
+
+ ///
+ public void ResetCursor()
+ {
+ this.isForcingCursor = false;
+
+ this.baseEventManager.ResetCursor();
+ }
+
+ private void PluginAddonEventHandler(AtkEventListener* self, AtkEventType eventType, uint eventParam, AtkEvent* eventData, IntPtr unknown)
+ {
+ if (this.eventHandlers.TryGetValue(eventParam, out var handler) && eventData is not null)
+ {
+ try
+ {
+ // We passed the AtkUnitBase into the EventData.Node field from our AddonEventHandler
+ handler?.Invoke((AddonEventType)eventType, (nint)eventData->Node, (nint)eventData->Target);
+ }
+ catch (Exception exception)
+ {
+ Log.Error(exception, "Exception in PluginAddonEventHandler custom event invoke.");
+ }
+ }
+ }
+}
diff --git a/Dalamud/Game/AddonEventManager/AddonEventManagerAddressResolver.cs b/Dalamud/Game/AddonEventManager/AddonEventManagerAddressResolver.cs
new file mode 100644
index 0000000000..ba1c07db81
--- /dev/null
+++ b/Dalamud/Game/AddonEventManager/AddonEventManagerAddressResolver.cs
@@ -0,0 +1,21 @@
+namespace Dalamud.Game.AddonEventManager;
+
+///
+/// AddonEventManager memory address resolver.
+///
+internal class AddonEventManagerAddressResolver : BaseAddressResolver
+{
+ ///
+ /// Gets the address of the AtkModule UpdateCursor method.
+ ///
+ public nint UpdateCursor { get; private set; }
+
+ ///
+ /// Scan for and setup any configured address pointers.
+ ///
+ /// The signature scanner to facilitate setup.
+ protected override void Setup64Bit(SigScanner scanner)
+ {
+ this.UpdateCursor = scanner.ScanText("48 89 74 24 ?? 48 89 7C 24 ?? 41 56 48 83 EC 20 4C 8B F1 E8 ?? ?? ?? ?? 49 8B CE");
+ }
+}
diff --git a/Dalamud/Game/AddonEventManager/AddonEventType.cs b/Dalamud/Game/AddonEventManager/AddonEventType.cs
new file mode 100644
index 0000000000..eef9763ff7
--- /dev/null
+++ b/Dalamud/Game/AddonEventManager/AddonEventType.cs
@@ -0,0 +1,132 @@
+namespace Dalamud.Game.AddonEventManager;
+
+///
+/// Reimplementation of AtkEventType.
+///
+public enum AddonEventType : byte
+{
+ ///
+ /// Mouse Down.
+ ///
+ MouseDown = 3,
+
+ ///
+ /// Mouse Up.
+ ///
+ MouseUp = 4,
+
+ ///
+ /// Mouse Move.
+ ///
+ MouseMove = 5,
+
+ ///
+ /// Mouse Over.
+ ///
+ MouseOver = 6,
+
+ ///
+ /// Mouse Out.
+ ///
+ MouseOut = 7,
+
+ ///
+ /// Mouse Click.
+ ///
+ MouseClick = 9,
+
+ ///
+ /// Input Received.
+ ///
+ InputReceived = 12,
+
+ ///
+ /// Focus Start.
+ ///
+ FocusStart = 18,
+
+ ///
+ /// Focus Stop.
+ ///
+ FocusStop = 19,
+
+ ///
+ /// Button Press, sent on MouseDown on Button.
+ ///
+ ButtonPress = 23,
+
+ ///
+ /// Button Release, sent on MouseUp and MouseOut.
+ ///
+ ButtonRelease = 24,
+
+ ///
+ /// Button Click, sent on MouseUp and MouseClick on button.
+ ///
+ ButtonClick = 25,
+
+ ///
+ /// List Item RollOver.
+ ///
+ ListItemRollOver = 33,
+
+ ///
+ /// List Item Roll Out.
+ ///
+ ListItemRollOut = 34,
+
+ ///
+ /// List Item Toggle.
+ ///
+ ListItemToggle = 35,
+
+ ///
+ /// Drag Drop Roll Over.
+ ///
+ DragDropRollOver = 52,
+
+ ///
+ /// Drag Drop Roll Out.
+ ///
+ DragDropRollOut = 53,
+
+ ///
+ /// Drag Drop Unknown.
+ ///
+ DragDropUnk54 = 54,
+
+ ///
+ /// Drag Drop Unknown.
+ ///
+ DragDropUnk55 = 55,
+
+ ///
+ /// Icon Text Roll Over.
+ ///
+ IconTextRollOver = 56,
+
+ ///
+ /// Icon Text Roll Out.
+ ///
+ IconTextRollOut = 57,
+
+ ///
+ /// Icon Text Click.
+ ///
+ IconTextClick = 58,
+
+ ///
+ /// Window Roll Over.
+ ///
+ WindowRollOver = 67,
+
+ ///
+ /// Window Roll Out.
+ ///
+ WindowRollOut = 68,
+
+ ///
+ /// Window Change Scale.
+ ///
+ WindowChangeScale = 69,
+}
diff --git a/Dalamud/Game/Gui/Dtr/DtrBar.cs b/Dalamud/Game/Gui/Dtr/DtrBar.cs
index dd1e7aa30e..ae01d48863 100644
--- a/Dalamud/Game/Gui/Dtr/DtrBar.cs
+++ b/Dalamud/Game/Gui/Dtr/DtrBar.cs
@@ -1,15 +1,22 @@
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Dalamud.Configuration.Internal;
+using Dalamud.Game.AddonEventManager;
using Dalamud.Game.Text.SeStringHandling;
+using Dalamud.Hooking;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
+using Dalamud.Logging.Internal;
+using Dalamud.Memory;
using Dalamud.Plugin.Services;
+using FFXIVClientStructs.FFXIV.Client.Graphics;
using FFXIVClientStructs.FFXIV.Client.System.Memory;
using FFXIVClientStructs.FFXIV.Component.GUI;
-using Serilog;
+
+using DalamudAddonEventManager = Dalamud.Game.AddonEventManager.AddonEventManager;
namespace Dalamud.Game.Gui.Dtr;
@@ -19,13 +26,15 @@ namespace Dalamud.Game.Gui.Dtr;
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
-#pragma warning disable SA1015
-[ResolveVia]
-#pragma warning restore SA1015
public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar
{
private const uint BaseNodeId = 1000;
+ private const uint MouseOverEventIdOffset = 10000;
+ private const uint MouseOutEventIdOffset = 20000;
+ private const uint MouseClickEventIdOffset = 30000;
+ private static readonly ModuleLog Log = new("DtrBar");
+
[ServiceManager.ServiceDependency]
private readonly Framework framework = Service.Get();
@@ -35,12 +44,25 @@ public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar
[ServiceManager.ServiceDependency]
private readonly DalamudConfiguration configuration = Service.Get();
- private List entries = new();
+ [ServiceManager.ServiceDependency]
+ private readonly DalamudAddonEventManager uiEventManager = Service.Get();
+
+ private readonly DtrBarAddressResolver address;
+ private readonly ConcurrentBag newEntries = new();
+ private readonly List entries = new();
+ private readonly Hook onAddonDrawHook;
+ private readonly Hook onAddonRequestedUpdateHook;
private uint runningNodeIds = BaseNodeId;
[ServiceManager.ServiceConstructor]
- private DtrBar()
+ private DtrBar(SigScanner sigScanner)
{
+ this.address = new DtrBarAddressResolver();
+ this.address.Setup(sigScanner);
+
+ this.onAddonDrawHook = Hook.FromAddress(this.address.AtkUnitBaseDraw, this.OnAddonDrawDetour);
+ this.onAddonRequestedUpdateHook = Hook.FromAddress(this.address.AddonRequestedUpdate, this.OnAddonRequestedUpdateDetour);
+
this.framework.Update += this.Update;
this.configuration.DtrOrder ??= new List();
@@ -48,28 +70,43 @@ private DtrBar()
this.configuration.QueueSave();
}
+ private delegate void AddonDrawDelegate(AtkUnitBase* addon);
+
+ private delegate void AddonRequestedUpdateDelegate(AtkUnitBase* addon, NumberArrayData** numberArrayData, StringArrayData** stringArrayData);
+
///
public DtrBarEntry Get(string title, SeString? text = null)
{
- if (this.entries.Any(x => x.Title == title))
+ if (this.entries.Any(x => x.Title == title) || this.newEntries.Any(x => x.Title == title))
throw new ArgumentException("An entry with the same title already exists.");
- var node = this.MakeNode(++this.runningNodeIds);
- var entry = new DtrBarEntry(title, node);
+ var entry = new DtrBarEntry(title, null);
entry.Text = text;
// Add the entry to the end of the order list, if it's not there already.
if (!this.configuration.DtrOrder!.Contains(title))
this.configuration.DtrOrder!.Add(title);
- this.entries.Add(entry);
- this.ApplySort();
+
+ this.newEntries.Add(entry);
return entry;
}
+
+ ///
+ public void Remove(string title)
+ {
+ if (this.entries.FirstOrDefault(entry => entry.Title == title) is { } dtrBarEntry)
+ {
+ dtrBarEntry.Remove();
+ }
+ }
///
void IDisposable.Dispose()
{
+ this.onAddonDrawHook.Dispose();
+ this.onAddonRequestedUpdateHook.Dispose();
+
foreach (var entry in this.entries)
this.RemoveNode(entry.TextNode);
@@ -130,12 +167,20 @@ internal void ApplySort()
return xPos.CompareTo(yPos);
});
}
+
+ [ServiceManager.CallWhenServicesReady]
+ private void ContinueConstruction()
+ {
+ this.onAddonDrawHook.Enable();
+ this.onAddonRequestedUpdateHook.Enable();
+ }
private AtkUnitBase* GetDtr() => (AtkUnitBase*)this.gameGui.GetAddonByName("_DTR").ToPointer();
private void Update(Framework unused)
{
this.HandleRemovedNodes();
+ this.HandleAddedNodes();
var dtr = this.GetDtr();
if (dtr == null) return;
@@ -148,7 +193,7 @@ private void Update(Framework unused)
if (!this.CheckForDalamudNodes())
this.RecreateNodes();
- var collisionNode = dtr->UldManager.NodeList[1];
+ var collisionNode = dtr->GetNodeById(17);
if (collisionNode == null) return;
// If we are drawing backwards, we should start from the right side of the collision node. That is,
@@ -157,28 +202,24 @@ private void Update(Framework unused)
? collisionNode->X + collisionNode->Width
: collisionNode->X;
- for (var i = 0; i < this.entries.Count; i++)
+ foreach (var data in this.entries)
{
- var data = this.entries[i];
var isHide = this.configuration.DtrIgnore!.Any(x => x == data.Title) || !data.Shown;
- if (data.Dirty && data.Added && data.Text != null && data.TextNode != null)
+ if (data is { Dirty: true, Added: true, Text: not null, TextNode: not null })
{
var node = data.TextNode;
- node->SetText(data.Text?.Encode());
+ node->SetText(data.Text.Encode());
ushort w = 0, h = 0;
- if (isHide)
- {
- node->AtkResNode.ToggleVisibility(false);
- }
- else
+ if (!isHide)
{
- node->AtkResNode.ToggleVisibility(true);
node->GetTextDrawSize(&w, &h, node->NodeText.StringPtr);
node->AtkResNode.SetWidth(w);
}
+ node->AtkResNode.ToggleVisibility(!isHide);
+
data.Dirty = false;
}
@@ -202,8 +243,91 @@ private void Update(Framework unused)
data.TextNode->AtkResNode.SetPositionFloat(runningXPos, 2);
}
}
+ }
+ }
- this.entries[i] = data;
+ private void HandleAddedNodes()
+ {
+ if (this.newEntries.Any())
+ {
+ foreach (var newEntry in this.newEntries)
+ {
+ newEntry.TextNode = this.MakeNode(++this.runningNodeIds);
+ this.entries.Add(newEntry);
+ }
+
+ this.newEntries.Clear();
+ this.ApplySort();
+ }
+ }
+
+ // This hooks all AtkUnitBase.Draw calls, then checks for our specific addon name.
+ // AddonDtr doesn't implement it's own Draw method, would need to replace vtable entry to be more efficient.
+ private void OnAddonDrawDetour(AtkUnitBase* addon)
+ {
+ this.onAddonDrawHook!.Original(addon);
+
+ try
+ {
+ if (MemoryHelper.ReadString((nint)addon->Name, 0x20) is not "_DTR") return;
+
+ this.UpdateNodePositions(addon);
+
+ if (!this.configuration.DtrSwapDirection)
+ {
+ var targetSize = (ushort)this.CalculateTotalSize();
+ var sizeDelta = targetSize - addon->RootNode->Width;
+
+ if (addon->RootNode->Width != targetSize)
+ {
+ addon->RootNode->SetWidth(targetSize);
+ addon->SetX((short)(addon->GetX() - sizeDelta));
+
+ // force a RequestedUpdate immediately to force the game to right-justify it immediately.
+ addon->OnUpdate(AtkStage.GetSingleton()->GetNumberArrayData(), AtkStage.GetSingleton()->GetStringArrayData());
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ Log.Error(e, "Exception in OnAddonDraw.");
+ }
+ }
+
+ private void UpdateNodePositions(AtkUnitBase* addon)
+ {
+ // If we grow to the right, we need to left-justify the original elements.
+ // else if we grow to the left, the game right-justifies it for us.
+ if (this.configuration.DtrSwapDirection)
+ {
+ var targetSize = (ushort)this.CalculateTotalSize();
+ addon->RootNode->SetWidth(targetSize);
+ var sizeOffset = addon->GetNodeById(17)->GetX();
+
+ var node = addon->RootNode->ChildNode;
+ while (node is not null)
+ {
+ if (node->NodeID < 1000 && node->IsVisible)
+ {
+ node->SetX(node->GetX() - sizeOffset);
+ }
+
+ node = node->PrevSiblingNode;
+ }
+ }
+ }
+
+ private void OnAddonRequestedUpdateDetour(AtkUnitBase* addon, NumberArrayData** numberArrayData, StringArrayData** stringArrayData)
+ {
+ this.onAddonRequestedUpdateHook.Original(addon, numberArrayData, stringArrayData);
+
+ try
+ {
+ this.UpdateNodePositions(addon);
+ }
+ catch (Exception e)
+ {
+ Log.Error(e, "Exception in OnAddonRequestedUpdate.");
}
}
@@ -235,11 +359,37 @@ private void RecreateNodes()
}
}
+ // Calculates the total width the dtr bar should be
+ private float CalculateTotalSize()
+ {
+ var addon = this.GetDtr();
+ if (addon is null || addon->RootNode is null || addon->UldManager.NodeList is null) return 0;
+
+ var totalSize = 0.0f;
+
+ foreach (var index in Enumerable.Range(0, addon->UldManager.NodeListCount))
+ {
+ var node = addon->UldManager.NodeList[index];
+
+ // Node 17 is the default CollisionNode that fits over the existing elements
+ if (node->NodeID is 17) totalSize += node->Width;
+
+ // Node > 1000, are our custom nodes
+ if (node->NodeID is > 1000 && node->IsVisible) totalSize += node->Width + this.configuration.DtrSpacing;
+ }
+
+ return totalSize;
+ }
+
private bool AddNode(AtkTextNode* node)
{
var dtr = this.GetDtr();
if (dtr == null || dtr->RootNode == null || dtr->UldManager.NodeList == null || node == null) return false;
+ this.uiEventManager.AddEvent(node->AtkResNode.NodeID + MouseOverEventIdOffset, (nint)dtr, (nint)node, AddonEventType.MouseOver, this.DtrEventHandler);
+ this.uiEventManager.AddEvent(node->AtkResNode.NodeID + MouseOutEventIdOffset, (nint)dtr, (nint)node, AddonEventType.MouseOut, this.DtrEventHandler);
+ this.uiEventManager.AddEvent(node->AtkResNode.NodeID + MouseClickEventIdOffset, (nint)dtr, (nint)node, AddonEventType.MouseClick, this.DtrEventHandler);
+
var lastChild = dtr->RootNode->ChildNode;
while (lastChild->PrevSiblingNode != null) lastChild = lastChild->PrevSiblingNode;
Log.Debug($"Found last sibling: {(ulong)lastChild:X}");
@@ -251,6 +401,7 @@ private bool AddNode(AtkTextNode* node)
Log.Debug("Set last sibling of DTR and updated child count");
dtr->UldManager.UpdateDrawNodeList();
+ dtr->UpdateCollisionNodeList(false);
Log.Debug("Updated node draw list");
return true;
}
@@ -260,6 +411,10 @@ private bool RemoveNode(AtkTextNode* node)
var dtr = this.GetDtr();
if (dtr == null || dtr->RootNode == null || dtr->UldManager.NodeList == null || node == null) return false;
+ this.uiEventManager.RemoveEvent(node->AtkResNode.NodeID + MouseOverEventIdOffset, (nint)node, AddonEventType.MouseOver);
+ this.uiEventManager.RemoveEvent(node->AtkResNode.NodeID + MouseOutEventIdOffset, (nint)node, AddonEventType.MouseOut);
+ this.uiEventManager.RemoveEvent(node->AtkResNode.NodeID + MouseClickEventIdOffset, (nint)node, AddonEventType.MouseClick);
+
var tmpPrevNode = node->AtkResNode.PrevSiblingNode;
var tmpNextNode = node->AtkResNode.NextSiblingNode;
@@ -272,25 +427,23 @@ private bool RemoveNode(AtkTextNode* node)
dtr->RootNode->ChildCount = (ushort)(dtr->RootNode->ChildCount - 1);
Log.Debug("Set last sibling of DTR and updated child count");
dtr->UldManager.UpdateDrawNodeList();
+ dtr->UpdateCollisionNodeList(false);
Log.Debug("Updated node draw list");
return true;
}
private AtkTextNode* MakeNode(uint nodeId)
{
- var newTextNode = (AtkTextNode*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkTextNode), 8);
+ var newTextNode = IMemorySpace.GetUISpace()->Create();
if (newTextNode == null)
{
- Log.Debug("Failed to allocate memory for text node");
+ Log.Debug("Failed to allocate memory for AtkTextNode");
return null;
}
- IMemorySpace.Memset(newTextNode, 0, (ulong)sizeof(AtkTextNode));
- newTextNode->Ctor();
-
newTextNode->AtkResNode.NodeID = nodeId;
newTextNode->AtkResNode.Type = NodeType.Text;
- newTextNode->AtkResNode.NodeFlags = NodeFlags.AnchorLeft | NodeFlags.AnchorTop;
+ newTextNode->AtkResNode.NodeFlags = NodeFlags.AnchorLeft | NodeFlags.AnchorTop | NodeFlags.Enabled | NodeFlags.RespondToMouse | NodeFlags.HasCollision | NodeFlags.EmitsEvents;
newTextNode->AtkResNode.DrawFlags = 12;
newTextNode->AtkResNode.SetWidth(22);
newTextNode->AtkResNode.SetHeight(22);
@@ -304,16 +457,96 @@ private bool RemoveNode(AtkTextNode* node)
newTextNode->SetText(" ");
- newTextNode->TextColor.R = 255;
- newTextNode->TextColor.G = 255;
- newTextNode->TextColor.B = 255;
- newTextNode->TextColor.A = 255;
-
- newTextNode->EdgeColor.R = 142;
- newTextNode->EdgeColor.G = 106;
- newTextNode->EdgeColor.B = 12;
- newTextNode->EdgeColor.A = 255;
+ newTextNode->TextColor = new ByteColor { R = 255, G = 255, B = 255, A = 255 };
+ newTextNode->EdgeColor = new ByteColor { R = 142, G = 106, B = 12, A = 255 };
return newTextNode;
}
+
+ private void DtrEventHandler(AddonEventType atkEventType, IntPtr atkUnitBase, IntPtr atkResNode)
+ {
+ var addon = (AtkUnitBase*)atkUnitBase;
+ var node = (AtkResNode*)atkResNode;
+
+ if (this.entries.FirstOrDefault(entry => entry.TextNode == node) is not { } dtrBarEntry) return;
+
+ if (dtrBarEntry is { Tooltip: not null })
+ {
+ switch (atkEventType)
+ {
+ case AddonEventType.MouseOver:
+ AtkStage.GetSingleton()->TooltipManager.ShowTooltip(addon->ID, node, dtrBarEntry.Tooltip.Encode());
+ break;
+
+ case AddonEventType.MouseOut:
+ AtkStage.GetSingleton()->TooltipManager.HideTooltip(addon->ID);
+ break;
+ }
+ }
+
+ if (dtrBarEntry is { OnClick: not null })
+ {
+ switch (atkEventType)
+ {
+ case AddonEventType.MouseOver:
+ this.uiEventManager.SetCursor(AddonCursorType.Clickable);
+ break;
+
+ case AddonEventType.MouseOut:
+ this.uiEventManager.ResetCursor();
+ break;
+
+ case AddonEventType.MouseClick:
+ dtrBarEntry.OnClick.Invoke();
+ break;
+ }
+ }
+ }
+}
+
+///
+/// Plugin-scoped version of a AddonEventManager service.
+///
+[PluginInterface]
+[InterfaceVersion("1.0")]
+[ServiceManager.ScopedService]
+#pragma warning disable SA1015
+[ResolveVia]
+#pragma warning restore SA1015
+internal class DtrBarPluginScoped : IDisposable, IServiceType, IDtrBar
+{
+ [ServiceManager.ServiceDependency]
+ private readonly DtrBar dtrBarService = Service.Get();
+
+ private readonly Dictionary pluginEntries = new();
+
+ ///
+ public void Dispose()
+ {
+ foreach (var entry in this.pluginEntries)
+ {
+ entry.Value.Remove();
+ }
+
+ this.pluginEntries.Clear();
+ }
+
+ ///
+ public DtrBarEntry Get(string title, SeString? text = null)
+ {
+ // If we already have a known entry for this plugin, return it.
+ if (this.pluginEntries.TryGetValue(title, out var existingEntry)) return existingEntry;
+
+ return this.pluginEntries[title] = this.dtrBarService.Get(title, text);
+ }
+
+ ///
+ public void Remove(string title)
+ {
+ if (this.pluginEntries.TryGetValue(title, out var existingEntry))
+ {
+ existingEntry.Remove();
+ this.pluginEntries.Remove(title);
+ }
+ }
}
diff --git a/Dalamud/Game/Gui/Dtr/DtrBarAddressResolver.cs b/Dalamud/Game/Gui/Dtr/DtrBarAddressResolver.cs
new file mode 100644
index 0000000000..1e6fd09cd9
--- /dev/null
+++ b/Dalamud/Game/Gui/Dtr/DtrBarAddressResolver.cs
@@ -0,0 +1,29 @@
+namespace Dalamud.Game.Gui.Dtr;
+
+///
+/// DtrBar memory address resolver.
+///
+public class DtrBarAddressResolver : BaseAddressResolver
+{
+ ///
+ /// Gets the address of the AtkUnitBaseDraw method.
+ /// This is the base handler for all addons.
+ /// We will use this here because _DTR does not have a overloaded handler, so we must use the base handler.
+ ///
+ public nint AtkUnitBaseDraw { get; private set; }
+
+ ///
+ /// Gets the address of the DTRRequestUpdate method.
+ ///
+ public nint AddonRequestedUpdate { get; private set; }
+
+ ///
+ /// Scan for and setup any configured address pointers.
+ ///
+ /// The signature scanner to facilitate setup.
+ protected override void Setup64Bit(SigScanner scanner)
+ {
+ this.AtkUnitBaseDraw = scanner.ScanText("48 83 EC 28 F6 81 ?? ?? ?? ?? ?? 4C 8B C1");
+ this.AddonRequestedUpdate = scanner.ScanText("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 48 8B BA ?? ?? ?? ?? 48 8B F1 49 8B 98 ?? ?? ?? ?? 33 D2");
+ }
+}
diff --git a/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs b/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs
index c5bdb7e85c..f04e1427d0 100644
--- a/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs
+++ b/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs
@@ -41,6 +41,16 @@ public SeString? Text
this.Dirty = true;
}
}
+
+ ///
+ /// Gets or sets a tooltip to be shown when the user mouses over the dtr entry.
+ ///
+ public SeString? Tooltip { get; set; }
+
+ ///
+ /// Gets or sets a action to be invoked when the user clicks on the dtr entry.
+ ///
+ public Action? OnClick { get; set; }
///
/// Gets or sets a value indicating whether this entry is visible.
diff --git a/Dalamud/Plugin/Services/IAddonEventManager.cs b/Dalamud/Plugin/Services/IAddonEventManager.cs
new file mode 100644
index 0000000000..dbbfd784b7
--- /dev/null
+++ b/Dalamud/Plugin/Services/IAddonEventManager.cs
@@ -0,0 +1,46 @@
+using Dalamud.Game.AddonEventManager;
+
+namespace Dalamud.Plugin.Services;
+
+///
+/// Service provider for addon event management.
+///
+public interface IAddonEventManager
+{
+ ///
+ /// Delegate to be called when an event is received.
+ ///
+ /// Event type for this event handler.
+ /// The parent addon for this event handler.
+ /// The specific node that will trigger this event handler.
+ public delegate void AddonEventHandler(AddonEventType atkEventType, nint atkUnitBase, nint atkResNode);
+
+ ///
+ /// Registers an event handler for the specified addon, node, and type.
+ ///
+ /// Unique Id for this event, maximum 0x10000.
+ /// The parent addon for this event.
+ /// The node that will trigger this event.
+ /// The event type for this event.
+ /// The handler to call when event is triggered.
+ void AddEvent(uint eventId, nint atkUnitBase, nint atkResNode, AddonEventType eventType, AddonEventHandler eventHandler);
+
+ ///
+ /// Unregisters an event handler with the specified event id and event type.
+ ///
+ /// The Unique Id for this event.
+ /// The node for this event.
+ /// The event type for this event.
+ void RemoveEvent(uint eventId, nint atkResNode, AddonEventType eventType);
+
+ ///
+ /// Force the game cursor to be the specified cursor.
+ ///
+ /// Which cursor to use.
+ void SetCursor(AddonCursorType cursor);
+
+ ///
+ /// Un-forces the game cursor.
+ ///
+ void ResetCursor();
+}
diff --git a/Dalamud/Plugin/Services/IDtrBar.cs b/Dalamud/Plugin/Services/IDtrBar.cs
index 6c2b8ad1ef..a5a750cf68 100644
--- a/Dalamud/Plugin/Services/IDtrBar.cs
+++ b/Dalamud/Plugin/Services/IDtrBar.cs
@@ -19,4 +19,10 @@ public interface IDtrBar
/// The entry object used to update, hide and remove the entry.
/// Thrown when an entry with the specified title exists.
public DtrBarEntry Get(string title, SeString? text = null);
+
+ ///
+ /// Removes a DTR bar entry from the system.
+ ///
+ /// Title of the entry to remove.
+ public void Remove(string title);
}