annotate Libs/LibSharedMedia-3.0/LibSharedMedia-3.0.lua @ 23:52973d00a183

- framework update.
author Tercio
date Tue, 08 Sep 2015 13:23:31 -0300
parents
children 0682d738499b
rev   line source
Tercio@23 1 --[[
Tercio@23 2 Name: LibSharedMedia-3.0
Tercio@23 3 Revision: $Revision: 69 $
Tercio@23 4 Author: Elkano (elkano@gmx.de)
Tercio@23 5 Inspired By: SurfaceLib by Haste/Otravi (troeks@gmail.com)
Tercio@23 6 Website: http://www.wowace.com/projects/libsharedmedia-3-0/
Tercio@23 7 Description: Shared handling of media data (fonts, sounds, textures, ...) between addons.
Tercio@23 8 Dependencies: LibStub, CallbackHandler-1.0
Tercio@23 9 License: LGPL v2.1
Tercio@23 10 ]]
Tercio@23 11
Tercio@23 12 local MAJOR, MINOR = "LibSharedMedia-3.0", 4030402 -- increase manualy on changes
Tercio@23 13 local lib = LibStub:NewLibrary(MAJOR, MINOR)
Tercio@23 14
Tercio@23 15 if not lib then return end
Tercio@23 16
Tercio@23 17 local _G = getfenv(0)
Tercio@23 18
Tercio@23 19 local pairs = _G.pairs
Tercio@23 20 local type = _G.type
Tercio@23 21
Tercio@23 22 local band = _G.bit.band
Tercio@23 23
Tercio@23 24 local table_insert = _G.table.insert
Tercio@23 25 local table_sort = _G.table.sort
Tercio@23 26
Tercio@23 27 local locale = GetLocale()
Tercio@23 28 local locale_is_western
Tercio@23 29 local LOCALE_MASK = 0
Tercio@23 30 lib.LOCALE_BIT_koKR = 1
Tercio@23 31 lib.LOCALE_BIT_ruRU = 2
Tercio@23 32 lib.LOCALE_BIT_zhCN = 4
Tercio@23 33 lib.LOCALE_BIT_zhTW = 8
Tercio@23 34 lib.LOCALE_BIT_western = 128
Tercio@23 35
Tercio@23 36 local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0")
Tercio@23 37
Tercio@23 38 lib.callbacks = lib.callbacks or CallbackHandler:New(lib)
Tercio@23 39
Tercio@23 40 lib.DefaultMedia = lib.DefaultMedia or {}
Tercio@23 41 lib.MediaList = lib.MediaList or {}
Tercio@23 42 lib.MediaTable = lib.MediaTable or {}
Tercio@23 43 lib.MediaType = lib.MediaType or {}
Tercio@23 44 lib.OverrideMedia = lib.OverrideMedia or {}
Tercio@23 45
Tercio@23 46 local defaultMedia = lib.DefaultMedia
Tercio@23 47 local mediaList = lib.MediaList
Tercio@23 48 local mediaTable = lib.MediaTable
Tercio@23 49 local overrideMedia = lib.OverrideMedia
Tercio@23 50
Tercio@23 51
Tercio@23 52 -- create mediatype constants
Tercio@23 53 lib.MediaType.BACKGROUND = "background" -- background textures
Tercio@23 54 lib.MediaType.BORDER = "border" -- border textures
Tercio@23 55 lib.MediaType.FONT = "font" -- fonts
Tercio@23 56 lib.MediaType.STATUSBAR = "statusbar" -- statusbar textures
Tercio@23 57 lib.MediaType.SOUND = "sound" -- sound files
Tercio@23 58
Tercio@23 59 -- populate lib with default Blizzard data
Tercio@23 60 -- BACKGROUND
Tercio@23 61 if not lib.MediaTable.background then lib.MediaTable.background = {} end
Tercio@23 62 lib.MediaTable.background["None"] = [[]]
Tercio@23 63 lib.MediaTable.background["Blizzard Dialog Background"] = [[Interface\DialogFrame\UI-DialogBox-Background]]
Tercio@23 64 lib.MediaTable.background["Blizzard Dialog Background Dark"] = [[Interface\DialogFrame\UI-DialogBox-Background-Dark]]
Tercio@23 65 lib.MediaTable.background["Blizzard Dialog Background Gold"] = [[Interface\DialogFrame\UI-DialogBox-Gold-Background]]
Tercio@23 66 lib.MediaTable.background["Blizzard Low Health"] = [[Interface\FullScreenTextures\LowHealth]]
Tercio@23 67 lib.MediaTable.background["Blizzard Marble"] = [[Interface\FrameGeneral\UI-Background-Marble]]
Tercio@23 68 lib.MediaTable.background["Blizzard Out of Control"] = [[Interface\FullScreenTextures\OutOfControl]]
Tercio@23 69 lib.MediaTable.background["Blizzard Parchment"] = [[Interface\AchievementFrame\UI-Achievement-Parchment-Horizontal]]
Tercio@23 70 lib.MediaTable.background["Blizzard Parchment 2"] = [[Interface\AchievementFrame\UI-GuildAchievement-Parchment-Horizontal]]
Tercio@23 71 lib.MediaTable.background["Blizzard Rock"] = [[Interface\FrameGeneral\UI-Background-Rock]]
Tercio@23 72 lib.MediaTable.background["Blizzard Tabard Background"] = [[Interface\TabardFrame\TabardFrameBackground]]
Tercio@23 73 lib.MediaTable.background["Blizzard Tooltip"] = [[Interface\Tooltips\UI-Tooltip-Background]]
Tercio@23 74 lib.MediaTable.background["Solid"] = [[Interface\Buttons\WHITE8X8]]
Tercio@23 75 lib.DefaultMedia.background = "None"
Tercio@23 76
Tercio@23 77 -- BORDER
Tercio@23 78 if not lib.MediaTable.border then lib.MediaTable.border = {} end
Tercio@23 79 lib.MediaTable.border["None"] = [[]]
Tercio@23 80 lib.MediaTable.border["Blizzard Achievement Wood"] = [[Interface\AchievementFrame\UI-Achievement-WoodBorder]]
Tercio@23 81 lib.MediaTable.border["Blizzard Chat Bubble"] = [[Interface\Tooltips\ChatBubble-Backdrop]]
Tercio@23 82 lib.MediaTable.border["Blizzard Dialog"] = [[Interface\DialogFrame\UI-DialogBox-Border]]
Tercio@23 83 lib.MediaTable.border["Blizzard Dialog Gold"] = [[Interface\DialogFrame\UI-DialogBox-Gold-Border]]
Tercio@23 84 lib.MediaTable.border["Blizzard Party"] = [[Interface\CHARACTERFRAME\UI-Party-Border]]
Tercio@23 85 lib.MediaTable.border["Blizzard Tooltip"] = [[Interface\Tooltips\UI-Tooltip-Border]]
Tercio@23 86 lib.DefaultMedia.border = "None"
Tercio@23 87
Tercio@23 88 -- FONT
Tercio@23 89 if not lib.MediaTable.font then lib.MediaTable.font = {} end
Tercio@23 90 local SML_MT_font = lib.MediaTable.font
Tercio@23 91 if locale == "koKR" then
Tercio@23 92 LOCALE_MASK = lib.LOCALE_BIT_koKR
Tercio@23 93 --
Tercio@23 94 SML_MT_font["굵은 글꼴"] = [[Fonts\2002B.TTF]]
Tercio@23 95 SML_MT_font["기본 글꼴"] = [[Fonts\2002.TTF]]
Tercio@23 96 SML_MT_font["데미지 글꼴"] = [[Fonts\K_Damage.TTF]]
Tercio@23 97 SML_MT_font["퀘스트 글꼴"] = [[Fonts\K_Pagetext.TTF]]
Tercio@23 98 --
Tercio@23 99 lib.DefaultMedia["font"] = "기본 글꼴" -- someone from koKR please adjust if needed
Tercio@23 100 --
Tercio@23 101 elseif locale == "zhCN" then
Tercio@23 102 LOCALE_MASK = lib.LOCALE_BIT_zhCN
Tercio@23 103 --
Tercio@23 104 SML_MT_font["伤害数字"] = [[Fonts\ARKai_C.ttf]]
Tercio@23 105 SML_MT_font["默认"] = [[Fonts\ARKai_T.ttf]]
Tercio@23 106 SML_MT_font["聊天"] = [[Fonts\ARHei.ttf]]
Tercio@23 107 --
Tercio@23 108 lib.DefaultMedia["font"] = "默认" -- someone from zhCN please adjust if needed
Tercio@23 109 --
Tercio@23 110 elseif locale == "zhTW" then
Tercio@23 111 LOCALE_MASK = lib.LOCALE_BIT_zhTW
Tercio@23 112 --
Tercio@23 113 SML_MT_font["提示訊息"] = [[Fonts\bHEI00M.ttf]]
Tercio@23 114 SML_MT_font["聊天"] = [[Fonts\bHEI01B.ttf]]
Tercio@23 115 SML_MT_font["傷害數字"] = [[Fonts\bKAI00M.ttf]]
Tercio@23 116 SML_MT_font["預設"] = [[Fonts\bLEI00D.ttf]]
Tercio@23 117 --
Tercio@23 118 lib.DefaultMedia["font"] = "預設" -- someone from zhTW please adjust if needed
Tercio@23 119
Tercio@23 120 elseif locale == "ruRU" then
Tercio@23 121 LOCALE_MASK = lib.LOCALE_BIT_ruRU
Tercio@23 122 --
Tercio@23 123 SML_MT_font["Arial Narrow"] = [[Fonts\ARIALN.TTF]]
Tercio@23 124 SML_MT_font["Friz Quadrata TT"] = [[Fonts\FRIZQT__.TTF]]
Tercio@23 125 SML_MT_font["Morpheus"] = [[Fonts\MORPHEUS.TTF]]
Tercio@23 126 SML_MT_font["Nimrod MT"] = [[Fonts\NIM_____.ttf]]
Tercio@23 127 SML_MT_font["Skurri"] = [[Fonts\SKURRI.TTF]]
Tercio@23 128 --
Tercio@23 129 lib.DefaultMedia.font = "Friz Quadrata TT"
Tercio@23 130 --
Tercio@23 131 else
Tercio@23 132 LOCALE_MASK = lib.LOCALE_BIT_western
Tercio@23 133 locale_is_western = true
Tercio@23 134 --
Tercio@23 135 SML_MT_font["Arial Narrow"] = [[Fonts\ARIALN.TTF]]
Tercio@23 136 SML_MT_font["Friz Quadrata TT"] = [[Fonts\FRIZQT__.TTF]]
Tercio@23 137 SML_MT_font["Morpheus"] = [[Fonts\MORPHEUS.TTF]]
Tercio@23 138 SML_MT_font["Skurri"] = [[Fonts\SKURRI.TTF]]
Tercio@23 139 --
Tercio@23 140 lib.DefaultMedia.font = "Friz Quadrata TT"
Tercio@23 141 --
Tercio@23 142 end
Tercio@23 143
Tercio@23 144 -- STATUSBAR
Tercio@23 145 if not lib.MediaTable.statusbar then lib.MediaTable.statusbar = {} end
Tercio@23 146 lib.MediaTable.statusbar["Blizzard"] = [[Interface\TargetingFrame\UI-StatusBar]]
Tercio@23 147 lib.MediaTable.statusbar["Blizzard Character Skills Bar"] = [[Interface\PaperDollInfoFrame\UI-Character-Skills-Bar]]
Tercio@23 148 lib.DefaultMedia.statusbar = "Blizzard"
Tercio@23 149
Tercio@23 150 -- SOUND
Tercio@23 151 if not lib.MediaTable.sound then lib.MediaTable.sound = {} end
Tercio@23 152 lib.MediaTable.sound["None"] = [[Interface\Quiet.ogg]] -- Relies on the fact that PlaySound[File] doesn't error on non-existing input.
Tercio@23 153 lib.DefaultMedia.sound = "None"
Tercio@23 154
Tercio@23 155 local function rebuildMediaList(mediatype)
Tercio@23 156 local mtable = mediaTable[mediatype]
Tercio@23 157 if not mtable then return end
Tercio@23 158 if not mediaList[mediatype] then mediaList[mediatype] = {} end
Tercio@23 159 local mlist = mediaList[mediatype]
Tercio@23 160 -- list can only get larger, so simply overwrite it
Tercio@23 161 local i = 0
Tercio@23 162 for k in pairs(mtable) do
Tercio@23 163 i = i + 1
Tercio@23 164 mlist[i] = k
Tercio@23 165 end
Tercio@23 166 table_sort(mlist)
Tercio@23 167 end
Tercio@23 168
Tercio@23 169 function lib:Register(mediatype, key, data, langmask)
Tercio@23 170 if type(mediatype) ~= "string" then
Tercio@23 171 error(MAJOR..":Register(mediatype, key, data, langmask) - mediatype must be string, got "..type(mediatype))
Tercio@23 172 end
Tercio@23 173 if type(key) ~= "string" then
Tercio@23 174 error(MAJOR..":Register(mediatype, key, data, langmask) - key must be string, got "..type(key))
Tercio@23 175 end
Tercio@23 176 mediatype = mediatype:lower()
Tercio@23 177 if mediatype == lib.MediaType.FONT and ((langmask and band(langmask, LOCALE_MASK) == 0) or not (langmask or locale_is_western)) then return false end
Tercio@23 178 if not mediaTable[mediatype] then mediaTable[mediatype] = {} end
Tercio@23 179 local mtable = mediaTable[mediatype]
Tercio@23 180 if mtable[key] then return false end
Tercio@23 181
Tercio@23 182 mtable[key] = data
Tercio@23 183 rebuildMediaList(mediatype)
Tercio@23 184 self.callbacks:Fire("LibSharedMedia_Registered", mediatype, key)
Tercio@23 185 return true
Tercio@23 186 end
Tercio@23 187
Tercio@23 188 function lib:Fetch(mediatype, key, noDefault)
Tercio@23 189 local mtt = mediaTable[mediatype]
Tercio@23 190 local overridekey = overrideMedia[mediatype]
Tercio@23 191 local result = mtt and ((overridekey and mtt[overridekey] or mtt[key]) or (not noDefault and defaultMedia[mediatype] and mtt[defaultMedia[mediatype]])) or nil
Tercio@23 192 return result ~= "" and result or nil
Tercio@23 193 end
Tercio@23 194
Tercio@23 195 function lib:IsValid(mediatype, key)
Tercio@23 196 return mediaTable[mediatype] and (not key or mediaTable[mediatype][key]) and true or false
Tercio@23 197 end
Tercio@23 198
Tercio@23 199 function lib:HashTable(mediatype)
Tercio@23 200 return mediaTable[mediatype]
Tercio@23 201 end
Tercio@23 202
Tercio@23 203 function lib:List(mediatype)
Tercio@23 204 if not mediaTable[mediatype] then
Tercio@23 205 return nil
Tercio@23 206 end
Tercio@23 207 if not mediaList[mediatype] then
Tercio@23 208 rebuildMediaList(mediatype)
Tercio@23 209 end
Tercio@23 210 return mediaList[mediatype]
Tercio@23 211 end
Tercio@23 212
Tercio@23 213 function lib:GetGlobal(mediatype)
Tercio@23 214 return overrideMedia[mediatype]
Tercio@23 215 end
Tercio@23 216
Tercio@23 217 function lib:SetGlobal(mediatype, key)
Tercio@23 218 if not mediaTable[mediatype] then
Tercio@23 219 return false
Tercio@23 220 end
Tercio@23 221 overrideMedia[mediatype] = (key and mediaTable[mediatype][key]) and key or nil
Tercio@23 222 self.callbacks:Fire("LibSharedMedia_SetGlobal", mediatype, overrideMedia[mediatype])
Tercio@23 223 return true
Tercio@23 224 end
Tercio@23 225
Tercio@23 226 function lib:GetDefault(mediatype)
Tercio@23 227 return defaultMedia[mediatype]
Tercio@23 228 end
Tercio@23 229
Tercio@23 230 function lib:SetDefault(mediatype, key)
Tercio@23 231 if mediaTable[mediatype] and mediaTable[mediatype][key] and not defaultMedia[mediatype] then
Tercio@23 232 defaultMedia[mediatype] = key
Tercio@23 233 return true
Tercio@23 234 else
Tercio@23 235 return false
Tercio@23 236 end
Tercio@23 237 end