annotate modules/Totem.lua @ 218:e63aefb8a555

Demodularization of buttons - register class instead of config
author Flick <flickerstreak@gmail.com>
date Fri, 19 Nov 2010 23:06:24 -0800
parents c8777ae7d460
children d08a74e86c96
rev   line source
flickerstreak@161 1 --[[
flickerstreak@161 2 ReAction Totem button module
flickerstreak@161 3
flickerstreak@161 4 --]]
flickerstreak@161 5
flickerstreak@161 6 -- local imports
flickerstreak@175 7 local addonName, addonTable = ...
flickerstreak@175 8 local ReAction = addonTable.ReAction
flickerstreak@161 9 local L = ReAction.L
flickerstreak@161 10 local _G = _G
flickerstreak@161 11
flickerstreak@161 12 -- button
flickerstreak@161 13 local Button = ReAction.Button.MultiCast
flickerstreak@161 14
flickerstreak@161 15 -- module declaration
flickerstreak@161 16 local moduleID = "Totem"
flickerstreak@161 17 local module = ReAction:NewModule( moduleID,
flickerstreak@161 18 "AceEvent-3.0"
flickerstreak@161 19 -- mixins go here
flickerstreak@161 20 )
flickerstreak@161 21
flickerstreak@161 22 -- handlers
flickerstreak@161 23 function module:OnInitialize()
flickerstreak@161 24 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@161 25 {
flickerstreak@161 26 profile = {
flickerstreak@161 27 buttons = { }
flickerstreak@161 28 }
flickerstreak@161 29 }
flickerstreak@161 30 )
flickerstreak@161 31
flickerstreak@161 32 self.buttons = { }
flickerstreak@161 33
flickerstreak@161 34 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
flickerstreak@161 35 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@161 36 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@161 37 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@161 38 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@161 39
flickerstreak@162 40 self:RegisterEvent("UPDATE_MULTI_CAST_ACTIONBAR","PLAYER_ENTERING_WORLD")
flickerstreak@161 41 end
flickerstreak@161 42
flickerstreak@161 43 function module:OnEnable()
flickerstreak@218 44 ReAction:RegisterBarType(Button)
flickerstreak@161 45 end
flickerstreak@161 46
flickerstreak@161 47 function module:OnDisable()
flickerstreak@218 48 ReAction:UnregisterBarType(Button)
flickerstreak@161 49 end
flickerstreak@161 50
flickerstreak@161 51 function module:OnDestroyBar(event, bar, name)
flickerstreak@161 52 local btns = self.buttons[bar]
flickerstreak@161 53 if btns then
flickerstreak@161 54 for _,b in pairs(btns) do
flickerstreak@161 55 if b then
flickerstreak@161 56 b:Destroy()
flickerstreak@161 57 end
flickerstreak@161 58 end
flickerstreak@161 59 self.buttons[bar] = nil
flickerstreak@161 60 end
flickerstreak@161 61 end
flickerstreak@161 62
flickerstreak@161 63 function module:OnRefreshBar(event, bar, name)
flickerstreak@161 64 if bar.config.type == moduleID then
flickerstreak@161 65 local btns = self.buttons[bar]
flickerstreak@161 66 if btns == nil then
flickerstreak@161 67 btns = { }
flickerstreak@161 68 self.buttons[bar] = btns
flickerstreak@161 69 end
flickerstreak@161 70 local profile = self.db.profile
flickerstreak@161 71 if profile.buttons[name] == nil then
flickerstreak@161 72 profile.buttons[name] = {}
flickerstreak@161 73 end
flickerstreak@161 74 local btnCfg = profile.buttons[name]
flickerstreak@161 75
flickerstreak@161 76 local r, c = bar:GetButtonGrid()
flickerstreak@163 77 local n = min(r*c,6)
flickerstreak@163 78 Button.SetupBarHeader(bar)
flickerstreak@161 79 for i = 1, n do
flickerstreak@161 80 if btnCfg[i] == nil then
flickerstreak@161 81 btnCfg[i] = {}
flickerstreak@161 82 end
flickerstreak@164 83 if not btns[i] then
flickerstreak@161 84 local success, r = pcall(Button.New,Button,i,btnCfg,bar)
flickerstreak@163 85 if success then
flickerstreak@161 86 btns[i] = r
flickerstreak@163 87 if r then
flickerstreak@163 88 bar:AddButton(i,r)
flickerstreak@163 89 end
flickerstreak@161 90 else
flickerstreak@161 91 geterrorhandler()(r)
flickerstreak@161 92 n = i - 1
flickerstreak@161 93 bar:ClipNButtons(n)
flickerstreak@161 94 break
flickerstreak@161 95 end
flickerstreak@161 96 end
flickerstreak@163 97 if btns[i] then
flickerstreak@163 98 btns[i]:Refresh()
flickerstreak@163 99 end
flickerstreak@161 100 end
flickerstreak@161 101 for i = n+1, #btns do
flickerstreak@161 102 if btns[i] then
flickerstreak@161 103 bar:RemoveButton(btns[i])
flickerstreak@161 104 btns[i] = btns[i]:Destroy()
flickerstreak@161 105 if btnCfg[i] then
flickerstreak@161 106 btnCfg[i] = nil
flickerstreak@161 107 end
flickerstreak@161 108 end
flickerstreak@161 109 end
flickerstreak@161 110 end
flickerstreak@161 111
flickerstreak@161 112 end
flickerstreak@161 113
flickerstreak@161 114 function module:OnEraseBar(event, bar, name)
flickerstreak@161 115 self.db.profile.buttons[name] = nil
flickerstreak@161 116 end
flickerstreak@161 117
flickerstreak@161 118 function module:OnRenameBar(event, bar, oldName, newName)
flickerstreak@161 119 local b = self.db.profile.buttons
flickerstreak@161 120 b[newname], b[oldname] = b[oldname], nil
flickerstreak@161 121 end
flickerstreak@161 122
flickerstreak@161 123 function module:RefreshAll()
flickerstreak@161 124 for bar in pairs(self.buttons) do
flickerstreak@161 125 self:OnRefreshBar(nil,bar,bar:GetName())
flickerstreak@161 126 end
flickerstreak@161 127 end
flickerstreak@161 128
flickerstreak@162 129 function module:UPDATE_MULTI_CAST_ACTIONBAR()
flickerstreak@162 130 if not InCombatLockdown() then
flickerstreak@162 131 for bar in pairs(self.buttons) do
flickerstreak@162 132 self:OnRefreshBar("OnRefreshBar", bar, bar:GetName())
flickerstreak@162 133 end
flickerstreak@162 134 end
flickerstreak@162 135 end
flickerstreak@162 136
flickerstreak@162 137 function module:PLAYER_ENTERING_WORLD()
flickerstreak@162 138 for bar in pairs(self.buttons) do
flickerstreak@162 139 self:OnRefreshBar("OnRefreshBar", bar, bar:GetName())
flickerstreak@162 140 end
flickerstreak@162 141 end
flickerstreak@162 142
flickerstreak@161 143