-- GridLayout.lua -- insert boilerplate --{{{ Libraries local Compost = AceLibrary("Compost-2.0") local AceOO = AceLibrary("AceOO-2.0") --}}} --{{{ Localization local L = AceLibrary("AceLocale-2.0"):new("GridLayout") --{{{ `- enUS local function strings_enUS() return { } end L:RegisterTranslations("enUS", strings_enUS) --}}} --}}} --{{{ GridLayout --{{{ Initialization GridLayout = Grid:NewModule("GridLayout") --{{{ AceDB defaults GridLayout.defaultDB = { Padding = 1, Spacing = 10, ScaleSize = 1.0, BorderR = .5, BorderG = .5, BorderB = .5, BorderA = 1, BackgroundR = .1, BackgroundG = .1, BackgroundB = .1, BackgroundA = .65, FrameLock = false, FrameDisplay = "always", } --}}} --{{{ AceOptions table GridLayout.options = { type = "group", name = "Layout", desc = "Options for GridLayout.", args = { ["padding"] = { type = "range", name = "Padding", desc = "Adjust frame padding.", max = 20, min = 0, step = 1, get = function () return GridLayout.db.profile.Padding end, set = function (v) GridLayout.db.profile.Padding = v GridLayout:UpdateDisplay() -- GridLayout:UpdateSize() end, }, ["spacing"] = { type = "range", name = "Spacing", desc = "Adjust frame spacing.", max = 100, min = 0, step = 1, get = function () return GridLayout.db.profile.Spacing end, set = function (v) GridLayout.db.profile.Spacing = v GridLayout:UpdateDisplay() end, }, ["scale"] = { type = "range", name = "Scale", desc = "Adjust Grid scale.", min = 0.5, max = 2.0, step = 0.05, isPercent = true, get = function () return GridLayout.db.profile.ScaleSize end, set = function (v) GridLayout.db.profile.ScaleSize = v GridLayout:Scale() end, }, ["border"] = { type = "color", name = "Border", desc = "Adjust border color and alpha.", get = function () return GridLayout.db.profile.BorderR, GridLayout.db.profile.BorderG, GridLayout.db.profile.BorderB, GridLayout.db.profile.BorderA end, set = function (r, g, b, a) GridLayout.db.profile.BorderR, GridLayout.db.profile.BorderG, GridLayout.db.profile.BorderB, GridLayout.db.profile.BorderA = r, g, b, a GridLayout:UpdateColor() end, hasAlpha = true }, ["background"] = { type = "color", name = "Background", desc = "Adjust background color and alpha.", get = function () return GridLayout.db.profile.BackgroundR, GridLayout.db.profile.BackgroundG, GridLayout.db.profile.BackgroundB, GridLayout.db.profile.BackgroundA end, set = function (r, g, b, a) GridLayout.db.profile.BackgroundR, GridLayout.db.profile.BackgroundG, GridLayout.db.profile.BackgroundB, GridLayout.db.profile.BackgroundA = r, g, b, a GridLayout:UpdateColor() end, hasAlpha = true }, ["lock"] = { type = "toggle", name = "Frame lock", desc = "Locks/unlocks the grid for movement.", get = function() return GridLayout.db.profile.FrameLock end, set = function() GridLayout.db.profile.FrameLock = not GridLayout.db.profile.FrameLock end, }, ["display"] = { type = "text", name = "Show Frame", desc = "Sets when the Grid is visible: Choose 'always' or 'grouped'.", get = function() return GridLayout.db.profile.FrameDisplay end, set = function(v) GridLayout.db.profile.FrameDisplay = v GridLayout:CheckVisibility() end, validate = {"always", "grouped"}, }, }, } --}}} --}}} function GridLayout:OnEnable() if not self.frame then self:CreateFrames() end self:UpdateDisplay() -- position and scale frame self:RestorePosition() self:Scale() -- update display, create grid frames self:RegisterEvent("Grid_UpdateDisplay", "UpdateDisplay") end function GridLayout:OnDisable() self.frame:Hide() end function GridLayout:StartMoveFrame() if not GridLayout.db.profile.FrameLock and arg1 == "LeftButton" then self.frame:StartMoving() self.frame.isMoving = true end end function GridLayout:StopMoveFrame() if self.frame.isMoving then self.frame:StopMovingOrSizing() self:SavePosition() self.frame.isMoving = false end end function GridLayout:CreateFrames() -- create main frame to hold all our gui elements local f = CreateFrame("Frame", "Grid", UIParent) f:EnableMouse(true) f:SetMovable(true) f:SetClampedToScreen(true) f:SetPoint("CENTER", UIParent, "CENTER") f:SetScript("OnMouseUp", function () self:StopMoveFrame() end) f:SetScript("OnHide", function () self:StopMoveFrame() end) f:SetScript("OnMouseDown", function () self:StartMoveFrame() end) f:SetFrameStrata("MEDIUM") -- create background f:SetFrameLevel(0) f:SetBackdrop({ bgFile = "Interface\\ChatFrame\\ChatFrameBackground", tile = true, tileSize = 16, edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16, insets = {left = 4, right = 4, top = 4, bottom = 4}, }) -- create bg texture f.texture = f:CreateTexture(nil, "BORDER") f.texture:SetTexture("Interface\\ChatFrame\\ChatFrameBackground") f.texture:SetPoint("TOPLEFT", f, "TOPLEFT", 4, -4) f.texture:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -4, 4) f.texture:SetBlendMode("ADD") f.texture:SetGradientAlpha("VERTICAL", .1, .1, .1, 0, .2, .2, .2, 0.5) self.frame = f end function GridLayout:PlaceFrame(frame, group, number) -- position frame local x = (frame:GetWidth() + self.db.profile.Padding) * (group - 1) + self.db.profile.Spacing local y = (frame:GetHeight() + self.db.profile.Padding) * (number - 1) + self.db.profile.Spacing frame:SetParent(self.frame) frame:SetPosition(self.frame, x, -y) end function GridLayout:UpdateDisplay() local name, group, number local maxGroup = 0 local maxNumber = 0 local sortModule = self.core:GetModule("GridSort") local frameModule = self.core:GetModule("GridFrame") for name,group,number in sortModule:Iterator() do local frame = frameModule:GetUnitFrame(name) if frame then self:PlaceFrame(frame, group, number) frame:Show() if group > maxGroup then maxGroup = group end if number > maxNumber then maxNumber = number end end end self.cols = maxGroup self.lines = maxNumber self:UpdateSize() self:UpdateColor() self:CheckVisibility() end function GridLayout:UpdateSize() local x = self.cols * (GridFrame:GetFrameSize() + self.db.profile.Padding) - self.db.profile.Padding + self.db.profile.Spacing * 2 local y = self.lines * (GridFrame:GetFrameSize() + self.db.profile.Padding) - self.db.profile.Padding + self.db.profile.Spacing * 2 self.frame:SetWidth(x) self.frame:SetHeight(y) end function GridLayout:UpdateColor() self.frame:SetBackdropBorderColor(GridLayout.db.profile.BorderR, GridLayout.db.profile.BorderG, GridLayout.db.profile.BorderB, GridLayout.db.profile.BorderA) self.frame:SetBackdropColor(GridLayout.db.profile.BackgroundR, GridLayout.db.profile.BackgroundG, GridLayout.db.profile.BackgroundB, GridLayout.db.profile.BackgroundA) end function GridLayout:CheckVisibility() if GridLayout.db.profile.FrameDisplay == "always" or GetNumPartyMembers() > 0 or GetNumRaidMembers() > 0 then self.frame:Show() else -- we should probably shutdown modules here? self.frame:Hide() end end function GridLayout:SavePosition() local f = self.frame local x, y = f:GetLeft(), f:GetTop() local s = f:GetEffectiveScale() x, y = x*s, y*s self.db.profile.PosX = x self.db.profile.PosY = y end function GridLayout:RestorePosition() local f = self.frame local x = self.db.profile.PosX or 500 local y = self.db.profile.PosY or 400 local s = f:GetEffectiveScale() x, y = x/s, y/s f:ClearAllPoints() f:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x, y) end function GridLayout:Scale() self:SavePosition() self.frame:SetScale(self.db.profile.ScaleSize) self:RestorePosition() end --}}}