Пример #1
0
        private void ParseTilesetFromImageLayer(XElement elemImageLayer)
        {
            string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name");

            XElement xmlImage = elemImageLayer.Element("image");

            if (xmlImage == null)
            {
                Program.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName);
                return;
            }

            TmxImage tmxImage = TmxImage.FromXml(xmlImage);

            RegisterImagePath(tmxImage.Path);

            uint firstId  = this.Tiles.Max(t => t.Key) + 1;
            uint localId  = 1;
            uint globalId = firstId + localId;

            TmxTile tile = new TmxTile(globalId, localId, tilesetName, tmxImage);

            tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
            tile.SetLocationOnSource(0, 0);
            this.Tiles[tile.GlobalId] = tile;
        }
Пример #2
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();
            tmxImage.Path = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            // Width and height are optional.
            int width = TmxHelper.GetAttributeAsInt(elemImage, "width", 0);
            int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0);

            // Prefer to use the actual width and height anyway so that UVs do not get jacked
            using (Image bitmap = Bitmap.FromFile(tmxImage.Path))
            {
                width = bitmap.Width;
                height = bitmap.Height;
            }

            tmxImage.Size = new System.Drawing.Size(width, height);

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && !tmxImage.TransparentColor.StartsWith("#"))
            {
                // The hash makes it an HTML color
                tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
            }

            return tmxImage;
        }
Пример #3
0
        public static TsxTileset FromImageLayerXml(XElement xml, TmxMap tmxMap)
        {
            TsxTileset tsxTileset = new TsxTileset(tmxMap);

            tsxTileset.Name = TmxHelper.GetAttributeAsString(xml, "name");
            XElement xElement = xml.Element("image");

            if (xElement == null)
            {
                Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tsxTileset.Name);
                return(null);
            }
            TmxProperties tmxProperties          = TmxProperties.FromXml(xml);
            string        propertyValueAsString  = tmxProperties.GetPropertyValueAsString("unity:namePrefix", "");
            string        propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", "");
            TmxImage      tmxImage = TmxImage.FromXml(xElement, propertyValueAsString, propertyValueAsString2);
            uint          num      = 1u;

            if (tmxMap.Tiles.Count > 0)
            {
                num = tmxMap.Tiles.Max((KeyValuePair <uint, TmxTile> t) => t.Key) + 1;
            }
            uint    num2     = 1u;
            uint    globalId = num + num2;
            TmxTile tmxTile  = new TmxTile(tmxMap, globalId, num2, tsxTileset.Name, tmxImage);

            tmxTile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
            tmxTile.SetLocationOnSource(0, 0);
            tsxTileset.Tiles.Add(tmxTile);
            tsxTileset.Tiles.ForEach(delegate(TmxTile t)
            {
                tmxMap.Tiles.Add(t.GlobalId, t);
            });
            return(tsxTileset);
        }
Пример #4
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();
            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            try
            {
                tmxImage.ImageBitmap = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath);
            }
            catch (FileNotFoundException fnf)
            {
                string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                throw new TmxException(msg, fnf);
            }

            tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor))
            {
                if (!tmxImage.TransparentColor.StartsWith("#"))
                {
                    // The hash makes it an HTML color
                    tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
                }

                System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
            }

            return tmxImage;
        }
Пример #5
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.Path = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            // Width and height are optional.
            int width  = TmxHelper.GetAttributeAsInt(elemImage, "width", 0);
            int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0);

            // Prefer to use the actual width and height anyway so that UVs do not get jacked
            using (Image bitmap = Bitmap.FromFile(tmxImage.Path))
            {
                width  = bitmap.Width;
                height = bitmap.Height;
            }

            tmxImage.Size = new System.Drawing.Size(width, height);

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && !tmxImage.TransparentColor.StartsWith("#"))
            {
                // The hash makes it an HTML color
                tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
            }

            return(tmxImage);
        }
Пример #6
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            tmxImage.ImageBitmap = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath);
            tmxImage.Size        = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor))
            {
                if (!tmxImage.TransparentColor.StartsWith("#"))
                {
                    // The hash makes it an HTML color
                    tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
                }

                System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
            }

            return(tmxImage);
        }
Пример #7
0
        private void ParseTilesetFromImageLayer(XElement elemImageLayer)
        {
            string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name");

            XElement xmlImage = elemImageLayer.Element("image");

            if (xmlImage == null)
            {
                Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName);
                return;
            }

            TmxProperties properties = TmxProperties.FromXml(elemImageLayer);
            string        prefix     = properties.GetPropertyValueAsString("unity:namePrefix", "");
            string        postfix    = properties.GetPropertyValueAsString("unity:namePostfix", "");

            TmxImage tmxImage = TmxImage.FromXml(xmlImage, prefix, postfix);

            // The "firstId" is is always one more than all the tiles that we've already parsed (which may be zero)
            uint firstId = 1;

            if (this.Tiles.Count > 0)
            {
                firstId = this.Tiles.Max(t => t.Key) + 1;
            }

            uint localId  = 1;
            uint globalId = firstId + localId;

            TmxTile tile = new TmxTile(this, globalId, localId, tilesetName, tmxImage);

            tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
            tile.SetLocationOnSource(0, 0);
            this.Tiles[tile.GlobalId] = tile;
        }
Пример #8
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            // Get default image size in case we are not opening the file
            {
                int width  = TmxHelper.GetAttributeAsInt(elemImage, "width", 0);
                int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0);
                tmxImage.Size = new System.Drawing.Size(width, height);
            }

            // Do not open the image in Tiled2UnityLite (due to difficulty with GDI+ in some mono installs)
#if !TILED_2_UNITY_LITE
            if (!Tiled2Unity.Settings.IsAutoExporting)
            {
                try
                {
                    tmxImage.ImageBitmap = TmxHelper.FromFileBitmap32bpp(tmxImage.AbsolutePath);
                }
                catch (FileNotFoundException fnf)
                {
                    string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                    throw new TmxException(msg, fnf);

                    // Testing for when image files are missing. Just make up an image.
                    //int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
                    //int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
                    //tmxImage.ImageBitmap = new TmxHelper.CreateBitmap32bpp(width, height);
                    //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap))
                    //{
                    //    int color32 = tmxImage.AbsolutePath.GetHashCode();
                    //    Color color = Color.FromArgb(color32);
                    //    color = Color.FromArgb(255, color);
                    //    using (Brush brush = new SolidBrush(color))
                    //    {
                    //        g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size));
                    //    }
                    //}
                }

                tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);
            }
#endif

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");

#if !TILED_2_UNITY_LITE
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null)
            {
                System.Drawing.Color transColor = TmxHelper.ColorFromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
            }
#endif

            return(tmxImage);
        }
Пример #9
0
 public TmxTile(uint globalId, uint localId, string tilesetName, TmxImage tmxImage)
 {
     this.GlobalId    = globalId;
     this.LocalId     = localId;
     this.TmxImage    = tmxImage;
     this.Properties  = new TmxProperties();
     this.ObjectGroup = new TmxObjectGroup();
 }
Пример #10
0
 public TmxTile(uint globalId, uint localId, string tilesetName, TmxImage tmxImage)
 {
     this.GlobalId = globalId;
     this.LocalId = localId;
     this.TmxImage = tmxImage;
     this.Properties = new TmxProperties();
     this.ObjectGroup = new TmxObjectGroup();
     this.Animation = TmxAnimation.FromTileId(globalId);
     this.Meshes = new List<TmxMesh>();
 }
Пример #11
0
 public TmxTile(uint globalId, uint localId, string tilesetName, TmxImage tmxImage)
 {
     this.GlobalId    = globalId;
     this.LocalId     = localId;
     this.TmxImage    = tmxImage;
     this.Properties  = new TmxProperties();
     this.ObjectGroup = new TmxObjectGroup();
     this.Animation   = TmxAnimation.FromTileId(globalId);
     this.Meshes      = new List <TmxMesh>();
 }
Пример #12
0
 public TmxTile(TmxMap tmxMap, uint globalId, uint localId, string tilesetName, TmxImage tmxImage)
 {
     TmxMap      = TmxMap;
     GlobalId    = globalId;
     LocalId     = localId;
     TmxImage    = tmxImage;
     Properties  = new TmxProperties();
     ObjectGroup = new TmxObjectGroup(null, TmxMap);
     Animation   = TmxAnimation.FromTileId(globalId);
     Meshes      = new List <TmxMesh>();
 }
Пример #13
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();
            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            #if TILED_2_UNITY_LITE
            // Do not open the image in Tiled2UnityLite (due to difficulty with GDI+ in some mono installs)
            int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
            int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
            tmxImage.Size = new System.Drawing.Size(width, height);
            #else
            try
            {
                tmxImage.ImageBitmap = TmxHelper.FromFileBitmap32bpp(tmxImage.AbsolutePath);
            }
            catch (FileNotFoundException fnf)
            {
                string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                throw new TmxException(msg, fnf);

                // Testing for when image files are missing. Just make up an image.
                //int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
                //int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
                //tmxImage.ImageBitmap = new TmxHelper.CreateBitmap32bpp(width, height);
                //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap))
                //{
                //    int color32 = tmxImage.AbsolutePath.GetHashCode();
                //    Color color = Color.FromArgb(color32);
                //    color = Color.FromArgb(255, color);
                //    using (Brush brush = new SolidBrush(color))
                //    {
                //        g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size));
                //    }
                //}
            }

            tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);
            #endif

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor))
            {
            #if !TILED_2_UNITY_LITE
                System.Drawing.Color transColor = TmxHelper.ColorFromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
            #endif
            }

            return tmxImage;
        }
Пример #14
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            try
            {
                tmxImage.ImageBitmap = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath);
            }
            catch (FileNotFoundException fnf)
            {
                string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                throw new TmxException(msg, fnf);

                // Testing for when image files are missing. Just make up an image.
                //int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
                //int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
                //tmxImage.ImageBitmap = new Bitmap(width, height);
                //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap))
                //{
                //    int color32 = tmxImage.AbsolutePath.GetHashCode();
                //    Color color = Color.FromArgb(color32);
                //    color = Color.FromArgb(255, color);
                //    using (Brush brush = new SolidBrush(color))
                //    {
                //        g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size));
                //    }
                //}
            }

            tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor))
            {
                if (!tmxImage.TransparentColor.StartsWith("#"))
                {
                    // The hash makes it an HTML color
                    tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
                }

                System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
            }

            return(tmxImage);
        }
Пример #15
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.Path = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

            // Width and height are optional.
            int width  = TmxHelper.GetAttributeAsInt(elemImage, "width", 0);
            int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0);

            // Prefer to use the actual width and height anyway so that UVs do not get jacked
            try
            {
                using (Image bitmap = Bitmap.FromFile(tmxImage.Path))
                {
                    width  = bitmap.Width;
                    height = bitmap.Height;
                }
            }

            // Problem with the image file (too large, or unsupported format).
            // Warn that there may be issues with the resulting map file.
            catch
            {
                Program.WriteWarning("Image file " + tmxImage.Path + " too large or unsupported format. There may be issues in resulting prefab.");
            }



            tmxImage.Size = new System.Drawing.Size(width, height);

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && !tmxImage.TransparentColor.StartsWith("#"))
            {
                // The hash makes it an HTML color
                tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
            }

            return(tmxImage);
        }
Пример #16
0
        // This method is called eventually for external tilesets too
        // Only the gid attribute has been consumed at this point for the tileset
        private void ParseInternalTileset(XElement elemTileset, uint firstId)
        {
            string tilesetName = TmxHelper.GetAttributeAsString(elemTileset, "name");

            Logger.WriteVerbose("Parse internal tileset '{0}' (gid = {1}) ...", tilesetName, firstId);

            int tileWidth  = TmxHelper.GetAttributeAsInt(elemTileset, "tilewidth");
            int tileHeight = TmxHelper.GetAttributeAsInt(elemTileset, "tileheight");
            int spacing    = TmxHelper.GetAttributeAsInt(elemTileset, "spacing", 0);
            int margin     = TmxHelper.GetAttributeAsInt(elemTileset, "margin", 0);

            PointF   tileOffset    = PointF.Empty;
            XElement xmlTileOffset = elemTileset.Element("tileoffset");

            if (xmlTileOffset != null)
            {
                tileOffset.X = TmxHelper.GetAttributeAsInt(xmlTileOffset, "x");
                tileOffset.Y = TmxHelper.GetAttributeAsInt(xmlTileOffset, "y");
            }

            IList <TmxTile> tilesToAdd = new List <TmxTile>();

            // Get proerties on tileset
            TmxProperties properties = TmxProperties.FromXml(elemTileset);
            string        prefix     = properties.GetPropertyValueAsString("unity:namePrefix", "");
            string        postfix    = properties.GetPropertyValueAsString("unity:namePostfix", "");

            // Tilesets may have an image for all tiles within it, or it may have an image per tile
            if (elemTileset.Element("image") != null)
            {
                TmxImage tmxImage = TmxImage.FromXml(elemTileset.Element("image"), prefix, postfix);

                // Create all the tiles
                // This is a bit complicated because of spacing and margin
                // (Margin is ignored from Width and Height)
                for (int end_y = margin + tileHeight; end_y <= tmxImage.Size.Height; end_y += spacing + tileHeight)
                {
                    for (int end_x = margin + tileWidth; end_x <= tmxImage.Size.Width; end_x += spacing + tileWidth)
                    {
                        uint    localId  = (uint)tilesToAdd.Count();
                        uint    globalId = firstId + localId;
                        TmxTile tile     = new TmxTile(this, globalId, localId, tilesetName, tmxImage);
                        tile.Offset = tileOffset;
                        tile.SetTileSize(tileWidth, tileHeight);
                        tile.SetLocationOnSource(end_x - tileWidth, end_y - tileHeight);
                        tilesToAdd.Add(tile);
                    }
                }
            }
            else
            {
                // Each tile will have it's own image
                foreach (var t in elemTileset.Elements("tile"))
                {
                    TmxImage tmxImage = TmxImage.FromXml(t.Element("image"), prefix, postfix);

                    uint localId = (uint)tilesToAdd.Count();

                    // Local Id can be overridden by the tile element
                    // This is because tiles can be removed from the tileset, so we won'd always have a zero-based index
                    localId = TmxHelper.GetAttributeAsUInt(t, "id", localId);

                    uint    globalId = firstId + localId;
                    TmxTile tile     = new TmxTile(this, globalId, localId, tilesetName, tmxImage);
                    tile.Offset = tileOffset;
                    tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
                    tile.SetLocationOnSource(0, 0);
                    tilesToAdd.Add(tile);
                }
            }

            StringBuilder builder = new StringBuilder();

            foreach (TmxTile tile in tilesToAdd)
            {
                builder.AppendFormat("{0}", tile.ToString());
                if (tile != tilesToAdd.Last())
                {
                    builder.Append("\n");
                }
                this.Tiles[tile.GlobalId] = tile;
            }
            Logger.WriteVerbose("Added {0} tiles", tilesToAdd.Count);

            // Add any extra data to tiles
            foreach (var elemTile in elemTileset.Elements("tile"))
            {
                int localTileId = TmxHelper.GetAttributeAsInt(elemTile, "id");
                var tiles       = from t in this.Tiles
                                  where t.Value.GlobalId == localTileId + firstId
                                  select t.Value;

                // Note that some old tile data may be sticking around
                if (tiles.Count() == 0)
                {
                    Logger.WriteWarning("Tile '{0}' in tileset '{1}' does not exist but there is tile data for it.\n{2}", localTileId, tilesetName, elemTile.ToString());
                }
                else
                {
                    tiles.First().ParseTileXml(elemTile, this, firstId);
                }
            }
        }
Пример #17
0
        private static void ParseTilesetXml(XElement xml, TsxTileset tileset)
        {
            tileset.Name = TmxHelper.GetAttributeAsString(xml, "name");
            Logger.WriteVerbose("Parse internal tileset '{0}' (gid = {1}) ...", tileset.Name, tileset.FirstGlobalId);
            tileset.TileWidth  = TmxHelper.GetAttributeAsInt(xml, "tilewidth");
            tileset.TileHeight = TmxHelper.GetAttributeAsInt(xml, "tileheight");
            tileset.Spacing    = TmxHelper.GetAttributeAsInt(xml, "spacing", 0);
            tileset.Margin     = TmxHelper.GetAttributeAsInt(xml, "margin", 0);
            PointF   empty    = PointF.Empty;
            XElement xElement = xml.Element("tileoffset");

            if (xElement != null)
            {
                empty.X = (float)TmxHelper.GetAttributeAsInt(xElement, "x");
                empty.Y = (float)TmxHelper.GetAttributeAsInt(xElement, "y");
            }
            tileset.TileOffset = empty;
            List <TmxTile> list                   = new List <TmxTile>();
            TmxProperties  tmxProperties          = TmxProperties.FromXml(xml);
            string         propertyValueAsString  = tmxProperties.GetPropertyValueAsString("unity:namePrefix", "");
            string         propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", "");

            if (xml.Element("image") != null)
            {
                TmxImage tmxImage = TmxImage.FromXml(xml.Element("image"), propertyValueAsString, propertyValueAsString2);
                for (int i = tileset.Margin + tileset.TileHeight; i <= tmxImage.Size.Height; i += tileset.Spacing + tileset.TileHeight)
                {
                    for (int j = tileset.Margin + tileset.TileWidth; j <= tmxImage.Size.Width; j += tileset.Spacing + tileset.TileWidth)
                    {
                        uint    num      = (uint)list.Count();
                        uint    globalId = tileset.FirstGlobalId + num;
                        TmxTile tmxTile  = new TmxTile(tileset.TmxMap, globalId, num, tileset.Name, tmxImage);
                        tmxTile.Offset = empty;
                        tmxTile.SetTileSize(tileset.TileWidth, tileset.TileHeight);
                        tmxTile.SetLocationOnSource(j - tileset.TileWidth, i - tileset.TileHeight);
                        list.Add(tmxTile);
                    }
                }
            }
            else
            {
                foreach (XElement item in xml.Elements("tile"))
                {
                    TmxImage tmxImage2    = TmxImage.FromXml(item.Element("image"), propertyValueAsString, propertyValueAsString2);
                    uint     defaultValue = (uint)list.Count();
                    defaultValue = TmxHelper.GetAttributeAsUInt(item, "id", defaultValue);
                    uint    globalId2 = tileset.FirstGlobalId + defaultValue;
                    TmxTile tmxTile2  = new TmxTile(tileset.TmxMap, globalId2, defaultValue, tileset.Name, tmxImage2);
                    tmxTile2.Offset = empty;
                    tmxTile2.SetTileSize(tmxImage2.Size.Width, tmxImage2.Size.Height);
                    tmxTile2.SetLocationOnSource(0, 0);
                    list.Add(tmxTile2);
                }
            }
            tileset.Tiles.AddRange(list);
            Logger.WriteVerbose("Added {0} tiles", list.Count);
            foreach (XElement item2 in xml.Elements("tile"))
            {
                int localTileId = TmxHelper.GetAttributeAsInt(item2, "id");
                IEnumerable <TmxTile> source = from t in tileset.Tiles
                                               where t.GlobalId == localTileId + tileset.FirstGlobalId
                                               select t;
                if (source.Count() == 0)
                {
                    Logger.WriteWarning("Tile '{0}' in tileset '{1}' does not exist but there is tile data for it.\n{2}", localTileId, tileset.Name, item2.ToString());
                }
                else
                {
                    source.First().ParseTileXml(item2, tileset.TmxMap, tileset.FirstGlobalId);
                }
            }
        }
Пример #18
0
        public static TmxImage FromXml(XElement elemImage, string prefix, string postfix)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");
            tmxImage.ImageName    = String.Format("{0}{1}{2}", prefix, Path.GetFileNameWithoutExtension(tmxImage.AbsolutePath), postfix);

            // Get default image size in case we are not opening the file
            {
                int width  = TmxHelper.GetAttributeAsInt(elemImage, "width", 0);
                int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0);
                tmxImage.Size = new System.Drawing.Size(width, height);
            }

            bool canMakeTransparentPixels = true;

            // Do not open the image in Tiled2UnityLite (no SkiaSharp in Tiled2UnityLite)
#if !TILED_2_UNITY_LITE
            if (!Tiled2Unity.Settings.IsAutoExporting)
            {
                try
                {
                    if (!tmxImage.Size.IsEmpty)
                    {
                        // We know our size and can decode the image into our prefered format
                        var info = new SKImageInfo();
                        info.ColorType       = SKColorType.Rgba8888;
                        info.AlphaType       = SKAlphaType.Unpremul;
                        info.Width           = tmxImage.Size.Width;
                        info.Height          = tmxImage.Size.Height;
                        tmxImage.ImageBitmap = SKBitmap.Decode(tmxImage.AbsolutePath, info);
                    }
                    else
                    {
                        // Open the image without any helpful information
                        // This stops us from being able to make pixels transparent
                        tmxImage.ImageBitmap     = SKBitmap.Decode(tmxImage.AbsolutePath);
                        canMakeTransparentPixels = false;
                    }
                }
                catch (FileNotFoundException fnf)
                {
                    string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                    throw new TmxException(msg, fnf);
                }
                catch (Exception e)
                {
                    // Disable previewing. Some users are reporting problems. Perhaps due to older versions of windows.
                    Logger.WriteError("Error creating image with Skia Library. Exception: {0}", e.Message);
                    Tiled2Unity.Settings.DisablePreviewing();
                }

                tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);
            }
#endif

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");

#if !TILED_2_UNITY_LITE
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null)
            {
                if (canMakeTransparentPixels)
                {
                    Logger.WriteLine("Removing alpha from transparent pixels.");
                    System.Drawing.Color systemTransColor = TmxHelper.ColorFromHtml(tmxImage.TransparentColor);

                    // Set the transparent pixels if using color-keying
                    SKColor transColor = new SKColor((uint)systemTransColor.ToArgb()).WithAlpha(0);
                    for (int x = 0; x < tmxImage.ImageBitmap.Width; ++x)
                    {
                        for (int y = 0; y < tmxImage.ImageBitmap.Height; ++y)
                        {
                            SKColor pixel = tmxImage.ImageBitmap.GetPixel(x, y);
                            if (pixel.Red == transColor.Red && pixel.Green == transColor.Green && pixel.Blue == transColor.Blue)
                            {
                                tmxImage.ImageBitmap.SetPixel(x, y, transColor);
                            }
                        }
                    }
                }
                else
                {
                    Logger.WriteWarning("Cannot make transparent pixels for viewing purposes. Save tileset with newer verion of Tiled.");
                }
            }
#endif
            return(tmxImage);
        }
Пример #19
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();
            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

#if TILED_2_UNITY_LITE
            // Do not open the image in Tiled2UnityLite (due to difficulty with GDI+ in some mono installs)
            int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
            int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
            tmxImage.Size = new System.Drawing.Size(width, height);
#else
            try
            {
                // We use pre-muliplied alpha pixel format (it is 2x faster)
                Bitmap bitmapRaw = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath);
                Bitmap bitmapPArgb = new Bitmap(bitmapRaw.Width, bitmapRaw.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                using (Graphics g = Graphics.FromImage(bitmapPArgb))
                {
                    g.DrawImage(bitmapRaw, 0, 0, bitmapPArgb.Width, bitmapPArgb.Height);
                    tmxImage.ImageBitmap = bitmapPArgb;
                }

                tmxImage.ImageBitmap = bitmapPArgb;
            }
            catch (FileNotFoundException fnf)
            {
                string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                throw new TmxException(msg, fnf);

                // Testing for when image files are missing. Just make up an image.
                //int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
                //int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
                //tmxImage.ImageBitmap = new Bitmap(width, height);
                //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap))
                //{
                //    int color32 = tmxImage.AbsolutePath.GetHashCode();
                //    Color color = Color.FromArgb(color32);
                //    color = Color.FromArgb(255, color);
                //    using (Brush brush = new SolidBrush(color))
                //    {
                //        g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size));
                //    }
                //}
            }

            tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);
#endif

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor))
            {
                if (!tmxImage.TransparentColor.StartsWith("#"))
                {
                    // The hash makes it an HTML color
                    tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
                }

#if !TILED_2_UNITY_LITE
                System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
#endif
            }

            return tmxImage;
        }
Пример #20
0
        // This method is called eventually for external tilesets too
        // Only the gid attribute has been consumed at this point for the tileset
        private void ParseInternalTileset(XElement elemTileset, uint firstId)
        {
            string tilesetName = TmxHelper.GetAttributeAsString(elemTileset, "name");

            Program.WriteLine("Parse internal tileset '{0}' (gid = {1}) ...", tilesetName, firstId);
            Program.WriteVerbose(elemTileset.ToString());

            int tileWidth  = TmxHelper.GetAttributeAsInt(elemTileset, "tilewidth");
            int tileHeight = TmxHelper.GetAttributeAsInt(elemTileset, "tileheight");
            int spacing    = TmxHelper.GetAttributeAsInt(elemTileset, "spacing", 0);
            int margin     = TmxHelper.GetAttributeAsInt(elemTileset, "margin", 0);

            IList <TmxTile> tilesToAdd = new List <TmxTile>();

            // Tilesets may have an image for all tiles within it, or it may have an image per tile
            if (elemTileset.Element("image") != null)
            {
                TmxImage tmxImage = TmxImage.FromXml(elemTileset.Element("image"));
                RegisterImagePath(tmxImage.Path);

                // Create all the tiles
                // This is a bit complicated because of spacing and margin
                // (Margin is ignored from Width and Height)
                for (int end_y = margin + tileHeight; end_y <= tmxImage.Size.Height; end_y += spacing + tileHeight)
                {
                    for (int end_x = margin + tileWidth; end_x <= tmxImage.Size.Width; end_x += spacing + tileWidth)
                    {
                        uint    localId  = (uint)tilesToAdd.Count();
                        uint    globalId = firstId + localId;
                        TmxTile tile     = new TmxTile(globalId, localId, tilesetName, tmxImage);
                        tile.SetTileSize(tileWidth, tileHeight);
                        tile.SetLocationOnSource(end_x - tileWidth, end_y - tileHeight);
                        tilesToAdd.Add(tile);
                    }
                }
            }
            else
            {
                // Each tile will have it's own image
                foreach (var t in elemTileset.Elements("tile"))
                {
                    TmxImage tmxImage = TmxImage.FromXml(t.Element("image"));
                    RegisterImagePath(tmxImage.Path);

                    uint    localId  = (uint)tilesToAdd.Count();
                    uint    globalId = firstId + localId;
                    TmxTile tile     = new TmxTile(globalId, localId, tilesetName, tmxImage);
                    tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height);
                    tile.SetLocationOnSource(0, 0);
                    tilesToAdd.Add(tile);
                }
            }

            StringBuilder builder = new StringBuilder();

            foreach (TmxTile tile in tilesToAdd)
            {
                builder.AppendFormat("{0}", tile.ToString());
                if (tile != tilesToAdd.Last())
                {
                    builder.Append("\n");
                }
                this.Tiles[tile.GlobalId] = tile;
            }
            Program.WriteLine("Added {0} tiles", tilesToAdd.Count);
            Program.WriteVerbose(builder.ToString());

            // Add any extra data to tiles
            foreach (var elemTile in elemTileset.Elements("tile"))
            {
                int localTileId = TmxHelper.GetAttributeAsInt(elemTile, "id");
                var tiles       = from t in this.Tiles
                                  where t.Value.GlobalId == localTileId + firstId
                                  select t.Value;
                tiles.First().ParseXml(elemTile, this, firstId);
            }
        }
Пример #21
0
        public static TmxImage FromXml(XElement elemImage, string prefix, string postfix)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");
            tmxImage.ImageName    = $"{prefix}{Path.GetFileNameWithoutExtension(tmxImage.AbsolutePath)}{postfix}";
            int attributeAsInt  = TmxHelper.GetAttributeAsInt(elemImage, "width", 0);
            int attributeAsInt2 = TmxHelper.GetAttributeAsInt(elemImage, "height", 0);

            tmxImage.Size = new Size(attributeAsInt, attributeAsInt2);
            bool flag = true;

            if (!Settings.IsAutoExporting)
            {
                try
                {
                    if (!tmxImage.Size.IsEmpty)
                    {
                        UnityEngine.Texture2D texture2 = new UnityEngine.Texture2D(tmxImage.Size.Width, tmxImage.Size.Height);


                        //bitmapInfo.AlphaType = SKAlphaType.Unpremul;
                        //bitmapInfo.Width = tmxImage.Size.Width;
                        //bitmapInfo.Height = tmxImage.Size.Height;
                        var v = File.ReadAllBytes(tmxImage.AbsolutePath);
                        //   ImageMagick.MagickImage img = new ImageMagick.MagickImage(v);

                        texture2.LoadImage(v);
                        //texture2.LoadRawTextureData(v);
                        tmxImage.ImageBitmap = texture2;
                        //using (FileStream stream = File.Open(tmxImage.AbsolutePath, FileMode.Open))
                        //{


                        //                      tmxImage.ImageBitmap = SKBitmap.Decode(stream, bitmapInfo);
                        //}
                    }
                    else
                    {
                        UnityEngine.Texture2D texture2 = new UnityEngine.Texture2D(tmxImage.Size.Width, tmxImage.Size.Height);

                        var v = File.ReadAllBytes(tmxImage.AbsolutePath);
                        texture2.LoadImage(v);
                        tmxImage.ImageBitmap = texture2;
                        flag = false;
                    }
                    TmxImage tmxImage2 = tmxImage;
                    tmxImage2.Size = new Size(tmxImage2.ImageBitmap.width, tmxImage.ImageBitmap.height);
                }
                catch (FileNotFoundException inner)
                {
                    throw new TmxException($"Image file not found: {tmxImage.AbsolutePath}", inner);
                }
                catch (Exception ex)
                {
                    Logger.WriteError("Skia Library exception: {0}\n\tStack:\n{1}", ex.Message, ex.StackTrace);
                    Settings.DisablePreviewing();
                }
            }
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!string.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null)
            {
                if (flag)
                {
                    Logger.WriteInfo("Removing alpha from transparent pixels.");
                    UnityEngine.Color color = TmxHelper.ColorFromHtml(tmxImage.TransparentColor);
                    color.a = 0;
                    for (int i = 0; i < tmxImage.ImageBitmap.width; i++)
                    {
                        for (int j = 0; j < tmxImage.ImageBitmap.height; j++)
                        {
                            UnityEngine.Color pixel = tmxImage.ImageBitmap.GetPixel(i, j);
                            if (pixel.r == color.r && pixel.g == color.g && pixel.b == color.b)
                            {
                                tmxImage.ImageBitmap.SetPixel(i, j, color);
                            }
                        }
                    }
                }
                else
                {
                    Logger.WriteWarning("Cannot make transparent pixels for viewing purposes. Save tileset with newer verion of Tiled.");
                }
            }
            return(tmxImage);
        }
Пример #22
0
        public static TmxImage FromXml(XElement elemImage)
        {
            TmxImage tmxImage = new TmxImage();

            tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source");

#if TILED_2_UNITY_LITE
            // Do not open the image in Tiled2UnityLite (due to difficulty with GDI+ in some mono installs)
            int width  = TmxHelper.GetAttributeAsInt(elemImage, "width");
            int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
            tmxImage.Size = new System.Drawing.Size(width, height);
#else
            try
            {
                // We use pre-muliplied alpha pixel format (it is 2x faster)
                Bitmap bitmapRaw   = (Bitmap)Bitmap.FromFile(tmxImage.AbsolutePath);
                Bitmap bitmapPArgb = new Bitmap(bitmapRaw.Width, bitmapRaw.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                using (Graphics g = Graphics.FromImage(bitmapPArgb))
                {
                    g.DrawImage(bitmapRaw, 0, 0, bitmapPArgb.Width, bitmapPArgb.Height);
                    tmxImage.ImageBitmap = bitmapPArgb;
                }

                tmxImage.ImageBitmap = bitmapPArgb;
            }
            catch (FileNotFoundException fnf)
            {
                string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath);
                throw new TmxException(msg, fnf);

                // Testing for when image files are missing. Just make up an image.
                //int width = TmxHelper.GetAttributeAsInt(elemImage, "width");
                //int height = TmxHelper.GetAttributeAsInt(elemImage, "height");
                //tmxImage.ImageBitmap = new Bitmap(width, height);
                //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap))
                //{
                //    int color32 = tmxImage.AbsolutePath.GetHashCode();
                //    Color color = Color.FromArgb(color32);
                //    color = Color.FromArgb(255, color);
                //    using (Brush brush = new SolidBrush(color))
                //    {
                //        g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size));
                //    }
                //}
            }

            tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height);
#endif

            // Some images use a transparency color key instead of alpha (blerg)
            tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", "");
            if (!String.IsNullOrEmpty(tmxImage.TransparentColor))
            {
                if (!tmxImage.TransparentColor.StartsWith("#"))
                {
                    // The hash makes it an HTML color
                    tmxImage.TransparentColor = "#" + tmxImage.TransparentColor;
                }

#if !TILED_2_UNITY_LITE
                System.Drawing.Color transColor = System.Drawing.ColorTranslator.FromHtml(tmxImage.TransparentColor);
                tmxImage.ImageBitmap.MakeTransparent(transColor);
#endif
            }

            return(tmxImage);
        }