annotate modules/ReAction_Action/ReAction_Action.lua @ 28:21bcaf8215ff

- converted to Ace3 - rearranged file layout - configGUI menus not working right now
author Flick <flickerstreak@gmail.com>
date Mon, 17 Mar 2008 18:24:53 +0000
parents 9e1984088124
children 0d95ce7a9ec2
rev   line source
flickerstreak@24 1 --[[
flickerstreak@24 2 ReAction Action-button module.
flickerstreak@24 3
flickerstreak@24 4 The button module implements standard action button functionality by wrapping Blizzard's
flickerstreak@24 5 ActionButton frame and associated functions. It also provides some button layout
flickerstreak@24 6 modification tools.
flickerstreak@24 7
flickerstreak@24 8 --]]
flickerstreak@24 9
flickerstreak@24 10 -- local imports
flickerstreak@24 11 local ReAction = ReAction
flickerstreak@24 12 local L = ReAction.L
flickerstreak@24 13 local _G = _G
flickerstreak@24 14 local CreateFrame = CreateFrame
flickerstreak@24 15 local print = ReAction.print
flickerstreak@24 16
flickerstreak@24 17 -- module declaration
flickerstreak@24 18 local moduleID = "Action"
flickerstreak@28 19 local module = ReAction:NewModule( moduleID )
flickerstreak@24 20
flickerstreak@24 21 -- module methods
flickerstreak@24 22 function module:OnInitialize()
flickerstreak@28 23 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@24 24 {
flickerstreak@28 25 profile = {
flickerstreak@28 26 buttons = { }
flickerstreak@28 27 }
flickerstreak@24 28 }
flickerstreak@24 29 )
flickerstreak@24 30 self.buttons = { }
flickerstreak@24 31 end
flickerstreak@24 32
flickerstreak@24 33 function module:OnEnable()
flickerstreak@24 34 end
flickerstreak@24 35
flickerstreak@24 36 function module:OnDisable()
flickerstreak@24 37 end
flickerstreak@24 38
flickerstreak@24 39 function module:ApplyToBar(bar)
flickerstreak@24 40 self:RefreshBar(bar)
flickerstreak@24 41 end
flickerstreak@24 42
flickerstreak@24 43 function module:RefreshBar(bar)
flickerstreak@24 44 if self.buttons[bar] == nil then
flickerstreak@24 45 self.buttons[bar] = { }
flickerstreak@24 46 end
flickerstreak@24 47 local btns = self.buttons[bar]
flickerstreak@24 48 local profile = self.db.profile
flickerstreak@24 49 local barName = bar:GetName()
flickerstreak@24 50 if profile.buttons[barName] == nil then
flickerstreak@24 51 profile.buttons[barName] = {}
flickerstreak@24 52 end
flickerstreak@24 53 local btnCfg = profile.buttons[barName]
flickerstreak@24 54
flickerstreak@24 55 local r, c = bar:GetButtonGrid()
flickerstreak@24 56 local n = r*c
flickerstreak@24 57 for i = 1, n do
flickerstreak@24 58 if btnCfg[i] == nil then
flickerstreak@24 59 btnCfg[i] = {}
flickerstreak@24 60 end
flickerstreak@24 61 if btns[i] == nil then
flickerstreak@24 62 btns[i] = self.BtnClass:new(bar,i,btnCfg[i])
flickerstreak@24 63 else
flickerstreak@24 64 btns[i]:Refresh(bar,i)
flickerstreak@24 65 end
flickerstreak@24 66 end
flickerstreak@24 67 for i = n+1, #btns do
flickerstreak@24 68 btns[i] = btns[i]:Destroy()
flickerstreak@24 69 if btnCfg[i] then
flickerstreak@24 70 btnCfg[i] = nil
flickerstreak@24 71 end
flickerstreak@24 72 end
flickerstreak@24 73 end
flickerstreak@24 74
flickerstreak@24 75 function module:RemoveFromBar(bar)
flickerstreak@24 76 if self.buttons[bar] then
flickerstreak@24 77 local btns = self.buttons[bar]
flickerstreak@24 78 for _,b in pairs(btns) do
flickerstreak@24 79 if b then
flickerstreak@24 80 b:Destroy()
flickerstreak@24 81 end
flickerstreak@24 82 end
flickerstreak@24 83 self.buttons[bar] = nil
flickerstreak@24 84 end
flickerstreak@24 85 end
flickerstreak@24 86
flickerstreak@24 87 function module:EraseBarConfig(barName)
flickerstreak@24 88 self.db.profile.buttons[barName] = nil
flickerstreak@24 89 end
flickerstreak@24 90
flickerstreak@24 91 function module:ApplyConfigMode(mode,bars)
flickerstreak@24 92 for _, btns in pairs(self.buttons) do
flickerstreak@24 93 if btn then
flickerstreak@24 94 for _, b in pairs(btns) do
flickerstreak@24 95 if b then
flickerstreak@24 96 if mode then
flickerstreak@24 97 if not b.actionIDLabel then
flickerstreak@24 98 local label = b:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@24 99 label:SetAllPoints()
flickerstreak@24 100 label:SetJustifyH("CENTER")
flickerstreak@24 101 label:SetShadowColor(0,0,0,1)
flickerstreak@24 102 label:SetShadowOffset(2,-2)
flickerstreak@24 103 label:SetText(tostring(b:GetActionID()))
flickerstreak@24 104 b.actionIDLabel = label
flickerstreak@24 105 end
flickerstreak@24 106 b.actionIDLabel:Show()
flickerstreak@24 107 elseif b.actionIDLabel then
flickerstreak@24 108 b.actionIDLabel:Hide()
flickerstreak@24 109 end
flickerstreak@24 110 end
flickerstreak@24 111 end
flickerstreak@24 112 end
flickerstreak@24 113 end
flickerstreak@24 114 end
flickerstreak@24 115
flickerstreak@24 116 function module:GetGlobalBarOptions(opts)
flickerstreak@24 117 if self.globalBarOpts == nil then
flickerstreak@24 118 self.globalBarOpts = {
flickerstreak@24 119 newActionBar = {
flickerstreak@24 120 type = "execute",
flickerstreak@24 121 name = L["New Action Bar"],
flickerstreak@24 122 desc = L["Create a new bar of standard action buttons"],
flickerstreak@24 123 func = function()
flickerstreak@28 124 ReAction:CreateBar()
flickerstreak@24 125 end,
flickerstreak@24 126 disabled = InCombatLockdown,
flickerstreak@24 127 }
flickerstreak@24 128 }
flickerstreak@24 129 end
flickerstreak@24 130 return self.globalBarOpts
flickerstreak@24 131 end
flickerstreak@24 132
flickerstreak@24 133 function module:GetBarMenuOptions(bar)
flickerstreak@24 134 if not bar.modMenuOpts[moduleID] then
flickerstreak@24 135 bar.modMenuOpts[moduleID] = {
flickerstreak@24 136 }
flickerstreak@24 137 end
flickerstreak@24 138 return bar.modMenuOpts[moduleID]
flickerstreak@24 139 end
flickerstreak@24 140
flickerstreak@24 141 function module:GetBarConfigOptions(bar)
flickerstreak@24 142 if not bar.modConfigOpts[moduleID] then
flickerstreak@24 143 bar.modConfigOpts[moduleID] = {
flickerstreak@24 144 }
flickerstreak@24 145 end
flickerstreak@24 146 return bar.modConfigOpts[moduleID]
flickerstreak@24 147 end
flickerstreak@24 148
flickerstreak@24 149
flickerstreak@24 150
flickerstreak@24 151 -- use-count of action IDs
flickerstreak@24 152 local ActionIDList = setmetatable( {}, {
flickerstreak@24 153 __index = function(self, idx)
flickerstreak@24 154 if idx == nil then
flickerstreak@24 155 for i = 1, 120 do
flickerstreak@24 156 if rawget(self,i) == nil then
flickerstreak@24 157 rawset(self,i,1)
flickerstreak@24 158 return i
flickerstreak@24 159 end
flickerstreak@24 160 end
flickerstreak@24 161 else
flickerstreak@24 162 local c = rawget(self,idx) or 0
flickerstreak@24 163 rawset(self,idx,c+1)
flickerstreak@24 164 return idx
flickerstreak@24 165 end
flickerstreak@24 166 end,
flickerstreak@24 167 __newindex = function(self,idx,value)
flickerstreak@24 168 if value == nil then
flickerstreak@24 169 value = rawget(self,idx)
flickerstreak@24 170 if value == 1 then
flickerstreak@24 171 value = nil
flickerstreak@24 172 elseif value then
flickerstreak@24 173 value = value - 1
flickerstreak@24 174 end
flickerstreak@24 175 end
flickerstreak@24 176 rawset(self,idx,value)
flickerstreak@24 177 end
flickerstreak@24 178 })
flickerstreak@24 179
flickerstreak@24 180
flickerstreak@24 181
flickerstreak@28 182
flickerstreak@28 183 ------ Button class ------
flickerstreak@28 184 local Button = { }
flickerstreak@28 185
flickerstreak@28 186 local function Constructor( self, bar, idx, config )
flickerstreak@24 187 self.bar, self.idx, self.config = bar, idx, config
flickerstreak@24 188
flickerstreak@24 189 local barFrame = bar:GetFrame()
flickerstreak@24 190
flickerstreak@24 191 self.name = config.name or "ReAction_"..bar:GetName().."_"..idx
flickerstreak@24 192 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured
flickerstreak@24 193
flickerstreak@24 194 local f = CreateFrame("CheckButton", self.name, barFrame, "ActionBarButtonTemplate")
flickerstreak@24 195 -- TODO: re-implement ActionButton event handlers that don't do secure stuff
flickerstreak@24 196
flickerstreak@24 197 -- this will probably cause taint, using right now for display/debugging purposes
flickerstreak@24 198 f:SetScript("OnAttributeChanged",
flickerstreak@24 199 function()
flickerstreak@24 200 ActionButton_UpdateAction()
flickerstreak@24 201 end
flickerstreak@24 202 )
flickerstreak@24 203 f:SetAttribute("action", config.actionID)
flickerstreak@24 204 barFrame:SetAttribute("addchild",f)
flickerstreak@24 205 self.frame = f
flickerstreak@24 206 self:Refresh(bar,idx)
flickerstreak@24 207 end
flickerstreak@24 208
flickerstreak@24 209 function Button:Destroy()
flickerstreak@24 210 local f = self.frame
flickerstreak@24 211 f:UnregisterAllEvents()
flickerstreak@24 212 f:Hide()
flickerstreak@24 213 f:SetParent(UIParent)
flickerstreak@24 214 f:ClearAllPoints()
flickerstreak@28 215 if self.name then
flickerstreak@28 216 _G[self.name] = nil
flickerstreak@24 217 end
flickerstreak@24 218 ActionIDList[self.config.actionID] = nil
flickerstreak@24 219 self.frame = nil
flickerstreak@24 220 self.config = nil
flickerstreak@24 221 self.bar = nil
flickerstreak@24 222 end
flickerstreak@24 223
flickerstreak@24 224 function Button:Refresh(bar,idx)
flickerstreak@24 225 bar:PlaceButton(self.frame, idx, 36, 36)
flickerstreak@24 226 end
flickerstreak@24 227
flickerstreak@24 228 function Button:GetFrame()
flickerstreak@24 229 return self.frame
flickerstreak@24 230 end
flickerstreak@24 231
flickerstreak@24 232 function Button:GetName()
flickerstreak@24 233 return self.name
flickerstreak@24 234 end
flickerstreak@24 235
flickerstreak@24 236 function Button:GetActionID()
flickerstreak@24 237 return self.config.actionID
flickerstreak@24 238 end
flickerstreak@28 239
flickerstreak@28 240
flickerstreak@28 241 -- export as a class-factory to module
flickerstreak@28 242 module.BtnClass = {
flickerstreak@28 243 new = function(self, ...)
flickerstreak@28 244 local x = { }
flickerstreak@28 245 for k,v in pairs(Button) do
flickerstreak@28 246 x[k] = v
flickerstreak@28 247 end
flickerstreak@28 248 Constructor(x, ...)
flickerstreak@28 249 return x
flickerstreak@28 250 end
flickerstreak@28 251 }