From 5a573c9fc12252999613fedb364acc59db9182a9 Mon Sep 17 00:00:00 2001 From: Pieter Geerkens Date: Fri, 5 Apr 2019 10:14:58 -0400 Subject: [PATCH] Tidied up MapDisplayPainter --- HexgridPanel/MapDisplayPainter.cs | 69 ++++++++++++++----------------- HexgridPanel/MapPanel.cs | 1 + 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/HexgridPanel/MapDisplayPainter.cs b/HexgridPanel/MapDisplayPainter.cs index d015ce6..0622086 100644 --- a/HexgridPanel/MapDisplayPainter.cs +++ b/HexgridPanel/MapDisplayPainter.cs @@ -27,15 +27,14 @@ ///////////////////////////////////////////////////////////////////////////////////////// #endregion using System; +using System.Drawing; +using System.Drawing.Drawing2D; using PGNapoleonics.HexUtilities; using PGNapoleonics.HexUtilities.Common; using PGNapoleonics.HexUtilities.Pathfinding; namespace PGNapoleonics.HexgridPanel { - using System.Drawing; - using System.Drawing.Drawing2D; - /// Extension methods to paint an from a . public static partial class MapDisplayPainter { /// Paint the base layer of the display, graphics that changes rarely between refreshes. @@ -47,11 +46,10 @@ public static void PaintMap(this IMapDisplayWinForms @this, Graphics bool showHexgrid) where THex:IHex => graphics.Contain( g => { - var boardHexes = @this.BoardHexes; - var landmarks = @this.Landmarks; - var clipHexes = @this.GetClipInHexes(graphics.VisibleClipBounds); + var boardHexes = @this.BoardHexes; + var landmarks = @this.Landmarks; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; - @this.PaintForEachHex(graphics, clipHexes, coords => { + @this.PaintForEachHex(graphics, coords => { boardHexes[coords].IfHasValueDo(h => { if(h is IHex hex) hex.Paint(graphics, @this.HexgridPath, hex.GetBrush()); }); @@ -77,7 +75,7 @@ public static void PaintHighlight(this IMapDisplayWinForms @this, Gr } ); if (@this.Path != null) { - graphics.Contain(g => { @this.PaintPath(g, @this.Path); }); + graphics.Contain(g => { @this.PaintPath(g); }); } if (@this.ShowRangeLine) { @@ -100,47 +98,59 @@ public static void PaintShading(this IMapDisplayWinForms @this, Grap var fov = @this?.Fov; if (fov != null) { graphics.CompositingMode = CompositingMode.SourceOver; - var clipHexes = @this.GetClipInHexes(graphics.VisibleClipBounds); using(var shadeBrush = new SolidBrush(Color.FromArgb(@this.ShadeBrushAlpha, ShadeColor))) { - @this.PaintForEachHex(graphics, clipHexes, coords => { + @this.PaintForEachHex(graphics, coords => { if ( ! fov[coords]) { graphics.FillPath(shadeBrush, @this.HexgridPath); } } ); } } } ); + /// . + /// + /// The map to be painted as a . + /// The object for the canvas being painted. + public static void PaintUnits(this IMapDisplayWinForms @this, Graphics graphics) + where THex:IHex { + if (@this == null) throw new ArgumentNullException("this"); + if (graphics == null) throw new ArgumentNullException("graphics"); + + /* NO-OP - Not implemented in examples. */ + } + /// Paints all the hexes in by executing /// for each hex on . /// The map to be painted as a . /// The object for the canvas being painted. /// The rectangular extent of hexes to be painted as a . /// The paint action to be performed for each hex as a . - public static void PaintForEachHex(this IMapDisplayWinForms @this, Graphics graphics, - CoordsRectangle clipRectangle, Action paintAction) - where THex:IHex - => @this.ForEachHex(clipRectangle, hex => { + private static void PaintForEachHex(this IMapDisplayWinForms @this, Graphics graphics, + Action paintAction) + where THex:IHex { + var clipRectangle = @this.GetClipInHexes(graphics.VisibleClipBounds); + @this.ForEachHex(clipRectangle, hex => { graphics.Transform = @this.TranslateToHex(hex.Coords); paintAction(hex.Coords); } ); + } /// Paint the current shortese path. /// The map to be painted as a . /// The object for the canvas being painted. /// Type: - /// A directed path (ie linked-list> of hexes to be painted. - public static void PaintPath(this IMapDisplayWinForms @this, Graphics graphics, - Maybe maybePath) + private static void PaintPath(this IMapDisplayWinForms @this, Graphics graphics) where THex:IHex { if (graphics==null) throw new ArgumentNullException("graphics"); - var path = maybePath.ElseDefault(); + var path = @this.Path.ElseDefault(); using(var brush = new SolidBrush(Color.FromArgb(78, Color.PaleGoldenrod))) { while (path != null) { var coords = path.PathStep.Coords; graphics.Transform = @this.TranslateToHex(coords); graphics.FillPath(brush, @this.HexgridPath); - if (@this.ShowPathArrow) @this.PaintPathArrow(graphics, path); + if (@this.ShowPathArrow) @this.PaintPathDetail(graphics, path); path = path.PathSoFar; } @@ -152,11 +162,8 @@ public static void PaintPath(this IMapDisplayWinForms @this, Graphic /// The object for the canvas being painted. /// Type: - /// A directed path (ie linked-list> of hexes to be highlighted with a direction arrow. - static void PaintPathArrow(this IMapDisplayWinForms @this, Graphics graphics, IDirectedPathCollection path) + static void PaintPathDetail(this IMapDisplayWinForms @this, Graphics graphics, IDirectedPathCollection path) where THex:IHex { - if (graphics==null) throw new ArgumentNullException("graphics"); - if (path==null) throw new ArgumentNullException("path"); - graphics.TranslateTransform(@this.HexCentreOffset.Width, @this.HexCentreOffset.Height); if (path.PathSoFar == null) @this.PaintPathDestination(graphics); else @this.PaintPathArrow(graphics, path.PathStep.HexsideExit); @@ -170,8 +177,6 @@ static void PaintPathArrow(this IMapDisplayWinForms @this, Graphics /// The current graphics origin must be the centre of the current hex. static void PaintPathArrow(this IMapDisplayWinForms @this, Graphics graphics, Hexside hexside) where THex:IHex { - if (graphics==null) throw new ArgumentNullException("graphics"); - var unit = @this.GridSize.Height/8.0F; graphics.RotateTransform(60 * hexside); graphics.DrawLine(Pens.Black, 0,unit*4, 0, -unit); @@ -185,31 +190,17 @@ static void PaintPathArrow(this IMapDisplayWinForms @this, Graphics /// The current graphics origin must be the centre of the current hex. static void PaintPathDestination(this IMapDisplayWinForms @this, Graphics graphics) where THex:IHex { - if (graphics==null) throw new ArgumentNullException("graphics"); - var unit = @this.GridSize.Height/8.0F; graphics.DrawLine(Pens.Black, -unit*2,-unit*2, unit*2, unit*2); graphics.DrawLine(Pens.Black, -unit*2, unit*2, unit*2,-unit*2); } - /// . - /// - /// The map to be painted as a . - /// The object for the canvas being painted. - public static void PaintUnits(this IMapDisplayWinForms @this, Graphics graphics) - where THex:IHex { - if (@this == null) throw new ArgumentNullException("this"); - if (graphics == null) throw new ArgumentNullException("graphics"); - - /* NO-OP - Not implemented in examples. */ - } - /// . /// /// /// Returns clones to avoid inter-thread contention. /// - public static Brush GetBrush(this IHex hex) { + private static Brush GetBrush(this IHex hex) { switch(hex.TerrainType) { default: return UndefinedBrush; case '.': return ClearBrush; diff --git a/HexgridPanel/MapPanel.cs b/HexgridPanel/MapPanel.cs index 5853ad9..85108f5 100644 --- a/HexgridPanel/MapPanel.cs +++ b/HexgridPanel/MapPanel.cs @@ -31,6 +31,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; + using PGNapoleonics.HexUtilities.Common; using PGNapoleonics.HexgridPanel.WinForms;