annotate Bar.lua @ 99:f200bcb193d6

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