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