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