/// <summary> /// Gets the tile at the location given. /// </summary> /// <param name="location">The location.</param> /// <returns></returns> private TileBase GetTileOver(Vector2 location) { Dictionary <Tile, IGraphicsPath> tilePaths = new Dictionary <Tile, IGraphicsPath>(); try { Matrix3x2 inverseViewMatrix = Controller.View.InverseViewMatrix; IEnumerable <TileBase> tiles = Controller.Tiles; foreach (TileBase tile in tiles) { IGraphicsPath path; bool disposePath = false; TileInstance tileInstance; Tile rawTile = tile as Tile; if (rawTile != null) { path = new GDIGraphics.GraphicsPath(); rawTile.PopulateGraphicsPath(path); tilePaths.Add(rawTile, path); } else if ((tileInstance = tile as TileInstance) != null) { if (!tilePaths.TryGetValue(tileInstance.Tile, out path)) { path = new GDIGraphics.GraphicsPath(); tileInstance.Tile.PopulateGraphicsPath(path); tilePaths.Add(tileInstance.Tile, path); } Debug.Assert(path != null, "path != null"); } else { disposePath = true; path = new GDIGraphics.GraphicsPath(); tile.PopulateGraphicsPath(path); } using (disposePath ? path : null) { Matrix3x2 tranform; if (Matrix3x2.Invert(tile.Transform, out tranform) && path.ContainsPoint(location, inverseViewMatrix * tranform)) { return(tile); } } } } finally { foreach (IGraphicsPath path in tilePaths.Values) { Debug.Assert(path != null, "path != null"); path.Dispose(); } } Debug.Fail("There should be a tile"); return(null); }
/// <summary> /// Draws a tiling. /// </summary> /// <param name="tiles">The tiles to draw.</param> /// <param name="graphics">The graphics to draw to.</param> /// <param name="lineStyle">The line style.</param> /// <param name="fillStyle">If not null, overwrites the <see cref="TileBase.Style" /> of the tiles.</param> /// <exception cref="ArgumentNullException"> /// </exception> public static void DrawTiling( [NotNull] IEnumerable <TileBase> tiles, [NotNull] IGraphics graphics, [NotNull] LineStyle lineStyle, [CanBeNull] IStyle fillStyle = null) { if (tiles == null) { throw new ArgumentNullException(nameof(tiles)); } if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } if (lineStyle == null) { throw new ArgumentNullException(nameof(lineStyle)); } Matrix3x2 initialTransform = graphics.Transform; graphics.SetLineStyle(lineStyle); if (fillStyle != null) { graphics.FillStyle = fillStyle; } Dictionary <Tile, IGraphicsPath> tilePaths = new Dictionary <Tile, IGraphicsPath>(); try { foreach (TileBase tile in tiles) { if (tile == null) { throw new ArgumentNullException(); } if (fillStyle == null) { graphics.FillStyle = tile.Style ?? SolidColourStyle.Transparent; } IGraphicsPath path; bool disposePath = false; TileInstance tileInstance; Tile rawTile = tile as Tile; if (rawTile != null) { path = graphics.CreatePath(); rawTile.PopulateGraphicsPath(path); tilePaths.Add(rawTile, path); graphics.Transform = initialTransform; } else if ((tileInstance = tile as TileInstance) != null) { if (!tilePaths.TryGetValue(tileInstance.Tile, out path)) { path = graphics.CreatePath(); tileInstance.Tile.PopulateGraphicsPath(path); tilePaths.Add(tileInstance.Tile, path); } Debug.Assert(path != null, "path != null"); graphics.Transform = tile.Transform * initialTransform; } else { disposePath = true; path = graphics.CreatePath(); tile.PopulateGraphicsPath(path); graphics.Transform = initialTransform; } using (disposePath ? path : null) { graphics.FillPath(path); graphics.DrawPath(path); } } } finally { graphics.Transform = initialTransform; foreach (IGraphicsPath path in tilePaths.Values) { Debug.Assert(path != null, "path != null"); path.Dispose(); } } }