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