diff --git a/Source/Editor/Surface/Archetypes/Tools.cs b/Source/Editor/Surface/Archetypes/Tools.cs index 9f6db7ea4..201a29418 100644 --- a/Source/Editor/Surface/Archetypes/Tools.cs +++ b/Source/Editor/Surface/Archetypes/Tools.cs @@ -787,7 +787,7 @@ namespace FlaxEditor.Surface.Archetypes } } - private class AsNode : SurfaceNode + internal class AsNode : SurfaceNode { private TypePickerControl _picker; @@ -838,6 +838,15 @@ namespace FlaxEditor.Surface.Archetypes box.CurrentType = type ? type : ScriptType.FlaxObject; } + /// + /// Sets the type of the picker and the type of the output box + /// + /// Target Type + public void SetPickerValue(ScriptType type) + { + _picker.Value = type; + } + /// public override void OnDestroy() { @@ -999,6 +1008,15 @@ namespace FlaxEditor.Surface.Archetypes GetBox(4).CurrentType = type ? type : _picker.Type; } + /// + /// Sets the type of the picker and the type of the output box + /// + /// Target Type + public void SetPickerValue(ScriptType type) + { + _picker.Value = type; + } + /// public override void OnDestroy() { diff --git a/Source/Editor/Surface/Elements/Box.cs b/Source/Editor/Surface/Elements/Box.cs index a03c8f701..87cafd64b 100644 --- a/Source/Editor/Surface/Elements/Box.cs +++ b/Source/Editor/Surface/Elements/Box.cs @@ -820,8 +820,70 @@ namespace FlaxEditor.Surface.Elements if (useCaster) { // Connect via Caster - //AddCaster(oB, iB); - throw new NotImplementedException("AddCaster(..) function"); + const float casterXOffset = 250; + if (Surface.Undo != null && Surface.Undo.Enabled) + { + bool undoEnabled = Surface.Undo.Enabled; + Surface.Undo.Enabled = false; + SurfaceNode node = Surface.Context.SpawnNode(7, 22, Float2.Zero); // 22 AsNode, 25 CastNode + Surface.Undo.Enabled = undoEnabled; + + if(node is not Archetypes.Tools.AsNode castNode) + throw new Exception("Node is not a casting node!"); + + // Set the type of the casting node + undoEnabled = castNode.Surface.Undo.Enabled; + castNode.Surface.Undo.Enabled = false; + castNode.SetPickerValue(iB.CurrentType); + castNode.Surface.Undo.Enabled = undoEnabled; + + if (node.GetBox(0) is not OutputBox castOutputBox || node.GetBox(1) is not InputBox castInputBox) + { + throw new NullReferenceException("Casting failed. Cast node is invalid!"); + } + + // We set the position of the cast node here to set it relative to the target nodes input box + undoEnabled = castNode.Surface.Undo.Enabled; + castNode.Surface.Undo.Enabled = false; + var wantedOffset = iB.ParentNode.Location - new Float2(casterXOffset, -(iB.LocalY - castOutputBox.LocalY)); + castNode.Location = Surface.Root.PointFromParent(ref wantedOffset); + castNode.Surface.Undo.Enabled = undoEnabled; + + var spawnNodeAction = new AddRemoveNodeAction(castNode, true); + + var connectToCastNodeAction = new ConnectBoxesAction(castInputBox, oB, true); + castInputBox.CreateConnection(oB); + connectToCastNodeAction.End(); + + var connectCastToTargetNodeAction = new ConnectBoxesAction(iB, castOutputBox, true); + iB.CreateConnection(castOutputBox); + connectCastToTargetNodeAction.End(); + + Surface.AddBatchedUndoAction(new MultiUndoAction(spawnNodeAction, connectToCastNodeAction, connectCastToTargetNodeAction)); + } + else + { + SurfaceNode node = Surface.Context.SpawnNode(7, 22, Float2.Zero); // 22 AsNode, 25 CastNode + + if(node is not Archetypes.Tools.AsNode castNode) + throw new Exception("Node is not a casting node!"); + + // Set the type of the casting node + castNode.SetPickerValue(iB.CurrentType); + + if (node.GetBox(0) is not OutputBox castOutputBox || node.GetBox(1) is not InputBox castInputBox) + { + throw new NullReferenceException("Casting failed. Cast node is invalid!"); + } + + // We set the position of the cast node here to set it relative to the target nodes input box + var wantedOffset = iB.ParentNode.Location - new Float2(casterXOffset, -(iB.LocalY - castOutputBox.LocalY)); + castNode.Location = Surface.Root.PointFromParent(ref wantedOffset); + + castInputBox.CreateConnection(oB); + iB.CreateConnection(castOutputBox); + } + Surface.MarkAsEdited(); } else {