Skip to content

NinePatchRect

Inherits: Control < CanvasItem < Entity < Object

A control that displays a texture by keeping its corners intact, but tiling its edges and center.

Also known as 9-slice panels, NinePatchRect produces clean panels of any size based on a small texture. To do so, it splits the texture in a 3×3 grid. When you scale the node, it tiles the texture’s edges horizontally or vertically, tiles the center on both axes, and leaves the corners unchanged.

Type Name Default
AxisStretchMode axis_stretch_horizontal 0
AxisStretchMode axis_stretch_vertical 0
bool draw_center true
Control.MouseFilter mouse_filter 2 (overrides Control)
int patch_margin_bottom 0
int patch_margin_left 0
int patch_margin_right 0
int patch_margin_top 0
Rect2 region_rect Rect2(0, 0, 0, 0)
Texture2D texture
Returns Method
int get_patch_margin(margin: Side) const
void set_patch_margin(margin: Side, value: int)

texture_changed

texture_changed()

Emitted when the node’s texture changes.

AxisStretchMode

enum AxisStretchMode

AXIS_STRETCH_MODE_STRETCH = 0

Stretches the center texture across the NinePatchRect. This may cause the texture to be distorted.

AXIS_STRETCH_MODE_TILE = 1

Repeats the center texture across the NinePatchRect. This won’t cause any visible distortion. The texture must be seamless for this to work without displaying artifacts between edges.

AXIS_STRETCH_MODE_TILE_FIT = 2

Repeats the center texture across the NinePatchRect, but will also stretch the texture to make sure each tile is visible in full. This may cause the texture to be distorted, but less than AXIS_STRETCH_MODE_STRETCH. The texture must be seamless for this to work without displaying artifacts between edges.

axis_stretch_horizontal

AxisStretchMode axis_stretch_horizontal = 0

The stretch mode to use for horizontal stretching/tiling.

axis_stretch_vertical

AxisStretchMode axis_stretch_vertical = 0

The stretch mode to use for vertical stretching/tiling.

draw_center

bool draw_center = true

If true, draw the panel’s center. Else, only draw the 9-slice’s borders.

patch_margin_bottom

int patch_margin_bottom = 0

The height of the 9-slice’s bottom row. A margin of 16 means the 9-slice’s bottom corners and side will have a height of 16 pixels. You can set all 4 margin values individually to create panels with non-uniform borders.

patch_margin_left

int patch_margin_left = 0

The width of the 9-slice’s left column. A margin of 16 means the 9-slice’s left corners and side will have a width of 16 pixels. You can set all 4 margin values individually to create panels with non-uniform borders.

patch_margin_right

int patch_margin_right = 0

The width of the 9-slice’s right column. A margin of 16 means the 9-slice’s right corners and side will have a width of 16 pixels. You can set all 4 margin values individually to create panels with non-uniform borders.

patch_margin_top

int patch_margin_top = 0

The height of the 9-slice’s top row. A margin of 16 means the 9-slice’s top corners and side will have a height of 16 pixels. You can set all 4 margin values individually to create panels with non-uniform borders.

region_rect

Rect2 region_rect = Rect2(0, 0, 0, 0)

Rectangular region of the texture to sample from. If you’re working with an atlas, use this property to define the area the 9-slice should use. All other properties are relative to this one. If the rect is empty, NinePatchRect will use the whole texture.

texture

Texture2D texture

The node’s texture resource.

get_patch_margin

int get_patch_margin(margin: Side) const

Returns the size of the margin on the specified Side.

set_patch_margin

void set_patch_margin(margin: Side, value: int)

Sets the size of the margin on the specified Side to value pixels.