SetState() public method

public SetState ( StateSave stateToSet, IElement container ) : void
stateToSet FlatRedBall.Glue.SaveClasses.StateSave
container IElement
return void
示例#1
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
                    }
                }
            }
        }
示例#2
0
        public static void InterpolateBetween(ElementRuntime elementRuntime, object firstStateSaveAsObject, object secondStateSaveAsObject, object interpolationValueAsObject,
            StringBuilder logStringBuilder = null)
        {
            StateSave firstStateSave = firstStateSaveAsObject as StateSave;
            StateSave secondStateSave = secondStateSaveAsObject as StateSave;
            float interpolationValue = 0;

            if (interpolationValueAsObject is float)
            {
                interpolationValue = (float)interpolationValueAsObject;
            }
            if (interpolationValueAsObject is double)
            {
                interpolationValue = (float)((double)interpolationValueAsObject);
            }
            else if (interpolationValueAsObject is int)
            {
                interpolationValue = (int)interpolationValueAsObject;
            }

            if (float.IsNaN(interpolationValue))
            {
                throw new Exception("InterpolationValue is NaN");
            }

            StateSave resultingStateSave = StateSaveExtensionMethodsGlueView.CreateCombinedState(firstStateSave, secondStateSave, interpolationValue);
            string nameOfObject = "unnamed object";
            if (elementRuntime.AssociatedNamedObjectSave != null)
            {
                nameOfObject = elementRuntime.AssociatedNamedObjectSave.ToString();
            }
            else if (elementRuntime.AssociatedIElement != null)
            {
                nameOfObject = elementRuntime.AssociatedIElement.ToString();
            }
            if (logStringBuilder != null)
            {
                logStringBuilder.AppendLine("Interpolating " + nameOfObject + " between " + firstStateSave + " and " + secondStateSave + " with value " + interpolationValue);

                foreach (InstructionSave instruction in resultingStateSave.InstructionSaves)
                {
                    logStringBuilder.AppendLine("\t" + instruction);
                }
            }

            try
            {
                elementRuntime.SetState(resultingStateSave, elementRuntime.AssociatedIElement);
            }
            catch (Exception e)
            {
                throw new StateSettingException("Error in script trying to interpolate " + nameOfObject + " between " + firstStateSave + " and " + secondStateSave + " with value " + interpolationValue);
            }
        }