Rotated Text on panels

[lua]
local mat = Matrix();
local matScale = Vector(1, 1, 0);
local matAng = Angle(0,0,0);
local vec = Vector();
mat:Scale(matScale);
function dbg.DrawRotatedText(txt, posX, posY, ang)
surface.SetTextPos(0, 0);
matAng.y = ang;
vec.x = posX;
vec.y = posY;
mat:SetAngles(matAng);
mat:SetTranslation(vec);
cam.PushModelMatrix(mat);
surface.DrawText(txt);
cam.PopModelMatrix();
end
[/lua]

I found Wizard of Ass’s code on a thread from forever ago and it appears to work when not in a panel, but i need it to work in a panel… anyone have any ideas?

[editline]7th July 2014[/editline]

I think this may be a bug, can anyone confirm?

[editline]7th July 2014[/editline]

Here’s a picture,

http://meep.gentoocorp.org/fp/drt.gif

Try swapping


vec.x = posX;
vec.y = posY;

for


vec.x = posY;
vec.y = posX;

Your gif looks to me like it’s swapped X and Y. Most likely due to the fact that the text is rotated, and it’s X is Y (and visa versa).

I thought exactly that, so I tried it before and it didn’t help anything.

http://meep.gentoocorp.org/fp/drt2.gif

Could you provide a gif of you just moving it on set axis? (as in, move it horizontally, stop, move it vertically, stop), as currently moving it in circles makes it difficult to see what’s going on.

Can you make a gif of the OP code moving only left/right then only up/down, I can’t tell exactly what’s going on when you move in circles

edit: poster above had the same thoughts but faster :stuck_out_tongue:

sure,

http://meep.gentoocorp.org/fp/drt3.gif

Can you also post a gif doing the same thing with my suggestion? (x = y, y = x)

http://meep.gentoocorp.org/fp/drt4.gif

It’s kind of hard since it is right at the edge of the screen, but I tried my best.

Rotated text did not show up for me when I drew it in a DPanel,
I drew both RotatedText, and draw.DrawText and set them both to the same posistions, and only 1 showed up.
It did work when hooking it in HUDPaint.
here’s a screenshot.

http://puu.sh/a1hMR/3700a658bd.jpg

This looks to be doing exactly as the previous gif you posted (non flipped X and Y). Weird.

Yeah, i know.

Have you tried the function posted on the wiki to draw rotated text?


function draw.TextRotated( text, x, y, color, font, ang )
	render.PushFilterMag( TEXFILTER.ANISOTROPIC )
	render.PushFilterMin( TEXFILTER.ANISOTROPIC )
	surface.SetFont( font )
	surface.SetTextColor( color )
	surface.SetTextPos( 0, 0 )
	local textWidth, textHeight = surface.GetTextSize( text )
	local rad = -math.rad( ang )
	local halvedPi = math.pi / 2
	x = x - ( math.sin( rad + halvedPi ) * textWidth / 2 + math.sin( rad ) * textHeight / 2 )
	y = y - ( math.cos( rad + halvedPi ) * textWidth / 2 + math.cos( rad ) * textHeight / 2 )
	local m = Matrix()
	m:SetAngles( Angle( 0, ang, 0 ) )
	m:SetTranslation( Vector( x, y, 0 ) )
	cam.PushModelMatrix( m )
		surface.DrawText( text )
	cam.PopModelMatrix()
	render.PopFilterMag()
	render.PopFilterMin()
end

It’s the exact same code, just implemented in lua (and it still does the same thing)

The important bit seems to be


x = x - ( math.sin( rad + halvedPi ) * textWidth / 2 + math.sin( rad ) * textHeight / 2 )
	y = y - ( math.cos( rad + halvedPi ) * textWidth / 2 + math.cos( rad ) * textHeight / 2 )

here, which yours doesn’t have.

EDIT: Also, are you sure you’re actually passing your function valid X/Y coords? Try a surface.DrawRect with your X and Y to make sure it doesn’t do the same thing.

[lua]

local mat = Matrix();
local matScale = Vector(1, 1, 0);
local matAng = Angle(0,0,0);
local vec = Vector();
mat:Scale(matScale);
local function other(text, x, y, ang)
	surface.SetTextPos(0, 0)
	local textWidth, textHeight = surface.GetTextSize(text)
	local rad = -math.rad(ang)
	local halvedPi = math.pi / 2
	x = x - (math.sin(rad + halvedPi) * textWidth / 2 + math.sin(rad) * textHeight / 2)
	y = y - (math.cos(rad + halvedPi) * textWidth / 2 + math.cos(rad) * textHeight / 2)
	matAng.y = ang;
	mat:SetAngles(matAng)
	vec.x = x;
	vec.y = y;
	mat:SetTranslation(vec)
	cam.PushModelMatrix(mat)
		surface.DrawText(text)
	cam.PopModelMatrix()
end
function dbg.DrawRotatedText(txt, posX, posY, ang)
	if(txt == "OnGamemodeLoaded") then
		surface.SetTextPos(posX - 100, posY); -- due to edge
		surface.DrawText(txt);
		surface.SetTextPos(0, 0);
		matAng.y = ang;
		vec.x = posY;
		vec.y = posX;
		mat:SetAngles(matAng);
		mat:SetTranslation(vec);
		cam.PushModelMatrix(mat);
			surface.DrawText("11"..txt);
		cam.PopModelMatrix();
		other("22"..txt, posX, posY, ang);
	end
end

[/lua]

http://meep.gentoocorp.org/fp/drt6.gif

What happens if you chuck a surface.DrawRect(x, y, 100, 100) after cam.PopModelMatrix() in your DrawRotatedText?

http://puu.sh/a1jNx/5af12eaf22.png

[lua]

		cam.PushModelMatrix(mat);
			surface.DrawText("11"..txt);
			surface.SetDrawColor(Color(0,0,0,255));
		cam.PopModelMatrix();
		surface.DrawRect(posX - 100, posY, 100, 100);

[/lua]

I assume this stays there when moving too?

Yes, that’s why i made it static.

I got this code working, adapted from something on the wiki;


local function DrawRotatedText(text, x, y, angle)
	local matrix = Matrix()
	local position = Vector(x, y)
	
	matrix:Translate(position)
	matrix:Rotate(Angle(0, angle, 0) )
	matrix:Translate(-position)
	
	cam.PushModelMatrix(matrix)	
		surface.SetTextPos(x, y)
		surface.DrawText(text)
	cam.PopModelMatrix()
end