BBCode Style DLabel Coloring

I have seen this mentioned a few times. How hard is it to implement? I only need to change the color of two sets of words in a DLabel. I looked at the TextBoxPlus that someone released but I have no idea what is going on in there.

I know it works like this:



[c=255,255,255]Text[c=255,255,255]Text2


But I don’t know how to convert it to colors instead of text.

Why don’t you just

[lua]
Color( r, g, b, a )
[/lua]

Wouldn’t work, I am trying to change different words to different colors with the DLabels SetText.

Basically I want to have one DLabel and I want to be able to change certain words colors.

[lua]function ProcessChat( txt, chattable )
local colormatch = “%[c=(%d+),(%d+),(%d+)%]”
local startpos, endpos, r, g, b = string.find( txt, colormatch )

if startpos and endpos and r and g and b then
	startpos = startpos - 1
	endpos = endpos + 1
	local starttxt = string.sub( txt, 1, startpos )
	local color = Color( r, g, b )
	local endtxt = string.sub( txt, endpos )
	local more = string.find( endtxt, colormatch )
	
	if starttxt != "" then
		table.insert( chattable, starttxt )
	end
	
	table.insert( chattable, color )

	if more then
		return ProcessChat( endtxt, chattable )
	elseif endtxt != "" then
		table.insert( chattable, endtxt )
	end
	
	return chattable
else
	table.insert( chattable, txt )
	return chattable
end

end[/lua]

How my chatbox does it. Probably not the best way to do it, but it works. :v:

Pretty much you process the text, it returns a table that you can then use to create multiple DLabels in a line or whatever.

Thanks I didn’t release you used multiple DLabels.

You could use markup:

[lua]
function BBtoMarkup(str)
str = str:gsub("[c=","<color=") – not sure if you have to escape %[ here…
str = str:gsub("]","</color>")
return markup.Parse(str)
end

local bb = BBtoMarkup("[c=255,255,255]Text[c=255,255,255]Text2")

label = vgui.Create(“DLabel”)
label:SetText("")
local ldraw = label.Draw
label.Draw = function(self)
ldraw(self)
bb:Draw(0,0)
end
label:SizeToContent()[/lua]

Untested but should work.

You are super awesome! This fixes so many problems! I can’t wait to explore this.

Took 14 lines of code ( 2 Label ) down to 2 lines of code!

Edit:
How do I use the
, I can’t find the right place to put it.

I don’t think that works. The way you’re replacing [c=x,x,x] would result in “<color=255,255,255</color>Text<color=255,255,255</color>Text2”

For the proper result, do this:
[lua]
str = str:gsub( “%[c%s*=(%s*%d+,%s*%d+,%s*%d+)%]”, “<color=%1>” ):gsub( “%[/s%]”, “</color>” )[/lua]

Whoops yeah, haven’t had time to test (and find the error) at work :()

It is fine, I ended up crunching all your code into two lines and it works perfectly :smiley:

One question though, could you provide a rough example the the
operator? I have tried it in multiple places in the parse string but it seems to have no effect.

I’m not too sure, bit I guess you’ll have to size the DLabel if you’r going to make newlines (it’ll cut off otherwhise)

This is actually pretty cool. Never thought about using the markup object. I’m trying to convert my system into it.

One thing I noticed is that if you don’t supply an alpha value, it defaults to 0…

We should create our own lib. The default markup is pretty slow :frowning:

It is really simple, you don’t even really need a DLabel from what I can see. I just put the draw function in a DPanels paint.

Yep, just converted my chatbox to use it. It’s nice because it can automatically parse new lines for you!

[lua]function BAdmin.BBtoMarkup( str, maxwide )
str = str:gsub( “%[c%s*=(%s*%d+,%s*%d+,%s*%d+)%]”, “<color=%1>” ):gsub( “%[/s%]”, “</color>” ):gsub( “<font%s*=(%s*%a+)>”, “” ) --Convert BB to markup, and don’t allow font changes
return markup.Parse( “<font=BCText>”…str…"</font>", maxwide ), str
end

local ColoredText, nicetext = BAdmin.BBtoMarkup( text, ScrH() / 2 )

local label = vgui.Create(“DPanel”,row)
label.Paint = function( self )
ColoredText:Draw( 0, 0, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 255 )
end
label:SetSize( ColoredText:GetWidth(), ColoredText:GetHeight() )

[/lua]