forked from tullamods/Dominos_Cast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediaPanel.lua
More file actions
executable file
·287 lines (228 loc) · 7.38 KB
/
mediaPanel.lua
File metadata and controls
executable file
·287 lines (228 loc) · 7.38 KB
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
281
282
283
284
285
286
287
local AddonName = ...
local Addon = LibStub('AceAddon-3.0'):GetAddon(GetAddOnDependencies(AddonName))
local LibSharedMedia = LibStub('LibSharedMedia-3.0')
local NUM_ITEMS, WIDTH, HEIGHT, OFFSET = 8, 150, 20, 0
--[[ buttons ]]--
local mediaButton = Addon:CreateClass('Button')
do
function mediaButton:New(parent)
local button = self:Bind(CreateFrame('Button', nil, parent))
button:SetSize(WIDTH, HEIGHT)
button:SetNormalFontObject('GameFontNormal')
button:SetHighlightFontObject('GameFontHighlight')
button:SetScript('OnEnter', function(self)
self:GetFontString():SetTextColor(1,1,1,1)
end)
button:SetScript('OnLeave', function(self)
self:GetFontString():SetTextColor(1,1,0,1)
end)
return button
end
function mediaButton:Set(mType, getFileName)
local fileName = getFileName()
local filePath = LibSharedMedia:Fetch(mType, fileName)
self:SetText(fileName)
if mType == 'border' then
self:SetBackdrop({
edgeFile = filePath,
edgeSize = 16,
})
elseif (mType == 'background') or (mType == 'statusbar') then
self:SetBackdrop({
bgFile = filePath,
tile = false,
})
end
local fs = self:GetFontString()
if fs then
if mType == 'font' then
fs:SetFont(filePath, 12)
self:SetBackdrop(nil)
else
fs:SetFont(LibSharedMedia:Fetch('font', 'Friz Quadrata TT'), 12)
end
self:GetFontString():SetAllPoints(self)
end
self:Show()
end
end
--[[ panel ]]--
local mediaPanel = Addon:CreateClass('Frame')
do
function mediaPanel:New(name)
local panel = self:Bind(CreateFrame('Frame', name, UIParent, 'TranslucentFrameTemplate'))
-- add close button
panel.close = CreateFrame('Button', panel:GetName() .. 'CloseButton', panel, 'UIPanelCloseButton')
panel.close:SetPoint('TOPRIGHT', -5, -5)
-- add title text
panel.title = panel:CreateFontString(nil, 'OVERLAY', 'GameFontNormal')
panel.title:SetPoint('TOPLEFT', 15, -15)
-- add scroll area
panel:CreateScrollArea()
--add list buttons
panel:CreateButtons()
panel:SetSize(WIDTH+40, 50 + (NUM_ITEMS * HEIGHT))
panel:SetScript('OnHide', function(self)
if self.holding then
self.holding:SetChecked(false)
self.holding:GetScript('OnShow')(self)
self.holding = nil
end
end)
-- make panel close on escape
tinsert(UISpecialFrames, panel:GetName())
return panel
end
function mediaPanel:CreateScrollArea()
local panel = self
local updatePanelList = function() panel:UpdateList() end
local scroll = CreateFrame('ScrollFrame', panel:GetName() .. 'ScrollFrame', panel, 'FauxScrollFrameTemplate')
scroll:SetPoint('TOPLEFT', 8, 0)
scroll:SetPoint('BOTTOMRIGHT', -22, 2)
local scrollBar = _G[scroll:GetName() .. 'ScrollBar']
scrollBar:ClearAllPoints()
scrollBar:SetPoint('TOPLEFT', scroll, 'TOPRIGHT', -6, -50)
scrollBar:SetPoint('BOTTOMLEFT', scroll, 'BOTTOMRIGHT', -6, 26)
scroll:SetScript('OnVerticalScroll', function(scroll, arg1)
FauxScrollFrame_OnVerticalScroll(scroll, arg1, HEIGHT + OFFSET, updatePanelList)
end)
scroll:SetScript('OnShow', function()
panel.buttons[1]:SetWidth(WIDTH)
end)
scroll:SetScript('OnHide', function()
panel.buttons[1]:SetWidth(WIDTH + 20)
end)
scroll:SetScript('OnMouseWheel', function(scroll, direction)
scrollBar:SetValue(scrollBar:GetValue() - direction * (scrollBar:GetHeight()/2))
updatePanelList()
end)
scroll:EnableMouse(false)
scroll:SetScript('OnEnter', function()
scroll:SetScript('OnUpdate', function()
for i, button in pairs(panel.buttons) do
if MouseIsOver(button) and button:IsShown() then
if not button.entered then
button.entered = true
button:GetScript('OnEnter')(button)
end
else
if button.entered then
button.entered = nil
button:GetScript('OnLeave')(button)
end
end
end
end)
end)
scroll:SetScript('OnLeave', function()
scroll:SetScript('OnUpdate', nil)
end)
--Convenient, but the mediaButton's need mouseover interaction.
-- [[
scroll:SetScript('OnMouseUp', function()
local buttons = panel.buttons
for i, button in pairs(panel.buttons) do
if MouseIsOver(button) and button:IsShown() then
if panel.set then
panel.set(button:GetText())
panel:Hide()
end
end
end
end)
--]]
scroll:EnableMouse(true)
panel.scroll = scroll
end
function mediaPanel:CreateButtons()
local buttons = {}
for i = 1, NUM_ITEMS do
local button = mediaButton:New(self)
if i == 1 then
button:SetPoint('TOPLEFT', 12, -37)
else
button:SetPoint('TOPLEFT', buttons[i - 1], 'BOTTOMLEFT', 0, -OFFSET)
end
tinsert(buttons, button)
end
self.buttons = buttons
end
function mediaPanel:UpdateList()
FauxScrollFrame_Update(self.scroll, #self.list, #self.buttons, HEIGHT + OFFSET)
for i,button in pairs(self.buttons) do
local index = i + self.scroll.offset
if index <= #self.list then
local filePath = LibSharedMedia:Fetch(self.mediatype, self.list[index])
button:Set(self.mediatype, function() return self.list[index] end)
button:Show()
else
button:Hide()
end
end
end
end
do
local Media = {}
function Media:Display(anchor, clicked, mediaType, get, set)
local panel = self:GetOrCreatePanel()
local holding = panel.holding
if holding then
holding:SetChecked(false)
end
if clicked and (holding == clicked) then
clicked:SetChecked(false)
panel.holding = nil
return panel:Hide()
end
panel.holding = clicked
clicked:SetChecked(true)
panel.owner = anchor.owner
panel:SetParent(anchor)
panel.title:SetText(mediaType)
panel:ClearAllPoints()
panel:SetPoint('TOPLEFT', anchor:GetParent(), 'TOPRIGHT')
panel.mediatype = string.lower(mediaType)
panel.list = LibSharedMedia:List(panel.mediatype)
panel.get = function() return anchor.owner[get](anchor.owner) end
panel.set = function(...) return anchor.owner[set](anchor.owner, ...) end
panel:UpdateList()
panel:Show()
return panel
end
function Media:GetOrCreatePanel()
if not self.panel then
self.panel = mediaPanel:New(GetAddOnDependencies(AddonName)..'MediaPanel')
end
return self.panel
end
function Media:NewMediaButton(parent, name, mediaType, get, set)
local button = CreateFrame('CheckButton', ('%sMediaButton%s'):format(parent:GetName(), name), parent, 'UIMenuButtonStretchTemplate')
button:SetHeight(20)
button:SetText(name)
button:SetWidth(button:GetTextWidth()+8)
button.get = function() return parent.owner[get](parent.owner) end
button.set = function(...) return parent.owner[set](parent.owner, ...) end
button:SetHitRectInsets(0,-75,0,0)
button.preview = mediaButton:New(button)
button.preview:SetPoint('LEFT', button,'RIGHT')
button.preview:EnableMouse(false)
button:SetScript('OnClick', function()
self:Display(parent, button, mediaType, get, set)
end)
button:SetScript('OnShow', function()
button.preview:Set(string.lower(mediaType), function() return button.get() end)
button.preview:SetWidth(abs(parent:GetWidth()-(button:GetWidth()))-5) --don't like this here, but it glitches otherwise. ~Goranaws
end)
button:SetCheckedTexture(button:GetHighlightTexture():GetTexture())
local prev = parent.checkbutton --I'd like to call it parent.button instead, everywhere. ~Goranaws
if prev then
button:SetPoint('TOPLEFT', prev, 'BOTTOMLEFT', 0, -2)
else
button:SetPoint('TOPLEFT', 2, -2)
end
parent.checkbutton = button
parent.height = parent.height + 27
return button
end
Addon.MediaPanel = Media
end