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