annotate modules/Stance.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 bf64e71701e2
children d08a74e86c96
rev   line source
flickerstreak@134 1 --[[
flickerstreak@134 2 ReAction Stance button module
flickerstreak@134 3
flickerstreak@134 4 --]]
flickerstreak@134 5
flickerstreak@134 6 -- local imports
flickerstreak@175 7 local addonName, addonTable = ...
flickerstreak@175 8 local ReAction = addonTable.ReAction
flickerstreak@134 9 local L = ReAction.L
flickerstreak@134 10 local _G = _G
flickerstreak@134 11
flickerstreak@134 12 -- Stance button
flickerstreak@134 13 local Button = ReAction.Button.Stance
flickerstreak@134 14
flickerstreak@134 15 -- module declaration
flickerstreak@134 16 local moduleID = "Stance"
flickerstreak@134 17 local module = ReAction:NewModule( moduleID
flickerstreak@134 18 -- mixins go here
flickerstreak@134 19 )
flickerstreak@134 20
flickerstreak@134 21 -- handlers
flickerstreak@134 22 function module:OnInitialize()
flickerstreak@134 23 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@134 24 {
flickerstreak@134 25 profile = {
flickerstreak@134 26 buttons = { }
flickerstreak@134 27 }
flickerstreak@134 28 }
flickerstreak@134 29 )
flickerstreak@134 30
flickerstreak@134 31 self.buttons = { }
flickerstreak@134 32
flickerstreak@134 33 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
flickerstreak@134 34 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@134 35 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@134 36 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@134 37 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@134 38 end
flickerstreak@134 39
flickerstreak@134 40 function module:OnEnable()
flickerstreak@218 41 ReAction:RegisterBarType(Button)
flickerstreak@134 42 end
flickerstreak@134 43
flickerstreak@134 44 function module:OnDisable()
flickerstreak@218 45 ReAction:UnregisterBarType(Button)
flickerstreak@134 46 end
flickerstreak@134 47
flickerstreak@134 48 function module:OnDestroyBar(event, bar, name)
flickerstreak@134 49 local btns = self.buttons[bar]
flickerstreak@134 50 if btns then
flickerstreak@134 51 for _,b in pairs(btns) do
flickerstreak@134 52 if b then
flickerstreak@134 53 b:Destroy()
flickerstreak@134 54 end
flickerstreak@134 55 end
flickerstreak@134 56 self.buttons[bar] = nil
flickerstreak@134 57 end
flickerstreak@134 58 end
flickerstreak@134 59
flickerstreak@134 60 function module:OnRefreshBar(event, bar, name)
flickerstreak@134 61 if bar.config.type == moduleID then
flickerstreak@134 62 local btns = self.buttons[bar]
flickerstreak@134 63 if btns == nil then
flickerstreak@134 64 btns = { }
flickerstreak@134 65 self.buttons[bar] = btns
flickerstreak@134 66 end
flickerstreak@134 67 local profile = self.db.profile
flickerstreak@134 68 if profile.buttons[name] == nil then
flickerstreak@134 69 profile.buttons[name] = {}
flickerstreak@134 70 end
flickerstreak@134 71 local btnCfg = profile.buttons[name]
flickerstreak@134 72
flickerstreak@134 73 local r, c = bar:GetButtonGrid()
flickerstreak@134 74 local n = r*c
flickerstreak@134 75 for i = 1, n do
flickerstreak@134 76 if btnCfg[i] == nil then
flickerstreak@134 77 btnCfg[i] = {}
flickerstreak@134 78 end
flickerstreak@134 79 if btns[i] == nil then
flickerstreak@134 80 local success, r = pcall(Button.New,Button,i,profile,bar,i>1 and btnCfg[i-1].stanceID)
flickerstreak@134 81 if success and r then
flickerstreak@134 82 btns[i] = r
flickerstreak@134 83 bar:AddButton(i,r)
flickerstreak@134 84 else
flickerstreak@134 85 n = i - 1
flickerstreak@134 86 bar:ClipNButtons(n)
flickerstreak@134 87 break
flickerstreak@134 88 end
flickerstreak@134 89 end
flickerstreak@134 90 btns[i]:Refresh()
flickerstreak@134 91 end
flickerstreak@134 92 for i = n+1, #btns do
flickerstreak@134 93 if btns[i] then
flickerstreak@134 94 bar:RemoveButton(btns[i])
flickerstreak@134 95 btns[i] = btns[i]:Destroy()
flickerstreak@134 96 if btnCfg[i] then
flickerstreak@134 97 btnCfg[i] = nil
flickerstreak@134 98 end
flickerstreak@134 99 end
flickerstreak@134 100 end
flickerstreak@134 101 end
flickerstreak@134 102
flickerstreak@134 103 end
flickerstreak@134 104
flickerstreak@134 105 function module:OnEraseBar(event, bar, name)
flickerstreak@134 106 self.db.profile.buttons[name] = nil
flickerstreak@134 107 end
flickerstreak@134 108
flickerstreak@134 109 function module:OnRenameBar(event, bar, oldName, newName)
flickerstreak@134 110 local b = self.db.profile.buttons
flickerstreak@134 111 b[newname], b[oldname] = b[oldname], nil
flickerstreak@134 112 end
flickerstreak@134 113
flickerstreak@134 114 function module:RefreshAll()
flickerstreak@134 115 for bar in pairs(self.buttons) do
flickerstreak@134 116 self:OnRefreshBar(nil,bar,bar:GetName())
flickerstreak@134 117 end
flickerstreak@134 118 end
flickerstreak@134 119