Mercurial > wow > reaction
comparison modules/ReAction_PossessBar/ReAction_PossessBar.lua @ 54:8b81d4b3e73d
Possess bar support (actions only)
| author | Flick <flickerstreak@gmail.com> |
|---|---|
| date | Fri, 25 Apr 2008 22:13:02 +0000 |
| parents | |
| children | b85118b61564 |
comparison
equal
deleted
inserted
replaced
| 53:7e09c02ae620 | 54:8b81d4b3e73d |
|---|---|
| 1 --[[ | |
| 2 ReAction Possess Bar (Mind Control/etc) button module. | |
| 3 | |
| 4 Wraps the standard Action buttons 121-132 with an automatic show/hide | |
| 5 when mind control (etc) is active | |
| 6 | |
| 7 --]] | |
| 8 | |
| 9 -- local imports | |
| 10 local ReAction = ReAction | |
| 11 local L = ReAction.L | |
| 12 local _G = _G | |
| 13 local CreateFrame = CreateFrame | |
| 14 | |
| 15 -- module declaration | |
| 16 local moduleID = "PossessBar" | |
| 17 local module = ReAction:NewModule( moduleID ) | |
| 18 | |
| 19 -- module methods | |
| 20 function module:OnInitialize() | |
| 21 self.db = ReAction.db:RegisterNamespace( moduleID, | |
| 22 { | |
| 23 profile = { | |
| 24 buttons = { } | |
| 25 } | |
| 26 } | |
| 27 ) | |
| 28 self.buttons = { } | |
| 29 | |
| 30 ReAction:RegisterOptions("global", self, { | |
| 31 hideEmptyPossess = { | |
| 32 type = "toggle", | |
| 33 name = L["Hide Empty Possess Bar Buttons"], | |
| 34 get = function() return self.db.profile.hideEmptyButtons end, | |
| 35 set = function(info, val) module:SetHideEmptyButtons(val) end, | |
| 36 } | |
| 37 }) | |
| 38 end | |
| 39 | |
| 40 function module:OnEnable() | |
| 41 ReAction:RegisterBarType(L["Possess Bar"], | |
| 42 { | |
| 43 type = moduleID, | |
| 44 defaultButtonSize = 36, | |
| 45 defaultBarRows = 1, | |
| 46 defaultBarCols = 12, | |
| 47 defaultBarSpacing = 3 | |
| 48 }) | |
| 49 end | |
| 50 | |
| 51 function module:OnDisable() | |
| 52 ReAction:UnregisterBarType(L["Possess Bar"]) | |
| 53 end | |
| 54 | |
| 55 function module:ApplyToBar(bar) | |
| 56 if bar.config.type == moduleID then | |
| 57 bar:GetFrame():SetParent(PossessBarFrame) | |
| 58 bar.config.parent = "PossessBarFrame" | |
| 59 end | |
| 60 self:RefreshBar(bar) | |
| 61 end | |
| 62 | |
| 63 function module:RefreshBar(bar) | |
| 64 if bar.config.type == moduleID then | |
| 65 if self.buttons[bar] == nil then | |
| 66 self.buttons[bar] = { } | |
| 67 end | |
| 68 local btns = self.buttons[bar] | |
| 69 local profile = self.db.profile | |
| 70 local barName = bar:GetName() | |
| 71 if profile.buttons[barName] == nil then | |
| 72 profile.buttons[barName] = {} | |
| 73 end | |
| 74 local btnCfg = profile.buttons[barName] | |
| 75 | |
| 76 local r, c = bar:GetButtonGrid() | |
| 77 local n = r*c | |
| 78 for i = 1, n do | |
| 79 if btnCfg[i] == nil then | |
| 80 btnCfg[i] = {} | |
| 81 end | |
| 82 if btns[i] == nil then | |
| 83 local ok, b = pcall(self.BtnClass.new, self.BtnClass, bar, i, btnCfg[i]) | |
| 84 if ok and b then | |
| 85 btns[i] = b | |
| 86 end | |
| 87 else | |
| 88 btns[i]:Refresh(bar,i) | |
| 89 end | |
| 90 end | |
| 91 for i = n+1, #btns do | |
| 92 if btns[i] then | |
| 93 btns[i] = btns[i]:Destroy() | |
| 94 if btnCfg[i] then | |
| 95 btnCfg[i] = nil | |
| 96 end | |
| 97 end | |
| 98 end | |
| 99 end | |
| 100 end | |
| 101 | |
| 102 function module:RemoveFromBar(bar) | |
| 103 if self.buttons[bar] then | |
| 104 local btns = self.buttons[bar] | |
| 105 for _,b in pairs(btns) do | |
| 106 if b then | |
| 107 b:Destroy() | |
| 108 end | |
| 109 end | |
| 110 self.buttons[bar] = nil | |
| 111 end | |
| 112 end | |
| 113 | |
| 114 function module:EraseBarConfig(barName) | |
| 115 self.db.profile.buttons[barName] = nil | |
| 116 end | |
| 117 | |
| 118 function module:RenameBarConfig(oldname, newname) | |
| 119 local b = self.db.profile.buttons | |
| 120 b[newname], b[oldname] = b[oldname], nil | |
| 121 end | |
| 122 | |
| 123 function module:SetHideEmptyButtons(hide) | |
| 124 if hide ~= self.db.profile.hideEmptyButtons then | |
| 125 for _, bar in pairs(self.buttons) do | |
| 126 for _, b in pairs(bar) do | |
| 127 if hide then | |
| 128 ActionButton_HideGrid(b.frame) | |
| 129 else | |
| 130 ActionButton_ShowGrid(b.frame) | |
| 131 end | |
| 132 end | |
| 133 end | |
| 134 self.db.profile.hideEmptyButtons = hide | |
| 135 end | |
| 136 end | |
| 137 | |
| 138 function module:ApplyConfigMode(mode,bars) | |
| 139 for _, bar in pairs(bars) do | |
| 140 if bar and self.buttons[bar] then | |
| 141 for _, b in pairs(self.buttons[bar]) do | |
| 142 if b then | |
| 143 if mode then | |
| 144 ActionButton_ShowGrid(b.frame) | |
| 145 self:showActionIDLabel(b) | |
| 146 else | |
| 147 ActionButton_HideGrid(b.frame) | |
| 148 self:hideActionIDLabel(b) | |
| 149 end | |
| 150 end | |
| 151 end | |
| 152 local f = bar:GetFrame() | |
| 153 if mode then | |
| 154 f:SetParent(UIParent) | |
| 155 f:Show() | |
| 156 else | |
| 157 f:SetParent(PossessBarFrame) | |
| 158 end | |
| 159 end | |
| 160 end | |
| 161 end | |
| 162 | |
| 163 function module:showActionIDLabel(button) | |
| 164 if not button.actionIDLabel and button:GetActionID() then | |
| 165 local label = button:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge") | |
| 166 label:SetAllPoints() | |
| 167 label:SetJustifyH("CENTER") | |
| 168 label:SetShadowColor(0,0,0,1) | |
| 169 label:SetShadowOffset(2,-2) | |
| 170 label:SetText(tostring(button:GetActionID())) | |
| 171 button.actionIDLabel = label | |
| 172 end | |
| 173 button.actionIDLabel:Show() | |
| 174 end | |
| 175 | |
| 176 function module:hideActionIDLabel(button) | |
| 177 if button.actionIDLabel then | |
| 178 button.actionIDLabel:Hide() | |
| 179 end | |
| 180 end | |
| 181 | |
| 182 | |
| 183 | |
| 184 -- use-count of action IDs | |
| 185 local minActionID = 121 | |
| 186 local maxActionID = 132 | |
| 187 local ActionIDList = setmetatable( {}, { | |
| 188 __index = function(self, idx) | |
| 189 if idx == nil then | |
| 190 for i = minActionID, maxActionID do | |
| 191 if rawget(self,i) == nil then | |
| 192 rawset(self,i,1) | |
| 193 return i | |
| 194 end | |
| 195 end | |
| 196 error("ran out of action IDs") | |
| 197 else | |
| 198 local c = rawget(self,idx) or 0 | |
| 199 rawset(self,idx,c+1) | |
| 200 return idx | |
| 201 end | |
| 202 end, | |
| 203 __newindex = function(self,idx,value) | |
| 204 if value == nil then | |
| 205 value = rawget(self,idx) | |
| 206 if value == 1 then | |
| 207 value = nil | |
| 208 elseif value then | |
| 209 value = value - 1 | |
| 210 end | |
| 211 end | |
| 212 rawset(self,idx,value) | |
| 213 end | |
| 214 }) | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 ------ Button class ------ | |
| 220 local Button = { } | |
| 221 | |
| 222 local function Constructor( self, bar, idx, config ) | |
| 223 self.bar, self.idx, self.config = bar, idx, config | |
| 224 | |
| 225 local barFrame = bar:GetFrame() | |
| 226 | |
| 227 config.name = config.name or "ReAction_"..bar:GetName().."_Possess_"..idx | |
| 228 self.name = config.name | |
| 229 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured | |
| 230 | |
| 231 local f = CreateFrame("CheckButton", self.name, barFrame, "BonusActionButtonTemplate") | |
| 232 | |
| 233 -- TODO: re-implement ActionButton event handlers that don't do secure stuff | |
| 234 | |
| 235 -- this will probably cause taint, using right now for display/debugging purposes | |
| 236 f:SetScript("OnAttributeChanged", ActionButton_UpdateAction) | |
| 237 f:SetAttribute("action", config.actionID) | |
| 238 | |
| 239 barFrame:SetAttribute("addchild",f) | |
| 240 | |
| 241 self.frame = f | |
| 242 self:Refresh(bar,idx) | |
| 243 | |
| 244 if not module.db.profile.hideEmptyButtons then | |
| 245 ActionButton_ShowGrid(self.frame) | |
| 246 end | |
| 247 | |
| 248 if ReAction.configMode then | |
| 249 ActionButton_ShowGrid(self.frame) | |
| 250 module:showActionIDLabel(self) | |
| 251 end | |
| 252 end | |
| 253 | |
| 254 function Button:Destroy() | |
| 255 local f = self.frame | |
| 256 f:UnregisterAllEvents() | |
| 257 f:Hide() | |
| 258 f:SetParent(UIParent) | |
| 259 f:ClearAllPoints() | |
| 260 if self.name then | |
| 261 _G[self.name] = nil | |
| 262 end | |
| 263 if self.config.actionID then | |
| 264 ActionIDList[self.config.actionID] = nil | |
| 265 end | |
| 266 self.frame = nil | |
| 267 self.config = nil | |
| 268 self.bar = nil | |
| 269 end | |
| 270 | |
| 271 function Button:Refresh(bar,idx) | |
| 272 bar:PlaceButton(self.frame, idx, 36, 36) | |
| 273 end | |
| 274 | |
| 275 function Button:GetFrame() | |
| 276 return self.frame | |
| 277 end | |
| 278 | |
| 279 function Button:GetName() | |
| 280 return self.name | |
| 281 end | |
| 282 | |
| 283 function Button:GetActionID() | |
| 284 return self.config.actionID | |
| 285 end | |
| 286 | |
| 287 | |
| 288 -- export as a class-factory to module | |
| 289 module.BtnClass = { | |
| 290 new = function(self, ...) | |
| 291 local x = { } | |
| 292 for k,v in pairs(Button) do | |
| 293 x[k] = v | |
| 294 end | |
| 295 Constructor(x, ...) | |
| 296 return x | |
| 297 end | |
| 298 } |
