annotate classes/Bar.lua @ 122:a2d2f23137c8

- Rearranged and consolidated some files in modules directory - Added 'classes' directory, moved Bar and Overlay there - Added Button, ActionButton, and GridProxy classes, not in use yet
author Flick <flickerstreak@gmail.com>
date Mon, 23 Feb 2009 18:56:57 +0000
parents
children 943eed2c7def
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@122 11 local Bar = { }
flickerstreak@122 12 local proto = { __index = Bar }
flickerstreak@122 13 local weak = { __mode = "k" }
flickerstreak@122 14
flickerstreak@122 15 ReAction.Bar = Bar -- export to ReAction
flickerstreak@122 16
flickerstreak@122 17 function Bar:New( name, config )
flickerstreak@122 18 if type(config) ~= "table" then
flickerstreak@122 19 error("ReAction.Bar: config table required")
flickerstreak@122 20 end
flickerstreak@122 21
flickerstreak@122 22 -- create new self
flickerstreak@122 23 self = setmetatable(
flickerstreak@122 24 {
flickerstreak@122 25 config = config,
flickerstreak@122 26 name = name,
flickerstreak@122 27 buttons = setmetatable( { }, weak ),
flickerstreak@122 28 width = config.width or 480,
flickerstreak@122 29 height = config.height or 40
flickerstreak@122 30 },
flickerstreak@122 31 proto )
flickerstreak@122 32
flickerstreak@122 33 -- The frame type is 'Button' in order to have an OnClick handler. However, the frame itself is
flickerstreak@122 34 -- not mouse-clickable by the user.
flickerstreak@122 35 local parent = config.parent and (ReAction:GetBar(config.parent) or _G[config.parent]) or UIParent
flickerstreak@122 36 local f = CreateFrame("Button", name and format("ReAction-%s",name), parent,
flickerstreak@122 37 "SecureHandlerStateTemplate, SecureHandlerClickTemplate")
flickerstreak@122 38 f:SetFrameStrata("MEDIUM")
flickerstreak@122 39 f:SetWidth(self.width)
flickerstreak@122 40 f:SetWidth(self.height)
flickerstreak@122 41 f:SetAlpha(config.alpha or 1.0)
flickerstreak@122 42 f:Show()
flickerstreak@122 43 f:EnableMouse(false)
flickerstreak@122 44 f:SetClampedToScreen(true)
flickerstreak@122 45
flickerstreak@122 46 f:SetAttribute("_onstate-showgrid",
flickerstreak@122 47 -- function(self,stateid,newstate)
flickerstreak@122 48 [[
flickerstreak@122 49 control:ChildUpdate(stateid,newstate)
flickerstreak@122 50 control:CallMethod("UpdateShowGrid")
flickerstreak@122 51 ]])
flickerstreak@122 52 f.UpdateShowGrid = function(frame)
flickerstreak@122 53 for button in self:IterateButtons() do
flickerstreak@122 54 button:UpdateShowGrid()
flickerstreak@122 55 end
flickerstreak@122 56 end
flickerstreak@122 57 ReAction.gridProxy:AddFrame(f)
flickerstreak@122 58
flickerstreak@122 59 -- Override the default frame accessor to provide strict read-only access
flickerstreak@122 60 function self:GetFrame()
flickerstreak@122 61 return f
flickerstreak@122 62 end
flickerstreak@122 63
flickerstreak@122 64 self:ApplyAnchor()
flickerstreak@122 65 ReAction.RegisterCallback(self, "OnConfigModeChanged")
flickerstreak@122 66
flickerstreak@122 67 return self
flickerstreak@122 68 end
flickerstreak@122 69
flickerstreak@122 70 function Bar:Destroy()
flickerstreak@122 71 local f = self:GetFrame()
flickerstreak@122 72 f:UnregisterAllEvents()
flickerstreak@122 73 ReAction.UnregisterAllCallbacks(self)
flickerstreak@122 74 ReAction.gridProxy:RemoveFrame(f)
flickerstreak@122 75 f:Hide()
flickerstreak@122 76 f:SetParent(UIParent)
flickerstreak@122 77 f:ClearAllPoints()
flickerstreak@122 78 end
flickerstreak@122 79
flickerstreak@122 80 function Bar:OnConfigModeChanged(event, mode)
flickerstreak@122 81 self:ShowControls(mode) -- Bar:ShowControls() defined in Overlay.lua
flickerstreak@122 82 end
flickerstreak@122 83
flickerstreak@122 84 function Bar:ApplyAnchor()
flickerstreak@122 85 local f = self:GetFrame()
flickerstreak@122 86 local c = self.config
flickerstreak@122 87 local p = c.point
flickerstreak@122 88
flickerstreak@122 89 f:SetWidth(c.width)
flickerstreak@122 90 f:SetHeight(c.height)
flickerstreak@122 91 f:ClearAllPoints()
flickerstreak@122 92
flickerstreak@122 93 if p then
flickerstreak@122 94 local a = f:GetParent()
flickerstreak@122 95 if c.anchor then
flickerstreak@122 96 local bar = ReAction:GetBar(c.anchor)
flickerstreak@122 97 if bar then
flickerstreak@122 98 a = bar:GetFrame()
flickerstreak@122 99 else
flickerstreak@122 100 a = _G[c.anchor]
flickerstreak@122 101 end
flickerstreak@122 102 end
flickerstreak@122 103 local fr = a or f:GetParent()
flickerstreak@122 104 f:SetPoint(p, a or f:GetParent(), c.relpoint, c.x or 0, c.y or 0)
flickerstreak@122 105 else
flickerstreak@122 106 f:SetPoint("CENTER")
flickerstreak@122 107 end
flickerstreak@122 108 end
flickerstreak@122 109
flickerstreak@122 110 function Bar:SetAnchor(point, frame, relativePoint, x, y)
flickerstreak@122 111 local c = self.config
flickerstreak@122 112 c.point = point or c.point
flickerstreak@122 113 c.anchor = frame or c.anchor
flickerstreak@122 114 c.relpoint = relativePoint or c.relpoint
flickerstreak@122 115 c.x = x or c.x
flickerstreak@122 116 c.y = y or c.y
flickerstreak@122 117 self:ApplyAnchor()
flickerstreak@122 118 ReAction:RefreshBar(self)
flickerstreak@122 119 end
flickerstreak@122 120
flickerstreak@122 121 function Bar:GetAnchor()
flickerstreak@122 122 local c = self.config
flickerstreak@122 123 return (c.point or "CENTER"),
flickerstreak@122 124 (c.anchor or self:GetFrame():GetParent():GetName()),
flickerstreak@122 125 (c.relpoint or c.point or "CENTER"),
flickerstreak@122 126 (c.x or 0),
flickerstreak@122 127 (c.y or 0)
flickerstreak@122 128 end
flickerstreak@122 129
flickerstreak@122 130 function Bar:GetSize()
flickerstreak@122 131 local f = self:GetFrame()
flickerstreak@122 132 return f:GetWidth(), f:GetHeight()
flickerstreak@122 133 end
flickerstreak@122 134
flickerstreak@122 135 function Bar:SetSize(w,h)
flickerstreak@122 136 local f = self:GetFrame()
flickerstreak@122 137 self.config.width = w
flickerstreak@122 138 self.config.height = h
flickerstreak@122 139 f:SetWidth(w)
flickerstreak@122 140 f:SetHeight(h)
flickerstreak@122 141 end
flickerstreak@122 142
flickerstreak@122 143 function Bar:GetButtonSize()
flickerstreak@122 144 local w = self.config.btnWidth or 32
flickerstreak@122 145 local h = self.config.btnHeight or 32
flickerstreak@122 146 -- TODO: get from modules?
flickerstreak@122 147 return w,h
flickerstreak@122 148 end
flickerstreak@122 149
flickerstreak@122 150 function Bar:SetButtonSize(w,h)
flickerstreak@122 151 if w > 0 and h > 0 then
flickerstreak@122 152 self.config.btnWidth = w
flickerstreak@122 153 self.config.btnHeight = h
flickerstreak@122 154 end
flickerstreak@122 155 ReAction:RefreshBar(self)
flickerstreak@122 156 end
flickerstreak@122 157
flickerstreak@122 158 function Bar:GetButtonGrid()
flickerstreak@122 159 local cfg = self.config
flickerstreak@122 160 local r = cfg.btnRows or 1
flickerstreak@122 161 local c = cfg.btnColumns or 1
flickerstreak@122 162 local s = cfg.spacing or 4
flickerstreak@122 163 return r,c,s
flickerstreak@122 164 end
flickerstreak@122 165
flickerstreak@122 166 function Bar:GetNumButtons()
flickerstreak@122 167 local r,c = self:GetButtonGrid()
flickerstreak@122 168 return r*c
flickerstreak@122 169 end
flickerstreak@122 170
flickerstreak@122 171 function Bar:SetButtonGrid(r,c,s)
flickerstreak@122 172 if r > 0 and c > 0 and s > 0 then
flickerstreak@122 173 local cfg = self.config
flickerstreak@122 174 cfg.btnRows = r
flickerstreak@122 175 cfg.btnColumns = c
flickerstreak@122 176 cfg.spacing = s
flickerstreak@122 177 end
flickerstreak@122 178 ReAction:RefreshBar(self)
flickerstreak@122 179 end
flickerstreak@122 180
flickerstreak@122 181 function Bar:ClipNButtons( n )
flickerstreak@122 182 local cfg = self.config
flickerstreak@122 183 local r = cfg.btnRows or 1
flickerstreak@122 184 local c = cfg.btnColumns or 1
flickerstreak@122 185
flickerstreak@122 186 cfg.btnRows = ceil(n/c)
flickerstreak@122 187 cfg.btnColumns = min(n,c)
flickerstreak@122 188 end
flickerstreak@122 189
flickerstreak@122 190 function Bar:GetName()
flickerstreak@122 191 return self.name
flickerstreak@122 192 end
flickerstreak@122 193
flickerstreak@122 194 function Bar:GetFrame()
flickerstreak@122 195 -- this method is included for documentation purposes. It is overridden
flickerstreak@122 196 -- for each object in the :New() method.
flickerstreak@122 197 error("Invalid Bar object: used without initialization")
flickerstreak@122 198 end
flickerstreak@122 199
flickerstreak@122 200 -- only ReAction:RenameBar() should call this function. Calling from any other
flickerstreak@122 201 -- context will desync the bar list in the ReAction class.
flickerstreak@122 202 function Bar:SetName(name)
flickerstreak@122 203 self.name = name
flickerstreak@122 204 self:SetLabel(self.name) -- Bar:SetLabel() defined in Overlay.lua
flickerstreak@122 205 end
flickerstreak@122 206
flickerstreak@122 207 function Bar:GetAlpha()
flickerstreak@122 208 return self.config.alpha or 1.0
flickerstreak@122 209 end
flickerstreak@122 210
flickerstreak@122 211 function Bar:SetAlpha(value)
flickerstreak@122 212 self.config.alpha = value
flickerstreak@122 213 self:GetFrame():SetAlpha(value or 1.0)
flickerstreak@122 214 ReAction:RefreshBar(self)
flickerstreak@122 215 end
flickerstreak@122 216
flickerstreak@122 217 function Bar:AddButton(idx, button)
flickerstreak@122 218 local f = self:GetFrame()
flickerstreak@122 219
flickerstreak@122 220 -- store in a weak reverse-index array
flickerstreak@122 221 self.buttons[button] = idx
flickerstreak@122 222
flickerstreak@122 223 -- Store a properly wrapped reference to the child frame as an attribute
flickerstreak@122 224 -- (accessible via "frameref-btn#")
flickerstreak@122 225 f:SetFrameRef(format("btn%d",idx), button:GetFrame())
flickerstreak@122 226 end
flickerstreak@122 227
flickerstreak@122 228 function Bar:RemoveButton(button)
flickerstreak@122 229 local idx = self.buttons[button]
flickerstreak@122 230 if idx then
flickerstreak@122 231 self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil)
flickerstreak@122 232 self.buttons[button] = nil
flickerstreak@122 233 end
flickerstreak@122 234 end
flickerstreak@122 235
flickerstreak@122 236 -- iterator returns button, idx and does NOT iterate in index order
flickerstreak@122 237 function Bar:IterateButtons()
flickerstreak@122 238 return pairs(self.buttons)
flickerstreak@122 239 end
flickerstreak@122 240
flickerstreak@122 241 function Bar:PlaceButton(button, baseW, baseH)
flickerstreak@122 242 local idx = self.buttons[button]
flickerstreak@122 243 if idx then
flickerstreak@122 244 local r, c, s = self:GetButtonGrid()
flickerstreak@122 245 local bh, bw = self:GetButtonSize()
flickerstreak@122 246 local row, col = floor((idx-1)/c), fmod((idx-1),c) -- zero-based
flickerstreak@122 247 local x, y = col*bw + (col+0.5)*s, -(row*bh + (row+0.5)*s)
flickerstreak@122 248 local scale = bw/baseW
flickerstreak@122 249 local b = button:GetFrame()
flickerstreak@122 250
flickerstreak@122 251 b:ClearAllPoints()
flickerstreak@122 252 b:SetPoint("TOPLEFT",x/scale,y/scale)
flickerstreak@122 253 b:SetScale(scale)
flickerstreak@122 254 end
flickerstreak@122 255 end
flickerstreak@122 256
flickerstreak@122 257 function Bar:SkinButton()
flickerstreak@122 258 -- does nothing by default
flickerstreak@122 259 end