Let's say I have a CanvasItem that needs to draw the background first, occupying the entire item rect, and then draw all its content within a smaller rect leaving a margin.
How should this be done in the NOTIFICATION_DRAW
within _notofication
?
This is what I've tried:
void MyCustomItem::_notification(int p_what) {
case NOTIFICATION_DRAW: {
// Enable clipping
VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
draw_background();
// Clip to margins
VisualServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), true, Rect2(
Point2(
MARGIN_LEFT,
MARGIN_TOP
),
Size2(
width - MARGIN_LEFT - MARGIN_RIGHT,
height - MARGIN_TOP - MARGIN_BOTTOM
)
));
draw_stuff();
}
The problem with this is that it will clip everything to the margins including the background.