c2d3d84c05fae91d10d1d83d08ae56244529fc00
[wowui.git] / libs / AceGUI-3.0-SharedMediaWidgets / 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 = 10
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), "Master")
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                         local width = self.frame:GetWidth()
161                         self.dropdown:SetPoint("TOPLEFT", self.frame, "BOTTOMLEFT")
162                         self.dropdown:SetPoint("TOPRIGHT", self.frame, "BOTTOMRIGHT", width < 160 and (160 - width) or 0, 0)
163                         for k, v in pairs(self.list) do
164                                 sortedlist[#sortedlist+1] = k
165                         end
166                         table.sort(sortedlist, textSort)
167                         for i, k in ipairs(sortedlist) do
168                                 local f = GetContentLine()
169                                 f.text:SetText(k)
170                                 if k == self.value then
171                                         f.check:Show()
172                                 end
173                                 f.obj = self
174                                 self.dropdown:AddFrame(f)
175                         end
176                         wipe(sortedlist)
177                 end
178         end
179
180         local function ClearFocus(self)
181                 if self.dropdown then
182                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
183                 end
184         end
185
186         local function OnHide(this)
187                 local self = this.obj
188                 if self.dropdown then
189                         self.dropdown = AGSMW:ReturnDropDownFrame(self.dropdown)
190                 end
191         end
192
193         local function Drop_OnEnter(this)
194                 this.obj:Fire("OnEnter")
195         end
196
197         local function Drop_OnLeave(this)
198                 this.obj:Fire("OnLeave")
199         end
200
201         local function WidgetPlaySound(this)
202                 local self = this.obj
203                 local sound = self.frame.text:GetText()
204                 PlaySoundFile(self.list[sound] ~= sound and self.list[sound] or Media:Fetch('sound',sound), "Master")
205         end
206
207         local function Constructor()
208                 local frame = AGSMW:GetBaseFrame()
209                 local self = {}
210
211                 self.type = widgetType
212                 self.frame = frame
213                 frame.obj = self
214                 frame.dropButton.obj = self
215                 frame.dropButton:SetScript("OnEnter", Drop_OnEnter)
216                 frame.dropButton:SetScript("OnLeave", Drop_OnLeave)
217                 frame.dropButton:SetScript("OnClick",ToggleDrop)
218                 frame:SetScript("OnHide", OnHide)
219
220
221                 local soundbutton = CreateFrame("Button", nil, frame)
222                         soundbutton:SetWidth(16)
223                         soundbutton:SetHeight(16)
224                         soundbutton:SetPoint("LEFT",frame.DLeft,"LEFT",26,1)
225                         soundbutton:SetScript("OnClick", WidgetPlaySound)
226                         soundbutton.obj = self
227                 self.soundbutton = soundbutton
228                 frame.text:SetPoint("LEFT",soundbutton,"RIGHT",2,0)
229
230
231                 local speaker = soundbutton:CreateTexture(nil, "BACKGROUND")
232                         speaker:SetTexture("Interface\\Common\\VoiceChat-Speaker")
233                         speaker:SetAllPoints(soundbutton)
234                 self.speaker = speaker
235                 local speakeron = soundbutton:CreateTexture(nil, "HIGHLIGHT")
236                         speakeron:SetTexture("Interface\\Common\\VoiceChat-On")
237                         speakeron:SetAllPoints(soundbutton)
238                 self.speakeron = speakeron
239
240                 self.alignoffset = 31
241
242                 self.OnRelease = OnRelease
243                 self.OnAcquire = OnAcquire
244                 self.ClearFocus = ClearFocus
245                 self.SetText = SetText
246                 self.SetValue = SetValue
247                 self.GetValue = GetValue
248                 self.SetList = SetList
249                 self.SetLabel = SetLabel
250                 self.SetDisabled = SetDisabled
251                 self.AddItem = AddItem
252                 self.SetMultiselect = SetMultiselect
253                 self.GetMultiselect = GetMultiselect
254                 self.SetItemValue = SetItemValue
255                 self.SetItemDisabled = SetItemDisabled
256                 self.ToggleDrop = ToggleDrop
257
258                 AceGUI:RegisterAsWidget(self)
259                 return self
260         end
261
262         AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion)
263
264 end