annotate modules/ReAction_Action/ReAction_Action.lua @ 48:7b7d178dec52

Implemented bar-type selection, extended CreateBar functionality
author Flick <flickerstreak@gmail.com>
date Sat, 12 Apr 2008 00:15:09 +0000
parents 0d95ce7a9ec2
children dcf8116560a1
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@48 34 ReAction:RegisterDefaultBarConfig(L["Action Bar"], { type = "actionbar" }, true)
flickerstreak@24 35 end
flickerstreak@24 36
flickerstreak@24 37 function module:OnDisable()
flickerstreak@48 38 ReAction:UnregisterDefaultBarConfig(L["Action Bar"])
flickerstreak@24 39 end
flickerstreak@24 40
flickerstreak@24 41 function module:ApplyToBar(bar)
flickerstreak@48 42 if bar.config.type == "actionbar" then
flickerstreak@48 43 self:RefreshBar(bar)
flickerstreak@48 44 end
flickerstreak@24 45 end
flickerstreak@24 46
flickerstreak@24 47 function module:RefreshBar(bar)
flickerstreak@48 48 if bar.config.type == "actionbar" then
flickerstreak@48 49 if self.buttons[bar] == nil then
flickerstreak@48 50 self.buttons[bar] = { }
flickerstreak@48 51 end
flickerstreak@48 52 local btns = self.buttons[bar]
flickerstreak@48 53 local profile = self.db.profile
flickerstreak@48 54 local barName = bar:GetName()
flickerstreak@48 55 if profile.buttons[barName] == nil then
flickerstreak@48 56 profile.buttons[barName] = {}
flickerstreak@48 57 end
flickerstreak@48 58 local btnCfg = profile.buttons[barName]
flickerstreak@24 59
flickerstreak@48 60 local r, c = bar:GetButtonGrid()
flickerstreak@48 61 local n = r*c
flickerstreak@48 62 for i = 1, n do
flickerstreak@48 63 if btnCfg[i] == nil then
flickerstreak@48 64 btnCfg[i] = {}
flickerstreak@48 65 end
flickerstreak@48 66 if btns[i] == nil then
flickerstreak@48 67 btns[i] = self.BtnClass:new(bar,i,btnCfg[i])
flickerstreak@48 68 else
flickerstreak@48 69 btns[i]:Refresh(bar,i)
flickerstreak@48 70 end
flickerstreak@24 71 end
flickerstreak@48 72 for i = n+1, #btns do
flickerstreak@48 73 btns[i] = btns[i]:Destroy()
flickerstreak@48 74 if btnCfg[i] then
flickerstreak@48 75 btnCfg[i] = nil
flickerstreak@48 76 end
flickerstreak@24 77 end
flickerstreak@24 78 end
flickerstreak@24 79 end
flickerstreak@24 80
flickerstreak@24 81 function module:RemoveFromBar(bar)
flickerstreak@24 82 if self.buttons[bar] then
flickerstreak@24 83 local btns = self.buttons[bar]
flickerstreak@24 84 for _,b in pairs(btns) do
flickerstreak@24 85 if b then
flickerstreak@24 86 b:Destroy()
flickerstreak@24 87 end
flickerstreak@24 88 end
flickerstreak@24 89 self.buttons[bar] = nil
flickerstreak@24 90 end
flickerstreak@24 91 end
flickerstreak@24 92
flickerstreak@24 93 function module:EraseBarConfig(barName)
flickerstreak@24 94 self.db.profile.buttons[barName] = nil
flickerstreak@24 95 end
flickerstreak@24 96
flickerstreak@48 97 function module:RenameBarConfig(oldname, newname)
flickerstreak@48 98 local b = self.db.profile.buttons
flickerstreak@48 99 b[newname], b[oldname] = b[oldname], nil
flickerstreak@48 100 end
flickerstreak@48 101
flickerstreak@24 102 function module:ApplyConfigMode(mode,bars)
flickerstreak@24 103 for _, btns in pairs(self.buttons) do
flickerstreak@24 104 if btn then
flickerstreak@24 105 for _, b in pairs(btns) do
flickerstreak@24 106 if b then
flickerstreak@24 107 if mode then
flickerstreak@24 108 if not b.actionIDLabel then
flickerstreak@24 109 local label = b:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@24 110 label:SetAllPoints()
flickerstreak@24 111 label:SetJustifyH("CENTER")
flickerstreak@24 112 label:SetShadowColor(0,0,0,1)
flickerstreak@24 113 label:SetShadowOffset(2,-2)
flickerstreak@24 114 label:SetText(tostring(b:GetActionID()))
flickerstreak@24 115 b.actionIDLabel = label
flickerstreak@24 116 end
flickerstreak@24 117 b.actionIDLabel:Show()
flickerstreak@24 118 elseif b.actionIDLabel then
flickerstreak@24 119 b.actionIDLabel:Hide()
flickerstreak@24 120 end
flickerstreak@24 121 end
flickerstreak@24 122 end
flickerstreak@24 123 end
flickerstreak@24 124 end
flickerstreak@24 125 end
flickerstreak@24 126
flickerstreak@24 127 -- use-count of action IDs
flickerstreak@24 128 local ActionIDList = setmetatable( {}, {
flickerstreak@24 129 __index = function(self, idx)
flickerstreak@24 130 if idx == nil then
flickerstreak@24 131 for i = 1, 120 do
flickerstreak@24 132 if rawget(self,i) == nil then
flickerstreak@24 133 rawset(self,i,1)
flickerstreak@24 134 return i
flickerstreak@24 135 end
flickerstreak@24 136 end
flickerstreak@24 137 else
flickerstreak@24 138 local c = rawget(self,idx) or 0
flickerstreak@24 139 rawset(self,idx,c+1)
flickerstreak@24 140 return idx
flickerstreak@24 141 end
flickerstreak@24 142 end,
flickerstreak@24 143 __newindex = function(self,idx,value)
flickerstreak@24 144 if value == nil then
flickerstreak@24 145 value = rawget(self,idx)
flickerstreak@24 146 if value == 1 then
flickerstreak@24 147 value = nil
flickerstreak@24 148 elseif value then
flickerstreak@24 149 value = value - 1
flickerstreak@24 150 end
flickerstreak@24 151 end
flickerstreak@24 152 rawset(self,idx,value)
flickerstreak@24 153 end
flickerstreak@24 154 })
flickerstreak@24 155
flickerstreak@24 156
flickerstreak@24 157
flickerstreak@28 158
flickerstreak@28 159 ------ Button class ------
flickerstreak@28 160 local Button = { }
flickerstreak@28 161
flickerstreak@28 162 local function Constructor( self, bar, idx, config )
flickerstreak@24 163 self.bar, self.idx, self.config = bar, idx, config
flickerstreak@24 164
flickerstreak@24 165 local barFrame = bar:GetFrame()
flickerstreak@24 166
flickerstreak@24 167 self.name = config.name or "ReAction_"..bar:GetName().."_"..idx
flickerstreak@24 168 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured
flickerstreak@24 169
flickerstreak@24 170 local f = CreateFrame("CheckButton", self.name, barFrame, "ActionBarButtonTemplate")
flickerstreak@24 171 -- TODO: re-implement ActionButton event handlers that don't do secure stuff
flickerstreak@24 172
flickerstreak@24 173 -- this will probably cause taint, using right now for display/debugging purposes
flickerstreak@30 174 f:SetScript("OnAttributeChanged", ActionButton_UpdateAction)
flickerstreak@24 175 f:SetAttribute("action", config.actionID)
flickerstreak@24 176 barFrame:SetAttribute("addchild",f)
flickerstreak@24 177 self.frame = f
flickerstreak@24 178 self:Refresh(bar,idx)
flickerstreak@24 179 end
flickerstreak@24 180
flickerstreak@24 181 function Button:Destroy()
flickerstreak@24 182 local f = self.frame
flickerstreak@24 183 f:UnregisterAllEvents()
flickerstreak@24 184 f:Hide()
flickerstreak@24 185 f:SetParent(UIParent)
flickerstreak@24 186 f:ClearAllPoints()
flickerstreak@28 187 if self.name then
flickerstreak@28 188 _G[self.name] = nil
flickerstreak@24 189 end
flickerstreak@24 190 ActionIDList[self.config.actionID] = nil
flickerstreak@24 191 self.frame = nil
flickerstreak@24 192 self.config = nil
flickerstreak@24 193 self.bar = nil
flickerstreak@24 194 end
flickerstreak@24 195
flickerstreak@24 196 function Button:Refresh(bar,idx)
flickerstreak@24 197 bar:PlaceButton(self.frame, idx, 36, 36)
flickerstreak@24 198 end
flickerstreak@24 199
flickerstreak@24 200 function Button:GetFrame()
flickerstreak@24 201 return self.frame
flickerstreak@24 202 end
flickerstreak@24 203
flickerstreak@24 204 function Button:GetName()
flickerstreak@24 205 return self.name
flickerstreak@24 206 end
flickerstreak@24 207
flickerstreak@24 208 function Button:GetActionID()
flickerstreak@24 209 return self.config.actionID
flickerstreak@24 210 end
flickerstreak@28 211
flickerstreak@28 212
flickerstreak@28 213 -- export as a class-factory to module
flickerstreak@28 214 module.BtnClass = {
flickerstreak@28 215 new = function(self, ...)
flickerstreak@28 216 local x = { }
flickerstreak@28 217 for k,v in pairs(Button) do
flickerstreak@28 218 x[k] = v
flickerstreak@28 219 end
flickerstreak@28 220 Constructor(x, ...)
flickerstreak@28 221 return x
flickerstreak@28 222 end
flickerstreak@28 223 }