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@126
|
91 if f then
|
flickerstreak@126
|
92 f:Hide()
|
flickerstreak@126
|
93 f:SetParent(trash)
|
flickerstreak@126
|
94 f:ClearAllPoints()
|
flickerstreak@126
|
95 end
|
flickerstreak@122
|
96 end
|
flickerstreak@122
|
97
|
flickerstreak@128
|
98 function Button:GetBar()
|
flickerstreak@128
|
99 return self.bar
|
flickerstreak@128
|
100 end
|
flickerstreak@128
|
101
|
flickerstreak@122
|
102 function Button:GetFrame()
|
flickerstreak@122
|
103 return self.frame
|
flickerstreak@122
|
104 end
|
flickerstreak@122
|
105
|
flickerstreak@122
|
106 function Button:GetName()
|
flickerstreak@122
|
107 return self.name
|
flickerstreak@122
|
108 end
|
flickerstreak@122
|
109
|
flickerstreak@122
|
110 function Button:GetConfig()
|
flickerstreak@122
|
111 return self.config
|
flickerstreak@122
|
112 end
|
flickerstreak@122
|
113
|
flickerstreak@122
|
114 function Button:GetActionID()
|
flickerstreak@122
|
115 -- derived classes should override this
|
flickerstreak@122
|
116 return nil
|
flickerstreak@122
|
117 end
|
flickerstreak@122
|
118
|
flickerstreak@122
|
119 function Button:SetActionIDPool( poolID, maxID )
|
flickerstreak@122
|
120 self.actionPoolID = poolID
|
flickerstreak@122
|
121 self.actionMaxID = maxID
|
flickerstreak@122
|
122 end
|
flickerstreak@122
|
123
|
flickerstreak@122
|
124 function Button:AcquireActionID( id, hint, unique )
|
flickerstreak@122
|
125 local poolID = self.actionPoolID
|
flickerstreak@122
|
126 local maxID = self.actionMaxID
|
flickerstreak@122
|
127 if not poolID or not maxID then
|
flickerstreak@122
|
128 error("AcquireActionID: must setup pool first with SetActionIDPool")
|
flickerstreak@122
|
129 end
|
flickerstreak@123
|
130 local pool = idPools[poolID]
|
flickerstreak@122
|
131 if not pool then
|
flickerstreak@122
|
132 pool = { nWraps = 0, useCount = { } }
|
flickerstreak@122
|
133 for i = 1, maxID do
|
flickerstreak@122
|
134 pool.useCount[i] = 0
|
flickerstreak@122
|
135 end
|
flickerstreak@123
|
136 idPools[poolID] = pool
|
flickerstreak@122
|
137 end
|
flickerstreak@122
|
138 local useCount = pool.useCount
|
flickerstreak@122
|
139 if id == nil then
|
flickerstreak@122
|
140 repeat
|
flickerstreak@126
|
141 local nWraps = pool.nWraps or 0
|
flickerstreak@126
|
142 if hint and (useCount[hint] == nil or useCount[hint] == nWraps) then
|
flickerstreak@122
|
143 id = hint
|
flickerstreak@122
|
144 else
|
flickerstreak@122
|
145 local start = hint or 1
|
flickerstreak@122
|
146 for i = start, maxID do
|
flickerstreak@122
|
147 if useCount[i] == nil or useCount[i] == nWraps then
|
flickerstreak@122
|
148 id = i
|
flickerstreak@122
|
149 break
|
flickerstreak@122
|
150 end
|
flickerstreak@122
|
151 end
|
flickerstreak@122
|
152 if not id then
|
flickerstreak@122
|
153 for i = 1, start do
|
flickerstreak@122
|
154 if useCount[i] == nil or useCount[i] == nWraps then
|
flickerstreak@122
|
155 id = i
|
flickerstreak@122
|
156 break
|
flickerstreak@122
|
157 end
|
flickerstreak@122
|
158 end
|
flickerstreak@122
|
159 end
|
flickerstreak@122
|
160 end
|
flickerstreak@122
|
161 if id == nil then
|
flickerstreak@122
|
162 if unique then
|
flickerstreak@122
|
163 return nil
|
flickerstreak@122
|
164 end
|
flickerstreak@122
|
165 pool.nWraps = nWraps + 1
|
flickerstreak@122
|
166 end
|
flickerstreak@126
|
167 until id ~= nil
|
flickerstreak@122
|
168 end
|
flickerstreak@122
|
169 useCount[id] = (useCount[id] or 0) + 1
|
flickerstreak@122
|
170 return id
|
flickerstreak@122
|
171 end
|
flickerstreak@122
|
172
|
flickerstreak@122
|
173 function Button:ReleaseActionID( id )
|
flickerstreak@122
|
174 local poolID = self.actionPoolID
|
flickerstreak@122
|
175 if not poolID then
|
flickerstreak@122
|
176 error("ReleaseActionID: must setup pool first with SetActionIDPool")
|
flickerstreak@122
|
177 end
|
flickerstreak@123
|
178 local pool = idPools[poolID]
|
flickerstreak@122
|
179 if pool and id and pool.useCount[id] then
|
flickerstreak@122
|
180 pool.useCount[id] = pool.useCount[id] - 1
|
flickerstreak@122
|
181 pool.nWraps = min(pool.useCount[id], pool.nWraps)
|
flickerstreak@122
|
182 end
|
flickerstreak@122
|
183 end
|
flickerstreak@122
|
184
|
flickerstreak@122
|
185 function Button:Refresh()
|
flickerstreak@129
|
186 local f = self:GetFrame()
|
flickerstreak@129
|
187 self.bar:PlaceButton( self, f:GetWidth(), f:GetHeight() )
|
flickerstreak@122
|
188 end
|
flickerstreak@122
|
189
|
flickerstreak@122
|
190 function Button:SetKeybindMode( mode )
|
flickerstreak@122
|
191 local f = self.frame
|
flickerstreak@122
|
192 if mode then
|
flickerstreak@122
|
193 self.oldOnEnter = f:GetScript("OnEnter")
|
flickerstreak@122
|
194 f:SetScript("OnEnter", kb_onEnter)
|
flickerstreak@124
|
195 elseif self.oldOnEnter then
|
flickerstreak@122
|
196 f:SetScript("OnEnter", self.oldOnEnter)
|
flickerstreak@122
|
197 self.oldOnEnter = nil
|
flickerstreak@122
|
198 end
|
flickerstreak@128
|
199 self:ShowGridTemp(mode)
|
flickerstreak@122
|
200 self:UpdateKeybindModeDisplay( mode )
|
flickerstreak@122
|
201 end
|
flickerstreak@122
|
202
|
flickerstreak@122
|
203 function Button:UpdateKeybindModeDisplay( mode )
|
flickerstreak@131
|
204 local border = self.frames.border or _G[format("%sBorder",tostring(self:GetName()))]
|
flickerstreak@131
|
205 if border then
|
flickerstreak@122
|
206 if mode then
|
flickerstreak@131
|
207 border:SetVertexColor(KB:GetColorKeyBoundMode())
|
flickerstreak@131
|
208 border:Show()
|
flickerstreak@122
|
209 else
|
flickerstreak@131
|
210 border:Hide()
|
flickerstreak@122
|
211 end
|
flickerstreak@122
|
212 end
|
flickerstreak@122
|
213 end
|
flickerstreak@122
|
214
|
flickerstreak@122
|
215 function Button:UpdateHotkey( hotkey )
|
flickerstreak@131
|
216 hotkey = hotkey or self.frames.hotkey
|
flickerstreak@122
|
217 if not hotkey then
|
flickerstreak@129
|
218 hotkey = _G[self:GetName().."HotKey"]
|
flickerstreak@131
|
219 self.frames.hotkey = hotkey
|
flickerstreak@122
|
220 end
|
flickerstreak@122
|
221 if hotkey then
|
flickerstreak@122
|
222 local txt = self.frame:GetHotkey()
|
flickerstreak@122
|
223 hotkey:SetText( txt )
|
flickerstreak@122
|
224 if txt == nil or txt == "" then
|
flickerstreak@122
|
225 hotkey:Hide()
|
flickerstreak@122
|
226 else
|
flickerstreak@122
|
227 hotkey:Show()
|
flickerstreak@122
|
228 end
|
flickerstreak@122
|
229 end
|
flickerstreak@122
|
230 end
|
flickerstreak@122
|
231
|
flickerstreak@122
|
232 function Button:GetActionIDLabel( create )
|
flickerstreak@129
|
233 local f = self:GetFrame()
|
flickerstreak@122
|
234 if not f.actionIDLabel and create then
|
flickerstreak@122
|
235 local label = f:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
|
flickerstreak@122
|
236 label:SetAllPoints()
|
flickerstreak@122
|
237 label:SetJustifyH("CENTER")
|
flickerstreak@122
|
238 label:SetShadowColor(0,0,0,1)
|
flickerstreak@122
|
239 label:SetShadowOffset(2,-2)
|
flickerstreak@122
|
240 f.actionIDLabel = label -- store the label with the frame for recycling
|
flickerstreak@122
|
241 end
|
flickerstreak@122
|
242 return f.actionIDLabel
|
flickerstreak@122
|
243 end
|
flickerstreak@122
|
244
|
flickerstreak@122
|
245 function Button:UpdateActionIDLabel( show )
|
flickerstreak@122
|
246 local label = self:GetActionIDLabel( show )
|
flickerstreak@122
|
247 if label then
|
flickerstreak@122
|
248 if show then
|
flickerstreak@122
|
249 local id = self:GetActionID()
|
flickerstreak@122
|
250 if id then
|
flickerstreak@122
|
251 label:SetText(tostring(id))
|
flickerstreak@122
|
252 label:Show()
|
flickerstreak@122
|
253 return
|
flickerstreak@122
|
254 end
|
flickerstreak@122
|
255 end
|
flickerstreak@122
|
256 label:Hide()
|
flickerstreak@122
|
257 end
|
flickerstreak@122
|
258 end
|
flickerstreak@122
|
259
|
flickerstreak@122
|
260 function Button:SetNormalVertexColor( r, g, b, a )
|
flickerstreak@122
|
261 if LBF then
|
flickerstreak@129
|
262 LBF:SetNormalVertexColor(self:GetFrame(), r, g, b, a)
|
flickerstreak@122
|
263 else
|
flickerstreak@129
|
264 self:GetFrame():GetNormalTexture():SetVertexColor(r,g,b,a)
|
flickerstreak@122
|
265 end
|
flickerstreak@122
|
266 end
|
flickerstreak@122
|
267
|
flickerstreak@122
|
268 function Button:GetNormalVertexColor()
|
flickerstreak@122
|
269 if LBF then
|
flickerstreak@129
|
270 return LBF:GetNormalVertexColor(self:GetFrame())
|
flickerstreak@122
|
271 else
|
flickerstreak@129
|
272 return self:GetFrame():GetNormalTexture():GetVertexColor()
|
flickerstreak@122
|
273 end
|
flickerstreak@122
|
274 end
|
flickerstreak@122
|
275
|
flickerstreak@122
|
276 function Button:UpdateShowGrid()
|
flickerstreak@122
|
277 -- does nothing by default
|
flickerstreak@122
|
278 end
|
flickerstreak@128
|
279
|
flickerstreak@128
|
280 function Button:ShowGridTemp(show)
|
flickerstreak@128
|
281 -- does nothing by default
|
flickerstreak@128
|
282 end
|
flickerstreak@128
|
283
|
flickerstreak@128
|
284 function Button:ShowGrid(show)
|
flickerstreak@128
|
285 -- does nothing by default
|
flickerstreak@128
|
286 end
|