View Single Post
02-28-12, 02:27 PM   #19
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Basic template for creating and calling a panel

TOC
lua Code:
  1. ## Interface: 40300
  2. ## Title: rConfig
  3. ## Author: zork
  4. ## Notes: A config template
  5. ## Version: 0.001
  6. ## SavedVariables: RCONFIG_DB_GLOB
  7. ## SavedVariablesPerCharacter: RCONFIG_DB_CHAR
  8. core.lua

LUA
lua Code:
  1. --VARIABLES/CONSTANTS
  2.  
  3. --init the addon frame
  4. local addon = CreateFrame("Frame")
  5. local db_glob, db_char
  6. local _G = _G
  7.  
  8. --FUNCTIONS
  9.  
  10. --default values
  11. local function loadDefaultsGlobal()
  12.   local t = {
  13.     name = 'green',
  14.   }
  15.   return t
  16. end
  17. local function loadDefaultsChar()
  18.   local t = {
  19.     width = 100,
  20.   }
  21.   return t
  22. end
  23.  
  24. local function loadDatabase()
  25.   db_glob = _G["RCONFIG_DB_GLOB"] or loadDefaultsGlobal()
  26.   _G["RCONFIG_DB_GLOB"] = db_glob
  27.   --saved variables per character
  28.   db_char = _G["RCONFIG_DB_CHAR"] or loadDefaultsChar()
  29.   _G["RCONFIG_DB_CHAR"] = db_char
  30. end
  31.  
  32. local function resetAllValues()
  33.   db_glob = loadDefaultsGlobal()
  34.   _G["RCONFIG_DB_GLOB"] = db_glob
  35.   db_char = loadDefaultsChar()
  36.   _G["RCONFIG_DB_CHAR"] = db_char
  37. end
  38.  
  39. --do you really want to reset all values?
  40. StaticPopupDialogs["RESET"] = {
  41.   text = "Reset all values for rConfig?",
  42.   button1 = ACCEPT,
  43.   button2 = CANCEL,
  44.   OnAccept = function() resetAllValues() print('reset done') end,
  45.   OnCancel = function() print('reset canceled') end,
  46.   timeout = 0,
  47.   whileDead = 1,
  48. }
  49.  
  50. local function createSubPanel(parent, name)
  51.   local panel = CreateFrame("FRAME")
  52.   panel.name = name
  53.   panel.parent = parent.name
  54.   panel.okay = function (self)
  55.     print(name..' okay')
  56.   end
  57.   panel.cancel = function (self)
  58.     print(name..' cancel')
  59.   end
  60.   panel.default = function (self)
  61.     --resetAllValues()
  62.     StaticPopup_Show("RESET")
  63.     print(name..' default')
  64.   end
  65.   panel.refresh = function (self)
  66.     print(name..' refresh')
  67.   end
  68.   InterfaceOptions_AddCategory(panel)
  69. end
  70.  
  71. local function initPanel()
  72.   local mainpanel = CreateFrame("FRAME")
  73.   mainpanel.name = "rConfig"
  74.   InterfaceOptions_AddCategory(mainpanel)
  75.   createSubPanel(mainpanel,"rConfigSubPanel1")
  76.   createSubPanel(mainpanel,"rConfigSubPanel2")
  77.   return mainpanel
  78. end
  79.  
  80. local function initSlashCmd(panel)
  81.   SLASH_RCONFIG1, SLASH_RCONFIG2 = '/rc', '/rconfig'
  82.   function SlashCmdList.RCONFIG(msg, editbox)
  83.     InterfaceOptionsFrame_OpenToCategory(panel)
  84.   end
  85. end
  86.  
  87. --INIT
  88.  
  89. function addon:VARIABLES_LOADED(...)
  90.   local self = self
  91.   --load the database
  92.   loadDatabase()
  93.   self:UnregisterEvent("VARIABLES_LOADED")
  94. end
  95.  
  96. function addon:PLAYER_LOGIN(...)
  97.   local self = self
  98.   print(db_glob.name)
  99.   local mainpanel = initPanel()
  100.   initSlashCmd(mainpanel)
  101.   self:UnregisterEvent("PLAYER_LOGIN")
  102. end
  103.  
  104. --CALL
  105.  
  106. --by doing this the addon will call functions based on registered events by itself
  107. addon:SetScript("OnEvent", function(self, event)
  108.   self[event](self)
  109.   --return self[event](self)
  110. end)
  111. --register events
  112. addon:RegisterEvent("VARIABLES_LOADED")
  113. addon:RegisterEvent("PLAYER_LOGIN")

Result is a blank panel.
Example shows loading the DB, creating the panel/subpanel and adding a slashcmd to call the panel.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 02-28-12 at 02:49 PM.
  Reply With Quote