-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
addon.lua
316 lines (273 loc) · 9.82 KB
/
addon.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
local addonName, addon = ...
addon.data = {}
local modifier
local ERR_COLOR = CreateColor(1, 32/255, 32/255)
local TEMPLATES = {
'SecureActionButtonTemplate',
'SecureHandlerAttributeTemplate',
'SecureHandlerEnterLeaveTemplate',
}
if not addon:IsRetail() then
-- AutoCastShine was removed in 11.0, but we'll keep on using it in classic
table.insert(TEMPLATES, 'AutoCastShineTemplate')
end
local Molinari = addon:CreateButton('Button', addonName, UIParent, table.concat(TEMPLATES, ','))
Molinari:SetFrameStrata('TOOLTIP')
Molinari:Hide()
local function tooltipHelp(msg, color)
GameTooltip:AddLine(' ')
GameTooltip:AddLine(msg, color and color:GetRGB())
GameTooltip:Show() -- re-render
end
local function tooltipHook(tooltip, item)
if tooltip:GetOwner() == Molinari then
-- don't trigger on our own tooltips
return
end
if not item then
return
elseif InCombatLockdown() then
return
elseif UnitHasVehicleUI and UnitHasVehicleUI('player') then
return
elseif (PaperDollFrameItemFlyoutButtons or EquipmentFlyoutFrame) and (PaperDollFrameItemFlyoutButtons or EquipmentFlyoutFrame):IsVisible() then
return
elseif (AuctionFrame or AuctionHouseFrame) and (AuctionFrame or AuctionHouseFrame):IsVisible() then
return
elseif not IsAltKeyDown() then
return
elseif modifier == 'CTRL' and not IsControlKeyDown() then
return
elseif modifier ~= 'CTRL' and IsControlKeyDown() then
return
elseif modifier == 'SHIFT' and not IsShiftKeyDown() then
return
elseif modifier ~= 'SHIFT' and IsShiftKeyDown() then
return
end
local itemID = item:GetItemID()
if not itemID then
return
end
if addon:NonDisenchantable(itemID) or (C_Item.IsCosmeticItem and C_Item.IsCosmeticItem(itemID)) then
tooltipHelp(ITEM_DISENCHANT_NOT_DISENCHANTABLE, ERR_COLOR)
return
end
local spellID, color, numItemsRequired = addon:IsSalvagable(itemID)
if spellID then
if not IsPlayerSpell(spellID) then
local spellName = (C_Spell.GetSpellName or GetSpellInfo)(spellID)
tooltipHelp(ERR_USE_LOCKED_WITH_SPELL_S:format(spellName), ERR_COLOR)
return
elseif numItemsRequired and C_Item.GetStackCount(item:GetItemLocation()) < numItemsRequired then
tooltipHelp(SPELL_FAILED_NEED_MORE_ITEMS:format(numItemsRequired, C_Item.GetItemNameByID(itemID)), ERR_COLOR)
return
else
return Molinari:ApplySpell(item, spellID, color)
end
end
local key
key, color = addon:IsOpenableProfession(itemID)
if key then
return Molinari:ApplyItem(item, key, color)
end
end
function addon:OnLogin()
-- load late so our hooks are added late, that way our tooltip lines are more
-- likely to render at the bottom of the tooltip (but no guarantee)
addon:HookTooltip(tooltipHook)
end
local MACRO_SALVAGE = '/run C_TradeSkillUI.CraftSalvage(%d, 1, ItemLocation:CreateFromBagAndSlot(%d, %d))'
local MACRO_TRADE = '/cast %s\n/run ClickTargetTradeButton(7)'
function Molinari:ApplySpell(item, spellID, color)
local location = item:GetItemLocation()
if location and location:IsBagAndSlot() then
local bagID, slotID = location:GetBagAndSlot()
self:SetAttribute('target-bag', bagID)
self:SetAttribute('target-slot', slotID)
if not addon:IsRetail() or FindSpellBookSlotBySpellID(spellID) then
self:SetAttribute('spell', spellID)
else
self:SetAttribute('macrotext', MACRO_SALVAGE:format(spellID, bagID, slotID))
end
elseif item:GetItemLink() == GetTradeTargetItemLink(7) and color == addon.colors.openable then
self:SetAttribute('macrotext', MACRO_TRADE:format((C_Spell.GetSpellName or GetSpellInfo)(spellID)))
self.itemLink = item:GetItemLink() -- store item link for the tooltip
else
return
end
self.spellID = spellID -- for tooltips
self:Show()
self:SetColor(color)
end
function Molinari:ApplyItem(item, key, color)
local location = item:GetItemLocation()
if location and location:IsBagAndSlot() then
local bagID, slotID = location:GetBagAndSlot()
self:SetAttribute('target-bag', bagID)
self:SetAttribute('target-slot', slotID)
self:SetAttribute('item', 'item:' .. key)
self.itemID = item:GetItemID() -- for tooltips
self:Show()
self:SetColor(color)
end
end
-- update attribute driver with the correct modifiers
function Molinari:UpdateAttributeDriver()
if modifier == 'CTRL' then
addon:Defer('RegisterAttributeDriver', self, 'visibility', '[mod:alt,mod:ctrl] show; hide')
elseif modifier == 'SHIFT' then
addon:Defer('RegisterAttributeDriver', self, 'visibility', '[mod:alt,mod:shift] show; hide')
else
addon:Defer('RegisterAttributeDriver', self, 'visibility', '[mod:alt] show; hide')
end
end
-- update secure attribute type when setting the action
Molinari:HookScript('OnAttributeChanged', function(self, name)
if name == 'spell' or name == 'item' or name == 'macrotext' then
if modifier == 'CTRL' then
self:SetAttribute('alt-ctrl-type1', name:gsub('macrotext', 'macro'))
elseif modifier == 'SHIFT' then
self:SetAttribute('alt-shift-type1', name:gsub('macrotext', 'macro'))
else
self:SetAttribute('alt-type1', name:gsub('macrotext', 'macro'))
end
end
end)
-- re-anchor when shown
Molinari:HookScript('OnShow', function(self)
-- some addons put slots into a scrollframe for whatever reason, which we can't anchor to,
-- lets calculate anchors ourselves instead, adjusting for scale
local left, bottom, width, height = GameTooltip:GetOwner():GetScaledRect()
local scaleMultiplier = 1/UIParent:GetScale()
self:ClearAllPoints()
self:SetPoint('BOTTOMLEFT', left * scaleMultiplier, bottom * scaleMultiplier)
self:SetSize(width * scaleMultiplier, height * scaleMultiplier)
end)
-- set attribute to trigger EnterLeave driver
Molinari:HookScript('OnShow', function(self)
self:SetAttribute('_entered', true)
end)
-- use EnterLeave to securely deactivate when the mouse leaves the item
Molinari:SetAttribute('_onleave', 'self:ClearAllPoints();self:Hide()')
-- use attribute driver to securely deactivate when the modifier key is released
Molinari:SetAttribute('_onattributechanged', [[
if name == 'visibility' and value == 'hide' and self:IsShown() then
self:ClearAllPoints()
self:Hide()
end
]])
-- reset attributes when hidden
Molinari:HookScript('OnHide', function(self)
self.itemLink = nil
self.spellID = nil
self.itemID = nil
addon:DeferMethod(self, 'SetAttribute', 'target-bag')
addon:DeferMethod(self, 'SetAttribute', 'target-slot')
addon:DeferMethod(self, 'SetAttribute', '_entered', false)
end)
-- tooltips
Molinari:HookScript('OnLeave', GameTooltip_Hide)
Molinari:HookScript('OnEnter', function(self)
if self:GetRight() >= GetScreenWidth() / 2 then
GameTooltip:SetOwner(self, 'ANCHOR_LEFT')
else
GameTooltip:SetOwner(self, 'ANCHOR_RIGHT')
end
if self.itemLink then
GameTooltip:SetHyperlink(self.itemLink)
else
GameTooltip:SetBagItem(self:GetAttribute('target-bag'), self:GetAttribute('target-slot'))
end
if addon:IsRetail() then
if self.spellID then
local spellName = (C_Spell.GetSpellName or GetSpellInfo)(self.spellID)
tooltipHelp((('\n'):split(NPEV2_CASTER_ABILITYINITIAL:gsub(' %%s ', '%s'))):format('|A:NPE_LeftClick:18:18|a', '|cff0090ff' .. spellName .. '|r'))
elseif self.itemID then
tooltipHelp(NPEV2_ABILITYINITIAL:format('|A:NPE_LeftClick:18:18|a', '|cff0090ff' .. C_Item.GetItemNameByID(self.itemID) .. '|r'))
end
end
GameTooltip:Show()
end)
-- hide whenever a bag update occurs
function addon:BAG_UPDATE_DELAYED()
if Molinari:IsShown() and not InCombatLockdown() then
Molinari:Hide()
end
end
-- force-update tooltip whenever a modifier changes, as the state driver doesn't handle OnEnter
function addon:MODIFIER_STATE_CHANGED()
if Molinari:IsShown() then
Molinari:GetScript('OnEnter')(Molinari)
if modifier == 'CTRL' and not IsControlKeyDown() then
addon:DeferMethod(Molinari, 'Hide')
elseif modifier ~= 'CTRL' and IsControlKeyDown() then
addon:DeferMethod(Molinari, 'Hide')
elseif modifier == 'SHIFT' and not IsShiftKeyDown() then
addon:DeferMethod(Molinari, 'Hide')
elseif modifier ~= 'SHIFT' and IsShiftKeyDown() then
addon:DeferMethod(Molinari, 'Hide')
end
elseif GameTooltip:IsShown() then
local owner = GameTooltip:GetOwner()
if owner and not (GameTooltip:IsForbidden() or GameTooltip:IsProtected()) and owner:IsMouseOver() then
if owner.GetSlotAndBagID then
local slotIndex, bagID = owner:GetSlotAndBagID()
if slotIndex and bagID then
local item = Item:CreateFromBagAndSlot(bagID, slotIndex)
if item then
tooltipHook(GameTooltip, item)
return
end
end
end
local _, itemLink = GameTooltip:GetItem()
if itemLink then
tooltipHook(GameTooltip, Item:CreateFromItemLink(itemLink))
end
end
end
end
-- update state driver when setting changes
addon:RegisterOptionCallback('modifier', function(value)
modifier = value
addon:DeferMethod(Molinari, 'UpdateAttributeDriver')
end)
if addon:IsRetail() then
-- glow animation
local Glow = Molinari:CreateTexture(nil, 'ARTWORK')
Glow:SetPoint('CENTER')
Glow:SetAtlas('UI-HUD-ActionBar-Proc-Loop-Flipbook')
Glow:SetDesaturated(true) -- it's normally yellow, can't color that
local Animation = Molinari:CreateAnimationGroup()
Animation:SetLooping('REPEAT')
local FlipBook = Animation:CreateAnimation('FlipBook')
FlipBook:SetTarget(Glow)
FlipBook:SetDuration(1)
FlipBook:SetFlipBookColumns(5)
FlipBook:SetFlipBookRows(6)
FlipBook:SetFlipBookFrames(30)
function Molinari:SetColor(color)
Glow:SetVertexColor(color:GetRGB())
-- need to adjust the size too
local width, height = self:GetSize()
Glow:SetSize(width * 1.4, height * 1.4)
end
Molinari:HookScript('OnShow', function()
Animation:Play()
end)
Molinari:HookScript('OnHide', function()
Animation:Stop()
end)
else
-- use AutoCastShine
function Molinari:SetColor(color)
AutoCastShine_AutoCastStart(self, color:GetRGB())
end
-- adjust the glow sparkles
for _, sparkle in next, Molinari.sparkles do
sparkle:SetHeight(sparkle:GetHeight() * 3)
sparkle:SetWidth(sparkle:GetWidth() * 3)
end
Molinari:HookScript('OnHide', AutoCastShine_AutoCastStop)
end