Class GraphEdit
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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 represents 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 delete 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 LowProcessorUsageMode) when using GraphEdits.
public class GraphEdit : Control, IDisposable
- Inheritance
-
GraphEdit
- Implements
- Inherited Members
Constructors
GraphEdit()
public GraphEdit()
Properties
ConnectionLinesAntialiased
If true
, the lines between nodes will use antialiasing.
public bool ConnectionLinesAntialiased { get; set; }
Property Value
ConnectionLinesCurvature
The curvature of the lines between the nodes. 0 results in straight lines.
public float ConnectionLinesCurvature { get; set; }
Property Value
ConnectionLinesThickness
The thickness of the lines between the nodes.
public float ConnectionLinesThickness { get; set; }
Property Value
MinimapEnabled
If true
, the minimap is visible.
public bool MinimapEnabled { get; set; }
Property Value
MinimapOpacity
The opacity of the minimap rectangle.
public float MinimapOpacity { get; set; }
Property Value
MinimapSize
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.
public Vector2 MinimapSize { get; set; }
Property Value
PanningScheme
Defines the control scheme for panning with mouse wheel.
public GraphEdit.PanningSchemeEnum PanningScheme { get; set; }
Property Value
RightDisconnects
If true
, enables disconnection of existing connections in the GraphEdit by dragging the right end.
public bool RightDisconnects { get; set; }
Property Value
ScrollOffset
The scroll offset.
public Vector2 ScrollOffset { get; set; }
Property Value
ShowArrangeButton
If true
, the button to automatically arrange graph nodes is visible.
public bool ShowArrangeButton { get; set; }
Property Value
ShowGrid
If true
, the grid is visible.
public bool ShowGrid { get; set; }
Property Value
ShowGridButtons
If true
, buttons that allow to configure grid and snapping options are visible.
public bool ShowGridButtons { get; set; }
Property Value
ShowMenu
If true
, the menu toolbar is visible.
public bool ShowMenu { get; set; }
Property Value
ShowMinimapButton
If true
, the button to toggle the minimap is visible.
public bool ShowMinimapButton { get; set; }
Property Value
ShowZoomButtons
If true
, buttons that allow to change and reset the zoom level are visible.
public bool ShowZoomButtons { get; set; }
Property Value
ShowZoomLabel
If true
, the label with the current zoom level is visible. The zoom level is displayed in percents.
public bool ShowZoomLabel { get; set; }
Property Value
SnappingDistance
The snapping distance in pixels, also determines the grid line distance.
public int SnappingDistance { get; set; }
Property Value
SnappingEnabled
If true
, enables snapping.
public bool SnappingEnabled { get; set; }
Property Value
Zoom
The current zoom value.
public float Zoom { get; set; }
Property Value
ZoomMax
The upper zoom limit.
public float ZoomMax { get; set; }
Property Value
ZoomMin
The lower zoom limit.
public float ZoomMin { get; set; }
Property Value
ZoomStep
The step of each zoom level.
public float ZoomStep { get; set; }
Property Value
Methods
AddValidConnectionType(int, int)
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 SetSlot(int, bool, int, Color, bool, int, Color, Texture2D, Texture2D, bool) method.
See also IsValidConnectionType(int, int) and RemoveValidConnectionType(int, int).
public void AddValidConnectionType(int fromType, int toType)
Parameters
AddValidLeftDisconnectType(int)
Allows to disconnect nodes when dragging from the left port of the GraphNode's slot if it has the specified type. See also RemoveValidLeftDisconnectType(int).
public void AddValidLeftDisconnectType(int type)
Parameters
type
int
AddValidRightDisconnectType(int)
Allows to disconnect nodes when dragging from the right port of the GraphNode's slot if it has the specified type. See also RemoveValidRightDisconnectType(int).
public void AddValidRightDisconnectType(int type)
Parameters
type
int
ArrangeNodes()
Rearranges selected nodes in a layout with minimum crossings between connections and uniform horizontal and vertical gap between nodes.
public void ArrangeNodes()
ClearConnections()
Removes all connections between nodes.
public void ClearConnections()
ConnectNode(StringName, int, StringName, int)
Create a connection between the fromPort
of the fromNode
GraphNode and the toPort
of the toNode
GraphNode. If the connection already exists, no connection is created.
public Error ConnectNode(StringName fromNode, int fromPort, StringName toNode, int toPort)
Parameters
fromNode
StringNamefromPort
inttoNode
StringNametoPort
int
Returns
DisconnectNode(StringName, int, StringName, int)
Removes the connection between the fromPort
of the fromNode
GraphNode and the toPort
of the toNode
GraphNode. If the connection does not exist, no connection is removed.
public void DisconnectNode(StringName fromNode, int fromPort, StringName toNode, int toPort)
Parameters
fromNode
StringNamefromPort
inttoNode
StringNametoPort
int
ForceConnectionDragEnd()
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 ConnectionDragStarted and ConnectionDragEnded to add custom behavior like node addition through shortcuts.
Note: This method suppresses any other connection request signals apart from ConnectionDragEnded.
public void ForceConnectionDragEnd()
GetConnectionLine(Vector2, Vector2)
Returns the points which would make up a connection between fromNode
and toNode
.
public Vector2[] GetConnectionLine(Vector2 fromNode, Vector2 toNode)
Parameters
Returns
- Vector2[]
GetConnectionList()
Returns an Array containing the list of connections. A connection consists in a structure of the form { from_port: 0, from_node: "GraphNode name 0", to_port: 1, to_node: "GraphNode name 1" }
.
public Array<Dictionary> GetConnectionList()
Returns
GetMenuHBox()
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 Visible property.
public HBoxContainer GetMenuHBox()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsNodeConnected(StringName, int, StringName, int)
Returns true
if the fromPort
of the fromNode
GraphNode is connected to the toPort
of the toNode
GraphNode.
public bool IsNodeConnected(StringName fromNode, int fromPort, StringName toNode, int toPort)
Parameters
fromNode
StringNamefromPort
inttoNode
StringNametoPort
int
Returns
IsValidConnectionType(int, int)
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 SetSlot(int, bool, int, Color, bool, int, Color, Texture2D, Texture2D, bool) method.
See also AddValidConnectionType(int, int) and RemoveValidConnectionType(int, int).
public bool IsValidConnectionType(int fromType, int toType)
Parameters
Returns
RemoveValidConnectionType(int, int)
Disallows the connection between two different port types previously allowed by AddValidConnectionType(int, int). The port type is defined individually for the left and the right port of each slot with the SetSlot(int, bool, int, Color, bool, int, Color, Texture2D, Texture2D, bool) method.
See also IsValidConnectionType(int, int).
public void RemoveValidConnectionType(int fromType, int toType)
Parameters
RemoveValidLeftDisconnectType(int)
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 AddValidLeftDisconnectType(int).
public void RemoveValidLeftDisconnectType(int type)
Parameters
type
int
RemoveValidRightDisconnectType(int)
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 AddValidRightDisconnectType(int).
public void RemoveValidRightDisconnectType(int type)
Parameters
type
int
SetConnectionActivity(StringName, int, StringName, int, float)
Sets the coloration of the connection between fromNode
's fromPort
and toNode
's toPort
with the color provided in the activity
theme property. The color is linearly interpolated between the connection color and the activity color using amount
as weight.
public void SetConnectionActivity(StringName fromNode, int fromPort, StringName toNode, int toPort, float amount)
Parameters
fromNode
StringNamefromPort
inttoNode
StringNametoPort
intamount
float
SetSelected(Node)
Sets the specified node
as the one selected.
public void SetSelected(Node node)
Parameters
node
Node
_GetConnectionLine(Vector2, Vector2)
Virtual method which can be overridden to customize how connections are drawn.
public virtual Vector2[] _GetConnectionLine(Vector2 fromPosition, Vector2 toPosition)
Parameters
Returns
- Vector2[]
_IsInInputHotzone(GodotObject, int, Vector2)
Returns whether the mousePosition
is in the input hot zone.
By default, a hot zone is a Rect2 positioned such that its center is at inNode
.GetInputPortPosition(int)(inPort
) (For output's case, call GetOutputPortPosition(int) 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_input_port_position(in_port) - port_size / 2
var rect = Rect2(port_pos, port_size)
return rect.has_point(mouse_position)</code></pre>
public virtual bool _IsInInputHotzone(GodotObject inNode, int inPort, Vector2 mousePosition)
Parameters
inNode
GodotObjectinPort
intmousePosition
Vector2
Returns
_IsInOutputHotzone(GodotObject, int, Vector2)
Returns whether the mousePosition
is in the output hot zone. For more information on hot zones, see _IsInInputHotzone(GodotObject, int, Vector2).
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_output_port_position(in_port) - port_size / 2
var rect = Rect2(port_pos, port_size)
return rect.has_point(mouse_position)</code></pre>
public virtual bool _IsInOutputHotzone(GodotObject inNode, int inPort, Vector2 mousePosition)
Parameters
inNode
GodotObjectinPort
intmousePosition
Vector2
Returns
_IsNodeHoverValid(StringName, int, StringName, int)
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:
public override bool _IsNodeHoverValid(StringName fromNode, int fromPort, StringName toNode, int toPort)
{
return fromNode != toNode;
}
public virtual bool _IsNodeHoverValid(StringName fromNode, int fromPort, StringName toNode, int toPort)
Parameters
fromNode
StringNamefromPort
inttoNode
StringNametoPort
int
Returns
Events
BeginNodeMove
Emitted at the beginning of a GraphElement's movement.
public event Action BeginNodeMove
Event Type
ConnectionDragEnded
Emitted at the end of a connection drag.
public event Action ConnectionDragEnded
Event Type
ConnectionDragStarted
Emitted at the beginning of a connection drag.
public event GraphEdit.ConnectionDragStartedEventHandler ConnectionDragStarted
Event Type
ConnectionFromEmpty
Emitted when user drags a connection from an input port into the empty space of the graph.
public event GraphEdit.ConnectionFromEmptyEventHandler ConnectionFromEmpty
Event Type
ConnectionRequest
Emitted to the GraphEdit when the connection between the fromPort
of the fromNode
GraphNode and the toPort
of the toNode
GraphNode is attempted to be created.
public event GraphEdit.ConnectionRequestEventHandler ConnectionRequest
Event Type
ConnectionToEmpty
Emitted when user drags a connection from an output port into the empty space of the graph.
public event GraphEdit.ConnectionToEmptyEventHandler ConnectionToEmpty
Event Type
CopyNodesRequest
Emitted when this GraphEdit captures a ui_copy
action (Ctrl + C by default). In general, this signal indicates that the selected GraphElements should be copied.
public event Action CopyNodesRequest
Event Type
DeleteNodesRequest
Emitted when this GraphEdit captures a ui_graph_delete
action (Delete by default).
nodes
is an array of node names that should be removed. These usually include all selected nodes.
public event GraphEdit.DeleteNodesRequestEventHandler DeleteNodesRequest
Event Type
DisconnectionRequest
Emitted to the GraphEdit when the connection between fromPort
of fromNode
GraphNode and toPort
of toNode
GraphNode is attempted to be removed.
public event GraphEdit.DisconnectionRequestEventHandler DisconnectionRequest
Event Type
DuplicateNodesRequest
Emitted when this GraphEdit captures a ui_graph_duplicate
action (Ctrl + D by default). In general, this signal indicates that the selected GraphElements should be duplicated.
public event Action DuplicateNodesRequest
Event Type
EndNodeMove
Emitted at the end of a GraphElement's movement.
public event Action EndNodeMove
Event Type
NodeDeselected
Emitted when the given GraphElement node is deselected.
public event GraphEdit.NodeDeselectedEventHandler NodeDeselected
Event Type
NodeSelected
Emitted when the given GraphElement node is selected.
public event GraphEdit.NodeSelectedEventHandler NodeSelected
Event Type
PasteNodesRequest
Emitted when this GraphEdit captures a ui_paste
action (Ctrl + V by default). In general, this signal indicates that previously copied GraphElements should be pasted.
public event Action PasteNodesRequest
Event Type
PopupRequest
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.
public event GraphEdit.PopupRequestEventHandler PopupRequest
Event Type
ScrollOffsetChanged
Emitted when the scroll offset is changed by the user. It will not be emitted when changed in code.
public event GraphEdit.ScrollOffsetChangedEventHandler ScrollOffsetChanged