diff --git a/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs b/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs index 43321062c..6b0926aba 100644 --- a/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs +++ b/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs @@ -227,7 +227,8 @@ namespace FlaxEditor.Surface.Archetypes /// public override void OnDestroy() { - Surface.RemoveContext(this); + if (Surface != null) + Surface.RemoveContext(this); _maxTransitionsPerUpdate = null; _reinitializeOnBecomingRelevant = null; diff --git a/Source/Editor/Surface/Archetypes/Animation.cs b/Source/Editor/Surface/Archetypes/Animation.cs index 5d6041fcd..5cad7db6e 100644 --- a/Source/Editor/Surface/Archetypes/Animation.cs +++ b/Source/Editor/Surface/Archetypes/Animation.cs @@ -53,7 +53,8 @@ namespace FlaxEditor.Surface.Archetypes { base.OnSurfaceLoaded(); - UpdateTitle(); + if (Surface != null) + UpdateTitle(); } private void UpdateTitle() diff --git a/Source/Editor/Surface/Archetypes/Constants.cs b/Source/Editor/Surface/Archetypes/Constants.cs index 9d4a30459..a3f6df6d8 100644 --- a/Source/Editor/Surface/Archetypes/Constants.cs +++ b/Source/Editor/Surface/Archetypes/Constants.cs @@ -309,6 +309,8 @@ namespace FlaxEditor.Surface.Archetypes _output.CurrentType = dictionaryType; _isUpdatingUI = false; + if (Surface == null) + return; var canEdit = Surface.CanEdit; _keyTypePicker.Enabled = canEdit; _valueTypePicker.Enabled = canEdit; diff --git a/Source/Editor/Surface/Archetypes/Function.cs b/Source/Editor/Surface/Archetypes/Function.cs index 73c9c8a88..b72737f6e 100644 --- a/Source/Editor/Surface/Archetypes/Function.cs +++ b/Source/Editor/Surface/Archetypes/Function.cs @@ -380,16 +380,19 @@ namespace FlaxEditor.Surface.Archetypes public FunctionInputNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch) : base(id, context, nodeArch, groupArch) { - _types = ((IFunctionSurface)Surface).FunctionTypes; - _typePicker = new ComboBox + if (Surface is IFunctionSurface surface) { - Location = new Vector2(4, 32), - Width = 80.0f, - Parent = this, - }; - for (int i = 0; i < _types.Length; i++) - _typePicker.AddItem(Surface.GetTypeName(new ScriptType(_types[i]))); - _nameField.Location = new Vector2(_typePicker.Right + 2.0f, _typePicker.Y); + _types = surface.FunctionTypes; + _typePicker = new ComboBox + { + Location = new Vector2(4, 32), + Width = 80.0f, + Parent = this, + }; + for (int i = 0; i < _types.Length; i++) + _typePicker.AddItem(Surface.GetTypeName(new ScriptType(_types[i]))); + _nameField.Location = new Vector2(_typePicker.Right + 2.0f, _typePicker.Y); + } } /// @@ -401,8 +404,11 @@ namespace FlaxEditor.Surface.Archetypes _outputBox.CurrentType = SignatureType; _defaultValueBox = GetBox(1); _defaultValueBox.CurrentType = _outputBox.CurrentType; - _typePicker.SelectedIndex = Array.IndexOf(_types, _outputBox.CurrentType.Type); - _typePicker.SelectedIndexChanged += OnTypePickerSelectedIndexChanged; + if (_typePicker != null) + { + _typePicker.SelectedIndex = Array.IndexOf(_types, _outputBox.CurrentType.Type); + _typePicker.SelectedIndexChanged += OnTypePickerSelectedIndexChanged; + } } private void OnTypePickerSelectedIndexChanged(ComboBox picker) @@ -417,7 +423,8 @@ namespace FlaxEditor.Surface.Archetypes _outputBox.CurrentType = SignatureType; _defaultValueBox.CurrentType = _outputBox.CurrentType; - _typePicker.SelectedIndex = Array.IndexOf(_types, _outputBox.CurrentType.Type); + if (_typePicker != null) + _typePicker.SelectedIndex = Array.IndexOf(_types, _outputBox.CurrentType.Type); } /// @@ -442,16 +449,19 @@ namespace FlaxEditor.Surface.Archetypes public FunctionOutputNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch) : base(id, context, nodeArch, groupArch) { - _types = ((IFunctionSurface)Surface).FunctionTypes; - _typePicker = new ComboBox + if (Surface is IFunctionSurface surface) { - Location = new Vector2(24, 32), - Width = 80.0f, - Parent = this, - }; - for (int i = 0; i < _types.Length; i++) - _typePicker.AddItem(Surface.GetTypeName(new ScriptType(_types[i]))); - _nameField.Location = new Vector2(_typePicker.Right + 2.0f, _typePicker.Y); + _types = surface.FunctionTypes; + _typePicker = new ComboBox + { + Location = new Vector2(24, 32), + Width = 80.0f, + Parent = this, + }; + for (int i = 0; i < _types.Length; i++) + _typePicker.AddItem(Surface.GetTypeName(new ScriptType(_types[i]))); + _nameField.Location = new Vector2(_typePicker.Right + 2.0f, _typePicker.Y); + } } /// @@ -461,8 +471,11 @@ namespace FlaxEditor.Surface.Archetypes _inputBox = GetBox(0); _inputBox.CurrentType = SignatureType; - _typePicker.SelectedIndex = Array.IndexOf(_types, _inputBox.CurrentType.Type); - _typePicker.SelectedIndexChanged += OnTypePickerSelectedIndexChanged; + if (_typePicker != null) + { + _typePicker.SelectedIndex = Array.IndexOf(_types, _inputBox.CurrentType.Type); + _typePicker.SelectedIndexChanged += OnTypePickerSelectedIndexChanged; + } } private void OnTypePickerSelectedIndexChanged(ComboBox picker) @@ -476,7 +489,8 @@ namespace FlaxEditor.Surface.Archetypes base.OnValuesChanged(); _inputBox.CurrentType = SignatureType; - _typePicker.SelectedIndex = Array.IndexOf(_types, _inputBox.CurrentType.Type); + if (_typePicker != null) + _typePicker.SelectedIndex = Array.IndexOf(_types, _inputBox.CurrentType.Type); } /// @@ -505,8 +519,8 @@ namespace FlaxEditor.Surface.Archetypes { _parameters = null; var methodInfo = ScriptMemberInfo.Null; - var surface = (VisualScriptSurface)Surface; - if (surface.Script && !surface.Script.WaitForLoaded(100)) + var surface = Surface as VisualScriptSurface; + if (surface != null && surface.Script && !surface.Script.WaitForLoaded(100)) { var scriptMeta = surface.Script.Meta; var scriptType = TypeUtils.GetType(scriptMeta.BaseTypename); @@ -520,7 +534,7 @@ namespace FlaxEditor.Surface.Archetypes } } } - if (!_isTypesChangedEventRegistered) + if (!_isTypesChangedEventRegistered && surface != null) { _isTypesChangedEventRegistered = true; Editor.Instance.CodeEditing.TypesChanged += UpdateSignature; @@ -976,7 +990,8 @@ namespace FlaxEditor.Surface.Archetypes if (method) { TooltipText = SurfaceUtils.GetVisualScriptMemberInfoDescription(method); - SetValue(4, GetSignatureData(method, parameters)); + if (Surface != null) + SetValue(4, GetSignatureData(method, parameters)); } ResizeAuto(); @@ -1656,9 +1671,9 @@ namespace FlaxEditor.Surface.Archetypes LoadSignature(); // Send event - for (int i = 0; i < Surface.Nodes.Count; i++) + for (int i = 0; i < Context.Nodes.Count; i++) { - if (Surface.Nodes[i] is IFunctionsDependantNode node) + if (Context.Nodes[i] is IFunctionsDependantNode node) node.OnFunctionCreated(this); } } @@ -1754,7 +1769,7 @@ namespace FlaxEditor.Surface.Archetypes } } } - if (!_isTypesChangedEventRegistered) + if (!_isTypesChangedEventRegistered && Surface != null) { _isTypesChangedEventRegistered = true; Editor.Instance.CodeEditing.TypesChanged += UpdateSignature; @@ -1765,8 +1780,11 @@ namespace FlaxEditor.Surface.Archetypes { type = fieldInfo.ValueType; isStatic = fieldInfo.IsStatic; - SetValue(2, type.TypeName); - SetValue(3, isStatic); + if (Surface != null) + { + SetValue(2, type.TypeName); + SetValue(3, isStatic); + } } else { diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 424511cce..d492f8be2 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -65,7 +65,7 @@ namespace FlaxEditor.Surface.Archetypes { // Try get parent material window // Maybe too hacky :D - if (!(Surface.Owner is MaterialWindow materialWindow) || materialWindow.Item == null) + if (!(Surface?.Owner is MaterialWindow materialWindow) || materialWindow.Item == null) return; // Layered material diff --git a/Source/Editor/Surface/Archetypes/Parameters.cs b/Source/Editor/Surface/Archetypes/Parameters.cs index 10748c447..89118c026 100644 --- a/Source/Editor/Surface/Archetypes/Parameters.cs +++ b/Source/Editor/Surface/Archetypes/Parameters.cs @@ -312,8 +312,11 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - UpdateCombo(); - UpdateLayout(); + if (Surface != null) + { + UpdateCombo(); + UpdateLayout(); + } } /// @@ -321,7 +324,10 @@ namespace FlaxEditor.Surface.Archetypes { base.OnSurfaceLoaded(); - UpdateTitle(); + if (Surface != null) + { + UpdateTitle(); + } } /// @@ -601,8 +607,11 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - UpdateCombo(); - UpdateUI(); + if (Surface != null) + { + UpdateCombo(); + UpdateUI(); + } } /// @@ -610,7 +619,10 @@ namespace FlaxEditor.Surface.Archetypes { base.OnSurfaceLoaded(); - UpdateUI(); + if (Surface != null) + { + UpdateUI(); + } } /// diff --git a/Source/Editor/Surface/Archetypes/ParticleModules.cs b/Source/Editor/Surface/Archetypes/ParticleModules.cs index 916ed76ea..9c4da4b8e 100644 --- a/Source/Editor/Surface/Archetypes/ParticleModules.cs +++ b/Source/Editor/Surface/Archetypes/ParticleModules.cs @@ -275,7 +275,7 @@ namespace FlaxEditor.Surface.Archetypes base.OnSurfaceLoaded(); - ParticleSurface.ArrangeModulesNodes(); + ParticleSurface?.ArrangeModulesNodes(); } /// diff --git a/Source/Editor/Surface/Archetypes/Particles.cs b/Source/Editor/Surface/Archetypes/Particles.cs index 17f96228f..a0714f15e 100644 --- a/Source/Editor/Surface/Archetypes/Particles.cs +++ b/Source/Editor/Surface/Archetypes/Particles.cs @@ -218,6 +218,9 @@ namespace FlaxEditor.Surface.Archetypes { base.OnSurfaceLoaded(); + if (Surface == null) + return; + // Always keep root node in the back (modules with lay on top of it) IndexInParent = 0; @@ -231,7 +234,7 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLocationChanged(); - if (Surface != null && ParticleSurface._rootNode == this) + if (ParticleSurface != null && ParticleSurface._rootNode == this) { // Update modules to match root node location ParticleSurface.ArrangeModulesNodes(); @@ -242,7 +245,8 @@ namespace FlaxEditor.Surface.Archetypes public override void OnDestroy() { // Unlink - ParticleSurface._rootNode = null; + if (ParticleSurface != null) + ParticleSurface._rootNode = null; base.OnDestroy(); } diff --git a/Source/Editor/Surface/Archetypes/Tools.cs b/Source/Editor/Surface/Archetypes/Tools.cs index 3bf8909b6..7cbdff858 100644 --- a/Source/Editor/Surface/Archetypes/Tools.cs +++ b/Source/Editor/Surface/Archetypes/Tools.cs @@ -685,10 +685,10 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - var surface = (VisualScriptSurface)Context.Surface; - var type = TypeUtils.GetType(surface.Script.ScriptTypeName); - var box = (OutputBox)GetBox(0); - box.CurrentType = type ? type : new ScriptType(typeof(VisualScript)); + var type = ScriptType.Null; + if (Context.Surface is VisualScriptSurface visjectSurface) + type = TypeUtils.GetType(visjectSurface.Script.ScriptTypeName); + GetBox(0).CurrentType = type ? type : new ScriptType(typeof(VisualScript)); } } @@ -825,7 +825,8 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - _picker.ValueTypeName = (string)Values[0]; + if (Surface != null) + _picker.ValueTypeName = (string)Values[0]; UpdateOutputBox(); } @@ -883,7 +884,8 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - _picker.ValueTypeName = (string)Values[0]; + if (Surface != null) + _picker.ValueTypeName = (string)Values[0]; } /// @@ -933,7 +935,8 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - _picker.ValueTypeName = (string)Values[0]; + if (Surface != null) + _picker.ValueTypeName = (string)Values[0]; } /// @@ -984,7 +987,8 @@ namespace FlaxEditor.Surface.Archetypes { base.OnLoaded(); - _picker.ValueTypeName = (string)Values[0]; + if (Surface != null) + _picker.ValueTypeName = (string)Values[0]; UpdateOutputBox(); } diff --git a/Source/Editor/Surface/Elements/Box.cs b/Source/Editor/Surface/Elements/Box.cs index cf133d4c9..89028fce1 100644 --- a/Source/Editor/Surface/Elements/Box.cs +++ b/Source/Editor/Surface/Elements/Box.cs @@ -91,7 +91,7 @@ namespace FlaxEditor.Surface.Elements _currentType = value; // Check if will need to update box connections due to type change - if (Surface._isUpdatingBoxTypes == 0 && HasAnyConnection && !CanCast(prev, _currentType)) + if ((Surface == null || Surface._isUpdatingBoxTypes == 0) && HasAnyConnection && !CanCast(prev, _currentType)) { // Remove all invalid connections and update those which still can be valid var connections = Connections.ToArray(); @@ -181,9 +181,12 @@ namespace FlaxEditor.Surface.Elements { _currentType = DefaultType; Text = Archetype.Text; - var hints = parentNode.Archetype.ConnectionsHints; - Surface.Style.GetConnectionColor(_currentType, hints, out _currentTypeColor); - TooltipText = Surface.GetTypeName(CurrentType) ?? GetConnectionHintTypeName(hints); + if (Surface != null) + { + var hints = parentNode.Archetype.ConnectionsHints; + Surface.Style.GetConnectionColor(_currentType, hints, out _currentTypeColor); + TooltipText = Surface.GetTypeName(CurrentType) ?? GetConnectionHintTypeName(hints); + } } private static string GetConnectionHintTypeName(ConnectionsHint hint) @@ -215,9 +218,15 @@ namespace FlaxEditor.Surface.Elements public bool CanUseType(ScriptType type) { // Check direct connection - if (Surface.CanUseDirectCast(type, _currentType)) + if (Surface != null) { - return true; + if (Surface.CanUseDirectCast(type, _currentType)) + return true; + } + else + { + if (VisjectSurface.CanUseDirectCastStatic(type, _currentType)) + return true; } // Check using connection hints @@ -464,9 +473,12 @@ namespace FlaxEditor.Surface.Elements /// protected virtual void OnCurrentTypeChanged() { - var hints = ParentNode.Archetype.ConnectionsHints; - Surface.Style.GetConnectionColor(_currentType, hints, out _currentTypeColor); - TooltipText = Surface.GetTypeName(CurrentType) ?? GetConnectionHintTypeName(hints); + if (Surface != null) + { + var hints = ParentNode.Archetype.ConnectionsHints; + Surface.Style.GetConnectionColor(_currentType, hints, out _currentTypeColor); + TooltipText = Surface.GetTypeName(CurrentType) ?? GetConnectionHintTypeName(hints); + } CurrentTypeChanged?.Invoke(this); } diff --git a/Source/Editor/Surface/Elements/SkeletonBoneIndexSelectElement.cs b/Source/Editor/Surface/Elements/SkeletonBoneIndexSelectElement.cs index 12e77d062..367079a62 100644 --- a/Source/Editor/Surface/Elements/SkeletonBoneIndexSelectElement.cs +++ b/Source/Editor/Surface/Elements/SkeletonBoneIndexSelectElement.cs @@ -18,6 +18,8 @@ namespace FlaxEditor.Surface.Elements { _isAutoSelect = true; + if (Surface == null) + return; UpdateComboBox(); // Select saved value diff --git a/Source/Editor/Surface/Elements/SkeletonNodeNameSelectElement.cs b/Source/Editor/Surface/Elements/SkeletonNodeNameSelectElement.cs index 4a37ae365..7cdfc5579 100644 --- a/Source/Editor/Surface/Elements/SkeletonNodeNameSelectElement.cs +++ b/Source/Editor/Surface/Elements/SkeletonNodeNameSelectElement.cs @@ -45,6 +45,8 @@ namespace FlaxEditor.Surface.Elements { _isAutoSelect = true; + if (Surface == null) + return; UpdateComboBox(); // Select saved value diff --git a/Source/Editor/Surface/SurfaceNode.cs b/Source/Editor/Surface/SurfaceNode.cs index 1fc6fc62c..62e3e35e8 100644 --- a/Source/Editor/Surface/SurfaceNode.cs +++ b/Source/Editor/Surface/SurfaceNode.cs @@ -158,6 +158,8 @@ namespace FlaxEditor.Surface /// The height. public void Resize(float width, float height) { + if (Surface == null) + return; Size = CalculateNodeSize(width, height); // Update boxes on width change @@ -178,6 +180,8 @@ namespace FlaxEditor.Surface /// public void ResizeAuto() { + if (Surface == null) + return; var width = 0.0f; var height = 0.0f; var leftHeight = 0.0f; @@ -462,10 +466,21 @@ namespace FlaxEditor.Surface } // Check if that type if part of default type - if (Surface.CanUseDirectCast(type, b.Connections[0].DefaultType)) + if (Surface != null) { - type = b.Connections[0].CurrentType; - break; + if (Surface.CanUseDirectCast(type, b.Connections[0].DefaultType)) + { + type = b.Connections[0].CurrentType; + break; + } + } + else + { + if (VisjectSurface.CanUseDirectCastStatic(type, b.Connections[0].DefaultType)) + { + type = b.Connections[0].CurrentType; + break; + } } } } @@ -891,7 +906,7 @@ namespace FlaxEditor.Surface /// True if graph has been edited (nodes structure or parameter value). public virtual void SetValue(int index, object value, bool graphEdited = true) { - if (_isDuringValuesEditing || !Surface.CanEdit) + if (_isDuringValuesEditing || (Surface != null && !Surface.CanEdit)) return; if (FlaxEngine.Json.JsonSerializer.ValueEquals(value, Values[index])) return; @@ -900,13 +915,13 @@ namespace FlaxEditor.Surface _isDuringValuesEditing = true; - var before = Surface.Undo != null ? (object[])Values.Clone() : null; + var before = Surface?.Undo != null ? (object[])Values.Clone() : null; Values[index] = value; OnValuesChanged(); - Surface.MarkAsEdited(graphEdited); + Surface?.MarkAsEdited(graphEdited); - Surface.Undo?.AddAction(new EditNodeValuesAction(this, before, graphEdited)); + Surface?.Undo?.AddAction(new EditNodeValuesAction(this, before, graphEdited)); _isDuringValuesEditing = false; } @@ -948,7 +963,7 @@ namespace FlaxEditor.Surface public virtual void OnValuesChanged() { ValuesChanged?.Invoke(); - Surface.OnNodeValuesEdited(this); + Surface?.OnNodeValuesEdited(this); } /// diff --git a/Source/Editor/Surface/VisjectSurface.Connecting.cs b/Source/Editor/Surface/VisjectSurface.Connecting.cs index fb6bc9f84..bb1fbaa45 100644 --- a/Source/Editor/Surface/VisjectSurface.Connecting.cs +++ b/Source/Editor/Surface/VisjectSurface.Connecting.cs @@ -47,13 +47,7 @@ namespace FlaxEditor.Surface public partial class VisjectSurface { - /// - /// Checks if can use direct conversion from one type to another. - /// - /// Source type. - /// Target type. - /// True if can use direct conversion, otherwise false. - public bool CanUseDirectCast(ScriptType from, ScriptType to) + internal static bool CanUseDirectCastStatic(ScriptType from, ScriptType to, bool supportsImplicitCastFromObjectToBoolean = true) { if (from == ScriptType.Null || to == ScriptType.Null) return false; @@ -76,7 +70,7 @@ namespace FlaxEditor.Surface // Implicit casting is supported for object reference to test whenever it is valid var toType = to.Type; - if (_supportsImplicitCastFromObjectToBoolean && toType == typeof(bool) && ScriptType.FlaxObject.IsAssignableFrom(from)) + if (supportsImplicitCastFromObjectToBoolean && toType == typeof(bool) && ScriptType.FlaxObject.IsAssignableFrom(from)) { return true; } @@ -124,6 +118,17 @@ namespace FlaxEditor.Surface return result; } + /// + /// Checks if can use direct conversion from one type to another. + /// + /// Source type. + /// Target type. + /// True if can use direct conversion, otherwise false. + public bool CanUseDirectCast(ScriptType from, ScriptType to) + { + return CanUseDirectCastStatic(from, to, _supportsImplicitCastFromObjectToBoolean); + } + /// /// Begins connecting surface objects action. /// diff --git a/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs b/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs index 560a706e4..e6bef340c 100644 --- a/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs +++ b/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs @@ -77,7 +77,8 @@ namespace FlaxEditor.Surface /// True if failed, otherwise false. public bool Load() { - Surface._isUpdatingBoxTypes++; + if (_surface != null) + _surface._isUpdatingBoxTypes++; try { @@ -178,7 +179,8 @@ namespace FlaxEditor.Surface } finally { - Surface._isUpdatingBoxTypes--; + if (_surface != null) + _surface._isUpdatingBoxTypes--; } return false; @@ -445,6 +447,9 @@ namespace FlaxEditor.Surface { // IMPORTANT! This must match C++ Graph format + var nodeArchetypes = _surface?.NodeArchetypes ?? NodeFactory.DefaultGroups; + var customNodes = _surface?.GetCustomNodes(); + // Magic Code int tmp = stream.ReadInt32(); if (tmp != 1963542358) @@ -489,7 +494,7 @@ namespace FlaxEditor.Surface if (groupId == Archetypes.Custom.GroupID) node = new DummyCustomNode(id, this); else - node = NodeFactory.CreateNode(_surface.NodeArchetypes, id, this, groupId, typeId); + node = NodeFactory.CreateNode(nodeArchetypes, id, this, groupId, typeId); if (node == null) node = new MissingNode(id, this, groupId, typeId); Nodes.Add(node); @@ -550,7 +555,6 @@ namespace FlaxEditor.Surface string typeName = typeNameValue as string ?? string.Empty; // Find custom node archetype that matches this node type (it must be unique) - var customNodes = _surface.GetCustomNodes(); if (customNodes?.Archetypes != null && typeName.Length != 0) { NodeArchetype arch = null; @@ -674,7 +678,7 @@ namespace FlaxEditor.Surface if (groupId == Archetypes.Custom.GroupID) node = new DummyCustomNode(id, this); else - node = NodeFactory.CreateNode(_surface.NodeArchetypes, id, this, groupId, typeId); + node = NodeFactory.CreateNode(nodeArchetypes, id, this, groupId, typeId); if (node == null) node = new MissingNode(id, this, groupId, typeId); Nodes.Add(node); @@ -722,7 +726,6 @@ namespace FlaxEditor.Surface string typeName = typeNameValue as string ?? string.Empty; // Find custom node archetype that matches this node type (it must be unique) - var customNodes = _surface.GetCustomNodes(); if (customNodes?.Archetypes != null && typeName.Length != 0) { NodeArchetype arch = null; @@ -856,7 +859,7 @@ namespace FlaxEditor.Surface { control.OnSpawned(); ControlSpawned?.Invoke(control); - if (control is SurfaceNode node) + if (Surface != null && control is SurfaceNode node) Surface.OnNodeSpawned(node); } diff --git a/Source/Editor/Surface/VisjectSurfaceContext.cs b/Source/Editor/Surface/VisjectSurfaceContext.cs index 60b18eb8a..acb98b6ca 100644 --- a/Source/Editor/Surface/VisjectSurfaceContext.cs +++ b/Source/Editor/Surface/VisjectSurfaceContext.cs @@ -335,7 +335,8 @@ namespace FlaxEditor.Surface /// Created node. public SurfaceNode SpawnNode(ushort groupID, ushort typeID, Vector2 location, object[] customValues = null, Action beforeSpawned = null) { - if (NodeFactory.GetArchetype(_surface.NodeArchetypes, groupID, typeID, out var groupArchetype, out var nodeArchetype)) + var nodeArchetypes = _surface?.NodeArchetypes ?? NodeFactory.DefaultGroups; + if (NodeFactory.GetArchetype(nodeArchetypes, groupID, typeID, out var groupArchetype, out var nodeArchetype)) { return SpawnNode(groupArchetype, nodeArchetype, location, customValues, beforeSpawned); } @@ -360,7 +361,7 @@ namespace FlaxEditor.Surface var flags = nodeArchetype.Flags; nodeArchetype.Flags &= ~NodeFlags.NoSpawnViaGUI; nodeArchetype.Flags &= ~NodeFlags.NoSpawnViaPaste; - if (!_surface.CanUseNodeType(nodeArchetype)) + if (_surface != null && !_surface.CanUseNodeType(nodeArchetype)) { nodeArchetype.Flags = flags; Editor.LogWarning("Cannot spawn given node type. Title: " + nodeArchetype.Title); @@ -394,7 +395,8 @@ namespace FlaxEditor.Surface OnControlSpawned(node); // Undo action - Surface.Undo?.AddAction(new AddRemoveNodeAction(node, true)); + if (Surface != null && Surface.Undo != null) + Surface.Undo.AddAction(new AddRemoveNodeAction(node, true)); MarkAsModified();