GridRect

Inherits: Control < CanvasItem < Node < Object

Draws a rectangular grid.

Description

A control which allows to draw a rectangular grid of any size. Provides basic methods for converting between grid and view coordinates with or without snapping, see methods view_to_point and point_to_view. The drawing can be completely customized by overriding _draw_line method.

Note: the scope of this class is mostly limited to drawing, and does not provide additional features like dragging, scrolling or zooming as seen in classes like GraphEdit, but nonetheless possible to implement via script by modifying origin_offset and origin_scale properties.

Methods

bool _draw_line ( Vector2 from, Vector2 to, Color color, float width, Dictionary line ) virtual
void clear_cell_metadata ( )
Variant get_cell_metadata ( Vector2 cell ) const
Vector2 point_to_view ( Vector2 point ) const
void set_cell_metadata ( Vector2 cell, Variant metadata )
Vector2 view_to_point ( Vector2 position ) const
Vector2 view_to_point_snapped ( Vector2 position ) const

Signals

Emitted when the grid is clicked. The point and point_snapped are specified in grid coordinates and are computed with view_to_point and view_to_point_snapped respectively. The local_position is the position in pixels relative to this control.

Enumerations

enum CellOrigin:

  • CELL_ORIGIN_TOP_LEFT = 0 — Cell’s top-left offset, corresponds to Vector2(0, 0).
  • CELL_ORIGIN_CENTER = 1 — Cell’s center offset, corresponds to cell_size * Vector2(0.5, 0.5).

enum Axis:

  • AXIS_X = 0 — Horizontal axis.
  • AXIS_Y = 1 — Vertical axis.

enum Line:

  • LINE_CELL = 0 — Cell lines.
  • LINE_DIVISION = 1 — Division lines.
  • LINE_AXIS = 2 — Origin axes lines (X and Y).

Property Descriptions

Default 1.0
Setter set_cell_line_width(value)
Getter get_cell_line_width()

The width of cell lines.


Default 0
Setter set_cell_origin(value)
Getter get_cell_origin()

Determines the cell’s origin offset for drawing and snapping.


Default Vector2( 32, 32 )
Setter set_cell_size(value)
Getter get_cell_size()

The size of individual cells drawn via grid line intersections.


  • int divisions_horizontal
Default 8
Setter set_divisions_horizontal(value)
Getter get_divisions_horizontal()

The number of horizontal divisions along the AXIS_X.


  • float divisions_line_width
Default 1.0
Setter set_divisions_line_width(value)
Getter get_divisions_line_width()

The width of division lines.


  • int divisions_vertical
Default 8
Setter set_divisions_vertical(value)
Getter get_divisions_vertical()

The number of horizontal divisions along the AXIS_Y.


  • bool metadata_show_tooltip
Default true
Setter set_metadata_show_tooltip(value)
Getter is_showing_metadata_tooltip()

If true, displays a tooltip on hover with coordinates snapped to the grid.


  • float origin_axes_line_width
Default 1.0
Setter set_origin_axes_line_width(value)
Getter get_origin_axes_line_width()

The width of main axes lines (X and Y). Only relevant when origin_axes_visible is set to true.


  • bool origin_axes_visible
Default false
Setter set_origin_axes_visible(value)
Getter is_origin_axes_visible()

If true, displays main axes (X and Y).


  • bool origin_centered
Default false
Setter set_origin_centered(value)
Getter is_origin_centered()

If true, the origin is centered to this control’s center. The origin_offset will additionally shift the origin away from the center.


Default Vector2( 0, 0 )
Setter set_origin_offset(value)
Getter get_origin_offset()

The offset that shifts the origin. Can be interpreted as a scroll offset.


Default Vector2( 1, 1 )
Setter set_origin_scale(value)
Getter get_origin_scale()

The scale that zoom in or out the grid coordinates.

Method Descriptions

Called before drawing a grid line. The first four parameters match the interface of CanvasItem.draw_line.

The line parameter is a Dictionary which contains the following fields:

step: The line’s step, which describes the current grid integer coordinate along the axis (can be negative or positive).

axis: The line’s axis along which it’s drawn, one of Axis values.

type: The line’s type, one of Line values.

Return true to draw the current line using default parameters, or false to discard it. You can still draw the line using CanvasItem.draw_line method, or any other drawing method.


  • void clear_cell_metadata ( )

Clears all cell metadata from the grid previously assigned with set_cell_metadata.


Returns metadata for individual cell on the grid previously assigned with set_cell_metadata (only integer coordinates are accepted). See also metadata_show_tooltip.


Converts a point specified in grid coordinates to local position of this control.


Assigns metadata for individual cell on the grid (only integer coordinates are accepted). See also metadata_show_tooltip.


Converts a local position of this control to grid coordinates. See also view_to_point_snapped.


Converts a point specified in grid coordinates to local position of this control. The point is snapped to grid line intersections when cell_origin is set to CELL_ORIGIN_TOP_LEFT, or snapped to grid cell center if cell_origin is set to CELL_ORIGIN_CENTER. See also view_to_point.