annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 8:deb31e7b9ae4

Tagging as beta
author Xiiph
date Fri, 18 Feb 2011 23:58:47 +0100
parents 98c6f55e6619
children
rev   line source
Xiiph@0 1 --[[-----------------------------------------------------------------------------
Xiiph@0 2 Label Widget
Xiiph@0 3 Displays text and optionally an icon.
Xiiph@0 4 -------------------------------------------------------------------------------]]
Xiiph@0 5 local Type, Version = "Label", 21
Xiiph@0 6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
Xiiph@0 7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
Xiiph@0 8
Xiiph@0 9 -- Lua APIs
Xiiph@0 10 local max, select, pairs = math.max, select, pairs
Xiiph@0 11
Xiiph@0 12 -- WoW APIs
Xiiph@0 13 local CreateFrame, UIParent = CreateFrame, UIParent
Xiiph@0 14
Xiiph@0 15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
Xiiph@0 16 -- List them here for Mikk's FindGlobals script
Xiiph@0 17 -- GLOBALS: GameFontHighlightSmall
Xiiph@0 18
Xiiph@0 19 --[[-----------------------------------------------------------------------------
Xiiph@0 20 Support functions
Xiiph@0 21 -------------------------------------------------------------------------------]]
Xiiph@0 22
Xiiph@0 23 local function UpdateImageAnchor(self)
Xiiph@0 24 if self.resizing then return end
Xiiph@0 25 local frame = self.frame
Xiiph@0 26 local width = frame.width or frame:GetWidth() or 0
Xiiph@0 27 local image = self.image
Xiiph@0 28 local label = self.label
Xiiph@0 29 local height
Xiiph@0 30
Xiiph@0 31 label:ClearAllPoints()
Xiiph@0 32 image:ClearAllPoints()
Xiiph@0 33
Xiiph@0 34 if self.imageshown then
Xiiph@0 35 local imagewidth = image:GetWidth()
Xiiph@0 36 if (width - imagewidth) < 200 or (label:GetText() or "") == "" then
Xiiph@0 37 -- image goes on top centered when less than 200 width for the text, or if there is no text
Xiiph@0 38 image:SetPoint("TOP")
Xiiph@0 39 label:SetPoint("TOP", image, "BOTTOM")
Xiiph@0 40 label:SetPoint("LEFT")
Xiiph@0 41 label:SetWidth(width)
Xiiph@0 42 height = image:GetHeight() + label:GetHeight()
Xiiph@0 43 else
Xiiph@0 44 -- image on the left
Xiiph@0 45 image:SetPoint("TOPLEFT")
Xiiph@0 46 label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
Xiiph@0 47 label:SetWidth(width - imagewidth - 4)
Xiiph@0 48 height = max(image:GetHeight(), label:GetHeight())
Xiiph@0 49 end
Xiiph@0 50 else
Xiiph@0 51 -- no image shown
Xiiph@0 52 label:SetPoint("TOPLEFT")
Xiiph@0 53 label:SetWidth(width)
Xiiph@0 54 height = label:GetHeight()
Xiiph@0 55 end
Xiiph@0 56
Xiiph@0 57 self.resizing = true
Xiiph@0 58 frame:SetHeight(height)
Xiiph@0 59 frame.height = height
Xiiph@0 60 self.resizing = nil
Xiiph@0 61 end
Xiiph@0 62
Xiiph@0 63 --[[-----------------------------------------------------------------------------
Xiiph@0 64 Methods
Xiiph@0 65 -------------------------------------------------------------------------------]]
Xiiph@0 66 local methods = {
Xiiph@0 67 ["OnAcquire"] = function(self)
Xiiph@0 68 -- set the flag to stop constant size updates
Xiiph@0 69 self.resizing = true
Xiiph@0 70 -- height is set dynamically by the text and image size
Xiiph@0 71 self:SetWidth(200)
Xiiph@0 72 self:SetText()
Xiiph@0 73 self:SetImage(nil)
Xiiph@0 74 self:SetImageSize(16, 16)
Xiiph@0 75 self:SetColor()
Xiiph@0 76 self:SetFontObject()
Xiiph@0 77
Xiiph@0 78 -- reset the flag
Xiiph@0 79 self.resizing = nil
Xiiph@0 80 -- run the update explicitly
Xiiph@0 81 UpdateImageAnchor(self)
Xiiph@0 82 end,
Xiiph@0 83
Xiiph@0 84 -- ["OnRelease"] = nil,
Xiiph@0 85
Xiiph@0 86 ["OnWidthSet"] = function(self, width)
Xiiph@0 87 UpdateImageAnchor(self)
Xiiph@0 88 end,
Xiiph@0 89
Xiiph@0 90 ["SetText"] = function(self, text)
Xiiph@0 91 self.label:SetText(text)
Xiiph@0 92 UpdateImageAnchor(self)
Xiiph@0 93 end,
Xiiph@0 94
Xiiph@0 95 ["SetColor"] = function(self, r, g, b)
Xiiph@0 96 if not (r and g and b) then
Xiiph@0 97 r, g, b = 1, 1, 1
Xiiph@0 98 end
Xiiph@0 99 self.label:SetVertexColor(r, g, b)
Xiiph@0 100 end,
Xiiph@0 101
Xiiph@0 102 ["SetImage"] = function(self, path, ...)
Xiiph@0 103 local image = self.image
Xiiph@0 104 image:SetTexture(path)
Xiiph@0 105
Xiiph@0 106 if image:GetTexture() then
Xiiph@0 107 self.imageshown = true
Xiiph@0 108 local n = select("#", ...)
Xiiph@0 109 if n == 4 or n == 8 then
Xiiph@0 110 image:SetTexCoord(...)
Xiiph@0 111 else
Xiiph@0 112 image:SetTexCoord(0, 1, 0, 1)
Xiiph@0 113 end
Xiiph@0 114 else
Xiiph@0 115 self.imageshown = nil
Xiiph@0 116 end
Xiiph@0 117 UpdateImageAnchor(self)
Xiiph@0 118 end,
Xiiph@0 119
Xiiph@0 120 ["SetFont"] = function(self, font, height, flags)
Xiiph@0 121 self.label:SetFont(font, height, flags)
Xiiph@0 122 end,
Xiiph@0 123
Xiiph@0 124 ["SetFontObject"] = function(self, font)
Xiiph@0 125 self:SetFont((font or GameFontHighlightSmall):GetFont())
Xiiph@0 126 end,
Xiiph@0 127
Xiiph@0 128 ["SetImageSize"] = function(self, width, height)
Xiiph@0 129 self.image:SetWidth(width)
Xiiph@0 130 self.image:SetHeight(height)
Xiiph@0 131 UpdateImageAnchor(self)
Xiiph@0 132 end,
Xiiph@0 133 }
Xiiph@0 134
Xiiph@0 135 --[[-----------------------------------------------------------------------------
Xiiph@0 136 Constructor
Xiiph@0 137 -------------------------------------------------------------------------------]]
Xiiph@0 138 local function Constructor()
Xiiph@0 139 local frame = CreateFrame("Frame", nil, UIParent)
Xiiph@0 140 frame:Hide()
Xiiph@0 141
Xiiph@0 142 local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
Xiiph@0 143 label:SetJustifyH("LEFT")
Xiiph@0 144 label:SetJustifyV("TOP")
Xiiph@0 145
Xiiph@0 146 local image = frame:CreateTexture(nil, "BACKGROUND")
Xiiph@0 147
Xiiph@0 148 -- create widget
Xiiph@0 149 local widget = {
Xiiph@0 150 label = label,
Xiiph@0 151 image = image,
Xiiph@0 152 frame = frame,
Xiiph@0 153 type = Type
Xiiph@0 154 }
Xiiph@0 155 for method, func in pairs(methods) do
Xiiph@0 156 widget[method] = func
Xiiph@0 157 end
Xiiph@0 158
Xiiph@0 159 return AceGUI:RegisterAsWidget(widget)
Xiiph@0 160 end
Xiiph@0 161
Xiiph@0 162 AceGUI:RegisterWidgetType(Type, Constructor, Version)