GetCustomVariable() public method

public GetCustomVariable ( string name ) : CustomVariableInNamedObject
name string
return CustomVariableInNamedObject
Esempio n. 1
0
        /// <summary>
        /// Returns the value of a variable either set in the NamedObjectSave (if it is set there) or in the underlying Entity if not
        /// </summary>
        /// <param name="instance">The NamedObjectSave to get the variable from.</param>
        /// <param name="variableName">The name of the variable, such as "ScaleX"</param>
        /// <returns>The value of the variable in either the NamedObjectSave or underlying Entity.  Returns null if varible isn't found.</returns>
        public static object GetEffectiveValue(this NamedObjectSave instance, string variableName)
        {
            CustomVariableInNamedObject cvino = instance.GetCustomVariable(variableName);

            if (cvino == null || cvino.Value == null)
            {
                if (instance.SourceType == SourceType.Entity && !string.IsNullOrEmpty(instance.SourceClassType))
                {
                    EntitySave entitySave = ObjectFinder.Self.GetEntitySave(instance.SourceClassType);

                    if (entitySave != null)
                    {
                        CustomVariable variable = entitySave.GetCustomVariableRecursively(variableName);

                        if (variable != null)
                        {
                            return(variable.DefaultValue);
                        }
                    }
                }
            }
            else
            {
                return(cvino.Value);
            }
            return(null);
        }
        private void ReactToTextureAddressMode(NamedObjectSave namedObjectSave, object oldValue)
        {
            bool isSprite = namedObjectSave.SourceType == SourceType.FlatRedBallType && namedObjectSave.SourceClassType == "Sprite";
            if(isSprite)
            {
                var addressModeVariable = namedObjectSave.GetCustomVariable("TextureAddressMode");

                if (addressModeVariable != null && addressModeVariable.Value != null && 
                    ((TextureAddressMode)(addressModeVariable.Value) == TextureAddressMode.Wrap || (TextureAddressMode)(addressModeVariable.Value) == TextureAddressMode.Mirror))
                {
                    // How big is the texture?
                    var textureVariable = namedObjectSave.GetCustomVariable("Texture");

                    if (textureVariable != null && textureVariable.Value != null)
                    {
                        string value = textureVariable.Value as string;

                        var rfs = namedObjectSave.GetContainer().GetReferencedFileSaveByInstanceName(value);

                        if (rfs != null)
                        {

                            var width = ImageHeader.GetDimensions(
                                    ProjectManager.MakeAbsolute(rfs.Name)).Width;
                            var height = ImageHeader.GetDimensions(
                                    ProjectManager.MakeAbsolute(rfs.Name)).Height;

                            string whatIsWrong = null;

                            if (FlatRedBall.Math.MathFunctions.IsPowerOfTwo(width) == false)
                            {
                                whatIsWrong = "This Sprite's texture (" + textureVariable.Value + ") has a width of " + 
                                    width + " but it should be a power of two to use " + addressModeVariable.Value + " TextureAddressMode";
                            }


                            if (FlatRedBall.Math.MathFunctions.IsPowerOfTwo(height) == false)
                            {
                                whatIsWrong = "This Sprite's texture (" + textureVariable.Value + ") has a height of " +
                                    height + " but it should be a power of two to use " + addressModeVariable.Value + " TextureAddressMode";
                            }

                            if(!string.IsNullOrEmpty(whatIsWrong))
                            {
                                whatIsWrong += "\nWhat would you like to do?";

                                MultiButtonMessageBox mbmb = new MultiButtonMessageBox();
                                mbmb.MessageText = whatIsWrong;
                                mbmb.AddButton("Undo the change", DialogResult.Cancel);
                                mbmb.AddButton("Keep the change (May cause runtime crashes)", DialogResult.Yes);

                                var result = mbmb.ShowDialog();

                                if(result == DialogResult.Cancel)
                                {
                                    addressModeVariable.Value = oldValue;
                                }

                            }
                        }
                    }
                }
            }
        }
        private void ReactToFontSet(NamedObjectSave namedObjectSave, object oldValue)
        {
            string value = namedObjectSave.GetCustomVariable("Font").Value as string;

            if (!string.IsNullOrEmpty(value))
            {
                IElement element = EditorLogic.CurrentElement;

                ReferencedFileSave referencedFileSave = element.GetReferencedFileSaveByInstanceNameRecursively(value);


                if (referencedFileSave != null)
                {
                    string file = referencedFileSave.GetRelativePath();
                    file = ProjectManager.MakeAbsolute(file, true);

                    string contents = FileManager.FromFileText(file);

                    int size =
                        StringFunctions.GetIntAfter(
                        "size=", contents);

                    float lineHeightInPixels =
                        StringFunctions.GetIntAfter(
                        "lineHeight=", contents);

                    lineHeightInPixels /= 2.0f;

                    namedObjectSave.SetPropertyValue("Scale", (float)lineHeightInPixels);
                    namedObjectSave.SetPropertyValue("Spacing", (float)lineHeightInPixels);
                    namedObjectSave.SetPropertyValue("NewLineDistance", (float)(lineHeightInPixels * 1.5f));

                }
            }
        }
        private void ReactToAnimationChainSet(NamedObjectSave namedObjectSave, object oldValue)
        {
            AvailableAnimationChainsStringConverter aacsc = new AvailableAnimationChainsStringConverter(
                GlueState.Self.CurrentElement, namedObjectSave);

            var customVariable = namedObjectSave.GetCustomVariable("CurrentChainName");

            string currentChain = null;

            if (customVariable != null)
            {
                currentChain = customVariable.Value as string;
            }

            if (!aacsc.AvailableChains.Contains(currentChain))
            {
                if (aacsc.AvailableChains.Length == 0)
                {
                    namedObjectSave.SetPropertyValue("CurrentChainName", null);

                }
                else
                {
                    namedObjectSave.SetPropertyValue("CurrentChainName", aacsc.AvailableChains[0]);
                }
            }
        }
        public static void SetVariableToDefault(NamedObjectSave currentNamedObject, string variableToSet)
        {
            // July 13, 2014
            // This used to simply set the value to null, but why don't we remove it if it exists?
            // This way if an error is introduced by some plugin that sets the type to something invalid
            // the user can still remove it through this option and recover the type later.
            //currentNamedObject.SetPropertyValue(variableToSet, null);
            currentNamedObject.InstructionSaves.RemoveAll(item => item.Member == variableToSet);

            if (currentNamedObject.GetCustomVariable(variableToSet) != null)
            {
                // See if this variable is tunneled into in this element.
                // If so, set that value too.
                CustomVariableInNamedObject cvino = currentNamedObject.GetCustomVariable(variableToSet);
                object value = cvino.Value;

                foreach (CustomVariable customVariable in EditorLogic.CurrentElement.CustomVariables)
                {
                    if (customVariable.SourceObject == currentNamedObject.InstanceName &&
                        customVariable.SourceObjectProperty == variableToSet)
                    {
                        customVariable.DefaultValue = value;
                        break;
                    }
                }

                GlueCommands.Self.RefreshCommands.RefreshPropertyGrid();
            }
        }