// -------------------------------------------------------------------------------
        public void CleanupMuxPort(iCS_EditorObject port)
        {
            // Make certain we have the parent mux port.
            while (port.IsChildMuxPort)
            {
                port = port.Parent;
            }
            if (!port.IsParentMuxPort)
            {
                Debug.LogWarning("iCanScript: Invalid object given in CleanupMuxPort");
                return;
            }
            // Determine # of child mux ports.
            int nbChildPorts        = 0;
            iCS_EditorObject aChild = null;

            port.ForEachChildPort(
                c => {
                ++nbChildPorts;
                aChild = c;
            }
                );
            // Remove mux port if no children exist.
            if (nbChildPorts == 0)
            {
                DestroyInstance(port);
                return;
            }
            // Transform mux port to standard dynamic port if only one child port exist.
            if (nbChildPorts == 1)
            {
                var source = aChild.ProducerPort;
                DestroyInstance(aChild);
                if (source != null)
                {
                    port.ObjectType = port.IsOutMuxPort ? VSObjectType.OutDynamicDataPort : VSObjectType.InDynamicDataPort;
                    SetSource(port, source);
                    GraphEditor.AdjustPortIndexes(port.ParentNode);
                }
                else
                {
                    DestroyInstance(port);
                }
                return;
            }
            // Adjust the indexes of parent & child ports.
            int idx = 0;

            port.PortIndex = (int)iCS_PortIndex.Return;
            port.ForEachChildPort(
                c => {
                c.PortIndex = idx++;
            }
                );
        }
        // ----------------------------------------------------------------------
        public iCS_EditorObject[] GetCurrentProposedPorts(iCS_EditorObject node)
        {
            var proposedPorts = new List <iCS_EditorObject>();

            node.ForEachChildPort(p => { if (p.IsProposedDataPort)
                                         {
                                             proposedPorts.Add(p);
                                         }
                                  });
            return(proposedPorts.ToArray());
        }
        // ===================================================================
        // ENABLE PORTS
        // -------------------------------------------------------------------------
        /// Returns the list of all enable ports on the given node.
        ///
        /// @param node The node from which to extract the enable ports.
        /// @return The list of enable ports.
        ///
        public static iCS_EditorObject[] GetEnablePorts(iCS_EditorObject node)
        {
            var enables = new List <iCS_EditorObject>();

            node.ForEachChildPort(
                p => {
                if (p.IsEnablePort)
                {
                    enables.Add(p);
                }
            }
                );
            return(enables.ToArray());
        }
Example #4
0
        // ----------------------------------------------------------------------
        /// Creates a Unity event handler with Undo capabilities.
        ///
        /// @param parent The parent visual script object.
        /// @param globalPos The layout position for the event handler node.
        /// @param libraryEventHandler The library event handler object.
        /// @return The create Unity event handler node. _null_ on error.
        ///
        public static iCS_EditorObject CreateEventHandler(iCS_EditorObject parent, Vector2 globalPos, LibraryEventHandler libraryEventHandler)
        {
#if SHOW_DEBUG
            Debug.Log("iCanScript: Create Unity EVent Handler => " + libraryEventHandler.displayString);
#endif
            if (parent == null)
            {
                return(null);
            }
            if (!IsCreationAllowed())
            {
                return(null);
            }
            var iStorage = parent.IStorage;
            var nodeName = libraryEventHandler.nodeName;
            if (!iCS_AllowedChildren.CanAddChildNode(nodeName, VSObjectType.InstanceMessage, parent, iStorage))
            {
                return(null);
            }
            OpenTransaction(iStorage);
            iCS_EditorObject msgHandler = null;
            try {
                iStorage.AnimateGraph(null,
                                      _ => {
                    msgHandler = iStorage.CreateNode(parent.InstanceId, libraryEventHandler);
                    msgHandler.SetInitialPosition(globalPos);
                    msgHandler.ForEachChildPort(p => { p.AnimationStartRect = BuildRect(globalPos, Vector2.zero); });
                    iStorage.ForcedRelayoutOfTree();
                    iStorage.ReduceCollisionOffset();
                }
                                      );
            }
            catch (System.Exception) {
                CancelTransaction(iStorage);
                return(null);
            }
            if (msgHandler == null)
            {
                CancelTransaction(iStorage);
                return(null);
            }
            CloseTransaction(iStorage, "Create " + nodeName);
            SystemEvents.AnnounceVisualScriptElementAdded(msgHandler);
            return(msgHandler);
        }
Example #5
0
        // ======================================================================
        // High-order functions
        // -------------------------------------------------------------------------
        public int GetNextDynamicOrProposedPortIndex(iCS_EditorObject node)
        {
            // -- Assure that the port indexes are proper. --
            GraphEditor.AdjustPortIndexes(node);
            // -- Search for last parameter port. --
            int lastIdx = 0;

            node.ForEachChildPort(
                p => {
                var idx = p.PortIndex;
                if (idx < (int)iCS_PortIndex.ParametersEnd)
                {
                    if (lastIdx <= idx)
                    {
                        lastIdx = idx + 1;
                    }
                }
            }
                );
            return(lastIdx);
        }
Example #6
0
 // ----------------------------------------------------------------------
 public void RebuildConnectionsFor(iCS_EditorObject node)
 {
     // Rebuild connection from end-to-end.
     node.ForEachChildPort(
         p => {
         if (p.IsDataOrControlPort)
         {
             var outputPort = p.SegmentProducerPort;
             foreach (var inputPort in p.SegmentEndConsumerPorts)
             {
                 RebuildDataConnection(outputPort, inputPort);
             }
         }
         if (p.IsStatePort)
         {
             var fromState = GetFromStatePort(p);
             var toState   = GetToStatePort(p);
             RebuildStateConnection(fromState, toState);
         }
     }
         );
 }
Example #7
0
        // -------------------------------------------------------------------------
        // Wraps the given object in a package
        public iCS_EditorObject WrapInPackage(iCS_EditorObject obj)
        {
            if (obj == null || !obj.CanHavePackageAsParent())
            {
                return(null);
            }
            var parent  = obj.ParentNode;
            var package = CreatePackage(parent.InstanceId, obj.DisplayName);

            ChangeParent(obj, package);
            // Attempt to reposition the package ports to match the object ports.
            obj.ForEachChildPort(
                p => {
                var sourcePort = p.ProducerPort;
                if (sourcePort != null && sourcePort.ParentNode == package)
                {
                    sourcePort.Edge = p.Edge;
                    sourcePort.PortPositionRatio = p.PortPositionRatio;
                }
                else
                {
                    package.UntilMatchingChild(
                        pp => {
                        if (pp.ProducerPort == p)
                        {
                            pp.Edge = p.Edge;
                            pp.PortPositionRatio = p.PortPositionRatio;
                            return(true);
                        }
                        return(false);
                    }
                        );
                }
            }
                );
            ForcedRelayoutOfTree();
            return(package);
        }
Example #8
0
 // ----------------------------------------------------------------------
 public void AutoLayoutPortOnNode(iCS_EditorObject node)
 {
     node.ForEachChildPort(p => AutoLayoutPort(p));
 }