comparison classes/Button.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 --[[
2 ReAction Button base class
3 --]]
4
5 -- local imports
6 local ReAction = ReAction
7 local L = ReAction.L
8 local _G = _G
9 local CreateFrame = CreateFrame
10 local GetBindingKey = GetBindingKey
11 local format = string.format
12
13 ReAction:UpdateRevision("$Revision: 154 $")
14
15 -- libraries
16 local KB = LibStub("LibKeyBound-1.0")
17 local LBF = LibStub("LibButtonFacade",true) -- optional
18
19 -- private
20 local trash = CreateFrame("Frame")
21 local frameList = { }
22 local idPools = { }
23
24 local function kb_onEnter( frame )
25 KB:Set(frame)
26 end
27
28 -- Button class
29 local Button = { }
30
31 ReAction.Button = Button -- export to ReAction
32
33 function Button:New( name, config, bar, idx, inherits, buttonType )
34 buttonType = buttonType or "CheckButton"
35
36 -- create new self
37 self = setmetatable(
38 {
39 bar = bar,
40 idx = idx,
41 config = config,
42 name = name,
43 },
44 { __index = self } )
45
46 -- have to recycle frames with the same name: CreateFrame() doesn't overwrite
47 -- existing globals. Can't set to nil in the global because it's then tainted.
48 -- Caller is responsible for ensuring global uniqueness of names.
49 local f = name and frameList[name]
50 if f then
51 f:SetParent(bar:GetFrame())
52 else
53 f = CreateFrame(buttonType, name, bar:GetFrame(), inherits)
54 if name then
55 frameList[name] = f
56 end
57 end
58
59 self.frame = f
60
61 if config then
62 config.name = name
63 end
64
65 -- install LibKeyBound handlers onto frame
66 function f:GetActionName()
67 return format("%s:%s", bar:GetName(), idx)
68 end
69
70 local clickBinding = format("CLICK %s:LeftButton", name)
71 function f:GetHotkey()
72 return KB:ToShortKey(GetBindingKey(clickBinding))
73 end
74
75 return self
76 end
77
78 function Button:Destroy()
79 local f = self.frame
80 gridProxy:RemoveGridFrame(f)
81 f:Hide()
82 f:SetParent(trash)
83 f:ClearAllPoints()
84 end
85
86 function Button:GetFrame()
87 return self.frame
88 end
89
90 function Button:GetName()
91 return self.name
92 end
93
94 function Button:GetConfig()
95 return self.config
96 end
97
98 function Button:GetActionID()
99 -- derived classes should override this
100 return nil
101 end
102
103 function Button:SetActionIDPool( poolID, maxID )
104 self.actionPoolID = poolID
105 self.actionMaxID = maxID
106 end
107
108 function Button:AcquireActionID( id, hint, unique )
109 local poolID = self.actionPoolID
110 local maxID = self.actionMaxID
111 if not poolID or not maxID then
112 error("AcquireActionID: must setup pool first with SetActionIDPool")
113 end
114 local pool = pools[poolID]
115 if not pool then
116 pool = { nWraps = 0, useCount = { } }
117 for i = 1, maxID do
118 pool.useCount[i] = 0
119 end
120 pools[poolID] = pool
121 end
122 local useCount = pool.useCount
123 if id == nil then
124 repeat
125 local nWraps = pool.nWraps
126 if useCount[hint] == nil or useCount[hint] == nWraps then
127 id = hint
128 else
129 local start = hint or 1
130 for i = start, maxID do
131 if useCount[i] == nil or useCount[i] == nWraps then
132 id = i
133 break
134 end
135 end
136 if not id then
137 for i = 1, start do
138 if useCount[i] == nil or useCount[i] == nWraps then
139 id = i
140 break
141 end
142 end
143 end
144 end
145 if id == nil then
146 if unique then
147 return nil
148 end
149 pool.nWraps = nWraps + 1
150 end
151 until id
152 end
153 useCount[id] = (useCount[id] or 0) + 1
154 return id
155 end
156
157 function Button:ReleaseActionID( id )
158 local poolID = self.actionPoolID
159 if not poolID then
160 error("ReleaseActionID: must setup pool first with SetActionIDPool")
161 end
162 local pool = pools[poolID]
163 if pool and id and pool.useCount[id] then
164 pool.useCount[id] = pool.useCount[id] - 1
165 pool.nWraps = min(pool.useCount[id], pool.nWraps)
166 end
167 end
168
169 function Button:Refresh()
170 self.bar:PlaceButton( self, self.frame:GetWidth(), self.frame:GetHeight() )
171 end
172
173 function Button:SetKeybindMode( mode )
174 local f = self.frame
175 if mode then
176 self.oldOnEnter = f:GetScript("OnEnter")
177 f:SetScript("OnEnter", kb_onEnter)
178 else
179 f:SetScript("OnEnter", self.oldOnEnter)
180 self.oldOnEnter = nil
181 end
182 self:UpdateKeybindModeDisplay( mode )
183 end
184
185 function Button:UpdateKeybindModeDisplay( mode )
186 self.border = self.border or _G[format("%sBorder",tostring(self:GetName()))]
187 if self.border then
188 if mode then
189 self.border:SetVertexColor(KB:GetColorKeyBoundMode())
190 self.border:Show()
191 else
192 self.border:Hide()
193 end
194 end
195 end
196
197 function Button:UpdateHotkey( hotkey )
198 hotkey = hotkey or self.hotkey
199 if not hotkey then
200 hotkey = _G[format("%sHotKey", tostring(self:GetName()))]
201 self.hotkey = hotkey
202 end
203 if hotkey then
204 local txt = self.frame:GetHotkey()
205 hotkey:SetText( txt )
206 if txt == nil or txt == "" then
207 hotkey:Hide()
208 else
209 hotkey:Show()
210 end
211 end
212 end
213
214 function Button:GetActionIDLabel( create )
215 local f = self.frame
216 if not f.actionIDLabel and create then
217 local label = f:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
218 label:SetAllPoints()
219 label:SetJustifyH("CENTER")
220 label:SetShadowColor(0,0,0,1)
221 label:SetShadowOffset(2,-2)
222 f.actionIDLabel = label -- store the label with the frame for recycling
223 end
224 return f.actionIDLabel
225 end
226
227 function Button:UpdateActionIDLabel( show )
228 local label = self:GetActionIDLabel( show )
229 if label then
230 if show then
231 local id = self:GetActionID()
232 if id then
233 label:SetText(tostring(id))
234 label:Show()
235 return
236 end
237 end
238 label:Hide()
239 end
240 end
241
242 function Button:SetNormalVertexColor( r, g, b, a )
243 if LBF then
244 LBF:SetNormalVertexColor(self.frame, r, g, b, a)
245 else
246 self.frame:GetNormalTexture():SetVertexColor(r,g,b,a)
247 end
248 end
249
250 function Button:GetNormalVertexColor()
251 if LBF then
252 return LBF:GetNormalVertexColor(self.frame)
253 else
254 return self.frame:GetNormalTexture():GetVertexColor()
255 end
256 end
257
258 function Button:ShowGrid( show )
259 if not InCombatLockdown() then
260 local f = self.frame
261 local count = f:GetAttribute("showgrid")
262 if show then
263 count = count + 1
264 else
265 count = count - 1
266 end
267 if count < 0 then
268 count = 0
269 end
270 f:SetAttribute("showgrid",count)
271 self:UpdateShowGrid()
272 end
273 end
274
275 function Button:UpdateShowGrid()
276 -- does nothing by default
277 end