WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Options Panel help (https://www.wowinterface.com/forums/showthread.php?t=20555)

weaselSR 02-15-09 11:52 AM

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!

Ne0nguy 05-04-09 06:05 PM

I've been having a little trouble with this issue myself.
Here's what I've learned so far:

Whenever you press the 'Okay' button, it executes all of the panel.okay functions, regardless of what panel is currently displayed.
If you use an OnShow function to load your variables into the options panel all the values you would normally be saving with the panel.okay function will be empty until that panel is specifically shown.
One way around this would be to set a variable in your OnShow function (MyAddon.panelLoaded = true) and check for that in your panel.okay function (if MyAddon.panelLoaded ~= nil) before you actually try to save those settings.

As for the title, this seemed to work for me:
Code:

local fonttitle = panel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge");
fonttitle:SetPoint("TOPLEFT", panel, "TOPLEFT", 15, -15);
fonttitle:SetText("Addon Title Here");


Exawatt 05-04-09 11:24 PM

Quote:

Originally Posted by Ne0nguy (Post 133529)
Whenever you press the 'Okay' button, it executes all of the panel.okay functions, regardless of what panel is currently displayed.
If you use an OnShow function to load your variables into the options panel all the values you would normally be saving with the panel.okay function will be empty until that panel is specifically shown.

I didn't realize the "Okay" button functioned that way. This actually helps me solve the same problem I've been having with my addons. Thanks. :)


All times are GMT -6. The time now is 10:01 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI