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); }
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); }
private void ParseImageLayerXml(XElement xml) { if (xml.Element("image") == null) { Logger.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", base.Name); base.Ignore = IgnoreSettings.True; } else { Width = 1; Height = 1; string imagePath = TmxHelper.GetAttributeAsFullPath(xml.Element("image"), "source"); TmxTile value = base.ParentMap.Tiles.First((KeyValuePair <uint, TmxTile> t) => t.Value.TmxImage.AbsolutePath.ToLower() == imagePath.ToLower()).Value; Data = new TmxData(this); TmxChunk tmxChunk = new TmxChunk(Data); tmxChunk.X = 0; tmxChunk.Y = 0; tmxChunk.Width = 1; tmxChunk.Height = 1; tmxChunk.TileIds.Add(value.GlobalId); Data.Chunks.Add(tmxChunk); PointF offset = base.Offset; offset.Y -= (float)base.ParentMap.TileHeight; offset.Y += (float)value.TmxImage.Size.Height; PointF pointF = TmxMath.TileCornerInScreenCoordinates(base.ParentMap, 0, 0); offset.X -= pointF.X; offset.Y -= pointF.Y; offset.X += TmxHelper.GetAttributeAsFloat(xml, "x", 0f); offset.Y += TmxHelper.GetAttributeAsFloat(xml, "y", 0f); base.Offset = offset; } }
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); }
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); }
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); }
public static TmxLayer FromXml(XElement elem, TmxLayerNode parent, TmxMap tmxMap) { TmxLayer tmxLayer = new TmxLayer(parent, tmxMap); tmxLayer.FromXmlInternal(elem); // We can build a layer from a "tile layer" (default) or an "image layer" if (elem.Name == "layer") { tmxLayer.Width = TmxHelper.GetAttributeAsInt(elem, "width"); tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height"); tmxLayer.ParseData(elem.Element("data")); } else if (elem.Name == "imagelayer") { XElement xmlImage = elem.Element("image"); if (xmlImage == null) { Logger.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", tmxLayer.Name); tmxLayer.Ignore = IgnoreSettings.True; return(tmxLayer); } // An image layer is sort of like an tile layer but with just one tile tmxLayer.Width = 1; tmxLayer.Height = 1; // Find the "tile" that matches our image string imagePath = TmxHelper.GetAttributeAsFullPath(elem.Element("image"), "source"); TmxTile tile = tmxMap.Tiles.First(t => t.Value.TmxImage.AbsolutePath == imagePath).Value; tmxLayer.TileIds = new uint[1] { tile.GlobalId }; // The image layer needs to be tranlated in an interesting way when expressed as a tile layer PointF translated = tmxLayer.Offset; // Make up for height of a regular tile in the map translated.Y -= (float)tmxMap.TileHeight; // Make up for the height of this image translated.Y += (float)tile.TmxImage.Size.Height; // Correct for any orientation effects on the map (like isometric) // (We essentially undo the translation via orientation here) PointF orientation = TmxMath.TileCornerInScreenCoordinates(tmxMap, 0, 0); translated.X -= orientation.X; translated.Y -= orientation.Y; // Translate by the x and y coordiantes translated.X += TmxHelper.GetAttributeAsFloat(elem, "x", 0); translated.Y += TmxHelper.GetAttributeAsFloat(elem, "y", 0); tmxLayer.Offset = translated; } // Sometimes TMX files have "dead" tiles in them (tiles that were removed but are still referenced) // Remove these tiles from the layer by replacing them with zero for (int t = 0; t < tmxLayer.TileIds.Length; ++t) { uint tileId = tmxLayer.TileIds[t]; tileId = TmxMath.GetTileIdWithoutFlags(tileId); if (!tmxMap.Tiles.ContainsKey(tileId)) { tmxLayer.TileIds[t] = 0; } } // Each layer will be broken down into "meshes" which are collections of tiles matching the same texture or animation tmxLayer.Meshes = TmxMesh.ListFromTmxLayer(tmxLayer); // Each layer may contain different collision types which are themselves put into "Collison Layers" to be processed later tmxLayer.BuildCollisionLayers(); return(tmxLayer); }
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); }
public static TmxLayer FromXml(XElement elem, TmxMap tmxMap) { Program.WriteVerbose(elem.ToString()); TmxLayer tmxLayer = new TmxLayer(tmxMap); // Order within Xml file is import for layer types tmxLayer.XmlElementIndex = elem.NodesBeforeSelf().Count(); // Have to decorate layer names in order to force them into being unique // Also, can't have whitespace in the name because Unity will add underscores tmxLayer.Name = TmxHelper.GetAttributeAsString(elem, "name"); tmxLayer.Visible = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1; tmxLayer.Opacity = TmxHelper.GetAttributeAsFloat(elem, "opacity", 1); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(elem, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(elem, "offsety", 0); tmxLayer.Offset = offset; // Set our properties tmxLayer.Properties = TmxProperties.FromXml(elem); // Set the "ignore" setting on this layer tmxLayer.Ignore = tmxLayer.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False); // We can build a layer from a "tile layer" (default) or an "image layer" if (elem.Name == "layer") { tmxLayer.Width = TmxHelper.GetAttributeAsInt(elem, "width"); tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height"); tmxLayer.ParseData(elem.Element("data")); } else if (elem.Name == "imagelayer") { XElement xmlImage = elem.Element("image"); if (xmlImage == null) { Program.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", tmxLayer.Name); tmxLayer.Ignore = IgnoreSettings.True; return(tmxLayer); } // An image layer is sort of like an tile layer but with just one tile tmxLayer.Width = 1; tmxLayer.Height = 1; // Find the "tile" that matches our image string imagePath = TmxHelper.GetAttributeAsFullPath(elem.Element("image"), "source"); TmxTile tile = tmxMap.Tiles.First(t => t.Value.TmxImage.AbsolutePath == imagePath).Value; tmxLayer.TileIds = new uint[1] { tile.GlobalId }; // The image layer needs to be tranlated in an interesting way when expressed as a tile layer PointF translated = tmxLayer.Offset; // Make up for height of a regular tile in the map translated.Y -= (float)tmxMap.TileHeight; // Make up for the height of this image translated.Y += (float)tile.TmxImage.Size.Height; // Correct for any orientation effects on the map (like isometric) // (We essentially undo the translation via orientation here) PointF orientation = TmxMath.TileCornerInScreenCoordinates(tmxMap, 0, 0); translated.X -= orientation.X; translated.Y -= orientation.Y; // Translate by the x and y coordiantes translated.X += TmxHelper.GetAttributeAsFloat(elem, "x", 0); translated.Y += TmxHelper.GetAttributeAsFloat(elem, "y", 0); tmxLayer.Offset = translated; } // Each layer will be broken down into "meshes" which are collections of tiles matching the same texture or animation tmxLayer.Meshes = TmxMesh.ListFromTmxLayer(tmxLayer); return(tmxLayer); }
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); }
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); }