Mercurial > wow > reaction
comparison Button.lua @ 2:8e0ff8ae4c08
Version 0.2
| author | Flick <flickerstreak@gmail.com> |
|---|---|
| date | Tue, 20 Mar 2007 21:08:31 +0000 |
| parents | c11ca1d8ed91 |
| children |
comparison
equal
deleted
inserted
replaced
| 1:c11ca1d8ed91 | 2:8e0ff8ae4c08 |
|---|---|
| 1 -- private constants | 1 -- private constants |
| 2 local namePrefix = "ReActionButton_" | 2 local namePrefix = "ReActionButton" |
| 3 local _G = getfenv(0) | 3 local _G = getfenv(0) |
| 4 local ACTION_FREE = { } | 4 local ACTION_FREE = { } |
| 5 local MAX_ACTIONS = 120 | 5 local MAX_ACTIONS = 120 |
| 6 | 6 |
| 7 local hotKeyDefaultColor = { r=1.0, g=1.0, b=1.0, a=1.0 } | 7 local hotKeyDefaultColor = { r=1.0, g=1.0, b=1.0, a=1.0 } |
| 10 | 10 |
| 11 local hotKeyModifierColors = { | 11 local hotKeyModifierColors = { |
| 12 S = { r=0.6, g=0.6, b=1.0, a=1.0 }, -- shift | 12 S = { r=0.6, g=0.6, b=1.0, a=1.0 }, -- shift |
| 13 C = { r=1.0, g=0.82, b=0, a=1.0 }, -- ctrl | 13 C = { r=1.0, g=0.82, b=0, a=1.0 }, -- ctrl |
| 14 A = { r=0.1, g=1.0, b=0.1, a=1.0 }, -- alt | 14 A = { r=0.1, g=1.0, b=0.1, a=1.0 }, -- alt |
| 15 } | |
| 16 | |
| 17 -- TODO: localize these key names with GetBindingText(KEY_) | |
| 18 local keybindAbbreviations = { | |
| 19 ["Mouse Button "] = "M-", | |
| 20 ["Spacebar"] = "Sp", | |
| 21 ["Num Pad "] = "Num-", | |
| 22 ["Page Up"] = "PgUp", | |
| 23 ["Page Down"] = "PgDn", | |
| 24 [" Arrow"] = "", | |
| 15 } | 25 } |
| 16 | 26 |
| 17 local equippedActionBorderColor = { r=0, g=1.0, b=0, a=0.35 } | 27 local equippedActionBorderColor = { r=0, g=1.0, b=0, a=0.35 } |
| 18 | 28 |
| 19 local actionUsableColor = { r=1.0, g=1.0, b=1.0, a=1.0 } | 29 local actionUsableColor = { r=1.0, g=1.0, b=1.0, a=1.0 } |
| 58 end | 68 end |
| 59 local t = actionButtonTbl[id] | 69 local t = actionButtonTbl[id] |
| 60 | 70 |
| 61 t.inUse = true | 71 t.inUse = true |
| 62 if t.button then | 72 if t.button then |
| 63 t.button:Configure(parent,config,barIdx,id) | 73 t.button:Configure(parent,config,barIdx) |
| 64 else | 74 else |
| 65 t.button = self:new(parent,config,barIdx,id) | 75 t.button = self:new(parent,config,barIdx,id) |
| 66 end | 76 end |
| 67 | 77 |
| 68 if actionButtonTbl[t.button:GetActionID()].inUse ~= true then | 78 -- fix screwy config with overlapping IDs |
| 69 end | 79 config.actionIDs[barIdx] = id |
| 70 | |
| 71 return t.button | 80 return t.button |
| 72 end | 81 end |
| 73 | 82 |
| 74 function ReActionButton:release( b ) | 83 function ReActionButton:release( b ) |
| 75 if b then | 84 if b then |
| 98 ---------------------- | 107 ---------------------- |
| 99 function ReActionButton.prototype:init( parentFrame, config, barIdx, id ) | 108 function ReActionButton.prototype:init( parentFrame, config, barIdx, id ) |
| 100 ReActionButton.super.prototype.init(self) | 109 ReActionButton.super.prototype.init(self) |
| 101 | 110 |
| 102 -- create the button widget | 111 -- create the button widget |
| 103 self.name = namePrefix.."_"..id | 112 self.name = namePrefix..id |
| 104 self.button = CreateFrame("CheckButton", self.name, parentFrame, "ReActionButtonTemplate") | 113 self.button = CreateFrame("CheckButton", self.name, parentFrame, "ReActionButtonTemplate") |
| 105 | 114 |
| 106 -- create the actionID label on the control widget | |
| 107 local actionIDLabel = self.button:CreateFontString(nil,"ARTWORK", "NumberFontNormalSmall") | |
| 108 actionIDLabel:SetPoint("BOTTOMLEFT",0,0) | |
| 109 actionIDLabel:Hide() | |
| 110 | |
| 111 -- store references to the various sub-frames so we don't have to look it up all the time | 115 -- store references to the various sub-frames so we don't have to look it up all the time |
| 112 self.frames = { | 116 self.frames = { |
| 113 hotkey = _G[self.name.."HotKey"], | 117 hotkey = _G[self.name.."HotKey"], |
| 114 count = _G[self.name.."Count"], | 118 count = _G[self.name.."Count"], |
| 115 cooldown = _G[self.name.."Cooldown"], | 119 cooldown = _G[self.name.."Cooldown"], |
| 117 macro = _G[self.name.."Name"], | 121 macro = _G[self.name.."Name"], |
| 118 icon = _G[self.name.."Icon"], | 122 icon = _G[self.name.."Icon"], |
| 119 border = _G[self.name.."Border"], | 123 border = _G[self.name.."Border"], |
| 120 normalTexture = _G[self.name.."NormalTexture"], | 124 normalTexture = _G[self.name.."NormalTexture"], |
| 121 flash = _G[self.name.."Flash"], | 125 flash = _G[self.name.."Flash"], |
| 122 actionID = actionIDLabel | 126 actionID = _G[self.name.."ActionID"], |
| 123 } | 127 } |
| 124 | 128 |
| 125 -- provide a reference back to this object for the frame to use in event handlers | 129 -- provide a reference back to this object for the frame to use in event handlers |
| 126 self.button.rxnBtn = self | 130 self.button.rxnBtn = self |
| 127 | 131 |
| 132 -- set the action ID | |
| 133 self:SetActionID(id) | |
| 134 | |
| 135 -- register button with ReBinder for keybinding | |
| 136 ReBinder:AddKeybindTarget(self.button) | |
| 137 | |
| 128 -- initialize | 138 -- initialize |
| 129 self:Configure(parentFrame, config, barIdx, id) | 139 self:Configure(parentFrame, config, barIdx) |
| 140 end | |
| 141 | |
| 142 local function tcopy(t) | |
| 143 local r = { } | |
| 144 for k, v in pairs(t) do | |
| 145 r[k] = (type(v) == "table" and tcopy(v) or v) | |
| 146 end | |
| 147 return r | |
| 130 end | 148 end |
| 131 | 149 |
| 132 function ReActionButton.prototype:Recycle() | 150 function ReActionButton.prototype:Recycle() |
| 133 local b = self.button | 151 local b = self.button |
| 134 local action = self:GetActionID() | |
| 135 | |
| 136 self.config.actionIDs[self.barIdx] = nil | |
| 137 | 152 |
| 138 self:SetKeyBinding(nil) | 153 self:SetKeyBinding(nil) |
| 139 self:UpdateDisplay() | 154 self:UpdateDisplay() |
| 140 b:UnregisterAllEvents() | 155 b:UnregisterAllEvents() |
| 156 b:SetParent(ReActionButtonRecycleFrame) | |
| 157 b:ClearAllPoints() | |
| 158 b:SetPoint("TOPLEFT",0,0) | |
| 141 b:Hide() | 159 b:Hide() |
| 142 b:ClearAllPoints() | 160 self.config = tcopy(self.config) -- ew, but necessary |
| 143 b:SetParent(ReActionButtonRecycleFrame) | 161 end |
| 144 end | 162 |
| 145 | 163 function ReActionButton.prototype:BarUnlocked() |
| 164 self:ShowGridTmp() | |
| 165 end | |
| 166 | |
| 167 function ReActionButton.prototype:BarLocked() | |
| 168 self:HideGridTmp() | |
| 169 end | |
| 146 | 170 |
| 147 | 171 |
| 148 -- set the button location | 172 -- set the button location |
| 149 function ReActionButton.prototype:PlaceButton(point, x, y, sz) | 173 function ReActionButton.prototype:PlaceButton(point, x, y, sz) |
| 150 local b = self.button | 174 local b = self.button |
| 164 end | 188 end |
| 165 | 189 |
| 166 | 190 |
| 167 | 191 |
| 168 -- configuration and setup | 192 -- configuration and setup |
| 169 function ReActionButton.prototype:Configure( parentFrame, config, barIdx, id ) | 193 function ReActionButton.prototype:Configure( parentFrame, config, barIdx ) |
| 170 self.config = config | 194 self.config = config |
| 171 self.barIdx = barIdx | 195 self.barIdx = barIdx |
| 172 self.showGrid = config.showGrid and 1 or 0 | 196 self.showGridTmp_ = 0 |
| 173 | 197 |
| 174 self.button:ClearAllPoints() | 198 self.button:ClearAllPoints() |
| 175 self.button:SetParent(parentFrame) | 199 self.button:SetParent(parentFrame) |
| 176 | 200 |
| 177 self:SetupAttributes() | 201 self:SetupAttributes() |
| 178 self:RegisterStaticEvents() | 202 self:RegisterStaticEvents() |
| 179 | 203 |
| 180 if id then | |
| 181 -- set action ID | |
| 182 self:SetActionID(id) | |
| 183 else | |
| 184 self:ApplyActionID() | |
| 185 end | |
| 186 self:ApplyLayout() | 204 self:ApplyLayout() |
| 187 self:ApplyStyle() | 205 self:ApplyStyle() |
| 188 | 206 |
| 189 self:UpdateDisplay() | 207 self:UpdateDisplay() |
| 190 end | 208 end |
| 191 | 209 |
| 192 function ReActionButton.prototype:SetupAttributes() | 210 function ReActionButton.prototype:SetupAttributes() |
| 193 local b = self.button | 211 local b = self.button |
| 194 b:SetAttribute("type", "action") | 212 b:SetAttribute("type", "action") |
| 195 b:SetAttribute("shift-type*", ATTRIBUTE_NOOP) | 213 b:SetAttribute("shift-type*", ATTRIBUTE_NOOP) |
| 196 b:SetAttribute("alt-type*", ATTRIBUTE_NOOP) | |
| 197 b:SetAttribute("checkselfcast", true) | 214 b:SetAttribute("checkselfcast", true) |
| 198 b:SetAttribute("useparent-unit", true) | 215 b:SetAttribute("useparent-unit", true) |
| 199 end | 216 end |
| 200 | 217 |
| 201 function ReActionButton.prototype:RegisterStaticEvents() | 218 function ReActionButton.prototype:RegisterStaticEvents() |
| 382 | 399 |
| 383 function ReActionButton.prototype:GetKeyBinding() | 400 function ReActionButton.prototype:GetKeyBinding() |
| 384 return GetBindingKey("CLICK "..self.name..":LeftButton") | 401 return GetBindingKey("CLICK "..self.name..":LeftButton") |
| 385 end | 402 end |
| 386 | 403 |
| 387 function ReActionButton.prototype:ShowAssignKeybinding() | |
| 388 local f = ReActionKeybindFrame | |
| 389 f:ClearAllPoints() | |
| 390 f:SetPoint("BOTTOM", self.button, "TOP", 0, 10) | |
| 391 ReActionKeybindFrameButton.keybindTarget = self | |
| 392 local k = self:GetKeyBinding() | |
| 393 if k then | |
| 394 local txt = GetBindingText(k, "KEY_") | |
| 395 ReActionKeybindFrameButton:SetText(txt or "") | |
| 396 end | |
| 397 f:Show() | |
| 398 end | |
| 399 | |
| 400 | |
| 401 local mouseButtonConvert = { | |
| 402 LeftButton = "BUTTON1", | |
| 403 RightButton = "BUTTON2", | |
| 404 MiddleButton = "BUTTON3", | |
| 405 Button4 = "BUTTON4", | |
| 406 Button5 = "BUTTON5" | |
| 407 } | |
| 408 | |
| 409 function ReActionButton.prototype:HandleKeybindAssign(button, key, mouseButton) | |
| 410 mouseButton = mouseButton and mouseButtonConvert[mouseButton] | |
| 411 if mouseButton ~= "BUTTON1" and mouseButton ~= "BUTTON2" then | |
| 412 key = key or mouseButton | |
| 413 if key == nil or key == "UNKNOWN" or key == "SHIFT" or key == "CTRL" or key == "ALT" then | |
| 414 return | |
| 415 end | |
| 416 if key == "ESCAPE" then | |
| 417 ReActionKeybindFrame:Hide() | |
| 418 return | |
| 419 end | |
| 420 if IsShiftKeyDown() then | |
| 421 key = "SHIFT-"..key | |
| 422 end | |
| 423 if IsControlKeyDown() then | |
| 424 key = "CTRL-"..key | |
| 425 end | |
| 426 if IsAltKeyDown() then | |
| 427 keyPressed = "ALT-"..key | |
| 428 end | |
| 429 local oldAction = GetBindingAction(key) | |
| 430 local oldKey = self:GetKeyBinding() | |
| 431 if oldAction then | |
| 432 -- can't pop a modal dialog box, will need to think something up | |
| 433 end | |
| 434 if oldKey == key then | |
| 435 SetBinding(key,nil) | |
| 436 key = nil | |
| 437 end | |
| 438 self:SetKeyBinding(key) | |
| 439 button:SetText(key and GetBindingText(key, "KEY_") or "") | |
| 440 self:UpdateDisplay() | |
| 441 SaveBindings(2) -- 2 = character specific bindings... hmm... | |
| 442 end | |
| 443 button.selected = false | |
| 444 this:SetButtonState("NORMAL") | |
| 445 end | |
| 446 | |
| 447 | |
| 448 -- action ID functions | 404 -- action ID functions |
| 449 function ReActionButton.prototype:SetActionID( id ) | 405 function ReActionButton.prototype:SetActionID( id ) |
| 450 self.config.actionIDs[self.barIdx] = tonumber(id) -- force data integrity | 406 self.actionID = tonumber(id) -- force data integrity |
| 451 self:ApplyActionID() | 407 self:ApplyActionID() |
| 452 end | 408 end |
| 453 | 409 |
| 454 function ReActionButton.prototype:GetActionID() | 410 function ReActionButton.prototype:GetActionID() |
| 455 return self.config.actionIDs[self.barIdx] | 411 return self.actionID |
| 456 end | 412 end |
| 457 | 413 |
| 458 function ReActionButton.prototype:ApplyActionID() | 414 function ReActionButton.prototype:ApplyActionID() |
| 459 local action = tonumber(self:GetActionID()) | 415 local action = tonumber(self:GetActionID()) |
| 460 self.button:SetAttribute("action",action or nil) | 416 self.button:SetAttribute("action",action) |
| 461 self.frames.actionID:SetText(action or "") | 417 self.frames.actionID:SetText(action or "") |
| 462 end | 418 end |
| 463 | 419 |
| 464 function ReActionButton.prototype:ShowActionID() | 420 function ReActionButton.prototype:ShowActionID() |
| 465 self.frames.actionID:Show() | 421 self.frames.actionID:Show() |
| 486 function ReActionButton.prototype:ShouldPickupAction(mouseButton) | 442 function ReActionButton.prototype:ShouldPickupAction(mouseButton) |
| 487 return IsShiftKeyDown() and not SecureButton_GetModifiedAttribute(self.button, "type", mouseButton) | 443 return IsShiftKeyDown() and not SecureButton_GetModifiedAttribute(self.button, "type", mouseButton) |
| 488 end | 444 end |
| 489 | 445 |
| 490 function ReActionButton.prototype:ShowGridTmp() | 446 function ReActionButton.prototype:ShowGridTmp() |
| 491 self.showGrid = self.showGrid + 1 | 447 self.showGridTmp_ = self.showGridTmp_ + 1 |
| 492 self:UpdateVisibility() | 448 self:UpdateVisibility() |
| 493 end | 449 end |
| 494 | 450 |
| 495 function ReActionButton.prototype:HideGridTmp() | 451 function ReActionButton.prototype:HideGridTmp() |
| 496 self.showGrid = self.showGrid - 1 | 452 self.showGridTmp_ = self.showGridTmp_ - 1 |
| 497 self:UpdateVisibility() | 453 self:UpdateVisibility() |
| 498 end | 454 end |
| 499 | 455 |
| 500 function ReActionButton.prototype:ShowGrid() | 456 function ReActionButton.prototype:ShowGrid() |
| 501 self.config.showGrid = true | 457 self.config.showGrid = true |
| 502 self:ShowGridTmp() | 458 self:UpdateVisibility() |
| 503 end | 459 end |
| 504 | 460 |
| 505 function ReActionButton.prototype:HideGrid() | 461 function ReActionButton.prototype:HideGrid() |
| 506 self.config.showGrid = false | 462 self.config.showGrid = false |
| 507 self:HideGridTmp() | 463 self:UpdateVisibility() |
| 508 end | 464 end |
| 509 | 465 |
| 510 | 466 |
| 511 | 467 |
| 512 -- layout & style functions | 468 -- layout & style functions |
| 514 local f = self.frames | 470 local f = self.frames |
| 515 | 471 |
| 516 if self.config.keyBindLoc then | 472 if self.config.keyBindLoc then |
| 517 local h = f.hotkey | 473 local h = f.hotkey |
| 518 local loc = self.config.keyBindLoc | 474 local loc = self.config.keyBindLoc |
| 475 local top = string.match(loc,"TOP") | |
| 476 local bottom = string.match(loc, "BOTTOM") | |
| 519 h:ClearAllPoints() | 477 h:ClearAllPoints() |
| 520 h:SetPoint(loc,0,0) | 478 h:SetWidth(40) |
| 479 h:SetPoint(top or bottom,0,top and 2 or -2) | |
| 521 local j | 480 local j |
| 522 if string.match(loc,"LEFT") then | 481 if string.match(loc,"LEFT") then |
| 523 j = "LEFT" | 482 j = "LEFT" |
| 524 elseif string.match(loc,"RIGHT") then | 483 elseif string.match(loc,"RIGHT") then |
| 525 j = "RIGHT" | 484 j = "RIGHT" |
| 530 end | 489 end |
| 531 | 490 |
| 532 if self.config.stackCountLoc then | 491 if self.config.stackCountLoc then |
| 533 local c = f.count | 492 local c = f.count |
| 534 local loc = self.config.stackCountLoc | 493 local loc = self.config.stackCountLoc |
| 494 local top = string.match(loc,"TOP") | |
| 495 local bottom = string.match(loc, "BOTTOM") | |
| 535 c:ClearAllPoints() | 496 c:ClearAllPoints() |
| 536 c:SetPoint(loc,0,0) | 497 c:SetWidth(40) |
| 498 c:SetPoint(top or bottom,0,top and 2 or -2) | |
| 537 local j | 499 local j |
| 538 if string.match(loc,"LEFT") then | 500 if string.match(loc,"LEFT") then |
| 539 j = "LEFT" | 501 j = "LEFT" |
| 540 elseif string.match(loc,"RIGHT") then | 502 elseif string.match(loc,"RIGHT") then |
| 541 j = "RIGHT" | 503 j = "RIGHT" |
| 697 local b = self.button | 659 local b = self.button |
| 698 local f = self.frames | 660 local f = self.frames |
| 699 local key = self:GetKeyBinding() | 661 local key = self:GetKeyBinding() |
| 700 local txt = GetBindingText(key, "KEY_",1) | 662 local txt = GetBindingText(key, "KEY_",1) |
| 701 | 663 |
| 664 -- abbreviate long key names | |
| 665 for pat, rep in pairs(keybindAbbreviations) do | |
| 666 txt = string.gsub(txt,pat,rep) | |
| 667 end | |
| 668 | |
| 702 if txt then | 669 if txt then |
| 703 f.hotkey:SetText(string.upper(txt)) | 670 f.hotkey:SetText(string.upper(txt)) |
| 704 self:ColorHotKey() | 671 self:ColorHotKey() |
| 705 else | 672 else |
| 706 f.hotkey:SetText("") | 673 f.hotkey:SetText("") |
| 773 if b:GetAttribute("statehidden") then | 740 if b:GetAttribute("statehidden") then |
| 774 b:Hide() | 741 b:Hide() |
| 775 elseif action and HasAction(action) then | 742 elseif action and HasAction(action) then |
| 776 b:GetNormalTexture():SetAlpha(1.0) | 743 b:GetNormalTexture():SetAlpha(1.0) |
| 777 b:Show() | 744 b:Show() |
| 778 elseif self.showGrid > 0 then | 745 elseif self.showGridTmp_ > 0 or self.config.showGrid then |
| 779 b:GetNormalTexture():SetAlpha(0.5) | 746 b:GetNormalTexture():SetAlpha(0.5) |
| 780 self.frames.cooldown:Hide() | 747 self.frames.cooldown:Hide() |
| 781 b:Show() | 748 b:Show() |
| 782 else | 749 else |
| 783 b:Hide() | 750 b:Hide() |
| 793 end | 760 end |
| 794 end | 761 end |
| 795 | 762 |
| 796 function ReActionButton.prototype:UpdateTooltip() | 763 function ReActionButton.prototype:UpdateTooltip() |
| 797 local action = self:GetActionID() | 764 local action = self:GetActionID() |
| 798 if action and GameTooltip:SetAction(action) then | 765 if GameTooltip:IsOwned(self.button) and action and GameTooltip:SetAction(action) then |
| 799 self.tooltipTime = TOOLTIP_UPDATE_TIME | 766 self.tooltipTime = TOOLTIP_UPDATE_TIME |
| 800 else | 767 else |
| 801 self.tooltipTime = nil | 768 self.tooltipTime = nil |
| 802 end | 769 end |
| 803 end | 770 end |
