public static bool GetShouldCustomVariableBeConvertedToType(MemberChangeArgs args, CustomVariable variable)
        {
            var runtimeType = variable.GetRuntimeType();

            return(runtimeType != null && args.Value is string && !variable.GetIsFile() && variable.Type != "Color");
        }
예제 #2
0
        private void SetVariablesForNamedObject(object newlyCreatedElementRuntime, NamedObjectSave n)
        {
            if (newlyCreatedElementRuntime != null)
            {
                ElementRuntime containedElementRuntime = GetContainedElementRuntime(n);

                Type typeOfNewObject = newlyCreatedElementRuntime.GetType();

                // As of June 24, 2012
                // States are set before
                // CustomVariables:
                if (!string.IsNullOrEmpty(n.CurrentState))
                {
                    containedElementRuntime.SetState(n.CurrentState, false);
                }


                // If it's null, this means it's a default value so don't set anything
                foreach (CustomVariableInNamedObject cvino in n.InstructionSaves.Where(cvino => cvino.Value != null))
                {
                    // If there isn't a TypedMember for the variable, that means Glue won't generate code for it, so we shouln't be applying it.
                    if (!ShouldApplyVariableOnRuntime(cvino, n))
                    {
                        continue;
                    }

                    try
                    {
                        // We used to execute
                        // the Instructions right
                        // on the element runtime itself
                        // but I think it's best if we use
                        // the CustomVariable code so that we
                        // get all the benefit of CustomVariables
                        // like loading of files, and calling events.
                        //cvino.ToInstruction(newlyCreatedElementRuntime).Execute();

                        // Update May 25, 2012
                        // We used to get the ElementRuntime
                        // for the NamedObjectSave and have it
                        // set the CustomVariable - however this
                        // doesn't work because it tries to access
                        // files within its own scope, instead of files
                        // that belong to "this".
                        //CustomVariable customVariable = new CustomVariable();
                        //customVariable.Type = cvino.Type;
                        ////customVariable.SourceObject = n.InstanceName;
                        ////customVariable.SourceObjectProperty = cvino.Member;
                        //customVariable.DefaultValue = cvino.Value;
                        //customVariable.Name = cvino.Member;
                        //containedElementRuntime.SetCustomVariable(customVariable, customVariable.DefaultValue, false);

                        CustomVariable customVariable = new CustomVariable();
                        customVariable.Type                 = cvino.Type;
                        customVariable.SourceObject         = n.InstanceName;
                        customVariable.SourceObjectProperty = cvino.Member;
                        customVariable.DefaultValue         = cvino.Value;
                        customVariable.Name                 = cvino.Member;
                        if (cvino.Value is string && customVariable.GetIsFile())
                        {
                            // We want to load the file at this level and pass the result down:
                            ReferencedFileSave rfs         = GetReferencedFileFromName(cvino.Value);
                            object             fileRuntime = null;

                            if (rfs == null)
                            {
                                fileRuntime = null;
                            }
                            else
                            {
                                fileRuntime = LoadReferencedFileSave(rfs, true, this.AssociatedIElement);
                            }

                            customVariable.DefaultValue = fileRuntime;
                        }
                        if (customVariable.DefaultValue is float && customVariable.SourceObjectProperty == "Z" &&
                            newlyCreatedElementRuntime is PositionedObject && ((PositionedObject)newlyCreatedElementRuntime).Parent == Camera.Main)
                        {
                            float value = (float)customVariable.DefaultValue - 40;

                            customVariable.DefaultValue = value;
                        }
                        SetCustomVariable(customVariable, mAssociatedIElement, customVariable.DefaultValue, false);
                    }
                    catch (Exception e)
                    {
                        int m = 3;
                        m++;
                        // for now, do nothing
                    }
                }
            }
        }
예제 #3
0
        public static string GetRightSideAssignmentValueAsString(IElement element, InstructionSave instruction)
        {
            CustomVariable customVariable = element.GetCustomVariableRecursively(instruction.Member);

            IElement referencedElement = null;

            #region Determine if the assignment is a file

            bool isFile = false;

            if (customVariable != null)
            {
                referencedElement =
                    BaseElementTreeNode.GetElementIfCustomVariableIsVariableState(customVariable, element);

                isFile = customVariable.GetIsFile();
            }

            #endregion

            string valueAsString = "";

            if (referencedElement == null)
            {
                valueAsString = CodeParser.ParseObjectValue(instruction.Value);

                if (isFile)
                {
                    valueAsString = valueAsString.Replace("\"", "");

                    if (valueAsString == "<NONE>")
                    {
                        valueAsString = "null";
                    }
                }
                else if (CustomVariableCodeGenerator.ShouldAssignToCsv(customVariable, valueAsString))
                {
                    valueAsString = CustomVariableCodeGenerator.GetAssignmentToCsvItem(customVariable, element, valueAsString);
                }
                else if (customVariable != null && customVariable.Type == "Color")
                {
                    valueAsString = "Color." + valueAsString.Replace("\"", "");
                }
                if (customVariable != null && !string.IsNullOrEmpty(customVariable.SourceObject) && !isFile)
                {
                    NamedObjectSave namedObject = element.GetNamedObjectRecursively(customVariable.SourceObject);

                    bool isVariableState = customVariable.GetIsVariableState();

                    IElement objectElement = null;

                    if (namedObject != null)
                    {
                        ObjectFinder.Self.GetIElement(namedObject.SourceClassType);
                    }

                    if (objectElement != null)
                    {
                        if (isVariableState)
                        {
                            string typeName = "VariableState";

                            StateSaveCategory category = objectElement.GetStateCategoryRecursively(customVariable.Type);

                            if (category != null && category.SharesVariablesWithOtherCategories == false)
                            {
                                typeName = category.Name;
                            }
                            valueAsString = objectElement.Name.Replace("/", ".").Replace("\\", ".") + "." + typeName + "." + valueAsString.Replace("\"", "");
                        }
                    }

                    valueAsString = CodeWriter.MakeLocalizedIfNecessary(
                        namedObject,
                        instruction.Member,
                        instruction.Value,
                        valueAsString,
                        customVariable);
                }
            }
            else
            {
                string enumValue = (string)instruction.Value;

                if (!string.IsNullOrEmpty(enumValue) && enumValue != "<NONE>")
                {
                    string variableType = "VariableState";

                    if (customVariable != null && customVariable.Type.ToLower() != "string")
                    {
                        variableType = customVariable.Type;
                    }
                    valueAsString = FullyQualifyStateValue(referencedElement, enumValue, variableType);
                }
            }
            return(valueAsString);
        }
        private static object GetDefaultValueFor(CustomVariable customVariable, IElement element)
        {
            object valueToReturn = null;

            // Lets handle all file types here:
            //if (customVariable.Type == "Texture2D")
            if (customVariable.GetIsFile())
            {
                // If we don't return
                // null then this will
                // return the name of the
                // file (like "redball.bmp").
                // This will screw up code generation
                // because texture variables can only be
                // set to Texture2D instances, not to the
                // name of the image.
                valueToReturn = null;
            }
            else
            {
                try
                {
                    if (!string.IsNullOrEmpty(customVariable.SourceObject) && !string.IsNullOrEmpty(customVariable.SourceObjectProperty))
                    {
                        NamedObjectSave nos = element.GetNamedObjectRecursively(customVariable.SourceObject);

                        if (nos != null)
                        {
                            object defaultValue = GetDefaultValueFor(nos, customVariable.SourceObjectProperty, customVariable.OverridingPropertyType);

                            valueToReturn = defaultValue;
                        }
                    }
                    else if (customVariable.Name == "Visible" && element is EntitySave && ((EntitySave)element).ImplementsIVisible)
                    {
                        // This shouldn't be true, we don't want to set the value
                        // because we don't want to kick off events
                        //valueToReturn = true;
                        valueToReturn = null;
                    }
                    else if (customVariable.Name == "Enabled" && element is EntitySave && ((EntitySave)element).ImplementsIWindow)
                    {
                        // This shouldn't be true, we don't want to set the value
                        // because we don't want to kick off events
                        //valueToReturn = true;
                        valueToReturn = null;
                    }
                    else
                    {
                        valueToReturn = GetDefaultValueForPropertyInType(customVariable.Name, "PositionedObject");
                    }
                }
                catch
                {
                    // do nothing.
                }

                if (valueToReturn is Microsoft.Xna.Framework.Color)
                {
                    string standardName =
                        AvailableColorTypeConverter.GetStandardColorNameFrom((Color)valueToReturn);

                    if (string.IsNullOrEmpty(standardName))
                    {
                        return("Red");
                    }
                    else
                    {
                        return(standardName);
                    }
                }
            }
            return(valueToReturn);
        }
        public static InterpolationCharacteristic GetInterpolationCharacteristic(CustomVariable customVariable, IElement container)
        {
            string variableType = null;

            if (customVariable != null)
            {
                if (!string.IsNullOrEmpty(customVariable.OverridingPropertyType))
                {
                    variableType = customVariable.OverridingPropertyType;
                }
                else
                {
                    variableType = customVariable.Type;
                }
            }


            if (customVariable != null && customVariable.GetIsVariableState(container))
            {
                return(InterpolationCharacteristic.CanInterpolate);
            }

            if (customVariable == null ||
                variableType == null ||
                variableType == "string" ||
                variableType == "bool" ||
                variableType == "Color" ||
                customVariable.GetIsFile() ||
                customVariable.GetIsCsv() ||
                (customVariable.GetRuntimeType() != null && customVariable.GetRuntimeType().IsEnum)
                )
            {
                return(InterpolationCharacteristic.CantInterpolate);
            }

            string velocityMember = null;

            if (!string.IsNullOrEmpty(customVariable.SourceObject))
            {
                velocityMember = FlatRedBall.Instructions.InstructionManager.GetVelocityForState(customVariable.SourceObjectProperty);
            }
            else
            {
                velocityMember = FlatRedBall.Instructions.InstructionManager.GetVelocityForState(customVariable.Name);
            }
            if (!string.IsNullOrEmpty(velocityMember))
            {
                // There's a velocity variable for this, but we need to make sure
                // it's actually available
                var exposableMembers = ExposedVariableManager.GetExposableMembersFor(container, false);

                if (exposableMembers.Any(item => item.Member == velocityMember))
                {
                    return(InterpolationCharacteristic.CanInterpolate);
                }
            }

            // December 26, 2013
            // This used to not pass
            // a value for maxDepth which
            // means a maxDepth of 0.  Not
            // sure why, but we do want to look
            // at tunneling at any depth.
            int maxDepth = int.MaxValue;

            if (customVariable.HasAccompanyingVelocityConsideringTunneling(container, maxDepth))
            {
                return(InterpolationCharacteristic.CanInterpolate);
            }

            else
            {
                return(InterpolationCharacteristic.NeedsVelocityVariable);
            }
        }
예제 #6
0
        private static object GetDefaultValueFor(CustomVariable customVariable, IElement element)
        {
            object valueToReturn = null;

            // Lets handle all file types here:
            //if (customVariable.Type == "Texture2D")
            if(customVariable.GetIsFile())
            {
                // If we don't return 
                // null then this will
                // return the name of the
                // file (like "redball.bmp").
                // This will screw up code generation
                // because texture variables can only be
                // set to Texture2D instances, not to the
                // name of the image.
                valueToReturn = null;
            }
            else
            {



                try
                {
                    if (!string.IsNullOrEmpty(customVariable.SourceObject) && !string.IsNullOrEmpty(customVariable.SourceObjectProperty))
                    {
                        NamedObjectSave nos = element.GetNamedObjectRecursively(customVariable.SourceObject);

                        if (nos != null)
                        {
                            object defaultValue = GetDefaultValueFor(nos, customVariable.SourceObjectProperty, customVariable.OverridingPropertyType);

                            valueToReturn = defaultValue;
                        }
                    }
                    else if (customVariable.Name == "Visible" && element is EntitySave && ((EntitySave)element).ImplementsIVisible)
                    {
                        // This shouldn't be true, we don't want to set the value
                        // because we don't want to kick off events
                        //valueToReturn = true;
                        valueToReturn = null;
                    }
                    else if (customVariable.Name == "Enabled" && element is EntitySave && ((EntitySave)element).ImplementsIWindow)
                    {
                        // This shouldn't be true, we don't want to set the value
                        // because we don't want to kick off events
                        //valueToReturn = true;
                        valueToReturn = null;
                    }
                    else
                    {

                        valueToReturn = GetDefaultValueForPropertyInType(customVariable.Name, "PositionedObject");
                    }
                }
                catch
                {
                    // do nothing.
                }

                if (valueToReturn is Microsoft.Xna.Framework.Color)
                {
                    string standardName =
                        AvailableColorTypeConverter.GetStandardColorNameFrom((Color)valueToReturn);

                    if (string.IsNullOrEmpty(standardName))
                    {
                        return "Red";
                    }
                    else
                    {
                        return standardName;
                    }
                }
            }
            return valueToReturn;
        }
예제 #7
0
        public static InterpolationCharacteristic GetInterpolationCharacteristic(CustomVariable customVariable, IElement container)
        {
            string variableType = null;
            if (customVariable != null)
            {
                if (!string.IsNullOrEmpty(customVariable.OverridingPropertyType))
                {
                    variableType = customVariable.OverridingPropertyType;
                }
                else
                {
                    variableType = customVariable.Type;
                }
            }


            if (customVariable != null && customVariable.GetIsVariableState(container))
            {
                return InterpolationCharacteristic.CanInterpolate;
            }

            if (customVariable == null ||
                variableType == null ||
                variableType == "string" ||
                variableType == "bool" ||
                variableType == "Color" ||
                customVariable.GetIsFile() ||
                customVariable.GetIsCsv() ||
                (customVariable.GetRuntimeType() != null && customVariable.GetRuntimeType().IsEnum)
                )
            {
                return InterpolationCharacteristic.CantInterpolate;
            }

            string velocityMember = null;

            if(!string.IsNullOrEmpty(customVariable.SourceObject))
            {
                velocityMember = FlatRedBall.Instructions.InstructionManager.GetVelocityForState(customVariable.SourceObjectProperty);
            }
            else
            {
                velocityMember = FlatRedBall.Instructions.InstructionManager.GetVelocityForState(customVariable.Name);

            }
            if (!string.IsNullOrEmpty(velocityMember))
            {
                // There's a velocity variable for this, but we need to make sure
                // it's actually available
                var exposableMembers = ExposedVariableManager.GetExposableMembersFor(container, false);

                if (exposableMembers.Any(item=>item.Member ==velocityMember))
                {
                    return InterpolationCharacteristic.CanInterpolate;
                }
            }            
            
            // December 26, 2013
            // This used to not pass
            // a value for maxDepth which
            // means a maxDepth of 0.  Not
            // sure why, but we do want to look
            // at tunneling at any depth.
            int maxDepth = int.MaxValue;
            if(customVariable.HasAccompanyingVelocityConsideringTunneling(container, maxDepth) )
            {
                return InterpolationCharacteristic.CanInterpolate;
            }

            else
            {
                return InterpolationCharacteristic.NeedsVelocityVariable;
            }


        }