Esempio n. 1
0
        public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "objectgroup");

            TmxObjectGroup tmxObjectGroup = new TmxObjectGroup();

            tmxObjectGroup.Name       = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObjectGroup.Visible    = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObjectGroup.Color      = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128));
            tmxObjectGroup.Properties = TmxProperties.FromXml(xml);

            PointF offset = new PointF(0, 0);

            offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            tmxObjectGroup.Offset = offset;

            // Get all the objects
            Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name);
            var objects = from obj in xml.Elements("object")
                          select TmxObject.FromXml(obj, tmxMap);

            tmxObjectGroup.Objects = objects.ToList();

            return(tmxObjectGroup);
        }
Esempio n. 2
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteVerbose("Parsing map root ...");
            XElement xElement = doc.Element("map");

            try
            {
                Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(xElement, "orientation");
                StaggerAxis         = TmxHelper.GetAttributeAsEnum(xElement, "staggeraxis", MapStaggerAxis.Y);
                StaggerIndex        = TmxHelper.GetAttributeAsEnum(xElement, "staggerindex", MapStaggerIndex.Odd);
                HexSideLength       = TmxHelper.GetAttributeAsInt(xElement, "hexsidelength", 0);
                DrawOrderHorizontal = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("right") ? 1 : (-1));
                DrawOrderVertical   = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("down") ? 1 : (-1));
                Width           = TmxHelper.GetAttributeAsInt(xElement, "width");
                Height          = TmxHelper.GetAttributeAsInt(xElement, "height");
                TileWidth       = TmxHelper.GetAttributeAsInt(xElement, "tilewidth");
                TileHeight      = TmxHelper.GetAttributeAsInt(xElement, "tileheight");
                BackgroundColor = TmxHelper.GetAttributeAsColor(xElement, "backgroundcolor", new Color32(128, 128, 128, 255));
            }
            catch (Exception inner)
            {
                TmxException.FromAttributeException(inner, xElement);
            }
            Properties = TmxProperties.FromXml(xElement);
            IsResource = Properties.GetPropertyValueAsBoolean("unity:resource", false);
            IsResource = (IsResource || !string.IsNullOrEmpty(Properties.GetPropertyValueAsString("unity:resourcePath", null)));
            ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            ParseAllTilesets(doc);
            ParseAllTemplates(doc);
            LayerNodes      = TmxLayerNode.ListFromXml(xElement, null, this);
            MapSizeInPixels = CalculateMapSizeInPixels();
            TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor();

            Visit(visitor);
        }
Esempio n. 3
0
        protected void FromXmlInternal(XElement xml)
        {
            // Get common elements amoung layer nodes from xml
            this.Name    = TmxHelper.GetAttributeAsString(xml, "name", "");
            this.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            this.Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1);

            // Get the offset
            PointF offset = new PointF(0, 0);

            offset.X    = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y    = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            this.Offset = offset;

            // Get all the properties
            this.Properties = TmxProperties.FromXml(xml);

            // Set the "ignore" setting on this object group
            this.Ignore = this.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False);

            // Explicit sorting properties
            this.ExplicitSortingLayerName = this.Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            if (this.Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                this.ExplicitSortingOrder = this.Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }

            // Are we using a unity:layer override?
            this.UnityLayerOverrideName = this.Properties.GetPropertyValueAsString("unity:layer", "");

            // Add all our children
            this.LayerNodes = TmxLayerNode.ListFromXml(xml, this, this.TmxMap);
        }
Esempio n. 4
0
        public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "objectgroup");

            TmxObjectGroup tmxObjectGroup = new TmxObjectGroup();

            // Order within Xml file is import for layer types
            tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count();

            tmxObjectGroup.Name       = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObjectGroup.Visible    = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObjectGroup.Color      = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128));
            tmxObjectGroup.Properties = TmxProperties.FromXml(xml);

            PointF offset = new PointF(0, 0);

            offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            tmxObjectGroup.Offset = offset;

            // Get all the objects
            Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name);
            var objects = from obj in xml.Elements("object")
                          select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap);

            tmxObjectGroup.Objects = objects.ToList();

            // Are we using a unity:layer override?
            tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", "");

            return(tmxObjectGroup);
        }
Esempio n. 5
0
        private void ParseMap(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            Program.WriteVerbose(doc.ToString());
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsString(map, "orientation");
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // We only support orthogonal maps
            if (this.Orientation != "orthogonal")
            {
                TmxException.ThrowFormat("Only orthogonal maps are supported. This map is set to \"{0}\"", this.Orientation);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);
        }
Esempio n. 6
0
        private void ParseMapXml(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            //Program.WriteVerbose(doc.ToString()); // Some TMX files are far too big (cause out of memory exception) so don't do this
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);

            // Once everything is loaded, take a moment to do additional plumbing
            ParseCompleted();
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap)
        {
            Logger.WriteVerbose("Parsing object ...");
            uint attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "tid", 0u);

            if (attributeAsUInt != 0 && tmxMap.Templates.TryGetValue(attributeAsUInt, out TgxTemplate value))
            {
                xml    = value.Templatize(xml);
                tmxMap = value.TemplateGroupMap;
            }
            TmxObject tmxObject = null;

            if (xml.Element("ellipse") != null)
            {
                tmxObject = new TmxObjectEllipse();
            }
            else if (xml.Element("polygon") != null)
            {
                tmxObject = new TmxObjectPolygon();
            }
            else if (xml.Element("polyline") != null)
            {
                tmxObject = new TmxObjectPolyline();
            }
            else if (xml.Attribute("gid") != null)
            {
                uint attributeAsUInt2 = TmxHelper.GetAttributeAsUInt(xml, "gid");
                attributeAsUInt2 = TmxMath.GetTileIdWithoutFlags(attributeAsUInt2);
                if (tmxMap.Tiles.ContainsKey(attributeAsUInt2))
                {
                    tmxObject = new TmxObjectTile();
                }
                else
                {
                    Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", attributeAsUInt2, xml.ToString());
                    tmxObject = new TmxObjectRectangle();
                }
            }
            else
            {
                tmxObject = new TmxObjectRectangle();
            }
            tmxObject.Id                = TmxHelper.GetAttributeAsInt(xml, "id", 0);
            tmxObject.Name              = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObject.Type              = TmxHelper.GetAttributeAsString(xml, "type", "");
            tmxObject.Visible           = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1);
            tmxObject.ParentObjectGroup = tmxObjectGroup;
            float attributeAsFloat  = TmxHelper.GetAttributeAsFloat(xml, "x");
            float attributeAsFloat2 = TmxHelper.GetAttributeAsFloat(xml, "y");
            float attributeAsFloat3 = TmxHelper.GetAttributeAsFloat(xml, "width", 0f);
            float attributeAsFloat4 = TmxHelper.GetAttributeAsFloat(xml, "height", 0f);
            float attributeAsFloat5 = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0f);

            tmxObject.Position   = new PointF(attributeAsFloat, attributeAsFloat2);
            tmxObject.Size       = new SizeF(attributeAsFloat3, attributeAsFloat4);
            tmxObject.Rotation   = attributeAsFloat5;
            tmxObject.Properties = TmxProperties.FromXml(xml);
            tmxObject.InternalFromXml(xml, tmxMap);
            return(tmxObject);
        }
Esempio n. 9
0
        private void ParseMap(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            Program.WriteVerbose(doc.ToString());
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public static TmxFrame FromXml(XElement xml, uint globalStartId)
        {
            TmxFrame tmxFrame        = new TmxFrame();
            uint     attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "tileid");

            tmxFrame.GlobalTileId = attributeAsUInt + globalStartId;
            tmxFrame.DurationMs   = TmxHelper.GetAttributeAsInt(xml, "duration", 100);
            return(tmxFrame);
        }
Esempio n. 12
0
        public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap)
        {
            Program.WriteLine("Parsing object ...");
            Program.WriteVerbose(xml.ToString());

            // What kind of TmxObject are we creating?
            TmxObject tmxObject = null;

            if (xml.Element("ellipse") != null)
            {
                tmxObject = new TmxObjectEllipse();
            }
            else if (xml.Element("polygon") != null)
            {
                tmxObject = new TmxObjectPolygon();
            }
            else if (xml.Element("polyline") != null)
            {
                tmxObject = new TmxObjectPolyline();
            }
            else if (xml.Attribute("gid") != null)
            {
                tmxObject = new TmxObjectTile();
            }
            else
            {
                // Just a rectangle
                tmxObject = new TmxObjectRectangle();
            }

            // Data found on every object type
            tmxObject.Name              = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObject.Type              = TmxHelper.GetAttributeAsString(xml, "type", "");
            tmxObject.Visible           = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObject.ParentObjectGroup = tmxObjectGroup;

            float x = TmxHelper.GetAttributeAsFloat(xml, "x");
            float y = TmxHelper.GetAttributeAsFloat(xml, "y");
            float w = TmxHelper.GetAttributeAsFloat(xml, "width", 0);
            float h = TmxHelper.GetAttributeAsFloat(xml, "height", 0);
            float r = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0);

            tmxObject.Position = new System.Drawing.PointF(x, y);
            tmxObject.Size     = new System.Drawing.SizeF(w, h);
            tmxObject.Rotation = r;

            tmxObject.Properties = TmxProperties.FromXml(xml);

            tmxObject.InternalFromXml(xml, tmxMap);

            return(tmxObject);
        }
Esempio n. 13
0
        private static List <TmxChunk> ListFromDataXml_Infinite(XElement xml, TmxData parentData)
        {
            List <TmxChunk> list = new List <TmxChunk>();

            foreach (XElement item in xml.Elements("chunk"))
            {
                TmxChunk tmxChunk = new TmxChunk(parentData);
                tmxChunk.X      = TmxHelper.GetAttributeAsInt(item, "x", 0);
                tmxChunk.Y      = TmxHelper.GetAttributeAsInt(item, "y", 0);
                tmxChunk.Width  = TmxHelper.GetAttributeAsInt(item, "width", 0);
                tmxChunk.Height = TmxHelper.GetAttributeAsInt(item, "height", 0);
                tmxChunk.ReadTileIds(item);
                list.Add(tmxChunk);
            }
            return(list);
        }
Esempio n. 14
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteVerbose("Parsing map root ...");

            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            // Check properties for us being a resource
            this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false);
            this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null));

            ParseAllTilesets(doc);

            // Get all our child layer nodes
            this.LayerNodes = TmxLayerNode.ListFromXml(map, null, this);

            // Calcuate the size of the map. Isometric and hex maps make this more complicated than a simple width and height thing.
            this.MapSizeInPixels = CalculateMapSizeInPixels();

            // Visit each node in the map to assign display order
            TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor();

            this.Visit(visitor);
        }
Esempio n. 15
0
        public static TmxLayer FromXml(XElement elem, int layerIndex)
        {
            Program.WriteVerbose(elem.ToString());
            TmxLayer tmxLayer = new TmxLayer();

            // 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.DefaultName = TmxHelper.GetAttributeAsString(elem, "name");
            tmxLayer.UniqueName  = String.Format("{0}_{1}", tmxLayer.DefaultName, layerIndex.ToString("D2")).Replace(" ", "_");

            tmxLayer.Visible    = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1;
            tmxLayer.Width      = TmxHelper.GetAttributeAsInt(elem, "width");
            tmxLayer.Height     = TmxHelper.GetAttributeAsInt(elem, "height");
            tmxLayer.Properties = TmxProperties.FromXml(elem);

            tmxLayer.ParseData(elem.Element("data"));

            return(tmxLayer);
        }
Esempio n. 16
0
        protected void FromXmlInternal(XElement xml)
        {
            Name    = TmxHelper.GetAttributeAsString(xml, "name", "");
            Visible = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1);
            Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1f);
            PointF offset = new PointF(0f, 0f);

            offset.X   = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0f);
            offset.Y   = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0f);
            Offset     = offset;
            Properties = TmxProperties.FromXml(xml);
            Ignore     = Properties.GetPropertyValueAsEnum("unity:ignore", IgnoreSettings.False);
            ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            if (Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                ExplicitSortingOrder = Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }
            UnityLayerOverrideName = Properties.GetPropertyValueAsString("unity:layer", "");
            LayerNodes             = ListFromXml(xml, this, ParentMap);
        }
Esempio n. 17
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);
        }
Esempio n. 18
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteLine("Parsing map root ...");

            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            // Check properties for us being a resource
            this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false);
            this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null));

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);

            // Once everything is loaded, take a moment to do additional plumbing
            ParseCompleted();
        }
Esempio n. 19
0
        public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "objectgroup");

            TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(tmxMap);

            // Order within Xml file is import for layer types
            tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count();

            tmxObjectGroup.Name       = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObjectGroup.Visible    = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObjectGroup.Opacity    = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1);
            tmxObjectGroup.Color      = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128));
            tmxObjectGroup.Properties = TmxProperties.FromXml(xml);

            // Set the "ignore" setting on this object group
            tmxObjectGroup.Ignore = tmxObjectGroup.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False);

            PointF offset = new PointF(0, 0);

            offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0);
            offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0);
            tmxObjectGroup.Offset = offset;

            // Get all the objects
            Logger.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name);
            var objects = from obj in xml.Elements("object")
                          select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap);

            // The objects are ordered "visually" by Y position
            tmxObjectGroup.Objects = objects.OrderBy(o => TmxMath.ObjectPointFToMapSpace(tmxMap, o.Position).Y).ToList();

            // Are we using a unity:layer override?
            tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", "");

            return(tmxObjectGroup);
        }
Esempio n. 20
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);
        }
Esempio n. 21
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);
                }
            }
        }
Esempio n. 22
0
 private void ParseLayerXml(XElement xml)
 {
     Width  = TmxHelper.GetAttributeAsInt(xml, "width");
     Height = TmxHelper.GetAttributeAsInt(xml, "height");
     ParseData(xml.Element("data"));
 }
Esempio n. 23
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);
        }
Esempio n. 24
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);
                }
            }
        }
Esempio n. 25
0
        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);
        }
Esempio n. 26
0
        public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap)
        {
            Logger.WriteLine("Parsing object ...");

            // What kind of TmxObject are we creating?
            TmxObject tmxObject = null;

            if (xml.Element("ellipse") != null)
            {
                tmxObject = new TmxObjectEllipse();
            }
            else if (xml.Element("polygon") != null)
            {
                tmxObject = new TmxObjectPolygon();
            }
            else if (xml.Element("polyline") != null)
            {
                tmxObject = new TmxObjectPolyline();
            }
            else if (xml.Attribute("gid") != null)
            {
                uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");
                gid = TmxMath.GetTileIdWithoutFlags(gid);
                if (tmxMap.Tiles.ContainsKey(gid))
                {
                    tmxObject = new TmxObjectTile();
                }
                else
                {
                    // For some reason, the tile is not in any of our tilesets
                    // Warn the user and use a rectangle
                    Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", gid, xml.ToString());
                    tmxObject = new TmxObjectRectangle();
                }
            }
            else
            {
                // Just a rectangle
                tmxObject = new TmxObjectRectangle();
            }

            // Data found on every object type
            tmxObject.Name              = TmxHelper.GetAttributeAsString(xml, "name", "");
            tmxObject.Type              = TmxHelper.GetAttributeAsString(xml, "type", "");
            tmxObject.Visible           = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1;
            tmxObject.ParentObjectGroup = tmxObjectGroup;

            float x = TmxHelper.GetAttributeAsFloat(xml, "x");
            float y = TmxHelper.GetAttributeAsFloat(xml, "y");
            float w = TmxHelper.GetAttributeAsFloat(xml, "width", 0);
            float h = TmxHelper.GetAttributeAsFloat(xml, "height", 0);
            float r = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0);

            tmxObject.Position = new System.Drawing.PointF(x, y);
            tmxObject.Size     = new System.Drawing.SizeF(w, h);
            tmxObject.Rotation = r;

            tmxObject.Properties = TmxProperties.FromXml(xml);

            tmxObject.InternalFromXml(xml, tmxMap);

            return(tmxObject);
        }
Esempio n. 27
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);
            }
        }
Esempio n. 28
0
        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);
        }
Esempio n. 29
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);
        }