From 8ae2bba2bac0614ffe25c73afe2035213267f481 Mon Sep 17 00:00:00 2001 From: lucasstarsz Date: Sun, 8 May 2022 21:31:31 -0400 Subject: [PATCH] removed RenderStyle from builder constructor Breaking Changes: - Removed RenderStyle from Polygon2DBuilder constructors, as well as other Polygon2D static methods. Instead, it is configured from `Polygon2DBuilder#withRenderStyle(RenderStyle)`. --- .../tech/fastj/examples/behaviors/Main.java | 3 +- .../tech/fastj/examples/polygon2d/Main.java | 3 +- .../tech/fastj/graphics/game/Polygon2D.java | 49 +++++++------------ .../fastj/graphics/game/Polygon2DBuilder.java | 23 ++++++--- .../tech/fastj/resources/models/ObjUtil.java | 2 +- .../tech/fastj/resources/models/PsdfUtil.java | 3 +- .../graphics/game/Polygon2DTests.java | 6 ++- .../resources/models/ObjMtlUtilTests.java | 6 ++- .../resources/models/PsdfUtilTests.java | 6 ++- 9 files changed, 55 insertions(+), 46 deletions(-) diff --git a/examples/java/tech/fastj/examples/behaviors/Main.java b/examples/java/tech/fastj/examples/behaviors/Main.java index 7c28a65d..ed0b0e9c 100644 --- a/examples/java/tech/fastj/examples/behaviors/Main.java +++ b/examples/java/tech/fastj/examples/behaviors/Main.java @@ -59,7 +59,8 @@ public void init(FastJCanvas canvas) { * * For this example, we'll just use simpleRotation. */ - Polygon2D premadeBehaviorsBox = Polygon2D.create(DrawUtil.createBox(500f, 500f, 50f), RenderStyle.FillAndOutline) + Polygon2D premadeBehaviorsBox = Polygon2D.create(DrawUtil.createBox(500f, 500f, 50f)) + .withRenderStyle(RenderStyle.FillAndOutline) .withFill(Color.red) .withOutline(Polygon2D.DefaultOutlineStroke, Polygon2D.DefaultOutlineColor) .build(); diff --git a/examples/java/tech/fastj/examples/polygon2d/Main.java b/examples/java/tech/fastj/examples/polygon2d/Main.java index 101fc8cc..d4872a70 100644 --- a/examples/java/tech/fastj/examples/polygon2d/Main.java +++ b/examples/java/tech/fastj/examples/polygon2d/Main.java @@ -77,7 +77,8 @@ public void init(FastJCanvas canvas) { float largeSquareRotation = 30f; Pointf largeSquareScale = new Pointf(0.5f, 0.5f); - Polygon2D largeSquare = Polygon2D.create(largeSquareMesh, RenderStyle.FillAndOutline) + Polygon2D largeSquare = Polygon2D.create(largeSquareMesh) + .withRenderStyle(RenderStyle.FillAndOutline) .withFill(Color.blue) .withOutline(largeSquareOutlineStroke, Color.black) .withTransform(largeSquareTranslation, largeSquareRotation, largeSquareScale) diff --git a/src/main/java/tech/fastj/graphics/game/Polygon2D.java b/src/main/java/tech/fastj/graphics/game/Polygon2D.java index 7b31ea7a..709ab4e0 100644 --- a/src/main/java/tech/fastj/graphics/game/Polygon2D.java +++ b/src/main/java/tech/fastj/graphics/game/Polygon2D.java @@ -72,64 +72,51 @@ protected Polygon2D(Pointf[] points, Point[] altIndexes) { /** * Gets a {@link Polygon2DBuilder} instance while setting the eventual {@link Polygon2D}'s {@code points} field. - *

* * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. * @return A {@code Polygon2DBuilder} instance for creating a {@code Polygon2D}. */ public static Polygon2DBuilder create(Pointf[] points) { - return new Polygon2DBuilder(points, null, DefaultRenderStyle, Drawable.DefaultShouldRender); + return new Polygon2DBuilder(points, null, Drawable.DefaultShouldRender); } /** - * Gets a {@link Polygon2DBuilder} instance while setting the eventual {@link Polygon2D}'s {@code points} field. - *

+ * Gets a {@link Polygon2DBuilder} instance while setting the eventual {@link Polygon2D}'s {@code points} and + * {@code altIndexes} fields. * - * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. + * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. + * @param altIndexes The {@code Point} array of alternate indexes defining where curves are in the array of points, + * as well as other {@code Path2D} options. * @return A {@code Polygon2DBuilder} instance for creating a {@code Polygon2D}. */ public static Polygon2DBuilder create(Pointf[] points, Point[] altIndexes) { - return new Polygon2DBuilder(points, altIndexes, DefaultRenderStyle, Drawable.DefaultShouldRender); + return new Polygon2DBuilder(points, altIndexes, Drawable.DefaultShouldRender); } /** * Gets a {@link Polygon2DBuilder} instance while setting the eventual {@link Polygon2D}'s {@code points} and * {@code shouldRender} fields. - *

* * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. * @param shouldRender {@code boolean} that defines whether the {@code Polygon2D} would be rendered to the screen. * @return A {@code Polygon2DBuilder} instance for creating a {@code Polygon2D}. */ public static Polygon2DBuilder create(Pointf[] points, boolean shouldRender) { - return new Polygon2DBuilder(points, null, DefaultRenderStyle, shouldRender); - } - - /** - * Gets a {@link Polygon2DBuilder} instance while setting the eventual {@link Polygon2D}'s {@code points} and - * {@code renderStyle} fields. - *

- * - * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. - * @param renderStyle {@code RenderStyle} that defines the render style for the {@code Polygon2D}. - * @return A {@code Polygon2DBuilder} instance for creating a {@code Polygon2D}. - */ - public static Polygon2DBuilder create(Pointf[] points, RenderStyle renderStyle) { - return new Polygon2DBuilder(points, null, renderStyle, Drawable.DefaultShouldRender); + return new Polygon2DBuilder(points, null, shouldRender); } /** * Gets a {@link Polygon2DBuilder} instance while setting the eventual {@link Polygon2D}'s {@code points}, * {@code renderStyle}, and {@code shouldRender} fields. - *

* * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. - * @param renderStyle {@code RenderStyle} that defines the render style for the {@code Polygon2D}. + * @param altIndexes The {@code Point} array of alternate indexes defining where curves are in the array of + * points, as well as other {@code Path2D} options. * @param shouldRender {@code boolean} that defines whether the {@code Polygon2D} would be rendered to the screen. * @return A {@code Polygon2DBuilder} instance for creating a {@code Polygon2D}. */ - public static Polygon2DBuilder create(Pointf[] points, RenderStyle renderStyle, boolean shouldRender) { - return new Polygon2DBuilder(points, null, renderStyle, shouldRender); + public static Polygon2DBuilder create(Pointf[] points, Point[] altIndexes, boolean shouldRender) { + return new Polygon2DBuilder(points, altIndexes, shouldRender); } /** @@ -139,17 +126,19 @@ public static Polygon2DBuilder create(Pointf[] points, RenderStyle renderStyle, * @return The resulting {@code Polygon2D}. */ public static Polygon2D fromPoints(Pointf[] points) { - return new Polygon2DBuilder(points, null, DefaultRenderStyle, Drawable.DefaultShouldRender).build(); + return new Polygon2DBuilder(points, null, Drawable.DefaultShouldRender).build(); } /** - * Creates a {@code Polygon2D} from the specified points. + * Creates a {@code Polygon2D} from the specified points and alternate indexes. * - * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. + * @param points {@code Pointf} array that defines the points for the {@code Polygon2D}. + * @param altIndexes The {@code Point} array of alternate indexes defining where curves are in the array of points, + * as well as other {@code Path2D} options. * @return The resulting {@code Polygon2D}. */ public static Polygon2D fromPoints(Pointf[] points, Point[] altIndexes) { - return new Polygon2DBuilder(points, altIndexes, DefaultRenderStyle, Drawable.DefaultShouldRender).build(); + return new Polygon2DBuilder(points, altIndexes, Drawable.DefaultShouldRender).build(); } /** @@ -163,7 +152,7 @@ public Pointf[] getOriginalPoints() { /** * Gets the polygon's alternate indexes, which are associated with the - * {@link #getOriginalPoints() original point set.} + * {@link #getOriginalPoints() original point set}. * * @return The original set of points for this polygon, as a {@code Pointf[]}. */ diff --git a/src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java b/src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java index cb3929a8..0f466c9a 100644 --- a/src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java +++ b/src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java @@ -15,8 +15,8 @@ public class Polygon2DBuilder { private final Pointf[] points; private final Point[] altIndexes; private final boolean shouldRender; - private final RenderStyle renderStyle; + private RenderStyle renderStyle = Polygon2D.DefaultRenderStyle; private Paint fillPaint = Polygon2D.DefaultFill; private BasicStroke outlineStroke = Polygon2D.DefaultOutlineStroke; private Color outlineColor = Polygon2D.DefaultOutlineColor; @@ -26,20 +26,31 @@ public class Polygon2DBuilder { private Pointf scale = Transform2D.DefaultScale.copy(); /** - * {@code Polygon2DBuilder} constructor, taking in a set of points, a render style, and a {@code shouldRender} - * boolean. + * {@code Polygon2DBuilder} constructor, taking in a set of points, a set of alternate indexes (for defining + * curves), and a {@code shouldRender} boolean. * * @param points The {@code Pointf} array of mesh points to use for the resulting {@code Polygon2D}. - * @param renderStyle The {@code RenderStyle} to use for the resulting {@code Polygon2D}. + * @param altIndexes The {@code Point} array of alternate indexes defining where curves are in the array of + * points, as well as other {@code Path2D} options. * @param shouldRender The "should render" {@code boolean} to use for the resulting {@code Polygon2D}. */ - Polygon2DBuilder(Pointf[] points, Point[] altIndexes, RenderStyle renderStyle, boolean shouldRender) { + Polygon2DBuilder(Pointf[] points, Point[] altIndexes, boolean shouldRender) { this.points = Objects.requireNonNull(points, "The array of points must not be null."); this.altIndexes = altIndexes; - this.renderStyle = Objects.requireNonNull(renderStyle, "The render style must not be null."); this.shouldRender = shouldRender; } + /** + * Sets the builder's render style value. + * + * @param renderStyle The {@code RenderStyle} to be used in the resulting {@code Polygon2D}. + * @return The {@code Polygon2DBuilder}, for method chaining. + */ + public Polygon2DBuilder withRenderStyle(RenderStyle renderStyle) { + this.renderStyle = Objects.requireNonNull(renderStyle, "The render style must not be null."); + return this; + } + /** * Sets the builder's fill paint value. * diff --git a/src/main/java/tech/fastj/resources/models/ObjUtil.java b/src/main/java/tech/fastj/resources/models/ObjUtil.java index 2fabf97e..395b77de 100644 --- a/src/main/java/tech/fastj/resources/models/ObjUtil.java +++ b/src/main/java/tech/fastj/resources/models/ObjUtil.java @@ -69,7 +69,7 @@ public static Polygon2D[] parse(Path modelPath, List lines) { polygons.get(lastPolygonIndex).setRenderStyle(RenderStyle.FillAndOutline); MtlUtil.parse(polygons.get(lastPolygonIndex), materialLibraryPath, currentMaterial, false); } else { - Polygon2D polygonFromVertexes = Polygon2D.create(vertexesFromFaces, RenderStyle.Outline).build(); + Polygon2D polygonFromVertexes = Polygon2D.create(vertexesFromFaces).withRenderStyle(RenderStyle.Outline).build(); MtlUtil.parse(polygonFromVertexes, materialLibraryPath, currentMaterial, false); polygons.add(polygonFromVertexes); } diff --git a/src/main/java/tech/fastj/resources/models/PsdfUtil.java b/src/main/java/tech/fastj/resources/models/PsdfUtil.java index 879b246e..d1717e06 100644 --- a/src/main/java/tech/fastj/resources/models/PsdfUtil.java +++ b/src/main/java/tech/fastj/resources/models/PsdfUtil.java @@ -109,7 +109,8 @@ public static Polygon2D[] parse(Path modelPath, List lines) { if (tokens.length == 4 && tokens[3].equals(";")) { assert polygons != null; - polygons[polygonsIndex] = Polygon2D.create(polygonPoints.toArray(new Pointf[0]), renderStyle, shouldRender) + polygons[polygonsIndex] = Polygon2D.create(polygonPoints.toArray(new Pointf[0]), null, shouldRender) + .withRenderStyle(renderStyle) .withOutline(outlineStroke, outlineColor) .withTransform(translation, rotation, scale) .build(); diff --git a/src/test/java/unittest/testcases/graphics/game/Polygon2DTests.java b/src/test/java/unittest/testcases/graphics/game/Polygon2DTests.java index 6efd37c9..3fe15612 100644 --- a/src/test/java/unittest/testcases/graphics/game/Polygon2DTests.java +++ b/src/test/java/unittest/testcases/graphics/game/Polygon2DTests.java @@ -45,7 +45,8 @@ void checkPolygon2DCreation_withPointfArrayParam_andRandomlyGeneratedRenderStyle RenderStyle renderStyle = RenderStyle.values()[Maths.randomInteger(0, RenderStyle.values().length - 1)]; boolean shouldRender = Maths.randomBoolean(); - Polygon2D polygon2D = Polygon2D.create(square, renderStyle, shouldRender) + Polygon2D polygon2D = Polygon2D.create(square, shouldRender) + .withRenderStyle(renderStyle) .withFill(randomColor) .build(); @@ -75,8 +76,9 @@ void checkPolygon2DCreation_withPointfArrayParam_andRandomlyGeneratedRenderStyle float randomRotation = Maths.random(-5000f, 5000f); float expectedNormalizedRotation = randomRotation % 360; - Polygon2D polygon2D = Polygon2D.create(square, renderStyle, shouldRender) + Polygon2D polygon2D = Polygon2D.create(square, shouldRender) .withTransform(randomTranslation, randomRotation, randomScale) + .withRenderStyle(renderStyle) .withOutline(outlineStroke, outlineColor) .withFill(randomColor) .build(); diff --git a/src/test/java/unittest/testcases/resources/models/ObjMtlUtilTests.java b/src/test/java/unittest/testcases/resources/models/ObjMtlUtilTests.java index 732e51c8..8baf6dbe 100644 --- a/src/test/java/unittest/testcases/resources/models/ObjMtlUtilTests.java +++ b/src/test/java/unittest/testcases/resources/models/ObjMtlUtilTests.java @@ -60,7 +60,8 @@ class ObjMtlUtilTests { private static final RenderStyle expectedHouseWallsRenderStyle = RenderStyle.FillAndOutline; private static final BasicStroke expectedHouseWallsOutlineStroke = new BasicStroke(5.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 15f); private static final Color expectedHouseWallsOutlineColor = new Color(150, 150, 150, 150); - private static final Polygon2D expectedHouseWalls = Polygon2D.create(expectedHouseWallsMesh, expectedHouseWallsRenderStyle) + private static final Polygon2D expectedHouseWalls = Polygon2D.create(expectedHouseWallsMesh) + .withRenderStyle(expectedHouseWallsRenderStyle) .withOutline(expectedHouseWallsOutlineStroke, expectedHouseWallsOutlineColor) .build(); private static final RadialGradientPaint expectedHouseWallsGradient = Gradients.radialGradient(expectedHouseWalls) @@ -75,7 +76,8 @@ class ObjMtlUtilTests { }; private static final RenderStyle expectedHouseRoofRenderStyle = RenderStyle.Outline; private static final BasicStroke expectedHouseRoofOutlineStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.0f); - private static final Polygon2D expectedHouseRoof = Polygon2D.create(expectedHouseRoofMesh, expectedHouseRoofRenderStyle) + private static final Polygon2D expectedHouseRoof = Polygon2D.create(expectedHouseRoofMesh) + .withRenderStyle(expectedHouseRoofRenderStyle) .withOutline(expectedHouseRoofOutlineStroke, Polygon2D.DefaultOutlineColor) .build(); diff --git a/src/test/java/unittest/testcases/resources/models/PsdfUtilTests.java b/src/test/java/unittest/testcases/resources/models/PsdfUtilTests.java index ad30f47f..98cd1406 100644 --- a/src/test/java/unittest/testcases/resources/models/PsdfUtilTests.java +++ b/src/test/java/unittest/testcases/resources/models/PsdfUtilTests.java @@ -51,7 +51,8 @@ class PsdfUtilTests { private static final RenderStyle expectedHouseWallsRenderStyle = RenderStyle.FillAndOutline; private static final BasicStroke expectedHouseWallsOutlineStroke = new BasicStroke(5.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 15f); private static final Color expectedHouseWallsOutlineColor = new Color(150, 150, 150, 150); - private static final Polygon2D expectedHouseWalls = Polygon2D.create(expectedHouseWallsMesh, expectedHouseWallsRenderStyle) + private static final Polygon2D expectedHouseWalls = Polygon2D.create(expectedHouseWallsMesh) + .withRenderStyle(expectedHouseWallsRenderStyle) .withOutline(expectedHouseWallsOutlineStroke, expectedHouseWallsOutlineColor) .build(); private static final RadialGradientPaint expectedHouseWallsGradient = Gradients.radialGradient(expectedHouseWalls) @@ -66,7 +67,8 @@ class PsdfUtilTests { }; private static final RenderStyle expectedHouseRoofRenderStyle = RenderStyle.Outline; private static final BasicStroke expectedHouseRoofOutlineStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.0f); - private static final Polygon2D expectedHouseRoof = Polygon2D.create(expectedHouseRoofMesh, expectedHouseRoofRenderStyle) + private static final Polygon2D expectedHouseRoof = Polygon2D.create(expectedHouseRoofMesh) + .withRenderStyle(expectedHouseRoofRenderStyle) .withOutline(expectedHouseRoofOutlineStroke, Polygon2D.DefaultOutlineColor) .build(); private static final LinearGradientPaint expectedHouseRoofGradient = Gradients.linearGradient(expectedHouseRoof, Boundary.TopLeft, Boundary.BottomRight)