-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAutoBarClassBasicButton.lua
More file actions
385 lines (330 loc) · 11.2 KB
/
AutoBarClassBasicButton.lua
File metadata and controls
385 lines (330 loc) · 11.2 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
--
-- AutoBarClassBasicButton
-- Copyright 2008+ Toadkiller of Proudmoore.
-- Implements SecureHandler and other code common to both anchor and popup buttons
--
-- Buttons are contained by AutoBar.Class.Bar
-- http://muffinmangames.com
--
local _, AB = ...
local types = AB.types ---@class ABTypes
local code = AB.code ---@class ABCode
local AutoBar = AutoBar
local ABGData = AutoBarGlobalDataObject
local L = AutoBarGlobalDataObject.locale
local _G = _G
local _
if (not AutoBar.Class) then
AutoBar.Class = {}
end
local function class_new(cls, ...)
local instance = setmetatable({}, cls)
instance:init(...)
return instance
end
local function Class(parent)
local c = parent and setmetatable({}, {__index = parent}) or {}
c.__index = c
c.super = parent
c.new = class_new
return c
end
AutoBar.Class.new_class = Class
-- Basic Button with textures, highlighting, keybindText, tooltips etc.
AutoBar.Class.BasicButton = Class()
function AutoBar.Class.BasicButton:init(parentBar, buttonDB)
end
-- OnLeave function. Added to a button to allow calling it via control:CallMethod("TooltipHide")
function AutoBar.Class.BasicButton.TooltipHide()
GameTooltip:Hide()
end
-- OnEnter function. Added to a button to allow calling it via control:CallMethod("TooltipSet")
function AutoBar.Class.BasicButton.TooltipShow(button)
local x = button:GetRight()
if (x >= (GetScreenWidth() / 2)) then
GameTooltip:SetOwner(button, "ANCHOR_LEFT")
else
GameTooltip:SetOwner(button, "ANCHOR_RIGHT")
end
local itemLink = button:GetAttribute("itemLink")
local buttonType = button:GetAttribute("type")
local item_guid = button:GetAttribute("AutoBarGUID")
local item_data = AB.InfoFromGUID(item_guid)
if (AutoBar.moveButtonsMode) then
local name = AB.GetButtonDisplayName(button.class.buttonDB)
GameTooltip:AddLine(name, 0.8, 0, 1, -1)
GameTooltip:Show()
elseif(item_data) then
if(item_data.link) then
GameTooltip:SetHyperlink(item_data.link)
elseif(item_data.tooltip) then
GameTooltip:AddLine(item_data.tooltip, 1, 1, 1)
elseif(item_data.ab_type == ABGData.TYPE_TOY) then
GameTooltip:SetToyByItemID(item_data.item_id)
end
button.UpdateTooltip = AutoBar.Class.BasicButton.TooltipShow
GameTooltip:Show()
elseif (buttonType == "spell") then
local spell_name = button:GetAttribute("spell")
if (tonumber(spell_name)) then
GameTooltip:SetSpellByID(spell_name);
else
local spell_info = AutoBarSearch.GetRegisteredSpellInfo(spell_name)
GameTooltip:SetSpellByID(spell_info.spell_id);
end
button.UpdateTooltip = AutoBar.Class.BasicButton.TooltipShow
elseif (itemLink) then
GameTooltip:SetHyperlink(itemLink)
-- if (GameTooltip:SetHyperlink(itemLink)) then
button.UpdateTooltip = AutoBar.Class.BasicButton.TooltipShow
-- end
elseif (buttonType == "macro") then
-- There is no accesible GameTooltip function for macros so make one with its name and the macro text
local macroName = button:GetAttribute("macroName")
local macroBody = button:GetAttribute("macroBody")
if (macroName and macroBody) then
GameTooltip:AddLine(macroName, 0.2, 0.8, 0.8)
GameTooltip:AddLine(macroBody, 1, 1, 1, 1)
button.UpdateTooltip = AutoBar.Class.BasicButton.TooltipShow
GameTooltip:Show()
end
elseif (buttonType == "item") then
-- There is no way to get charge information outside built in Blizzard functions for buttonType == "action"
-- The best we can do is link to a specific bag/slot so at least the tooltip can show this info
-- Sadly, the itemString for this is "[bag] [slot]" which is not a valid paramater for SetHyperlink
-- It is thus special cased here
local bagslot = button:GetAttribute("item")
if (bagslot) then
local bag, slot = strmatch(bagslot, "^(%d+)%s+(%d+)$")
if (bag and slot and GameTooltip:SetBagItem(bag, slot)) then
button.UpdateTooltip = AutoBar.Class.BasicButton.TooltipShow
end
end
end
local rightClickType = button:GetAttribute("type2")
if (rightClickType == "spell") then
local spellName = button:GetAttribute("spell")
local spellName2 = button:GetAttribute("spell2")
if (not spellName or (spellName2 ~= spellName)) then
GameTooltip:AddLine(L["Right Click casts "] .. spellName2, 1, 0.2, 1, 1)
GameTooltip:Show()
end
end
end
--[[
/dump GameTooltip:SetHyperlink("spell:32246")
--]]
-- Apply tooltipType to the Button
function AutoBar.Class.BasicButton:TooltipApply(button)
if (AutoBarDB2.settings.show_tooltip) then
if (not button.TooltipShow) then
button.TooltipShow = AutoBar.Class.BasicButton.TooltipShow
-- SecureHandlerWrapScript(button, "OnEnter", button, [[ control:CallMethod("TooltipShow", self) ]])
end
if (not button.TooltipHide) then
button.TooltipHide = AutoBar.Class.BasicButton.TooltipHide
SecureHandlerWrapScript(button, "OnLeave", button, [[ control:CallMethod("TooltipHide") ]])
end
button:SetAttribute("showTooltip", true)
else
button:SetAttribute("showTooltip", nil)
end
end
local function get_texture_for_action(p_action)
local texture
if (p_action) then
texture = select(3, AB.GetSpellInfo(p_action)) or code.GetIconForItemID(p_action)
end
return texture;
end
--local function get_texture_for_macro_body(p_macro_body)
-- local debug = false
--
-- local action = AB.GetActionForMacroBody(p_macro_body);
-- local texture = get_texture_for_action(action)
--
-- if (debug) then
-- print(" texture:", texture);
-- print(" action:" .. action)
-- end
--
--
-- return texture
--end
local borderBlue = {r = 0, g = 0, b = 1.0, a = 0.35}
local borderGreen = {r = 0, g = 1.0, b = 0, a = 0.35}
function AutoBar.Class.BasicButton:GetIconTexture(frame)
local texture, borderColor
local itemType = frame:GetAttribute("type")
local item_guid = frame:GetAttribute("AutoBarGUID")
local item_data = AB.InfoFromGUID(item_guid)
if(item_data) then
if(item_data.icon) then
texture = item_data.icon -- Use cached icon if we have one
elseif(item_data.ab_type == ABGData.TYPE_TOY) then
texture = code.GetIconForToyID(item_data.item_id)
end
elseif (itemType == "item") then
local itemId = frame:GetAttribute("itemId")
if (itemId) then
texture = code.GetIconForItemID(tonumber(itemId))
local bag, slot = AutoBarSearch.found:GetItemData(itemId)
if ((not bag) and slot) then
-- Add a green border if button is an equipped item
borderColor = borderGreen
end
end
elseif (itemType == "macro") then
local macroIndex = frame:GetAttribute("macro")
if (macroIndex) then
_, texture = GetMacroInfo(macroIndex)
else
texture = frame.class.macroTexture or self.frame:GetAttribute("macro_icon")
if (not texture) then
local macro_action = self.frame:GetAttribute("macro_action")
texture = get_texture_for_action(macro_action) or "Interface\\Icons\\INV_Misc_Gift_05"
end
end
elseif (itemType == "spell") then
local spellName = frame:GetAttribute("spell")
if (spellName) then
texture = code.get_spell_icon_by_name_fast(spellName) or select(3, AB.GetSpellInfo(spellName))
-- Add a blue border if button is a spell
borderColor = borderBlue
end
end
-- Fall through to right click spell
if (not texture) then
local spellName = frame:GetAttribute("spell2")
if (spellName) then
_, _, texture = AB.GetSpellInfo(spellName)
-- Add a blue border if button is a spell
borderColor = borderBlue
end
end
return texture, borderColor
end
-- Set cooldown based on the type settings
function AutoBar.Class.BasicButton:UpdateCooldown()
local itemType = self.frame:GetAttribute("type")
if (not itemType) then-- and not self.parentBar.faded
return;
end
local start, duration, enabled = 0, 0, 0
if (itemType == "item") then
local item_id = self.frame:GetAttribute("itemId")
if (item_id) then
start, duration, enabled = C_Container.GetItemCooldown(item_id)
end
elseif (itemType == "toy") then
local item_guid = self.frame:GetAttribute("AutoBarGUID")
local item_data = AB.InfoFromGUID(item_guid)
if (item_data) then
start, duration, enabled = C_Container.GetItemCooldown(item_data.item_id)
end
-- elseif (itemType == "macro") then --ToDo some day
-- local macroText = self.frame:GetAttribute("macrotext")
-- SecureCmdOptionParse()?
elseif (itemType == "spell") then
local spellName = self.frame:GetAttribute("spell")
start, duration = AB.GetSpellCooldown(spellName)
end
-- if (start and duration and start > 0 and duration > 0) then
CooldownFrame_Set(self.frame.cooldown, start, duration, enabled)
-- self.frame.cooldown:SetSwipeColor(0, 0, 0);
-- else
-- CooldownFrame_Set(self.frame.cooldown, 0, 0, 0)
-- end
end
-- Set count based on the type and type2 settings
function AutoBar.Class.BasicButton:UpdateCount()
local frame = self.frame
if (not AutoBarDB2.settings.show_count) then
frame.count:Hide()
return
end
frame.count:Show()
local count1 = 0
local count2 = 0
local itemType = frame:GetAttribute("type")
if (itemType) then
if (itemType == "item") then
local itemId = frame:GetAttribute("itemId") or 1
count1 = code.GetItemCount(tonumber(itemId), nil, true) or 0
-- Toys and Macros don't have counts, though a macro of an item could.
-- elseif (itemType == "macro") then
-- elseif (itemType == "toy") then
elseif (itemType == "spell") then
local spellName = frame:GetAttribute("spell")
count1 = AB.GetSpellCount(spellName) or 0
if issecretvalue and issecretvalue(count1) then count1 = 0; end
local spellName2 = frame:GetAttribute("spell2")
if (spellName2) then
count2 = AB.GetSpellCount(spellName2) or 0
if issecretvalue and issecretvalue(count2) then count2 = 0; end
end
end
end
local displayCount1 = (count1 > 999) and "*" or tostring(count1)
local displayCount2 = (count2 > 999) and "*" or tostring(count2)
if (itemType == "spell") then
if (count1 > 1 and count2 > 0) then
frame.count:SetText(displayCount1 .. "/" .. displayCount2)
elseif (count2 > 0) then
frame.count:SetText("/" .. displayCount2)
elseif (count1 > 0) then
frame.count:SetText(displayCount1)
else
frame.count:SetText("")
end
elseif (count1 > 1) then
frame.count:SetText(displayCount1)
else
frame.count:SetText("")
end
end
function AutoBar.Class.BasicButton:UpdateUsable()
local frame = self.frame
if not (frame and frame:IsShown()) then
return
end
local itemType = frame:GetAttribute("type")
if (not itemType) then
return
end
local isUsable
if (itemType == "item") then
local itemId = frame:GetAttribute("itemId")
isUsable = code.IsUsableItem(itemId)
if (isUsable) then
-- Single use in combat potion hack
local _, _, enabled = C_Container.GetItemCooldown(itemId)
if (not enabled) then
isUsable = false
end
end
elseif (itemType == "spell") then
local spellName = frame:GetAttribute("spell")
local notEnoughMana
isUsable, notEnoughMana = AB.IsUsableSpell(spellName)
isUsable = isUsable and (not notEnoughMana)
elseif (itemType == "macro") then
isUsable = true
else
frame.icon:SetVertexColor(1.0, 1.0, 1.0)
frame.hotKey:SetVertexColor(1.0, 1.0, 1.0)
return
end
if (isUsable) then
frame.icon:SetVertexColor(1.0, 1.0, 1.0)
frame.hotKey:SetVertexColor(1.0, 1.0, 1.0)
else
frame.icon:SetVertexColor(0.4, 0.4, 0.4)
end
end
local function scriptOnEvent(self, event, ...)
self.class[event](self.class, ...)
end
function AutoBar.Class.BasicButton:EventsEnable()
self.frame:SetScript("OnEvent", scriptOnEvent)
end