Quantcast
WoWInterface - View Single Post - Toggling modules with proxy table
View Single Post
02-14-18, 11:42 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I already don't know what the heck you've done there.

I'd do something like this very light and totally untested system that uses AceDB and replicates the basic features of AceAddon and AceEvent, plus some specialized logic to automatically toggle modules on and off based on spec:

Lua Code:
  1. -- create addon object:
  2. local MyAddon = CreateFrame("Frame", "MyAddon")
  3.  
  4. -- create table to use as a module registry:
  5. MyAddon.modules = {}
  6.  
  7. -- set up AceEvent-style event handling:
  8. local function eventHandler(self, event, ...) return self[event](self, ...) end
  9. MyAddon:SetScript("OnEvent", eventHandler)
  10. MyAddon:RegisterEvent("PLAYER_LOGIN")
  11.  
  12. -- add module creation:
  13. function MyAddon:NewModule(name, defaults)
  14.     -- make each module its own object with its own event handling:
  15.     local module = CreateFrame("Frame")
  16.     module:SetScript("OnEvent", eventHandler)
  17.  
  18.     if defaults then
  19.         if self.db then
  20.             -- for modules registered after login:
  21.             module.db = self.db:RegisterNamespace(name, defaults)
  22.         else
  23.             -- for modules registered before/during login,
  24.             -- the core db doesn't exist yet, so hold onto it:
  25.             self.pending = self.pending or {}
  26.             self.pending[name] = defaults
  27.         end
  28.     end
  29.  
  30.     -- add it to the registry:
  31.     self.modules[name] = module
  32.  
  33.     -- and pass back a reference to the object:
  34.     return module
  35. end
  36.  
  37. function MyAddon:GetModule(name)
  38.     return self.modules[name]
  39. end
  40.  
  41. -- basically OnInitialize:
  42. function MyAddon:PLAYER_LOGIN()
  43.     -- register the core addon db here:
  44.     local defaults = {
  45.         profile = {
  46.             cats = true,
  47.             dogs = false,
  48.         }
  49.     }
  50.     self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true)
  51.  
  52.     -- deal with modules registered before/during login:
  53.     if self.pending then
  54.         for name, defaults in pairs(self.pending) do
  55.             local module = self.modules[name]
  56.             module.db = self.db:RegisterNamespace(name, defaults)
  57.         end
  58.         self.pending = nil
  59.     end
  60.  
  61.     -- register your main logic event:
  62.     self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
  63.     self:ACTIVE_TALENT_GROUP_CHANGED()
  64. end
  65.  
  66. -- handle your main logic event:
  67. function MyAddon:ACTIVE_TALENT_GROUP_CHANGED()
  68.     local id = GetSpecializationInfo(GetSpecialization())
  69.     -- go through all the registered modules:
  70.     for name, module in pairs(self.modules) do
  71.         -- and enable or disable them according to whether they match the current spec:
  72.         if module.specID == id then
  73.             module.enabled = true
  74.             module:Enable()
  75.         else
  76.             module:Disable()
  77.             module.enabled = false
  78.         end
  79.     end
  80. end

And then in each module:
Lua Code:
  1. -- create a new module with some default profile settings:
  2. local DruidCatModule = MyAddon:NewModule("DruidCat", {
  3.     profile = {
  4.         fluffy = true,
  5.     }
  6. })
  7.  
  8. -- give it a specID so the core can figure out when to toggle it:
  9. DruidCatModule.specID = 12345 -- note: not the real spell ID!
  10.  
  11. -- and an Enable method the core can call to turn it on:
  12. function DruidCatModule:Enable()
  13.     -- register for events, create frames if they don't exist yet, show frames
  14.  
  15.     -- self.db gives you the module's specific db
  16.  
  17.     -- register an event just like you would in the core or with AceEvent:
  18.     self:RegisterEvent("EXAMPLE_EVENT")
  19. end
  20.  
  21. -- and a Disable method the core can call to turn it off:
  22. function DruidCatModule:Disable()
  23.     -- unregister events, hide frames
  24.  
  25.     self:UnregisterEvent("EXAMPLE_EVENT")
  26. end
  27.  
  28. -- and handle an event like so:
  29. function DruidCatModule:EXAMPLE_EVENT(fakeArg)
  30.     print("This will never happen.")
  31. end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Donít message me about addon bugs or programming questions.

Last edited by Phanx : 02-15-18 at 12:06 AM.
  Reply With Quote