This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
StatusNode.cs
259 lines (211 loc) · 9.54 KB
/
StatusNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Dalamud.Logging;
using Dalamud.Plugin;
using FFXIVClientStructs.FFXIV.Client.System.Memory;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace NamePlateDebuffs.StatusNode
{
public unsafe class StatusNode
{
private NamePlateDebuffsPlugin _plugin;
public AtkResNode* RootNode { get; private set; }
public AtkImageNode* IconNode { get; private set; }
public AtkTextNode* DurationNode { get; private set; }
public bool Visible { get; private set; }
public static int DefaultIconId = 10205;
private int CurrentIconId = DefaultIconId;
private int CurrentTimer = 60;
public StatusNode(NamePlateDebuffsPlugin p)
{
_plugin = p;
}
public void SetVisibility(bool enable)
{
RootNode->ToggleVisibility(enable);
}
public void SetStatus(int id, int timer)
{
SetVisibility(true);
if (id != CurrentIconId)
{
IconNode->LoadIconTexture(id, 0);
CurrentIconId = id;
}
if (timer != CurrentTimer)
{
DurationNode->SetNumber(timer);
CurrentTimer = timer;
}
}
public void LoadConfig()
{
if (!Built()) return;
IconNode->AtkResNode.SetPositionShort((short)_plugin.Config.IconX, (short)_plugin.Config.IconY);
IconNode->AtkResNode.SetHeight((ushort)_plugin.Config.IconHeight);
IconNode->AtkResNode.SetWidth((ushort)_plugin.Config.IconWidth);
DurationNode->AtkResNode.SetPositionShort((short)_plugin.Config.DurationX, (short)_plugin.Config.DurationY);
DurationNode->FontSize = (byte) _plugin.Config.FontSize;
ushort outWidth = 0;
ushort outHeight = 0;
DurationNode->GetTextDrawSize(&outWidth, &outHeight);
DurationNode->AtkResNode.SetWidth((ushort)(outWidth + 2 * _plugin.Config.DurationPadding));
DurationNode->AtkResNode.SetHeight((ushort)(outHeight + 2 * _plugin.Config.DurationPadding));
var iconHeight = (ushort)(_plugin.Config.IconY + _plugin.Config.IconHeight);
var durationHeight = (ushort)(_plugin.Config.DurationY + DurationNode->AtkResNode.Height);
RootNode->SetHeight(durationHeight > iconHeight ? durationHeight : iconHeight);
RootNode->SetWidth((ushort)(DurationNode->AtkResNode.Width > _plugin.Config.IconWidth ? DurationNode->AtkResNode.Width : _plugin.Config.IconWidth));
DurationNode->TextColor.R = (byte)(_plugin.Config.DurationTextColor.X * 255);
DurationNode->TextColor.G = (byte)(_plugin.Config.DurationTextColor.Y * 255);
DurationNode->TextColor.B = (byte)(_plugin.Config.DurationTextColor.Z * 255);
DurationNode->TextColor.A = (byte)(_plugin.Config.DurationTextColor.W * 255);
DurationNode->EdgeColor.R = (byte)(_plugin.Config.DurationEdgeColor.X * 255);
DurationNode->EdgeColor.G = (byte)(_plugin.Config.DurationEdgeColor.Y * 255);
DurationNode->EdgeColor.B = (byte)(_plugin.Config.DurationEdgeColor.Z * 255);
DurationNode->EdgeColor.A = (byte)(_plugin.Config.DurationEdgeColor.W * 255);
}
public bool Built() => RootNode != null && IconNode != null && DurationNode != null;
public bool BuildNodes(uint baseNodeId)
{
if (Built()) return true;
var rootNode = CreateRootNode();
if (rootNode == null) return false;
RootNode = rootNode;
var iconNode = CreateIconNode();
if (iconNode == null)
{
DestroyNodes();
return false;
}
IconNode = iconNode;
var durationNode = CreateDurationNode();
if (durationNode == null)
{
DestroyNodes();
return false;
}
DurationNode = durationNode;
RootNode->NodeID = baseNodeId;
RootNode->ChildCount = 2;
RootNode->ChildNode = (AtkResNode*) IconNode;
IconNode->AtkResNode.NodeID = baseNodeId + 1;
IconNode->AtkResNode.ParentNode = RootNode;
IconNode->AtkResNode.PrevSiblingNode = (AtkResNode*)DurationNode;
DurationNode->AtkResNode.NodeID = baseNodeId + 2;
DurationNode->AtkResNode.ParentNode = RootNode;
DurationNode->AtkResNode.NextSiblingNode = (AtkResNode*)IconNode;
LoadConfig();
return true;
}
public void DestroyNodes()
{
if (IconNode != null)
{
IconNode->UnloadTexture();
IMemorySpace.Free(IconNode->PartsList->Parts->UldAsset, (ulong)sizeof(AtkUldAsset));
IMemorySpace.Free(IconNode->PartsList->Parts, (ulong)sizeof(AtkUldPart));
IMemorySpace.Free(IconNode->PartsList, (ulong)sizeof(AtkUldPartsList));
IconNode->PartsList = null;
IconNode->AtkResNode.Destroy(true);
IconNode = null;
}
if (DurationNode != null)
{
DurationNode->AtkResNode.Destroy(true);
DurationNode = null;
}
if (RootNode != null)
{
RootNode->Destroy(true);
RootNode = null;
}
}
private AtkResNode* CreateRootNode()
{
var newResNode = (AtkResNode*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkResNode), 8);
if (newResNode == null)
{
PluginLog.Debug("Failed to allocate memory for res node");
return null;
}
IMemorySpace.Memset(newResNode, 0, (ulong)sizeof(AtkResNode));
newResNode->Ctor();
newResNode->Type = NodeType.Res;
newResNode->Flags = (short)(NodeFlags.AnchorLeft | NodeFlags.AnchorTop);
newResNode->DrawFlags = 0;
return newResNode;
}
private AtkImageNode* CreateIconNode()
{
var newImageNode = (AtkImageNode*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkImageNode), 8);
if (newImageNode == null)
{
PluginLog.Debug("Failed to allocate memory for image node");
return null;
}
IMemorySpace.Memset(newImageNode, 0, (ulong)sizeof(AtkImageNode));
newImageNode->Ctor();
newImageNode->AtkResNode.Type = NodeType.Image;
newImageNode->AtkResNode.Flags = (short)(NodeFlags.AnchorLeft | NodeFlags.AnchorTop | NodeFlags.UseDepthBasedPriority);
newImageNode->AtkResNode.DrawFlags = 0;
newImageNode->WrapMode = 1;
newImageNode->Flags |= (byte)ImageNodeFlags.AutoFit;
var partsList = (AtkUldPartsList*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkUldPartsList), 8);
if (partsList == null)
{
PluginLog.Debug("Failed to allocate memory for parts list");
newImageNode->AtkResNode.Destroy(true);
return null;
}
partsList->Id = 0;
partsList->PartCount = 1;
var part = (AtkUldPart*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkUldPart), 8);
if (part == null)
{
PluginLog.Debug("Failed to allocate memory for part");
IMemorySpace.Free(partsList, (ulong)sizeof(AtkUldPartsList));
newImageNode->AtkResNode.Destroy(true);
}
part->U = 0;
part->V = 0;
part->Width = 24;
part->Height = 32;
partsList->Parts = part;
var asset = (AtkUldAsset*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkUldAsset), 8);
if (asset == null)
{
PluginLog.Log("Failed to allocate memory for asset");
IMemorySpace.Free(part, (ulong)sizeof(AtkUldPart));
IMemorySpace.Free(partsList, (ulong)sizeof(AtkUldPartsList));
newImageNode->AtkResNode.Destroy(true);
}
asset->Id = 0;
asset->AtkTexture.Ctor();
part->UldAsset = asset;
newImageNode->PartsList = partsList;
newImageNode->LoadIconTexture(DefaultIconId, 0);
return newImageNode;
}
private AtkTextNode* CreateDurationNode()
{
var newTextNode = (AtkTextNode*)IMemorySpace.GetUISpace()->Malloc((ulong)sizeof(AtkTextNode), 8);
if (newTextNode == null)
{
PluginLog.Debug("Failed to allocate memory for text node");
return null;
}
IMemorySpace.Memset(newTextNode, 0, (ulong)sizeof(AtkTextNode));
newTextNode->Ctor();
newTextNode->AtkResNode.Type = NodeType.Text;
newTextNode->AtkResNode.Flags = (short)(NodeFlags.AnchorLeft | NodeFlags.AnchorTop | NodeFlags.UseDepthBasedPriority);
newTextNode->AtkResNode.DrawFlags = 12;
newTextNode->AtkResNode.SetWidth(24);
newTextNode->AtkResNode.SetHeight(17);
newTextNode->LineSpacing = 12;
newTextNode->AlignmentFontType = 4;
newTextNode->FontSize = 12;
newTextNode->TextFlags = (byte)(TextFlags.AutoAdjustNodeSize | TextFlags.Edge);
newTextNode->TextFlags2 = 0;
newTextNode->SetNumber(20);
return newTextNode;
}
}
}