annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 51:dbf04157d63e v7.3.0.051

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