44f6c09c04a32124be501bcbb92e11d0f39d8825
[wowui.git] / libs / LibSharedMedia-3.0 / LibSharedMedia-3.0.lua
1 --[[
2 Name: LibSharedMedia-3.0
3 Revision: $Revision: 58 $
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", 90000 + tonumber(("$Revision: 58 $"):match("(%d+)"))
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["Blizzard Dialog Background"]         = [[Interface\DialogFrame\UI-DialogBox-Background]]
63 lib.MediaTable.background["Blizzard Low Health"]                        = [[Interface\FullScreenTextures\LowHealth]]
64 lib.MediaTable.background["Blizzard Out of Control"]            = [[Interface\FullScreenTextures\OutOfControl]]
65 lib.MediaTable.background["Blizzard Parchment"]                         = [[Interface\AchievementFrame\UI-Achievement-Parchment-Horizontal]]
66 lib.MediaTable.background["Blizzard Parchment 2"]                       = [[Interface\AchievementFrame\UI-Achievement-Parchment]]
67 lib.MediaTable.background["Blizzard Tabard Background"]         = [[Interface\TabardFrame\TabardFrameBackground]]
68 lib.MediaTable.background["Blizzard Tooltip"]                           = [[Interface\Tooltips\UI-Tooltip-Background]]
69 lib.MediaTable.background["Solid"]                                                      = [[Interface\Buttons\WHITE8X8]]
70
71 -- BORDER
72 if not lib.MediaTable.border then lib.MediaTable.border = {} end
73 lib.MediaTable.border["None"]                                                           = [[Interface\None]]
74 lib.MediaTable.border["Blizzard Achievement Wood"]                      = [[Interface\AchievementFrame\UI-Achievement-WoodBorder]]
75 lib.MediaTable.border["Blizzard Chat Bubble"]                           = [[Interface\Tooltips\ChatBubble-Backdrop]]
76 lib.MediaTable.border["Blizzard Dialog"]                                        = [[Interface\DialogFrame\UI-DialogBox-Border]]
77 lib.MediaTable.border["Blizzard Dialog Gold"]                           = [[Interface\DialogFrame\UI-DialogBox-Gold-Border]]
78 lib.MediaTable.border["Blizzard Party"]                                         = [[Interface\CHARACTERFRAME\UI-Party-Border]]
79 lib.MediaTable.border["Blizzard Tooltip"]                                       = [[Interface\Tooltips\UI-Tooltip-Border]]
80
81 -- FONT
82 if not lib.MediaTable.font then lib.MediaTable.font = {} end
83 local SML_MT_font = lib.MediaTable.font
84 if locale == "koKR" then
85         LOCALE_MASK = lib.LOCALE_BIT_koKR
86 --
87         SML_MT_font["굵은 글꼴"]            = [[Fonts\2002B.TTF]]
88         SML_MT_font["기본 글꼴"]            = [[Fonts\2002.TTF]]
89         SML_MT_font["데미지 글꼴"]         = [[Fonts\K_Damage.TTF]]
90         SML_MT_font["퀘스트 글꼴"]         = [[Fonts\K_Pagetext.TTF]]
91 --
92         lib.DefaultMedia["font"] = "기본 글꼴" -- someone from koKR please adjust if needed
93 --
94 elseif locale == "zhCN" then
95         LOCALE_MASK = lib.LOCALE_BIT_zhCN
96 --
97         SML_MT_font["伤害数字"]             = [[Fonts\ZYKai_C.ttf]]
98         SML_MT_font["默认"]                   = [[Fonts\ZYKai_T.ttf]]
99         SML_MT_font["聊天"]                   = [[Fonts\ZYHei.ttf]]
100 --
101         lib.DefaultMedia["font"] = "默认" -- someone from zhCN please adjust if needed
102 --
103 elseif locale == "zhTW" then
104         LOCALE_MASK = lib.LOCALE_BIT_zhTW
105 --
106         SML_MT_font["提示訊息"]             = [[Fonts\bHEI00M.ttf]]
107         SML_MT_font["聊天"]                   = [[Fonts\bHEI01B.ttf]]
108         SML_MT_font["傷害數字"]             = [[Fonts\bKAI00M.ttf]]
109         SML_MT_font["預設"]                   = [[Fonts\bLEI00D.ttf]]
110 --
111         lib.DefaultMedia["font"] = "預設" -- someone from zhTW please adjust if needed
112
113 elseif locale == "ruRU" then
114         LOCALE_MASK = lib.LOCALE_BIT_ruRU
115 --
116         SML_MT_font["Arial Narrow"]                     = [[Fonts\ARIALN.TTF]]
117         SML_MT_font["Friz Quadrata TT"]         = [[Fonts\FRIZQT__.TTF]]
118         SML_MT_font["Morpheus"]                         = [[Fonts\MORPHEUS.TTF]]
119         SML_MT_font["Nimrod MT"]                        = [[Fonts\NIM_____.ttf]]
120         SML_MT_font["Skurri"]                           = [[Fonts\SKURRI.TTF]]
121 --
122         lib.DefaultMedia.font = "Friz Quadrata TT"
123 --
124 else
125         LOCALE_MASK = lib.LOCALE_BIT_western
126         locale_is_western = true
127 --
128         SML_MT_font["Arial Narrow"]                     = [[Fonts\ARIALN.TTF]]
129         SML_MT_font["Friz Quadrata TT"]         = [[Fonts\FRIZQT__.TTF]]
130         SML_MT_font["Morpheus"]                         = [[Fonts\MORPHEUS.TTF]]
131         SML_MT_font["Skurri"]                           = [[Fonts\SKURRI.TTF]]
132 --
133         lib.DefaultMedia.font = "Friz Quadrata TT"
134 --
135 end
136
137 -- STATUSBAR
138 if not lib.MediaTable.statusbar then lib.MediaTable.statusbar = {} end
139 lib.MediaTable.statusbar["Blizzard"]                                            = [[Interface\TargetingFrame\UI-StatusBar]]
140 lib.DefaultMedia.statusbar = "Blizzard"
141
142 -- SOUND
143 if not lib.MediaTable.sound then lib.MediaTable.sound = {} end
144 lib.MediaTable.sound["None"]                                                            = [[Interface\Quiet.mp3]]       -- Relies on the fact that PlaySound[File] doesn't error on non-existing input.
145 lib.DefaultMedia.sound = "None"
146
147 local function rebuildMediaList(mediatype)
148         local mtable = mediaTable[mediatype]
149         if not mtable then return end
150         if not mediaList[mediatype] then mediaList[mediatype] = {} end
151         local mlist = mediaList[mediatype]
152         -- list can only get larger, so simply overwrite it
153         local i = 0
154         for k in pairs(mtable) do
155                 i = i + 1
156                 mlist[i] = k
157         end
158         table_sort(mlist)
159 end
160
161 function lib:Register(mediatype, key, data, langmask)
162         if type(mediatype) ~= "string" then
163                 error(MAJOR..":Register(mediatype, key, data, langmask) - mediatype must be string, got "..type(mediatype))
164         end
165         if type(key) ~= "string" then
166                 error(MAJOR..":Register(mediatype, key, data, langmask) - key must be string, got "..type(key))
167         end
168         if mediatype == lib.MediaType.FONT  and ((langmask and band(langmask, LOCALE_MASK) == 0) or not (langmask or locale_is_western)) then return false end
169         mediatype = mediatype:lower()
170         if not mediaTable[mediatype] then mediaTable[mediatype] = {} end
171         local mtable = mediaTable[mediatype]
172         if mtable[key] then return false end
173         
174         mtable[key] = data
175         rebuildMediaList(mediatype)
176         self.callbacks:Fire("LibSharedMedia_Registered", mediatype, key)
177         return true
178 end
179
180 function lib:Fetch(mediatype, key, noDefault)
181         local mtt = mediaTable[mediatype]
182         local overridekey = overrideMedia[mediatype]
183         local result = mtt and ((overridekey and mtt[overridekey] or mtt[key]) or (not noDefault and defaultMedia[mediatype] and mtt[defaultMedia[mediatype]])) or nil
184
185         return result
186 end
187
188 function lib:IsValid(mediatype, key)
189         return mediaTable[mediatype] and (not key or mediaTable[mediatype][key]) and true or false
190 end
191
192 function lib:HashTable(mediatype)
193         return mediaTable[mediatype]
194 end
195
196 function lib:List(mediatype)
197         if not mediaTable[mediatype] then
198                 return nil
199         end
200         if not mediaList[mediatype] then
201                 rebuildMediaList(mediatype)
202         end
203         return mediaList[mediatype]
204 end
205
206 function lib:GetGlobal(mediatype)
207         return overrideMedia[mediatype]
208 end
209
210 function lib:SetGlobal(mediatype, key)
211         if not mediaTable[mediatype] then
212                 return false
213         end
214         overrideMedia[mediatype] = (key and mediaTable[mediatype][key]) and key or nil
215         self.callbacks:Fire("LibSharedMedia_SetGlobal", mediatype, overrideMedia[mediatype])
216         return true
217 end
218
219 function lib:GetDefault(mediatype)
220         return defaultMedia[mediatype]
221 end
222
223 function lib:SetDefault(mediatype, key)
224         if mediaTable[mediatype] and mediaTable[mediatype][key] and not defaultMedia[mediatype] then
225                 defaultMedia[mediatype] = key
226                 return true
227         else
228                 return false
229         end
230 end