forked from astralguild/AstralKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frame Functions.lua
280 lines (231 loc) · 7.57 KB
/
Frame Functions.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
276
277
278
279
280
local _, addon = ...
function addon.CreateCheckBox(parent, label, width)
local checkbox = CreateFrame('CheckButton', nil, parent, "BackdropTemplate")
checkbox:SetSize(width or 200, 20)
checkbox:SetBackdrop(nil)
checkbox:SetBackdropBorderColor(85/255, 85/255, 85/255)
checkbox:SetNormalFontObject(InterUIRegular_Normal)
checkbox:SetText(label)
checkbox:SetBackdropColor(0, 0, 0)
checkbox:SetPushedTextOffset(1,-1)
local tex = checkbox:CreateTexture('PUSHED_TEXTURE_BOX', 'BACKGROUND')
tex:SetSize(12, 12)
tex:SetPoint('LEFT', checkbox, 'LEFT', -2, 0)
tex:SetTexture('Interface\\AddOns\\AstralKeys\\Media\\box2.tga')
tex:SetVertexColor(0.3, 0.3, 0.3)
checkbox.t = checkbox:CreateTexture('PUSHEDTEXTURE', 'BACKGROUND')
checkbox.t:SetSize(12, 12)
checkbox.t:SetPoint('CENTER', tex, 'CENTER', 0, 0)
checkbox.t:SetTexture('Interface\\AddOns\\AstralKeys\\Media\\Texture\\[email protected]')
checkbox:SetCheckedTexture(checkbox.t)
if label then
checkbox:GetFontString():SetPoint('LEFT', tex, 'RIGHT', 5, 0)
end
return checkbox
end
local BACKDROP2 = {
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface\\ChatFrame\\ChatFrameBackground", tile = true, tileSize = 16, edgeSize = 1,
insets = {left = 0, right = 0, top = 0, bottom = 0}
}
local function AcquireButtonFromPool(parent)
local button
button = table.remove(parent.pool, 1)
return button
end
local function CreateButton(parent)
local button = AcquireButtonFromPool(parent)
if not button then
button = CreateFrame('BUTTON', nil, parent)
button:SetSize(140, 20)
button:SetNormalFontObject(InterUIMedium_Normal)
local texture = button:CreateTexture()
texture:SetColorTexture(0.5, 0.5, 0.5, 1)
texture:SetBlendMode('BLEND')
texture:SetPoint('TOPLEFT', 1, -1)
texture:SetPoint('BOTTOMRIGHT', -1, 1)
texture:SetGradient('HORIZONTAL', CreateColor(.5, .5, .5, .8), CreateColor(.5, .5, .5, 0))
button:SetHighlightTexture(texture)
button.menuTexture = button:CreateTexture(nil, 'ARTWORK')
button.menuTexture:SetSize(16, 16)
button.menuTexture:SetTexture('Interface\\AddOns\\AstralKeys\\Media\\Texture\\baseline_keyboard_arrow_right_white_24dp')
button.menuTexture:SetVertexColor(0.8, 0.8, 0.8, 0.8)
button.menuTexture:SetPoint('RIGHT', button, 'RIGHT', -5, 0)
function button:Recycle()
button:SetText(nil)
button:SetScript('OnClick', nil)
button:SetScript('OnShow', nil)
end
end
return button
end
local function CloseSubMenu(frame)
if not frame then return end
local nextTierFrame, tier = frame:GetName():match('([%a]+)(%d)')
nextTierFrame = nextTierFrame .. (tier + 1)
if _G[nextTierFrame] then _G[nextTierFrame]:Hide() end
end
local function CloseDropDownMenu(frame)
if not frame then return end
local baseFrame, tier = frame:GetName():match('([%a]+)(%d)')
for i = 1, tier do
if _G[baseFrame .. i] then _G[baseFrame .. i]:Hide() end
end
end
function addon.CreateDropDownFrame(name, level, parent)
local frame = CreateFrame('FRAME', 'AstralMenuFrame' .. name .. level, parent, "BackdropTemplate")
frame:Hide()
frame.tier = level
frame.buttons = {}
frame.pool = {}
frame.unit = ''
frame.units = {}
frame:SetFrameStrata('TOOLTIP')
frame:SetWidth(10)
frame:SetHeight(40)
frame:SetFrameLevel(level * 5)
frame:SetBackdrop(BACKDROP2)
frame:SetBackdropBorderColor(33/255, 33/255, 33/255, 0.8)
frame:SetBackdropColor(0, 0, 0)
frame.background = frame:CreateTexture(nil, 'BACKGROUND')
frame.background:SetAllPoints(frame)
frame.background:SetColorTexture(0, 0, 0, 1)
frame:EnableKeyboard(true)
frame:SetPropagateKeyboardInput(true)
frame:SetScript('OnKeyDown', function(self, key)
if key == 'ESCAPE' then
if self.tier == 1 then
self:SetPropagateKeyboardInput(false)
end
self:Hide()
end
end)
frame:SetScript('OnShow', function(self)
self:SetPropagateKeyboardInput(true)
end)
frame.title = frame:CreateFontString(nil, 'ARTWORK', 'InterUIBlack_Normal')
frame.title:SetNonSpaceWrap(false)
frame.title:SetJustifyH('LEFT')
frame.title:SetSize(150, 15)
frame.title:SetPoint('TOPLEFT', frame, 'TOPLEFT', 5, -5)
function frame:SetTitle(text)
self.title:SetText(text)
local stringLength = self.title:GetUnboundedStringWidth()
self.title:SetWidth(stringLength + 10)
self:AdjustWidth(stringLength)
end
function frame:AddUnit(unit)
self.units[unit] = true
end
function frame:RemoveUnit(unit)
self.units[unit] = nil
end
function frame:WipeUnits()
wipe(self.units)
end
function frame:GetUnits()
return self.units
end
function frame:SetUnit(unit)
self.unit = unit
end
function frame:AdjustWidth(overrideWidth)
local longestStringLength = 0
if not overrideWidth then
for i = 1, #self.buttons do
local btnStringLength = self.buttons[i]:GetFontString():GetUnboundedStringWidth() + (self.buttons[i].menuTexture:IsShown() and 20 or 0)
longestStringLength = math.max(longestStringLength, btnStringLength)
end
longestStringLength = math.max(longestStringLength, self.title:GetUnboundedStringWidth())
else
longestStringLength = overrideWidth
end
self:SetWidth(longestStringLength + 20)
for i = 1, #self.buttons do
self.buttons[i]:SetWidth(longestStringLength)
end
end
frame:SetScript('OnShow', function(self)
self:AdjustWidth()
end)
function frame:AddButton(name1, onClick, onShow, onEnter, subMenu, subFrame)
local button = CreateButton(self)
button:SetWidth(self:GetWidth() - 20)
button:SetPoint('TOPLEFT', self.title, 'BOTTOMLEFT', 5, -20*(#self.buttons))
button:SetText(name1)
local stringLength = button:GetFontString():GetUnboundedStringWidth()
if not subMenu then
button.menuTexture:Hide()
button:SetScript('OnClick', function(self)
if onClick then onClick(self) end
CloseDropDownMenu(self:GetParent())
end)
button:SetScript('OnEnter', function(self)
CloseSubMenu(self:GetParent())
end)
else
button:SetWidth(self:GetWidth() + 15)
stringLength = stringLength + 40 -- to account for sub menu texture and padding on both sides of it
button.menuTexture:Show()
button:SetScript('OnEnter', function(self)
subFrame:SetPoint('TOPLEFT', self, 'TOPRIGHT', -5, 0)
subFrame:SetShown(true)
end)
-- Show an arrow texture to indicate a menu
button:SetScript('OnClick', function(self)
if onClick then onClick(self) end
--subFrame:SetPoint('TOPLEFT', self, 'TOPRIGHT', -5, 0)
--subFrame:SetShown(not subFrame:IsShown())
end)
end
if onEnter then
button:HookScript('OnEnter', onEnter)
end
button:SetScript('OnShow', onShow)
table.insert(self.buttons, button)
self:SetHeight(#self.buttons * 20 + 30)
self:AdjustWidth()
return button
end
frame:SetScript('OnHide', function(self)
self:WipeUnits()
end)
function frame:ClearButtons()
for i = #self.buttons, 1, -1 do
self.buttons[i]:Recycle()
table.insert(self.pool, table.remove(self.buttons, i))
end
end
return frame
end
function addon.AddEscHandler(frame)
if not frame and type(frame) ~= 'table' then
error('frame expcted, got '.. type(frame))
end
if frame:GetScript('OnKeyDown') then
frame:HookScript('OnKeyDown', function(self, key)
if key == 'ESCAPE' then
self:SetPropagateKeyboardInput(false)
self:Hide()
end
end)
else
frame:EnableKeyboard(true)
frame:SetPropagateKeyboardInput(true)
frame:SetScript('OnKeyDown', function(self, key)
if key == 'ESCAPE' then
self:SetPropagateKeyboardInput(false)
self:Hide()
end
end)
end
if frame:GetScript('OnShow') then
frame:HookScript('OnShow', function(self)
self:SetPropagateKeyboardInput(true)
end)
else
frame:SetScript('OnShow', function(self)
self:SetPropagateKeyboardInput(true)
end)
end
end