Skip to content

Commit

Permalink
Add type PencilMarkViewNode (Closes #367).
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnieShine committed Nov 30, 2022
1 parent 7baaeaa commit 3581e56
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/Sudoku.Gdip/Gdip/DrawingConfigurations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public sealed class DrawingConfigurations
/// </summary>
public Color WheelTextColor { get; set; } = Color.Black;

/// <summary>
/// Indicates pencilmark text color.
/// </summary>
public Color PencilmarkTextColor { get; set; } = Color.DimGray;

/// <summary>
/// The color palette. This property stores a list of customized colors to be used as user-defined colors.
/// </summary>
Expand Down Expand Up @@ -256,4 +261,9 @@ public sealed class DrawingConfigurations
/// Indicates the font of wheel text.
/// </summary>
public FontData WheelFont { get; set; } = new("MiSans", 12F, FontStyle.Regular);

/// <summary>
/// Indicates the font of pencilmarks.
/// </summary>
public FontData PencilmarkFont { get; set; } = new("Segoe UI", 12F, FontStyle.Regular);
}
18 changes: 18 additions & 0 deletions src/Sudoku.Gdip/Gdip/FontData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ public bool Equals([NotNullWhen(true)] FontData? other)

[GeneratedOverriddingMember(GeneratedGetHashCodeBehavior.CallingHashCodeCombine, nameof(FontName), nameof(FontSize), nameof(FontStyle))]
public override partial int GetHashCode();


/// <summary>
/// Creates a <see cref="Font"/> instance via the current <see cref="FontData"/> instance.
/// </summary>
/// <returns>
/// The <see cref="Font"/> instance created. Please note that the created result should use <see langword="using"/> statement
/// to limit the lifestyle:
/// <code><![CDATA[
/// using var font = data.CreateFont();
/// ]]></code>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Font CreateFont()
{
var (fontName, fontSize, fontStyle) = this;
return new(fontName, fontSize, fontStyle);
}
}
47 changes: 42 additions & 5 deletions src/Sudoku.Gdip/Gdip/IGridImageGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ private static Font GetFont(string? fontName, float size, decimal scale, FontSty
partial void DrawClockfaceDot(Graphics g);
partial void DrawNeighborSigns(Graphics g);
partial void DrawWheel(Graphics g);
partial void DrawPencilmarks(Graphics g);
}

partial class GridImageGenerator
Expand Down Expand Up @@ -359,7 +360,6 @@ partial void DrawView(Graphics g)
DrawLinks(g);
DrawUnknownValue(g);

#if ENHANCED_DRAWING_APIS
// Shapes
DrawBorderBar(g);
DrawKropkiDot(g);
Expand All @@ -371,7 +371,7 @@ partial void DrawView(Graphics g)
DrawClockfaceDot(g);
DrawNeighborSigns(g);
DrawWheel(g);
#endif
DrawPencilmarks(g);
}

/// <summary>
Expand Down Expand Up @@ -1316,7 +1316,7 @@ partial void DrawWheel(Graphics g)
using var font = new Font(fontName, fontSize, fontStyle);
using var textBrush = new SolidBrush(textColor);

var positions = (stackalloc PointF[4]);
scoped var positions = (stackalloc PointF[4]);
foreach (var wheelNode in wheelNodes)
{
if (wheelNode is not (var cell, _) { Identifier: var identifier, DigitString: var digitString })
Expand All @@ -1335,9 +1335,9 @@ partial void DrawWheel(Graphics g)
g.DrawEllipse(pen, rect);

// Draw strings.
positions[0] = new(x, y - ch * SqrtOf2 / 2/* - fontSize / 4*/); // fontSize / 4: a little offset correction.
positions[0] = new(x, y - ch * SqrtOf2 / 2);
positions[1] = new(x + ch * SqrtOf2 / 2, y);
positions[2] = new(x, y + ch * SqrtOf2 / 2/* + fontSize / 4*/);
positions[2] = new(x, y + ch * SqrtOf2 / 2);
positions[3] = new(x - ch * SqrtOf2 / 2, y);
for (var i = 0; i < 4; i++)
{
Expand All @@ -1352,4 +1352,41 @@ partial void DrawWheel(Graphics g)
}
}
}

/// <summary>
/// Draw pencilmarks.
/// </summary>
/// <param name="g"><inheritdoc cref="RenderTo(Graphics)" path="/param[@name='g']"/></param>
[Conditional("ENHANCED_DRAWING_APIS")]
partial void DrawPencilmarks(Graphics g)
{
if (this is not
{
View.PencilMarkNodes: var pencilmarkNodes,
Calculator: { CellSize: var (_, ch) } calc,
Preferences: { PencilmarkFont: var fontData, PencilmarkTextColor: var textColor }
})
{
return;
}

using var font = fontData.CreateFont();
using var textBrush = new SolidBrush(textColor);

foreach (var pencilmarkNode in pencilmarkNodes)
{
if (pencilmarkNode is not (var cell, _) { Notation: var notation })
{
continue;
}

var renderingSize = g.MeasureString(notation, font);
var (_, th) = renderingSize;
var (centerX, centerY) = calc.GetMousePointInCenter(cell);
var position = new PointF(centerX, centerY - ch / 2 + th / 2);

// Draw text.
g.DrawString(notation, font, textBrush, position, DefaultStringFormat);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Sudoku.Presentation.Nodes.Shapes;

/// <summary>
/// Defines a pencil-mark view node.
/// </summary>
public sealed partial class PencilMarkViewNode : SingleCellMarkViewNode
{
/// <summary>
/// Initializes a <see cref="PencilMarkViewNode"/> instance via the specified values.
/// </summary>
/// <param name="cell">The cell.</param>
/// <param name="notation">The notation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public PencilMarkViewNode(int cell, string notation) : base(DisplayColorKind.Normal, cell, Direction.None) => Notation = notation;


/// <summary>
/// Indicates the notation.
/// </summary>
public string Notation { get; }


/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals([NotNullWhen(true)] ViewNode? other)
=> other is PencilMarkViewNode comparer
&& Identifier == comparer.Identifier && Cell == comparer.Cell && Notation == comparer.Notation;

[GeneratedOverriddingMember(GeneratedGetHashCodeBehavior.CallingHashCodeCombine, nameof(Identifier), nameof(Cell), nameof(Notation))]
public override partial int GetHashCode();

[GeneratedOverriddingMember(GeneratedToStringBehavior.RecordLike, nameof(Identifier), nameof(Cell), nameof(Notation))]
public override partial string ToString();

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override PencilMarkViewNode Clone() => new(Cell, Notation);
}
5 changes: 5 additions & 0 deletions src/Sudoku.Presentation/Presentation/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ private View()
/// </summary>
public IEnumerable<WheelViewNode> WheelNodes => _nodes.OfType<WheelViewNode>();

/// <summary>
/// Indicates pencilmark nodes that the current data type stores.
/// </summary>
public IEnumerable<PencilMarkViewNode> PencilMarkNodes => _nodes.OfType<PencilMarkViewNode>();


/// <summary>
/// Indicates the empty instance.
Expand Down
34 changes: 34 additions & 0 deletions src/docxml/Sudoku.Presentation.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3581e56

Please sign in to comment.