View Single Post
02-15-09, 11:52 AM   #1
weaselSR
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Feb 2008
Posts: 5
Options Panel help

Hi, I'm trying to add a simple GUI for my mod, I found some code and edited it to do what I want -- it works, but it seems to randomly reset itself to default values.

I'm wondering if anybody can tell me why that is, and if so remove any unneeded code at the same time, such as the radio buttons that I won't be using etc.

Code:
MyAddon = CreateFrame("Frame", "MyAddon", UIParent) 
	MyAddon:RegisterEvent("ADDON_LOADED") 
	MyAddon:SetScript("OnEvent", function() 
	    if (event == "ADDON_LOADED" and arg1 == "MyAddon") then 
	        -- DEFAULT USER SETTINGS 
	        MyAddonDB = MyAddonDB or {} 
	        if MyAddonDB.Fakes == nil then MyAddonDB.Fakes = true end 
	        if MyAddonDB.Swears == nil then MyAddonDB.Swears = true end 
	        if MyAddonDB.Slabs == nil then MyAddonDB.Slabs = true end 
	        if MyAddonDB.LFG == nil then MyAddonDB.LFG = true end 
	        if MyAddonDB.GRecruit == nil then MyAddonDB.GRecruit = false end 
	 
	        SlashCmdList["MyAddon"] = function() InterfaceOptionsFrame_OpenToCategory('MyAddon') end 
	        SLASH_MyAddon1 = "/MyAddon"
			SLASH_MyAddon2 = "/MyAddon2"
			SLASH_MyAddon3 = "/MyAddon3"			
	 
	        MyAddon:LoadOptions(self) 
	    end 
	end) 
	
	function MyAddon:LoadOptions(self) 
	    local panel = CreateFrame("FRAME", "MyAddon_Options_Panel") 
			 
	    panel.name = "MyAddon"
	    panel.okay = function() 
	        for i, button in ipairs{panel:GetChildren()} do 
	            button.origValue = table[field] 
	        end 
	    end 
	 
	    panel.cancel = function() 
	        for i, button in ipairs{panel:GetChildren()} do 
	            if button:Restore() then 
	                button:Restore() 
	            end 
	        end 
	    end 
	 
	    panel.default = function() 
	        MyAddonDB.Fakes = true
	        MyAddonDB.Swears = true 
	        MyAddonDB.State = true 
	        MyAddonDB.Alert = true 
	        MyAddonDB.Blah = false 
	        InterfaceOptionsFrameOkay:Click() 
	     end 
	 
	    -- Checkbox 1
	    local Button1 = MyAddon:CreateCheckButton("Fakes", panel, MyAddonDB, "Fakes", false) 
	    Button1:SetPoint('TOPLEFT', 10, -8) 
		
		-- Checkbox 2
	    local Button2 = MyAddon:CreateCheckButton("Swears", panel, MyAddonDB, "Swears", false) 
	    Button2:SetPoint('TOPLEFT', 10, -30) 
	 
		-- Checkbox 3
	    local Button3 = MyAddon:CreateCheckButton("Slabs", panel, MyAddonDB, "Slabs", false) 
	    Button3:SetPoint('TOPLEFT', 10, -52) 
	 
		-- Checkbox 4
	    local Button4 = MyAddon:CreateCheckButton("LFG", panel, MyAddonDB, "LFG", false) 
	    Button4:SetPoint('TOPLEFT', 10, -74) 
		
		-- Checkbox 5
		local Button5 = MyAddon:CreateCheckButton("Guild Recruit", panel, MyAddonDB, "GRecruit", false) 
	    Button5:SetPoint('TOPLEFT', 10, -96) 
	 
	    InterfaceOptions_AddCategory(panel) 
	end 

	function MyAddon:CreateCheckButton(name, parent, table, field, radio) 
	    local button 
	    if radio then 
	        button = CreateFrame('CheckButton', parent:GetName() .. name, parent, 'SendMailRadioButtonTemplate') 
	    else 
	        button = CreateFrame('CheckButton', parent:GetName() .. name, parent, 'OptionsCheckButtonTemplate') 
	    end 
	 
	    local frame = _G[button:GetName() .. 'Text'] 
	    frame:SetText(name) 
	    frame:SetTextColor(1, 1, 1, 1) 
	    frame:SetFontObject(GameFontNormal) 
	 
	    button:SetScript("OnShow", 
	        function (self) 
	            self:SetChecked(table[field]) 
	            self.origValue = table[field] or self.origValue 
	        end 
	    ) 
	 
	    if radio then 
	        button:SetScript("OnClick", 
	            function (self, button, down) 
	                _G["MyAddon_Options_PanelUse alert.wav"]:SetChecked(nil) 
	                _G["MyAddon_Options_PanelUse a random in-game sound"]:SetChecked(nil)	 
	                this:SetChecked(1) 
	                table[field] = not table[field] 
	            end 
	        ) 
	    else 
	        button:SetScript("OnClick", 
	            function (self, button, down) 
	                table[field] = not table[field] 
	            end 
	        ) 
	    end 
	 
	    function button:Restore() 
	        table[field] = self.origValue 
	    end 
	 
	    return button 
	end
Also, how would I add some Text to the top of the window with a description/title of the addon? I tried doing something like this (below) but it wouldn't show up.
Code:
	MyAddonname = title	
	local text = MyAddon:CreateFontString(nil, 'ARTWORK', 'GameFontNormalLarge')
	text:SetPoint('TOPLEFT', 16, -16)
	text:SetText("Title of MyAddon")
Thanks for looking!
  Reply With Quote