private static void ParseOptionalCommandLineArgs(string[] args, out TiledMapSave.CSVPropertyType type, out string layerName)
        {
            type = TiledMapSave.CSVPropertyType.Tile;
            layerName = null;
            if (args.Length >= 3)
            {
                for (int x = 2; x < args.Length; ++x)
                {
                    string arg = args[x];
                    string[] tokens = arg.Split("=".ToCharArray());
                    if (tokens.Length == 2)
                    {
                        string key = tokens[0];
                        string value = tokens[1];

                        switch (key.ToLowerInvariant())
                        {
                            case "type":
                                const bool ignoreCase = true;
                                if (!Enum.TryParse(value, ignoreCase, out type))
                                {
                                    type = TiledMapSave.CSVPropertyType.Tile;
                                }
                                break;
                            case "layername":
                                layerName = value;
                                break;
                            default:
                                Console.Error.WriteLine("Invalid command line argument: {0}", arg);
                                break;
                        }
                    }
                }
            }
        }
        private static bool GetIfHasMultiTextureLayers(TiledMapSave tms)
        {
            foreach(var layer in tms.Layers)
            {
                Tileset tileset = null;

                foreach(var dataItem in layer.data)
                {
                    foreach(var tile in dataItem.tiles)
                    {
                        var tilesetForThis = tms.GetTilesetForGid(tile);

                        if(tileset == null)
                        {
                            tileset = tilesetForThis;
                        }
                        else if (tilesetForThis != null && tileset != tilesetForThis)
                        {
                            // early out for perf.
                            return true;
                        }
                    }
                }

            }

            return false;
        }
        public void ToCSVStringLayerPropertyTest()
        {
            const TiledMapSave.CSVPropertyType type = TiledMapSave.CSVPropertyType.Layer;
            var target = new TiledMapSave
            {
                Layers = new List<MapLayer> {new MapLayer()
                {
                    properties = new List<property>
                    {
                        new property() {name = "name1", value = "val"},
                        new property() {name = "name2", value = "val2"}
                    },
                    Name="layer1"
                }, new MapLayer()
                    {
                        properties = new List<property>
                        {
                            new property() {name = "name1", value = "val"},
                            new property() {name = "name3", value = "val3"}
                        },
                        Name="layer2"
                    }}
            };

            var expected =
                "Name (required),name2,name1,name3\r\nlayer1,\"val2\",\"val\",\r\nlayer2,,\"val\",\"val3\"\r\n";
            // TODO: Initialize to an appropriate value
            var actual = target.ToCSVString(type, null);
            Assert.AreEqual(expected, actual);

            string layerName = "layer1";
            expected = "Name (required),name2,name1\r\nlayer1,\"val2\",\"val\"\r\n";
            actual = target.ToCSVString(type, layerName);
            Assert.AreEqual(expected, actual);
        }
        public static bool CopyTmxTilesetImagesToDestination(string sourceTmx, string destinationScnx, TiledMapSave tms)
        {
            bool success = true;
            //////////Early Out///////////////////
            if (tms.Tilesets == null)
            {
                // Not sure if we should consider this a success or failure
                return success;
            }
            ////////End Early Out////////////////

            var oldDir = FileManager.RelativeDirectory;

            FileManager.RelativeDirectory = FileManager.GetDirectory(sourceTmx);
            string tmxPath = FileManager.RelativeDirectory;
            string destinationPath = FileManager.GetDirectory(destinationScnx);

            foreach (Tileset tileset in tms.Tilesets)
            {
                foreach (TilesetImage image in tileset.Images)
                {
                    bool didCopySucceed = TryCopyTilesetImage(tmxPath, destinationPath, tileset, image);

                    if (!didCopySucceed)
                    {
                        success = false;
                    }
                }
            }

            return success;
        }
        public static ShapeCollection ToShapeCollection(this TiledMapSave tiledMapSave, string layerName)
        {
            MapLayer mapLayer = null;

            if (!string.IsNullOrEmpty(layerName))
            {
                mapLayer = tiledMapSave.Layers.FirstOrDefault(l => l.Name.Equals(layerName));
            }
            var shapes = new ShapeCollection();

            if ((mapLayer != null && !mapLayer.IsVisible && mapLayer.VisibleBehavior == TMXGlueLib.TiledMapSave.LayerVisibleBehavior.Skip) ||
                tiledMapSave.objectgroup == null || tiledMapSave.objectgroup.Count == 0)
            {
                return(shapes);
            }

            foreach (mapObjectgroup group in tiledMapSave.objectgroup)
            {
                if (group.@object != null && !string.IsNullOrEmpty(group.Name) && (string.IsNullOrEmpty(layerName) || group.Name.Equals(layerName)))
                {
                    foreach (mapObjectgroupObject @object in group.@object)
                    {
                        AddShapeToShapeCollection(@object, shapes);
                    }
                }
            }
            return(shapes);
        }
 public static void FixupImageSources(TiledMapSave tms)
 {
     Console.WriteLine("Fixing up tileset relative paths");
     if (tms.tileset != null)
     {
         foreach (mapTileset tileset in tms.tileset)
         {
             foreach (mapTilesetImage image in tileset.Image)
             {
                 image.source = image.sourceFileName;
             }
         }
     }
 }
        private static void ParseOptionalCommandLineArgs(string[] args, out float scale, out TiledMapSave.LayerVisibleBehavior layerVisibleBehavior, out Tuple<float, float, float> offset)
        {
            scale = 1.0f;
            layerVisibleBehavior = TiledMapSave.LayerVisibleBehavior.Ignore;
            offset = new Tuple<float, float, float>(0f, 0f, 0f);
            for (int x = 2; x < args.Length; ++x)
            {
                string arg = args[x];
                string[] tokens = arg.Split("=".ToCharArray());
                if (tokens.Length == 2)
                {
                    string key = tokens[0];
                    string value = tokens[1];

                    switch (key.ToLowerInvariant())
                    {
                        case "scale":
                            if (!float.TryParse(value, out scale))
                            {
                                scale = 1.0f;
                            }
                            break;
                        case "layervisiblebehavior":
                            if (!Enum.TryParse(value, out layerVisibleBehavior))
                            {
                                layerVisibleBehavior = TiledMapSave.LayerVisibleBehavior.Ignore;
                            }
                            break;
                        case "offset":
                            string[] tupleVals = value.Split(",".ToCharArray());
                            if (tupleVals.Length == 3)
                            {
                                float xf, yf, zf;
                                if (float.TryParse(tupleVals[0], out xf) && float.TryParse(tupleVals[1], out yf) &&
                                    float.TryParse(tupleVals[2], out zf))
                                {
                                    offset = new Tuple<float, float, float>(xf, yf, zf);
                                }
                            }
                            break;
                        default:
                            Console.Error.WriteLine("Invalid command line argument: {0}", arg);
                            break;
                    }
                }
            }
        }
Esempio n. 8
0
        private static PolygonSave ConvertTmxObjectToFrbPolygonSave(this TiledMapSave tiledMapSave, string name, double x, double y, double w, double h, double rotation, mapObjectgroupObjectEllipse ellipse)
        {
            var pointsSb = new StringBuilder();

            if (ellipse == null)
            {
                pointsSb.AppendFormat("{0},{1}", -w / 2, -h / 2);

                pointsSb.AppendFormat(" {0},{1}", w / 2, -h / 2);
                pointsSb.AppendFormat(" {0},{1}", w / 2, h / 2);
                pointsSb.AppendFormat(" {0},{1}", -w / 2, h / 2);
            }
            else
            {
                const double a = .5;
                const double b = .5;

                // x = a cos t
                // y = b cos t
                var    first      = true;
                string firstPoint = "";
                for (var angle = 0; angle <= 360; angle += 18)
                {
                    var radians = MathHelper.ToRadians(angle);


                    // This code made the position of the poly be top left, not optimized!
                    //var newx = a*Math.Cos(radians)*w+w/2;
                    //var newy = b*Math.Sin(radians)*h+h/2;

                    var newx = a * Math.Cos(radians) * w;
                    var newy = b * Math.Sin(radians) * h;

                    if (first)
                    {
                        firstPoint = string.Format("{0},{1}", newx, newy);
                    }
                    pointsSb.AppendFormat("{2}{0},{1}", newx, newy, first ? "" : " ");
                    first = false;
                }

                pointsSb.AppendFormat(" {0}", firstPoint);
            }

            return(tiledMapSave.ConvertTmxObjectToFrbPolygonSave(name, x + w / 2.0f, y + h / 2.0f, rotation, pointsSb.ToString(), true));
        }
        public static void CopyTmxTilesetImagesToDestination(string sourceTmx, string destinationScnx, TiledMapSave tms)
        {
            //////////Early Out///////////////////
            if (tms.tileset == null)
            {
                return;
            }
            ////////End Early Out////////////////

            string tmxPath = sourceTmx.Substring(0, sourceTmx.LastIndexOf('\\'));
            string destinationPath = destinationScnx.Contains("\\") ? destinationScnx.Substring(0, destinationScnx.LastIndexOf('\\')) : ".";

            foreach (mapTileset tileset in tms.tileset)
            {
                foreach (mapTilesetImage image in tileset.Image)
                {
                    string sourcepath = tmxPath + "\\" + image.source;
                    if (tileset.Source != null)
                    {
                        if (tileset.SourceDirectory != ".")
                        {
                            sourcepath = tmxPath + "\\" + tileset.SourceDirectory + "\\" + image.source;
                        }
                        else
                        {
                            sourcepath = tmxPath + "\\" + image.source;

                        }
                    }
                    string destinationFullPath = destinationPath + "\\" + image.sourceFileName;
                    if (!sourcepath.Equals(destinationFullPath, StringComparison.InvariantCultureIgnoreCase) &&
                        !FileManager.GetDirectory(destinationFullPath).Equals(FileManager.GetDirectory(sourcepath)))
                    {
                        Console.WriteLine("Copying \"{0}\" to \"{1}\".", sourcepath, destinationFullPath);

                        File.Copy(sourcepath, destinationFullPath, true);
                    }
                }
            }
        }
        private TiledMapSave CreateTileMapSaveObjects(dynamic mapDef)
        {
            var map = new TiledMapSave
            {
                Height = mapDef.Height,
                Width = mapDef.Width,
                tileheight = mapDef.tileheight,
                tilewidth = mapDef.tilewidth,
                orientation = "orthogonal",
                Tilesets = new List<Tileset>
                {
                    new Tileset
                    {
                        Firstgid = 1u,
                        Name = "test",
                        Tilewidth = mapDef.tilewidth,
                        Tileheight = mapDef.tileheight,
                        Spacing = 1,
                        Margin = 1,
                        Images = new[]
                        {
                            new TilesetImage
                            {
                                height = 199,
                                width = 199,
                                Source = "nothing"
                            }
                        },
                    }
                },
                Layers = new List<MapLayer>
                {
                    new MapLayer
                    {
                        Name = "Layer1",
                        width = mapDef.Width,
                        height = mapDef.Height,
                        data = new[]
                        {
                            new mapLayerData
                            {
                                dataTiles = new[]
                                {
                                    new mapLayerDataTile
                                    {
                                        gid = "1"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "3"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "9"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "11"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "17"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "19"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            map.objectgroup = new List<mapObjectgroup>();
            map.objectgroup.Add(
                                    new mapObjectgroup
                    {
                        name = "Objects",
                        @object = CreateTileMapSaveObjectGroups(mapDef.objects)
                    });

            return map;
        }
Esempio n. 11
0
 public void ForceRebuildPropertyDictionary()
 {
     propertyDictionaryField = TiledMapSave.BuildPropertyDictionaryConcurrently(properties);
 }
        private static void SaveTilb(TmxToScnxCommandLineArgs parsedArgs, TiledMapSave tms)
        {
            if (parsedArgs.IsVerbose)
            {
                Console.WriteLine("Saving \"{0}\".", parsedArgs.DestinationFile);
            }
            // all files should have been copied over, and since we're using the .scnx files,
            // we are going to use the destination instead of the source.

            FileReferenceType referenceType = FileReferenceType.NoDirectory;

            if (parsedArgs.CopyImages == false)
            {
                referenceType = FileReferenceType.Absolute;
            }

            ReducedTileMapInfo rtmi =
                ReducedTileMapInfo.FromTiledMapSave(tms, parsedArgs.Scale, parsedArgs.Offset.Item3, FileManager.GetDirectory(parsedArgs.DestinationFile), referenceType);

            var directoryToMakeRelativeTo = FileManager.GetDirectory(parsedArgs.SourceFile);

            // If CopyImages is false, then we're going to keep the images where they are and reference them from there.
            if(parsedArgs.CopyImages == false)
            {
                directoryToMakeRelativeTo = FileManager.GetDirectory(parsedArgs.DestinationFile);
            }

            if (parsedArgs.CopyImages == false)
            {
                foreach (var item in rtmi.Layers)
                {
                    item.Texture = FileManager.MakeRelative(item.Texture, directoryToMakeRelativeTo);
                }
            }

            if (File.Exists(parsedArgs.DestinationFile))
            {
                File.Delete(parsedArgs.DestinationFile);
            }

            using (Stream outputStream = File.OpenWrite(parsedArgs.DestinationFile))
            using (BinaryWriter binaryWriter = new BinaryWriter(outputStream))
            {
                rtmi.WriteTo(binaryWriter);
                if (parsedArgs.IsVerbose)
                {
                    Console.WriteLine("Saved \"{0}\".", parsedArgs.DestinationFile);
                }
            }
        }
        public void ToSceneSaveOrthogonalTest()
        {
            TiledMapSave.Offset = new Tuple<float, float, float>(0f, 0f, 0f);
            var target = new TiledMapSave()
                {
                    height = 64,
                    width = 64,
                    tileheight = 32,
                    tilewidth = 32,
                    tileset = new List<mapTileset>()
                        {
                            new mapTileset
                                {
                                    Firstgid = 1,
                                    Image = new mapTilesetImage[]
                                        {
                                            new mapTilesetImage
                                                {
                                                    height = 64,
                                                    width = 64,
                                                    source =
                                                        "../../../../../Program Files (x86)/Tiled/examples/tmw_desert_spacing.png"
                                                }
                                        },
                                    Name = "tileset1",
                                    Tiles = new List<mapTilesetTile>
                                        {
                                            new mapTilesetTile
                                                {
                                                    id = 0,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tile1"
                                                                }
                                                        }
                                                },
                                            new mapTilesetTile
                                                {
                                                    id = 1,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tile2"
                                                                }
                                                        }
                                                },
                                            new mapTilesetTile
                                                {
                                                    id = 2,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tile3"
                                                                }
                                                        }
                                                },
                                                new mapTilesetTile
                                                {
                                                    id = 3,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tile4"
                                                                }
                                                        }
                                                }

                                        },
                                    Tileheight = 32,
                                    Tilewidth = 32
                                }
                        },
                    Layers = new List<MapLayer>()
                        {
                            new MapLayer
                                {
                                    Name = "layer1",
                                    data = new mapLayerData[]
                                        {
                                            new mapLayerData()
                                                {
                                                    dataTiles = new mapLayerDataTile[]
                                                        {
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "1"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "3"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "4"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "2"
                                                                },
                                                        }
                                                }
                                        }
                                }
                        },
                    orientation = "orthogonal"
                };
            const float scale = 1F;
            SceneSave actual = target.ToSceneSave(scale);

            Assert.AreEqual(4, actual.SpriteList.Count);

            var tile = actual.SpriteList.SingleOrDefault(s => s.Name == "tile1");
            Assert.IsNotNull(tile);
            Assert.AreEqual(16f, tile.X);
            Assert.AreEqual(-16, tile.Y);

            tile = actual.SpriteList.SingleOrDefault(s => s.Name == "tile2");
            Assert.IsNotNull(tile);
            Assert.AreEqual(112f, tile.X);
            Assert.AreEqual(-16f, tile.Y);

            tile = actual.SpriteList.SingleOrDefault(s => s.Name == "tile3");
            Assert.IsNotNull(tile);
            Assert.AreEqual(48f, tile.X);
            Assert.AreEqual(-16f, tile.Y);

            tile = actual.SpriteList.SingleOrDefault(s => s.Name == "tile4");
            Assert.IsNotNull(tile);
            Assert.AreEqual(80f, tile.X);
            Assert.AreEqual(-16f, tile.Y);
        }
Esempio n. 14
0
        public static ShapeCollectionSave ToShapeCollectionSave(this TiledMapSave tiledMapSave, string layerName)
        {
            MapLayer mapLayer = null;

            if (!string.IsNullOrEmpty(layerName))
            {
                mapLayer = tiledMapSave.Layers.FirstOrDefault(l => l.Name.Equals(layerName));
            }
            var shapes = new ShapeCollectionSave();

            if ((mapLayer != null && !mapLayer.IsVisible && mapLayer.VisibleBehavior == TMXGlueLib.TiledMapSave.LayerVisibleBehavior.Skip) ||
                tiledMapSave.objectgroup == null || tiledMapSave.objectgroup.Count == 0)
            {
                return(shapes);
            }

            foreach (mapObjectgroup group in tiledMapSave.objectgroup)
            {
                if (group.@object != null && !string.IsNullOrEmpty(group.Name) && (string.IsNullOrEmpty(layerName) || group.Name.Equals(layerName)))
                {
                    foreach (mapObjectgroupObject @object in group.@object)
                    {
                        ///November 8th, 2015
                        ///Jesse Crafts-Finch
                        ///If a polygon has a gid, and therefore an image associate with it, it will be turned into a spritesave, not a polygon.
                        if (@object.gid != null)
                        {
                            continue;
                        }

                        if (@object.polygon != null)
                        {
                            foreach (mapObjectgroupObjectPolygon polygon in @object.polygon)
                            {
                                // TODO: Make this a rectangle object
                                PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name,
                                                                                              @object.x, @object.y, @object.Rotation, polygon.points, true);
                                if (p != null)
                                {
                                    shapes.PolygonSaves.Add(p);
                                }
                            }
                        }

                        if (@object.polyline != null)
                        {
                            foreach (mapObjectgroupObjectPolyline polyline in @object.polyline)
                            {
                                PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name,
                                                                                              @object.x, @object.y, @object.Rotation, polyline.points, false);
                                if (p != null)
                                {
                                    shapes.PolygonSaves.Add(p);
                                }
                            }
                        }



                        if (@object.polygon == null && @object.polyline == null)
                        {
                            PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.width, @object.height, @object.Rotation, @object.ellipse);
                            if (p != null)
                            {
                                shapes.PolygonSaves.Add(p);
                            }
                        }
                    }
                }
            }
            return(shapes);
        }
        public void ToCSVStringTilesetPropertyTest()
        {
            var target = new TiledMapSave
            {
                Tilesets = new List<Tileset>
                {
                    new Tileset
                    {
                        Firstgid = 1,
                        Name = "tileset1",
                        Tiles = new List<mapTilesetTile>
                        {
                            new mapTilesetTile
                            {
                                id = 1,
                                properties = new List<property>
                                {
                                    new property
                                    {
                                        name = "commonPropertyName",
                                        value = "commonPropertyValueTileset1Tile1"
                                    },
                                    new property
                                    {
                                        name = "tileset1Tile1PropertyName",
                                        value = "tileset1Tile1PropertyValue"
                                    },
                                    new property
                                    {
                                        name = "name",
                                        value = "tileset1Tile1"
                                    }
                                }
                            },
                            new mapTilesetTile
                            {
                                id = 2,
                                properties = new List<property>
                                {
                                    new property
                                    {
                                        name = "name",
                                        value = "tileset1Tile2"
                                    },
                                    new property
                                    {
                                        name = "commonPropertyName",
                                        value = "commonPropertyValueTileset1Tile2"
                                    },
                                    new property
                                    {
                                        name = "tileset1Tile2PropertyName",
                                        value = "tileset1Tile2PropertyValue"
                                    }
                                }
                            },
                        },
                    },
                    new Tileset
                    {
                        Firstgid = 3,
                        Name = "tileset2",
                        Tiles = new List<mapTilesetTile>
                        {
                            new mapTilesetTile
                            {
                                id = 1,
                                properties = new List<property>
                                {
                                    new property
                                    {
                                        name = "name",
                                        value = "tileset2Tile1"
                                    },
                                    new property
                                    {
                                        name = "commonPropertyName",
                                        value = "commonPropertyValueTileset2Tile1"
                                    },
                                    new property
                                    {
                                        name = "tileset2Tile1PropertyName",
                                        value = "tileset2Tile1PropertyValue"
                                    }
                                }
                            }
                        }
                    }
                },
                Layers = new List<MapLayer>
                {
                    new MapLayer
                    {
                        height = 32,
                        width = 96,
                        Name = "layer1",
                        visible = 1,
                        data = new[]
                        {
                            new mapLayerData
                            {
                                dataTiles = new[]
                                {
                                    new mapLayerDataTile
                                    {
                                        gid = "1"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "2"
                                    },
                                    new mapLayerDataTile
                                    {
                                        gid = "3"
                                    }
                                }
                            }
                        },
                    }
                },
                orientation = "Orthogonal",
                tileheight = 32,
                tilewidth = 32
            };

            string expected =
                "Name (required),EmbeddedAnimation (List<FlatRedBall.Content.AnimationChain.AnimationFrameSaveBase>),tileset1Tile1PropertyName,commonPropertyName,tileset1Tile2PropertyName,tileset2Tile1PropertyName\r\n\"tileset1Tile1\",\"\",\"tileset1Tile1PropertyValue\",\"commonPropertyValueTileset1Tile1\",\"\",\"\"\r\n\"tileset1Tile2\",\"\",\"\",\"commonPropertyValueTileset1Tile2\",\"tileset1Tile2PropertyValue\",\"\"\r\n\"tileset2Tile1\",\"\",\"\",\"commonPropertyValueTileset2Tile1\",\"\",\"tileset2Tile1PropertyValue\"\r\n";
            string actual = target.ToCSVString();
            Assert.AreEqual(expected, actual);
        }
        public void ToCSVStringObjectPropertyTest()
        {
            var target = new TiledMapSave
            {
                objectgroup = new mapObjectgroup[2]
                    {
                        new mapObjectgroup
                            {
                                name = "objectGroup1",
                                @object= new mapObjectgroupObject[1]
                                    {
                                        new mapObjectgroupObject
                                            {
                                                Name="object1",
                                                properties = new List<property>
                                                    {
                                                        new property
                                                            {
                                                                name="prop1",
                                                                value = "val1"
                                                            },
                                                        new property
                                                            {
                                                                name="prop2",
                                                                value = "val2"
                                                            }
                                                    }
                                            }
                                    }
                            },
                        new mapObjectgroup
                            {
                                    @object=new mapObjectgroupObject[1]
                                        {
                                            new mapObjectgroupObject
                                                {
                                                    Name = "object2",
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "prop3",
                                                                    value="val3"
                                                                },
                                                            new property
                                                                {
                                                                    name="prop2",
                                                                    value = "val4"
                                                                }
                                                        }
                                                }
                                        }
                            }
                    }
            };
            string actual = target.ToCSVString(TiledMapSave.CSVPropertyType.Object);
            const string expected =
                "Name (required),X (int),Y (int),prop1,prop2,prop3\r\nobject1,0,0,\"val1\",\"val2\",\r\nobject2,0,0,,\"val4\",\"val3\"\r\n";

            Assert.AreEqual(expected, actual);
        }
 internal mapTileset GetTilesetByName(TiledMapSave tms, string name)
 {
     return tms.tileset.FirstOrDefault(tileset => tileset != null && tileset.Name == name);
 }
 public void LoadTiledMapSave(string fileName)
 {
     LastLoadedFile = fileName;
     TiledMapSave = TiledMapSave.FromFile(fileName);
 }
        public void ToCSVStringTilesetPropertyTest()
        {
            var target = new TiledMapSave
                {
                    tileset = new List<mapTileset>()
                        {
                            new mapTileset
                                {
                                    Firstgid = 1,
                                    Name = "tileset1",
                                    Tiles = new List<mapTilesetTile>
                                        {
                                            new mapTilesetTile
                                                {
                                                    id = 1,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "commonPropertyName",
                                                                    value = "commonPropertyValueTileset1Tile1"
                                                                },
                                                            new property
                                                                {
                                                                    name = "tileset1Tile1PropertyName",
                                                                    value = "tileset1Tile1PropertyValue"
                                                                },
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tileset1Tile1"
                                                                }
                                                        }
                                                },
                                            new mapTilesetTile
                                                {
                                                    id = 2,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tileset1Tile2"
                                                                },
                                                            new property
                                                                {
                                                                    name = "commonPropertyName",
                                                                    value = "commonPropertyValueTileset1Tile2"
                                                                },
                                                            new property
                                                                {
                                                                    name = "tileset1Tile2PropertyName",
                                                                    value = "tileset1Tile2PropertyValue"
                                                                }
                                                        }
                                                },
                                        },
                                },
                            new mapTileset
                                {
                                    Firstgid = 3,
                                    Name = "tileset2",
                                    Tiles = new List<mapTilesetTile>
                                        {
                                            new mapTilesetTile
                                                {
                                                    id = 1,
                                                    properties = new List<property>
                                                        {
                                                            new property
                                                                {
                                                                    name = "name",
                                                                    value = "tileset2Tile1"
                                                                },
                                                            new property
                                                                {
                                                                    name = "commonPropertyName",
                                                                    value = "commonPropertyValueTileset2Tile1"
                                                                },
                                                            new property
                                                                {
                                                                    name = "tileset2Tile1PropertyName",
                                                                    value = "tileset2Tile1PropertyValue"
                                                                }
                                                        }
                                                }
                                        }
                                }
                        },
                    Layers = new List<MapLayer>()
                        {
                            new MapLayer
                                {
                                    height = 32,
                                    width = 96,
                                    Name = "layer1",
                                    visible = 1,
                                    data = new mapLayerData[1]
                                        {
                                            new mapLayerData
                                                {
                                                    dataTiles = new mapLayerDataTile[3]
                                                        {
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "1"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "2"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "3"
                                                                }
                                                        }
                                                }
                                        },
                                }
                        },
                    orientation = "Orthogonal",
                    tileheight = 32,
                    tilewidth = 32
                };

            string expected =
                "Name (required),tileset1Tile1PropertyName,commonPropertyName,tileset1Tile2PropertyName,tileset2Tile1PropertyName\r\ntileset1Tile1,\"tileset1Tile1PropertyValue\",\"commonPropertyValueTileset1Tile1\",,\r\ntileset1Tile2,,\"commonPropertyValueTileset1Tile2\",\"tileset1Tile2PropertyValue\",\r\ntileset2Tile1,,\"commonPropertyValueTileset2Tile1\",,\"tileset2Tile1PropertyValue\"\r\n";
            string actual = target.ToCSVString(type: TiledMapSave.CSVPropertyType.Tile, layerName: null);
            Assert.AreEqual(expected, actual);

            expected =
                "Name (required),tileset1Tile1PropertyName,commonPropertyName,tileset1Tile2PropertyName,tileset2Tile1PropertyName\r\ntileset1Tile1,\"tileset1Tile1PropertyValue\",\"commonPropertyValueTileset1Tile1\",,\r\ntileset1Tile2,,\"commonPropertyValueTileset1Tile2\",\"tileset1Tile2PropertyValue\",\r\ntileset2Tile1,,\"commonPropertyValueTileset2Tile1\",,\"tileset2Tile1PropertyValue\"\r\n";
        }
        public void ToShapeCollectionSaveOrthogonalTest()
        {
            var map = new TiledMapSave
                {
                    height = 3,
                    width = 2,
                    tileheight = 32,
                    tilewidth = 32,
                    orientation = "orthogonal",
                    tileset = new List<mapTileset>
                        {
                            new mapTileset
                                {
                                    Firstgid = 1u,
                                    Name = "test",
                                    Tilewidth = 32,
                                    Tileheight = 32,
                                    Spacing = 1,
                                    Margin = 1,
                                    Image = new mapTilesetImage[]
                                        {
                                            new mapTilesetImage
                                                {
                                                    height = 199,
                                                    width = 199,
                                                    source = "nothing"
                                                }
                                        },
                                }
                        },
                    Layers = new List<MapLayer>()
                        {
                            new MapLayer
                                {
                                    Name = "Layer1",
                                    width = 2,
                                    height = 3,
                                    data = new mapLayerData[]
                                        {
                                            new mapLayerData
                                                {
                                                    dataTiles = new mapLayerDataTile[]
                                                        {
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "1"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "3"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "9"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "11"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "17"
                                                                },
                                                            new mapLayerDataTile
                                                                {
                                                                    gid = "19"
                                                                }
                                                        }
                                                }
                                        }
                                }
                        },
                    objectgroup = new mapObjectgroup[]
                        {
                            new mapObjectgroup
                                {
                                    name = "Objects",
                                    @object = new mapObjectgroupObject[]
                                        {
                                            new mapObjectgroupObject
                                                {
                                                    x = 0,
                                                    y = 0,
                                                    width = 64,
                                                    height = 32
                                                },
                                            new mapObjectgroupObject
                                                {
                                                    x = 9,
                                                    y = 45,
                                                    polygon = new mapObjectgroupObjectPolygon[]
                                                        {
                                                            new mapObjectgroupObjectPolygon
                                                                {
                                                                    points = "0,0 42,0 23,23"
                                                                }
                                                        }
                                                },
                                            new mapObjectgroupObject
                                                {
                                                    x = 6,
                                                    y = 66,
                                                    polyline = new mapObjectgroupObjectPolyline[]
                                                        {
                                                            new mapObjectgroupObjectPolyline
                                                                {
                                                                    points = "0,0 7,19 42,19 52,-1"
                                                                }
                                                        }
                                                },
                                            new mapObjectgroupObject
                                                {
                                                    x = 8,
                                                    y = 13,
                                                    width = 14,
                                                    height = 12
                                                },
                                            new mapObjectgroupObject
                                                {
                                                    x = 38,
                                                    y = 14,
                                                    width = 17,
                                                    height = 12
                                                }
                                        }
                                }
                        }
                };
            var shapeCollectionSave = map.ToShapeCollectionSave(null);

            Assert.AreEqual(5, shapeCollectionSave.PolygonSaves.Count);

            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(0).X);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(0).Y);
            Assert.AreEqual(0, shapeCollectionSave.PolygonSaves.ElementAt(0).Z);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.First().X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.First().Y);
            Assert.AreEqual(80, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(1).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(1).Y);
            Assert.AreEqual(80, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(2).X);
            Assert.AreEqual(-48, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(2).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(3).X);
            Assert.AreEqual(-48, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(3).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(4).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(0).Points.ElementAt(4).Y);

            Assert.AreEqual(-7, shapeCollectionSave.PolygonSaves.ElementAt(1).X);
            Assert.AreEqual(-29, shapeCollectionSave.PolygonSaves.ElementAt(1).Y);
            Assert.AreEqual(0, shapeCollectionSave.PolygonSaves.ElementAt(1).Z);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.First().X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.First().Y);
            Assert.AreEqual(58, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.ElementAt(1).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.ElementAt(1).Y);
            Assert.AreEqual(39, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.ElementAt(2).X);
            Assert.AreEqual(-39, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.ElementAt(2).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.ElementAt(3).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(1).Points.ElementAt(3).Y);

            Assert.AreEqual(-10, shapeCollectionSave.PolygonSaves.ElementAt(2).X);
            Assert.AreEqual(-50, shapeCollectionSave.PolygonSaves.ElementAt(2).Y);
            Assert.AreEqual(0, shapeCollectionSave.PolygonSaves.ElementAt(2).Z);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.First().X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.First().Y);
            Assert.AreEqual(23, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.ElementAt(1).X);
            Assert.AreEqual(-35, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.ElementAt(1).Y);
            Assert.AreEqual(58, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.ElementAt(2).X);
            Assert.AreEqual(-35, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.ElementAt(2).Y);
            Assert.AreEqual(68, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.ElementAt(3).X);
            Assert.AreEqual(-15, shapeCollectionSave.PolygonSaves.ElementAt(2).Points.ElementAt(3).Y);

            Assert.AreEqual(-8, shapeCollectionSave.PolygonSaves.ElementAt(3).X);
            Assert.AreEqual(3, shapeCollectionSave.PolygonSaves.ElementAt(3).Y);
            Assert.AreEqual(0, shapeCollectionSave.PolygonSaves.ElementAt(3).Z);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(0).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(0).Y);
            Assert.AreEqual(30, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(1).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(1).Y);
            Assert.AreEqual(30, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(2).X);
            Assert.AreEqual(-28, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(2).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(3).X);
            Assert.AreEqual(-28, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(3).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(4).X);
            Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(3).Points.ElementAt(4).Y);

            Assert.AreEqual(22, shapeCollectionSave.PolygonSaves.ElementAt(4).X);
               Assert.AreEqual(2, shapeCollectionSave.PolygonSaves.ElementAt(4).Y);
             Assert.AreEqual(0, shapeCollectionSave.PolygonSaves.ElementAt(4).Z);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(0).X);
               Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(0).Y);
            Assert.AreEqual(33, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(1).X);
               Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(1).Y);
            Assert.AreEqual(33, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(2).X);
               Assert.AreEqual(-28, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(2).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(3).X);
               Assert.AreEqual(-28, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(3).Y);
            Assert.AreEqual(16, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(4).X);
               Assert.AreEqual(-16, shapeCollectionSave.PolygonSaves.ElementAt(4).Points.ElementAt(4).Y);
        }
Esempio n. 21
0
        public static ShapeCollection ToShapeCollection(this TiledMapSave tiledMapSave, string layerName = null)
        {
            var scs = tiledMapSave.ToShapeCollectionSave(layerName);

            return(scs.ToShapeCollection());
        }
Esempio n. 22
0
        private static PolygonSave ConvertTmxObjectToFrbPolygonSave(this TiledMapSave tiledMapSave, string name, double x, double y, double rotation, string points, bool connectBackToStart)
        {
            if (string.IsNullOrEmpty(points))
            {
                return(null);
            }

            var polygon = new PolygonSave();

            string[] pointString = points.Split(" ".ToCharArray());

            polygon.Name = name;

            // Nov. 19th, 2014 - Domenic:
            // I am ripping this code apart a little, because shapes really should not involve tile sizes in their x/y calculations.
            // I'm not sure why this was ever done this way, as TMX gives the X/Y and width/height already. The old way was basically to convert
            // the x/y coordinates into tile based coordinates and then re-convert back to full x/y coordinates. This makes no sense any more to me.
            //
            // Having examined TMX format a little more, it seems that the x/y position is always specified
            //
            //float fx = x;
            //float fy = y;

            //if ("orthogonal".Equals(tiledMapSave.orientation))
            //{
            //    fx -= tiledMapSave.tilewidth / 2.0f;
            //    fy -= tiledMapSave.tileheight + (tiledMapSave.tileheight / 2.0f);
            //}
            //else if ("isometric".Equals(tiledMapSave.orientation))
            //{
            //    fx -= tiledMapSave.tilewidth / 4.0f;
            //    fy -= tiledMapSave.tileheight / 2.0f;
            //}

            //tiledMapSave.CalculateWorldCoordinates(
            //    0, fx / tiledMapSave.tileheight, fy / tiledMapSave.tileheight,
            //    tiledMapSave.tilewidth, tiledMapSave.tileheight,
            //    w * tiledMapSave.tilewidth, out newx, out newy, out z);

            //polygon.X = newx - tiledMapSave.tilewidth / 2.0f;
            //polygon.Y = newy - tiledMapSave.tileheight / 2.0f;
            //var pointsArr = new Point[pointString.Length + (connectBackToStart ? 1 : 0)];

            var pointsList =
                pointString.Select(p =>
            {
                var xy = p.Split(",".ToCharArray());
                return(new Point
                {
                    X = Convert.ToDouble(xy[0]),
                    Y = -Convert.ToDouble(xy[1])
                });
            }).ToList();

            if (connectBackToStart)
            {
                pointsList.Add(new Point(pointsList[0].X, pointsList[0].Y));
            }

            polygon.Points    = pointsList.ToArray();
            polygon.X         = (float)x;
            polygon.Y         = (float)-y;
            polygon.RotationZ = -MathHelper.ToRadians((float)rotation);

            return(polygon);
        }
        private static void PerformSave(TmxToScnxCommandLineArgs parsedArgs, TiledMapSave tms)
        {
            string extension = FileManager.GetExtension(parsedArgs.DestinationFile);

            if (extension == "scnx")
            {

                SaveScnx(parsedArgs, tms);
            }
            else if (extension == "tilb")
            {
                SaveTilb(parsedArgs, tms);

            }
            else
            {
                Console.WriteLine("The following extension is not understood: " + extension);
            }
        }
        private static void SaveScnx(TmxToScnxCommandLineArgs parsedArgs, TiledMapSave tms)
        {
            Console.WriteLine("Saving \"{0}\".", parsedArgs.DestinationFile);

            SceneSave spriteEditorScene = tms.ToSceneSave(parsedArgs.Scale);

            spriteEditorScene.FileName = parsedArgs.DestinationFile;
            spriteEditorScene.ScenePath = FileManager.GetDirectory(parsedArgs.DestinationFile);
            spriteEditorScene.AssetsRelativeToSceneFile = true;
            var result = spriteEditorScene.GetMissingFiles();
            foreach (var missing in result)
            {
                Console.Error.WriteLine("Missing file: " + spriteEditorScene.ScenePath + missing);
            }
            spriteEditorScene.Save(parsedArgs.DestinationFile.Trim());
        }
        public void ToCSVStringMapPropertyTest()
        {
            var target = new TiledMapSave
                {
                    properties = new List<property>
                        {
                            new property
                                {
                                    name = "mapProperty1",
                                    value = "mapValue1"
                                },
                            new property
                                {
                                    name = "mapProperty2",
                                    value = "mapValue2"
                                }
                        }
                };
            string actual = target.ToCSVString(TiledMapSave.CSVPropertyType.Map);
            const string expected = "Name (required),mapProperty1,mapProperty2\r\nmap,\"mapValue1\",\"mapValue2\"\r\n";

            Assert.AreEqual(expected, actual);
        }
        public void ToCSVStringLayerPropertyTest()
        {
            const TiledMapSave.CSVPropertyType type = TiledMapSave.CSVPropertyType.Layer;
            var target = new TiledMapSave
            {
                Layers = new List<MapLayer>
                {
                    new MapLayer
                    {
                        properties = new List<property>
                        {
                            new property {name = "name1", value = "val"},
                            new property {name = "name2", value = "val2"}
                        },
                        Name = "layer1"
                    },
                    new MapLayer
                    {
                        properties = new List<property>
                        {
                            new property {name = "name1", value = "val"},
                            new property {name = "name3", value = "val3"}
                        },
                        Name = "layer2"
                    }
                }
            };

            var expected =
                "Name (required),name1,name2,name3\r\n\"layer1\",\"val\",\"val2\",\"\"\r\n\"layer2\",\"val\",\"\",\"val3\"\r\n";
            var actual = target.ToCSVString(type);
            Assert.AreEqual(expected, actual);

            string layerName = "layer1";
            expected = "Name (required),name1,name2\r\n\"layer1\",\"val\",\"val2\"\r\n";
            actual = target.ToCSVString(type, layerName);
            Assert.AreEqual(expected, actual);
        }
 public void ToNodeNetworkSaveTest()
 {
     TiledMapSave target = new TiledMapSave(); // TODO: Initialize to an appropriate value
     bool linkHorizontally = false; // TODO: Initialize to an appropriate value
     bool linkVertically = false; // TODO: Initialize to an appropriate value
     bool linkDiagonally = false; // TODO: Initialize to an appropriate value
     bool requireTile = false; // TODO: Initialize to an appropriate value
     NodeNetworkSave expected = null; // TODO: Initialize to an appropriate value
     NodeNetworkSave actual;
     actual = target.ToNodeNetworkSave(linkHorizontally, linkVertically, linkDiagonally, requireTile);
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }