annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Label.lua @ 23:52973d00a183

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