View Single Post
03-15-14, 07:45 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
A simple solution would be to move the contents of your LSMLaunch.lua file into a callback function that gets called when your addon's active profile changes (or is loaded at login). Add this in your main file:

Lua Code:
  1. function BasicUI:OnInitialize()
  2.     self.db = LibStub("AceDB-3.0"):New("BasicDB", defaults, true)
  3.  
  4.     self.db.RegisterCallback(self, "OnProfileChanged", "OnProfileChanged")
  5.     self.db.RegisterCallback(self, "OnProfileCopied", "OnProfileChanged")
  6.     self.db.RegisterCallback(self, "OnProfileReset", "OnProfileChanged")
  7. end

And then modify your LSMLaunch.lua file like so:

Lua Code:
  1. local BasicUI = LibStub("AceAddon-3.0"):GetAddon("BasicUI")
  2. local LSM = LibStub("LibSharedMedia-3.0")
  3. local cfg
  4.  
  5. function BasicUI:OnProfileChanged()
  6.     cfg = BasicUI.db.profile
  7.  
  8.     -- Load All SharedMedia
  9.  
  10.     -- Font Media
  11.     cfg.media.fontNormal = LSM:Fetch("font", cfg.media.fontNormal)
  12.     cfg.media.fontBold = LSM:Fetch("font", cfg.media.fontBold)
  13.     cfg.media.fontItalic = LSM:Fetch("font", cfg.media.fontItalic)
  14.     cfg.media.fontBoldItalic = LSM:Fetch("font", cfg.media.fontBoldItalic)
  15.     cfg.media.fontNumber = LSM:Fetch("font", cfg.media.fontNumber)
  16.  
  17.     -- Minimap Media
  18.     cfg.minimap.border = LSM:Fetch("border", cfg.minimap.border)
  19.  
  20.     -- Skins Media
  21.     cfg.skin.border = LSM:Fetch("border", cfg.skin.border)
  22.     cfg.skin.statusbar = LSM:Fetch("statusbar", cfg.skin.statusbar)
  23.  
  24.     -- Buff Media
  25.     cfg.buff.border = LSM:Fetch("border", cfg.buff.border)
  26.  
  27.     -- Castbar Media
  28.     cfg.castbar.border = LSM:Fetch("border", cfg.castbar.border)
  29.     cfg.castbar.background = LSM:Fetch("background", cfg.castbar.background)
  30.     cfg.castbar.statusbar = LSM:Fetch("statusbar", cfg.castbar.statusbar)
  31.  
  32.     -- Chat Media
  33.     cfg.chat.border = LSM:Fetch("border", cfg.chat.border)
  34.     cfg.chat.background = LSM:Fetch("background", cfg.chat.background)
  35.     cfg.chat.editboxborder = LSM:Fetch("border", cfg.chat.editboxborder)
  36.     cfg.chat.editboxbackground = LSM:Fetch("background", cfg.chat.editboxbackground)
  37.     cfg.chat.sound = LSM:Fetch("sound", cfg.chat.sound)
  38.  
  39.     -- Datatext Media
  40.     cfg.datatext.border = LSM:Fetch("border", cfg.datatext.border)
  41.     cfg.datatext.background = LSM:Fetch("background", cfg.datatext.background)
  42.  
  43.     -- Nameplates Media
  44.     cfg.nameplates.border = LSM:Fetch("border", cfg.nameplates.border)
  45.     cfg.nameplates.background = LSM:Fetch("background", cfg.nameplates.background)
  46.     cfg.nameplates.statusbar = LSM:Fetch("statusbar", cfg.nameplates.statusbar)
  47.  
  48.     -- Powerbar Media
  49.     cfg.powerbar.background = LSM:Fetch("background", cfg.powerbar.background)
  50.     cfg.powerbar.statusbar = LSM:Fetch("statusbar", cfg.powerbar.statusbar)
  51.  
  52.     -- Tooltip Media
  53.     cfg.tooltip.border = LSM:Fetch("border", cfg.tooltip.border)
  54.     cfg.tooltip.background = LSM:Fetch("background", cfg.tooltip.background)
  55.     cfg.tooltip.statusbar = LSM:Fetch("statusbar", cfg.tooltip.statusbar)
  56. end)

However, there is a major flaw in your reasoning here. Presumably you are trying to convert names like "Arial Narrow" into file paths like "Fonts\\ARIALN__.ttf" for use in your addon, but with the code you are using, you are actually modifying what is saved in your addon's DB, so after the first time this code runs, "Arial Narrow" is no longer the value in the DB -- "Fonts\\ARIALN__.ttf" is -- and since LSM:Fetch requires a font name, not a file path, you will end up changing all of your fonts to whatever LSM's default font for your locale is, eg. "Fonts\\FRIZQT__ttf.".

What you should actually be doing, if you want to use LibSharedMedia, is storing the font name -- eg. "Arial Narrow" -- in your DB, and then look up the actual file path only when you're actually applying the font to a fontstring, eg:

Lua Code:
  1. local fontPath = LSM:Fetch("font", cfg.media.fontNormal)
  2. MY_FONT_STRING:SetFont(fontPath, 18, "OUTLINE")

Obviously, if you're setting the font on 20 fontstrings in the same chunk of code, you should only look up the font path once, and reuse the variable.

Finally, based on the contents of your defaults table you appear to be embedding part or all of Postal in your addon, which is a copyright no-no since that addon is not licensed under terms which allow you to redistribute it. Either get permission from Xinhuan to embed his addon, or write your own mailbox modules, or just recommend Postal as a companion to your UI.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 03-15-14 at 08:02 PM.
  Reply With Quote