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

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

-- 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
-------------------------------------------------------------------------------]]

local function UpdateImageAnchor(self)
nevcairiel's avatar
nevcairiel committed
24
	if self.resizing then return end
25 26 27 28 29 30 31 32 33 34 35 36 37
	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
38 39 40
			image:SetPoint("TOP")
			label:SetPoint("TOP", image, "BOTTOM")
			label:SetPoint("LEFT")
41
			label:SetWidth(width)
42
			height = image:GetHeight() + label:GetStringHeight()
43 44
		else
			-- image on the left
45
			image:SetPoint("TOPLEFT")
46
			if image:GetHeight() > label:GetStringHeight() then
47 48 49 50
				label:SetPoint("LEFT", image, "RIGHT", 4, 0)
			else
				label:SetPoint("TOPLEFT", image, "TOPRIGHT", 4, 0)
			end
51
			label:SetWidth(width - imagewidth - 4)
52
			height = max(image:GetHeight(), label:GetStringHeight())
53 54 55
		end
	else
		-- no image shown
56
		label:SetPoint("TOPLEFT")
57
		label:SetWidth(width)
58 59 60 61 62 63 64 65 66 67

		-- get the string height, of a space character if we need to
		local text = label:GetText()
		if not text or text == "" then
			label:SetText(" ")
			height = label:GetStringHeight()
			label:SetText(text)
		else
			height = label:GetStringHeight()
		end
68
	end
69
	
70 71 72 73 74 75 76 77 78 79 80
	self.resizing = true
	frame:SetHeight(height)
	frame.height = height
	self.resizing = nil
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
nevcairiel's avatar
nevcairiel committed
81 82 83
		-- set the flag to stop constant size updates
		self.resizing = true
		-- height is set dynamically by the text and image size
nevcairiel's avatar
nevcairiel committed
84
		self:SetWidth(200)
nevcairiel's avatar
nevcairiel committed
85
		self:SetText()
86
		self:SetImage(nil)
87
		self:SetImageSize(16, 16)
88
		self:SetColor()
nevcairiel's avatar
nevcairiel committed
89
		self:SetFontObject()
90 91
		self:SetJustifyH("LEFT")
		self:SetJustifyV("TOP")
nevcairiel's avatar
nevcairiel committed
92 93 94 95 96

		-- reset the flag
		self.resizing = nil
		-- run the update explicitly
		UpdateImageAnchor(self)
97 98
	end,

99
	-- ["OnRelease"] = nil,
100

101 102 103 104
	["OnWidthSet"] = function(self, width)
		UpdateImageAnchor(self)
	end,

105
	["SetText"] = function(self, text)
nevcairiel's avatar
nevcairiel committed
106
		self.label:SetText(text)
107
		UpdateImageAnchor(self)
108 109 110
	end,

	["SetColor"] = function(self, r, g, b)
111 112 113 114
		if not (r and g and b) then
			r, g, b = 1, 1, 1
		end
		self.label:SetVertexColor(r, g, b)
115 116 117
	end,

	["SetImage"] = function(self, path, ...)
118 119 120 121 122
		local image = self.image
		image:SetTexture(path)
		
		if image:GetTexture() then
			self.imageshown = true
123
			local n = select("#", ...)
124 125
			if n == 4 or n == 8 then
				image:SetTexCoord(...)
126 127
			else
				image:SetTexCoord(0, 1, 0, 1)
128 129 130 131 132
			end
		else
			self.imageshown = nil
		end
		UpdateImageAnchor(self)
133 134 135
	end,

	["SetFont"] = function(self, font, height, flags)
136
		self.label:SetFont(font, height, flags)
137 138 139
	end,

	["SetFontObject"] = function(self, font)
nevcairiel's avatar
nevcairiel committed
140
		self:SetFont((font or GameFontHighlightSmall):GetFont())
141 142 143
	end,

	["SetImageSize"] = function(self, width, height)
144 145 146
		self.image:SetWidth(width)
		self.image:SetHeight(height)
		UpdateImageAnchor(self)
147
	end,
148 149 150 151 152 153 154 155

	["SetJustifyH"] = function(self, justifyH)
		self.label:SetJustifyH(justifyH)
	end,

	["SetJustifyV"] = function(self, justifyV)
		self.label:SetJustifyV(justifyV)
	end,
156
}
157 158

--[[-----------------------------------------------------------------------------
159 160 161 162 163
Constructor
-------------------------------------------------------------------------------]]
local function Constructor()
	local frame = CreateFrame("Frame", nil, UIParent)
	frame:Hide()
164

165 166 167 168 169 170 171 172 173 174 175 176
	local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightSmall")
	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
177
	end
178

179
	return AceGUI:RegisterAsWidget(widget)
180 181
end

182
AceGUI:RegisterWidgetType(Type, Constructor, Version)