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
12 local MAJOR, MINOR = "LibSharedMedia-3.0", 90000 + tonumber(("$Revision: 58 $"):match("(%d+)"))
13 local lib = LibStub:NewLibrary(MAJOR, MINOR)
15 if not lib then return end
19 local pairs = _G.pairs
22 local band = _G.bit.band
24 local table_insert = _G.table.insert
25 local table_sort = _G.table.sort
27 local locale = GetLocale()
28 local locale_is_western
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
36 local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0")
38 lib.callbacks = lib.callbacks or CallbackHandler:New(lib)
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 {}
46 local defaultMedia = lib.DefaultMedia
47 local mediaList = lib.MediaList
48 local mediaTable = lib.MediaTable
49 local overrideMedia = lib.OverrideMedia
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
59 -- populate lib with default Blizzard data
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]]
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]]
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
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]]
92 lib.DefaultMedia["font"] = "기본 글꼴" -- someone from koKR please adjust if needed
94 elseif locale == "zhCN" then
95 LOCALE_MASK = lib.LOCALE_BIT_zhCN
97 SML_MT_font["伤害数字"] = [[Fonts\ZYKai_C.ttf]]
98 SML_MT_font["默认"] = [[Fonts\ZYKai_T.ttf]]
99 SML_MT_font["聊天"] = [[Fonts\ZYHei.ttf]]
101 lib.DefaultMedia["font"] = "默认" -- someone from zhCN please adjust if needed
103 elseif locale == "zhTW" then
104 LOCALE_MASK = lib.LOCALE_BIT_zhTW
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]]
111 lib.DefaultMedia["font"] = "預設" -- someone from zhTW please adjust if needed
113 elseif locale == "ruRU" then
114 LOCALE_MASK = lib.LOCALE_BIT_ruRU
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]]
122 lib.DefaultMedia.font = "Friz Quadrata TT"
125 LOCALE_MASK = lib.LOCALE_BIT_western
126 locale_is_western = true
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]]
133 lib.DefaultMedia.font = "Friz Quadrata TT"
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"
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"
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
154 for k in pairs(mtable) do
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))
165 if type(key) ~= "string" then
166 error(MAJOR..":Register(mediatype, key, data, langmask) - key must be string, got "..type(key))
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
175 rebuildMediaList(mediatype)
176 self.callbacks:Fire("LibSharedMedia_Registered", mediatype, key)
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
188 function lib:IsValid(mediatype, key)
189 return mediaTable[mediatype] and (not key or mediaTable[mediatype][key]) and true or false
192 function lib:HashTable(mediatype)
193 return mediaTable[mediatype]
196 function lib:List(mediatype)
197 if not mediaTable[mediatype] then
200 if not mediaList[mediatype] then
201 rebuildMediaList(mediatype)
203 return mediaList[mediatype]
206 function lib:GetGlobal(mediatype)
207 return overrideMedia[mediatype]
210 function lib:SetGlobal(mediatype, key)
211 if not mediaTable[mediatype] then
214 overrideMedia[mediatype] = (key and mediaTable[mediatype][key]) and key or nil
215 self.callbacks:Fire("LibSharedMedia_SetGlobal", mediatype, overrideMedia[mediatype])
219 function lib:GetDefault(mediatype)
220 return defaultMedia[mediatype]
223 function lib:SetDefault(mediatype, key)
224 if mediaTable[mediatype] and mediaTable[mediatype][key] and not defaultMedia[mediatype] then
225 defaultMedia[mediatype] = key