Skip to content

Commit

Permalink
Merge pull request #66 from phmatray/features/vogen
Browse files Browse the repository at this point in the history
use Vogen and Intellenum in domain project
  • Loading branch information
phmatray authored Feb 19, 2024
2 parents 55c6567 + 790b1bd commit 1bf2243
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public Task Execute(
includeEmptyAndFill, seed);

var imageConfiguration = new Settings(
cellWidthPixel,
new ThemeColor(backgroundColor),
new ThemeColor(foregroundColor));
CellWidthPixel.From(cellWidthPixel),
ThemeColor.From(backgroundColor),
ThemeColor.From(foregroundColor));

var imageDescription = imageDescriptionFactory
.NewImage(pattern, imageConfiguration);
Expand Down
15 changes: 4 additions & 11 deletions geometrix-api/Geometrix.Domain/Cells/CellsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@

namespace Geometrix.Domain.Cells;

public sealed class CellsCollection : List<Cell>
public sealed class CellsCollection(int cellGroupLength) : List<Cell>
{
private readonly int _cellGroupLength;

public CellsCollection(int cellGroupLength)
{
_cellGroupLength = cellGroupLength;
}

public void FillWithRandomCells(int seed, bool includeEmptyAndFill)
{
Random random = new(seed);

Clear();

for (var x = 0; x < _cellGroupLength; x++)
for (var x = 0; x < cellGroupLength; x++)
{
for (var y = 0; y < _cellGroupLength; y++)
for (var y = 0; y < cellGroupLength; y++)
{
var triangleDirection = TriangleDirection.CreateRandom(random, includeEmptyAndFill);
Cell cell = new(x, y, triangleDirection);
Expand All @@ -47,7 +40,7 @@ public void ExpandDown(int currentPower)

private Cell CreateMirrorCell(Cell original, bool isRight, int currentPower)
{
var mirrorFactor = _cellGroupLength * 2.Pow(currentPower) - 1;
var mirrorFactor = cellGroupLength * 2.Pow(currentPower) - 1;
var x = isRight ? -original.X + mirrorFactor : original.X;
var y = isRight ? original.Y : -original.Y + mirrorFactor;

Expand Down
5 changes: 5 additions & 0 deletions geometrix-api/Geometrix.Domain/Geometrix.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Intellenum" Version="1.0.0-beta.3" />
<PackageReference Include="Vogen" Version="3.0.24" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions geometrix-api/Geometrix.Domain/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Global using directives

global using Vogen;
19 changes: 7 additions & 12 deletions geometrix-api/Geometrix.Domain/ImageDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@

namespace Geometrix.Domain;

public class ImageDescription : IImageDescription
public class ImageDescription(Pattern pattern, Settings settings)
: IImageDescription
{
public ImageDescription(Pattern pattern, Settings settings)
{
Pattern = pattern;
Settings = settings;
}

public Pattern Pattern { get; } = pattern;
public Settings Settings { get; } = settings;

public string Id =>
$"{Pattern.Id}-{Settings.Id}";

public Pattern Pattern { get; }
public Settings Settings { get; }

public int WidthPixel =>
Pattern.HorizontalCell * Settings.CellWidthPixel;
Pattern.HorizontalCell * Settings.CellWidthPixel.Value;

public int HeightPixel =>
Pattern.VerticalCell * Settings.CellWidthPixel;
Pattern.VerticalCell * Settings.CellWidthPixel.Value;

public void Deconstruct(
out Pattern pattern,
Expand Down
4 changes: 4 additions & 0 deletions geometrix-api/Geometrix.Domain/ValueObjects/CellWidthPixel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Geometrix.Domain.ValueObjects;

[ValueObject<int>]
public readonly partial struct CellWidthPixel;
3 changes: 2 additions & 1 deletion geometrix-api/Geometrix.Domain/ValueObjects/PatternSeed.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
namespace Geometrix.Domain.ValueObjects;

public record PatternSeed(string Seed);
[ValueObject<string>]
public readonly partial struct PatternSeed;
2 changes: 1 addition & 1 deletion geometrix-api/Geometrix.Domain/ValueObjects/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Geometrix.Domain.ValueObjects;

public record Settings(
int CellWidthPixel,
CellWidthPixel CellWidthPixel,
ThemeColor BackgroundColor,
ThemeColor ForegroundColor)
{
Expand Down
90 changes: 11 additions & 79 deletions geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,13 @@
namespace Geometrix.Domain.ValueObjects;

public readonly struct ThemeColor(string value)
: IEquatable<ThemeColor>
{
public string Value { get; } = value;

public override bool Equals(object? obj)
=> obj is ThemeColor o && Equals(o);

public bool Equals(ThemeColor other)
=> Value == other.Value;

public override int GetHashCode()
=> HashCode.Combine(Value);

public static bool operator ==(ThemeColor left, ThemeColor right)
=> left.Equals(right);

public static bool operator !=(ThemeColor left, ThemeColor right)
=> !(left == right);

/// <summary>
/// Light.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Light = new("light");

/// <summary>
/// Dark.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Dark = new("dark");

/// <summary>
/// Red.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Red = new("red");

/// <summary>
/// Yellow.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Yellow = new("yellow");

/// <summary>
/// Green.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Green = new("green");

/// <summary>
/// Blue.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Blue = new("blue");

/// <summary>
/// Indigo.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Indigo = new("indigo");

/// <summary>
/// Purple.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Purple = new("purple");

/// <summary>
/// Pink.
/// </summary>
/// <returns>ThemeColor.</returns>
public static readonly ThemeColor Pink = new("pink");

public override string ToString()
{
return Value;
}
}
[ValueObject<string>]
[Instance("Light", "light")]
[Instance("Dark", "dark")]
[Instance("Red", "red")]
[Instance("Yellow", "yellow")]
[Instance("Green", "green")]
[Instance("Blue", "blue")]
[Instance("Indigo", "indigo")]
[Instance("Purple", "purple")]
[Instance("Pink", "pink")]
public readonly partial struct ThemeColor;
96 changes: 34 additions & 62 deletions geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,50 @@
namespace Geometrix.Domain.ValueObjects;

public readonly struct TriangleDirection(TriangleDirection.Direction value)
: IEquatable<TriangleDirection>
using Intellenum;

namespace Geometrix.Domain.ValueObjects;

[Intellenum]
[Member("None", 0)]
[Member("TopLeft", 1)]
[Member("TopRight", 2)]
[Member("BottomLeft", 3)]
[Member("BottomRight", 4)]
[Member("Filled", 5)]
public partial class TriangleDirection
{
public Direction Value { get; } = value;

public override bool Equals(object? obj)
=> obj is TriangleDirection other && Equals(other);

public bool Equals(TriangleDirection other)
=> Value == other.Value;

public override int GetHashCode()
=> (int) Value;

public static bool operator ==(TriangleDirection left, TriangleDirection right)
=> left.Equals(right);

public static bool operator !=(TriangleDirection left, TriangleDirection right)
=> !(left == right);

public static readonly TriangleDirection None = new(Direction.None);
public static readonly TriangleDirection TopLeft = new(Direction.TopLeft);
public static readonly TriangleDirection TopRight = new(Direction.TopRight);
public static readonly TriangleDirection BottomLeft = new(Direction.BottomLeft);
public static readonly TriangleDirection BottomRight = new(Direction.BottomRight);
public static readonly TriangleDirection Filled = new(Direction.Filled);

public static TriangleDirection MirrorRight(TriangleDirection direction)
=> direction.Value switch
{
return direction.Value switch
{
Direction.None => None,
Direction.TopLeft => TopRight,
Direction.TopRight => TopLeft,
Direction.BottomLeft => BottomRight,
Direction.BottomRight => BottomLeft,
Direction.Filled => Filled,
0 => None,
1 => TopRight,
2 => TopLeft,
3 => BottomRight,
4 => BottomLeft,
5 => Filled,
_ => throw new ArgumentOutOfRangeException(nameof(direction))
};
}

public static TriangleDirection MirrorDown(TriangleDirection direction)
=> direction.Value switch
{
return direction.Value switch
{
Direction.None => None,
Direction.TopLeft => BottomLeft,
Direction.TopRight => BottomRight,
Direction.BottomLeft => TopLeft,
Direction.BottomRight => TopRight,
Direction.Filled => Filled,
0 => None,
1 => BottomLeft,
2 => BottomRight,
3 => TopLeft,
4 => TopRight,
5 => Filled,
_ => throw new ArgumentOutOfRangeException(nameof(direction))
};
}

public static TriangleDirection CreateRandom(Random random, bool includeEmptyAndFill)
{
var direction = includeEmptyAndFill
? (Direction) random.Next(6)
: (Direction) (random.Next(4) + 1);

var triangleDirection = new TriangleDirection(direction);
return triangleDirection;
}
? random.Next(6)
: random.Next(4) + 1;

public override string ToString()
{
return $"{nameof(Value)}: {Value}";
}

public enum Direction
{
None = 0,
TopLeft = 1,
TopRight = 2,
BottomLeft = 3,
BottomRight = 4,
Filled = 5
return FromValue(direction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public sealed class TriangleService
/// <exception cref="ArgumentOutOfRangeException">direction</exception>
public Polygon? GetTriangle(TriangleDirection direction, int x, int y, int cellWidthPixel)
{
if (!Enum.IsDefined(typeof(TriangleDirection.Direction), direction.Value))
{
throw new ArgumentOutOfRangeException(nameof(direction), $"Invalid triangle direction: {direction}");
}

return direction.Value == TriangleDirection.Direction.None
return direction.Value == TriangleDirection.None.Value
? null
: _triangleFactory.CreateTriangle(direction, x, y, cellWidthPixel);
}
Expand All @@ -36,11 +31,11 @@ public Polygon CreateTriangle(TriangleDirection direction, int x, int y, int cel
{
return direction.Value switch
{
TriangleDirection.Direction.TopLeft => CreateTriangle(new[] { (x, y), (x + cellWidthPixel, y), (x, y + cellWidthPixel) }),
TriangleDirection.Direction.TopRight => CreateTriangle(new[] { (x + cellWidthPixel, y), (x + cellWidthPixel, y + cellWidthPixel), (x, y) }),
TriangleDirection.Direction.BottomLeft => CreateTriangle(new[] { (x, y + cellWidthPixel), (x, y), (x + cellWidthPixel, y + cellWidthPixel) }),
TriangleDirection.Direction.BottomRight => CreateTriangle(new[] { (x + cellWidthPixel, y + cellWidthPixel), (x, y + cellWidthPixel), (x + cellWidthPixel, y) }),
TriangleDirection.Direction.Filled => CreateRectangle(x, y, cellWidthPixel),
1 => CreateTriangle([(x, y), (x + cellWidthPixel, y), (x, y + cellWidthPixel)]),
2 => CreateTriangle([(x + cellWidthPixel, y), (x + cellWidthPixel, y + cellWidthPixel), (x, y)]),
3 => CreateTriangle([(x, y + cellWidthPixel), (x, y), (x + cellWidthPixel, y + cellWidthPixel)]),
4 => CreateTriangle([(x + cellWidthPixel, y + cellWidthPixel), (x, y + cellWidthPixel), (x + cellWidthPixel, y)]),
5 => CreateRectangle(x, y, cellWidthPixel),
_ => throw new ArgumentException("Invalid triangle direction", nameof(direction))
};
}
Expand Down
1 change: 1 addition & 0 deletions geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Content Include="..\..\README.md">
<Link>README.md</Link>
</Content>
<Content Remove="wwwroot\Images\4-42-T-2-2-dark-indigo-64 (1).png" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion geometrix-api/Geometrix.WebApi/ViewModels/ImageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ImageModel(ImageDescription imageDescription)
CellGroupLength = imageDescription.Pattern.CellGroupLength;
HorizontalCell = imageDescription.Pattern.HorizontalCell;
VerticalCell = imageDescription.Pattern.VerticalCell;
CellWidthPixel = imageDescription.Settings.CellWidthPixel;
CellWidthPixel = imageDescription.Settings.CellWidthPixel.Value;
BackgroundColor = imageDescription.Settings.BackgroundColor.Value;
ForegroundColor = imageDescription.Settings.ForegroundColor.Value;
}
Expand Down
4 changes: 2 additions & 2 deletions geometrix-api/global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"version": "8.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
"rollForward": "feature",
"allowPrerelease": false
}
}

0 comments on commit 1bf2243

Please sign in to comment.