CanvasItem

Inherits: Node < Object

Inherited By: Node2D, Control

Category: Core

Brief Description

Base class of anything 2D.

Member Functions

void _draw ( ) virtual
float draw_char ( Font font, Vector2 pos, String char, String next, Color modulate=Color(1,1,1,1) )
void draw_circle ( Vector2 pos, float radius, Color color )
void draw_colored_polygon ( Vector2Array points, Color color, Vector2Array uvs=Vector2Array([]), Texture texture=NULL )
void draw_line ( Vector2 from, Vector2 to, Color color, float width=1 )
void draw_polygon ( Vector2Array points, ColorArray colors, Vector2Array uvs=Vector2Array([]), Texture texture=NULL )
void draw_primitive ( Vector2Array points, ColorArray colors, Vector2Array uvs, Texture texture=NULL, float width=1 )
void draw_rect ( Rect2 rect, Color color )
void draw_set_transform ( Vector2 pos, float rot, Vector2 scale )
void draw_set_transform_matrix ( Matrix32 xform )
void draw_string ( Font font, Vector2 pos, String text, Color modulate=Color(1,1,1,1), int clip_w=-1 )
void draw_style_box ( StyleBox style_box, Rect2 rect )
void draw_texture ( Texture texture, Vector2 pos, Color modulate=Color(1,1,1,1) )
void draw_texture_rect ( Texture texture, Rect2 rect, bool tile, Color modulate=Color(1,1,1,1), bool transpose=false )
void draw_texture_rect_region ( Texture texture, Rect2 rect, Rect2 src_rect, Color modulate=Color(1,1,1,1), bool transpose=false )
Variant edit_get_state ( ) const
void edit_rotate ( float degrees )
void edit_set_rect ( Rect2 rect )
void edit_set_state ( Variant state )
int get_blend_mode ( ) const
RID get_canvas ( ) const
RID get_canvas_item ( ) const
Matrix32 get_canvas_transform ( ) const
Vector2 get_global_mouse_pos ( ) const
Matrix32 get_global_transform ( ) const
Matrix32 get_global_transform_with_canvas ( ) const
Rect2 get_item_and_children_rect ( ) const
Rect2 get_item_rect ( ) const
int get_light_mask ( ) const
Vector2 get_local_mouse_pos ( ) const
CanvasItemMaterial get_material ( ) const
float get_opacity ( ) const
float get_self_opacity ( ) const
Matrix32 get_transform ( ) const
bool get_use_parent_material ( ) const
Rect2 get_viewport_rect ( ) const
Matrix32 get_viewport_transform ( ) const
Object get_world_2d ( ) const
void hide ( )
bool is_draw_behind_parent_enabled ( ) const
bool is_hidden ( ) const
bool is_set_as_toplevel ( ) const
bool is_visible ( ) const
Vector2 make_canvas_pos_local ( Vector2 screen_point ) const
InputEvent make_input_local ( InputEvent event ) const
void set_as_toplevel ( bool enable )
void set_blend_mode ( int blend_mode )
void set_draw_behind_parent ( bool enable )
void set_hidden ( bool hidden )
void set_light_mask ( int light_mask )
void set_material ( CanvasItemMaterial material )
void set_opacity ( float opacity )
void set_self_opacity ( float self_opacity )
void set_use_parent_material ( bool enable )
void show ( )
void update ( )

Signals

  • draw ( )

Emitted when the CanvasItem must redraw. This can only be connected realtime, as deferred will not allow drawing.

  • hide ( )

Emitted when becoming hidden.

  • item_rect_changed ( )

Emitted when the item rect has changed.

  • visibility_changed ( )

Emitted when the visibility (hidden/visible) changes.

Numeric Constants

  • BLEND_MODE_MIX = 0 — Mix blending mode. Colors are assumed to be independent of the alpha (opacity) value.
  • BLEND_MODE_ADD = 1 — Additive blending mode.
  • BLEND_MODE_SUB = 2 — Subtractive blending mode.
  • BLEND_MODE_MUL = 3 — Multiplicative blending mode.
  • BLEND_MODE_PREMULT_ALPHA = 4 — Mix blending mode. Colors are assumed to be premultiplied by the alpha (opacity) value.
  • NOTIFICATION_DRAW = 30 — CanvasItem is requested to draw.
  • NOTIFICATION_VISIBILITY_CHANGED = 31 — Canvas item visibility has changed.
  • NOTIFICATION_ENTER_CANVAS = 32 — Canvas item has entered the canvas.
  • NOTIFICATION_EXIT_CANVAS = 33 — Canvas item has exited the canvas.
  • NOTIFICATION_TRANSFORM_CHANGED = 29 — Canvas item transform has changed. Only received if requested.

Description

Base class of anything 2D. Canvas items are laid out in a tree and children inherit and extend the transform of their parent. CanvasItem is extended by Control, for anything GUI related, and by Node2D for anything 2D engine related.

Any CanvasItem can draw. For this, the “update” function must be called, then NOTIFICATION_DRAW will be received on idle time to request redraw. Because of this, canvas items don’t need to be redraw on every frame, improving the performance significantly. Several functions for drawing on the CanvasItem are provided (see draw_* functions). They can only be used inside the notification, signal or _draw() overrides function, though.

Canvas items are draw in tree order. By default, children are on top of their parents so a root CanvasItem will be drawn behind everything (this can be changed per item though).

Canvas items can also be hidden (hiding also their subtree). They provide many means for changing standard parameters such as opacity (for it and the subtree) and self opacity, blend mode.

Ultimately, a transform notification can be requested, which will notify the node that its global position changed in case the parent tree changed.

Member Function Description

  • void _draw ( ) virtual

Called (if exists) to draw the canvas item.

Draw a string character using a custom font. Returns the advance, depending on the char width and kerning with an optional next char.

Draw a colored circle.

Draw a colored polygon of any amount of points, convex or concave.

Draw a line from a 2D point to another, with a given color and width.

Draw a polygon of any amount of points, convex or concave.

Draw a custom primitive, 1 point for a point, 2 points for a line, 3 points for a triangle and 4 points for a quad.

Draw a colored rectangle.

Set a custom transform for drawing. Anything drawn afterwards will be transformed by this.

  • void draw_set_transform_matrix ( Matrix32 xform )

Draw a string using a custom font.

Draw a styled rectangle.

Draw a texture at a given position.

Draw a textured rectangle at a given position, optionally modulated by a color. Transpose swaps the x and y coordinates when reading the texture.

  • void draw_texture_rect_region ( Texture texture, Rect2 rect, Rect2 src_rect, Color modulate=Color(1,1,1,1), bool transpose=false )

Draw a textured rectangle region at a given position, optionally modulated by a color. Transpose swaps the x and y coordinates when reading the texture.

Used for editing, returns an opaque value representing the transform state.

  • void edit_rotate ( float degrees )

Used for editing, handle rotation.

  • void edit_set_rect ( Rect2 rect )
  • void edit_set_state ( Variant state )

Set the transform state of this CanvasItem. For Node2D, this is an Array with (in order) a Vector2 for position, a float for rotation and another Vector2 for scale. For Control this is a Rect2 with the position and size.

  • int get_blend_mode ( ) const

Return the current blending mode from enum BLEND_MODE_*.

  • RID get_canvas ( ) const

Return the RID of the World2D canvas where this item is in.

  • RID get_canvas_item ( ) const

Return the canvas item RID used by VisualServer for this item.

  • Matrix32 get_canvas_transform ( ) const

Get the transform matrix of this item’s canvas.

  • Vector2 get_global_mouse_pos ( ) const

Get the global position of the mouse.

  • Matrix32 get_global_transform ( ) const

Get the global transform matrix of this item.

  • Matrix32 get_global_transform_with_canvas ( ) const

Get the global transform matrix of this item in relation to the canvas.

  • Rect2 get_item_and_children_rect ( ) const

Get a Rect2 with the boundaries of this item and its children.

  • Rect2 get_item_rect ( ) const

Return a rect containing the editable boundaries of the item.

  • int get_light_mask ( ) const

Get this item’s light mask number.

  • Vector2 get_local_mouse_pos ( ) const

Get the mouse position relative to this item’s position.

Get the material of this item.

  • float get_opacity ( ) const

Return the canvas item opacity. This affects the canvas item and all the children.

  • float get_self_opacity ( ) const

Return the canvas item self-opacity.

Get the transform matrix of this item.

  • bool get_use_parent_material ( ) const

Get whether this item uses its parent’s material.

  • Rect2 get_viewport_rect ( ) const

Get the viewport’s boundaries as a Rect2.

  • Matrix32 get_viewport_transform ( ) const

Get this item’s transform in relation to the viewport.

  • Object get_world_2d ( ) const

Get the World2D where this item is in.

  • void hide ( )

Hide the CanvasItem currently visible.

  • bool is_draw_behind_parent_enabled ( ) const

Return whether the item is drawn behind its parent.

  • bool is_hidden ( ) const

Return true if this CanvasItem is hidden. Note that the CanvasItem may not be visible, but as long as it’s not hidden (hide called) the function will return false.

  • bool is_set_as_toplevel ( ) const

Return if set as toplevel. See set_as_toplevel.

  • bool is_visible ( ) const

Return true if this CanvasItem is visible. It may be invisible because itself or a parent canvas item is hidden.

Takes a global input event and convert to this item’s coordinate system.

  • void set_as_toplevel ( bool enable )

Set as top level. This means that it will not inherit transform from parent canvas items.

  • void set_blend_mode ( int blend_mode )

Set the blending mode from enum BLEND_MODE_*.

  • void set_draw_behind_parent ( bool enable )

Set whether the canvas item is drawn behind its parent.

  • void set_hidden ( bool hidden )

Set whether this item should be hidden or not. Note that no matter what is set here this item won’t be shown if its parent or grandparents nodes are also hidden. A hidden CanvasItem make all children hidden too.

  • void set_light_mask ( int light_mask )

Set the ligtht mask number of this item.

Set the material of this item.

  • void set_opacity ( float opacity )

Set canvas item opacity. This will affect the canvas item and all the children.

  • void set_self_opacity ( float self_opacity )

Set canvas item self-opacity. This does not affect the opacity of children items.

  • void set_use_parent_material ( bool enable )

Set whether or not this item should use its parent’s material.

  • void show ( )

Show the CanvasItem currently hidden.

  • void update ( )

Queue the CanvasItem for update. NOTIFICATION_DRAW will be called on idle time to request redraw.