annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 11:371e14cd2feb

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