Masking problems: Sprite2D with a ColorRect

Godot Version

4.1

Question

I’ve seen that fortunately in Godot 4 I can mask using the CanvasItem->ClipChildren property and setting it to “Clip Only”. So I made a ColorRect, and it worked perfect for its UI children, only those inside the ColorRect were drawn.

Everything was happiness until I tried to add a Sprite2D. It completely ignores the mask and gets fully drawn.

Why is this? How can I make this mask work for a Sprite2D as well?

Also another thing I realized is, if I create an UI child, say another ColorRect, part of it inside the mask and part of it outside, this child sends mouse_entered() and mouse_exited() depending on this child ColorRect and not on the mask, reacting “as if” the child ColorRect was visible.

Can this behaviour be fixed somehow?

Some things you can try:

  • Use the clip_contents property (boolean true) instead of clip_children
  • Must the Sprite2D be a Sprite2D? Can it be a TextureRect, instead?

For the mouse entered and exited problem, you could detect those signals on the mask, instead of the ColorRect. If the cursor must be in both the ColorRect and the mask, can listen to signals on both, keep track of the state, and only run the code you want when the cursor is in both.
Or create another invisible control in the overlapping region solely for detecting the mouse hover.

1 Like

Indeed with a TextureRect may work, but I wanted to reuse some .tscn (that holds a multilayer sprite) instead of making it again like this.

About the signal problem I guess I will try to put some transparent ColorRect nodes up and down the mask that block mouse events.

Still it makes me think about how necessary is that old idea of implementing a Mask2D node variety :thinking:

I solved the problem with mouse_entered() and mouse_exited() by making two invisible ColorRect nodes, one above the area and one below it. Both have Mouse->Filter = Stop so that nothing gets through and the mouse_entered() and mouse_exited signals() now work properly. Also having both with “Force Pass Scroll Events” to false makes it so they don’t process the mouse wheel and so on when the pointer is outside the mask.

My only remaining problem is that of course Sprite2D still doesn’t care about the mask. I guess if there’s no way to deal with this I’ll have to find a way to load the sprites I need in some TextureRect, its dirty but there doesn’t seem to be any other way unfortunately.

Finally solved everything, the problem wasn’t Sprite2D itself but messing with ZIndex broke it :no_mouth:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.