-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZSpellButtonTemplate.lua
executable file
·295 lines (279 loc) · 7.89 KB
/
ZSpellButtonTemplate.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
-- MAX_SPELLS = 1024
function ZSpellButton_SetButtons(parent, spells)
if not (parent and parent.count and parent.name and spells) then return end
parent.spells = spells
if not GetSpellName(1, "spell") then
parent:RegisterEvent("SPELLS_CHANGED")
return -1
end
local spell, finished, temp
local info = {}
-- Find All Spell IDs
for i=1, MAX_SPELLS do
spell = GetSpellName(i, "spell")
if not spell or finished then
break
end
finished = 1
for i2=1, table.getn(spells) do
if not info[i2] then
finished = nil
if spells[i2] == spell then
temp = i
while spells[i2] == spell do
temp = temp + 1
spell = GetSpellName(temp, "spell")
end
info[i2] = (temp-1)
break
end
end
end
end
-- Hide All Children
for i=1, parent.count do
getglobal(parent.name..i):Hide()
end
local button
local count = 1
-- Set Children IDs
for i, v in info do
if count > parent.count then break end
button = getglobal(parent.name..count)
button.id = v
ZSpellButton_UpdateButton(button)
button:Show()
-- Set The Parent To The First Spell
if count == 1 then
parent.id = v
ZSpellButton_UpdateButton(parent)
parent:Enable()
end
count = count + 1
end
return count - 1
end
function ZSpellButton_SetExpandDirection(parent, direction)
local button = getglobal(parent.name.."1")
local offset = parent:GetWidth() / 36
if parent.circle:IsVisible() then
offset = offset * 5
else
offset = offset * 3
end
button:ClearAllPoints()
if direction == "TOP" then
button:SetPoint("BOTTOM", parent, "TOP", 0, offset)
elseif direction == "BOTTOM" then
button:SetPoint("TOP", parent, "BOTTOM", 0, -1 * offset)
elseif direction == "LEFT" then
button:SetPoint("RIGHT", parent, "LEFT", -1 * offset, 0)
elseif direction == "RIGHT" then
button:SetPoint("LEFT", parent, "RIGHT", offset, 0)
end
end
function ZSpellButton_ArrangeChildren(parent, rows, count, horizontal, vertical)
if not (parent and parent.count and parent.name) then return end
local left, right, top, bottom = "LEFT", "RIGHT", "TOP", "BOTTOM"
local xoffset = parent:GetWidth() / 36
xoffset = xoffset * 3
local yoffset = -1 * xoffset
if horizontal then
left, right = "RIGHT", "LEFT"
xoffset = xoffset * -1
end
if vertical then
top, bottom = "BOTTOM", "TOP"
yoffset = yoffset * -1
end
if count > parent.count then
count = parent.count
end
local cols = ceil(count / rows)
local temp = 1
local button
for i=2, parent.count do
button = getglobal(parent.name..i)
if i > count then
button:Hide()
else
if button.id then
button:Show()
end
button:ClearAllPoints()
if temp >= cols then
button:SetPoint(top, getglobal(parent.name..(i-temp)), bottom, 0, yoffset)
temp = 1
else
button:SetPoint(left, getglobal(parent.name..(i-1)), right, xoffset, 0)
temp = temp + 1
end
end
end
end
function ZSpellButton_SetSize(parent, size, setChildren)
local bgscale = 64 / 36
local cdscale = 0.75 / 36
if setChildren then
local button
for i=1, parent.count do
button = getglobal(parent.name..i)
button:SetHeight(size)
button:SetWidth(size)
button.background:SetWidth(size * bgscale)
button.background:SetHeight(size * bgscale)
button.cooldown:SetModelScale(size * cdscale)
end
else
local cscale = 100/36
local cxscale = 20 / 36
local cyscale = -22 / 36
parent:SetHeight(size)
parent:SetWidth(size)
parent.background:SetWidth(size * bgscale)
parent.background:SetHeight(size * bgscale)
parent.cooldown:SetModelScale(size * cdscale)
parent.circle:SetWidth(size * cscale)
parent.circle:SetHeight(size * cscale)
parent.circle:SetPoint("CENTER", size * cxscale, size * cyscale)
end
end
function ZSpellButton_UpdateButton(button)
if not button then
button = this
end
if not (button.id and button.icontexture) then return end
local texture = button.icon or GetSpellTexture(button.id, "spell")
button.icontexture:SetTexture(texture)
button.icontexture:Show()
end
function ZSpellButton_UpdateCooldown(button)
if not button then
button = this
end
if not button.id then return end
local start, duration, enable = GetSpellCooldown(button.id, "spell")
CooldownFrame_SetTimer(button.cooldown, start, duration, enable)
local r, g, b = 1.0, 1.0, 1.0
if enable ~= 1 then
r, g, b = 0.4, 0.4, 0.4
end
button.icontexture:SetVertexColor(r, g, b)
end
function ZSpellButton_CreateChildren(parent, name, count)
if not (parent and name and count) then return end
parent.children = CreateFrame("Frame", parent:GetName().."Children", UIParent)
parent.count = count
parent.name = name
local button
for i=1, count do
button = CreateFrame("CheckButton", parent.name..i, parent.children, "ZSpellButtonChildTemplate")
button.parent = parent
end
end
function ZSpellButton_OnLoad()
this:RegisterEvent("SPELL_UPDATE_COOLDOWN")
this:RegisterForClicks("LeftButtonUp", "RightButtonUp")
local name = this:GetName()
this.icontexture = getglobal(name.."IconTexture")
this.cooldown = getglobal(name.."Cooldown")
this.background = getglobal(name.."Background")
end
function ZSpellButton_OnEvent()
if event == "SPELL_UPDATE_COOLDOWN" then
ZSpellButton_UpdateCooldown()
end
end
function ZSpellButton_OnEnter()
if this.parent.tooltip then
GameTooltip:SetOwner(this, "ANCHOR_TOPRIGHT")
GameTooltip:SetSpell(this.id, "spell")
end
end
function ZSpellButton_OnClick()
if type(this.parent.beforeclick) == "function" then
this.parent.beforeclick(this)
end
CastSpell(this.id, "spell")
this:SetChecked(0)
if this.parent.hideonclick then
this.parent.children:Hide()
end
if type(this.parent.afterclick) == "function" then
this.parent.afterclick(this)
end
end
function ZSpellButtonParent_OnLoad()
this:RegisterEvent("SPELL_UPDATE_COOLDOWN")
this:RegisterEvent("LEARNED_SPELL_IN_TAB")
this:RegisterForDrag("LeftButton")
this:RegisterForClicks("LeftButtonUp", "RightButtonUp")
local name = this:GetName()
this.icontexture = getglobal(name.."IconTexture")
this.cooldown = getglobal(name.."Cooldown")
this.background = getglobal(name.."Background")
this.circle = getglobal(name.."Circle")
this:Disable()
end
function ZSpellButtonParent_OnEvent()
if event == "SPELL_UPDATE_COOLDOWN" then
ZSpellButton_UpdateCooldown()
elseif event == "LEARNED_SPELL_IN_TAB" then
if this.name and this.count and this.spells then
ZSpellButton_SetButtons(this, this.spells)
end
elseif event == "SPELLS_CHANGED" then
this.found = ZSpellButton_SetButtons(this, this.spells)
if this.found == 0 then
this:Hide()
end
if this.found > -1 then
this:UnregisterEvent("SPELLS_CHANGED")
end
end
end
function ZSpellButtonParent_OnEnter(frame)
if not frame then
frame = this
end
if frame.tooltip and frame.id then
GameTooltip:SetOwner(frame, "ANCHOR_TOPLEFT")
local msg, rank = GetSpellName(frame.id, "spell")
if string.len(rank) > 0 then
msg = msg.." ("..rank..")"
end
GameTooltip:SetText(msg, 1, 1, 1)
GameTooltip:AddLine("Alt+Drag To Move This Button")
if frame.options then
GameTooltip:AddLine("Control+Alt+Shift+Click For Options")
end
GameTooltip:Show()
end
end
function ZSpellButtonParent_OnClick()
if IsControlKeyDown() and IsAltKeyDown() and IsShiftKeyDown() and this.options then
if this.options:IsVisible() then
this.options:Hide()
else
this.options:Show()
end
elseif arg1 == "RightButton" then
if this.children:IsVisible() then
this.children:Hide()
else
this.children:Show()
end
else
if type(this.beforeclick) == "function" then
this.beforeclick(this)
end
CastSpell(this.id, "spell")
if this.hideonclick then
this.children:Hide()
end
if type(this.afterclick) == "function" then
this.afterclick(this)
end
end
this:SetChecked(0)
end