annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 78:4b1375f5c4f2

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