comparison modules/Bar.lua @ 109:410d036c43b2

- reorganize modularity file structure (part 1)
author Flick <flickerstreak@gmail.com>
date Thu, 08 Jan 2009 00:57:27 +0000
parents Bar.lua@b2fb8f7dc780
children
comparison
equal deleted inserted replaced
108:b2fb8f7dc780 109:410d036c43b2
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 -- Override the default frame accessor to provide strict read-only access
47 function self:GetFrame()
48 return f
49 end
50
51 self:ApplyAnchor()
52 ReAction.RegisterCallback(self, "OnConfigModeChanged")
53
54 return self
55 end
56
57 function Bar:Destroy()
58 local f = self:GetFrame()
59 f:UnregisterAllEvents()
60 ReAction.UnregisterAllCallbacks(self)
61 f:Hide()
62 f:SetParent(UIParent)
63 f:ClearAllPoints()
64 end
65
66 function Bar:OnConfigModeChanged(event, mode)
67 self:ShowControls(mode) -- Bar:ShowControls() defined in Overlay.lua
68 end
69
70 function Bar:ApplyAnchor()
71 local f = self:GetFrame()
72 local c = self.config
73 local p = c.point
74
75 f:SetWidth(c.width)
76 f:SetHeight(c.height)
77 f:ClearAllPoints()
78
79 if p then
80 local a = f:GetParent()
81 if c.anchor then
82 local bar = ReAction:GetBar(c.anchor)
83 if bar then
84 a = bar:GetFrame()
85 else
86 a = _G[c.anchor]
87 end
88 end
89 local fr = a or f:GetParent()
90 f:SetPoint(p, a or f:GetParent(), c.relpoint, c.x or 0, c.y or 0)
91 else
92 f:SetPoint("CENTER")
93 end
94 end
95
96 function Bar:SetAnchor(point, frame, relativePoint, x, y)
97 local c = self.config
98 c.point = point or c.point
99 c.anchor = frame or c.anchor
100 c.relpoint = relativePoint or c.relpoint
101 c.x = x or c.x
102 c.y = y or c.y
103 self:ApplyAnchor()
104 ReAction:RefreshBar(self)
105 end
106
107 function Bar:GetAnchor()
108 local c = self.config
109 return (c.point or "CENTER"),
110 (c.anchor or self:GetFrame():GetParent():GetName()),
111 (c.relpoint or c.point or "CENTER"),
112 (c.x or 0),
113 (c.y or 0)
114 end
115
116 function Bar:GetSize()
117 local f = self:GetFrame()
118 return f:GetWidth(), f:GetHeight()
119 end
120
121 function Bar:SetSize(w,h)
122 local f = self:GetFrame()
123 self.config.width = w
124 self.config.height = h
125 f:SetWidth(w)
126 f:SetHeight(h)
127 end
128
129 function Bar:GetButtonSize()
130 local w = self.config.btnWidth or 32
131 local h = self.config.btnHeight or 32
132 -- TODO: get from modules?
133 return w,h
134 end
135
136 function Bar:SetButtonSize(w,h)
137 if w > 0 and h > 0 then
138 self.config.btnWidth = w
139 self.config.btnHeight = h
140 end
141 ReAction:RefreshBar(self)
142 end
143
144 function Bar:GetButtonGrid()
145 local cfg = self.config
146 local r = cfg.btnRows or 1
147 local c = cfg.btnColumns or 1
148 local s = cfg.spacing or 4
149 return r,c,s
150 end
151
152 function Bar:GetNumButtons()
153 local r,c = self:GetButtonGrid()
154 return r*c
155 end
156
157 function Bar:SetButtonGrid(r,c,s)
158 if r > 0 and c > 0 and s > 0 then
159 local cfg = self.config
160 cfg.btnRows = r
161 cfg.btnColumns = c
162 cfg.spacing = s
163 end
164 ReAction:RefreshBar(self)
165 end
166
167 function Bar:ClipNButtons( n )
168 local cfg = self.config
169 local r = cfg.btnRows or 1
170 local c = cfg.btnColumns or 1
171
172 cfg.btnRows = ceil(n/c)
173 cfg.btnColumns = min(n,c)
174 end
175
176 function Bar:GetName()
177 return self.name
178 end
179
180 function Bar:GetFrame()
181 -- this method is included for documentation purposes. It is overridden
182 -- for each object in the :New() method.
183 error("Invalid Bar object: used without initialization")
184 end
185
186 -- only ReAction:RenameBar() should call this function. Calling from any other
187 -- context will desync the bar list in the ReAction class.
188 function Bar:SetName(name)
189 self.name = name
190 self:SetLabel(self.name) -- Bar:SetLabel() defined in Overlay.lua
191 end
192
193 function Bar:GetAlpha()
194 return self.config.alpha or 1.0
195 end
196
197 function Bar:SetAlpha(value)
198 self.config.alpha = value
199 self:GetFrame():SetAlpha(value or 1.0)
200 ReAction:RefreshBar(self)
201 end
202
203 function Bar:AddButton(idx, button)
204 local f = self:GetFrame()
205
206 -- store in a weak reverse-index array
207 self.buttons[button] = idx
208
209 -- Store a properly wrapped reference to the child frame as an attribute
210 -- (accessible via "frameref-btn#")
211 f:SetFrameRef(format("btn%d",idx), button:GetFrame())
212 end
213
214 function Bar:RemoveButton(button)
215 local idx = self.buttons[button]
216 if idx then
217 self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil)
218 self.buttons[button] = nil
219 end
220 end
221
222 -- iterator returns button, idx and does NOT iterate in index order
223 function Bar:IterateButtons()
224 return pairs(self.buttons)
225 end
226
227 function Bar:PlaceButton(button, baseW, baseH)
228 local idx = self.buttons[button]
229 if idx then
230 local r, c, s = self:GetButtonGrid()
231 local bh, bw = self:GetButtonSize()
232 local row, col = floor((idx-1)/c), fmod((idx-1),c) -- zero-based
233 local x, y = col*bw + (col+0.5)*s, -(row*bh + (row+0.5)*s)
234 local scale = bw/baseW
235 local b = button:GetFrame()
236
237 b:ClearAllPoints()
238 b:SetPoint("TOPLEFT",x/scale,y/scale)
239 b:SetScale(scale)
240 end
241 end
242
243 function Bar:SkinButton()
244 -- does nothing by default
245 end