File size: 3,287 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
DefineClass.XLabel = {
	__parents = { "XTranslateText" },
	
	HandleMouse = false,
	Padding = box(2, 0, 2, 0),
	ShadowColor = RGBA(0, 0, 0, 48),
	ShadowSize = 1,
	
	highlighted_text = false,
	ignore_case = false,
}

local UIL = UIL
function XLabel:Measure(preferred_width, preferred_height)
	local width, height = UIL.MeasureText(self.text, self:GetFontId())
	return width, Max(height, self.font_height)
end

local function find_next(str_lower, str, substr, start_pos)
	local idx = string.find(str_lower, substr, start_pos, true)
	if idx then
		return str:sub(start_pos, idx - 1), str:sub(idx, idx + #substr - 1)
	else
		return str:sub(start_pos)
	end
end

local one = point(1, 1)
local StretchText = UIL.StretchText
local StretchTextShadow = UIL.StretchTextShadow
local StretchTextOutline = UIL.StretchTextOutline
local MeasureToCharStart = UIL.MeasureToCharStart
function XLabel:DrawContent()
	local text = self.text
	if text == "" then return end
	local shadow_type = self.ShadowType
	local shadow_size = self.ShadowSize
	local shadow_color = self:GetEnabled() and self.ShadowColor or self.DisabledShadowColor
	local cbox = self.content_box
	local font = self:GetFontId()
	if GetAlpha(shadow_color) == 0 or shadow_size == 0 then -- no shadow
		StretchText(text, cbox, font, self:CalcTextColor(), 0)
	elseif shadow_type == "shadow" then
		StretchTextShadow(text, cbox, font, self:CalcTextColor(), shadow_color, shadow_size, one, 0)
	elseif shadow_type == "extrude" then
		StretchTextShadow(text, cbox, font, self:CalcTextColor(), shadow_color, shadow_size, one, 0, true)
	elseif shadow_type == "outline" then
		StretchTextOutline(text, cbox, font, self:CalcTextColor(), shadow_color, shadow_size, 0)
	end
	
	if self.highlighted_text then
		local lower_text = self.ignore_case and text:lower() or text
		local other, word = find_next(lower_text, text, self.highlighted_text, 1)
		if word then
			local len_word = utf8.len(word)
			local len_other = utf8.len(other)
			local x1 = MeasureToCharStart(text, font, len_other + 1)
			local x2 = MeasureToCharStart(text, font, len_word + len_other + 1)
			local x, y1, _, y2 = cbox:xyxy()
			StretchText(word, box(x + x1, y1, x + x2, y2), font, self.highlight_color)
		end
	end
end

function XLabel:HighlightText(text, color, ignore_case)
	if self.highlighted_text == text and self.highlight_color == color and self.ignore_case == ignore_case then return end
	self.highlighted_text = text
	self.highlight_color = color
	self.ignore_case = ignore_case
	self:Invalidate()
end

DefineClass.XEmbedLabel = {
	__parents = { "XTranslateText" },
	properties = {
		{ category = "Visual", id = "UseXTextControl", editor = "bool", default = false, },
	},
}

function XEmbedLabel:Init(parent, context)
	self:SetUseXTextControl(self.UseXTextControl, context)
	self:SetTranslate(self.Translate)
	self:SetText(self.Text)
end

function XEmbedLabel:SetUseXTextControl(value, context)
	local class = value and "XText" or "XLabel"
	local label = rawget(self, "idLabel")
	if label then
		context = label.context
		label:delete()
	end
	label = g_Classes[class]:new({
		Id = "idLabel",
		VAlign = "center",
		Translate = self.Translate
	}, self, context)
	label:SetFontProps(self)
	self.UseXTextControl = value
end

LinkTextPropertiesToChild(XEmbedLabel, "idLabel")