SFX-WoW / Masque

A button-skinning engine for World of Warcraft.
Other
44 stars 10 forks source link

Feature: 10.1.0 Group Callbacks #324

Closed StormFX closed 1 year ago

StormFX commented 1 year ago

After adding the new Group callback API in 10.0.7, I realized that it may be useful to allow authors to register callbacks for only certain events (IE, changes to specific settings via Masque's options). Therefore, version 10.1.0 includes changes to enable this behavior.

Callbacks can still be registered in the same way they were in 10.0.7, however, now additional string parameters can be passed to target specific options. Some examples:

Before API_VERSION: 100100:

local function myCallback(Group, Option, Value)
    if Option == "Disabled" then
        -- Do something.
    end
end

local Group = Masque:Group("myAddon", "myGroup")
-- Note that this callback will be fired every time ANY option is changed and thus has to be filtered,
-- as demonstrated in the function above.
Group:RegisterCallback(myCallback)

After API_VERSION: 100100:

local function myCallback(Group, Option, Value)
    -- Do something.
end

local Group = Masque:Group("myAddon", "myGroup")
-- This callback will only be fired when the "Disabled" option is changed, so doesn't need to be filtered.
Group:RegisterCallback(myCallback, "Disabled")

The same logic can be applied when passing a method:

local myObj = {}
function myObj:myCallback(Group, Option, Value)
    -- Do something.
end

local Group = Masque:Group("myAddon", "myGroup")
Group:RegisterCallback(myObj.myCallback, myObj, "Disabled")

Additionally, multiple events can be registered with the same function, though will have to be filtered:

local function myCallback(Group, Option, Value)
    if Option == "Disabled" then
        -- Do something.
    elseif Option == "Reset" then
        -- Do something else.
    end
end

local Group = Masque:Group("myAddon", "myGroup")
Group:RegisterCallback(myCallback, "Disabled", "Reset")

The list of events/options can be found on the wiki page.

I'll try to put together a comprehensive article soon, but it may take me a bit. This, at least, should give a basic understanding of the changes.

P.S. The Register API method and the SetCallback Group method are both deprecated and no longer function (they'll throw out a warning once).