-- GridSort.lua --{{{ Libraries local Compost = AceLibrary("Compost-2.0") local RL = AceLibrary("RosterLib-2.0") --}}} --{{{ GridSort --{{{ Initialization GridSort = Grid:NewModule("GridSort") --{{{ AceDB defaults GridSort.defaultDB = { debug = true, } --}}} --{{{ AceOptions table GridSort.options = { type = "group", name = "Sort", desc = "Options for GridSort.", args = { ["debug"] = { type = "toggle", name = "Debug", desc = "Toggle debugging.", get = function () return GridSort.db.profile.debug end, set = function (v) GridSort.db.profile.debug = v GridSort.debugging = v end, }, }, } --}}} --}}} function GridSort:OnEnable() self:RegisterEvent("Grid_UnitJoined", "UpdateSort") self:RegisterEvent("Grid_UnitLeft", "UpdateSort") self:RegisterEvent("Grid_UpdateSort", "UpdateSort") end function GridSort:OnDisable() Compost:Reclaim(self.groups, 2) self.groups = nil end -- override this if you want to group units differently -- group units by raid subgroup function GridSort:UnitGroup(unit) return unit.subgroup or 1 end -- override this if you want to sort groups differently -- sort groups by name function GridSort.groupSorter(a, b) return a < b end -- generate sorted groups function GridSort:UpdateSort() local name, unit, group self:Debug("UpdateSort()") if self.groups then self.groups = Compost:Reclaim(self.groups, 2) end self.groups = Compost:Acquire() for unit in RL:IterateRoster() do group = self:UnitGroup(unit) -- create group if it doesn't exist if not self.groups[group] then -- create any intermediary groups too for i = (table.getn(self.groups)+1), group do self.groups[i] = Compost:Acquire() table.setn(self.groups, i) end end table.insert(self.groups[group], unit.name) end for i = 1, table.getn(self.groups) do table.sort(self.groups[i], self.groupSorter) end self:TriggerEvent("Grid_UpdateDisplay") end function GridSort:Iterator() local group = 1 local member = 0 if not self.groups then self:UpdateSort() end return function() member = member + 1 -- find the next populated group if we have an empty group, -- or we've reached the end of the current group while not self.groups[group] or not self.groups[group][member] do group = group + 1 member = 1 -- we've run out of groups if group > table.getn(self.groups) then return nil end end return self.groups[group][member], group, member end end --}}}