private static void DrawTilesInObjectGroup(Graphics g, TmxMap tmxMap, TmxObjectGroup objectGroup) { // Get opacity in eventually #if !TILED2UNITY_MAC ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix33 = objectGroup.Opacity; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); #endif foreach (var tmxObject in objectGroup.Objects) { if (!tmxObject.Visible) { continue; } TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; if (tmxObjectTile == null) { continue; } GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); { GraphicsState tileState = g.Save(); PrepareTransformForTileObject(g, tmxMap, tmxObjectTile); // Draw the tile Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height); Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize); //g.DrawRectangle(Pens.Black, destination); #if !TILED2UNITY_MAC g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source.X, source.Y, source.Width, source.Height, GraphicsUnit.Pixel, imageAttributes); #else g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source.X, source.Y, source.Width, source.Height, GraphicsUnit.Pixel); #endif g.Restore(tileState); } g.Restore(state); } }
private static void DrawTilesInObjectGroup(SKCanvas canvas, TmxMap tmxMap, TmxObjectGroup objectGroup) { using (SKPaint paint = new SKPaint()) { // Draw with the given opacity paint.Color = SKColors.White.WithAlpha((byte)(objectGroup.Opacity * byte.MaxValue)); foreach (var tmxObject in objectGroup.Objects) { if (!tmxObject.Visible) { continue; } TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; if (tmxObjectTile == null) { continue; } using (new SKAutoCanvasRestore(canvas)) { PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); canvas.Translate(xfPosition.X, xfPosition.Y); canvas.RotateDegrees(tmxObject.Rotation); using (new SKAutoCanvasRestore(canvas)) { PrepareTransformForTileObject(canvas, tmxMap, tmxObjectTile); // Draw the tile SKRect destination = new RectangleF(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height).ToSKRect(); SKRect source = new RectangleF(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize).ToSKRect(); canvas.DrawBitmap(tmxObjectTile.Tile.TmxImage.ImageBitmap, source, destination, paint); } } } } }
private static void PrepareTransformForTileObject(Graphics g, TmxMap tmxMap, TmxObjectTile tmxObjectTile) { // Isometric tiles are off by a half-width if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { g.TranslateTransform(-tmxObjectTile.Tile.TileSize.Width * 0.5f, 0); } // Apply scale { // Move to "local origin" of tile and scale float toCenter_x = (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) ? (tmxObjectTile.Tile.TileSize.Width * 0.5f) : 0.0f; g.TranslateTransform(toCenter_x, 0); { SizeF scale = tmxObjectTile.GetTileObjectScale(); g.ScaleTransform(scale.Width, scale.Height); } // Move back g.TranslateTransform(-toCenter_x, 0); } // Apply horizontal flip if (tmxObjectTile.FlippedHorizontal) { g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0); g.ScaleTransform(-1, 1); } // Apply vertical flip if (tmxObjectTile.FlippedVertical) { g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); g.ScaleTransform(1, -1); } }
private static void DrawObjectCollider(Graphics g, TmxMap tmxMap, TmxObject tmxObject, Color color) { using (Brush brush = TmxHelper.CreateObjectColliderBrush(color)) using (Pen pen = new Pen(color)) { GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxMap, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(g, pen, brush, tmxMap, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxMap, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxMap, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxMap, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { GraphicsState tileState = g.Save(); TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; PrepareTransformForTileObject(g, tmxMap, tmxObjectTile); // Draw the collisions // Temporarily set orienation to Orthogonal for tile colliders TmxMap.MapOrientation restoreOrientation = tmxMap.Orientation; tmxMap.Orientation = TmxMap.MapOrientation.Orthogonal; { // Make up for the fact that the bottom-left corner is the origin g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects) { DrawObjectCollider(g, tmxMap, obj, Color.Gray); } } tmxMap.Orientation = restoreOrientation; g.Restore(tileState); } else { g.Restore(state); Logger.WriteWarning("Unhandled object: {0}", tmxObject.GetNonEmptyName()); } // Restore our state g.Restore(state); } }
private static void DrawObjectCollider(Graphics g, TmxMap tmxMap, TmxObject tmxObject, Color color) { using (Brush brush = TmxHelper.CreateObjectColliderBrush(color)) using (Pen pen = new Pen(color)) { GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxMap, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(g, pen, brush, tmxMap, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxMap, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxMap, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxMap, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { GraphicsState tileState = g.Save(); TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; // Isometric tiles are off by a half-width if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { g.TranslateTransform(-tmxMap.TileWidth * 0.5f, 0); } // Apply scale SizeF scale = tmxObjectTile.GetTileObjectScale(); g.ScaleTransform(scale.Width, scale.Height); // Apply horizontal flip if (tmxObjectTile.FlippedHorizontal) { g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0); g.ScaleTransform(-1, 1); } // Apply vertical flip if (tmxObjectTile.FlippedVertical) { g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); g.ScaleTransform(1, -1); } // (Note: Now we can draw the tile and collisions as normal as the transforms have been set up.) // Draw the tile Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height); Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize); g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source, GraphicsUnit.Pixel); // Put a black border around the tile so it sticks out a bit as an object g.DrawRectangle(Pens.Black, destination); // Draw the collisions // Temporarily set orienation to Orthogonal for tile colliders TmxMap.MapOrientation restoreOrientation = tmxMap.Orientation; tmxMap.Orientation = TmxMap.MapOrientation.Orthogonal; { // Make up for the fact that the bottom-left corner is the origin g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects) { DrawObjectCollider(g, tmxMap, obj, Color.Gray); } } tmxMap.Orientation = restoreOrientation; g.Restore(tileState); } else { g.Restore(state); Logger.WriteWarning("Unhandled object: {0}", tmxObject.GetNonEmptyName()); } // Restore our state g.Restore(state); } }
private static void DrawObjectCollider(SKCanvas canvas, TmxMap tmxMap, TmxObject tmxObject, SKColor color) { using (new SKAutoCanvasRestore(canvas)) using (SKPaint paint = new SKPaint()) { PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); canvas.Translate(xfPosition.ToSKPoint()); canvas.RotateDegrees(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(canvas, color, tmxMap, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(canvas, color, tmxMap, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(canvas, color, tmxMap, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(canvas, color, tmxMap, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(canvas, color, tmxMap, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { using (new SKAutoCanvasRestore(canvas)) { TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; PrepareTransformForTileObject(canvas, tmxMap, tmxObjectTile); // Draw the collisions // Temporarily set orienation to Orthogonal for tile colliders TmxMap.MapOrientation restoreOrientation = tmxMap.Orientation; tmxMap.Orientation = TmxMap.MapOrientation.Orthogonal; { // Make up for the fact that the bottom-left corner is the origin canvas.Translate(0, -tmxObjectTile.Tile.TileSize.Height); foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects) { TmxObjectType type = tmxMap.ObjectTypes.GetValueOrDefault(obj.Type); DrawObjectCollider(canvas, tmxMap, obj, type.Color.ToSKColor()); } } tmxMap.Orientation = restoreOrientation; } } else { Logger.WriteWarning("Unhandled object: {0}", tmxObject.GetNonEmptyName()); } } }