Exemplo n.º 1
0
        // ===================================================================
        /// Returns the allowed types of variable the given port can support.
        ///
        /// @param port The port used to filter the types of variable.
        /// @return The filtered port specification.
        ///
        public static Enum GetAllowedPortSpecification(iCS_EditorObject port)
        {
            var producerPort = GetProducerPort(port);

            if (MustBeATypeVariable(producerPort))
            {
                return(TypeVariables.PublicVariable);
            }
            if (MustBeAParameter(producerPort))
            {
                return(ParameterVariable.Parameter);
            }
            if (producerPort.IsInDataOrControlPort)
            {
                var isFunctionDefinition = producerPort.ParentNode.IsFunctionDefinition;
                var runtimeType          = producerPort.RuntimeType;
                if (iCS_Types.IsA <UnityEngine.Object>(runtimeType))
                {
                    if (runtimeType == typeof(GameObject) ||
                        runtimeType == typeof(Transform))
                    {
                        if (isFunctionDefinition)
                        {
                            return(InFunctionDefinitionOwnerAndUnityObjectVariableType.PublicVariable);
                        }
                        return(InOwnerAndUnityObjectVariableType.PublicVariable);
                    }
                    else
                    {
                        if (isFunctionDefinition)
                        {
                            return(InFunctionDefinitionUnityObjectVariableType.PublicVariable);
                        }
                        if (GraphInfo.IsLocalType(producerPort))
                        {
                            return(InOwnerAndUnityObjectVariableType.Owner);
                        }
                        return(InUnityObjectVariableType.PublicVariable);
                    }
                }
                else
                {
                    if (isFunctionDefinition)
                    {
                        return(InFunctionDefinitionVariableType.PublicVariable);
                    }
                    if (GraphInfo.IsLocalType(producerPort))
                    {
                        return(InTypeVariableWithOwner.Owner);
                    }
                    return(InVariableType.PublicVariable);
                }
            }
            else if (producerPort.IsOutDataOrControlPort)
            {
                return(OutVariableType.PublicVariable);
            }

            return(TypeVariables.PublicVariable);
        }
Exemplo n.º 2
0
        // ======================================================================
        /// Sets the default port specification for the given port.
        ///
        /// @param p The port on which to set the default port specification.
        ///
        public static void SetDefaultPortSpec(iCS_EditorObject p)
        {
            // -- Setup spec for control ports. --
            var parentNode   = p.ParentNode;
            var producerPort = GraphInfo.GetProducerPort(p);

            // -- Only valid for the producer port. --
            if (producerPort != p)
            {
                return;
            }
            // -- Determine default port spec. --
            if (parentNode.IsFunctionDefinition)
            {
                if (p.IsInDataOrControlPort)
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.Parameter);
                }
            }
            else if (parentNode.IsEventHandler)
            {
                if (p.IsFixDataPort)
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.Parameter);
                }
                else
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.PublicVariable);
                }
            }
            else if (parentNode.IsVariableDefinition)
            {
                if (p.IsOutDataOrControlPort)
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.PublicVariable);
                }
                else if (p.IsInDataOrControlPort)
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.Constant);
                }
            }
            else if (parentNode.IsConstructor)
            {
                if (p.IsInDataOrControlPort)
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.Constant);
                }
                else if (p.IsOutDataOrControlPort)
                {
                    // -- Determine if this is a local variable of not. --
                    bool isLocal = false;
                    parentNode.ForEachChildPort(
                        cp => {
                        if (cp.IsInDataOrControlPort)
                        {
                            if (cp.ProducerPort != null)
                            {
                                isLocal = true;
                            }
                        }
                    }
                        );
                    if (isLocal)
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.LocalVariable);
                    }
                    else
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.PublicVariable);
                    }
                }
            }
            // TODO: Needs to be verified...
            else if (parentNode.IsKindOfFunction)
            {
                if (p.IsInDataOrControlPort)
                {
                    var initialValue = p.Value;
                    if (initialValue != null)
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.Constant);
                    }
                    else
                    {
                        var runtimeType = p.RuntimeType;
                        if (p.IsTargetPort &&
                            (runtimeType == typeof(GameObject) ||
                             runtimeType == typeof(Transform) ||
                             GraphInfo.IsLocalType(p)))
                        {
                            GraphEditor.SetPortSpec(p, PortSpecification.Owner);
                            p.Value = null;
                        }
                        else
                        {
                            GraphEditor.SetPortSpec(p, PortSpecification.PublicVariable);
                        }
                    }
                }
                else if (p.IsOutDataOrControlPort)
                {
                    if (GraphInfo.MustBeATypeVariable(p))
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.PrivateVariable);
                    }
                    else
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.LocalVariable);
                    }
                }
            }
            else if (parentNode.IsInstanceNode)
            {
                if (p.IsInDataOrControlPort)
                {
                    var runtimeType = p.RuntimeType;
                    if (p.IsTargetPort &&
                        (runtimeType == typeof(GameObject) ||
                         runtimeType == typeof(Transform) ||
                         GraphInfo.IsLocalType(p)))
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.Owner);
                        p.Value = null;
                    }
                    else
                    {
                        GraphEditor.SetPortSpec(p, PortSpecification.PublicVariable);
                    }
                }
            }
            else if (parentNode.IsKindOfPackage)
            {
                if (p.IsInDataOrControlPort)
                {
                    GraphEditor.SetPortSpec(p, PortSpecification.PublicVariable);
                }
            }
            else
            {
                GraphEditor.SetPortSpec(p, PortSpecification.LocalVariable);
            }
        }
Exemplo n.º 3
0
        // ----------------------------------------------------------------------
        /// Performs a snaity check on the visual script object.
        ///
        /// @param serviceKey The service key to use when registering with the
        ///					  error controller.
        /// @return _true_ is return if object is sane. _false_ otherwise.
        ///
        public bool SanityCheck(string serviceKey)
        {
            var visualScript = IStorage.VisualScript;

            // Verify that the runtime portion still exists.
            if (IsKindOfFunction)
            {
                var runtimeType = RuntimeType;
                var memberInfo  = runtimeType != null?LibraryController.LibraryDatabase.GetLibraryType(runtimeType) : null;

                if (memberInfo == null && !GraphInfo.IsLocalType(this))
                {
                    var message = "Unable to find the runtime code for " + FullName;
                    ErrorController.AddError(serviceKey, message, visualScript, InstanceId);
                    return(false);
                }
            }
            if (IsTargetPort)
            {
                if (ProducerPort == null || ProducerPort == this)
                {
                    var parentNode = ParentNode;
                    if (parentNode.IsKindOfFunction)
                    {
                        if (!IsOwner && !IsTypeVariable)
                        {
                            var message = "<color=red><b>Value</b></color> for <b>Target</b> port is not valid for node: <b>" + FullName + "</b>";
                            ErrorController.AddError(serviceKey, message, visualScript, InstanceId);
                            return(false);
                        }
                        else
                        {
                            if (parentNode.IsFunction("get_transform", "GameObject", "UnityEngine"))
                            {
                                var message = "When <b>'Target'</b> is set to <b>'Owner'</b>, the generated code has no effect.  The node can be removed safely: <b>" + ParentNode.FullName + "</b>";
                                ErrorController.AddWarning(serviceKey, message, visualScript, ParentNode.InstanceId);
                            }
                        }
                    }
                }
            }
            if (IsEnablePort)
            {
                var producerPort = SegmentProducerPort;
                if (producerPort != this)
                {
                    // -- Verify for unnecessary OR function --
                    var producerNode = producerPort.ParentNode;
                    if (producerNode.IsFunction("Or", "iCS_Boolean", ""))
                    {
                        if (producerPort.SegmentEndConsumerPorts.Length <= 1)
                        {
                            var message = "Consider using additional 'Enable(s)' ports instead of an OR function. => " + producerNode.FullName;
                            ErrorController.AddWarning(serviceKey, message, visualScript, producerNode.InstanceId);
                            ErrorController.AddWarning(serviceKey, message, visualScript, producerNode.InstanceId);
                        }
                    }
                    // -- Verify for unnecessary trigger->enble binding when
                    //    data flow already exist --
                    var parentNode = ParentNode;
                    if (producerPort.IsTriggerPort &&
                        producerNode.IsKindOfFunction && parentNode.IsKindOfFunction)
                    {
                        bool doesDataFlowExist = false;
                        parentNode.ForEachChildPort(
                            p => {
                            if (p.IsInDataPort)
                            {
                                var pp = p.SegmentProducerPort;
                                if (pp.ParentNode == producerNode)
                                {
                                    doesDataFlowExist = true;
                                }
                            }
                        }
                            );
                        if (doesDataFlowExist)
                        {
                            var message = "<b>Trigger</b> to <b>Enable</b> connection not needed since data flow already exist between nodes.  Consider removing the control flow.";
                            ErrorController.AddWarning(serviceKey, message, visualScript, InstanceId);
                            ErrorController.AddWarning(serviceKey, message, visualScript, producerPort.InstanceId);
                        }
                        // -- Determine if self to target connection should be used for
                        //    sequencing operation on same object.
                        var producerNodeTargetPort = producerNode.TargetPort;
                        var parentNodeTargetPort   = parentNode.TargetPort;
                        if (producerNodeTargetPort != null && parentNodeTargetPort != null)
                        {
                            var targetProducerPort = parentNodeTargetPort.SegmentProducerPort;
                            if (producerNodeTargetPort.SegmentProducerPort == targetProducerPort)
                            {
                                var targetProducerNode   = targetProducerPort.ParentNode;
                                var producerNodeSelfPort = producerNode.SelfPort;
                                var message = "Consider connecting the <b>Self</b> port of '<b>" + producerNode.DisplayName
                                              + "</b>' to the <b>Target</b> port of '<b>" + parentNode.DisplayName
                                              + "</b> to sequence operations on <b>" + targetProducerNode.DisplayName
                                              + "</b>.";
                                ErrorController.AddWarning(serviceKey, message, visualScript, parentNodeTargetPort.InstanceId);
                                ErrorController.AddWarning(serviceKey, message, visualScript, producerNodeSelfPort.InstanceId);
                            }
                        }
                    }
                }
            }
            return(true);
        }