public void TestConversion()
        {
            SpriteSave          spriteSave = new SpriteSave();
            SpriteSaveConverter converter  = new SpriteSaveConverter();

            var nos = converter.SpriteSaveToNamedObjectSave(spriteSave);

            if (nos.InstructionSaves.Count != 0)
            {
                throw new Exception("A default SpriteSave should have no properties");
            }

            spriteSave.X = 4;
            nos          = converter.SpriteSaveToNamedObjectSave(spriteSave);
            if (nos.InstructionSaves.Count != 1)
            {
                throw new Exception("A SpriteSave with non-zero X should have 1 property");
            }

            spriteSave.Texture = "Folder/filename.png";
            nos = converter.SpriteSaveToNamedObjectSave(spriteSave);
            if (nos.GetCustomVariable("Texture").Value as string != "filename")
            {
                throw new Exception("NOS's are not getting the right file name out of SpriteSaves");
            }
        }
示例#2
0
		void CustomInitialize()
		{
            SpriteSave spriteSave = new SpriteSave();
            spriteSave.X = 4;
            FileManager.InitializeUserFolder("Global");

            string directory = FileManager.GetUserFolder("Global");
            string fileName = directory + "TestSave.xml";
            FileManager.XmlSerialize(spriteSave, fileName);


            if(FileManager.FileExists(fileName) == false)
            {
                throw new Exception("The file " + fileName + " does not exist");
            }


            var loaded = FileManager.XmlDeserialize<SpriteSave>(fileName);

            if (loaded.X != 4)
            {
                throw new Exception("XML serialization is not working properly");
            }


            string fileToAddAndDelete = directory + "DeleteMe.xml";
            FileManager.XmlSerialize<SpriteSave>(spriteSave, fileName);
            FileManager.DeleteFile(fileName);



		}
示例#3
0
        protected override Sprite Read(ContentReader input, Sprite existingInstance)
        {
            if (existingInstance != null)
            {
                return(existingInstance);
            }

            SpriteSave spriteSave = null;

            if (ObjectReader.UseReflection)
            {
                spriteSave = ObjectReader.ReadObject <SpriteSave>(input);
            }
            else
            {
                spriteSave = ReadUsingGeneratedCode(input);

                if (spriteSave.mTextureInstance != null)
                {
                    spriteSave.mTextureInstance.Name = spriteSave.Texture;
                }
            }
            existingInstance = spriteSave.ToSprite("");



            return(existingInstance);
        }
        internal static ReducedQuadInfo FromSpriteSave(SpriteSave spriteSave, int textureWidth, int textureHeight)
        {
            ReducedQuadInfo toReturn = new ReducedQuadInfo();
            toReturn.LeftQuadCoordinate = spriteSave.X - spriteSave.ScaleX;
            toReturn.BottomQuadCoordinate = spriteSave.Y - spriteSave.ScaleY;

            
            bool isRotated = spriteSave.RotationZ != 0;
            if (isRotated)
            {
                toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedDiagonallyFlag);
            }

            var leftTextureCoordinate = System.Math.Min( spriteSave.LeftTextureCoordinate, spriteSave.RightTextureCoordinate);
            var topTextureCoordinate = System.Math.Min(spriteSave.TopTextureCoordinate, spriteSave.BottomTextureCoordinate);

            if (spriteSave.LeftTextureCoordinate > spriteSave.RightTextureCoordinate)
            {
                toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedHorizontallyFlag);
            }
            
            if(spriteSave.TopTextureCoordinate > spriteSave.BottomTextureCoordinate)
            {
                toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedVerticallyFlag);
            }
            
            toReturn.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(leftTextureCoordinate * textureWidth);
            toReturn.TopTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(topTextureCoordinate * textureHeight);

            toReturn.Name = spriteSave.Name;

            return toReturn;
        }
        private static object CreateAndAddInstanceFromClipboard(string instanceType, DataTypes.ArrowElementSave currentArrowElement, string xmlSerializedString)
        {
            object newObject = null;

            if (instanceType == typeof(AxisAlignedRectangleSave).Name)
            {
                AxisAlignedRectangleSave aars = FileManager.XmlDeserializeFromString <AxisAlignedRectangleSave>(
                    xmlSerializedString);

                currentArrowElement.Rectangles.Add(aars);
                newObject = aars;
            }
            else if (instanceType == typeof(CircleSave).Name)
            {
                CircleSave circleSave = FileManager.XmlDeserializeFromString <CircleSave>(
                    xmlSerializedString);

                currentArrowElement.Circles.Add(circleSave);
                newObject = circleSave;
            }
            else if (instanceType == typeof(SpriteSave).Name)
            {
                SpriteSave spriteSave = FileManager.XmlDeserializeFromString <SpriteSave>(
                    xmlSerializedString);

                currentArrowElement.Sprites.Add(spriteSave);
                newObject = spriteSave;
            }

            return(newObject);
        }
        public void TestConversion()
        {
            SpriteSave spriteSave = new SpriteSave();
            SpriteSaveConverter converter = new SpriteSaveConverter();

            var nos = converter.SpriteSaveToNamedObjectSave(spriteSave);

            if (nos.InstructionSaves.Count != 0)
            {
                throw new Exception("A default SpriteSave should have no properties");
            }

            spriteSave.X = 4;
            nos = converter.SpriteSaveToNamedObjectSave(spriteSave);
            if (nos.InstructionSaves.Count != 1)
            {
                throw new Exception("A SpriteSave with non-zero X should have 1 property");
            }

            spriteSave.Texture = "Folder/filename.png";
            nos = converter.SpriteSaveToNamedObjectSave(spriteSave);
            if (nos.GetCustomVariable("Texture").Value as string != "filename")
            {
                throw new Exception("NOS's are not getting the right file name out of SpriteSaves");
            }
        }
示例#7
0
        internal void Sprite()
        {
            if (ArrowState.Self.CurrentArrowElementSave != null)
            {
                TextInputWindow tiw = new TextInputWindow();

                tiw.Text   = "Enter new Sprite name:";
                tiw.Result = "Sprite";
                var result = tiw.ShowDialog();

                if (result.HasValue && result.Value)
                {
                    bool isInvalid = CheckAndShowMessageIfInvalid(tiw.Result);

                    if (!isInvalid)
                    {
                        SpriteSave spriteSave = new SpriteSave();
                        spriteSave.ScaleX         = 16;
                        spriteSave.ScaleY         = 16;
                        spriteSave.Name           = tiw.Result;
                        spriteSave.ColorOperation = "Color";

                        spriteSave.TintRed   = 255;
                        spriteSave.TintGreen = 255;

                        ArrowState.Self.CurrentArrowElementSave.Sprites.Add(spriteSave);

                        AfterAddLogic(ArrowState.Self.CurrentArrowElementSave, spriteSave);
                    }
                }
            }
        }
示例#8
0
        void CustomInitialize()
        {
            SpriteSave spriteSave = new SpriteSave();

            spriteSave.X = 4;
            FileManager.InitializeUserFolder("Global");

            string directory = FileManager.GetUserFolder("Global");
            string fileName  = directory + "TestSave.xml";

            FileManager.XmlSerialize(spriteSave, fileName);


            if (FileManager.FileExists(fileName) == false)
            {
                throw new Exception("The file " + fileName + " does not exist");
            }


            var loaded = FileManager.XmlDeserialize <SpriteSave>(fileName);

            if (loaded.X != 4)
            {
                throw new Exception("XML serialization is not working properly");
            }


            string fileToAddAndDelete = directory + "DeleteMe.xml";

            FileManager.XmlSerialize <SpriteSave>(spriteSave, fileName);
            FileManager.DeleteFile(fileName);
        }
        internal static ReducedQuadInfo FromSpriteSave(SpriteSave spriteSave, int textureWidth, int textureHeight)
        {
            ReducedQuadInfo toReturn = new ReducedQuadInfo();

            toReturn.LeftQuadCoordinate   = spriteSave.X - spriteSave.ScaleX;
            toReturn.BottomQuadCoordinate = spriteSave.Y - spriteSave.ScaleY;


            bool isRotated = spriteSave.RotationZ != 0;

            if (isRotated)
            {
                toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedDiagonallyFlag);
            }

            var leftTextureCoordinate = System.Math.Min(spriteSave.LeftTextureCoordinate, spriteSave.RightTextureCoordinate);
            var topTextureCoordinate  = System.Math.Min(spriteSave.TopTextureCoordinate, spriteSave.BottomTextureCoordinate);

            if (spriteSave.LeftTextureCoordinate > spriteSave.RightTextureCoordinate)
            {
                toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedHorizontallyFlag);
            }

            if (spriteSave.TopTextureCoordinate > spriteSave.BottomTextureCoordinate)
            {
                toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedVerticallyFlag);
            }

            toReturn.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(leftTextureCoordinate * textureWidth);
            toReturn.TopTexturePixel  = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(topTextureCoordinate * textureHeight);

            toReturn.Name = spriteSave.Name;

            return(toReturn);
        }
示例#10
0
        public int Paste(SpriteSave spriteSave, bool setZTo0)
        {
            // here we have the Sprite's X and Y in absolute coords as well as its texture coords
            // NOTE: I appended the Z coordinate for the sake of iso maps. This SHOULDN'T have an effect on the ortho maps since I believe the
            // TMX->SCNX tool sets all z to zero.

            // The AddTile method expects the bottom-left corner
            float x = spriteSave.X - spriteSave.ScaleX;
            float y = spriteSave.Y - spriteSave.ScaleY;
            float z = spriteSave.Z;

            if (setZTo0)
            {
                z = 0;
            }

            float width  = 2f * spriteSave.ScaleX; // w
            float height = 2f * spriteSave.ScaleY; // z

            float topTextureCoordinate    = spriteSave.TopTextureCoordinate;
            float bottomTextureCoordinate = spriteSave.BottomTextureCoordinate;
            float leftTextureCoordinate   = spriteSave.LeftTextureCoordinate;
            float rightTextureCoordinate  = spriteSave.RightTextureCoordinate;

            int tileIndex = mCurrentNumberOfTiles;

            RegisterName(spriteSave.Name, tileIndex);

            // add the textured tile to our map so that we may draw it.
            return(AddTile(new Vector3(x, y, z), new Vector2(width, height), new Vector4(leftTextureCoordinate, rightTextureCoordinate, topTextureCoordinate, bottomTextureCoordinate)));
        }
        public void TestConversion()
        {
            ArrowProjectSave project = new ArrowProjectSave();

            SpriteSave spriteSave = new SpriteSave();
            spriteSave.X = 3;
            spriteSave.Y = 4;
            spriteSave.Name = "SpriteInstance";
            spriteSave.Texture = "Entities/FirstElement/redball.BMP";
            

            ArrowElementSave element = new ArrowElementSave();
            element.Name = "FirstElement";
            element.Sprites.Add(spriteSave);
            project.Elements.Add(element);


            element = new ArrowElementSave();
            element.Name = "ContainerOfFirstElement";
            element.Sprites.Add(spriteSave);
            project.Elements.Add(element);

            ArrowElementInstance instance = new ArrowElementInstance();
            instance.Name = "FirstElementInstance";
            instance.Type = "FirstElement";
            instance.SetVariable("X", 4);
            instance.SetVariable("Y", 5);
            element.ElementInstances.Add(instance);



            ArrowProjectToGlueProjectConverter converter = new ArrowProjectToGlueProjectConverter();

            GlueProjectSave gps = converter.ToGlueProjectSave(project);

            EntitySave firstElementEntity = gps.Entities.FirstOrDefault(item => item.Name == "Entities/FirstElement");
            EntitySave containerOfFirstElement = gps.Entities.FirstOrDefault(item => item.Name == "Entities/ContainerOfFirstElement");
            if (firstElementEntity.Name.StartsWith("Entities/") == false)
            {
                throw new Exception("Entity names must start with \"Entities/\"");
            }

            if (firstElementEntity.ReferencedFiles.Count == 0)
            {
                throw new Exception("The Entity should automatically contain a ReferencedFile for the redball file");
            }

            if (containerOfFirstElement.NamedObjects.FirstOrDefault(item => item.InstanceName == "FirstElementInstance") == null)
            {
                throw new Exception("The entity should contain a NOS for another element, but it doesn't");
            }

            string gluxString;

            FileManager.XmlSerialize(gps, out gluxString);

            string aroxString;
            FileManager.XmlSerialize(project, out aroxString);

        }
示例#12
0
        static object GetValueForPropertyOnObject(SpriteSave sprite, string fieldOrProperty)
        {
            if (fieldOrProperty == "Alpha")
            {
                float valueToDivideBy = 255 / GraphicalEnumerations.MaxColorComponentValue;

                return((255 - sprite.Fade) / valueToDivideBy);
            }
            else if (fieldOrProperty == "PixelSize")
            {
                return(sprite.ConstantPixelSize);
            }
            else if (fieldOrProperty == "CurrentChainName")
            {
                // This thing should use an AnimationChain
                string animationChainRelative = sprite.AnimationChainsFile;

                string animationChainFile = FileManager.RelativeDirectory + sprite.AnimationChainsFile;

                AnimationChainListSave acls = AnimationChainListSave.FromFile(animationChainFile);

                int index = sprite.CurrentChain;

                if (index < acls.AnimationChains.Count)
                {
                    return(acls.AnimationChains[index].Name);
                }
                else
                {
                    return(null);
                }
            }

            return(GetValueForPropertyOnObject <SpriteSave>(sprite, fieldOrProperty));
        }
示例#13
0
        public object UpdateInstanceValuesFromRuntime(ElementRuntime runtime)
        {
            object instance =
                RelationshipManager.Self.InstanceForElementRuntime(runtime);

            object whatToPullValuesFrom = runtime;

            if (runtime.DirectObjectReference != null)
            {
                whatToPullValuesFrom = runtime.DirectObjectReference;
            }
            PositionedObject whatToPullFromAsPo = whatToPullValuesFrom as PositionedObject;

            if (instance is CircleSave)
            {
                CircleSave save = (instance as CircleSave);
                save.SetFrom(whatToPullValuesFrom as Circle);
                SetSavePositionsFromRelativeValues(whatToPullFromAsPo, save);
            }
            else if (instance is SpriteSave)
            {
                SpriteSave save = (instance as SpriteSave);
                save.SetFrom(whatToPullValuesFrom as Sprite);
                SetSavePositionsFromRelativeValues(whatToPullFromAsPo, save);
            }
            else if (instance is AxisAlignedRectangleSave)
            {
                AxisAlignedRectangleSave save = (instance as AxisAlignedRectangleSave);
                save.SetFrom(whatToPullValuesFrom as AxisAlignedRectangle);
                SetSavePositionsFromRelativeValues(whatToPullFromAsPo, save);
            }
            else if (instance is ArrowElementInstance)
            {
                ArrowElementInstance save = (instance as ArrowElementInstance);

                if (whatToPullFromAsPo != null)
                {
                    if (whatToPullFromAsPo.Parent == null)
                    {
                        save.SetVariable("X", whatToPullFromAsPo.X);
                        save.SetVariable("Y", whatToPullFromAsPo.Y);
                    }
                    else
                    {
                        save.SetVariable("X", whatToPullFromAsPo.RelativeX);
                        save.SetVariable("Y", whatToPullFromAsPo.RelativeY);
                    }
                }

                // We can't do this because this object technically doesn't have X and Y properties
                //SetSavePositionsFromRelativeValues(whatToPullFromAsPo, save);
            }
            else
            {
                throw new Exception("Saving of type " + instance.GetType() + " is not supported");
            }

            return(instance);
        }
        private NamedObjectSave CreateSpriteNamedObject(ReferencedFileSave rfs)
        {
            SpriteSave spriteSave = new SpriteSave();

            int imageHeight = AppState.Self.CurrentTileset.Images[0].height;
            int imageWidth  = AppState.Self.CurrentTileset.Images[0].width;

            var currentTileset = AppState.Self.CurrentTileset;
            var id             = (uint)(AppState.Self.CurrentMapTilesetTile.id + currentTileset.Firstgid);


            TMXGlueLib.TiledMapSave.SetSpriteTextureCoordinates(
                id,
                spriteSave,
                currentTileset,
                AppState.Self.CurrentTiledMapSave.orientation);

            NamedObjectSave nos = new NamedObjectSave();

            nos.SourceType      = SourceType.FlatRedBallType;
            nos.SourceClassType = "Sprite";
            nos.InstanceName    = "Sprite";

            nos.InstructionSaves.Add(new CustomVariableInNamedObject()
            {
                Member = "Texture", Type = "Texture2D", Value = rfs.GetInstanceName()
            });

            nos.InstructionSaves.Add(new CustomVariableInNamedObject()
            {
                Member = "TextureScale", Value = 1.0f
            });


            nos.InstructionSaves.Add(new CustomVariableInNamedObject()
            {
                Member = "TopTexturePixel", Value = spriteSave.TopTextureCoordinate * (float)imageHeight
            });

            nos.InstructionSaves.Add(new CustomVariableInNamedObject()
            {
                Member = "BottomTexturePixel", Value = spriteSave.BottomTextureCoordinate * (float)imageHeight
            });

            nos.InstructionSaves.Add(new CustomVariableInNamedObject()
            {
                Member = "LeftTexturePixel", Value = spriteSave.LeftTextureCoordinate * (float)imageWidth
            });

            nos.InstructionSaves.Add(new CustomVariableInNamedObject()
            {
                Member = "RightTexturePixel", Value = spriteSave.RightTextureCoordinate * (float)imageWidth
            });

            return(nos);
        }
        private static void SetQuadWidthAndHeight(ReducedTileMapInfo toReturn, SceneSave ses)
        {
            if (ses.SpriteList.Count != 0)
            {
                SpriteSave spriteSave = ses.SpriteList[0];

                toReturn.QuadWidth  = spriteSave.ScaleX * 2;
                toReturn.QuadHeight = spriteSave.ScaleY * 2;
            }
        }
        internal static ReducedQuadInfo FromSpriteSave(SpriteSave spriteSave, int textureWidth, int textureHeight)
        {
            ReducedQuadInfo toReturn = new ReducedQuadInfo();
            toReturn.LeftQuadCoordinate = spriteSave.X - spriteSave.ScaleX;
            toReturn.BottomQuadCorodinate = spriteSave.Y - spriteSave.ScaleY;

            toReturn.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.LeftTextureCoordinate * textureWidth);
            toReturn.TopTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.TopTextureCoordinate * textureHeight);

            toReturn.Name = spriteSave.Name;

            return toReturn;
        }
        private AnimationChainListSave GetAnimationChainListFromScnxReference(NamedObjectSave referencedNos)
        {
            string sourceFileName = ContentDirectory + referencedNos.SourceFile;

            string sourceFileDirectory = FlatRedBall.IO.FileManager.GetDirectory(sourceFileName);

            AnimationChainListSave acls = null;

            SpriteEditorScene ses;

            if (System.IO.File.Exists(sourceFileName))
            {
                ses = SpriteEditorScene.FromFile(sourceFileName);
                string truncatedName = referencedNos.SourceName.Substring(0, referencedNos.SourceName.LastIndexOf('(') - 1);



                SpriteSave spriteSave = ses.FindSpriteByName(truncatedName);

                if (spriteSave != null && !string.IsNullOrEmpty(spriteSave.AnimationChainsFile))
                {
                    acls = AnimationChainListSave.FromFile(
                        sourceFileDirectory + spriteSave.AnimationChainsFile);
                }

                if (acls == null)
                {
                    SpriteFrameSave sfs = ses.FindSpriteFrameSaveByName(truncatedName);

                    if (sfs != null)
                    {
                        acls = AnimationChainListSave.FromFile(
                            sourceFileDirectory + sfs.ParentSprite.AnimationChainsFile);
                    }
                }
            }



            return(acls);
        }
示例#18
0
        private static void AddResetVariablesForSpriteOrSpriteFrame(NamedObjectSave nos, List <string> variables)
        {
            // todo - support resetting animation chains on Sprites that aren't using a .scnx
            if (nos.SourceType == SourceType.File && !string.IsNullOrEmpty(nos.SourceFile))
            {
                string            fullFile = ProjectManager.MakeAbsolute(nos.SourceFile);
                SpriteEditorScene ses      = SpriteEditorScene.FromFile(fullFile);

                int endingIndex = nos.SourceName.LastIndexOf('(');

                string     nameWithoutType = nos.SourceName.Substring(0, endingIndex - 1);
                SpriteSave ss = ses.FindSpriteByName(nameWithoutType);

                string animationChainFileName = "";
                if (ss != null)
                {
                    animationChainFileName = ss.AnimationChainsFile;
                }
                else
                {
                    SpriteFrameSave sfs = ses.FindSpriteFrameSaveByName(nameWithoutType);
                    if (sfs != null)
                    {
                        animationChainFileName = sfs.ParentSprite.AnimationChainsFile;
                    }
                }

                if (!string.IsNullOrEmpty(animationChainFileName))
                {
                    variables.Add("CurrentChainName");
                    variables.Add("CurrentFrameIndex");
                }
            }

            variables.Add("Alpha");
            variables.Add("AlphaRate");
        }
示例#19
0
        private static void VerifySingleTexture(List <SpriteSave> spriteSaveList, int startingIndex, int count)
        {
            // Every Sprite should either have the same texture
            if (spriteSaveList.Count != 0)
            {
                string texture = spriteSaveList[startingIndex].Texture;

                for (int i = startingIndex + 1; i < startingIndex + count; i++)
                {
                    SpriteSave ss = spriteSaveList[i];

                    if (ss.Texture != texture)
                    {
                        float leftOfSprite = ss.X - ss.ScaleX;
                        float indexX       = leftOfSprite / (ss.ScaleX * 2);

                        float topOfSprite = ss.Y + ss.ScaleY;
                        float indexY      = (0 - topOfSprite) / (ss.ScaleY * 2);

                        throw new Exception("All Sprites do not have the same texture");
                    }
                }
            }
        }
示例#20
0
        private SpriteSave CreateSpriteSaveFromMapTileset(float scale, int layercount, MapLayer mapLayer, int tileIndex, uint gid, Tileset tileSet, FileReferenceType referenceType = FileReferenceType.NoDirectory)
        {
            var sprite = new SpriteSave();

            if (!mapLayer.IsVisible && mapLayer.VisibleBehavior == LayerVisibleBehavior.Match)
            {
                sprite.Visible = false;
            }

            int imageWidth  = tileSet.Images[0].width;
            int imageHeight = tileSet.Images[0].height;
            int tileWidth   = tileSet.Tilewidth;
            int spacing     = tileSet.Spacing;
            int tileHeight  = tileSet.Tileheight;
            int margin      = tileSet.Margin;

            // TODO: only calculate these once per tileset. Perhaps it can be done in the deserialize method
            //int tilesWide = (imageWidth - margin) / (tileWidth + spacing);
            //int tilesHigh = (imageHeight - margin) / (tileHeight + spacing);

            if (referenceType == FileReferenceType.NoDirectory)
            {
                sprite.Texture = tileSet.Images[0].sourceFileName;
            }
            else if (referenceType == FileReferenceType.Absolute)
            {
                string directory = FileManager.GetDirectory(this.FileName);

                if (!string.IsNullOrEmpty(tileSet.SourceDirectory) && tileSet.SourceDirectory != ".")
                {
                    directory += tileSet.SourceDirectory;

                    directory = FileManager.RemoveDotDotSlash(directory);
                }

                sprite.Texture = FileManager.RemoveDotDotSlash(directory + tileSet.Images[0].Source);
            }
            else
            {
                throw new NotImplementedException();
            }

            uint tileTextureRelativeToStartOfTileset =
                (0x0fffffff & gid) - tileSet.Firstgid + 1;

            if (tileSet.TileDictionary.ContainsKey(tileTextureRelativeToStartOfTileset))
            {
                var dictionary = tileSet.TileDictionary[tileTextureRelativeToStartOfTileset].PropertyDictionary;

                foreach (var kvp in dictionary)
                {
                    var key = kvp.Key;

                    if (IsName(key))
                    {
                        sprite.Name = kvp.Value;
                    }
                }
            }

            if (string.IsNullOrEmpty(sprite.Name))
            {
                sprite.Name = "Unnamed" + gid;
            }

            SetSpriteTextureCoordinates(gid, sprite, tileSet);
            CalculateWorldCoordinates(layercount, tileIndex, tileWidth, tileHeight, this.Width, out sprite.X, out sprite.Y, out sprite.Z);

            sprite.ScaleX = tileWidth / 2.0f;
            sprite.ScaleY = tileHeight / 2.0f;

            if (tileSet.Tileoffset != null && tileSet.Tileoffset.Length == 1)
            {
                sprite.X += tileSet.Tileoffset[0].x;
                sprite.Y -= tileSet.Tileoffset[0].y;
            }


            sprite.X *= scale;
            sprite.Y *= scale;
            // Update August 28, 2012
            // The TMX converter splits
            // the Layers by their Z values.
            // We want each Layer to have its
            // own explicit Z value, so we don't
            // want to adjust the Z's when we scale:
            //sprite.Z *= scale;

            sprite.ScaleX *= scale;
            sprite.ScaleY *= scale;
            return(sprite);
        }
示例#21
0
 public void Paste(SpriteSave spriteSave)
 {
     Paste(spriteSave, false);
 }
示例#22
0
        /* This creates a MapDrawableBatch (MDB) from the list of sprites provided to us by the FlatRedBall (FRB) Scene XML (scnx) file. */
        public static MapDrawableBatch FromSpriteSaves(List <SpriteSave> spriteSaveList, int startingIndex, int count, string contentManagerName, bool verifySameTexturesPerLayer)
        {
#if DEBUG
            if (verifySameTexturesPerLayer)
            {
                VerifySingleTexture(spriteSaveList, startingIndex, count);
            }
#endif

            // We got it!  We are going to make some assumptions:
            // First we need the texture.  We'll assume all Sprites
            // use the same texture:

            // TODO: I (Bryan) really HATE this assumption. But it will work for now.
            SpriteSave firstSprite = spriteSaveList[startingIndex];

            // This is the file name of the texture, but the file name is relative to the .scnx location
            string textureRelativeToScene = firstSprite.Texture;
            // so we load the texture
            Texture2D texture = FlatRedBallServices.Load <Texture2D>(textureRelativeToScene, contentManagerName);

            if (!MathFunctions.IsPowerOfTwo(texture.Width) || !MathFunctions.IsPowerOfTwo(texture.Height))
            {
                throw new Exception("The dimensions of the texture file " + texture.Name + " are not power of 2!");
            }


            // Assume all the dimensions of the textures are the same. I.e. all tiles use the same texture width and height.
            // This assumption is safe for Iso and Ortho tile maps.
            int tileFileDimensionsWidth  = 0;
            int tileFileDimensionsHeight = 0;
            if (spriteSaveList.Count > startingIndex)
            {
                SpriteSave s = spriteSaveList[startingIndex];

                // deduce the dimensionality of the tile from the texture coordinates
                tileFileDimensionsWidth  = (int)System.Math.Round((double)((s.RightTextureCoordinate - s.LeftTextureCoordinate) * texture.Width));
                tileFileDimensionsHeight = (int)System.Math.Round((double)((s.BottomTextureCoordinate - s.TopTextureCoordinate) * texture.Height));
            }


            // alas, we create the MDB
            MapDrawableBatch mMapBatch = new MapDrawableBatch(count, tileFileDimensionsWidth, tileFileDimensionsHeight, texture);

            int lastIndexExclusive = startingIndex + count;

            for (int i = startingIndex; i < lastIndexExclusive; i++)
            {
                SpriteSave spriteSave = spriteSaveList[i];

                // We don't want objects within the IDB to have a different Z than the IDB itself
                // (if possible) because that makes the IDB behave differently when using sorting vs.
                // the zbuffer.
                const bool setZTo0 = true;
                mMapBatch.Paste(spriteSave, setZTo0);
            }



            return(mMapBatch);
        }
示例#23
0
 public void Loadsprite(SpriteSave spriteSave)
 {
     textureFile = spriteSave.textureFile;
     position    = spriteSave.position;
     color       = spriteSave.color;
 }
示例#24
0
        public SceneSave ToSceneSave(float scale, FileReferenceType referenceType = FileReferenceType.NoDirectory)
        {
            var toReturn = new SceneSave {
                CoordinateSystem = FlatRedBall.Math.CoordinateSystem.RightHanded
            };

            // TODO: Somehow add all layers separately

            int layercount = 0;

            foreach (MapLayer mapLayer in this.Layers)
            {
                if (!mapLayer.IsVisible)
                {
                    switch (mapLayer.VisibleBehavior)
                    {
                    case LayerVisibleBehavior.Ignore:
                        break;

                    case LayerVisibleBehavior.Skip:
                        continue;
                    }
                }

                MapLayer mLayer      = mapLayer;
                int      mLayerCount = layercount;

                for (int i = 0; i < mapLayer.data[0].tiles.Count; i++)
                //Parallel.For(0, mapLayer.data[0].tiles.Count, i =>
                {
                    uint    gid     = mLayer.data[0].tiles[i];
                    Tileset tileSet = GetTilesetForGid(gid);
                    if (tileSet != null)
                    {
                        SpriteSave sprite = CreateSpriteSaveFromMapTileset(scale, mLayerCount, mLayer, i, gid, tileSet, referenceType);
                        lock (toReturn)
                        {
                            toReturn.SpriteList.Add(sprite);
                        }
                    }
                }
                //);
                ++layercount;
            }
            if (this.objectgroup != null && this.objectgroup.Count != 0)
            {
                foreach (mapObjectgroup group in this.objectgroup)
                {
                    if (group.@object != null && !string.IsNullOrEmpty(group.name)) //&& (string.IsNullOrEmpty(layerName) || group.name.Equals(layerName)))
                    {
                        foreach (mapObjectgroupObject @object in group.@object)
                        {
                            if (!String.IsNullOrEmpty(@object.gid))
                            {
                                SpriteSave sprite = CreateSpriteSaveFromObject(scale, @object, layercount, referenceType);
                                lock (toReturn)
                                {
                                    toReturn.SpriteList.Add(sprite);
                                }
                            }
                        }
                    }
                }
            }
            return(toReturn);
        }
        /* This creates a MapDrawableBatch (MDB) from the list of sprites provided to us by the FlatRedBall (FRB) Scene XML (scnx) file. */
        public static MapDrawableBatch FromSpriteSaves(List <SpriteSave> spriteSaveList, int startingIndex, int count, string contentManagerName, bool verifySameTexturesPerLayer)
        {
#if DEBUG
            if (verifySameTexturesPerLayer)
            {
                VerifySingleTexture(spriteSaveList, startingIndex, count);
            }
#endif

            // We got it!  We are going to make some assumptions:
            // First we need the texture.  We'll assume all Sprites
            // use the same texture:

            // TODO: I (Bryan) really HATE this assumption. But it will work for now.
            SpriteSave firstSprite = spriteSaveList[startingIndex];

            // This is the file name of the texture, but the file name is relative to the .scnx location
            string textureRelativeToScene = firstSprite.Texture;
            // so we load the texture
            Texture2D texture = FlatRedBallServices.Load <Texture2D>(textureRelativeToScene, contentManagerName);

            if (!MathFunctions.IsPowerOfTwo(texture.Width) || !MathFunctions.IsPowerOfTwo(texture.Height))
            {
                throw new Exception("The dimensions of the texture file " + texture.Name + " are not power of 2!");
            }


            // Assume all the dimensions of the textures are the same. I.e. all tiles use the same texture width and height.
            // This assumption is safe for Iso and Ortho tile maps.
            int tileFileDimensionsWidth  = 0;
            int tileFileDimensionsHeight = 0;
            if (spriteSaveList.Count > startingIndex)
            {
                SpriteSave s = spriteSaveList[startingIndex];

                // deduce the dimensionality of the tile from the texture coordinates
                tileFileDimensionsWidth  = (int)System.Math.Round((double)((s.RightTextureCoordinate - s.LeftTextureCoordinate) * texture.Width));
                tileFileDimensionsHeight = (int)System.Math.Round((double)((s.BottomTextureCoordinate - s.TopTextureCoordinate) * texture.Height));
            }


            // alas, we create the MDB
            MapDrawableBatch mMapBatch = new MapDrawableBatch(count, tileFileDimensionsWidth, tileFileDimensionsHeight, texture);

            int lastIndexExclusive = startingIndex + count;

            for (int i = startingIndex; i < lastIndexExclusive; i++)
            {
                SpriteSave spriteSave = spriteSaveList[i];

                // here we have the Sprite's X and Y in absolute coords as well as its texture coords
                // NOTE: I appended the Z coordinate for the sake of iso maps. This SHOULDN'T have an effect on the ortho maps since I believe the
                // TMX->SCNX tool sets all z to zero.

                // The AddTile method expects the bottom-left corner
                float x = spriteSave.X - spriteSave.ScaleX;
                float y = spriteSave.Y - spriteSave.ScaleY;
                float z = spriteSave.Z;

                float width  = 2f * spriteSave.ScaleX; // w
                float height = 2f * spriteSave.ScaleY; // z

                float topTextureCoordinate    = spriteSave.TopTextureCoordinate;
                float bottomTextureCoordinate = spriteSave.BottomTextureCoordinate;
                float leftTextureCoordinate   = spriteSave.LeftTextureCoordinate;
                float rightTextureCoordinate  = spriteSave.RightTextureCoordinate;

                int tileIndex = mMapBatch.mCurrentNumberOfTiles;

                mMapBatch.RegisterName(spriteSave.Name, tileIndex);

                // add the textured tile to our map so that we may draw it.
                mMapBatch.AddTile(new Vector3(x, y, z), new Vector2(width, height), new Vector4(leftTextureCoordinate, rightTextureCoordinate, topTextureCoordinate, bottomTextureCoordinate));
            }



            return(mMapBatch);
        }
示例#26
0
        public void TestConversion()
        {
            ArrowProjectSave project = new ArrowProjectSave();

            SpriteSave spriteSave = new SpriteSave();

            spriteSave.X       = 3;
            spriteSave.Y       = 4;
            spriteSave.Name    = "SpriteInstance";
            spriteSave.Texture = "Entities/FirstElement/redball.BMP";


            ArrowElementSave element = new ArrowElementSave();

            element.Name = "FirstElement";
            element.Sprites.Add(spriteSave);
            project.Elements.Add(element);


            element      = new ArrowElementSave();
            element.Name = "ContainerOfFirstElement";
            element.Sprites.Add(spriteSave);
            project.Elements.Add(element);

            ArrowElementInstance instance = new ArrowElementInstance();

            instance.Name = "FirstElementInstance";
            instance.Type = "FirstElement";
            instance.SetVariable("X", 4);
            instance.SetVariable("Y", 5);
            element.ElementInstances.Add(instance);



            ArrowProjectToGlueProjectConverter converter = new ArrowProjectToGlueProjectConverter();

            GlueProjectSave gps = converter.ToGlueProjectSave(project);

            EntitySave firstElementEntity      = gps.Entities.FirstOrDefault(item => item.Name == "Entities/FirstElement");
            EntitySave containerOfFirstElement = gps.Entities.FirstOrDefault(item => item.Name == "Entities/ContainerOfFirstElement");

            if (firstElementEntity.Name.StartsWith("Entities/") == false)
            {
                throw new Exception("Entity names must start with \"Entities/\"");
            }

            if (firstElementEntity.ReferencedFiles.Count == 0)
            {
                throw new Exception("The Entity should automatically contain a ReferencedFile for the redball file");
            }

            if (containerOfFirstElement.NamedObjects.FirstOrDefault(item => item.InstanceName == "FirstElementInstance") == null)
            {
                throw new Exception("The entity should contain a NOS for another element, but it doesn't");
            }

            string gluxString;

            FileManager.XmlSerialize(gps, out gluxString);

            string aroxString;

            FileManager.XmlSerialize(project, out aroxString);
        }
示例#27
0
        public void SetSpriteTextureCoordinates(uint gid, SpriteSave sprite, Tileset tileSet)
        {
            int imageWidth  = tileSet.Images[0].width;
            int imageHeight = tileSet.Images[0].height;
            int tileWidth   = tileSet.Tilewidth;
            int spacing     = tileSet.Spacing;
            int tileHeight  = tileSet.Tileheight;
            int margin      = tileSet.Margin;


            int leftPixelCoord;
            int topPixelCoord;
            int rightPixelCoord;
            int bottomPixelCoord;

            GetPixelCoordinatesFromGid(gid, tileSet,
                                       out leftPixelCoord, out topPixelCoord, out rightPixelCoord, out bottomPixelCoord);


            bool       flipDiagonally;
            var        gidWithoutRotation    = gid & 0x0fffffff;
            const uint FlippedDiagonallyFlag = 0x20000000;

            flipDiagonally = (gid & FlippedDiagonallyFlag) == FlippedDiagonallyFlag;


            //if (flipDiagonally)
            //{
            //    // this turns:
            //    // 1---2
            //    // |   |
            //    // 3---4

            //    // into:
            //    // 1---3
            //    // |   |
            //    // 2---4

            //    int newLeft = topPixelCoord;
            //    int newRight = bottomPixelCoord;
            //    int newTop = leftPixelCoord;
            //    int newBottom = rightPixelCoord;

            //    topPixelCoord = newTop;
            //    bottomPixelCoord = newBottom;
            //    leftPixelCoord = newLeft;
            //    rightPixelCoord = newRight;
            //}

            // Calculate relative texture coordinates based on pixel coordinates
            var changeVal = LessOrGreaterDesired.Greater;

            if (this.orientation != null && this.orientation.Equals("isometric"))
            {
                changeVal = LessOrGreaterDesired.NoChange;
            }

            sprite.TopTextureCoordinate  = GetTextureCoordinate(topPixelCoord, imageHeight, changeVal);
            sprite.LeftTextureCoordinate = GetTextureCoordinate(leftPixelCoord, imageWidth, changeVal);

            changeVal = LessOrGreaterDesired.Less;
            if (this.orientation != null && this.orientation.Equals("isometric"))
            {
                changeVal = LessOrGreaterDesired.NoChange;
            }

            sprite.RightTextureCoordinate  = GetTextureCoordinate(rightPixelCoord, imageWidth, changeVal);
            sprite.BottomTextureCoordinate = GetTextureCoordinate(bottomPixelCoord, imageHeight, changeVal);

            if (flipDiagonally)
            {
                sprite.RotationZ      = Microsoft.Xna.Framework.MathHelper.PiOver2;
                sprite.FlipHorizontal = true;
            }
        }
        /// <summary>
        /// Converts a TiledMapSave to a ReducedTileMapInfo object
        /// </summary>
        /// <param name="tiledMapSave">The TiledMapSave to convert</param>
        /// <param name="scale">The amount to scale by - default of 1</param>
        /// <param name="zOffset">The zOffset</param>
        /// <param name="directory">The directory of the file associated with the tiledMapSave, used to find file references.</param>
        /// <param name="referenceType">How the files in the .tmx are referenced.</param>
        /// <returns></returns>
        public static ReducedTileMapInfo FromTiledMapSave(TiledMapSave tiledMapSave, float scale, float zOffset, string directory, FileReferenceType referenceType)
        {
            var toReturn = new ReducedTileMapInfo();

            toReturn.NumberCellsTall = tiledMapSave.Height;
            toReturn.NumberCellsWide = tiledMapSave.Width;

            var ses = tiledMapSave.ToSceneSave(scale, referenceType);

            ses.SpriteList.Sort((first, second) => first.Z.CompareTo(second.Z));

            ReducedLayerInfo reducedLayerInfo = null;

            // If we rely on the image, it's both slow (have to open the images), and
            // doesn't work at runtime in games:
            //Dictionary<string, Point> loadedTextures = new Dictionary<string, Point>();
            //SetCellWidthAndHeight(tiledMapSave, directory, toReturn, ses, loadedTextures);

            toReturn.CellHeightInPixels = (ushort)tiledMapSave.tileheight;
            toReturn.CellWidthInPixels  = (ushort)tiledMapSave.tilewidth;


            SetQuadWidthAndHeight(toReturn, ses);

            float z = float.NaN;


            int textureWidth  = 0;
            int textureHeight = 0;


            for (int i = 0; i < ses.SpriteList.Count; i++)
            {
                SpriteSave spriteSave = ses.SpriteList[i];

                if (spriteSave.Z != z)
                {
                    z = spriteSave.Z;
                    reducedLayerInfo         = new ReducedLayerInfo();
                    reducedLayerInfo.Z       = spriteSave.Z;
                    reducedLayerInfo.Texture = spriteSave.Texture;

                    int layerIndex = FlatRedBall.Math.MathFunctions.RoundToInt(z - zOffset);
                    var mapLayer   = tiledMapSave.Layers[layerIndex];


                    // This should have data:

                    var     idOfTexture  = mapLayer.data[0].tiles.FirstOrDefault(item => item != 0);
                    Tileset tileSet      = tiledMapSave.GetTilesetForGid(idOfTexture);
                    var     tilesetIndex = tiledMapSave.Tilesets.IndexOf(tileSet);

                    textureWidth  = tileSet.Images[0].width;
                    textureHeight = tileSet.Images[0].height;

                    reducedLayerInfo.Name      = mapLayer.Name;
                    reducedLayerInfo.TextureId = tilesetIndex;
                    toReturn.Layers.Add(reducedLayerInfo);
                }

                ReducedQuadInfo quad = ReducedQuadInfo.FromSpriteSave(spriteSave, textureWidth, textureHeight);
                reducedLayerInfo.Quads.Add(quad);
            }
            return(toReturn);
        }
示例#29
0
        public SpriteSave SaveSprite()
        {
            SpriteSave spriteSave = new SpriteSave(textureFile, position, color);

            return(spriteSave);
        }
示例#30
0
 public void Paste(SpriteSave spriteSave)
 {
     Paste(spriteSave, false);
 }
示例#31
0
        public int Paste(SpriteSave spriteSave, bool setZTo0)
        {
            // here we have the Sprite's X and Y in absolute coords as well as its texture coords
            // NOTE: I appended the Z coordinate for the sake of iso maps. This SHOULDN'T have an effect on the ortho maps since I believe the
            // TMX->SCNX tool sets all z to zero.

            // The AddTile method expects the bottom-left corner
            float x = spriteSave.X - spriteSave.ScaleX;
            float y = spriteSave.Y - spriteSave.ScaleY;
            float z = spriteSave.Z;
            if (setZTo0)
            {
                z = 0;
            }

            float width = 2f * spriteSave.ScaleX; // w
            float height = 2f * spriteSave.ScaleY; // z

            float topTextureCoordinate = spriteSave.TopTextureCoordinate;
            float bottomTextureCoordinate = spriteSave.BottomTextureCoordinate;
            float leftTextureCoordinate = spriteSave.LeftTextureCoordinate;
            float rightTextureCoordinate = spriteSave.RightTextureCoordinate;

            int tileIndex = mCurrentNumberOfTiles;

            RegisterName(spriteSave.Name, tileIndex);

            // add the textured tile to our map so that we may draw it.
            return AddTile(new Vector3(x, y, z), new Vector2(width, height), new Vector4(leftTextureCoordinate, rightTextureCoordinate, topTextureCoordinate, bottomTextureCoordinate));
        }
示例#32
0
        internal void Sprite()
        {
            if (ArrowState.Self.CurrentArrowElementSave != null)
            {
                
                TextInputWindow tiw = new TextInputWindow();

                tiw.Text = "Enter new Sprite name:";
                tiw.Result = "Sprite";
                var result = tiw.ShowDialog();

                if (result.HasValue && result.Value)
                {
                    bool isInvalid = CheckAndShowMessageIfInvalid(tiw.Result);

                    if (!isInvalid)
                    {
                        SpriteSave spriteSave = new SpriteSave();
                        spriteSave.ScaleX = 16;
                        spriteSave.ScaleY = 16;
                        spriteSave.Name = tiw.Result;
                        spriteSave.ColorOperation = "Color";

                        spriteSave.TintRed = 255;
                        spriteSave.TintGreen = 255;

                        ArrowState.Self.CurrentArrowElementSave.Sprites.Add(spriteSave);

                        AfterAddLogic(ArrowState.Self.CurrentArrowElementSave, spriteSave);
                    }
                }
            }
        }
示例#33
0
        public static SpriteSave ReadUsingGeneratedCode(ContentReader input)
        {
            SpriteSave newObject = new SpriteSave();

            if (input.ReadBoolean())
            {
                newObject.mTextureInstance = input.ReadExternalReference <Microsoft.Xna.Framework.Graphics.Texture2D>();
            }
            if (input.ReadBoolean())
            {
                newObject.mAnimationChainListInstance = input.ReadExternalReference <FlatRedBall.Graphics.Animation.AnimationChainList>();
            }
            newObject.X                       = input.ReadSingle();
            newObject.Y                       = input.ReadSingle();
            newObject.Z                       = input.ReadSingle();
            newObject.XVelocity               = input.ReadSingle();
            newObject.YVelocity               = input.ReadSingle();
            newObject.ZVelocity               = input.ReadSingle();
            newObject.XAcceleration           = input.ReadSingle();
            newObject.YAcceleration           = input.ReadSingle();
            newObject.ZAcceleration           = input.ReadSingle();
            newObject.RotationX               = input.ReadSingle();
            newObject.RotationY               = input.ReadSingle();
            newObject.RotationZ               = input.ReadSingle();
            newObject.RotationZVelocity       = input.ReadSingle();
            newObject.ScaleX                  = input.ReadSingle();
            newObject.ScaleY                  = input.ReadSingle();
            newObject.ScaleXVelocity          = input.ReadSingle();
            newObject.ScaleYVelocity          = input.ReadSingle();
            newObject.RelativeX               = input.ReadSingle();
            newObject.RelativeY               = input.ReadSingle();
            newObject.RelativeZ               = input.ReadSingle();
            newObject.RelativeRotationX       = input.ReadSingle();
            newObject.RelativeRotationY       = input.ReadSingle();
            newObject.RelativeRotationZ       = input.ReadSingle();
            newObject.Fade                    = input.ReadSingle();
            newObject.FadeRate                = input.ReadSingle();
            newObject.TintRed                 = input.ReadSingle();
            newObject.TintGreen               = input.ReadSingle();
            newObject.TintBlue                = input.ReadSingle();
            newObject.TintRedRate             = input.ReadSingle();
            newObject.TintBlueRate            = input.ReadSingle();
            newObject.TintGreenRate           = input.ReadSingle();
            newObject.ColorOperation          = input.ReadString();
            newObject.BlendOperation          = input.ReadString();
            newObject.Name                    = input.ReadString();
            newObject.Parent                  = input.ReadString();
            newObject.Texture                 = input.ReadString();
            newObject.Animate                 = input.ReadBoolean();
            newObject.CurrentChain            = input.ReadInt32();
            newObject.AnimationChainsFile     = input.ReadString();
            newObject.Type                    = input.ReadString();
            newObject.Ordered                 = input.ReadBoolean();
            newObject.Active                  = input.ReadBoolean();
            newObject.ConstantPixelSize       = input.ReadSingle();
            newObject.Visible                 = input.ReadBoolean();
            newObject.TopTextureCoordinate    = input.ReadSingle();
            newObject.BottomTextureCoordinate = input.ReadSingle();
            newObject.LeftTextureCoordinate   = input.ReadSingle();
            newObject.RightTextureCoordinate  = input.ReadSingle();
            newObject.FlipHorizontal          = input.ReadBoolean();
            newObject.FlipVertical            = input.ReadBoolean();
            newObject.TextureAddressMode      = (Microsoft.Xna.Framework.Graphics.TextureAddressMode)Enum.ToObject(typeof(Microsoft.Xna.Framework.Graphics.TextureAddressMode), (int)input.ReadInt32());

            /////////////CUSTOM CODE
            if (newObject.mTextureInstance != null)
            {
                newObject.mTextureInstance.Name = newObject.Texture;
            }

            ///////////////END CUSTOM////////////

            return(newObject);
        }
        private static Point GetTextureDimensions(string directory, Dictionary<string, Point> loadedTextures, SpriteSave spriteSave)
        {
            string absoluteFile = directory + spriteSave.Texture;

            Point point;
            if (loadedTextures.ContainsKey(absoluteFile))
            {
                point = loadedTextures[absoluteFile];
            }
            else
            {
                point = GetDimensions(absoluteFile);
                loadedTextures.Add(absoluteFile, point);
            }
            return point;
        }