Skip to content

Commit

Permalink
removed RenderStyle from builder constructor
Browse files Browse the repository at this point in the history
Breaking Changes:
- Removed RenderStyle from Polygon2DBuilder constructors, as well as other Polygon2D static methods. Instead, it is configured from `Polygon2DBuilder#withRenderStyle(RenderStyle)`.
  • Loading branch information
lucasstarsz committed May 9, 2022
1 parent db10125 commit 8ae2bba
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 46 deletions.
3 changes: 2 additions & 1 deletion examples/java/tech/fastj/examples/behaviors/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion examples/java/tech/fastj/examples/polygon2d/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 19 additions & 30 deletions src/main/java/tech/fastj/graphics/game/Polygon2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
*
* @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.
* <p>
* 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.
* <p>
*
* @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.
* <p>
*
* @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.
* <p>
*
* @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);
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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[]}.
*/
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tech/fastj/resources/models/ObjUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static Polygon2D[] parse(Path modelPath, List<String> 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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/tech/fastj/resources/models/PsdfUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public static Polygon2D[] parse(Path modelPath, List<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8ae2bba

Please sign in to comment.