Originally Posted by MoonWitch
I know better, but shame prevents me from doing that I kinda fear someone (not you though) might mock me for life.
|
I will mock you for life for not showing it.
Originally Posted by MoonWitch
|
A couple quick notes before the important parts:
(1) While it's true that Blizzard designed the options panel API so that the "cancel" function is
supposed to revert all changes made since the panel was opened... addons generally do not do this, since pressing Esc triggers the "cancel" function instead of the "okay" function and it's just confusing for users to close the window and lose all their changes. Just apply settings changes immediately, and don't set "okay" or "cancel" functions.
(2) The "defaults" function is likewise pretty useless; almost no addon implements it, and I'm not sure any user has ever pressed that button with the
intention of resetting every single UI and addon setting. If it only applied to the current panel, that would be more useful, but that's not the case.
(3) Your current "cancel" function references a "temporary" table that's not defined anywhere. Fortunately this is not a real issue since you should just get rid of that whole function.
(4) Your widget API is fine, but you should use a consistent naming pattern. Either use "CreateX" or "AddX" but don't mix them; that's just confusing!
(6) Your "AddCheckbox" method is receiving a "parent" as its first (named) argument. However, none of the other widget constructor methods receive this argument, and since it's a method, it already gets the "self" argument, which in your use-case is the same as what you're passing as "parent", so you don't need to explicitly pass that.
(7) Most Blizzard frame templates require global names. You (probably) won't get errors from most of them, but you will end up with some weird overlapping global names for their regions and children. The name doesn't actually matter, so you can just randomize it.
(8) The UICheckButtonTemplate template already includes a fontstring, so you don't need to create one yourself. Also, I'd use the InterfaceOptionsCheckButtonTemplate template instead, since it also includes predefinted tooltip behavior for additional description text.
(9) Since you're going the API route for widgets, I'd go a little further and do something like this in the constructor function:
Lua Code:
function Panel:CreateCheckbox(text, tooltipText)
-- Use random name. Template includes a fontstring and tooltip behavior.
local checkButton = CreateFrame("Button", "BobCheckbox" .. random(1000000), self, "InterfaceOptionsCheckButtonTemplate")
-- Override the default OnClick script since it includes unwanted Blizz-specific behaviors.
checkButton:SetScript("OnClick", function(self)
local checked = not not self:GetChecked() -- convert 1/nil to true/false
PlaySound(checked and "igMainMenuOptionCheckBoxOn" or "igMainMenuOptionCheckBoxOff")
if self.func then
self:func(checked)
end
end)
-- Customize it
checkButton.text:SetText(text)
checkButton.tooltipText = tooltipText
-- Return it
return checkButton
end
This way your constructor doesn't need to know anything about which setting the checkbox is changing, or how to apply that setting to your addon. You can do that separately for each individual checkbox:
Lua Code:
local timeFormat = self:CreateCheckbox("Use 24-hour time format", "Give a longer description of the option here if you want, or remove this argument.")
-- set a poitn here
timeFormat.func = function(self, value)
BobTheClockDB.timeform24 = value
-- apply the setting to your UI
end