Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Label Widget Tercio@23: Displays text and optionally an icon. Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local Type, Version = "Label", 23 Tercio@23: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) Tercio@23: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end Tercio@23: Tercio@23: -- Lua APIs Tercio@23: local max, select, pairs = math.max, select, pairs Tercio@23: Tercio@23: -- WoW APIs Tercio@23: local CreateFrame, UIParent = CreateFrame, UIParent Tercio@23: Tercio@23: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Tercio@23: -- List them here for Mikk's FindGlobals script Tercio@23: -- GLOBALS: GameFontHighlightSmall Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Support functions Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: Tercio@23: local function UpdateImageAnchor(self) Tercio@23: if self.resizing then return end Tercio@23: local frame = self.frame Tercio@23: local width = frame.width or frame:GetWidth() or 0 Tercio@23: local image = self.image Tercio@23: local label = self.label Tercio@23: local height Tercio@23: Tercio@23: label:ClearAllPoints() Tercio@23: image:ClearAllPoints() Tercio@23: Tercio@23: if self.imageshown then Tercio@23: local imagewidth = image:GetWidth() Tercio@23: if (width - imagewidth) < 200 or (label:GetText() or "") == "" then Tercio@23: -- image goes on top centered when less than 200 width for the text, or if there is no text Tercio@23: image:SetPoint("TOP") Tercio@23: label:SetPoint("TOP", image, "BOTTOM") Tercio@23: label:SetPoint("LEFT") Tercio@23: label:SetWidth(width) Tercio@23: height = image:GetHeight() + label:GetHeight() Tercio@23: else Tercio@23: -- image on the left Tercio@23: image:SetPoint("TOPLEFT") Tercio@23: if image:GetHeight() > label:GetHeight() then Tercio@23: label:SetPoint("LEFT", image, "RIGHT", 4, 0) Tercio@23: else Tercio@23: label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0) Tercio@23: end Tercio@23: label:SetWidth(width - imagewidth - 4) Tercio@23: height = max(image:GetHeight(), label:GetHeight()) Tercio@23: end Tercio@23: else Tercio@23: -- no image shown Tercio@23: label:SetPoint("TOPLEFT") Tercio@23: label:SetWidth(width) Tercio@23: height = label:GetHeight() Tercio@23: end Tercio@23: Tercio@23: self.resizing = true Tercio@23: frame:SetHeight(height) Tercio@23: frame.height = height Tercio@23: self.resizing = nil Tercio@23: end Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Methods Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local methods = { Tercio@23: ["OnAcquire"] = function(self) Tercio@23: -- set the flag to stop constant size updates Tercio@23: self.resizing = true Tercio@23: -- height is set dynamically by the text and image size Tercio@23: self:SetWidth(200) Tercio@23: self:SetText() Tercio@23: self:SetImage(nil) Tercio@23: self:SetImageSize(16, 16) Tercio@23: self:SetColor() Tercio@23: self:SetFontObject() Tercio@23: Tercio@23: -- reset the flag Tercio@23: self.resizing = nil Tercio@23: -- run the update explicitly Tercio@23: UpdateImageAnchor(self) Tercio@23: end, Tercio@23: Tercio@23: -- ["OnRelease"] = nil, Tercio@23: Tercio@23: ["OnWidthSet"] = function(self, width) Tercio@23: UpdateImageAnchor(self) Tercio@23: end, Tercio@23: Tercio@23: ["SetText"] = function(self, text) Tercio@23: self.label:SetText(text) Tercio@23: UpdateImageAnchor(self) Tercio@23: end, Tercio@23: Tercio@23: ["SetColor"] = function(self, r, g, b) Tercio@23: if not (r and g and b) then Tercio@23: r, g, b = 1, 1, 1 Tercio@23: end Tercio@23: self.label:SetVertexColor(r, g, b) Tercio@23: end, Tercio@23: Tercio@23: ["SetImage"] = function(self, path, ...) Tercio@23: local image = self.image Tercio@23: image:SetTexture(path) Tercio@23: Tercio@23: if image:GetTexture() then Tercio@23: self.imageshown = true Tercio@23: local n = select("#", ...) Tercio@23: if n == 4 or n == 8 then Tercio@23: image:SetTexCoord(...) Tercio@23: else Tercio@23: image:SetTexCoord(0, 1, 0, 1) Tercio@23: end Tercio@23: else Tercio@23: self.imageshown = nil Tercio@23: end Tercio@23: UpdateImageAnchor(self) Tercio@23: end, Tercio@23: Tercio@23: ["SetFont"] = function(self, font, height, flags) Tercio@23: self.label:SetFont(font, height, flags) Tercio@23: end, Tercio@23: Tercio@23: ["SetFontObject"] = function(self, font) Tercio@23: self:SetFont((font or GameFontHighlightSmall):GetFont()) Tercio@23: end, Tercio@23: Tercio@23: ["SetImageSize"] = function(self, width, height) Tercio@23: self.image:SetWidth(width) Tercio@23: self.image:SetHeight(height) Tercio@23: UpdateImageAnchor(self) Tercio@23: end, Tercio@23: } Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Constructor Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local function Constructor() Tercio@23: local frame = CreateFrame("Frame", nil, UIParent) Tercio@23: frame:Hide() Tercio@23: Tercio@23: local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall") Tercio@23: label:SetJustifyH("LEFT") Tercio@23: label:SetJustifyV("TOP") Tercio@23: Tercio@23: local image = frame:CreateTexture(nil, "BACKGROUND") Tercio@23: Tercio@23: -- create widget Tercio@23: local widget = { Tercio@23: label = label, Tercio@23: image = image, Tercio@23: frame = frame, Tercio@23: type = Type Tercio@23: } Tercio@23: for method, func in pairs(methods) do Tercio@23: widget[method] = func Tercio@23: end Tercio@23: Tercio@23: return AceGUI:RegisterAsWidget(widget) Tercio@23: end Tercio@23: Tercio@23: AceGUI:RegisterWidgetType(Type, Constructor, Version)