ce5ff14dd8974f807cb192c12c5b2672ca9cdee6
[wowui.git] / libs / LibSharedMedia-3.0 / LibSharedMedia-3.0.lua
1 --[[
2 Name: LibSharedMedia-3.0
3 Revision: $Revision: 91 $
4 Author: Elkano (elkano@gmx.de)
5 Inspired By: SurfaceLib by Haste/Otravi (troeks@gmail.com)
6 Website: http://www.wowace.com/projects/libsharedmedia-3-0/
7 Description: Shared handling of media data (fonts, sounds, textures, ...) between addons.
8 Dependencies: LibStub, CallbackHandler-1.0
9 License: LGPL v2.1
10 ]]
11
12 local MAJOR, MINOR = "LibSharedMedia-3.0", 6010002 -- 6.1.0 v2 / increase manually on changes
13 local lib = LibStub:NewLibrary(MAJOR, MINOR)
14
15 if not lib then return end
16
17 local _G = getfenv(0)
18
19 local pairs             = _G.pairs
20 local type              = _G.type
21
22 local band                      = _G.bit.band
23
24 local table_insert      = _G.table.insert
25 local table_sort        = _G.table.sort
26
27 local locale = GetLocale()
28 local locale_is_western
29 local LOCALE_MASK = 0
30 lib.LOCALE_BIT_koKR             = 1
31 lib.LOCALE_BIT_ruRU             = 2
32 lib.LOCALE_BIT_zhCN             = 4
33 lib.LOCALE_BIT_zhTW             = 8
34 lib.LOCALE_BIT_western  = 128
35
36 local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0")
37
38 lib.callbacks           = lib.callbacks                 or CallbackHandler:New(lib)
39
40 lib.DefaultMedia        = lib.DefaultMedia              or {}
41 lib.MediaList           = lib.MediaList                 or {}
42 lib.MediaTable          = lib.MediaTable                or {}
43 lib.MediaType           = lib.MediaType                 or {}
44 lib.OverrideMedia       = lib.OverrideMedia             or {}
45
46 local defaultMedia = lib.DefaultMedia
47 local mediaList = lib.MediaList
48 local mediaTable = lib.MediaTable
49 local overrideMedia = lib.OverrideMedia
50
51
52 -- create mediatype constants
53 lib.MediaType.BACKGROUND        = "background"                  -- background textures
54 lib.MediaType.BORDER            = "border"                              -- border textures
55 lib.MediaType.FONT                      = "font"                                -- fonts
56 lib.MediaType.STATUSBAR         = "statusbar"                   -- statusbar textures
57 lib.MediaType.SOUND                     = "sound"                               -- sound files
58
59 -- populate lib with default Blizzard data
60 -- BACKGROUND
61 if not lib.MediaTable.background then lib.MediaTable.background = {} end
62 lib.MediaTable.background["None"]                                                                       = [[]]
63 lib.MediaTable.background["Blizzard Collections Background"]            = [[Interface\Collections\CollectionsBackgroundTile]]
64 lib.MediaTable.background["Blizzard Dialog Background"]                         = [[Interface\DialogFrame\UI-DialogBox-Background]]
65 lib.MediaTable.background["Blizzard Dialog Background Dark"]            = [[Interface\DialogFrame\UI-DialogBox-Background-Dark]]
66 lib.MediaTable.background["Blizzard Dialog Background Gold"]            = [[Interface\DialogFrame\UI-DialogBox-Gold-Background]]
67 lib.MediaTable.background["Blizzard Garrison Background"]                       = [[Interface\Garrison\GarrisonUIBackground]]
68 lib.MediaTable.background["Blizzard Garrison Background 2"]                     = [[Interface\Garrison\GarrisonUIBackground2]]
69 lib.MediaTable.background["Blizzard Garrison Background 3"]                     = [[Interface\Garrison\GarrisonMissionUIInfoBoxBackgroundTile]]
70 lib.MediaTable.background["Blizzard Low Health"]                                        = [[Interface\FullScreenTextures\LowHealth]]
71 lib.MediaTable.background["Blizzard Marble"]                                            = [[Interface\FrameGeneral\UI-Background-Marble]]
72 lib.MediaTable.background["Blizzard Out of Control"]                            = [[Interface\FullScreenTextures\OutOfControl]]
73 lib.MediaTable.background["Blizzard Parchment"]                                         = [[Interface\AchievementFrame\UI-Achievement-Parchment-Horizontal]]
74 lib.MediaTable.background["Blizzard Parchment 2"]                                       = [[Interface\AchievementFrame\UI-GuildAchievement-Parchment-Horizontal]]
75 lib.MediaTable.background["Blizzard Rock"]                                                      = [[Interface\FrameGeneral\UI-Background-Rock]]
76 lib.MediaTable.background["Blizzard Tabard Background"]                         = [[Interface\TabardFrame\TabardFrameBackground]]
77 lib.MediaTable.background["Blizzard Tooltip"]                                           = [[Interface\Tooltips\UI-Tooltip-Background]]
78 lib.MediaTable.background["Solid"]                                                                      = [[Interface\Buttons\WHITE8X8]]
79 lib.DefaultMedia.background = "None"
80
81 -- BORDER
82 if not lib.MediaTable.border then lib.MediaTable.border = {} end
83 lib.MediaTable.border["None"]                                                           = [[]]
84 lib.MediaTable.border["Blizzard Achievement Wood"]                      = [[Interface\AchievementFrame\UI-Achievement-WoodBorder]]
85 lib.MediaTable.border["Blizzard Chat Bubble"]                           = [[Interface\Tooltips\ChatBubble-Backdrop]]
86 lib.MediaTable.border["Blizzard Dialog"]                                        = [[Interface\DialogFrame\UI-DialogBox-Border]]
87 lib.MediaTable.border["Blizzard Dialog Gold"]                           = [[Interface\DialogFrame\UI-DialogBox-Gold-Border]]
88 lib.MediaTable.border["Blizzard Party"]                                         = [[Interface\CHARACTERFRAME\UI-Party-Border]]
89 lib.MediaTable.border["Blizzard Tooltip"]                                       = [[Interface\Tooltips\UI-Tooltip-Border]]
90 lib.DefaultMedia.border = "None"
91
92 -- FONT
93 if not lib.MediaTable.font then lib.MediaTable.font = {} end
94 local SML_MT_font = lib.MediaTable.font
95 --[[
96 All font files are currently in all clients, the following table depicts which font supports which charset as of 5.0.4
97 Fonts were checked using langcover.pl from DejaVu fonts (http://sourceforge.net/projects/dejavu/) and FontForge (http://fontforge.org/)
98 latin means check for: de, en, es, fr, it, pt
99
100 file                            name                                                    latin   koKR    ruRU    zhCN    zhTW
101 2002.ttf                        2002                                                    X               X               X               -               -
102 2002B.ttf                       2002 Bold                                               X               X               X               -               -
103 ARHei.ttf                       AR CrystalzcuheiGBK Demibold    X               -               X               X               X
104 ARIALN.TTF                      Arial Narrow                                    X               -               X               -               -
105 ARKai_C.ttf                     AR ZhongkaiGBK Medium (Combat)  X               -               X               X               X
106 ARKai_T.ttf                     AR ZhongkaiGBK Medium                   X               -               X               X               X
107 bHEI00M.ttf                     AR Heiti2 Medium B5                             -               -               -               -               X
108 bHEI01B.ttf                     AR Heiti2 Bold B5                               -               -               -               -               X
109 bKAI00M.ttf                     AR Kaiti Medium B5                              -               -               -               -               X
110 bLEI00D.ttf                     AR Leisu Demi B5                                -               -               -               -               X
111 FRIZQT__.TTF            Friz Quadrata TT                                X               -               -               -               -
112 FRIZQT___CYR.TTF        FrizQuadrataCTT                                 x               -               X               -               -
113 K_Damage.TTF            YDIWingsM                                               -               X               X               -               -
114 K_Pagetext.TTF          MoK                                                             X               X               X               -               -
115 MORPHEUS.TTF            Morpheus                                                X               -               -               -               -
116 MORPHEUS_CYR.TTF        Morpheus                                                X               -               X               -               -
117 NIM_____.ttf            Nimrod MT                                               X               -               X               -               -
118 SKURRI.TTF                      Skurri                                                  X               -               -               -               -
119 SKURRI_CYR.TTF          Skurri                                                  X               -               X               -               -
120
121 WARNING: Although FRIZQT___CYR is available on western clients, it doesn't support special European characters e.g. é, ï, ö
122 Due to this, we cannot use it as a replacement for FRIZQT__.TTF
123 ]]
124
125 if locale == "koKR" then
126         LOCALE_MASK = lib.LOCALE_BIT_koKR
127 --
128         SML_MT_font["굵은 글꼴"]            = [[Fonts\2002B.TTF]]
129         SML_MT_font["기본 글꼴"]            = [[Fonts\2002.TTF]]
130         SML_MT_font["데미지 글꼴"]         = [[Fonts\K_Damage.TTF]]
131         SML_MT_font["퀘스트 글꼴"]         = [[Fonts\K_Pagetext.TTF]]
132 --
133         lib.DefaultMedia["font"] = "기본 글꼴" -- someone from koKR please adjust if needed
134 --
135 elseif locale == "zhCN" then
136         LOCALE_MASK = lib.LOCALE_BIT_zhCN
137 --
138         SML_MT_font["伤害数字"]             = [[Fonts\ARKai_C.ttf]]
139         SML_MT_font["默认"]                   = [[Fonts\ARKai_T.ttf]]
140         SML_MT_font["聊天"]                   = [[Fonts\ARHei.ttf]]
141 --
142         lib.DefaultMedia["font"] = "默认" -- someone from zhCN please adjust if needed
143 --
144 elseif locale == "zhTW" then
145         LOCALE_MASK = lib.LOCALE_BIT_zhTW
146 --
147         SML_MT_font["提示訊息"]             = [[Fonts\bHEI00M.ttf]]
148         SML_MT_font["聊天"]                   = [[Fonts\bHEI01B.ttf]]
149         SML_MT_font["傷害數字"]             = [[Fonts\bKAI00M.ttf]]
150         SML_MT_font["預設"]                   = [[Fonts\bLEI00D.ttf]]
151 --
152         lib.DefaultMedia["font"] = "預設" -- someone from zhTW please adjust if needed
153
154 elseif locale == "ruRU" then
155         LOCALE_MASK = lib.LOCALE_BIT_ruRU
156 --
157         SML_MT_font["2002"]                                                             = [[Fonts\2002.TTF]]
158         SML_MT_font["2002 Bold"]                                                = [[Fonts\2002B.TTF]]
159         SML_MT_font["AR CrystalzcuheiGBK Demibold"]             = [[Fonts\ARHei.TTF]]
160         SML_MT_font["AR ZhongkaiGBK Medium (Combat)"]   = [[Fonts\ARKai_C.TTF]]
161         SML_MT_font["AR ZhongkaiGBK Medium"]                    = [[Fonts\ARKai_T.TTF]]
162         SML_MT_font["Arial Narrow"]                                             = [[Fonts\ARIALN.TTF]]
163         SML_MT_font["Friz Quadrata TT"]                                 = [[Fonts\FRIZQT___CYR.TTF]]
164         SML_MT_font["MoK"]                                                              = [[Fonts\K_Pagetext.TTF]]
165         SML_MT_font["Morpheus"]                                                 = [[Fonts\MORPHEUS_CYR.TTF]]
166         SML_MT_font["Nimrod MT"]                                                = [[Fonts\NIM_____.ttf]]
167         SML_MT_font["Skurri"]                                                   = [[Fonts\SKURRI_CYR.TTF]]
168 --
169         lib.DefaultMedia.font = "Friz Quadrata TT"
170 --
171 else
172         LOCALE_MASK = lib.LOCALE_BIT_western
173         locale_is_western = true
174 --
175         SML_MT_font["2002"]                                                             = [[Fonts\2002.TTF]]
176         SML_MT_font["2002 Bold"]                                                = [[Fonts\2002B.TTF]]
177         SML_MT_font["AR CrystalzcuheiGBK Demibold"]             = [[Fonts\ARHei.TTF]]
178         SML_MT_font["AR ZhongkaiGBK Medium (Combat)"]   = [[Fonts\ARKai_C.TTF]]
179         SML_MT_font["AR ZhongkaiGBK Medium"]                    = [[Fonts\ARKai_T.TTF]]
180         SML_MT_font["Arial Narrow"]                                             = [[Fonts\ARIALN.TTF]]
181         SML_MT_font["Friz Quadrata TT"]                                 = [[Fonts\FRIZQT__.TTF]]
182         SML_MT_font["MoK"]                                                              = [[Fonts\K_Pagetext.TTF]]
183         SML_MT_font["Morpheus"]                                                 = [[Fonts\MORPHEUS_CYR.TTF]]
184         SML_MT_font["Nimrod MT"]                                                = [[Fonts\NIM_____.ttf]]
185         SML_MT_font["Skurri"]                                                   = [[Fonts\SKURRI_CYR.TTF]]
186 --
187         lib.DefaultMedia.font = "Friz Quadrata TT"
188 --
189 end
190
191 -- STATUSBAR
192 if not lib.MediaTable.statusbar then lib.MediaTable.statusbar = {} end
193 lib.MediaTable.statusbar["Blizzard"]                                            = [[Interface\TargetingFrame\UI-StatusBar]]
194 lib.MediaTable.statusbar["Blizzard Character Skills Bar"]       = [[Interface\PaperDollInfoFrame\UI-Character-Skills-Bar]]
195 lib.MediaTable.statusbar["Blizzard Raid Bar"]                           = [[Interface\RaidFrame\Raid-Bar-Hp-Fill]]
196 lib.DefaultMedia.statusbar = "Blizzard"
197
198 -- SOUND
199 if not lib.MediaTable.sound then lib.MediaTable.sound = {} end
200 lib.MediaTable.sound["None"]                                                            = [[Interface\Quiet.ogg]]       -- Relies on the fact that PlaySound[File] doesn't error on non-existing input.
201 lib.DefaultMedia.sound = "None"
202
203 local function rebuildMediaList(mediatype)
204         local mtable = mediaTable[mediatype]
205         if not mtable then return end
206         if not mediaList[mediatype] then mediaList[mediatype] = {} end
207         local mlist = mediaList[mediatype]
208         -- list can only get larger, so simply overwrite it
209         local i = 0
210         for k in pairs(mtable) do
211                 i = i + 1
212                 mlist[i] = k
213         end
214         table_sort(mlist)
215 end
216
217 function lib:Register(mediatype, key, data, langmask)
218         if type(mediatype) ~= "string" then
219                 error(MAJOR..":Register(mediatype, key, data, langmask) - mediatype must be string, got "..type(mediatype))
220         end
221         if type(key) ~= "string" then
222                 error(MAJOR..":Register(mediatype, key, data, langmask) - key must be string, got "..type(key))
223         end
224         mediatype = mediatype:lower()
225         if mediatype == lib.MediaType.FONT and ((langmask and band(langmask, LOCALE_MASK) == 0) or not (langmask or locale_is_western)) then return false end
226         if mediatype == lib.MediaType.SOUND and type(data) == "string" then
227                 local path = data:lower()
228                 -- Only ogg and mp3 are valid sounds.
229                 if not path:find(".ogg", nil, true) and not path:find(".mp3", nil, true) then
230                         return false
231                 end
232         end
233         if not mediaTable[mediatype] then mediaTable[mediatype] = {} end
234         local mtable = mediaTable[mediatype]
235         if mtable[key] then return false end
236         
237         mtable[key] = data
238         rebuildMediaList(mediatype)
239         self.callbacks:Fire("LibSharedMedia_Registered", mediatype, key)
240         return true
241 end
242
243 function lib:Fetch(mediatype, key, noDefault)
244         local mtt = mediaTable[mediatype]
245         local overridekey = overrideMedia[mediatype]
246         local result = mtt and ((overridekey and mtt[overridekey] or mtt[key]) or (not noDefault and defaultMedia[mediatype] and mtt[defaultMedia[mediatype]])) or nil
247         return result ~= "" and result or nil
248 end
249
250 function lib:IsValid(mediatype, key)
251         return mediaTable[mediatype] and (not key or mediaTable[mediatype][key]) and true or false
252 end
253
254 function lib:HashTable(mediatype)
255         return mediaTable[mediatype]
256 end
257
258 function lib:List(mediatype)
259         if not mediaTable[mediatype] then
260                 return nil
261         end
262         if not mediaList[mediatype] then
263                 rebuildMediaList(mediatype)
264         end
265         return mediaList[mediatype]
266 end
267
268 function lib:GetGlobal(mediatype)
269         return overrideMedia[mediatype]
270 end
271
272 function lib:SetGlobal(mediatype, key)
273         if not mediaTable[mediatype] then
274                 return false
275         end
276         overrideMedia[mediatype] = (key and mediaTable[mediatype][key]) and key or nil
277         self.callbacks:Fire("LibSharedMedia_SetGlobal", mediatype, overrideMedia[mediatype])
278         return true
279 end
280
281 function lib:GetDefault(mediatype)
282         return defaultMedia[mediatype]
283 end
284
285 function lib:SetDefault(mediatype, key)
286         if mediaTable[mediatype] and mediaTable[mediatype][key] and not defaultMedia[mediatype] then
287                 defaultMedia[mediatype] = key
288                 return true
289         else
290                 return false
291         end
292 end