annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 17:3000eccbf1a0 v7.3.0.017

- ToC Update.
author Tercio
date Sat, 02 Sep 2017 14:10:48 -0300
parents fc346da3afd9
children
rev   line source
tercio@0 1 --[[-----------------------------------------------------------------------------
tercio@0 2 Label Widget
tercio@0 3 Displays text and optionally an icon.
tercio@0 4 -------------------------------------------------------------------------------]]
Tercio@17 5 local Type, Version = "Label", 24
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@17 81 self:SetJustifyH("LEFT")
Tercio@17 82 self:SetJustifyV("TOP")
tercio@0 83
tercio@0 84 -- reset the flag
tercio@0 85 self.resizing = nil
tercio@0 86 -- run the update explicitly
tercio@0 87 UpdateImageAnchor(self)
tercio@0 88 end,
tercio@0 89
tercio@0 90 -- ["OnRelease"] = nil,
tercio@0 91
tercio@0 92 ["OnWidthSet"] = function(self, width)
tercio@0 93 UpdateImageAnchor(self)
tercio@0 94 end,
tercio@0 95
tercio@0 96 ["SetText"] = function(self, text)
tercio@0 97 self.label:SetText(text)
tercio@0 98 UpdateImageAnchor(self)
tercio@0 99 end,
tercio@0 100
tercio@0 101 ["SetColor"] = function(self, r, g, b)
tercio@0 102 if not (r and g and b) then
tercio@0 103 r, g, b = 1, 1, 1
tercio@0 104 end
tercio@0 105 self.label:SetVertexColor(r, g, b)
tercio@0 106 end,
tercio@0 107
tercio@0 108 ["SetImage"] = function(self, path, ...)
tercio@0 109 local image = self.image
tercio@0 110 image:SetTexture(path)
tercio@0 111
tercio@0 112 if image:GetTexture() then
tercio@0 113 self.imageshown = true
tercio@0 114 local n = select("#", ...)
tercio@0 115 if n == 4 or n == 8 then
tercio@0 116 image:SetTexCoord(...)
tercio@0 117 else
tercio@0 118 image:SetTexCoord(0, 1, 0, 1)
tercio@0 119 end
tercio@0 120 else
tercio@0 121 self.imageshown = nil
tercio@0 122 end
tercio@0 123 UpdateImageAnchor(self)
tercio@0 124 end,
tercio@0 125
tercio@0 126 ["SetFont"] = function(self, font, height, flags)
tercio@0 127 self.label:SetFont(font, height, flags)
tercio@0 128 end,
tercio@0 129
tercio@0 130 ["SetFontObject"] = function(self, font)
tercio@0 131 self:SetFont((font or GameFontHighlightSmall):GetFont())
tercio@0 132 end,
tercio@0 133
tercio@0 134 ["SetImageSize"] = function(self, width, height)
tercio@0 135 self.image:SetWidth(width)
tercio@0 136 self.image:SetHeight(height)
tercio@0 137 UpdateImageAnchor(self)
tercio@0 138 end,
Tercio@17 139
Tercio@17 140 ["SetJustifyH"] = function(self, justifyH)
Tercio@17 141 self.label:SetJustifyH(justifyH)
Tercio@17 142 end,
Tercio@17 143
Tercio@17 144 ["SetJustifyV"] = function(self, justifyV)
Tercio@17 145 self.label:SetJustifyV(justifyV)
Tercio@17 146 end,
tercio@0 147 }
tercio@0 148
tercio@0 149 --[[-----------------------------------------------------------------------------
tercio@0 150 Constructor
tercio@0 151 -------------------------------------------------------------------------------]]
tercio@0 152 local function Constructor()
tercio@0 153 local frame = CreateFrame("Frame", nil, UIParent)
tercio@0 154 frame:Hide()
tercio@0 155
tercio@0 156 local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
tercio@0 157 local image = frame:CreateTexture(nil, "BACKGROUND")
tercio@0 158
tercio@0 159 -- create widget
tercio@0 160 local widget = {
tercio@0 161 label = label,
tercio@0 162 image = image,
tercio@0 163 frame = frame,
tercio@0 164 type = Type
tercio@0 165 }
tercio@0 166 for method, func in pairs(methods) do
tercio@0 167 widget[method] = func
tercio@0 168 end
tercio@0 169
tercio@0 170 return AceGUI:RegisterAsWidget(widget)
tercio@0 171 end
tercio@0 172
tercio@0 173 AceGUI:RegisterWidgetType(Type, Constructor, Version)