AceGUIWidget-Label.lua 4.14 KB
Newer Older
1 2 3 4
--[[-----------------------------------------------------------------------------
Label Widget
Displays text and optionally an icon.
-------------------------------------------------------------------------------]]
5 6 7
local Type, Version = "Label", 20
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
8

9 10 11 12 13 14 15 16 17 18
-- Lua APIs
local max, select = math.max, select

-- WoW APIs
local CreateFrame, UIParent = CreateFrame, UIParent

-- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
-- List them here for Mikk's FindGlobals script
-- GLOBALS: GameFontHighlightSmall

19
--[[-----------------------------------------------------------------------------
20
Support functions
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
-------------------------------------------------------------------------------]]

local function UpdateImageAnchor(self)
	local frame = self.frame
	local width = frame.width or frame:GetWidth() or 0
	local image = self.image
	local label = self.label
	local height

	label:ClearAllPoints()
	image:ClearAllPoints()

	if self.imageshown then
		local imagewidth = image:GetWidth()
		if (width - imagewidth) < 200 or (label:GetText() or "") == "" then
			-- image goes on top centered when less than 200 width for the text, or if there is no text
37 38 39 40
			image:SetPoint("TOP")
			label:SetPoint("TOP", image, "BOTTOM")
			label:SetPoint("LEFT")
			label:SetPoint("RIGHT")
41 42 43
			height = image:GetHeight() + label:GetHeight()
		else
			-- image on the left
44
			image:SetPoint("TOPLEFT")
45
			label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
46
			label:SetPoint("TOPRIGHT")
47 48 49 50
			height = max(image:GetHeight(), label:GetHeight())
		end
	else
		-- no image shown
51 52
		label:SetPoint("TOPLEFT")
		label:SetPoint("TOPRIGHT")
53 54
		height = label:GetHeight()
	end
55
	
56 57 58 59 60 61 62 63 64 65 66
	self.resizing = true
	frame:SetHeight(height)
	frame.height = height
	self.resizing = nil
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
nevcairiel's avatar
nevcairiel committed
67 68
		self:SetHeight(18)
		self:SetWidth(200)
69 70
		self:SetText("")
		self:SetImage(nil)
71
		self:SetImageSize(16, 16)
72
		self:SetColor()
73 74 75 76 77
		self.label:SetFontObject(nil)
		self.label:SetFont(GameFontHighlightSmall:GetFont())
	end,

	["OnRelease"] = function(self)
78 79
		self.frame:ClearAllPoints()
		self.frame:Hide()
80 81 82
	end,

	["SetText"] = function(self, text)
83 84
		self.label:SetText(text or "")
		UpdateImageAnchor(self)
85 86 87
	end,

	["SetColor"] = function(self, r, g, b)
88 89 90 91
		if not (r and g and b) then
			r, g, b = 1, 1, 1
		end
		self.label:SetVertexColor(r, g, b)
92 93 94
	end,

	["OnWidthSet"] = function(self, width)
95 96
		if self.resizing then return end
		UpdateImageAnchor(self)
97 98 99
	end,

	["SetImage"] = function(self, path, ...)
100 101 102 103 104
		local image = self.image
		image:SetTexture(path)
		
		if image:GetTexture() then
			self.imageshown = true
105
			local n = select("#", ...)
106 107
			if n == 4 or n == 8 then
				image:SetTexCoord(...)
108 109
			else
				image:SetTexCoord(0, 1, 0, 1)
110 111 112 113 114
			end
		else
			self.imageshown = nil
		end
		UpdateImageAnchor(self)
115 116 117
	end,

	["SetFont"] = function(self, font, height, flags)
118
		self.label:SetFont(font, height, flags)
119 120 121
	end,

	["SetFontObject"] = function(self, font)
122
		self.label:SetFontObject(font or GameFontHighlightSmall)
123 124 125
	end,

	["SetImageSize"] = function(self, width, height)
126 127 128
		self.image:SetWidth(width)
		self.image:SetHeight(height)
		UpdateImageAnchor(self)
129 130
	end,
}
131 132

--[[-----------------------------------------------------------------------------
133 134 135 136 137
Constructor
-------------------------------------------------------------------------------]]
local function Constructor()
	local frame = CreateFrame("Frame", nil, UIParent)
	frame:Hide()
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
	label:SetJustifyH("LEFT")
	label:SetJustifyV("TOP")

	local image = frame:CreateTexture(nil, "BACKGROUND")

	-- create widget
	local widget = {
		label = label,
		image = image,
		frame = frame,
		type  = Type
	}
	for method, func in pairs(methods) do
		widget[method] = func
154
	end
155

156
	return AceGUI:RegisterAsWidget(widget)
157 158
end

159
AceGUI:RegisterWidgetType(Type, Constructor, Version)