forked from Nevcairiel/Bartender4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonBar.lua
275 lines (224 loc) · 6.5 KB
/
ButtonBar.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
--[[
Copyright (c) 2009-2017, Hendrik "Nevcairiel" Leppkes < h.leppkes at gmail dot com >
All rights reserved.
]]
--[[ Generic Template for a Bar which contains Buttons ]]
local _, Bartender4 = ...
local Bar = Bartender4.Bar.prototype
local WoW10 = select(4, GetBuildInfo()) >= 100000
local setmetatable, tostring, pairs = setmetatable, tostring, pairs
local ButtonBar = setmetatable({}, {__index = Bar})
local ButtonBar_MT = {__index = ButtonBar}
local defaults = Bartender4.Util:Merge({
padding = 2,
rows = 1,
hidemacrotext = false,
hidehotkey = false,
hideequipped = false,
hideborder = false,
skin = {
Zoom = false,
},
}, Bartender4.Bar.defaults)
Bartender4.ButtonBar = {}
Bartender4.ButtonBar.prototype = ButtonBar
Bartender4.ButtonBar.defaults = defaults
local Masque = LibStub("Masque", true)
function Bartender4.ButtonBar:Create(id, config, name, noSkinning)
local bar = setmetatable(Bartender4.Bar:Create(id, config, name), ButtonBar_MT)
if Masque and not noSkinning then
bar.MasqueGroup = Masque:Group("Bartender4", name, tostring(id))
end
return bar
end
ButtonBar.BT4BarType = "ButtonBar"
function ButtonBar:ApplyConfig(config)
Bar.ApplyConfig(self, config)
-- any module inherting this template should call UpdateButtonLayout after setting up its buttons, we cannot call it here
--self:UpdateButtonLayout()
end
function ButtonBar:UpdateButtonConfig()
end
-- get the current padding
function ButtonBar:GetPadding()
return self.config.padding
end
-- set the padding and refresh layout
function ButtonBar:SetPadding(pad)
if pad ~= nil then
self.config.padding = pad
end
self:UpdateButtonLayout()
end
-- get the current number of rows
function ButtonBar:GetRows()
return self.config.rows
end
-- set the number of rows and refresh layout
function ButtonBar:SetRows(rows)
if rows ~= nil then
self.config.rows = rows
end
self:UpdateButtonLayout()
end
function ButtonBar:GetZoom()
return self.config.skin.Zoom
end
function ButtonBar:SetZoom(zoom)
self.config.skin.Zoom = zoom
self:UpdateButtonConfig()
self:UpdateButtonLayout()
end
function ButtonBar:SetHideMacroText(state)
if state ~= nil then
self.config.hidemacrotext = state
end
self:UpdateButtonConfig()
end
function ButtonBar:GetHideMacroText()
return self.config.hidemacrotext
end
function ButtonBar:SetHideHotkey(state)
if state ~= nil then
self.config.hidehotkey = state
end
self:UpdateButtonConfig()
end
function ButtonBar:GetHideHotkey()
return self.config.hidehotkey
end
function ButtonBar:SetHideEquipped(state)
if state ~= nil then
self.config.hideequipped = state
end
self:UpdateButtonConfig()
end
function ButtonBar:GetHideEquipped()
return self.config.hideequipped
end
function ButtonBar:SetHideBorder(state)
if state ~= nil then
self.config.hideborder = state
end
self:UpdateButtonConfig()
end
function ButtonBar:GetHideBorder()
return self.config.hideborder
end
function ButtonBar:SetHGrowth(value)
self.config.position.growHorizontal = value
self:AnchorOverlay()
self:UpdateButtonLayout()
end
function ButtonBar:GetHGrowth()
return self.config.position.growHorizontal
end
function ButtonBar:SetVGrowth(value)
self.config.position.growVertical = value
self:AnchorOverlay()
self:UpdateButtonLayout()
end
function ButtonBar:GetVGrowth()
return self.config.position.growVertical
end
ButtonBar.ClickThroughSupport = true
function ButtonBar:SetClickThrough(click)
if click ~= nil then
self.config.clickthrough = click
end
self:ForAll("EnableMouse", not self.config.clickthrough)
end
local math_floor = math.floor
local math_ceil = math.ceil
-- align the buttons and correct the size of the bar overlay frame
ButtonBar.button_width = WoW10 and 45 or 36
ButtonBar.button_height = WoW10 and 45 or 36
function ButtonBar:UpdateButtonLayout()
local buttons = self.buttons
local pad = self:GetPadding()
local numbuttons = self.numbuttons or #buttons
-- bail out if the bar has no buttons, for whatever reason
-- (eg. stanceless class, or no stances learned yet, etc.)
if numbuttons == 0 then return end
local Rows = self:GetRows()
local ButtonPerRow = math_ceil(numbuttons / Rows) -- just a precaution
Rows = math_ceil(numbuttons / ButtonPerRow)
if Rows > numbuttons then
Rows = numbuttons
ButtonPerRow = 1
end
local hpad = pad + (self.hpad_offset or 0)
local vpad = pad + (self.vpad_offset or 0)
self:SetSize((self.button_width + hpad) * ButtonPerRow - hpad + 8, (self.button_height + vpad) * Rows - vpad + 8)
local h1, h2, v1, v2
local xOff, yOff
if self.config.position.growHorizontal == "RIGHT" then
h1, h2 = "LEFT", "RIGHT"
xOff = 5
elseif self.config.position.growHorizontal == "LEFT" then
h1, h2 = "RIGHT", "LEFT"
xOff = -3
hpad = -hpad
elseif self.config.position.growHorizontal == "BOTH" then
h1, h2 = "LEFT", "RIGHT"
xOff = (self.button_width + hpad) * (ButtonPerRow - 1) / -2
end
if self.config.position.growVertical == "DOWN" then
v1, v2 = "TOP", "BOTTOM"
yOff = -3
else
v1, v2 = "BOTTOM", "TOP"
yOff = 5
vpad = -vpad
end
local valign = "TOP"
-- variable in-row button alignment
if self.config.verticalAlignment then
if self.config.verticalAlignment == "CENTER" then
valign = ""
elseif self.config.verticalAlignment == "BOTTOM" then
valign = "BOTTOM"
end
-- otherwise, top
end
-- anchor button 1
local anchor = self:GetAnchor()
buttons[1]:ClearSetPoint(anchor, self, anchor, xOff - (self.hpad_offset or 0), yOff - (self.vpad_offset or 0))
-- and anchor all other buttons relative to our button 1
for i = 2, numbuttons do
-- jump into a new row
if ((i-1) % ButtonPerRow) == 0 then
buttons[i]:ClearSetPoint(v1 .. h1, buttons[i-ButtonPerRow], v2 .. h1, 0, -vpad)
-- align to the previous button
else
buttons[i]:ClearSetPoint(valign .. h1, buttons[i-1], valign .. h2, hpad, 0)
end
end
if not Masque then
for i = 1, #buttons do
local button = buttons[i]
if button.icon and self.config.skin.Zoom then
button.icon:SetTexCoord(0.07,0.93,0.07,0.93)
elseif button.icon then
button.icon:SetTexCoord(0,1,0,1)
end
end
end
end
--[[===================================================================================
Utility function
===================================================================================]]--
-- get a iterator over all buttons
function ButtonBar:GetAll()
return pairs(self.buttons)
end
-- execute a member function on all buttons
function ButtonBar:ForAll(method, ...)
if not self.buttons then return end
for _, button in self:GetAll() do
local func = button[method]
if func then
func(button, ...)
end
end
end