annotate classes/Bar.lua @ 148:de1da46dadb3

moved unitwatch handling into bar
author Flick <flickerstreak@gmail.com>
date Fri, 08 May 2009 00:06:53 +0000
parents 901c91dc1bf2
children d5e11e924053
rev   line source
flickerstreak@122 1 local ReAction = ReAction
flickerstreak@122 2 local L = ReAction.L
flickerstreak@122 3 local _G = _G
flickerstreak@122 4 local CreateFrame = CreateFrame
flickerstreak@122 5 local floor = math.floor
flickerstreak@122 6 local fmod = math.fmod
flickerstreak@122 7 local format = string.format
flickerstreak@122 8
flickerstreak@122 9 ReAction:UpdateRevision("$Revision$")
flickerstreak@122 10
flickerstreak@147 11 local KB = LibStub("LibKeyBound-1.0")
flickerstreak@147 12
flickerstreak@147 13 ---- Bar class ----
flickerstreak@122 14 local Bar = { }
flickerstreak@122 15 local weak = { __mode = "k" }
flickerstreak@147 16 local frameList = { }
flickerstreak@122 17
flickerstreak@122 18 ReAction.Bar = Bar -- export to ReAction
flickerstreak@122 19
flickerstreak@122 20 function Bar:New( name, config )
flickerstreak@122 21 if type(config) ~= "table" then
flickerstreak@122 22 error("ReAction.Bar: config table required")
flickerstreak@122 23 end
flickerstreak@122 24
flickerstreak@122 25 -- create new self
flickerstreak@122 26 self = setmetatable(
flickerstreak@122 27 {
flickerstreak@122 28 config = config,
flickerstreak@122 29 name = name,
flickerstreak@122 30 buttons = setmetatable( { }, weak ),
flickerstreak@122 31 width = config.width or 480,
flickerstreak@147 32 height = config.height or 40,
flickerstreak@122 33 },
flickerstreak@147 34 {__index = self} )
flickerstreak@122 35
flickerstreak@122 36 -- The frame type is 'Button' in order to have an OnClick handler. However, the frame itself is
flickerstreak@122 37 -- not mouse-clickable by the user.
flickerstreak@122 38 local parent = config.parent and (ReAction:GetBar(config.parent) or _G[config.parent]) or UIParent
flickerstreak@147 39 name = name and "ReAction-"..name
flickerstreak@147 40 local f = name and frameList[name]
flickerstreak@147 41 if not f then
flickerstreak@147 42 f = CreateFrame("Button", name, parent, "SecureHandlerStateTemplate, SecureHandlerClickTemplate")
flickerstreak@147 43 if name then
flickerstreak@147 44 frameList[name] = f
flickerstreak@147 45 end
flickerstreak@147 46 end
flickerstreak@122 47 f:SetFrameStrata("MEDIUM")
flickerstreak@122 48 f:SetWidth(self.width)
flickerstreak@146 49 f:SetHeight(self.height)
flickerstreak@122 50 f:SetAlpha(config.alpha or 1.0)
flickerstreak@122 51 f:Show()
flickerstreak@122 52 f:EnableMouse(false)
flickerstreak@122 53 f:SetClampedToScreen(true)
flickerstreak@122 54
flickerstreak@122 55 f:SetAttribute("_onstate-showgrid",
flickerstreak@122 56 -- function(self,stateid,newstate)
flickerstreak@122 57 [[
flickerstreak@122 58 control:ChildUpdate(stateid,newstate)
flickerstreak@122 59 control:CallMethod("UpdateShowGrid")
flickerstreak@122 60 ]])
flickerstreak@122 61 f.UpdateShowGrid = function(frame)
flickerstreak@122 62 for button in self:IterateButtons() do
flickerstreak@122 63 button:UpdateShowGrid()
flickerstreak@122 64 end
flickerstreak@122 65 end
flickerstreak@122 66 ReAction.gridProxy:AddFrame(f)
flickerstreak@122 67
flickerstreak@122 68 -- Override the default frame accessor to provide strict read-only access
flickerstreak@122 69 function self:GetFrame()
flickerstreak@122 70 return f
flickerstreak@122 71 end
flickerstreak@122 72
flickerstreak@122 73 self:ApplyAnchor()
flickerstreak@147 74 self:SetConfigMode(ReAction:GetConfigMode())
flickerstreak@147 75 self:SetKeybindMode(ReAction:GetKeybindMode())
flickerstreak@147 76
flickerstreak@122 77 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@147 78 KB.RegisterCallback(self, "LIBKEYBOUND_ENABLED")
flickerstreak@147 79 KB.RegisterCallback(self, "LIBKEYBOUND_DISABLED")
flickerstreak@147 80 KB.RegisterCallback(self, "LIBKEYBOUND_MODE_COLOR_CHANGED","LIBKEYBOUND_ENABLED")
flickerstreak@122 81
flickerstreak@122 82 return self
flickerstreak@122 83 end
flickerstreak@122 84
flickerstreak@122 85 function Bar:Destroy()
flickerstreak@122 86 local f = self:GetFrame()
flickerstreak@122 87 f:UnregisterAllEvents()
flickerstreak@125 88 self:ShowControls(false)
flickerstreak@122 89 ReAction.UnregisterAllCallbacks(self)
flickerstreak@147 90 KB.UnregisterAllCallbacks(self)
flickerstreak@122 91 ReAction.gridProxy:RemoveFrame(f)
flickerstreak@122 92 f:SetParent(UIParent)
flickerstreak@122 93 f:ClearAllPoints()
flickerstreak@147 94 f:Hide()
flickerstreak@122 95 end
flickerstreak@122 96
flickerstreak@147 97 --
flickerstreak@147 98 -- Events
flickerstreak@147 99 --
flickerstreak@147 100
flickerstreak@147 101 function Bar:OnConfigModeChanged(event, mode)
flickerstreak@147 102 self:SetConfigMode(mode)
flickerstreak@123 103 end
flickerstreak@123 104
flickerstreak@147 105 function Bar:LIBKEYBOUND_ENABLED(evt)
flickerstreak@147 106 self:SetKeybindMode(true)
flickerstreak@147 107 end
flickerstreak@147 108
flickerstreak@147 109 function Bar:LIBKEYBOUND_DISABLED(evt)
flickerstreak@147 110 self:SetKeybindMode(false)
flickerstreak@122 111 end
flickerstreak@122 112
flickerstreak@122 113 function Bar:ApplyAnchor()
flickerstreak@122 114 local f = self:GetFrame()
flickerstreak@122 115 local c = self.config
flickerstreak@122 116 local p = c.point
flickerstreak@122 117
flickerstreak@122 118 f:SetWidth(c.width)
flickerstreak@122 119 f:SetHeight(c.height)
flickerstreak@122 120 f:ClearAllPoints()
flickerstreak@122 121
flickerstreak@122 122 if p then
flickerstreak@122 123 local a = f:GetParent()
flickerstreak@122 124 if c.anchor then
flickerstreak@122 125 local bar = ReAction:GetBar(c.anchor)
flickerstreak@122 126 if bar then
flickerstreak@122 127 a = bar:GetFrame()
flickerstreak@122 128 else
flickerstreak@122 129 a = _G[c.anchor]
flickerstreak@122 130 end
flickerstreak@122 131 end
flickerstreak@122 132 local fr = a or f:GetParent()
flickerstreak@122 133 f:SetPoint(p, a or f:GetParent(), c.relpoint, c.x or 0, c.y or 0)
flickerstreak@122 134 else
flickerstreak@122 135 f:SetPoint("CENTER")
flickerstreak@122 136 end
flickerstreak@122 137 end
flickerstreak@122 138
flickerstreak@122 139 function Bar:SetAnchor(point, frame, relativePoint, x, y)
flickerstreak@122 140 local c = self.config
flickerstreak@122 141 c.point = point or c.point
flickerstreak@122 142 c.anchor = frame or c.anchor
flickerstreak@122 143 c.relpoint = relativePoint or c.relpoint
flickerstreak@122 144 c.x = x or c.x
flickerstreak@122 145 c.y = y or c.y
flickerstreak@122 146 self:ApplyAnchor()
flickerstreak@122 147 ReAction:RefreshBar(self)
flickerstreak@122 148 end
flickerstreak@122 149
flickerstreak@122 150 function Bar:GetAnchor()
flickerstreak@122 151 local c = self.config
flickerstreak@122 152 return (c.point or "CENTER"),
flickerstreak@122 153 (c.anchor or self:GetFrame():GetParent():GetName()),
flickerstreak@122 154 (c.relpoint or c.point or "CENTER"),
flickerstreak@122 155 (c.x or 0),
flickerstreak@122 156 (c.y or 0)
flickerstreak@122 157 end
flickerstreak@122 158
flickerstreak@122 159 function Bar:GetSize()
flickerstreak@122 160 local f = self:GetFrame()
flickerstreak@122 161 return f:GetWidth(), f:GetHeight()
flickerstreak@122 162 end
flickerstreak@122 163
flickerstreak@122 164 function Bar:SetSize(w,h)
flickerstreak@122 165 local f = self:GetFrame()
flickerstreak@122 166 self.config.width = w
flickerstreak@122 167 self.config.height = h
flickerstreak@122 168 f:SetWidth(w)
flickerstreak@122 169 f:SetHeight(h)
flickerstreak@122 170 end
flickerstreak@122 171
flickerstreak@122 172 function Bar:GetButtonSize()
flickerstreak@122 173 local w = self.config.btnWidth or 32
flickerstreak@122 174 local h = self.config.btnHeight or 32
flickerstreak@122 175 -- TODO: get from modules?
flickerstreak@122 176 return w,h
flickerstreak@122 177 end
flickerstreak@122 178
flickerstreak@122 179 function Bar:SetButtonSize(w,h)
flickerstreak@122 180 if w > 0 and h > 0 then
flickerstreak@122 181 self.config.btnWidth = w
flickerstreak@122 182 self.config.btnHeight = h
flickerstreak@122 183 end
flickerstreak@122 184 ReAction:RefreshBar(self)
flickerstreak@122 185 end
flickerstreak@122 186
flickerstreak@122 187 function Bar:GetButtonGrid()
flickerstreak@122 188 local cfg = self.config
flickerstreak@122 189 local r = cfg.btnRows or 1
flickerstreak@122 190 local c = cfg.btnColumns or 1
flickerstreak@122 191 local s = cfg.spacing or 4
flickerstreak@122 192 return r,c,s
flickerstreak@122 193 end
flickerstreak@122 194
flickerstreak@122 195 function Bar:GetNumButtons()
flickerstreak@122 196 local r,c = self:GetButtonGrid()
flickerstreak@122 197 return r*c
flickerstreak@122 198 end
flickerstreak@122 199
flickerstreak@122 200 function Bar:SetButtonGrid(r,c,s)
flickerstreak@122 201 if r > 0 and c > 0 and s > 0 then
flickerstreak@122 202 local cfg = self.config
flickerstreak@122 203 cfg.btnRows = r
flickerstreak@122 204 cfg.btnColumns = c
flickerstreak@122 205 cfg.spacing = s
flickerstreak@122 206 end
flickerstreak@122 207 ReAction:RefreshBar(self)
flickerstreak@122 208 end
flickerstreak@122 209
flickerstreak@122 210 function Bar:ClipNButtons( n )
flickerstreak@122 211 local cfg = self.config
flickerstreak@122 212 local r = cfg.btnRows or 1
flickerstreak@122 213 local c = cfg.btnColumns or 1
flickerstreak@122 214
flickerstreak@122 215 cfg.btnRows = ceil(n/c)
flickerstreak@122 216 cfg.btnColumns = min(n,c)
flickerstreak@122 217 end
flickerstreak@122 218
flickerstreak@122 219 function Bar:GetName()
flickerstreak@122 220 return self.name
flickerstreak@122 221 end
flickerstreak@122 222
flickerstreak@122 223 function Bar:GetFrame()
flickerstreak@122 224 -- this method is included for documentation purposes. It is overridden
flickerstreak@122 225 -- for each object in the :New() method.
flickerstreak@122 226 error("Invalid Bar object: used without initialization")
flickerstreak@122 227 end
flickerstreak@122 228
flickerstreak@122 229 -- only ReAction:RenameBar() should call this function. Calling from any other
flickerstreak@122 230 -- context will desync the bar list in the ReAction class.
flickerstreak@122 231 function Bar:SetName(name)
flickerstreak@122 232 self.name = name
flickerstreak@122 233 self:SetLabel(self.name) -- Bar:SetLabel() defined in Overlay.lua
flickerstreak@122 234 end
flickerstreak@122 235
flickerstreak@122 236 function Bar:GetAlpha()
flickerstreak@122 237 return self.config.alpha or 1.0
flickerstreak@122 238 end
flickerstreak@122 239
flickerstreak@122 240 function Bar:SetAlpha(value)
flickerstreak@122 241 self.config.alpha = value
flickerstreak@122 242 self:GetFrame():SetAlpha(value or 1.0)
flickerstreak@122 243 ReAction:RefreshBar(self)
flickerstreak@122 244 end
flickerstreak@122 245
flickerstreak@122 246 function Bar:AddButton(idx, button)
flickerstreak@122 247 local f = self:GetFrame()
flickerstreak@122 248
flickerstreak@122 249 -- store in a weak reverse-index array
flickerstreak@122 250 self.buttons[button] = idx
flickerstreak@122 251
flickerstreak@122 252 -- Store a properly wrapped reference to the child frame as an attribute
flickerstreak@122 253 -- (accessible via "frameref-btn#")
flickerstreak@122 254 f:SetFrameRef(format("btn%d",idx), button:GetFrame())
flickerstreak@122 255 end
flickerstreak@122 256
flickerstreak@122 257 function Bar:RemoveButton(button)
flickerstreak@122 258 local idx = self.buttons[button]
flickerstreak@122 259 if idx then
flickerstreak@122 260 self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil)
flickerstreak@122 261 self.buttons[button] = nil
flickerstreak@122 262 end
flickerstreak@122 263 end
flickerstreak@122 264
flickerstreak@122 265 -- iterator returns button, idx and does NOT iterate in index order
flickerstreak@122 266 function Bar:IterateButtons()
flickerstreak@122 267 return pairs(self.buttons)
flickerstreak@122 268 end
flickerstreak@122 269
flickerstreak@147 270 function Bar:SetConfigMode(mode)
flickerstreak@147 271 self:ShowControls(mode)
flickerstreak@148 272 if self.unitwatch then
flickerstreak@148 273 if mode then
flickerstreak@148 274 UnregisterUnitWatch(self:GetFrame())
flickerstreak@148 275 self:GetFrame():Show()
flickerstreak@148 276 else
flickerstreak@148 277 RegisterUnitWatch(self:GetFrame())
flickerstreak@148 278 end
flickerstreak@148 279 end
flickerstreak@147 280 for b in self:IterateButtons() do
flickerstreak@147 281 b:ShowGridTemp(mode)
flickerstreak@147 282 b:UpdateActionIDLabel(mode)
flickerstreak@147 283 end
flickerstreak@147 284 end
flickerstreak@147 285
flickerstreak@147 286 function Bar:SetKeybindMode(mode)
flickerstreak@148 287 if self.unitwatch then
flickerstreak@148 288 if mode then
flickerstreak@148 289 UnregisterUnitWatch(self:GetFrame())
flickerstreak@148 290 self:GetFrame():Show()
flickerstreak@148 291 else
flickerstreak@148 292 RegisterUnitWatch(self:GetFrame())
flickerstreak@148 293 end
flickerstreak@148 294 end
flickerstreak@147 295 for b in self:IterateButtons() do
flickerstreak@147 296 b:SetKeybindMode(mode)
flickerstreak@147 297 end
flickerstreak@147 298 end
flickerstreak@147 299
flickerstreak@122 300 function Bar:PlaceButton(button, baseW, baseH)
flickerstreak@122 301 local idx = self.buttons[button]
flickerstreak@122 302 if idx then
flickerstreak@122 303 local r, c, s = self:GetButtonGrid()
flickerstreak@122 304 local bh, bw = self:GetButtonSize()
flickerstreak@122 305 local row, col = floor((idx-1)/c), fmod((idx-1),c) -- zero-based
flickerstreak@122 306 local x, y = col*bw + (col+0.5)*s, -(row*bh + (row+0.5)*s)
flickerstreak@122 307 local scale = bw/baseW
flickerstreak@122 308 local b = button:GetFrame()
flickerstreak@122 309
flickerstreak@122 310 b:ClearAllPoints()
flickerstreak@122 311 b:SetPoint("TOPLEFT",x/scale,y/scale)
flickerstreak@122 312 b:SetScale(scale)
flickerstreak@122 313 end
flickerstreak@122 314 end
flickerstreak@122 315
flickerstreak@122 316 function Bar:SkinButton()
flickerstreak@122 317 -- does nothing by default
flickerstreak@147 318 end
flickerstreak@148 319
flickerstreak@148 320 -- pass unit=nil to set up the unit elsewhere, if you want something more complex
flickerstreak@148 321 function Bar:RegisterUnitWatch( unit, enable )
flickerstreak@148 322 local f = self:GetFrame()
flickerstreak@148 323 if unit then
flickerstreak@148 324 f:SetAttribute("unit",unit)
flickerstreak@148 325 end
flickerstreak@148 326 if not ReAction:GetConfigMode() then
flickerstreak@148 327 if enable then
flickerstreak@148 328 RegisterUnitWatch(f)
flickerstreak@148 329 elseif self.unitwatch then
flickerstreak@148 330 UnregisterUnitWatch(f)
flickerstreak@148 331 end
flickerstreak@148 332 end
flickerstreak@148 333 self.unitwatch = enable
flickerstreak@148 334 end
flickerstreak@148 335