1553757 - Refactor updateIndicators
[wowui.git] / libs / AceGUI-3.0-SharedMediaWidgets / SoundWidget.lua
1 -- Widget is based on the AceGUIWidget-DropDown.lua supplied with AceGUI-3.0
2 -- Widget created by Yssaril
3
4 local AceGUI = LibStub("AceGUI-3.0")
5 local Media = LibStub("LibSharedMedia-3.0")
6
7 local AGSMW = LibStub("AceGUISharedMediaWidgets-1.0")
8
9 do
10         local widgetType = "LSM30_Sound"
11         local widgetVersion = 9
12
13         local contentFrameCache = {}
14         local function ReturnSelf(self)
15                 self:ClearAllPoints()
16                 self:Hide()
17                 self.check:Hide()
18                 table.insert(contentFrameCache, self)
19         end
20
21         local function ContentOnClick(this, button)
22                 local self = this.obj
23                 self:Fire("OnValueChanged", this.text:GetText())
24                 if self.dropdown then
25                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
26                 end
27         end
28
29         local function ContentSpeakerOnClick(this, button)
30                 local self = this.frame.obj
31                 local sound = this.frame.text:GetText()
32                 PlaySoundFile(self.list[sound] ~= sound and self.list[sound] or Media:Fetch('sound',sound))
33         end
34
35         local function GetContentLine()
36                 local frame
37                 if next(contentFrameCache) then
38                         frame = table.remove(contentFrameCache)
39                 else
40                         frame = CreateFrame("Button", nil, UIParent)
41                                 --frame:SetWidth(200)
42                                 frame:SetHeight(18)
43                                 frame:SetHighlightTexture([[Interface\QuestFrame\UI-QuestTitleHighlight]], "ADD")
44                                 frame:SetScript("OnClick", ContentOnClick)
45                         local check = frame:CreateTexture("OVERLAY")
46                                 check:SetWidth(16)
47                                 check:SetHeight(16)
48                                 check:SetPoint("LEFT",frame,"LEFT",1,-1)
49                                 check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
50                                 check:Hide()
51                         frame.check = check
52
53                         local soundbutton = CreateFrame("Button", nil, frame)
54                                 soundbutton:SetWidth(16)
55                                 soundbutton:SetHeight(16)
56                                 soundbutton:SetPoint("RIGHT",frame,"RIGHT",-1,0)
57                                 soundbutton.frame = frame
58                                 soundbutton:SetScript("OnClick", ContentSpeakerOnClick)
59                         frame.soundbutton = soundbutton
60
61                         local speaker = soundbutton:CreateTexture(nil, "BACKGROUND")
62                                 speaker:SetTexture("Interface\\Common\\VoiceChat-Speaker")
63                                 speaker:SetAllPoints(soundbutton)
64                         frame.speaker = speaker
65                         local speakeron = soundbutton:CreateTexture(nil, "HIGHLIGHT")
66                                 speakeron:SetTexture("Interface\\Common\\VoiceChat-On")
67                                 speakeron:SetAllPoints(soundbutton)
68                         frame.speakeron = speakeron
69
70                         local text = frame:CreateFontString(nil,"OVERLAY","GameFontWhite")
71                                 text:SetPoint("LEFT", check, "RIGHT", 1, 0)
72                                 text:SetPoint("RIGHT", soundbutton, "LEFT", -2, 0)
73                                 text:SetJustifyH("LEFT")
74                                 text:SetText("Test Test Test Test Test Test Test")
75                         frame.text = text
76                         frame.ReturnSelf = ReturnSelf
77                 end
78                 frame:Show()
79                 return frame
80         end
81
82         local function OnAcquire(self)
83                 self:SetHeight(44)
84                 self:SetWidth(200)
85         end
86
87         local function OnRelease(self)
88                 self:SetText("")
89                 self:SetLabel("")
90                 self:SetDisabled(false)
91
92                 self.value = nil
93                 self.list = nil
94                 self.open = nil
95                 self.hasClose = nil
96
97                 self.frame:ClearAllPoints()
98                 self.frame:Hide()
99         end
100
101         local function SetValue(self, value) -- Set the value to an item in the List.
102                 if self.list then
103                         self:SetText(value or "")
104                 end
105                 self.value = value
106         end
107
108         local function GetValue(self)
109                 return self.value
110         end
111
112         local function SetList(self, list) -- Set the list of values for the dropdown (key => value pairs)
113                 self.list = list or Media:HashTable("sound")
114         end
115
116         local function SetText(self, text) -- Set the text displayed in the box.
117                 self.frame.text:SetText(text or "")
118         end
119
120         local function SetLabel(self, text) -- Set the text for the label.
121                 self.frame.label:SetText(text or "")
122         end
123
124         local function AddItem(self, key, value) -- Add an item to the list.
125                 self.list = self.list or {}
126                 self.list[key] = value
127         end
128         local SetItemValue = AddItem -- Set the value of a item in the list. <<same as adding a new item>>
129
130         local function SetMultiselect(self, flag) end -- Toggle multi-selecting. <<Dummy function to stay inline with the dropdown API>>
131         local function GetMultiselect() return false end-- Query the multi-select flag. <<Dummy function to stay inline with the dropdown API>>
132         local function SetItemDisabled(self, key) end-- Disable one item in the list. <<Dummy function to stay inline with the dropdown API>>
133
134         local function SetDisabled(self, disabled) -- Disable the widget.
135                 self.disabled = disabled
136                 if disabled then
137                         self.frame:Disable()
138                         self.speaker:SetDesaturated(true)
139                         self.speakeron:SetDesaturated(true)
140                 else
141                         self.frame:Enable()
142                         self.speaker:SetDesaturated(false)
143                         self.speakeron:SetDesaturated(false)
144                 end
145         end
146
147         local function textSort(a,b)
148                 return string.upper(a) < string.upper(b)
149         end
150
151         local sortedlist = {}
152         local function ToggleDrop(this)
153                 local self = this.obj
154                 if self.dropdown then
155                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
156                         AceGUI:ClearFocus()
157                 else
158                         AceGUI:SetFocus(self)
159                         self.dropdown = AGSMW:GetDropDownFrame()
160                         self.dropdown:SetPoint("TOPLEFT", self.frame, "BOTTOMLEFT")
161                         for k, v in pairs(self.list) do
162                                 sortedlist[#sortedlist+1] = k
163                         end
164                         table.sort(sortedlist, textSort)
165                         for i, k in ipairs(sortedlist) do
166                                 local f = GetContentLine()
167                                 f.text:SetText(k)
168                                 if k == self.value then
169                                         f.check:Show()
170                                 end
171                                 f.obj = self
172                                 self.dropdown:AddFrame(f)
173                         end
174                         wipe(sortedlist)
175                 end
176         end
177
178         local function ClearFocus(self)
179                 if self.dropdown then
180                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
181                 end
182         end
183
184         local function OnHide(this)
185                 local self = this.obj
186                 if self.dropdown then
187                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
188                 end
189         end
190
191         local function Drop_OnEnter(this)
192                 this.obj:Fire("OnEnter")
193         end
194
195         local function Drop_OnLeave(this)
196                 this.obj:Fire("OnLeave")
197         end
198
199         local function WidgetPlaySound(this)
200                 local self = this.obj
201                 local sound = self.frame.text:GetText()
202                 PlaySoundFile(self.list[sound] ~= sound and self.list[sound] or Media:Fetch('sound',sound))
203         end
204
205         local function Constructor()
206                 local frame = AGSMW:GetBaseFrame()
207                 local self = {}
208
209                 self.type = widgetType
210                 self.frame = frame
211                 frame.obj = self
212                 frame.dropButton.obj = self
213                 frame.dropButton:SetScript("OnEnter", Drop_OnEnter)
214                 frame.dropButton:SetScript("OnLeave", Drop_OnLeave)
215                 frame.dropButton:SetScript("OnClick",ToggleDrop)
216                 frame:SetScript("OnHide", OnHide)
217
218
219                 local soundbutton = CreateFrame("Button", nil, frame)
220                         soundbutton:SetWidth(16)
221                         soundbutton:SetHeight(16)
222                         soundbutton:SetPoint("LEFT",frame.DLeft,"LEFT",26,1)
223                         soundbutton:SetScript("OnClick", WidgetPlaySound)
224                         soundbutton.obj = self
225                 self.soundbutton = soundbutton
226                 frame.text:SetPoint("LEFT",soundbutton,"RIGHT",2,0)
227
228
229                 local speaker = soundbutton:CreateTexture(nil, "BACKGROUND")
230                         speaker:SetTexture("Interface\\Common\\VoiceChat-Speaker")
231                         speaker:SetAllPoints(soundbutton)
232                 self.speaker = speaker
233                 local speakeron = soundbutton:CreateTexture(nil, "HIGHLIGHT")
234                         speakeron:SetTexture("Interface\\Common\\VoiceChat-On")
235                         speakeron:SetAllPoints(soundbutton)
236                 self.speakeron = speakeron
237
238                 self.alignoffset = 31
239
240                 self.OnRelease = OnRelease
241                 self.OnAcquire = OnAcquire
242                 self.ClearFocus = ClearFocus
243                 self.SetText = SetText
244                 self.SetValue = SetValue
245                 self.GetValue = GetValue
246                 self.SetList = SetList
247                 self.SetLabel = SetLabel
248                 self.SetDisabled = SetDisabled
249                 self.AddItem = AddItem
250                 self.SetMultiselect = SetMultiselect
251                 self.GetMultiselect = GetMultiselect
252                 self.SetItemValue = SetItemValue
253                 self.SetItemDisabled = SetItemDisabled
254                 self.ToggleDrop = ToggleDrop
255
256                 AceGUI:RegisterAsWidget(self)
257                 return self
258         end
259
260         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
261
262 end