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