Skip to content

Commit

Permalink
(#10, #32) update unit tests, bug fixes
Browse files Browse the repository at this point in the history
Bug Fixes:
- Add `break` statements to `PsdfUtil#writeRenderStyle` switch statement to prevent incorrect render style writing
- Fix issue with trying to parse floats in `PsdfUtil#parseOutlineStroke`
- Remove values stream to prevent stream re-usage in `SupportedFileFormats`
- Fix issue with only setting part of the render style content in `Polygon2DBuilder#build`

New Additions:
- Add method to create a random outline stroke
  • Loading branch information
lucasstarsz committed Jul 3, 2021
1 parent 383ed36 commit 23f47eb
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 178 deletions.
20 changes: 4 additions & 16 deletions src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,12 @@ public Polygon2DBuilder withTransform(Pointf translation, float rotation, Pointf
}

public Polygon2D build() {
Polygon2D polygon2D = (Polygon2D) new Polygon2D(points)
return (Polygon2D) new Polygon2D(points)
.setOutlineStroke(outlineStroke)
.setOutlineColor(outlineColor)
.setRenderStyle(renderStyle)
.setFill(fillPaint)
.setShouldRender(shouldRender)
.setTransform(translation, rotation, scale);

switch (renderStyle) {
case Fill: {
return polygon2D.setFill(fillPaint);
}
case Outline: {
return polygon2D.setOutline(outlineStroke, outlineColor);
}
case FillAndOutline: {
return polygon2D.setFill(fillPaint).setOutline(outlineStroke, outlineColor);
}
default: {
throw new IllegalStateException("Invalid render style: " + renderStyle.name());
}
}
}
}
39 changes: 30 additions & 9 deletions src/main/java/tech/fastj/graphics/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.game.Polygon2D;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.GraphicsEnvironment;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import java.awt.TexturePaint;
import java.awt.*;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
Expand Down Expand Up @@ -609,4 +601,33 @@ public static Font randomFont() {

return new Font(randomFontName, randomFontStyle, randomFontSize);
}

public static BasicStroke randomOutlineStroke() {
int cap = Maths.randomInteger(0, 2);
int randomCap;
if (cap == 0) {
randomCap = BasicStroke.CAP_BUTT;
} else if (cap == 1) {
randomCap = BasicStroke.CAP_ROUND;
} else {
randomCap = BasicStroke.CAP_SQUARE;
}

int join = Maths.randomInteger(0, 2);
int randomJoin;
if (join == 0) {
randomJoin = BasicStroke.JOIN_MITER;
} else if (join == 1) {
randomJoin = BasicStroke.JOIN_ROUND;
} else {
randomJoin = BasicStroke.JOIN_BEVEL;
}

return new BasicStroke(
Maths.random(0.0f, 32.0f),
randomCap,
randomJoin,
randomJoin == BasicStroke.JOIN_MITER ? Maths.random(Maths.FloatPrecision, 64.0f) : 0.0f
);
}
}
3 changes: 2 additions & 1 deletion src/main/java/tech/fastj/graphics/util/ModelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -76,7 +77,7 @@ private static String getFileExtension(Path filePath) {
}

private static void checkFileExtension(String fileExtension) {
if (SupportedFileFormats.valuesStream.noneMatch(fileFormat -> fileFormat.equalsIgnoreCase(fileExtension))) {
if (Arrays.stream(SupportedFileFormats.values).noneMatch(fileFormat -> fileFormat.equalsIgnoreCase(fileExtension))) {
throw new IllegalArgumentException(
"Unsupported file extension \"" + fileExtension + "\"."
+ System.lineSeparator()
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/tech/fastj/graphics/util/io/PsdfUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private static BasicStroke parseOutlineStroke(String[] tokens) {
}
}

return new BasicStroke(Integer.parseInt(tokens[1]), basicStrokeCap, basicStrokeJoinStyle, Integer.parseInt(tokens[4]), null, 0.0f);
return new BasicStroke(Float.parseFloat(tokens[1]), basicStrokeCap, basicStrokeJoinStyle, Float.parseFloat(tokens[4]), null, 0.0f);
}

private static Color parseOutlineColor(String[] tokens) {
Expand Down Expand Up @@ -307,12 +307,15 @@ private static void writeRenderStyle(StringBuilder fileContents, RenderStyle ren
switch (renderStyle) {
case Fill: {
fileContents.append(PsdfUtil.ParsingKeys.RenderStyle_Fill);
break;
}
case Outline: {
fileContents.append(PsdfUtil.ParsingKeys.RenderStyle_Outline);
fileContents.append(ParsingKeys.RenderStyle_Outline);
break;
}
case FillAndOutline: {
fileContents.append(PsdfUtil.ParsingKeys.RenderStyle_FillAndOutline);
break;
}
}
fileContents.append(LineSeparator);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.fastj.graphics.util.io;

import java.util.Arrays;
import java.util.stream.Stream;

public class SupportedFileFormats {
public static final String Psdf = "psdf";
Expand All @@ -10,6 +9,5 @@ public class SupportedFileFormats {
Psdf
};

public static final Stream<String> valuesStream = Arrays.stream(values);
public static final String valuesString = Arrays.toString(values);
}
12 changes: 6 additions & 6 deletions src/test/java/unittest/testcases/graphics/DrawableTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ void checkGenerateDrawableIDs_noneShouldMatch() {
@Test
void checkCollision_betweenPolygon2D_andModel2D() {
Pointf[] square = DrawUtil.createBox(0f, 0f, 50f);
Polygon2D polygon2D = new Polygon2D(square);
Polygon2D polygon2D = Polygon2D.fromPoints(square);

Pointf[] square1 = DrawUtil.createBox(Pointf.Origin, 50f);
Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.Origin, 25f), 50f);
Polygon2D[] polygons = {
new Polygon2D(square1),
new Polygon2D(square2)
Polygon2D.fromPoints(square1),
Polygon2D.fromPoints(square2)
};
Model2D model2D = new Model2D(polygons);

Expand All @@ -59,7 +59,7 @@ void checkCollision_betweenPolygon2D_andText2D() {
Text2D text2D = new Text2D(text, Pointf.Origin.copy());

Pointf[] square = DrawUtil.createBox(0f, 0f, 50f);
Polygon2D polygon2D = new Polygon2D(square);
Polygon2D polygon2D = Polygon2D.fromPoints(square);

assertTrue(text2D.collidesWith(polygon2D) && polygon2D.collidesWith(text2D), "The Polygon2D and Text2D should be intersecting.");
});
Expand All @@ -76,8 +76,8 @@ void checkCollision_betweenText2D_andModel2D() {
Pointf[] square1 = DrawUtil.createBox(Pointf.Origin, 50f);
Pointf[] square2 = DrawUtil.createBox(Pointf.add(Pointf.Origin, 25f), 50f);
Polygon2D[] polygons = {
new Polygon2D(square1),
new Polygon2D(square2)
Polygon2D.fromPoints(square1),
Polygon2D.fromPoints(square2)
};
Model2D model2D = new Model2D(polygons);

Expand Down
Loading

0 comments on commit 23f47eb

Please sign in to comment.