| 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@161 | 7 local ReAction = ReAction | 
| flickerstreak@161 | 8 local L = ReAction.L | 
| flickerstreak@161 | 9 local _G = _G | 
| flickerstreak@161 | 10 | 
| flickerstreak@161 | 11 -- button | 
| flickerstreak@161 | 12 local Button = ReAction.Button.MultiCast | 
| flickerstreak@161 | 13 | 
| flickerstreak@161 | 14 -- module declaration | 
| flickerstreak@161 | 15 local moduleID = "Totem" | 
| flickerstreak@161 | 16 local module = ReAction:NewModule( moduleID, | 
| flickerstreak@161 | 17   "AceEvent-3.0" | 
| flickerstreak@161 | 18   -- mixins go here | 
| flickerstreak@161 | 19 ) | 
| flickerstreak@161 | 20 | 
| flickerstreak@161 | 21 -- handlers | 
| flickerstreak@161 | 22 function module:OnInitialize() | 
| flickerstreak@161 | 23   self.db = ReAction.db:RegisterNamespace( moduleID, | 
| flickerstreak@161 | 24     { | 
| flickerstreak@161 | 25       profile = { | 
| flickerstreak@161 | 26         buttons = { } | 
| flickerstreak@161 | 27       } | 
| flickerstreak@161 | 28     } | 
| flickerstreak@161 | 29   ) | 
| flickerstreak@161 | 30 | 
| flickerstreak@161 | 31   self.buttons = { } | 
| flickerstreak@161 | 32 | 
| flickerstreak@161 | 33   ReAction:RegisterOptions(self, self:GetOptions()) | 
| flickerstreak@161 | 34 | 
| flickerstreak@161 | 35   ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar") | 
| flickerstreak@161 | 36   ReAction.RegisterCallback(self, "OnDestroyBar") | 
| flickerstreak@161 | 37   ReAction.RegisterCallback(self, "OnRefreshBar") | 
| flickerstreak@161 | 38   ReAction.RegisterCallback(self, "OnEraseBar") | 
| flickerstreak@161 | 39   ReAction.RegisterCallback(self, "OnRenameBar") | 
| flickerstreak@161 | 40 | 
| flickerstreak@161 | 41   -- TODO: register for learning new spells | 
| flickerstreak@161 | 42 end | 
| flickerstreak@161 | 43 | 
| flickerstreak@161 | 44 function module:OnEnable() | 
| flickerstreak@161 | 45   ReAction:RegisterBarType(L["Totem Bar"], | 
| flickerstreak@161 | 46     { | 
| flickerstreak@161 | 47       type = moduleID , | 
| flickerstreak@161 | 48       defaultButtonSize = 36, | 
| flickerstreak@161 | 49       defaultBarRows = 1, | 
| flickerstreak@161 | 50       defaultBarCols = 6, | 
| flickerstreak@161 | 51       defaultBarSpacing = 3 | 
| flickerstreak@161 | 52     }) | 
| flickerstreak@161 | 53 | 
| flickerstreak@161 | 54 end | 
| flickerstreak@161 | 55 | 
| flickerstreak@161 | 56 function module:OnDisable() | 
| flickerstreak@161 | 57   ReAction:UnregisterBarType(L["Totem Bar"]) | 
| flickerstreak@161 | 58 end | 
| flickerstreak@161 | 59 | 
| flickerstreak@161 | 60 function module:OnDestroyBar(event, bar, name) | 
| flickerstreak@161 | 61   local btns = self.buttons[bar] | 
| flickerstreak@161 | 62   if btns then | 
| flickerstreak@161 | 63     for _,b in pairs(btns) do | 
| flickerstreak@161 | 64       if b then | 
| flickerstreak@161 | 65         b:Destroy() | 
| flickerstreak@161 | 66       end | 
| flickerstreak@161 | 67     end | 
| flickerstreak@161 | 68     self.buttons[bar] = nil | 
| flickerstreak@161 | 69   end | 
| flickerstreak@161 | 70 end | 
| flickerstreak@161 | 71 | 
| flickerstreak@161 | 72 function module:OnRefreshBar(event, bar, name) | 
| flickerstreak@161 | 73   if bar.config.type == moduleID then | 
| flickerstreak@161 | 74     local btns = self.buttons[bar] | 
| flickerstreak@161 | 75     if btns == nil then | 
| flickerstreak@161 | 76       btns = { } | 
| flickerstreak@161 | 77       self.buttons[bar] = btns | 
| flickerstreak@161 | 78     end | 
| flickerstreak@161 | 79     local profile = self.db.profile | 
| flickerstreak@161 | 80     if profile.buttons[name] == nil then | 
| flickerstreak@161 | 81       profile.buttons[name] = {} | 
| flickerstreak@161 | 82     end | 
| flickerstreak@161 | 83     local btnCfg = profile.buttons[name] | 
| flickerstreak@161 | 84 | 
| flickerstreak@161 | 85     local r, c = bar:GetButtonGrid() | 
| flickerstreak@161 | 86     local n = r*c | 
| flickerstreak@161 | 87     n = min(n,Button.SetupBarHeader(bar)) | 
| flickerstreak@161 | 88     for i = 1, n do | 
| flickerstreak@161 | 89       if btnCfg[i] == nil then | 
| flickerstreak@161 | 90         btnCfg[i] = {} | 
| flickerstreak@161 | 91       end | 
| flickerstreak@161 | 92       if btns[i] == nil then | 
| flickerstreak@161 | 93         local success, r = pcall(Button.New,Button,i,btnCfg,bar) | 
| flickerstreak@161 | 94         if success and r then | 
| flickerstreak@161 | 95           btns[i] = r | 
| flickerstreak@161 | 96           bar:AddButton(i,r) | 
| flickerstreak@161 | 97         else | 
| flickerstreak@161 | 98           geterrorhandler()(r) | 
| flickerstreak@161 | 99           n = i - 1 | 
| flickerstreak@161 | 100           bar:ClipNButtons(n) | 
| flickerstreak@161 | 101           break | 
| flickerstreak@161 | 102         end | 
| flickerstreak@161 | 103       end | 
| flickerstreak@161 | 104       btns[i]:Refresh() | 
| flickerstreak@161 | 105     end | 
| flickerstreak@161 | 106     for i = n+1, #btns do | 
| flickerstreak@161 | 107       if btns[i] then | 
| flickerstreak@161 | 108         bar:RemoveButton(btns[i]) | 
| flickerstreak@161 | 109         btns[i] = btns[i]:Destroy() | 
| flickerstreak@161 | 110         if btnCfg[i] then | 
| flickerstreak@161 | 111           btnCfg[i] = nil | 
| flickerstreak@161 | 112         end | 
| flickerstreak@161 | 113       end | 
| flickerstreak@161 | 114     end | 
| flickerstreak@161 | 115   end | 
| flickerstreak@161 | 116 | 
| flickerstreak@161 | 117 end | 
| flickerstreak@161 | 118 | 
| flickerstreak@161 | 119 function module:OnEraseBar(event, bar, name) | 
| flickerstreak@161 | 120   self.db.profile.buttons[name] = nil | 
| flickerstreak@161 | 121 end | 
| flickerstreak@161 | 122 | 
| flickerstreak@161 | 123 function module:OnRenameBar(event, bar, oldName, newName) | 
| flickerstreak@161 | 124   local b = self.db.profile.buttons | 
| flickerstreak@161 | 125   b[newname], b[oldname] = b[oldname], nil | 
| flickerstreak@161 | 126 end | 
| flickerstreak@161 | 127 | 
| flickerstreak@161 | 128 function module:RefreshAll() | 
| flickerstreak@161 | 129   for bar in pairs(self.buttons) do | 
| flickerstreak@161 | 130     self:OnRefreshBar(nil,bar,bar:GetName()) | 
| flickerstreak@161 | 131   end | 
| flickerstreak@161 | 132 end | 
| flickerstreak@161 | 133 | 
| flickerstreak@161 | 134 | 
| flickerstreak@161 | 135 ---- options ---- | 
| flickerstreak@161 | 136 function module:GetOptions() | 
| flickerstreak@161 | 137   return { | 
| flickerstreak@161 | 138     stance = | 
| flickerstreak@161 | 139     { | 
| flickerstreak@161 | 140       name = L["Totem Buttons"], | 
| flickerstreak@161 | 141       type = "group", | 
| flickerstreak@161 | 142       args = { | 
| flickerstreak@161 | 143         -- TODO | 
| flickerstreak@161 | 144       } | 
| flickerstreak@161 | 145     } | 
| flickerstreak@161 | 146   } | 
| flickerstreak@161 | 147 end |