addNode method
This method is used to add a NodeInstance to the node editor by its prototype name.
The method takes the name of the node prototype and creates an instance of the node based on the prototype. The method also takes an optional offset parameter to set the initial position of the node in the node editor. The node is also inserted into the spatial hash grid for efficient querying of nodes based on their positions
See SpatialHashGrid and selectNodesByArea.
Emits an AddNodeEvent event.
Implementation
NodeInstance addNode(String name, {Offset offset = Offset.zero}) {
if (!nodePrototypes.containsKey(name)) {
throw Exception('Node prototype $name does not exist.');
}
if (config.enableSnapToGrid) {
offset = Offset(
(offset.dx / config.snapToGridSize).round() * config.snapToGridSize,
(offset.dy / config.snapToGridSize).round() * config.snapToGridSize,
);
}
final instance = createNode(
nodePrototypes[name]!,
controller: this,
offset: offset,
);
nodes.putIfAbsent(instance.id, () => instance);
_unboundNodeOffsets.putIfAbsent(instance.id, () => instance.offset);
nodesDataDirty = true;
eventBus.emit(
AddNodeEvent(id: const Uuid().v4(), instance),
);
return instance;
}