This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.lua
344 lines (292 loc) · 11.6 KB
/
core.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
--[[ Buffology is a World of Warcraft-addon by Mischback.
This is core.lua, where the addon-core is defined.
##### FUNCTIONS #####
VOID SlashHandler() - Handles the slash-command
VOID WeaponEnchantWatcher(FRAME self, INT elapsed) - Handles weapon enchants
VOID UpdateIcon(STRING unit, INT index, INT offset, STRING filter) - Gets the buff-/debuff-details and creates a new icon, if necessary
VOID SetIconPositions() - Sets the positions off all (visible) icons
VOID Trigger(STRING unit) - UNIT_AURA triggers this
]]
local ADDON_NAME, ns = ... -- get the addons namespace to exchange functions between core and layout
local settings = ns.settings -- get the settings
local strings = ns.strings -- get the localization
local lib = ns.lib -- get the lib
local menu = ns.menu -- get the menu
local core = CreateFrame('Frame') -- create the core
-- *****************************************************
local Buffology = CreateFrame('Frame', 'Buffology', UIParent)
local Buffology_trigger = CreateFrame('Frame', 'Buffology_trigger', Buffology)
local Buffology_enchant = CreateFrame('Frame', 'Buffology_enchant', Buffology)
local Buffology_Icons = {}
local Buffology_Icons_sort = {}
local tempList = {}
Buffology.framelist = {}
--[[ Handles the slash-command
VOID SlashHandler()
]]
core.SlashHandler = function(msg)
if ( msg == strings.menutrigger ) then
menu.CreateMenu()
menu.MenuFrame:Show()
for _, v in pairs(Buffology.framelist) do
v:SetScript('OnClick', menu.BuffologySetActiveFrame)
v:Show()
end
else
lib.debugging(strings.slash_nocommand)
lib.debugging(strings.slash_menustring)
end
end
--[[ Handles weapon enchants
VOID WeaponEnchantWatcher(FRAME self, INT elapsed)
This is an OnUpdate-Eventhandler, which checks, if you have a temporary weapon-enchant.
]]
core.WeaponEnchantWatcher = function(self, elapsed)
self.lastUpdate = self.lastUpdate + elapsed
if ( self.lastUpdate > settings.static.updateInterval) then
-- lib.debugging('checking for WeaponEnchant')
local posToggle = false
if ( not PlayerFrame.unit or PlayerFrame.unit ~= "player" ) then
return -- don't check, if the player doesn't control his char
end
local hasMainHandEnchant, mainHandExpiration, mainHandCharges, hasOffHandEnchant, offHandExpiration, offHandCharges = GetWeaponEnchantInfo()
local icon
if ( hasMainHandEnchant ) then
icon = Buffology_Icons[1]
if ( not icon ) then
icon = lib.CreateIcon(1)
posToggle = true
end
icon.name = 'Mainhand'
lib.CollectAura(icon.name, 'TempEnchant (MH)')
icon.display = lib.FindDisplayFrame(icon)
icon.texture:SetTexture(GetInventoryItemTexture('player', 16))
icon.duration = 0
if ( mainHandExpiration ) then
icon.timestring:SetText(lib.TimeFormat(mainHandExpiration/1000))
icon.timestring:Show()
else
icon.timestring:Hide()
end
icon:SetScript('OnClick', function() CancelItemTempEnchantment(1) end)
icon:Show()
Buffology_Icons[1] = icon
else
if ( Buffology_Icons[1] ) then -- hide icon, if one has already been created
if ( Buffology_Icons[1]:IsShown() ) then
Buffology_Icons[1]:Hide()
posToggle = true
end
end
end
if ( hasOffHandEnchant ) then
icon = Buffology_Icons[2]
if ( not icon ) then
icon = lib.CreateIcon(2)
lib.debugging('create icon 2')
posToggle = true
end
icon.name = 'Offhand'
lib.CollectAura(icon.name, 'TempEnchant (OH)')
icon.display = lib.FindDisplayFrame(icon)
icon.texture:SetTexture(GetInventoryItemTexture('player', 17))
icon.duration = 0
if ( offHandExpiration ) then
icon.timestring:SetText(lib.TimeFormat(offHandExpiration/1000))
icon.timestring:Show()
else
icon.timestring:Hide()
end
icon:Show()
icon:SetScript('OnClick', function() CancelItemTempEnchantment(2) end)
Buffology_Icons[2] = icon
else
if ( Buffology_Icons[2] ) then -- hide icon, if one has already been created
if ( Buffology_Icons[2]:IsShown() ) then
Buffology_Icons[2]:Hide()
posToggle = true
end
end
end
if ( posToggle ) then
for _, v in pairs(Buffology_Icons) do
if (v) then
table.insert(Buffology_Icons_sort, v)
end
end
table.sort(Buffology_Icons_sort, lib.SortIcons)
core.SetIconPositions()
end
self.lastUpdate = 0
end
end
--[[ Gets the buff-/debuff-details and creates a new icon, if necessary.
VOID UpdateIcon(STRING unit, INT index, INT offset, STRING filter)
]]
core.UpdateIcon = function(unit, index, offset, filter)
local name, rank, texture, count, dtype, duration, timeleft, caster, _, _, spellID = UnitAura(unit, index, filter)
local icon = Buffology_Icons[index+offset]
if ( not name ) then -- UNIT_AURA was triggered, but there is no aura on this index (works damn good, when an aura faded, 'cause UNIT_AURA is triggered in this case, too)
if ( icon ) then -- hide icon, if one has already been created
icon:Hide() -- hide icon, if one has already been created
end
else
-- lib.debugging(spellID..': '..name)
if ( not icon ) then -- UNIT_AURA was triggered, but there is no icon for this index -> let's create one
icon = lib.CreateIcon(index+offset) -- create Icon if necessary
end
icon.name = name -- set the name
icon.spellID = spellID -- set the spellID
icon.texture:SetTexture(texture) -- set the texture
if ( filter == 'HARMFUL' ) then
icon.isDebuff = true -- set debuff-flag
end
icon.display = lib.FindDisplayFrame(icon)
if ( duration > 0 ) then -- we got a time-limited aura
icon.timeleft = timeleft -- apply timeleft
icon.duration = timeleft -- apply duration
icon:SetScript('OnUpdate', lib.UpdateAuraTime) -- apply the OnUpdate-handler
icon.timestring:Show() -- show the text
else -- we got a permanent aura
icon.timeleft = nil -- no timeleft
icon.duration = 0
icon:SetScript('OnUpdate', nil) -- no OnUpdate-handler necessary
icon.timestring:Hide() -- hide the text
end
icon.count:SetText((count > 1 and count))
-- if ( icon.cd ) then -- apply the cooldown-spiral TODO: Do we want this?
-- if ( duration and duration > 0 ) then
-- icon.cd:SetCooldown(timeleft - duration, duration)
-- icon.cd:Show()
-- else
-- icon.cd:Hide()
-- end
-- end
icon:SetID(index)
icon:Show()
Buffology_Icons[index+offset] = icon
lib.CollectAura(spellID, name)
end
end
--[[ Sets the positions off all (visible) icons
VOID SetIconPositions()
]]
core.SetIconPositions = function()
for _, v in pairs(Buffology.framelist) do -- clear all frames
v.icons = 0 -- no icons in any frame
end
local xModifier, yModifier
for k, v in pairs(Buffology_Icons_sort) do
if ( v:IsShown() ) then -- just do positioning for visible icons
xModifier = -1 -- default direction: LEFT
yModifier = -1 -- default direction: DOWN
if ( settings.frames[v.display].xGrowDir == 'RIGHT' ) then
xModifier = 1 -- switching to grow RIGHT
end
if ( settings.frames[v.display].xGrowDir == 'UP' ) then
yModifier = 1 -- switching to grow UP
end
v:SetHeight(settings.frames[v.display].iconSize or settings.static.iconSize)
v:SetWidth(settings.frames[v.display].iconSize or settings.static.iconSize)
-- SetPoint() OF HELL!!!
v:ClearAllPoints()
if ( lib.LBF ) then
-- lib.debugging('CreateIcon(): adding icon to ButtonFacade...')
lib.facadegroup:RemoveButton(v, true)
lib.facadegroup:AddButton(v)
end
v:SetPoint(settings.frames[v.display].anchorPoint,
Buffology.framelist[v.display],
settings.frames[v.display].anchorPoint,
xModifier * (Buffology.framelist[v.display].icons % settings.frames[v.display].columns) * ((settings.frames[v.display].iconSize or settings.static.iconSize) + settings.frames[v.display].xSpacing),
yModifier * (math.floor(Buffology.framelist[v.display].icons / settings.frames[v.display].columns)) * ((settings.frames[v.display].iconSize or settings.static.iconSize) + settings.frames[v.display].ySpacing) )
-- inc the number of icons on this frame!
Buffology.framelist[v.display].icons = Buffology.framelist[v.display].icons + 1
end
end
wipe(Buffology_Icons_sort)
end
--[[ This function is triggered by UNIT_AURA. It just walks over ALL of your buffs/debuffs to make sure, none is missed.
VOID Trigger(STRING unit)
]]
core.Trigger = function(_, _, unit)
if ( unit ~= 'player' ) then return end -- jump out, if we're not on 'player'
-- TODO: Do we want to manage more than player-buffs?!?
-- lib.debugging('Trigger()')
local index
for index = 1, settings.static.buff_maxicons do -- handle BUFFS
core.UpdateIcon(unit, index, settings.static.enchant_maxicons, 'HELPFUL')
end
for index = 1, settings.static.debuff_maxicons do -- handle DEBUFFS
core.UpdateIcon(unit, index, (settings.static.buff_maxicons + settings.static.enchant_maxicons), 'HARMFUL')
end
for _, v in pairs(Buffology_Icons) do
if (v) then
table.insert(Buffology_Icons_sort, v)
end
end
table.sort(Buffology_Icons_sort, lib.SortIcons)
core.SetIconPositions()
end
Buffology:RegisterEvent('ADDON_LOADED')
Buffology:SetScript('OnEvent', function(self, event, addon)
if ( addon ~= ADDON_NAME ) then return end -- jump out, if it's not our addon
if ( not Buffology_trigger ) then return end -- jump out, if we don't got a trigger
-- lib.debugging('Buffology loaded...')
-- ***** Loading the SavedVars *************************
do
if ( not BuffologyOptions) then
BuffologyOptions = {}
end
for k, v in pairs(settings.options) do
if type(v) ~= type(BuffologyOptions[k]) then
BuffologyOptions[k] = v
end
end
settings.options = BuffologyOptions
if ( not BuffologyFrames) then
BuffologyFrames = {}
end
for k, v in pairs(settings.frames) do
if type(v) ~= type(BuffologyFrames[k]) then
BuffologyFrames[k] = v
end
end
settings.frames = BuffologyFrames
if ( not BuffologyAssignments) then
BuffologyAssignments = {}
end
for k, v in pairs(settings.assignments) do
if type(v) ~= type(BuffologyAssignments[k]) then
BuffologyAssignments[k] = v
end
end
settings.assignments = BuffologyAssignments
if ( not BuffologyAuraList ) then
BuffologyAuraList = {}
BuffologyAuraList['locale'] = GetLocale()
end
if ( not BuffologyFacade ) then
BuffologyFacade = settings.facade
end
settings.facade = BuffologyFacade
end
-- ***** Setting Up Buffology **************************
strings.loadLocalizedStrings() -- get the correct localization
local BlizzFrame = _G['BuffFrame'] -- get the default Blizzard BuffFrame
BlizzFrame:UnregisterEvent('UNIT_AURA') -- Unregister the event
BlizzFrame:Hide() -- hide the default Blizzard BuffFrame
BlizzFrame = _G['TemporaryEnchantFrame']
BlizzFrame:Hide()
BlizzFrame:SetScript('OnUpdate', nil)
SLASH_BUFFOLOGY1 = strings.slashcommand -- register slash-command
SlashCmdList['BUFFOLOGY'] = core.SlashHandler -- register slash-command
lib.SetUpFrames(settings.frames, Buffology) -- create our frames
Buffology_trigger:RegisterEvent('UNIT_AURA')
Buffology_trigger:SetScript('OnEvent', core.Trigger)
Buffology_enchant.lastUpdate = 0
Buffology_enchant:SetScript('OnUpdate', core.WeaponEnchantWatcher)
core.Trigger(_, _, 'player')
end)
-- *****************************************************
ns.core = core -- handover of the core to the namespace