+3 votes

Hi Everyone,

I am trying to create a custom control that can scroll its child controls, but I cannot find a base control (container, panel, etc) that will only show the parts of the child controls that are within the container's bounds, like the ScrollContainer does.
(I do not want to use the ScrollContainer)

I have tried the Container and Panel and others, but child controls are always fully visible, even if they are totally outside the parent.

How do you accomplish the clipping/masking behavior that the ScrollContainer does?

Thank you.

asked Mar 2, 2016 in Engine by unknown llama (80 points)

2 Answers

+6 votes
Best answer

I just look at the github repo and find the scroll container and I think it's awesome you can mask every subnode of any 2d shape with this line of code

 func _draw():
     VisualServer.canvas_item_set_clip(get_canvas_item(),true)

just add a container or even a sprite then add this code for it and all of it's children will be cliped

answered Mar 3, 2016 by Mohammad Hadi Aliakb (575 points)
selected Mar 4, 2016 by unknown llama
Thank you so much for helping me with this. I tried it, and it works!

Thats very useful, but the clipping only limits the appearance, input events can still hit the invisible buttons. Is there a way to also clip the input events ?

Nice! But it only clips to the bounding box of the canvas item.
Is there any way to clip the children to the actual shape of the parent? (which may be irregular, as taken from a texture image, for example)

The best thing I could think of to do that would be using a Light2D, settings its mode to "Mask" and setting its texture to that of the parent's shape. Then just make sure it culls the same layer that the children are on.

+1 vote

Control nodes have a checkbox for clipping the children nodes.

The name of the property is rect_clip_content.

answered Nov 27, 2018 by jabcross (16 points)

that's right.
the selected answer was for godot 1.x and 2.x
now we have rect_clip_content property with godot 3.x

That should be the accepted answer nowadays. In most cases there is no need to do the clipping manually in every draw call.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.