Пример #1
0
        public static AssetTypeInfo GetAssetTypeInfo(this NamedObjectSave instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (string.IsNullOrEmpty(instance.ClassType))
            {
                return(null);
            }

            // If this NOS uses an EntireFile, then we should ask the file for its AssetTypeInfo,
            // as there may be multiple file types that produce the same class type.
            // For example
            AssetTypeInfo returnAti = null;

            if (instance.IsEntireFile)
            {
                var container = instance.GetContainer();

                var rfs = container?.GetReferencedFileSave(instance.SourceFile);

                if (rfs != null)
                {
                    var candidateAti = rfs.GetAssetTypeInfo();

                    // The user may use a file, but may change the runtime type through the
                    // SourceName property, so we need to make sure they match:
                    if (candidateAti != null && candidateAti.RuntimeTypeName == instance.ClassType)
                    {
                        returnAti = candidateAti;
                    }
                }
            }

            if (returnAti == null)
            {
                returnAti = AvailableAssetTypes.Self.GetAssetTypeFromRuntimeType(instance.ClassType, instance, isObject: true);
            }

            if (instance.ClassType.StartsWith("PositionedObjectList<"))
            {
                return(null);
            }
            else
            {
                // Vic says: I don't think this should throw an exception anymore
                //if (returnAti == null)
                //{
                //    throw new InvalidOperationException("You probably need to add the class type " + this.ClassType +
                //        " to the ContentTypes.csv");
                //}

                return(returnAti);
            }
        }
        public void GetTextureCoordinates(NamedObjectSave nos, out float leftTextureCoordinate, out float rightTextureCoordinate, out float topTextureCoordinate, out float bottomTextureCoordinate, out string fullFileName)
        {
            var textureVar = nos.InstructionSaves.FirstOrDefault(item => item.Member == "Texture");
            var leftTextureCoordinateVar = nos.InstructionSaves.FirstOrDefault(item => item.Member == "LeftTexturePixel");
            var rightTextureCoordinateVar = nos.InstructionSaves.FirstOrDefault(item => item.Member == "RightTexturePixel");
            var topTextureCoordinateVar = nos.InstructionSaves.FirstOrDefault(item => item.Member == "TopTexturePixel");
            var bottomTextureCoordinateVar = nos.InstructionSaves.FirstOrDefault(item => item.Member == "BottomTexturePixel");


            leftTextureCoordinate = 0;
            rightTextureCoordinate = 1;
            topTextureCoordinate = 0;
            bottomTextureCoordinate = 1;

            fullFileName = null;

            if (textureVar != null && textureVar.Value != null)
            {
                var fileVariableName = textureVar.Value as string;

                var container = nos.GetContainer();

                var rfs = container.ReferencedFiles.FirstOrDefault((item) =>
                {
                    return FileManager.RemovePath(FileManager.RemoveExtension(item.Name)) == fileVariableName;
                });
                if (rfs != null)
                {
                    fullFileName = FlatRedBall.Glue.ProjectManager.MakeAbsolute(rfs.Name, true);
                }
            }

            if (leftTextureCoordinateVar != null && leftTextureCoordinateVar.Value != null)
            {
                leftTextureCoordinate = (float)leftTextureCoordinateVar.Value;
            }

            if (rightTextureCoordinateVar != null && rightTextureCoordinateVar.Value != null)
            {
                rightTextureCoordinate = (float)rightTextureCoordinateVar.Value;
            }

            if (topTextureCoordinateVar != null && topTextureCoordinateVar.Value != null)
            {
                topTextureCoordinate = (float)topTextureCoordinateVar.Value;
            }

            if (bottomTextureCoordinateVar != null && bottomTextureCoordinateVar.Value != null)
            {
                bottomTextureCoordinate = (float)bottomTextureCoordinateVar.Value;
            }
        }
Пример #3
0
        public static string NamedObjectSaveToString(NamedObjectSave nos)
        {
            IElement container = nos.GetContainer();

            string containerName = " (Uncontained)";

            if (container != null)
            {
                containerName = " in " + container.ToString();
            }

            return(nos.ClassType + " " + nos.FieldName + containerName);
        }
Пример #4
0
        public static ContainerType GetContainerType(this NamedObjectSave instance)
        {
            IElement container = instance.GetContainer();

            if (container == null)
            {
                return(SaveClasses.ContainerType.None);
            }
            else if (container is EntitySave)
            {
                return(SaveClasses.ContainerType.Entity);
            }
            else
            {
                return(SaveClasses.ContainerType.Screen);
            }
        }
        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;
                                }

                            }
                        }
                    }
                }
            }
        }
Пример #6
0
        public TreeNode NamedObjectTreeNode(NamedObjectSave namedObjectSave)
        {
            IElement container = namedObjectSave.GetContainer();

            if (container is ScreenSave)
            {
                ScreenTreeNode screenTreeNode = GlueState.Self.Find.ScreenTreeNode((ScreenSave)container);
                return screenTreeNode.GetTreeNodeFor(namedObjectSave);
            }
            else if (container is EntitySave)
            {
                EntityTreeNode entityTreeNode = GlueState.Self.Find.EntityTreeNode((EntitySave)container);
                return entityTreeNode.GetTreeNodeFor(namedObjectSave);
            }
            else
            {
                return null;
            }
        }
        private static bool MoveObjectOnObjectsRoot(TreeNode treeNodeMoving, TreeNode targetNode, NamedObjectSave movingNos, bool succeeded)
        {
            // Dropped it on the "Objects" tree node

            // Let's see if it's the Objects that contains node or another one

            IElement parentOfMovingNos = movingNos.GetContainer();
            IElement elementMovingInto = ((BaseElementTreeNode)targetNode.Parent).SaveObjectAsElement;

            if (parentOfMovingNos == elementMovingInto)
            {

                if (treeNodeMoving.Parent.IsNamedObjectNode())
                {
                    succeeded = true;

                    // removing from a list
                    NamedObjectSave container = treeNodeMoving.Parent.Tag as NamedObjectSave;



                    IElement elementToAddTo = movingNos.GetContainer();
                    container.ContainedObjects.Remove(movingNos);
                    NamedObjectSaveExtensionMethodsGlue.AddNewNamedObjectToElementTreeNode(EditorLogic.CurrentElementTreeNode, movingNos, false);
                    EditorLogic.CurrentElementTreeNode.UpdateReferencedTreeNodes();

                    IElement elementToRegenerate = targetNode.Parent.Tag as IElement;
                }
            }
            else
            {
                succeeded = DragDropNosIntoElement(movingNos, elementMovingInto);
            }
            return succeeded;
        }
        public static void GetActivityForNamedObject(NamedObjectSave namedObjectSave, ICodeBlock codeBlock)
        {
            ///////////////////////////EARLY OUT/////////////////////////////////////////////////
            if (
                (namedObjectSave.SetByContainer && namedObjectSave.GetContainer() is EntitySave)
                ||
                namedObjectSave.IsDisabled || namedObjectSave.CallActivity == false ||
                namedObjectSave.InstantiatedByBase || !namedObjectSave.IsFullyDefined)
            {
                return;
            }
            /////////////////////////END EARLY OUT///////////////////////////////////////////////

            bool setByDerived = namedObjectSave.SetByDerived;

            AddIfConditionalSymbolIfNecesssary(codeBlock, namedObjectSave);

            if (!setByDerived)
            {
                if (namedObjectSave.Instantiate == false)
                {
                    // This may be null or it may be instantiated later by the user, so we should
                    // handle both cases:
                    codeBlock = codeBlock.If(namedObjectSave.InstanceName + " != null");
                }

                if (namedObjectSave.SourceType == SourceType.Entity)
                {
                    // Entities need activity!
                    codeBlock.Line(namedObjectSave.InstanceName + ".Activity();");
                }
                else if (namedObjectSave.SourceType == SourceType.FlatRedBallType &&
                    namedObjectSave.ClassType != null &&
                    namedObjectSave.ClassType.Contains("PositionedObjectList<"))
                {
                    // Now let's see if the object in the list is an entity
                    string genericType = namedObjectSave.SourceClassGenericType;


                    if (genericType.Contains("Entities\\"))
                    {
                        codeBlock.For("int i = " + namedObjectSave.InstanceName + ".Count - 1; i > -1; i--")
                                    .If("i < " + namedObjectSave.InstanceName + ".Count")
                                        .Line("// We do the extra if-check because activity could destroy any number of entities")
                                        .Line(namedObjectSave.InstanceName + "[i].Activity();");
                    }


                }

            }

            // If it's an emitter, call TimedEmit:
            ParticleCodeGenerator.GenerateTimedEmit(codeBlock, namedObjectSave);

            if (!setByDerived)
            {

                if (namedObjectSave.Instantiate == false)
                {
                    // end the if-statement we started above.
                    codeBlock = codeBlock.End();
                }
            }

            AddEndIfIfNecessary(codeBlock, namedObjectSave);
        }
        public static string NamedObjectSaveToString(NamedObjectSave nos)
        {
            IElement container = nos.GetContainer();

            string containerName = " (Uncontained)";
            if (container != null)
            {
                containerName = " in " + container.ToString();
            }

            return nos.ClassType + " " + nos.FieldName + containerName;

        }
Пример #10
0
        public static void RemoveNamedObject(NamedObjectSave namedObjectToRemove, bool performSave, bool updateUi, List<string> additionalFilesToRemove)
        {
            StringBuilder removalInformation = new StringBuilder();

            // The additionalFilesToRemove is included for consistency with other methods.  It may be used later

            // There are the following things that need to happen:
            // 1.  Remove the NamedObject from the Glue project (GLUX)
            // 2.  Remove any variables that use this NamedObject as their source
            // 3.  Remove the named object from the GUI
            // 4.  Update the variables for any NamedObjects that use this element containing this NamedObject
            // 5.  Find any Elements that contain NamedObjects that are DefinedByBase - if so, see if we should remove those or make them not DefinedByBase
            // 6.  Remove any events that tunnel into this.

            IElement element = namedObjectToRemove.GetContainer();

            if (element != null)
            {

                if (!namedObjectToRemove.RemoveSelfFromNamedObjectList(element.NamedObjects))
                {
                    throw new ArgumentException();
                }

                #region Remove all CustomVariables that reference the removed NamedObject
                for (int i = element.CustomVariables.Count - 1; i > -1; i--)
                {
                    CustomVariable variable = element.CustomVariables[i];

                    if (variable.SourceObject == namedObjectToRemove.InstanceName)
                    {
                        removalInformation.AppendLine("Removed variable " + variable.ToString());

                        element.CustomVariables.RemoveAt(i);
                    }
                }
                #endregion

                // Remove any events that use this
                for (int i = element.Events.Count - 1; i > -1; i--)
                {
                    EventResponseSave ers = element.Events[i];
                    if (ers.SourceObject == namedObjectToRemove.InstanceName)
                    {
                        removalInformation.AppendLine("Removed event " + ers.ToString());
                        element.Events.RemoveAt(i);
                    }
                }

                // Remove any objects that use this as a layer
                for (int i = 0; i < element.NamedObjects.Count; i++)
                {
                    if (element.NamedObjects[i].LayerOn == namedObjectToRemove.InstanceName)
                    {
                        removalInformation.AppendLine("Removed the following object from the deleted Layer: " + element.NamedObjects[i].ToString());
                        element.NamedObjects[i].LayerOn = null;
                    }
                }




                element.RefreshStatesToCustomVariables();

                #region Ask the user what to do with all NamedObjects that are DefinedByBase

                List<IElement> derivedElements = new List<IElement>();
                if (element is EntitySave)
                {
                    derivedElements.AddRange(ObjectFinder.Self.GetAllEntitiesThatInheritFrom(element as EntitySave));
                }
                else
                {
                    derivedElements.AddRange(ObjectFinder.Self.GetAllScreensThatInheritFrom(element as ScreenSave));
                }

                foreach (IElement derivedElement in derivedElements)
                {
                    // At this point, namedObjectToRemove is already removed from the current Element, so this will only
                    // return NamedObjects that exist in the derived.
                    NamedObjectSave derivedNamedObject = derivedElement.GetNamedObjectRecursively(namedObjectToRemove.InstanceName);

                    if (derivedNamedObject != null && derivedNamedObject != namedObjectToRemove && derivedNamedObject.DefinedByBase)
                    {
                        MultiButtonMessageBox mbmb = new MultiButtonMessageBox();
                        mbmb.MessageText = "What would you like to do with the object " + derivedNamedObject.ToString();
                        mbmb.AddButton("Keep it", DialogResult.OK);
                        mbmb.AddButton("Delete it", DialogResult.Cancel);

                        DialogResult result = mbmb.ShowDialog(MainGlueWindow.Self);

                        if (result == DialogResult.OK)
                        {
                            // Keep it
                            derivedNamedObject.DefinedByBase = false;
                            BaseElementTreeNode treeNode = GlueState.Self.Find.ElementTreeNode(derivedElement);

                            if (updateUi)
                            {
                                treeNode.UpdateReferencedTreeNodes();
                            }
                            CodeWriter.GenerateCode(derivedElement);
                        }
                        else
                        {
                            // Delete it
                            RemoveNamedObject(derivedNamedObject, performSave, updateUi, additionalFilesToRemove);
                        }


                    }

                }
                #endregion


                if (element is ScreenSave)
                {
                    ScreenTreeNode stn = GlueState.Self.Find.ScreenTreeNode(element as ScreenSave);
                    if (updateUi)
                    {
                        stn.UpdateReferencedTreeNodes();
                    }
                    CodeWriter.GenerateCode(element);
                }
                else
                {
                    EntityTreeNode etn = GlueState.Self.Find.EntityTreeNode(element as EntitySave);
                    if (updateUi)
                    {
                        etn.UpdateReferencedTreeNodes();
                    }
                    CodeWriter.GenerateCode(element);

                    List<NamedObjectSave> entityNamedObjects = ObjectFinder.Self.GetAllNamedObjectsThatUseEntity(element.Name);

                    foreach (NamedObjectSave nos in entityNamedObjects)
                    {
                        nos.UpdateCustomProperties();
                    }
                }




            }

            if (performSave)
            {
                GluxCommands.Self.SaveGlux();
            }
        }