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