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