-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLootDrop.lua
507 lines (416 loc) · 13.7 KB
/
LootDrop.lua
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
------------------------------------------------
-- Loot Drop - show me what I got
--
-- @author Pawkette ( [email protected] )
------------------------------------------------
local LootDropPool = LootDropPool
local LootDrop = LootDropPool:Subclass()
LootDrop.dirty_flags = {}
LootDrop.pending_pool = setmetatable( {}, { __mode = 'kv' } )
LootDrop.config = nil
LootDrop.db = nil
local tinsert = table.insert
local tremove = table.remove
local ZO_ColorDef = ZO_ColorDef
local zo_min = zo_min
local zo_parselink = ZO_LinkHandler_ParseLink
local zo_commify = ZO_CommaDelimitNumber
local Config = LootDropConfig
local LootDroppable = LootDroppable
local CBM = CALLBACK_MANAGER
local LootDropFade = LootDropFade
local LootDropSlide = LootDropSlide
local LootDropPop = LootDropPop
local _
local defaults =
{
displayduration = 10,
experience = true,
coin = true,
loot = true,
alliance = true,
battle = true,
width = 202,
height = 42,
padding = 6,
font =
{
face = 'Univers 55',
size = 14,
deco = 'thin-outline',
},
}
--- Flags for updating UI aspects
local DirtyFlags =
{
LAYOUT = 1 -- we've added or removed a droppable
}
--- Create our ObjectPool
--
-- @param ...
function LootDrop:New( ... )
local result = LootDropPool.New( self )
result:Initialize( ... )
return result
end
--- I swear I'm going to use this for something
--
-- @param ...
function LootDrop:Initialize( control )
LootDropPool.Initialize( self, function() return self:CreateDroppable() end, function( ... ) self:ResetDroppable( ... ) end )
self.control = control
self.control:RegisterForEvent( EVENT_ADD_ON_LOADED, function( ... ) self:OnLoaded( ... ) end )
self.control:SetHandler( 'OnUpdate', function( _, ft ) self:OnUpdate( ft ) end )
self._fadeIn = LootDropFade:New( 0.0, 1.0, 200 )
self._fadeOut = LootDropFade:New( 1.0, 0.0, 200 )
self._slide = LootDropSlide:New( 200 )
self._pop = LootDropPop:New()
self._coinId = nil
self._xpId = nil
self._apId = nil
self._btId = nil
CBM:RegisterCallback( Config.EVENT_TOGGLE_COIN, function() self:ToggleCoin() end )
CBM:RegisterCallback( Config.EVENT_TOGGLE_XP, function() self:ToggleXP() end )
CBM:RegisterCallback( Config.EVENT_TOGGLE_LOOT, function() self:ToggleLoot() end )
CBM:RegisterCallback( Config.EVENT_TOGGLE_AP, function() self:ToggleAP() end )
CBM:RegisterCallback( Config.EVENT_TOGGLE_BT, function() self:ToggleBT() end )
end
--- Called when the addon is loaded
--
-- @param event the event id, we don't really care about
-- @param addon the string name of the addon loaded
function LootDrop:OnLoaded( event, addon )
if ( addon ~= 'LootDrop' ) then
return
end
self.db = ZO_SavedVars:NewAccountWide( 'LOOTDROP_DB', 2.8, nil, defaults )
self.config = Config:New( self.db )
self:ToggleCoin()
self:ToggleXP()
self:ToggleLoot()
self:ToggleAP()
self:ToggleBT()
end
function LootDrop:ToggleCoin()
if ( self.db.coin ) then
self.current_money = GetCurrentMoney()
self.control:RegisterForEvent( EVENT_MONEY_UPDATE, function( _, ... ) self:OnMoneyUpdated( ... ) end )
else
self.control:UnregisterForEvent( EVENT_MONEY_UPDATE )
end
end
function LootDrop:ToggleXP()
if ( self.db.experience ) then
self.current_xp = GetUnitXP( 'player' )
self.control:RegisterForEvent( EVENT_EXPERIENCE_UPDATE, function( _, ... ) self:OnXPUpdated( ... ) end )
else
self.control:UnregisterForEvent( EVENT_EXPERIENCE_UPDATE )
end
end
function LootDrop:ToggleLoot()
if ( self.db.loot ) then
self.control:RegisterForEvent( EVENT_LOOT_RECEIVED, function( _, ... ) self:OnItemLooted( ... ) end )
self.control:RegisterForEvent( EVENT_INVENTORY_SINGLE_SLOT_UPDATE, function( _, ... ) self:OnInventorySlotUpdated( ... ) end )
else
self.control:UnregisterForEvent( EVENT_LOOT_RECEIVED )
self.control:UnregisterForEvent( EVENT_INVENTORY_SINGLE_SLOT_UPDATE )
end
end
function LootDrop:ToggleAP()
if ( self.db.alliance ) then
self.control:RegisterForEvent( EVENT_ALLIANCE_POINT_UPDATE, function( _, ... ) self:OnAPUpdate( ... ) end )
else
self.control:UnregisterForEvent( EVENT_ALLIANCE_POINT_UPDATE )
end
end
function LootDrop:ToggleBT()
if ( self.db.battle ) then
self.control:RegisterForEvent( EVENT_BATTLE_TOKEN_UPDATE, function( _, ... ) self:OnBTUpdate( ... ) end )
else
self.control:UnregisterForEvent( EVENT_BATTLE_TOKEN_UPDATE )
end
end
--- Check if any flags are set, if no flag is passed will check if any flag is set.
--
-- @param flag the flag to check, or nil for any flag
-- @return if the provided flag is set, or any flag is set if nil
function LootDrop:IsDirty( flag )
if ( not flag ) then return #self.dirty_flags ~= 0 end
for _,v in pairs( self.dirty_flags ) do
if ( v == flag ) then
return true
end
end
return false
end
--- On every consecutive frame
--
-- @param frameTime the delta between frames
function LootDrop:OnUpdate( frameTime )
if ( not #self._active ) then
return
end
local i = 1
local entry = nil
while( i <= #self._active ) do
entry = self._active[ i ]
if ( frameTime - entry:GetTimestamp() > self.db.displayduration ) then
self:Release( entry )
tinsert( self.dirty_flags, DirtyFlags.LAYOUT )
else
i = i + 1
end
end
if ( self:IsDirty( DirtyFlags.LAYOUT ) ) then
local last_y = 0
entry = nil
for i=1,#self._active do
entry = self._active[ i ]
if ( not entry:IsVisible() ) then
entry:Show( last_y )
else
entry:Move( 0, last_y )
end
last_y = last_y - ( self.db.height + self.db.padding )
end
end
self.dirty_flags = {}
end
--- Create a new loot droppable
--
-- @return a new LootDroppable
function LootDrop:CreateDroppable()
return LootDroppable:New( self )
end
--- Reset a loot droppable
--
-- @param droppable the droppable to reset
-- @param key a key to check against
function LootDrop:ResetDroppable( droppable, key )
if ( key == self._coinId ) then
self._coinId = nil
elseif( key == self._xpId ) then
self._xpId = nil
end
droppable:Hide()
end
function LootDrop:Acquire()
local result, key = LootDropPool.Acquire( self )
result:Prepare()
tinsert( self.dirty_flags, DirtyFlags.LAYOUT )
return result, key
end
function LootDrop:FormatItemName( str )
local result = ''
local entry = nil
local char = ''
local needsUpper = false
for i=1,str:utf8len() do
char = str:utf8sub( i, i )
if ( i == 1 or needsUpper ) then
result = result .. char:utf8upper()
else
result = result .. char
end
needsUpper = ( char == ' ' )
end
return result
end
--- Parse an item link
--
-- @param link
-- @return the text of the link and the identifier
function LootDrop:ParseLink( link )
if ( type( link ) ~= 'string' ) then
return nil, nil
end
local text, _, _, identifier = zo_parselink( link )
if ( not text or text == '' ) then
text = link
end
return text, identifier
end
--- Called when the player loots stuff, and does other things >.>
--
-- @param bagId the bag id of the item
-- @param slotId the slot id within the bag
-- @param newItem if the item is new or not
function LootDrop:OnInventorySlotUpdated( bagId, slotId, newItem, _, updateReason )
if ( ( updateReason ~= INVENTORY_UPDATE_REASON_DEFAULT ) or ( bagId ~= BAG_BACKPACK ) ) then
return
end
local link = GetItemLink( bagId, slotId, LINK_STYLE_DEFAULT )
local _, identifier = self:ParseLink( link )
local icon, _, _, _, _, _, _, quality = GetItemInfo( bagId, slotId )
tinsert( self.pending_pool, { [ 'id' ] = identifier, [ 'quality' ] = quality, [ 'icon' ] = icon } )
end
--- Locate a pending item from the pending pool
--
-- @param identifier the id of the item we're looking for
-- @return pending item or nil
function LootDrop:FindPendingItem( identifier )
for k,pending in pairs( self.pending_pool ) do
if ( pending.id == identifier ) then
return tremove( self.pending_pool, k )
end
end
return nil
end
--- Called when you loot an Item
--
-- @param itemName the name of the item looted (link)
-- @param quantity the number of items looted
-- @param mine if this item is the local player's
function LootDrop:OnItemLooted( _, itemName, quantity, _, _, mine )
if ( not mine ) then
return
end
local icon, _, _, _, _ = GetItemLinkInfo( itemName )
local text, identifier = self:ParseLink( itemName )
local quality = ITEM_QUALITY_TRASH
local pending = self:FindPendingItem( identifier )
if ( pending ) then
quality = pending.quality
icon = pending.icon
end
local color_def = GetItemQualityColor( quality )
text = self:FormatItemName( text )
text = color_def:Colorize( text )
if ( not icon or icon == '' ) then
icon = [[/esoui/art/icons/icon_missing.dds]]
end
local newDrop, _ = self:Acquire()
newDrop:SetTimestamp( GetFrameTimeSeconds() )
newDrop:SetRarity( color_def )
newDrop:SetIcon( icon )
newDrop:SetLabel( zo_strformat( '<<1>> <<2[//x$d]>>', text, quantity ) )
end
--- Called when the amount of money you have changes
--
-- @param money the amount of money you currently have
function LootDrop:OnMoneyUpdated( money )
if ( self.current_money == money ) then
return
end
local difference = money - self.current_money
self.current_money = money
local pop = false
local newDrop = nil
if ( self._coinId ) then
newDrop = self:Get( self._coinId )
if ( newDrop ) then
pop = true
difference = difference + ( newDrop:GetLabel() or 0 )
end
end
if ( not newDrop ) then
newDrop, self._coinId = self:Acquire()
end
newDrop:SetTimestamp( GetFrameTimeSeconds() )
newDrop:SetRarity( ZO_ColorDef:New( 'FFFF66' ) )
newDrop:SetIcon( [[/esoui/art/icons/item_generic_coinbag.dds]] )
newDrop:SetLabel( zo_commify( difference ) )
if ( pop ) then
local anim = self._pop:Apply( newDrop.control )
anim:Forward()
end
end
--- Called when the player's XP changes
--
-- @param tag which player this applies too
-- @param exp the amount of xp change
-- @param maxExp the current max amount of xp for this level
-- @param reason why the plasyer gained xp
function LootDrop:OnXPUpdated( tag, exp, maxExp, reason )
if ( tag ~= 'player' ) then
return
end
local xp = zo_min( exp, maxExp )
if ( self.current_xp == xp ) then
return
end
local gain = xp - self.current_xp
self.current_xp = xp
if ( gain <= 0 ) then
return
end
local pop = false
local newDrop = nil
if ( self._xpId ) then
newDrop = self:Get( self._xpId )
if ( newDrop ) then
pop = true
gain = gain + ( newDrop:GetLabel() or 0 )
end
end
if ( not newDrop ) then
newDrop, self._xpId = self:Acquire()
end
newDrop:SetTimestamp( GetFrameTimeSeconds() )
newDrop:SetRarity( ZO_ColorDef:New( 0, 1, 0, 1 ) )
newDrop:SetIcon( [[/lootdrop/textures/decoration.dds]], { 0.734375, 1, 0, 0.234375 } )
newDrop:SetLabel( zo_commify( gain ) )
if ( pop ) then
local anim = self._pop:Apply( newDrop.control )
anim:Forward()
end
end
--- Called when the player's AP changes
--
-- @param difference the amount of AP change
function LootDrop:OnAPUpdate( _, _, difference )
local pop = false
local newDrop = nil
if ( self._apId ) then
newDrop = self:Get( self._apId )
if ( newDrop ) then
pop = true
difference = difference + ( newDrop:GetLabel() or 0 )
end
end
if ( not newDrop ) then
newDrop, self._apId = self:Acquire()
end
newDrop:SetTimestamp( GetFrameTimeSeconds() )
newDrop:SetRarity( ZO_ColorDef:New( 0, 0, 1, 1 ) )
newDrop:SetIcon( [[/lootdrop/textures/decoration.dds]], { 0, 0.2734375, 0.46875, 0.6328125 } )
newDrop:SetLabel( zo_commify( difference ) )
if ( pop ) then
local anim = self._pop:Apply( newDrop.control )
anim:Forward()
end
end
--- Called when the player's BT changes
--
-- @param difference the amount of BT change
function LootDrop:OnBTUpdate( _, _, difference )
local pop = false
local newDrop = nil
if ( self._btId ) then
newDrop = self:Get( self._btId )
if ( newDrop ) then
pop = true
difference = difference + ( newDrop:GetLabel() or 0 )
end
end
if ( not newDrop ) then
newDrop, self._btId = self:Acquire()
end
newDrop:SetTimestamp( GetFrameTimeSeconds() )
newDrop:SetRarity( ZO_ColorDef:New( 1, 0, 0, 1 ) )
newDrop:SetIcon( [[/lootdrop/textures/decoration.dds]], { 0.734375, 1, 0.2343750, 0.46875 } )
newDrop:SetLabel( zo_commify( difference ) )
if ( pop ) then
local anim = self._pop:Apply( newDrop.control )
anim:Forward()
end
end
--- Getter for the control xml element
--
-- @return the visual UI
function LootDrop:GetControl()
return self.control
end
function LootDrop_Initialized( self )
LOOT_DROP = LootDrop:New( self )
end