vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 button.lua
3 Scripts for Bongos Action Buttons, which add more features to blizzard action buttons
4  
5 Changes over the normal action buttons:
6 Colored red when out of range
7 Selfcast can be enabled
8 Altcast can be enabled
9 Hotkeys are shortened
10 Added the abiliy to skip pages when paging.
11 GetPagedID is flexible
12 --]]
13  
14 --local constants
15 local MAX_BUTTONS = 120; --the current maximum amount of action buttons
16  
17 local function ReuseAndRename(oldName, newName, newParent)
18 local button = getglobal(oldName);
19 button:SetParent(newParent);
20 button:SetAlpha(newParent:GetAlpha());
21 button:SetFrameLevel(0);
22 setglobal(newName, button);
23  
24 return button;
25 end
26  
27 BActionButton = {
28 --[[ Constructor Functions ]]--
29  
30 --Create an Action Button with the given ID and parent
31 Create = function(id, parent)
32 local name = "BActionButton" .. id;
33 local button;
34 if BongosSets.dontReuse then
35 button = CreateFrame("CheckButton", name, parent, "ActionButtonTemplate");
36 else
37 if id <= 12 then
38 button = ReuseAndRename("ActionButton" .. id, name, parent)
39 elseif id <= 24 then
40 button = ReuseAndRename("MultiBarBottomLeftButton" .. id-12, name, parent)
41 elseif id <= 36 then
42 button = ReuseAndRename("MultiBarBottomRightButton" .. id-24, name, parent)
43 elseif id <= 48 then
44 button = ReuseAndRename("MultiBarRightButton" .. id-36, name, parent)
45 elseif id <= 60 then
46 button = ReuseAndRename("MultiBarLeftButton" .. id-48, name, parent)
47 else
48 button = CreateFrame("CheckButton", name, parent, "ActionButtonTemplate");
49 end
50 end
51 button:SetID(id)
52  
53 getglobal(button:GetName() .. "Icon"):SetTexCoord(0.06,0.94,0.06,0.94);
54  
55 getglobal(button:GetName() .. "Border"):SetVertexColor(0, 1, 0, 0.6);
56  
57 if not BActionSets_HotkeysShown() then
58 getglobal(button:GetName() .. "HotKey"):Hide();
59 end
60  
61 if not BActionSets_MacrosShown() then
62 getglobal(button:GetName() .. "Name"):Hide();
63 end
64  
65 BActionButton.UpdateHotkey(button);
66 BActionButton.SetScripts(button);
67  
68 return button;
69 end,
70  
71 --Called after the button is created, sets all scripts and listeners
72 SetScripts = function(button)
73 button.flashing = 0;
74 button.flashtime = 0;
75  
76 button:RegisterForDrag("LeftButton", "RightButton");
77 button:RegisterForClicks("LeftButtonUp", "RightButtonUp");
78  
79 button:SetScript("OnUpdate", BActionButton.OnUpdate);
80 button:SetScript("OnClick", BActionButton.OnClick);
81 button:SetScript("OnDragStart", BActionButton.OnDragStart);
82 button:SetScript("OnReceiveDrag", BActionButton.OnReceiveDrag);
83 button:SetScript("OnEnter", BActionButton.OnEnter);
84 button:SetScript("OnLeave", BActionButton.OnLeave);
85 end,
86  
87 --[[ OnX Functions ]]--
88  
89 OnClick = function()
90 local pagedID = BActionButton.GetPagedID(this:GetID());
91  
92 if BActionSets_IsQuickMoveKeyDown() or bg_showGrid then
93 PickupAction(pagedID);
94 else
95 if MacroFrame_SaveMacro then
96 MacroFrame_SaveMacro();
97 end
98  
99 local selfCast;
100 if arg1 == "RightButton" then
101 selfCast = 1;
102 end
103 UseAction(pagedID, 0, selfCast);
104 end
105 BActionButton.UpdateState(this);
106 end,
107  
108 OnDragStart = function()
109 if not BActionSets_ButtonsLocked() or BActionSets_IsQuickMoveKeyDown() or bg_showGrid then
110 PickupAction(BActionButton.GetPagedID(this:GetID()));
111 BActionButton.UpdateState(this);
112 end
113 end,
114  
115 OnReceiveDrag = function()
116 PlaceAction(BActionButton.GetPagedID(this:GetID()));
117 BActionButton.UpdateState(this);
118 end,
119  
120 OnEnter = function()
121 BActionButton.UpdateTooltip(this);
122 end,
123  
124 OnLeave = function()
125 this.updateTooltip = nil;
126 GameTooltip:Hide();
127 end,
128  
129 --OnUpdate
130 OnUpdate = function()
131 --update flashing
132 if this.flashing == 1 then
133 this.flashtime = this.flashtime - arg1;
134 if this.flashtime <= 0 then
135 local overtime = -this.flashtime;
136 if overtime >= ATTACK_BUTTON_FLASH_TIME then
137 overtime = 0;
138 end
139 this.flashtime = ATTACK_BUTTON_FLASH_TIME - overtime;
140  
141 local flashTexture = getglobal(this:GetName().."Flash");
142 if flashTexture:IsShown() then
143 flashTexture:Hide();
144 else
145 flashTexture:Show();
146 end
147 end
148 end
149  
150 -- Handle range indicator
151 if this.rangeTimer then
152 if this.rangeTimer < 0 then
153 local pagedID = BActionButton.GetPagedID(this:GetID());
154 local hotkey = getglobal(this:GetName().."HotKey");
155  
156 if IsActionInRange(pagedID) == 0 then
157 hotkey:SetVertexColor(1, 0.1, 0.1);
158 if BActionSets_ColorOutOfRange() and IsUsableAction(pagedID) then
159 local rangeColor = BActionSets_GetRangeColor();
160 getglobal(this:GetName() .. "Icon"):SetVertexColor(rangeColor.r, rangeColor.g, rangeColor.b);
161 end
162 else
163 hotkey:SetVertexColor(0.6, 0.6, 0.6);
164 if IsUsableAction(pagedID) then
165 getglobal(this:GetName() .. "Icon"):SetVertexColor(1, 1, 1);
166 end
167 end
168 this.rangeTimer = TOOLTIP_UPDATE_TIME;
169 else
170 this.rangeTimer = this.rangeTimer - arg1;
171 end
172 end
173  
174 --Tooltip stuff, probably for the cooldown timer
175 if this.updateTooltip then
176 this.updateTooltip = this.updateTooltip - arg1;
177 if this.updateTooltip <= 0 then
178 if GameTooltip:IsOwned(this) then
179 BActionButton.UpdateTooltip(this);
180 else
181 this.updateTooltip = nil;
182 end
183 end
184 end
185 end,
186  
187 --[[ Update Functions ]]--
188  
189 --Updates the icon, count, cooldown, usability color, if the button is flashing, if the button is equipped, and macro text.
190 Update = function(button)
191 local pagedID = BActionButton.GetPagedID(button:GetID());
192 local buttonName = button:GetName();
193  
194 --if the button's not empty, then show the icon, else show an empty button
195 local texture = GetActionTexture(pagedID);
196 local icon = getglobal(buttonName.."Icon");
197 local buttonCooldown = getglobal(buttonName.."Cooldown");
198  
199 if texture then
200 icon:SetTexture(texture);
201 icon:Show();
202 button.rangeTimer = TOOLTIP_UPDATE_TIME;
203 button:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
204 getglobal(buttonName.."NormalTexture"):SetVertexColor(1, 1, 1, 1);
205 else
206 icon:Hide();
207 buttonCooldown:Hide();
208 button.rangeTimer = nil;
209 getglobal(buttonName.."HotKey"):SetVertexColor(0.6, 0.6, 0.6);
210 button:SetNormalTexture("Interface\\Buttons\\UI-Quickslot");
211 getglobal(buttonName.."NormalTexture"):SetVertexColor(1, 1, 1, 0.4);
212 end
213  
214 --update count
215 if IsConsumableAction(pagedID) then
216 getglobal(buttonName.."Count"):SetText(GetActionCount(pagedID));
217 else
218 getglobal(buttonName.."Count"):SetText("");
219 end
220  
221 --update cooldown/usability if there's an action
222 if HasAction(pagedID) then
223 button:Show();
224 BActionButton.UpdateUsable(button);
225 BActionButton.UpdateCooldown(button);
226 elseif not (bg_showGrid or BActionSets_ShowGrid()) then
227 button:Hide();
228 else
229 button:Show();
230 buttonCooldown:Hide();
231 end
232  
233 --Update if the button is flashing or not
234 BActionButton.UpdateFlash(button);
235  
236 -- Add a green border if the button is an equipped ite
237 local border = getglobal(buttonName.."Border");
238 if IsEquippedAction(pagedID) then
239 border:Show();
240 else
241 border:Hide();
242 end
243  
244 if GameTooltip:IsOwned(button) then
245 BActionButton.UpdateTooltip(button);
246 else
247 button.updateTooltip = nil;
248 end
249  
250 -- Update Macro Text
251 getglobal(buttonName.."Name"):SetText(GetActionText(pagedID));
252 end,
253  
254 --colors the action button if out of mana, out of range, etc
255 UpdateUsable = function(button)
256 local pagedID = BActionButton.GetPagedID(button:GetID());
257 local icon = getglobal(button:GetName().."Icon");
258  
259 local isUsable, notEnoughMana = IsUsableAction(pagedID);
260 if isUsable then
261 if BActionSets_ColorOutOfRange() and IsActionInRange(pagedID) == 0 then
262 local rangeColor = BActionSets_GetRangeColor();
263 icon:SetVertexColor(rangeColor.r, rangeColor.g, rangeColor.b);
264 else
265 icon:SetVertexColor(1,1,1);
266 end
267 elseif notEnoughMana then
268 --Make the icon blue if out of mana
269 icon:SetVertexColor(0.5, 0.5, 1);
270 else
271 --Skill unusable
272 icon:SetVertexColor(0.3, 0.3, 0.3);
273 end
274 end,
275  
276 --Update the cooldown timer
277 UpdateCooldown = function(button)
278 local start, duration, enable = GetActionCooldown(BActionButton.GetPagedID(button:GetID()));
279 CooldownFrame_SetTimer(getglobal(button:GetName().."Cooldown"), start, duration, enable);
280 end,
281  
282 --Update if a button is checked or not
283 UpdateState = function(button)
284 local pagedID = BActionButton.GetPagedID(button:GetID());
285 button:SetChecked(IsCurrentAction(pagedID) or IsAutoRepeatAction(pagedID));
286 end,
287  
288 --Update a a flashing button
289 UpdateFlash = function(button)
290 local pagedID = BActionButton.GetPagedID(button:GetID());
291 if (IsCurrentAction(pagedID) and IsAttackAction(pagedID)) or IsAutoRepeatAction(pagedID) then
292 button.flashing = 1;
293 button.flashtime = 0;
294 else
295 button.flashing = nil;
296 getglobal(button:GetName().."Flash"):Hide();
297 end
298 BActionButton.UpdateState(button);
299 end,
300  
301 UpdateTooltip = function(button)
302 if BActionSets_TooltipsShown() then
303 if GetCVar("UberTooltips") == "1" then
304 GameTooltip_SetDefaultAnchor(GameTooltip, button);
305 else
306 GameTooltip:SetOwner(button, "ANCHOR_RIGHT");
307 end
308  
309 if GameTooltip:SetAction(BActionButton.GetPagedID(button:GetID())) then
310 button.updateTooltip = TOOLTIP_UPDATE_TIME;
311 else
312 button.updateTooltip = nil;
313 end
314 end
315 end,
316  
317 UpdateHotkey = BBasicActionButton.UpdateHotkey,
318  
319 --next two are basically wrapper functions for use with ForAllButtons
320 UpdateUsableAndCooldown = function(button)
321 BActionButton.UpdateUsable(button);
322 BActionButton.UpdateCooldown(button);
323 end,
324  
325 UpdateAll = function(button)
326 BActionButton.Update(button);
327 BActionButton.UpdateState(button);
328 end,
329  
330 --hides a button, if its currently not attached to any actionbar
331 ShowIfUsed = function(button)
332 if button:GetParent() then
333 BActionButton.UpdateAll(button);
334 if not button:GetParent():IsShown() then
335 button:Hide();
336 end
337 else
338 button:Hide();
339 end
340 end,
341  
342 --[[ Toggles ]]--
343  
344 --Show Empty Buttons
345 ShowGrid = function(button)
346 button:Show();
347 end,
348  
349 --Hide Empty Buttons
350 HideGrid = function(button)
351 if not HasAction(BActionButton.GetPagedID(button:GetID())) then
352 button:Hide();
353 end
354 end,
355  
356 ShowMacroText = function(button, enable)
357 if enable then
358 getglobal(button:GetName().."Name"):Show();
359 else
360 getglobal(button:GetName().."Name"):Hide();
361 end
362 end,
363  
364 --[[ Action ID Functions ]]--
365  
366 --returns the barID of the button's parent
367 GetParent = function(buttonID)
368 local button = getglobal("BActionButton" .. buttonID);
369 if button and button:GetParent() then
370 return button:GetParent().id;
371 end
372  
373 local numActionBars = BActionBar.GetNumber();
374 local maxButtonsPerBar = math.floor(MAX_BUTTONS / numActionBars);
375 for i = 1, numActionBars do
376 if buttonID <= maxButtonsPerBar * i then
377 return i;
378 end
379 end
380  
381 return nil;
382 end,
383  
384 --Paging, this extends it to page 10 and allows page skipping
385 GetPagedID = function(buttonID, barID)
386 if not barID then
387 barID = BActionButton.GetParent(buttonID);
388 if not barID then
389 return buttonID;
390 end
391 end
392  
393 --normal paging
394 local pageOffset = BActionBar.GetPage(barID);
395 if pageOffset ~= 0 then
396 local pagedID = mod(buttonID + pageOffset * BActionBar.GetSize(barID), MAX_BUTTONS);
397 if pagedID == 0 then
398 return MAX_BUTTONS;
399 end
400 return pagedID;
401 end
402  
403 --stance paging - (bear form, fury stance, etc)
404 local stanceOffset = BActionBar.GetStance(barID);
405 if stanceOffset ~= 0 then
406 local pagedID = mod(buttonID + stanceOffset * BActionBar.GetSize(barID), MAX_BUTTONS);
407 if pagedID == 0 then
408 return MAX_BUTTONS;
409 end
410 return pagedID;
411 end
412  
413 --Contextual paging - targeting a friendly unit or something
414 local contextOffset = BActionBar.GetContext(barID)
415 if contextOffset ~= 0 then
416 local pagedID = mod(buttonID + contextOffset * BActionBar.GetSize(barID), MAX_BUTTONS);
417 if pagedID == 0 then
418 return MAX_BUTTONS;
419 end
420 return pagedID;
421 end
422  
423 return buttonID;
424 end,
425 }
426  
427 --[[ Function Hooks and Overrides ]]--
428  
429 local oUseAction = UseAction;
430 UseAction = function(id, s, onSelf)
431 if BActionSets_IsSelfCastHotkeyDown() then
432 oUseAction(id, s, 1);
433 if SpellIsTargeting() then
434 SpellTargetUnit("player");
435 end
436 else
437 oUseAction(id, s, onSelf);
438 if SpellIsTargeting() and onSelf then
439 SpellTargetUnit("player");
440 end
441 end
442 end
443  
444 --Release button, do action
445 ActionButtonUp = function(id, onSelf)
446 local button = getglobal("BActionButton".. id);
447  
448 -- Used to save a macro
449 if MacroFrame_SaveMacro then
450 MacroFrame_SaveMacro();
451 end
452  
453 local pagedID = BActionButton.GetPagedID(id);
454  
455 if button and button:GetButtonState() == "PUSHED" then
456 button:SetButtonState("NORMAL");
457 button:SetChecked(IsCurrentAction(pagedID));
458 end
459 UseAction(pagedID, 0, onSelf);
460 end
461  
462 --Press button
463 ActionButtonDown = function(id)
464 local button = getglobal("BActionButton".. id);
465  
466 if button and button:GetButtonState() == "NORMAL" then
467 button:SetButtonState("PUSHED");
468 end
469 end