Ejemplo n.º 1
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            var points = from pt in xml.Element("polygon").Attribute("points").Value.Split(' ')
                         let x = float.Parse(pt.Split(',')[0])
                         let y = float.Parse(pt.Split(',')[1])
                         select new PointF(x, y);

            this.Points = points.ToList();

            // Test if polygons are counter clocksise
            // From: http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order
            float sum = 0.0f;
            for (int i = 1; i < this.Points.Count(); i++)
            {
                var p1 = this.Points[i - 1];
                var p2 = this.Points[i];

                float v = (p2.X - p1.X) * -(p2.Y + p1.Y);
                sum += v;
            }

            if (sum < 0)
            {
                // Winding of polygons is counter-clockwise. Reverse the list.
                this.Points.Reverse();
            }
        }
Ejemplo n.º 2
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
            Logger.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;
        }
Ejemplo n.º 3
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            // Get the tile
            uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");
            this.FlippedHorizontal = TmxMath.IsTileFlippedHorizontally(gid);
            this.FlippedVertical = TmxMath.IsTileFlippedVertically(gid);
            uint rawTileId = TmxMath.GetTileIdWithoutFlags(gid);

            this.Tile = tmxMap.Tiles[rawTileId];

            // The tile needs to have a mesh on it.
            // Note: The tile may already be referenced by another TmxObjectTile instance, and as such will have its mesh data already made
            if (this.Tile.Meshes.Count() == 0)
            {
                this.Tile.Meshes = TmxMesh.FromTmxTile(this.Tile, tmxMap);
            }

            // Check properties for layer placement
            if (this.Properties.PropertyMap.ContainsKey("unity:sortingLayerName"))
            {
                this.SortingLayerName = this.Properties.GetPropertyValueAsString("unity:sortingLayerName");
            }
            if (this.Properties.PropertyMap.ContainsKey("unity:sortingOrder"))
            {
                this.SortingOrder = this.Properties.GetPropertyValueAsInt("unity:sortingOrder");
            }
        }
Ejemplo n.º 4
0
        public static bool DoStaggerY(TmxMap tmxMap, int y)
        {
            int staggerX = (tmxMap.StaggerAxis == TmxMap.MapStaggerAxis.X) ? 1 : 0;
            int staggerEven = (tmxMap.StaggerIndex == TmxMap.MapStaggerIndex.Even) ? 1 : 0;

            return staggerX == 0 && ((y & 1) ^ staggerEven) != 0;
        }
Ejemplo n.º 5
0
 public TmxLayer(TmxMap map)
 {
     this.TmxMap = map;
     this.Visible = true;
     this.Opacity = 1.0f;
     this.CollisionLayers = new List<TmxLayer>();
 }
Ejemplo n.º 6
0
        public static TmxMap LoadFromFile(string tmxPath)
        {
            // Refresh uniqueId counter
            TmxMap.NextUniqueId = 0;

            string fullTmxPath = Path.GetFullPath(tmxPath);
            using (ChDir chdir = new ChDir(fullTmxPath))
            {
                TmxMap tmxMap = new TmxMap();
                XDocument doc = tmxMap.LoadDocument(fullTmxPath);

                tmxMap.Name = Path.GetFileNameWithoutExtension(fullTmxPath);
                tmxMap.ParseMap(doc);

                // We're done reading and parsing the tmx file
                Program.WriteLine("Map details: {0}", tmxMap.ToString());
                Program.WriteSuccess("Finished parsing file: {0}", fullTmxPath);

                // Let listeners know of our success
                if (TmxMap.OnReadTmxFileCompleted != null)
                {
                    TmxMap.OnReadTmxFileCompleted(tmxMap);
                }

                return tmxMap;
            }
        }
Ejemplo n.º 7
0
 protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
 {
     this.Points = new List<System.Drawing.PointF>();
     this.Points.Add(new PointF(0, 0));
     this.Points.Add(new PointF(this.Size.Width, 0));
     this.Points.Add(new PointF(this.Size.Width, this.Size.Height));
     this.Points.Add(new PointF(0, this.Size.Height));
 }
Ejemplo n.º 8
0
 public static string UnityFriendlyMeshName(TmxMap map, string layerName, string imageName)
 {
     // Trying really hard to come up with a mesh-naming scheme that Unity won't rename on us
     // Using a combination of proper layer and image names won't work so stick with safe ascii and no spaces
     string meshName = map.GetMeshName(layerName, imageName);
     meshName = meshName.Replace(" ", "_");
     return meshName;
 }
Ejemplo n.º 9
0
 public Tiled2UnityViewer(TmxMap tmxMap)
 {
     this.tmxMap = tmxMap;
     this.preferencesForm = new PreviewPreferencesForm();
     this.preferencesForm.ApplyChanges += new PreviewPreferencesForm.OnApplyChanges(preferencesForm_ApplyChanges);
     this.preferencesForm.InitializePrefernces(tmxMap);
     InitializeComponent();
 }
Ejemplo n.º 10
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            // Get the tile
            uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");
            this.Tile = tmxMap.Tiles[gid];

            // Our size is given by the tile
            this.Size = this.Tile.TileSize;
        }
Ejemplo n.º 11
0
        public static List<PointF> GetPointsInMapSpace(TmxMap tmxMap, TmxHasPoints objectWithPoints)
        {
            PointF local = TmxMath.ObjectPointFToMapSpace(tmxMap, 0, 0);
            local.X = -local.X;
            local.Y = -local.Y;

            List<PointF> xfPoints = objectWithPoints.Points.Select(pt => TmxMath.ObjectPointFToMapSpace(tmxMap, pt)).ToList();
            xfPoints = xfPoints.Select(pt => TmxMath.AddPoints(pt, local)).ToList();
            return xfPoints;
        }
Ejemplo n.º 12
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "object");
            Debug.Assert(xml.Element("polyline") != null);

            var points = from pt in xml.Element("polyline").Attribute("points").Value.Split(' ')
                         let x = float.Parse(pt.Split(',')[0])
                         let y = float.Parse(pt.Split(',')[1])
                         select new PointF(x, y);

            this.Points = points.ToList();
        }
Ejemplo n.º 13
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            this.Points = new List<System.Drawing.PointF>();
            this.Points.Add(new PointF(0, 0));
            this.Points.Add(new PointF(this.Size.Width, 0));
            this.Points.Add(new PointF(this.Size.Width, this.Size.Height));
            this.Points.Add(new PointF(0, this.Size.Height));

            if (this.Size.Width == 0 || this.Size.Height == 0)
            {
                Program.WriteWarning("Warning: Rectangle has zero width or height in object group\n{0}", xml.Parent.ToString());
            }
        }
Ejemplo n.º 14
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;
        }
Ejemplo n.º 15
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            // Get the tile
            uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid");
            this.FlippedHorizontal = TmxMath.IsTileFlippedHorizontally(gid);
            this.FlippedVertical = TmxMath.IsTileFlippedVertically(gid);
            uint rawTileId = TmxMath.GetTileIdWithoutFlags(gid);

            this.Tile = tmxMap.Tiles[rawTileId];

            // The tile needs to have a mesh on it.
            // Note: The tile may already be referenced by another TmxObjectTile instance, and as such will have its mesh data already made
            if (this.Tile.Meshes.Count() == 0)
            {
                this.Tile.Meshes = TmxMesh.FromTmxTile(this.Tile, tmxMap);
            }
        }
Ejemplo n.º 16
0
        public static PointF ObjectPointFToMapSpace(TmxMap tmxMap, PointF pt)
        {
            if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric)
            {
                PointF xf = PointF.Empty;

                float origin_x = tmxMap.Height * tmxMap.TileWidth * 0.5f;
                float tile_y = pt.Y / tmxMap.TileHeight;
                float tile_x = pt.X / tmxMap.TileHeight;

                xf.X = (tile_x - tile_y) * tmxMap.TileWidth * 0.5f + origin_x;
                xf.Y = (tile_x + tile_y) * tmxMap.TileHeight * 0.5f;
                return xf;
            }

            // Other maps types don't transform object points
            return pt;
        }
Ejemplo n.º 17
0
        public static TmxMap LoadFromFile(string tmxPath)
        {
            string fullTmxPath = Path.GetFullPath(tmxPath);
            using (ChDir chdir = new ChDir(fullTmxPath))
            {
                TmxMap tmxMap = new TmxMap();
                XDocument doc = tmxMap.LoadDocument(fullTmxPath);

                tmxMap.Name = Path.GetFileNameWithoutExtension(fullTmxPath);
                tmxMap.ParseMapXml(doc);

                // We're done reading and parsing the tmx file
                Logger.WriteLine("Map details: {0}", tmxMap.ToString());
                Logger.WriteSuccess("Parsed: {0} ", fullTmxPath);

                tmxMap.IsLoaded = true;
                return tmxMap;
            }
        }
Ejemplo n.º 18
0
        private void FixTileColliderObjects(TmxMap tmxMap)
        {
            // Objects inside of tiles are colliders that will be merged with the colliders on neighboring tiles.
            // In order to promote this merging we have to perform the following clean up operations ...
            // - All rectangles objects are made into polygon objects
            // - All polygon objects will have their rotations burned into the polygon points (and Rotation set to zero)
            // - All cooridinates will be "sanitized" to make up for floating point errors due to rotation and poor placement of colliders
            // (The sanitation will round all numbers to the nearest 1/256th)

            // Replace rectangles with polygons
            for (int i = 0; i < this.ObjectGroup.Objects.Count; i++)
            {
                TmxObject tmxObject = this.ObjectGroup.Objects[i];
                if (tmxObject is TmxObjectRectangle)
                {
                    TmxObjectPolygon tmxObjectPolygon = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle);
                    this.ObjectGroup.Objects[i] = tmxObjectPolygon;
                }
            }

            // Burn rotation into all polygon points, sanitizing the point locations as we go
            foreach (TmxObject tmxObject in this.ObjectGroup.Objects)
            {
                TmxHasPoints tmxHasPoints = tmxObject as TmxHasPoints;
                if (tmxHasPoints != null)
                {
                    var pointfs = tmxHasPoints.Points.ToArray();

                    // Rotate our points by the rotation and position in the object
                    TmxMath.RotatePoints(pointfs, tmxObject);

                    // Sanitize our points to make up for floating point precision errors
                    pointfs = pointfs.Select(TmxMath.Sanitize).ToArray();

                    // Set the points back into the object
                    tmxHasPoints.Points = pointfs.ToList();

                    // Zero out our rotation
                    tmxObject.BakeRotation();
                }
            }
        }
Ejemplo n.º 19
0
        protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
        {
            Debug.Assert(xml.Name == "object");
            Debug.Assert(xml.Element("polyline") != null);

            var points = from pt in xml.Element("polyline").Attribute("points").Value.Split(' ')
                         let x = float.Parse(pt.Split(',')[0])
                         let y = float.Parse(pt.Split(',')[1])
                         select new PointF(x, y);

            this.Points = points.ToList();
            float grid = 4;
            for (int i = 0; i < Points.Count; i++)
            {
                PointF pt = Points[i];
                int x = (int)(Math.Round(pt.X / grid) * grid);
                int y = (int)(Math.Round(pt.Y / grid) * grid);
                Points[i] = new PointF(x, y);
            }
        }
        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);

            // 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;
        }
Ejemplo n.º 21
0
        public void ParseXml(XElement elem, TmxMap tmxMap, uint firstId)
        {
            Program.WriteLine("Parse tile data (gid = {0}, id {1}) ...", this.GlobalId, this.LocalId);
            Program.WriteVerbose(elem.ToString());

            this.Properties = TmxProperties.FromXml(elem);

            // Do we have an object group for this tile?
            XElement elemObjectGroup = elem.Element("objectgroup");
            if (elemObjectGroup != null)
            {
                this.ObjectGroup = TmxObjectGroup.FromXml(elemObjectGroup, tmxMap);
            }

            // Is this an animated tile?
            XElement elemAnimation = elem.Element("animation");
            if (elemAnimation != null)
            {
                this.Animation = TmxAnimation.FromXml(elemAnimation, firstId);
            }
        }
        public void InitializePrefernces(TmxMap map)
        {
            this.Preferences = new List<LayerPreference>();

            Dictionary<string, Color> layers2Colors = PerLayerColorData.StringCollectionToDictionary(Properties.Settings.Default.PerLayerColors);
            Color defaultColor = System.Drawing.Color.Tomato;

            foreach (var layer in map.Layers)
            {
                if (layer.Visible)
                {
                    string name = layer.DefaultName;
                    System.Drawing.Color color = layers2Colors.ContainsKey(name) ? layers2Colors[name] : defaultColor;
                    this.Preferences.Add(new LayerPreference() { Name = name, Previewing = true, Color = color, CanEditColor = true });
                }
            }

            // Object groups have a color setting from the map file. This is a purposeful setting so honor it and don't allow edits.
            foreach (var objects in map.ObjectGroups)
            {
                if (objects.Visible)
                {
                    string name = objects.Name;
                    Color color = objects.Color;
                    this.Preferences.Add(new LayerPreference() { Name = name, Previewing = true, Color = color, CanEditColor = false });
                }
            }

            for (int i = 0; i < this.Preferences.Count(); ++i)
            {
                var row = this.Preferences[i];
                this.dataGridView.Rows.Add(new object[3] { row.Previewing, row.Name, row.Color });

                if (row.CanEditColor == false)
                {
                    this.dataGridView.Rows[i].Cells[2].ReadOnly = true;
                }
            }
        }
Ejemplo n.º 23
0
        public void ParseXml(XElement elem, TmxMap tmxMap, uint firstId)
        {
            Program.WriteLine("Parse tile data (gid = {0}, id {1}) ...", this.GlobalId, this.LocalId);
            Program.WriteVerbose(elem.ToString());

            this.Properties = TmxProperties.FromXml(elem);

            // Do we have an object group for this tile?
            XElement elemObjectGroup = elem.Element("objectgroup");

            if (elemObjectGroup != null)
            {
                this.ObjectGroup = TmxObjectGroup.FromXml(elemObjectGroup, tmxMap);
            }

            // Is this an animated tile?
            XElement elemAnimation = elem.Element("animation");

            if (elemAnimation != null)
            {
                this.Animation = TmxAnimation.FromXml(elemAnimation, firstId);
            }
        }
Ejemplo n.º 24
0
        public bool InitializeWithArgs(string[] args, bool summary)
        {
            Logger.WriteLine("Command: {0}", String.Join(" ", args));

            // Create our map instance (which is empty/unloaded at first)
            this.TmxMap = new TmxMap();

            // Parse the arguments and let our listeners know in case any settings changed
            if (summary)
            {
                this.summaryReport.Capture("Arguments");
            }

            ParseEnvironmentVariables();
            bool success = ParseOptions(args);

            if (summary)
            {
                this.summaryReport.Report();
            }

            return(success);
        }
Ejemplo n.º 25
0
        public void ParseTileXml(XElement elem, TmxMap tmxMap, uint firstId)
        {
            Logger.WriteLine("Parse tile data (gid = {0}, id {1}) ...", this.GlobalId, this.LocalId);

            this.Properties = TmxProperties.FromXml(elem);

            // Do we have an object group for this tile?
            XElement elemObjectGroup = elem.Element("objectgroup");

            if (elemObjectGroup != null)
            {
                this.ObjectGroup = TmxObjectGroup.FromXml(elemObjectGroup, null, tmxMap);
                FixTileColliderObjects(tmxMap);
            }

            // Is this an animated tile?
            XElement elemAnimation = elem.Element("animation");

            if (elemAnimation != null)
            {
                this.Animation = TmxAnimation.FromXml(elemAnimation, firstId);
            }
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
0
        public void LoadTmxFile(string tmxFilePath)
        {
            this.TmxFilePath = tmxFilePath;

            this.summaryReport.Capture("Loading");

            // Load the TMX map
            try
            {
                this.TmxMap = TmxMap.LoadFromFile(this.TmxFilePath);

                // Load the Object Type Xml file if it exists
                if (!String.IsNullOrEmpty(Tiled2Unity.Settings.ObjectTypeXml))
                {
                    if (File.Exists(Tiled2Unity.Settings.ObjectTypeXml))
                    {
                        this.TmxMap.LoadObjectTypeXml(Tiled2Unity.Settings.ObjectTypeXml);
                    }
                    else
                    {
                        Logger.WriteError("Object Type Xml files does not exist: '{0}'", Tiled2Unity.Settings.ObjectTypeXml);
                    }
                }
            }
            catch (TmxException tmx)
            {
                this.TmxMap = new TmxMap();
                Logger.WriteError(tmx.Message);
            }
            catch (Exception e)
            {
                this.TmxMap = new TmxMap();
                Logger.WriteError(e.Message);
            }

            this.summaryReport.Report();
        }
Ejemplo n.º 28
0
        private void OpenTmxFile(string tmxPath)
        {
            this.warnings.Clear();
            this.errors.Clear();

            this.buttonFolderBrowser.Enabled = false;
            this.buttonViewer.Enabled        = false;
            this.buttonExport.Enabled        = false;

            try
            {
                // Load the TMX file and its dependencies, including the Object Type Xml file used.
                this.tmxMap = TmxMap.LoadFromFile(tmxPath);
                this.tmxMap.LoadObjectTypeXml(Program.ObjectTypeXml);

                this.tmxExporter = new TiledMapExporter(this.tmxMap);
                CheckExportButton();
                ReportSummary("Compilation complete");
            }
            catch (TmxException tmx)
            {
                Program.WriteError(tmx.Message);
            }
        }
Ejemplo n.º 29
0
        public static TgxTemplateGroup FromXml(XElement xml, TmxMap map)
        {
            TgxTemplateGroup tgxTemplateGroup = new TgxTemplateGroup(map);

            tgxTemplateGroup.FirstTemplateId = TmxHelper.GetAttributeAsUInt(xml, "firsttid");
            tgxTemplateGroup.Source          = Path.GetFullPath(TmxHelper.GetAttributeAsString(xml, "source"));
            if (File.Exists(tgxTemplateGroup.Source))
            {
                using (new ChDir(tgxTemplateGroup.Source))
                {
                    XDocument xDocument = TmxMap.LoadDocument(tgxTemplateGroup.Source);
                    tgxTemplateGroup.ParseTemplateGroupXml(xDocument.Root);
                }
            }
            else
            {
                Logger.WriteError("Template group file does not exist: {0}", tgxTemplateGroup.Source);
            }
            tgxTemplateGroup.Templates.ForEach(delegate(TgxTemplate t)
            {
                map.Templates.Add(t.GlobalId, t);
            });
            return(tgxTemplateGroup);
        }
Ejemplo n.º 30
0
        protected override void InternalFromXml(XElement xml, TmxMap tmxMap)
        {
            IEnumerable <PointF> source = from pt in xml.Element("polyline").Attribute("points").Value.Split(' ')
                                          let x                 = float.Parse(pt.Split(',')[0])
                                                          let y = float.Parse(pt.Split(',')[1])
                                                                  select new PointF(x, y);

            if (source.Count() == 2)
            {
                PointF pointF  = source.First();
                PointF pointF2 = source.Last();
                PointF item    = TmxMath.MidPoint(pointF, pointF2);
                Points = new List <PointF>
                {
                    pointF,
                    item,
                    pointF2
                };
            }
            else
            {
                Points = source.ToList();
            }
        }
Ejemplo n.º 31
0
 private void FixTileColliderObjects(TmxMap tmxMap)
 {
     for (int i = 0; i < ObjectGroup.Objects.Count; i++)
     {
         TmxObject tmxObject = ObjectGroup.Objects[i];
         if (tmxObject is TmxObjectRectangle)
         {
             TmxObjectPolygon value = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle);
             ObjectGroup.Objects[i] = value;
         }
     }
     foreach (TmxObject @object in ObjectGroup.Objects)
     {
         TmxHasPoints tmxHasPoints = @object as TmxHasPoints;
         if (tmxHasPoints != null)
         {
             PointF[] array = tmxHasPoints.Points.ToArray();
             TmxMath.RotatePoints(array, @object);
             array = array.Select(TmxMath.Sanitize).ToArray();
             tmxHasPoints.Points = array.ToList();
             @object.BakeRotation();
         }
     }
 }
Ejemplo n.º 32
0
 public TgxTemplateGroup(TmxMap map)
 {
     ParentMap = map;
     Tilesets  = new List <TsxTileset>();
     Templates = new List <TgxTemplate>();
 }
Ejemplo n.º 33
0
 public TmxGroupLayer(TmxLayerNode parent, TmxMap tmxMap)
     : base(parent, tmxMap)
 {
 }
Ejemplo n.º 34
0
 public static string GetExportedFilename(TmxMap tmxMap)
 {
     return String.Format("{0}.tiled2unity.xml", tmxMap.Name);
 }
Ejemplo n.º 35
0
 public Tiled2UnityViewer(TmxMap tmxMap)
 {
     this.tmxMap = tmxMap;
     InitializeComponent();
 }
Ejemplo n.º 36
0
 public TmxObjectGroup(TmxMap tmxMap) : base(tmxMap)
 {
     this.Objects = new List <TmxObject>();
 }
Ejemplo n.º 37
0
 void TmxMap_OnReadTmxFileCompleted(TmxMap tmxMap)
 {
     this.buttonFolderBrowser.Enabled = true;
     this.buttonViewer.Enabled        = true;
     CheckExportButton();
 }
Ejemplo n.º 38
0
 public TmxObjectGroup(TmxLayerNode parent, TmxMap tmxMap)
     : base(parent, tmxMap)
 {
     Objects = new List <TmxObject>();
 }
Ejemplo n.º 39
0
 public static string GetExportedFilename(TmxMap tmxMap)
 {
     return(String.Format("{0}.tiled2unity.xml", tmxMap.Name));
 }
Ejemplo n.º 40
0
 protected abstract void InternalFromXml(XElement xml, TmxMap tmxMap);
Ejemplo n.º 41
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);
        }
Ejemplo n.º 42
0
 protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
 {
     // No extra data for ellipses
 }
Ejemplo n.º 43
0
 public Tiled2UnityViewer(TmxMap tmxMap)
 {
     this.tmxMap = tmxMap;
     InitializeComponent();
 }
Ejemplo n.º 44
0
 public TmxLayer(TmxMap map)
 {
     this.TmxMap = map;
 }
Ejemplo n.º 45
0
        public static ClipperLib.PolyTree ExecuteClipper(TmxMap tmxMap, TmxLayer tmxLayer, TransformPointFunc xfFunc, ProgressFunc progFunc)
        {
            // The "fullClipper" combines the clipper results from the smaller pieces
            ClipperLib.Clipper fullClipper = new ClipperLib.Clipper();

            // From the perspective of Clipper lines are polygons too
            // Closed paths == polygons
            // Open paths == lines
            var polygonGroups = from y in Enumerable.Range(0, tmxLayer.Height)
                                from x in Enumerable.Range(0, tmxLayer.Width)
                                let rawTileId = tmxLayer.GetRawTileIdAt(x, y)
                                                let tileId = TmxMath.GetTileIdWithoutFlags(rawTileId)
                                                             where tileId != 0
                                                             let tile = tmxMap.Tiles[tileId]
                                                                        from polygon in tile.ObjectGroup.Objects
                                                                        where (polygon as TmxHasPoints) != null
                                                                        let groupX = x / LayerClipper.GroupBySize
                                                                                     let groupY = y / LayerClipper.GroupBySize
                                                                                                  group new
            {
                PositionOnMap         = tmxMap.GetMapPositionAt(x, y, tile),
                HasPointsInterface    = polygon as TmxHasPoints,
                TmxObjectInterface    = polygon,
                IsFlippedDiagnoally   = TmxMath.IsTileFlippedDiagonally(rawTileId),
                IsFlippedHorizontally = TmxMath.IsTileFlippedHorizontally(rawTileId),
                IsFlippedVertically   = TmxMath.IsTileFlippedVertically(rawTileId),
                TileCenter            = new PointF(tile.TileSize.Width * 0.5f, tile.TileSize.Height * 0.5f),
            }
            by Tuple.Create(groupX, groupY);

            int groupIndex = 0;
            int groupCount = polygonGroups.Count();

            foreach (var polyGroup in polygonGroups)
            {
                if (groupIndex % 5 == 0)
                {
                    progFunc(String.Format("Clipping '{0}' polygons: {1}%", tmxLayer.Name, (groupIndex / (float)groupCount) * 100));
                }
                groupIndex++;

                // The "groupClipper" clips the polygons in a smaller part of the world
                ClipperLib.Clipper groupClipper = new ClipperLib.Clipper();

                // Add all our polygons to the Clipper library so it can reduce all the polygons to a (hopefully small) number of paths
                foreach (var poly in polyGroup)
                {
                    // Create a clipper library polygon out of each and add it to our collection
                    ClipperPolygon clipperPolygon = new ClipperPolygon();

                    // Our points may be transformed due to tile flipping/rotation
                    // Before we transform them we put all the points into local space relative to the tile
                    SizeF    offset            = new SizeF(poly.TmxObjectInterface.Position);
                    PointF[] transformedPoints = poly.HasPointsInterface.Points.Select(pt => PointF.Add(pt, offset)).ToArray();

                    // Now transform the points relative to the tile
                    TmxMath.TransformPoints(transformedPoints, poly.TileCenter, poly.IsFlippedDiagnoally, poly.IsFlippedHorizontally, poly.IsFlippedVertically);

                    foreach (var pt in transformedPoints)
                    {
                        float x = poly.PositionOnMap.X + pt.X;
                        float y = poly.PositionOnMap.Y + pt.Y;

                        ClipperLib.IntPoint point = xfFunc(x, y);
                        clipperPolygon.Add(point);
                    }

                    // Because of Unity's cooridnate system, the winding order of the polygons must be reversed
                    clipperPolygon.Reverse();

                    // Add the "subject"
                    groupClipper.AddPath(clipperPolygon, ClipperLib.PolyType.ptSubject, poly.HasPointsInterface.ArePointsClosed());
                }

                // Get a solution for this group
                ClipperLib.PolyTree solution = new ClipperLib.PolyTree();
                groupClipper.Execute(ClipperLib.ClipType.ctUnion, solution, LayerClipper.SubjectFillRule, LayerClipper.ClipFillRule);

                // Combine the solutions into the full clipper
                fullClipper.AddPaths(ClipperLib.Clipper.ClosedPathsFromPolyTree(solution), ClipperLib.PolyType.ptSubject, true);
                fullClipper.AddPaths(ClipperLib.Clipper.OpenPathsFromPolyTree(solution), ClipperLib.PolyType.ptSubject, false);
            }
            progFunc(String.Format("Clipping '{0}' polygons: 100%", tmxLayer.Name));

            ClipperLib.PolyTree fullSolution = new ClipperLib.PolyTree();
            fullClipper.Execute(ClipperLib.ClipType.ctUnion, fullSolution, LayerClipper.SubjectFillRule, LayerClipper.ClipFillRule);

            return(fullSolution);
        }
Ejemplo n.º 46
0
        private void OpenTmxFile(string tmxPath)
        {
            this.warnings.Clear();
            this.errors.Clear();

            this.buttonFolderBrowser.Enabled = false;
            this.buttonViewer.Enabled = false;
            this.buttonExport.Enabled = false;

            try
            {
                this.tmxMap = TmxMap.LoadFromFile(tmxPath);
                this.tmxExporter = new TiledMapExporter(this.tmxMap);
                CheckExportButton();
                ReportSummary();
            }
            catch (TmxException tmx)
            {
                Program.WriteError(tmx.Message);
            }
        }
Ejemplo n.º 47
0
 private int GetMaxTilesWide(TmxMap tmxMap)
 {
     return(Math.Min(tmxMap.Width, MaxPreviewTilesWide));
 }
Ejemplo n.º 48
0
 void TmxMap_OnReadTmxFileCompleted(TmxMap tmxMap)
 {
     this.buttonFolderBrowser.Enabled = true;
     this.buttonViewer.Enabled = true;
     CheckExportButton();
     string exportDir = global::Tiled2Unity.Properties.Settings.Default.LastExportDirectory;
     if (Program.Cli && Directory.Exists(exportDir)) {
         this.tmxExporter = new TiledMapExporter(tmxMap);
         this.tmxExporter.Export(exportDir);
         Application.Exit ();
     }
 }
Ejemplo n.º 49
0
 public TmxLayerBase(TmxMap tmxMap)
 {
     this.TmxMap = tmxMap;
 }
Ejemplo n.º 50
0
 public TiledMapExporter(TmxMap tmxMap)
 {
     this.tmxMap = tmxMap;
 }
Ejemplo n.º 51
0
 private int GetMaxTilesHigh(TmxMap tmxMap)
 {
     return(Math.Min(tmxMap.Height, MaxPreviewTilesHigh));
 }
Ejemplo n.º 52
0
 protected override void InternalFromXml(System.Xml.Linq.XElement xml, TmxMap tmxMap)
 {
     // No extra data for ellipses
 }
Ejemplo n.º 53
0
 public TmxLayer(TmxLayerNode parent, TmxMap map) : base(parent, map)
 {
     this.Visible         = true;
     this.Opacity         = 1.0f;
     this.CollisionLayers = new List <TmxLayer>();
 }
Ejemplo n.º 54
0
 static public PointF ObjectPointFToMapSpace(TmxMap tmxMap, float x, float y)
 {
     return(ObjectPointFToMapSpace(tmxMap, new PointF(x, y)));
 }
Ejemplo n.º 55
0
        public static List <TmxLayerNode> ListFromXml(XElement xmlRoot, TmxLayerNode parent, TmxMap tmxMap)
        {
            List <TmxLayerNode> nodes = new List <TmxLayerNode>();

            foreach (var xmlNode in xmlRoot.Elements())
            {
                TmxLayerNode layerNode = null;

                if (xmlNode.Name == "layer" || xmlNode.Name == "imagelayer")
                {
                    layerNode = TmxLayer.FromXml(xmlNode, parent, tmxMap);
                }
                else if (xmlNode.Name == "objectgroup")
                {
                    layerNode = TmxObjectGroup.FromXml(xmlNode, parent, tmxMap);
                }
                else if (xmlNode.Name == "group")
                {
                    layerNode = TmxGroupLayer.FromXml(xmlNode, parent, tmxMap);
                }

                // If the layer is visible then add it to our list
                if (layerNode != null && layerNode.Visible)
                {
                    nodes.Add(layerNode);
                }
            }

            return(nodes);
        }
Ejemplo n.º 56
0
 void TmxMap_OnReadTmxFileCompleted(TmxMap tmxMap)
 {
     this.buttonFolderBrowser.Enabled = true;
     this.buttonViewer.Enabled = true;
     CheckExportButton();
 }
Ejemplo n.º 57
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);
        }
Ejemplo n.º 58
0
 public TiledMapExporter(TmxMap tmxMap)
 {
     this.tmxMap = tmxMap;
 }
Ejemplo n.º 59
0
 protected abstract void InternalFromXml(XElement xml, TmxMap tmxMap);
Ejemplo n.º 60
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);
        }