Skip to content

StyleBox

Inherits: Resource < RefCounted < Object

Inherited by: StyleBoxEmpty, StyleBoxFlat, StyleBoxLine, StyleBoxTexture

Abstract base class for defining stylized boxes for UI elements.

StyleBox is an abstract base class for drawing stylized boxes for UI elements. It is used for panels, buttons, LineEdit backgrounds, Tree backgrounds, etc. and also for testing a transparency mask for pointer signals. If mask test fails on a StyleBox assigned as mask to a control, clicks and motion signals will go through it to the one below.

Note: For control nodes that have Theme Properties, the focus StyleBox is displayed over the normal, hover or pressed StyleBox. This makes the focus StyleBox more reusable across different nodes.

Type Name Default
float content_margin_bottom -1.0
float content_margin_left -1.0
float content_margin_right -1.0
float content_margin_top -1.0
Returns Method
void _draw(to_canvas_item: RID, rect: Rect2) virtual required const
Rect2 _get_draw_rect(rect: Rect2) virtual const
Vector2 _get_minimum_size() virtual const
bool _test_mask(point: Vector2, rect: Rect2) virtual const
void draw(canvas_item: RID, rect: Rect2) const
float get_content_margin(margin: Side) const
CanvasItem get_current_item_drawn() const
float get_margin(margin: Side) const
Vector2 get_minimum_size() const
Vector2 get_offset() const
void set_content_margin(margin: Side, offset: float)
void set_content_margin_all(offset: float)
bool test_mask(point: Vector2, rect: Rect2) const

content_margin_bottom

float content_margin_bottom = -1.0

The bottom margin for the contents of this style box. Increasing this value reduces the space available to the contents from the bottom.

If this value is negative, it is ignored and a child-specific margin is used instead. For example, for StyleBoxFlat, the border thickness (if any) is used instead.

It is up to the code using this style box to decide what these contents are: for example, a Button respects this content margin for the textual contents of the button.

get_margin() should be used to fetch this value as consumer instead of reading these properties directly. This is because it correctly respects negative values and the fallback mentioned above.

content_margin_left

float content_margin_left = -1.0

The left margin for the contents of this style box. Increasing this value reduces the space available to the contents from the left.

Refer to content_margin_bottom for extra considerations.

content_margin_right

float content_margin_right = -1.0

The right margin for the contents of this style box. Increasing this value reduces the space available to the contents from the right.

Refer to content_margin_bottom for extra considerations.

content_margin_top

float content_margin_top = -1.0

The top margin for the contents of this style box. Increasing this value reduces the space available to the contents from the top.

Refer to content_margin_bottom for extra considerations.

_draw

void _draw(to_canvas_item: RID, rect: Rect2) virtual required const

_get_draw_rect

Rect2 _get_draw_rect(rect: Rect2) virtual const

_get_minimum_size

Vector2 _get_minimum_size() virtual const

Virtual method to be implemented by the user. Returns a custom minimum size that the stylebox must respect when drawing. By default get_minimum_size() only takes content margins into account. This method can be overridden to add another size restriction. A combination of the default behavior and the output of this method will be used, to account for both sizes.

_test_mask

bool _test_mask(point: Vector2, rect: Rect2) virtual const

draw

void draw(canvas_item: RID, rect: Rect2) const

Draws this stylebox using a canvas item identified by the given RID.

The RID value can either be the result of CanvasItem.get_canvas_item() called on an existing CanvasItem-derived node, or directly from creating a canvas item in the RenderingSystem with RenderingSystem.canvas_item_create().

get_content_margin

float get_content_margin(margin: Side) const

Returns the default margin of the specified Side.

get_current_item_drawn

CanvasItem get_current_item_drawn() const

Returns the CanvasItem that handles its CanvasItem.NOTIFICATION_DRAW or CanvasItem._draw() callback at this moment.

get_margin

float get_margin(margin: Side) const

Returns the content margin offset for the specified Side.

Positive values reduce size inwards, unlike Control’s margin values.

get_minimum_size

Vector2 get_minimum_size() const

Returns the minimum size that this stylebox can be shrunk to.

get_offset

Vector2 get_offset() const

Returns the “offset” of a stylebox. This helper function returns a value equivalent to Vector2(style.get_margin(MARGIN_LEFT), style.get_margin(MARGIN_TOP)).

set_content_margin

void set_content_margin(margin: Side, offset: float)

Sets the default value of the specified Side to offset pixels.

set_content_margin_all

void set_content_margin_all(offset: float)

Sets the default margin to offset pixels for all sides.

test_mask

bool test_mask(point: Vector2, rect: Rect2) const

Test a position in a rectangle, return whether it passes the mask test.