/
Button.lua
292 lines (233 loc) · 8.34 KB
/
Button.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
local myname, ns = ...
ns.BINDING_OFFSET = 27
local _G = _G
local function noop() end
local colors = {
none = {1.0, 1.0, 1.0},
grey = {0.4, 0.4, 0.4},
blue = {0.1, 0.3, 1.0},
red = {0.8, 0.1, 0.1},
}
local events = {
"ACTIONBAR_UPDATE_COOLDOWN",
"ACTIONBAR_UPDATE_STATE",
"CRAFT_CLOSE",
"CRAFT_SHOW",
"PLAYER_AURAS_CHANGED",
"PLAYER_ENTER_COMBAT",
"PLAYER_LEAVE_COMBAT",
"SPELL_UPDATE_CHARGES",
"START_AUTOREPEAT_SPELL",
"STOP_AUTOREPEAT_SPELL",
"TRADE_SKILL_CLOSE",
"TRADE_SKILL_SHOW",
"UPDATE_INVENTORY_ALERTS",
}
local function UpdateCooldown(self)
ActionButton_UpdateCooldown(self)
end
local function ActionButton_UpdateState(self)
self:SetChecked(IsCurrentAction(self.action) or IsAutoRepeatAction(self.action))
end
local function ActionButton_UpdateFlash(self)
if IsAttackAction(self.action) and IsCurrentAction(self.action) or IsAutoRepeatAction(self.action) then
self.flashtime = 0
ActionButton_UpdateState(self)
else
self.flashtime = nil
self.flash:Hide()
ActionButton_UpdateState(self)
end
end
local function GetChargesText(self)
local charges, maxCharges = GetActionCharges(self.action)
if maxCharges > 1 then return charges end
return ""
end
local function ActionButton_Update(self)
local texture = GetActionTexture(self.action)
self.icon:SetTexture(texture)
if texture then
self.icon:Show()
self:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
else
self.icon:Hide()
self.cooldown:Hide()
self:SetNormalTexture("Interface\\Buttons\\UI-Quickslot")
end
ActionButton_UpdateCount(self)
if HasAction(self.action) then
if not self.eventsRegistered then
for _,event in pairs(events) do self:RegisterEvent(event) end
self.eventsRegistered = 1
end
ActionButton_UpdateState(self)
CooldownFrame_Set(self.cooldown, GetActionCooldown(self.action))
ActionButton_UpdateFlash(self)
else
self:SetChecked(false)
if self.eventsRegistered then
for _,event in pairs(events) do self:UnregisterEvent(event) end
self.eventsRegistered = nil
end
end
-- Add a green border if button is an equipped item
if IsEquippedAction(self.action) then
self.border:SetVertexColor(0, 1.0, 0, 0.35)
self.border:Show()
else
self.border:Hide()
end
-- Update tooltip
local cd = GetActionCooldown(self.action)
if GameTooltip:GetOwner() == self and (self.wascd or cd ~= 0) then ActionButton_SetTooltip(self) end
self.feedback_action = self.action
self.wascd = cd ~= 0
end
local function OnUpdate(self, elapsed, ...)
self = self.owner
local id = SecureButton_GetModifiedAttribute(self, "*action*", SecureButton_GetEffectiveButton(self))
if not id then return end
if self.cachedaction ~= id then
self.action, self.cachedaction = id, id
ActionButton_Update(self)
end
local oor, isUsable, notEnoughMana = IsActionInRange(id), IsUsableAction(id)
local c = notEnoughMana and "blue" or oor == false and "red" or isUsable and "none" or "grey"
self.icon:SetVertexColor(unpack(colors[c]))
if HasAction(self.action) and self.flashtime then
self.flashtime = self.flashtime - elapsed
if self.flashtime <= 0 then
local overtime = -self.flashtime
if overtime >= ATTACK_BUTTON_FLASH_TIME then overtime = 0 end
self.flashtime = ATTACK_BUTTON_FLASH_TIME - overtime
if self.flash:IsShown() then self.flash:Hide() else self.flash:Show() end
end
else self.flash:Hide() end
end
local function OnReceiveDrag(self)
PlaceAction(self.action)
ActionButton_UpdateState(self)
ActionButton_UpdateFlash(self)
end
local function SetTooltip(frame)
local id = SecureButton_GetModifiedAttribute(self, "action", SecureButton_GetEffectiveButton(self))
if not id then return end
GameTooltip:SetOwner(frame, "ANCHOR_RIGHT")
GameTooltip:SetAction(id)
end
local function HideTooltip(frame)
GameTooltip:Hide()
end
local function OnDragStart(self)
if LOCK_ACTIONBAR ~= "1" or IsModifiedClick("PICKUPACTION") then
PickupAction(self.action)
ActionButton_UpdateState(self)
ActionButton_UpdateFlash(self)
end
end
local function ActionButton_OnEvent(self, event, action)
if event == "UPDATE_BINDINGS" then
self:UnregisterEvent("UPDATE_BINDINGS")
ns.SetBinding(self.action)
return
end
if event == "ACTIONBAR_UPDATE_COOLDOWN" then return UpdateCooldown(self) end
if event == "ACTIONBAR_SLOT_CHANGED" and (action ~= 0 or action == self.action) or event == "PLAYER_ENTERING_WORLD" then return ActionButton_Update(self) end
-- All event handlers below this line are only set when the button has an action
if event == "ACTIONBAR_UPDATE_STATE" or event == "CRAFT_SHOW" or event == "CRAFT_CLOSE" or event == "TRADE_SKILL_SHOW" or event == "TRADE_SKILL_CLOSE" then
return ActionButton_UpdateState(self)
elseif event == "PLAYER_ENTER_COMBAT" and IsAttackAction(self.action) or event == "START_AUTOREPEAT_SPELL" and IsAutoRepeatAction(self.action) then
self.flashtime = 0
return ActionButton_UpdateState(self)
elseif event == "PLAYER_LEAVE_COMBAT" and IsAttackAction(self.action) or event == "STOP_AUTOREPEAT_SPELL" and self.flashtime and not IsAttackAction(self.action) then
self.flashtime = nil
self.flash:Hide()
return ActionButton_UpdateState(self)
end
if event == "SPELL_UPDATE_CHARGES" then
ActionButton_UpdateCount(self)
end
if event == "SPELL_ACTIVATION_OVERLAY_GLOW_SHOW" then
local actionType, id, subType = GetActionInfo(self.action)
if actionType == "spell" and id == action then
ActionButton_ShowOverlayGlow(self)
elseif actionType == "macro" then
local _, _, spellId = GetMacroSpell(id)
if spellId and spellId == action then
ActionButton_ShowOverlayGlow(self)
end
elseif actionType == "flyout" and FlyoutHasSpell(id, action) then
ActionButton_ShowOverlayGlow(self)
end
elseif event == "SPELL_ACTIVATION_OVERLAY_GLOW_HIDE" then
local actionType, id, subType = GetActionInfo(self.action)
if actionType == "spell" and id == action then
ActionButton_HideOverlayGlow(self)
elseif actionType == "macro" then
local _, _, spellId = GetMacroSpell(id)
if spellId and spellId == action then
ActionButton_HideOverlayGlow(self)
end
elseif actionType == "flyout" and FlyoutHasSpell(id, action) then
ActionButton_HideOverlayGlow(self)
end
end
end
function ns.factory(name, parent, inherit)
inherit = inherit and "SecureActionButtonTemplate,"..inherit or "SecureActionButtonTemplate"
local b = CreateFrame("CheckButton", name, parent, inherit)
b:SetWidth(36) b:SetHeight(36)
b:Show()
local updater = CreateFrame("frame", nil, b)
updater.owner = b
updater:SetScript("OnUpdate", OnUpdate)
b:SetScript("PostClick", ActionButton_UpdateState)
b:SetScript("OnEvent", ActionButton_OnEvent)
b:SetScript("OnDragStart", OnDragStart)
b:SetScript("OnReceiveDrag", OnReceiveDrag)
b:HookScript("OnEnter", ActionButton_SetTooltip)
b:HookScript("OnLeave", HideTooltip)
b:RegisterEvent("PLAYER_ENTERING_WORLD")
b:RegisterEvent("ACTIONBAR_SLOT_CHANGED")
b:RegisterEvent("SPELL_ACTIVATION_OVERLAY_GLOW_SHOW")
b:RegisterEvent("SPELL_ACTIVATION_OVERLAY_GLOW_HIDE")
b:RegisterForDrag("LeftButton", "RightButton")
b:RegisterForClicks("AnyUp")
b:SetAttribute("type", "action")
b:SetAttribute("checkselfcast", true)
b:SetAttribute("useparent-unit", true)
b:SetAttribute("useparent-actionpage", true)
b.icon = b:CreateTexture(nil, "BACKGROUND")
b.icon:SetAllPoints()
b.flash = b:CreateTexture(nil, "ARTWORK")
b.flash:SetAllPoints()
b.flash:SetTexture("Interface\\Buttons\\UI-QuickslotRed")
b.flash:Hide()
b.count = b:CreateFontString(nil, "ARTWORK", "NumberFontNormal")
b.count:SetJustifyH("RIGHT")
b.count:SetPoint("BOTTOMRIGHT", -2, 2)
b.border = b:CreateTexture(nil, "OVERLAY")
b.border:SetWidth(62) b.border:SetHeight(62)
b.border:SetPoint("CENTER")
b.border:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
b.border:SetBlendMode("ADD")
b.border:Hide()
b.cooldown = CreateFrame("Cooldown", nil, b, "CooldownFrameTemplate")
b.cooldown:SetWidth(36) b.cooldown:SetHeight(36)
b.cooldown:SetPoint("CENTER", 0, -1)
b:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
b:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
b:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square") --ADD
b:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight") --ADD
b.action = ActionButton_CalculateAction(b)
SetActionUIButton(b, b.action, b.cooldown)
ActionButton_Update(b)
-- Cleanup
local icon = name and _G[name.."Icon"]
if icon then
icon:Hide()
icon.Show = icon.Hide
end
return b
end