Skip to content

Commit

Permalink
Tidied up MapDisplayPainter
Browse files Browse the repository at this point in the history
  • Loading branch information
pgeerkens committed Apr 5, 2019
1 parent 0ee6ae9 commit 5a573c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
69 changes: 30 additions & 39 deletions HexgridPanel/MapDisplayPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>Extension methods to paint an <see cref="IMapDisplayWinForms{T}"/> from a <see cref="Graphics"/>.</summary>
public static partial class MapDisplayPainter {
/// <summary>Paint the base layer of the display, graphics that changes rarely between refreshes.</summary>
Expand All @@ -47,11 +46,10 @@ public static void PaintMap<THex>(this IMapDisplayWinForms<THex> @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());
});
Expand All @@ -77,7 +75,7 @@ public static void PaintHighlight<THex>(this IMapDisplayWinForms<THex> @this, Gr
} );

if (@this.Path != null) {
graphics.Contain(g => { @this.PaintPath(g, @this.Path); });
graphics.Contain(g => { @this.PaintPath(g); });
}

if (@this.ShowRangeLine) {
Expand All @@ -100,47 +98,59 @@ public static void PaintShading<THex>(this IMapDisplayWinForms<THex> @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); }
} );
}
}
} );

/// <summary>.</summary>
/// <typeparam name="THex"></typeparam>
/// <param name="this">The map to be painted as a <see cref="MapDisplay{THex}"/>.</param>
/// <param name="graphics">The <see cref="Graphics"/> object for the canvas being painted.</param>
public static void PaintUnits<THex>(this IMapDisplayWinForms<THex> @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. */
}

/// <summary>Paints all the hexes in <paramref name="clipHexes"/> by executing <paramref name="paintAction"/>
/// for each hex on <paramref name="graphics"/>.</summary>
/// <param name="this">The map to be painted as a <see cref="MapDisplay{THex}"/>.</param>
/// <param name="graphics">The <see cref="Graphics"/> object for the canvas being painted.</param>
/// <param name="clipRectangle">The rectangular extent of hexes to be painted as a <see cref="CoordsRectangle"/>.</param>
/// <param name="paintAction">The paint action to be performed for each hex as a <see cref="Action{HexCoords}"/>.</param>
public static void PaintForEachHex<THex>(this IMapDisplayWinForms<THex> @this, Graphics graphics,
CoordsRectangle clipRectangle, Action<HexCoords> paintAction)
where THex:IHex
=> @this.ForEachHex(clipRectangle, hex => {
private static void PaintForEachHex<THex>(this IMapDisplayWinForms<THex> @this, Graphics graphics,
Action<HexCoords> paintAction)
where THex:IHex {
var clipRectangle = @this.GetClipInHexes(graphics.VisibleClipBounds);
@this.ForEachHex(clipRectangle, hex => {
graphics.Transform = @this.TranslateToHex(hex.Coords);
paintAction(hex.Coords);
} );
}

/// <summary>Paint the current shortese path.</summary>
/// <param name="this">The map to be painted as a <see cref="MapDisplay{THex}"/>.</param>
/// <param name="graphics">The <see cref="Graphics"/> object for the canvas being painted.</param>
/// <param name="maybePath">Type: <see cref="IDirectedPathCollection"/> -
/// A directed path (ie linked-list> of hexes to be painted.</param>
public static void PaintPath<THex>(this IMapDisplayWinForms<THex> @this, Graphics graphics,
Maybe<IDirectedPathCollection> maybePath)
private static void PaintPath<THex>(this IMapDisplayWinForms<THex> @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;
}
Expand All @@ -152,11 +162,8 @@ public static void PaintPath<THex>(this IMapDisplayWinForms<THex> @this, Graphic
/// <param name="graphics">The <see cref="Graphics"/> object for the canvas being painted.</param>
/// <param name="path">Type: <see cref="IDirectedPathCollection"/> -
/// A directed path (ie linked-list> of hexes to be highlighted with a direction arrow.</param>
static void PaintPathArrow<THex>(this IMapDisplayWinForms<THex> @this, Graphics graphics, IDirectedPathCollection path)
static void PaintPathDetail<THex>(this IMapDisplayWinForms<THex> @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);
Expand All @@ -170,8 +177,6 @@ static void PaintPathArrow<THex>(this IMapDisplayWinForms<THex> @this, Graphics
/// <remarks>The current graphics origin must be the centre of the current hex.</remarks>
static void PaintPathArrow<THex>(this IMapDisplayWinForms<THex> @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);
Expand All @@ -185,31 +190,17 @@ static void PaintPathArrow<THex>(this IMapDisplayWinForms<THex> @this, Graphics
/// <remarks>The current graphics origin must be the centre of the current hex.</remarks>
static void PaintPathDestination<THex>(this IMapDisplayWinForms<THex> @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);
}

/// <summary>.</summary>
/// <typeparam name="THex"></typeparam>
/// <param name="this">The map to be painted as a <see cref="MapDisplay{THex}"/>.</param>
/// <param name="graphics">The <see cref="Graphics"/> object for the canvas being painted.</param>
public static void PaintUnits<THex>(this IMapDisplayWinForms<THex> @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. */
}

/// <summary>.</summary>
/// <param name="hex"></param>
/// <remarks>
/// Returns clones to avoid inter-thread contention.
/// </remarks>
public static Brush GetBrush(this IHex hex) {
private static Brush GetBrush(this IHex hex) {
switch(hex.TerrainType) {
default: return UndefinedBrush;
case '.': return ClearBrush;
Expand Down
1 change: 1 addition & 0 deletions HexgridPanel/MapPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using PGNapoleonics.HexUtilities.Common;
using PGNapoleonics.HexgridPanel.WinForms;

Expand Down

0 comments on commit 5a573c9

Please sign in to comment.