Clipping a HUD/Canvas DrawText

Is there a way in unreal engine to clip HUD/Canvas Draw Text to a certain region? I tried setting the TextItem BlendMode to translucent however the really long test text string still goes over the bounds of the rectangle I draw below it. I have looked at Slate and it seems to do it (though I’m not sure how) and I wish to avoid it as both the layout management syntax is strange and the widgets don’t fit with the native operating system.

I know you can get the pixel dimensions for the text and so limit it on the x axis length, however there seems no way to just draw the bottom of half of the text for a scrolling widget that only partially shows that text.

Code

        DrawTextureSimple(texture, 500, 175, 1.0f, false);

	FCanvasTextItem TextItem(FVector2D(300, 175), FText::FromString("REALLY LONG TEXT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"), HUDFont, FColor::Black);
	TextItem.Scale = FVector2D(0.5, 0.5);
	TextItem.BlendMode = SE_BLEND_Translucent;
	FFontRenderInfo fri = FFontRenderInfo();
	fri.bClipText = true;
	TextItem.FontRenderInfo = fri;
	Canvas->DrawItem(TextItem);

Yet the text goes right over the drawn texture

Can’t you just process string based on text area?

@anonymous_user_f5a50610 that works great for horizontal text but not for vertical cutoffs that occur when having a panel scroll. The editor has plenty of these, where if you scroll to show only half of the text it works. So it must be able to be done however UE doesn’t present an obvious way to clip a region. I have looked through the slate code with no avail.

Here is one way to achieve clipping of the canvas / HUD using RHI scissor rects.

I added blueprint functionality as well but you will need to re-parent your HUD to HUDEx as it requires access to the protected member Canvas.

I believe Slate can also clip non axis aligned rectangles. To do this with Canvas you would probably need to either use masks with materials or cut up the triangles and update the UV coords. This would require replacing all existing canvas items to work correctly so materials may be easier with better masking options if anyone is interested in going beyond singular axis aligned clipping rects.

This should answer these related questions

1 Like

Doesn’t work for me:

Error	1	error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl SetScissorRect_Internal(bool,unsigned int,unsigned int,unsigned int,unsigned int)" (__imp_?SetScissorRect_Internal@@YAX_NIIII@Z) referenced in function "public: void __cdecl `public: virtual bool __cdecl FCanvasScissorRectRenderItem::Render_GameThread(class FCanvas const *)'::`2'::EURCMacro_CanvasScissorRectCommand::DoTask(enum ENamedThreads::Type,class TRefCountPtr<class FGraphEvent> const &)" (?DoTask@EURCMacro_CanvasScissorRectCommand@?1??Render_GameThread@FCanvasScissorRectRenderItem@@UEAA_NPEBVFCanvas@@@Z@QEAAXW4Type@ENamedThreads@@AEBV?$TRefCountPtr@VFGraphEvent@@@@@Z)	C:\Users\HW 685\Documents\Unreal Projects\StruggleOfMages\Intermediate\ProjectFiles\ClipCanvas.cpp.obj	StruggleOfMages

I am using unreal engine of version 4.22. The code can be compiled successfully with little modification, but it doesn’t work for me. Nothing is actually clipped at all. Is there any issue with it for unreal engine of recent versions? Sorry for my bad English.