-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeUtil.lua
More file actions
409 lines (310 loc) · 10.8 KB
/
Copy pathPrimeUtil.lua
File metadata and controls
409 lines (310 loc) · 10.8 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
--[[
====================================================
LibPrime
Author: Ivan Leben
====================================================
--]]
PrimeUtil = {}
--Table manipulation
--=========================================================
function PrimeUtil.CopyTableKeys (dst, src)
for key,value in pairs(src) do
if (type( value ) == "table") then
if (type( dst[key] ) ~= "table") then
dst[key] = CopyTable( value );
else
CB.Util_CopyTableKeys( dst[key], value );
end
else
dst[key] = value;
end
end
end
function PrimeUtil.MergeTables (t1, t2)
local t = CopyTable(t1);
CB.Util_CopyTableKeys(t,t2);
return t;
end
function PrimeUtil.ClearTable (t)
local len = table.getn( t );
while len > 0 do
table.remove( t );
len = len - 1;
end
end
function PrimeUtil.ClearTableKeys (t)
for k,v in pairs(t) do
t[ k ] = nil;
end
end
function PrimeUtil.CountTableKeys( t )
local count = 0;
for k,v in pairs(t) do
if (v ~= nil) then
count = count + 1;
end
end
return count;
end
--Capture list into table without creating a new one
--=========================================================
function PrimeUtil.CaptureList (t, ...)
--Get first value in the list
local i = 1;
local v = select( i, ... );
--Loop until end of list
while v ~= nil do
--Insert value at the end of the table
table.insert( t, v );
--Get next value
i = i + 1;
v = select( i, ... );
end
end
--Spell icon cache
--========================================================
function PrimeUtil.GetSpellIcon (spellName)
--Init cache table
if (CB.iconCache == nil) then
CB.iconCache = {};
end
--Try get it from the player's spellbook first
local icon = GetSpellTexture( spellName );
if (icon ~= nil) then
CB.iconCache[ spellName ] = icon;
return icon;
end
--Check for cached value
local icon = CB.iconCache[ spellName ];
if (icon ~= nil) then
if (icon ~= "")
then return icon;
else return nil;
end
end
--Search through all the spell IDs
for i=1,100000 do
local name,_,icon = GetSpellInfo( i );
if (name == spellName) then
CB.iconCache[ spellName ] = icon;
return icon;
end
end
--Mark not found
CB.iconCache[ spellName ] = "";
end
--Enchant scanning from weapon tooltip
--========================================================
function PrimeUtil.GetWeaponEnchantName (inventorySlotId)
local tt1 = CB.Util_GetTooltip(1); tt1:ClearLines();
local tt2 = CB.Util_GetTooltip(2); tt2:ClearLines();
--Set first tooltip to match inventory slot
tt1:SetInventoryItem( "player", inventorySlotId );
--Get hyperlink from first tooltip and set it to second tooltip
local itemLink = select( 2, tt1:GetItem() );
tt2:SetHyperlink( itemLink );
--Iterate all the lines of first tooltip
local numLines1 = tt1:NumLines();
for l1=1,numLines1 do
--Get line text and check if green color (red = 0)
local line1 = CB.Util_GetTooltipLineLeft( tt1, l1 );
local text1 = line1:GetText();
if (line1:GetTextColor() == 0) then
--Iterate all the lines of second tooltip
local found = false;
local numLines2 = tt2:NumLines();
for l2=1,numLines2 do
--Get line text and check if line from first tooltip found
local line2 = CB.Util_GetTooltipLineLeft( tt2, l2 );
local text2 = line2:GetText();
if (text2 == text1) then
found = true;
break;
end
end
--The green line that is missing in the second tooltip is the enchant!
if (not found) then
--Get rid of the duration in brackets at the end
local x = strfind( text1, "[(]" );
if (x ~= nil) then x = x - 2; end
return strsub( text1, 1, x );
end
end
end
end
--Scannable tooltips
--========================================================
function PrimeUtil.GetTooltip (index)
--Create tooltip table if missing
if (CB.tooltips == nil) then
CB.tooltips = {};
end
--Create tooltip at given index if missing
if (CB.tooltips[ index ] == nil) then
--Create new tooltip frame
local tt = CreateFrame( "GameTooltip", "Prime_Tooltip"..index );
tt:SetOwner( UIParent, "ANCHOR_NONE" );
CB.tooltips[ index ] = tt;
--Insert custom font strings for tooltip lines
tt.text = {}
for l=1,30 do
tt.text[l] = {};
tt.text[l].left = tt:CreateFontString();
tt.text[l].left:SetFontObject( "GameFontNormal" );
tt.text[l].right = tt:CreateFontString();
tt.text[l].right:SetFontObject( "GameFontNormal" );
tt:AddFontStrings( tt.text[l].left, tt.text[l].right );
end
--Clearing all lines will make tooltip store new text into custom ones
tt:ClearLines();
end
return CB.tooltips[ index ];
end
function PrimeUtil.GetTooltipLineLeft (tooltip, index)
return tooltip.text[ index ].left;
end
function PrimeUtil.GetTooltopLineRight (tooltip, index)
return tooltip.text[ index ].right;
end
--Refresh a context menu by closing and reopening it
--=======================================================
function PrimeUtil.UpdateMenu (menu)
--Check if the current menu frame matches the given menu and is shown
if not (UIDROPDOWNMENU_OPEN_MENU == menu and DropDownList1:IsShown()) then
return false
end
--Store current level and close all menu levels
local lastMenuLevel = UIDROPDOWNMENU_MENU_LEVEL;
CloseDropDownMenus()
--Reopen the first menu level at the same position
local x = DropDownList1:GetLeft();
local y = DropDownList1:GetBottom()
ToggleDropDownMenu( 1, nil, menu, UIParent, x, y );
--Iterate through all menu levels
for l = 2, lastMenuLevel do
--Get the frame of this menu level
local menuFrame = _G['DropDownList'..l];
if (menuFrame) then
--Get the parent button of this menu level
local _, buttonFrame = menuFrame:GetPoint();
if (buttonFrame) then
--Reopen this menu level with the same button as its anchor point
ToggleDropDownMenu( l, buttonFrame.value, nil, nil, nil, nil, nil, buttonFrame );
end
end
end
return true
end
function PrimeUtil.CompareVersions (old, new)
--Return false if both version the same
if (new == old) then return false end
--Walk through all the numbers in the version
local newNumbers = { strsplit( ".", new ) };
local oldNumbers = { strsplit( ".", old ) };
for i=1,table.getn(newNumbers) do
--New version is bigger if all previous numbers same
--and new version has additional numbers to old one
if (i > table.getn(oldNumbers)) then return true end
--Convert both strings to numbers
local newNum = tonumber( newNumbers[i] );
local oldNum = tonumber( oldNumbers[i] );
--New version is bigger or smaller if
--current number is bigger or smaller
if (newNum > oldNum) then return true end
if (newNum < oldNum) then return false end
end
--Old version is bigger if all previous numbers same
--and old version has additional numbers to new one
return false;
end
function PrimeUtil.CheckSettings()
--Check if global settings missing (first time use)
if (not ChronoBars_Settings) then
ChronoBars.Print( "Applying default settings" );
ChronoBars_Settings = CopyTable( ChronoBars.DEFAULT_SETTINGS );
end
--Check if character settings missing (first time use)
if (not ChronoBars_CharSettings) then
ChronoBars.Print( "Applying default character settings" );
ChronoBars_CharSettings = CopyTable( ChronoBars.DEFAULT_CHAR_SETTINGS );
--Create profile for this character first time if missing
local playerName = UnitName( "player" );
if (ChronoBars_Settings.profiles[ playerName ] == nil) then
ChronoBars_Settings.profiles[ playerName ] = CopyTable( ChronoBars.DEFAULT_PROFILE );
end
--Switch to profile with this character name
ChronoBars_CharSettings.activeProfile = playerName;
end
--Check for old settings version
local curVersion = ChronoBars_Settings.version;
for i=1,table.getn( ChronoBars.UPGRADE_LIST ) do
local nextVersion = ChronoBars.UPGRADE_LIST[i];
if (CB.CompareVersions( curVersion, nextVersion )) then
CB.Print( "Upgrading settings from version "
..curVersion.." to "..nextVersion );
local verString = string.gsub( nextVersion, "[.]", "_" );
local funcName = "Upgrade_"..verString;
local func = ChronoBars[ funcName ];
if (func) then func() else
CB.Print( "Missing upgrade function!" );
end
curVersion = nextVersion;
end
end
--Check for old char settings version
local curVersion = ChronoBars_CharSettings.version;
for i=1,table.getn( ChronoBars.UPGRADE_LIST ) do
local nextVersion = ChronoBars.UPGRADE_LIST[i];
if (CB.CompareVersions( curVersion, nextVersion )) then
ChronoBars.Print( "Upgrading character settings from version "
..curVersion.." to "..nextVersion );
local verString = string.gsub( nextVersion, "[.]", "_" );
local funcName = "UpgradeChar_"..verString;
local func = ChronoBars[ funcName ];
if (func) then func() else
ChronoBars.Print( "Missing upgrade function!" );
end
curVersion = nextVersion;
end
end
--Set to latest version
ChronoBars_Settings.version = ChronoBars.VERSION;
ChronoBars_CharSettings.version = ChronoBars.VERSION;
--Switch to default profile if active profile is missing
if (ChronoBars_Settings.profiles[ ChronoBars_CharSettings.activeProfile ] == nil) then
ChronoBars_CharSettings.activeProfile = "Default";
end
end
--[[
--Obsolete, but keeping it around just in case it
--comes in handy one day
========================================================
function ChronoBars.Bar_UpdateChildBounds (bar)
local l=nil; local r=nil; local b=nil; local t=nil;
if (bar.children == nil) then bar.children = {} end;
CB.Util_ClearTable( bar.children );
CB.Util_CaptureList( bar.children, bar:GetRegions() );
--CB.Util_CaptureList( bar.children, bar:GetChildren() );
local numChildren = table.getn( bar.children );
for c=1,numChildren do
if (bar.children[c]:IsShown()) then
local cl = bar.children[c]:GetLeft() - bar:GetLeft();
local cr = bar.children[c]:GetRight() - bar:GetLeft();
local ct = bar.children[c]:GetTop() - bar:GetBottom();
local cb = bar.children[c]:GetBottom() - bar:GetBottom();
if (l==nil or cl < l) then l = cl; end
if (r==nil or cr > r) then r = cr; end
if (b==nil or cb < b) then b = cb; end
if (t==nil or ct > t) then t = ct; end
end
end
if (l == nil) then l = 0; end
if (r == nil) then r = 0; end
if (b == nil) then b = 0; end
if (t == nil) then t = 0; end
bar.boundsL = l;
bar.boundsR = r;
bar.boundsB = b;
bar.boundsT = t;
end
--]]