yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Label Widget yellowfive@57: Displays text. yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: local Type, Version = "AmrUiLabel", 1 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: local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot") 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: yellowfive@57: --[[----------------------------------------------------------------------------- yellowfive@57: Support functions yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: yellowfive@57: local function updateSize(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 label = self.label yellowfive@57: local height yellowfive@57: yellowfive@57: label:ClearAllPoints() yellowfive@57: label:SetPoint("TOPLEFT") yellowfive@57: label:SetWidth(width) yellowfive@57: height = label:GetHeight() 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: Scripts yellowfive@57: -------------------------------------------------------------------------------]] yellowfive@57: yellowfive@57: local function frameOnEnter(frame) yellowfive@57: frame.obj:Fire("OnEnter") yellowfive@57: end yellowfive@57: yellowfive@57: local function frameOnLeave(frame) yellowfive@57: frame.obj:Fire("OnLeave") yellowfive@57: end yellowfive@57: yellowfive@57: local function frameOnMouseDown(frame, ...) yellowfive@57: frame.obj:Fire("OnMouseDown", ...) yellowfive@57: end yellowfive@57: yellowfive@57: local function frameOnMouseUp(frame, ...) yellowfive@57: frame.obj:Fire("OnMouseUp", ...) 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 size yellowfive@57: self:SetWidth(200) yellowfive@57: self:SetText() yellowfive@57: self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text)) yellowfive@57: self:SetJustifyH("LEFT") yellowfive@57: self:SetJustifyV("MIDDLE") yellowfive@57: self:SetWordWrap(true) yellowfive@57: self:SetVisible(true) yellowfive@57: yellowfive@57: -- reset the flag yellowfive@57: self.resizing = nil yellowfive@57: -- run the update explicitly yellowfive@57: updateSize(self) yellowfive@57: end, yellowfive@57: yellowfive@57: -- ["OnRelease"] = nil, yellowfive@57: yellowfive@57: ["OnWidthSet"] = function(self, width) yellowfive@57: updateSize(self) yellowfive@57: end, yellowfive@57: yellowfive@57: ["GetHeight"] = function(self) yellowfive@57: return self.frame:GetHeight() yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetText"] = function(self, text) yellowfive@57: self.label:SetText(text) yellowfive@57: updateSize(self) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetFont"] = function(self, font) yellowfive@57: self.label:SetFontObject(font) yellowfive@57: updateSize(self) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetJustifyV"] = function(self, val) yellowfive@57: self.label:SetJustifyV(val) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetJustifyH"] = function(self, val) yellowfive@57: self.label:SetJustifyH(val) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetWordWrap"] = function(self, enable) yellowfive@57: self.label:SetWordWrap(enable) yellowfive@57: updateSize(self) yellowfive@57: end, yellowfive@57: yellowfive@57: ["SetVisible"] = function(self, visible) yellowfive@57: if visible then yellowfive@57: self.frame:Show() yellowfive@57: else yellowfive@57: self.frame:Hide() yellowfive@57: end yellowfive@57: 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: frame:SetScript("OnEnter", frameOnEnter) yellowfive@57: frame:SetScript("OnLeave", frameOnLeave) yellowfive@57: frame:SetScript("OnMouseDown", frameOnMouseDown) yellowfive@57: frame:SetScript("OnMouseUp", frameOnMouseUp) yellowfive@57: yellowfive@57: local label = frame:CreateFontString(nil, "ARTWORK") yellowfive@57: label:SetFontObject(Amr.CreateFont("Regular", 16, Amr.Colors.Text)) yellowfive@57: yellowfive@57: -- create widget yellowfive@57: local widget = { yellowfive@57: label = label, 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)