GraphEdit

Inherits: Control < CanvasItem < Node < Object

GraphEdit is a control responsible for displaying and manipulating graph-like data using GraphNodes. It provides access to creation, removal, connection, and disconnection of nodes.

Description

GraphEdit provides tools for creation, manipulation, and display of various graphs. Its main purpose in the engine is to power the visual programming systems, such as visual shaders, but it is also available for use in user projects.

GraphEdit by itself is only an empty container, representing an infinite grid where GraphNodes can be placed. Each GraphNode represent a node in the graph, a single unit of data in the connected scheme. GraphEdit, in turn, helps to control various interactions with nodes and between nodes. When the user attempts to connect, disconnect, or close a GraphNode, a signal is emitted in the GraphEdit, but no action is taken by default. It is the responsibility of the programmer utilizing this control to implement the necessary logic to determine how each request should be handled.

Performance: It is greatly advised to enable low-processor usage mode (see OS.low_processor_usage_mode) when using GraphEdits.

Properties

bool

arrange_nodes_button_hidden

false

bool

clip_contents

true (overrides Control)

bool

connection_lines_antialiased

true

float

connection_lines_curvature

0.5

float

connection_lines_thickness

2.0

FocusMode

focus_mode

2 (overrides Control)

bool

minimap_enabled

true

float

minimap_opacity

0.65

Vector2

minimap_size

Vector2(240, 160)

PanningScheme

panning_scheme

0

bool

right_disconnects

false

Vector2

scroll_offset

Vector2(0, 0)

bool

show_zoom_label

false

int

snap_distance

20

bool

use_snap

true

float

zoom

1.0

float

zoom_max

2.0736

float

zoom_min

0.232568

float

zoom_step

1.2

Methods

PackedVector2Array

_get_connection_line ( Vector2 from_position, Vector2 to_position ) virtual const

bool

_is_in_input_hotzone ( Object in_node, int in_port, Vector2 mouse_position ) virtual

bool

_is_in_output_hotzone ( Object in_node, int in_port, Vector2 mouse_position ) virtual

bool

_is_node_hover_valid ( StringName from_node, int from_port, StringName to_node, int to_port ) virtual

void

add_valid_connection_type ( int from_type, int to_type )

void

add_valid_left_disconnect_type ( int type )

void

add_valid_right_disconnect_type ( int type )

void

arrange_nodes ( )

void

clear_connections ( )

Error

connect_node ( StringName from_node, int from_port, StringName to_node, int to_port )

void

disconnect_node ( StringName from_node, int from_port, StringName to_node, int to_port )

void

force_connection_drag_end ( )

PackedVector2Array

get_connection_line ( Vector2 from_node, Vector2 to_node )

Dictionary[]

get_connection_list ( ) const

HBoxContainer

get_zoom_hbox ( )

bool

is_node_connected ( StringName from_node, int from_port, StringName to_node, int to_port )

bool

is_valid_connection_type ( int from_type, int to_type ) const

void

remove_valid_connection_type ( int from_type, int to_type )

void

remove_valid_left_disconnect_type ( int type )

void

remove_valid_right_disconnect_type ( int type )

void

set_connection_activity ( StringName from_node, int from_port, StringName to_node, int to_port, float amount )

void

set_selected ( Node node )

Theme Properties

Color

activity

Color(1, 1, 1, 1)

Color

grid_major

Color(1, 1, 1, 0.2)

Color

grid_minor

Color(1, 1, 1, 0.05)

Color

selection_fill

Color(1, 1, 1, 0.3)

Color

selection_stroke

Color(1, 1, 1, 0.8)

int

port_hotzone_inner_extent

22

int

port_hotzone_outer_extent

26

Texture2D

layout

Texture2D

minimap

Texture2D

minus

Texture2D

more

Texture2D

reset

Texture2D

snap

StyleBox

bg

Signals

  • begin_node_move ( )

Emitted at the beginning of a GraphNode movement.


  • connection_drag_ended ( )

Emitted at the end of a connection drag.


  • connection_drag_started ( String from_node, int from_port, bool is_output )

Emitted at the beginning of a connection drag.


Emitted when user drags a connection from an input port into the empty space of the graph.


Emitted to the GraphEdit when the connection between the from_port of the from_node GraphNode and the to_port of the to_node GraphNode is attempted to be created.


Emitted when user drags a connection from an output port into the empty space of the graph.


  • copy_nodes_request ( )

Emitted when the user presses Ctrl + C.


Emitted when a GraphNode is attempted to be removed from the GraphEdit. Provides a list of node names to be removed (all selected nodes, excluding nodes without closing button).


Emitted to the GraphEdit when the connection between from_port of from_node GraphNode and to_port of to_node GraphNode is attempted to be removed.


  • duplicate_nodes_request ( )

Emitted when a GraphNode is attempted to be duplicated in the GraphEdit.


  • end_node_move ( )

Emitted at the end of a GraphNode movement.


  • node_deselected ( Node node )


  • node_selected ( Node node )

Emitted when a GraphNode is selected.


  • paste_nodes_request ( )

Emitted when the user presses Ctrl + V.


  • popup_request ( Vector2 position )

Emitted when a popup is requested. Happens on right-clicking in the GraphEdit. position is the position of the mouse pointer when the signal is sent.


  • scroll_offset_changed ( Vector2 offset )

Emitted when the scroll offset is changed by the user. It will not be emitted when changed in code.

Enumerations

enum PanningScheme:

  • SCROLL_ZOOMS = 0 --- Mouse Wheel will zoom, Ctrl + Mouse Wheel will move the view.

  • SCROLL_PANS = 1 --- Mouse Wheel will move the view, Ctrl + Mouse Wheel will zoom.

Property Descriptions

  • bool arrange_nodes_button_hidden

Default

false

Setter

set_arrange_nodes_button_hidden(value)

Getter

is_arrange_nodes_button_hidden()

If true, the Arrange Nodes button is hidden.


  • bool connection_lines_antialiased

Default

true

Setter

set_connection_lines_antialiased(value)

Getter

is_connection_lines_antialiased()

If true, the lines between nodes will use antialiasing.


  • float connection_lines_curvature

Default

0.5

Setter

set_connection_lines_curvature(value)

Getter

get_connection_lines_curvature()

The curvature of the lines between the nodes. 0 results in straight lines.


  • float connection_lines_thickness

Default

2.0

Setter

set_connection_lines_thickness(value)

Getter

get_connection_lines_thickness()

The thickness of the lines between the nodes.


  • bool minimap_enabled

Default

true

Setter

set_minimap_enabled(value)

Getter

is_minimap_enabled()

If true, the minimap is visible.


Default

0.65

Setter

set_minimap_opacity(value)

Getter

get_minimap_opacity()

The opacity of the minimap rectangle.


Default

Vector2(240, 160)

Setter

set_minimap_size(value)

Getter

get_minimap_size()

The size of the minimap rectangle. The map itself is based on the size of the grid area and is scaled to fit this rectangle.


Default

0

Setter

set_panning_scheme(value)

Getter

get_panning_scheme()

Defines the control scheme for panning with mouse wheel.


  • bool right_disconnects

Default

false

Setter

set_right_disconnects(value)

Getter

is_right_disconnects_enabled()

If true, enables disconnection of existing connections in the GraphEdit by dragging the right end.


Default

Vector2(0, 0)

Setter

set_scroll_ofs(value)

Getter

get_scroll_ofs()

The scroll offset.


  • bool show_zoom_label

Default

false

Setter

set_show_zoom_label(value)

Getter

is_showing_zoom_label()

If true, makes a label with the current zoom level visible. The zoom value is displayed in percents.


  • int snap_distance

Default

20

Setter

set_snap(value)

Getter

get_snap()

The snapping distance in pixels.


Default

true

Setter

set_use_snap(value)

Getter

is_using_snap()

If true, enables snapping.


Default

1.0

Setter

set_zoom(value)

Getter

get_zoom()

The current zoom value.


Default

2.0736

Setter

set_zoom_max(value)

Getter

get_zoom_max()

The upper zoom limit.


Default

0.232568

Setter

set_zoom_min(value)

Getter

get_zoom_min()

The lower zoom limit.


Default

1.2

Setter

set_zoom_step(value)

Getter

get_zoom_step()

The step of each zoom level.

Method Descriptions

Virtual method which can be overridden to customize how connections are drawn.


Returns whether the mouse_position is in the input hot zone.

By default, a hot zone is a Rect2 positioned such that its center is at in_node.GraphNode.get_connection_input_position(in_port) (For output's case, call GraphNode.get_connection_output_position instead). The hot zone's width is twice the Theme Property port_grab_distance_horizontal, and its height is twice the port_grab_distance_vertical.

Below is a sample code to help get started:

func _is_in_input_hotzone(in_node, in_port, mouse_position):
    var port_size : Vector2 = Vector2(get_theme_constant("port_grab_distance_horizontal"), get_theme_constant("port_grab_distance_vertical"))
    var port_pos : Vector2 = in_node.get_position() + in_node.get_connection_input_position(in_port) - port_size / 2
    var rect = Rect2(port_pos, port_size)

    return rect.has_point(mouse_position)

Returns whether the mouse_position is in the output hot zone. For more information on hot zones, see _is_in_input_hotzone.

Below is a sample code to help get started:

func _is_in_output_hotzone(in_node, in_port, mouse_position):
    var port_size : Vector2 = Vector2(get_theme_constant("port_grab_distance_horizontal"), get_theme_constant("port_grab_distance_vertical"))
    var port_pos : Vector2 = in_node.get_position() + in_node.get_connection_output_position(in_port) - port_size / 2
    var rect = Rect2(port_pos, port_size)

    return rect.has_point(mouse_position)

This virtual method can be used to insert additional error detection while the user is dragging a connection over a valid port.

Return true if the connection is indeed valid or return false if the connection is impossible. If the connection is impossible, no snapping to the port and thus no connection request to that port will happen.

In this example a connection to same node is suppressed:

func _is_node_hover_valid(from, from_port, to, to_port):
    return from != to

  • void add_valid_connection_type ( int from_type, int to_type )

Allows the connection between two different port types. The port type is defined individually for the left and the right port of each slot with the GraphNode.set_slot method.

See also is_valid_connection_type and remove_valid_connection_type.


  • void add_valid_left_disconnect_type ( int type )

Allows to disconnect nodes when dragging from the left port of the GraphNode's slot if it has the specified type. See also remove_valid_left_disconnect_type.


  • void add_valid_right_disconnect_type ( int type )

Allows to disconnect nodes when dragging from the right port of the GraphNode's slot if it has the specified type. See also remove_valid_right_disconnect_type.


  • void arrange_nodes ( )

Rearranges selected nodes in a layout with minimum crossings between connections and uniform horizontal and vertical gap between nodes.


  • void clear_connections ( )

Removes all connections between nodes.


Create a connection between the from_port of the from_node GraphNode and the to_port of the to_node GraphNode. If the connection already exists, no connection is created.


Removes the connection between the from_port of the from_node GraphNode and the to_port of the to_node GraphNode. If the connection does not exist, no connection is removed.


  • void force_connection_drag_end ( )

Ends the creation of the current connection. In other words, if you are dragging a connection you can use this method to abort the process and remove the line that followed your cursor.

This is best used together with connection_drag_started and connection_drag_ended to add custom behavior like node addition through shortcuts.

Note: This method suppresses any other connection request signals apart from connection_drag_ended.


Returns the points which would make up a connection between from_node and to_node.


Returns an Array containing the list of connections. A connection consists in a structure of the form { from_port: 0, from: "GraphNode name 0", to_port: 1, to: "GraphNode name 1" }.


Gets the HBoxContainer that contains the zooming and grid snap controls in the top left of the graph. You can use this method to reposition the toolbar or to add your own custom controls to it.

Warning: This is a required internal node, removing and freeing it may cause a crash. If you wish to hide it or any of its children, use their CanvasItem.visible property.


Returns true if the from_port of the from_node GraphNode is connected to the to_port of the to_node GraphNode.


  • bool is_valid_connection_type ( int from_type, int to_type ) const

Returns whether it's possible to make a connection between two different port types. The port type is defined individually for the left and the right port of each slot with the GraphNode.set_slot method.

See also add_valid_connection_type and remove_valid_connection_type.


  • void remove_valid_connection_type ( int from_type, int to_type )

Disallows the connection between two different port types previously allowed by add_valid_connection_type. The port type is defined individually for the left and the right port of each slot with the GraphNode.set_slot method.

See also is_valid_connection_type.


  • void remove_valid_left_disconnect_type ( int type )

Disallows to disconnect nodes when dragging from the left port of the GraphNode's slot if it has the specified type. Use this to disable disconnection previously allowed with add_valid_left_disconnect_type.


  • void remove_valid_right_disconnect_type ( int type )

Disallows to disconnect nodes when dragging from the right port of the GraphNode's slot if it has the specified type. Use this to disable disconnection previously allowed with add_valid_right_disconnect_type.


Sets the coloration of the connection between from_node's from_port and to_node's to_port with the color provided in the activity theme property.


  • void set_selected ( Node node )

Sets the specified node as the one selected.

Theme Property Descriptions

Default

Color(1, 1, 1, 1)

There is currently no description for this theme property. Please help us by contributing one!


Default

Color(1, 1, 1, 0.2)

Color of major grid lines.


Default

Color(1, 1, 1, 0.05)

Color of minor grid lines.


Default

Color(1, 1, 1, 0.3)

The fill color of the selection rectangle.


Default

Color(1, 1, 1, 0.8)

The outline color of the selection rectangle.


  • int port_hotzone_inner_extent

Default

22

The horizontal range within which a port can be grabbed (inner side).


  • int port_hotzone_outer_extent

Default

26

The horizontal range within which a port can be grabbed (outer side).


There is currently no description for this theme property. Please help us by contributing one!


There is currently no description for this theme property. Please help us by contributing one!


The icon for the zoom out button.


The icon for the zoom in button.


The icon for the zoom reset button.


The icon for the snap toggle button.


The background drawn under the grid.