Skip to content

Commit

Permalink
(#10, #32) Added outline support, polygon builder
Browse files Browse the repository at this point in the history
New Additions:
- Polygon2D now properly supports outlines (BasicStroke only)
- revamped ModelUtil (PsdfUtil → ModelUtil)
    - Added outline, render style, and transform support
    - Refractored code to allow for later support for more file formats

Breaking Changes:
- Removed public access to all `Polygon2D` constructors. `Polygon2D`s must now be created using `Polygon2D#create`, which corresponds to a `Polygon2DBuilder` instance.
- Renamed `PsdfUtil#loadPsdf` → `ModelUtil#loadModel` & `PsdfUtil#writePsdf` → `ModelUtil#writeModel` to better represent what the methods do.
  • Loading branch information
lucasstarsz committed Jul 4, 2021
1 parent 036ebb9 commit 6521afe
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 293 deletions.
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
exports tech.fastj.systems.control;
exports tech.fastj.systems.tags;
exports tech.fastj.graphics.util.gradients;
exports tech.fastj.graphics.util.io;
}
10 changes: 5 additions & 5 deletions src/main/java/tech/fastj/graphics/game/Polygon2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Polygon2D extends GameObject {
/** {@link Paint} representing the default fill paint value as the color black. */
public static final Paint DefaultPaint = Color.black;
/** {@link Stroke} representing the default outline stroke value as a 1px outline with sharp edges. */
public static final Stroke DefaultOutlineStroke = new BasicStroke(1f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f);
public static final BasicStroke DefaultOutlineStroke = new BasicStroke(1f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f);
/** {@link Color} representing the default outline color value as the color black. */
public static final Color DefaultOutlineColor = Color.black;

Expand All @@ -38,7 +38,7 @@ public class Polygon2D extends GameObject {
private RenderStyle renderStyle;
private Paint fillPaint;
private Color outlineColor;
private Stroke outlineStroke;
private BasicStroke outlineStroke;

/**
* {@code Polygon2D} constructor that takes in an array of points.
Expand Down Expand Up @@ -134,7 +134,7 @@ public Color getOutlineColor() {
return outlineColor;
}

public Stroke getOutlineStroke() {
public BasicStroke getOutlineStroke() {
return outlineStroke;
}

Expand All @@ -158,12 +158,12 @@ public Polygon2D setOutlineColor(Color newOutlineColor) {
return this;
}

public Polygon2D setOutlineStroke(Stroke newStroke) {
public Polygon2D setOutlineStroke(BasicStroke newStroke) {
outlineStroke = newStroke;
return this;
}

public Polygon2D setOutline(Stroke newStroke, Color newOutlineColor) {
public Polygon2D setOutline(BasicStroke newStroke, Color newOutlineColor) {
outlineStroke = newStroke;
outlineColor = newOutlineColor;
return this;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/tech/fastj/graphics/game/Polygon2DBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import tech.fastj.graphics.RenderStyle;
import tech.fastj.graphics.Transform2D;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Stroke;
import java.util.Objects;

public class Polygon2DBuilder {
Expand All @@ -16,7 +16,7 @@ public class Polygon2DBuilder {
private final RenderStyle renderStyle;

private Paint fillPaint = Polygon2D.DefaultPaint;
private Stroke outlineStroke = Polygon2D.DefaultOutlineStroke;
private BasicStroke outlineStroke = Polygon2D.DefaultOutlineStroke;
private Color outlineColor = Polygon2D.DefaultOutlineColor;

private Pointf translation = Transform2D.DefaultTranslation.copy();
Expand All @@ -34,7 +34,7 @@ public Polygon2DBuilder withFill(Paint fillPaint) {
return this;
}

public Polygon2DBuilder withOutline(Stroke outlineStroke, Color outlineColor) {
public Polygon2DBuilder withOutline(BasicStroke outlineStroke, Color outlineColor) {
this.outlineStroke = Objects.requireNonNull(outlineStroke, "The outline stroke must not be null.");
this.outlineColor = Objects.requireNonNull(outlineColor, "The outline color must not be null.");
return this;
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/tech/fastj/graphics/util/ModelUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tech.fastj.graphics.util;

import tech.fastj.engine.CrashMessages;
import tech.fastj.graphics.game.Model2D;
import tech.fastj.graphics.game.Polygon2D;
import tech.fastj.graphics.util.io.PsdfUtil;
import tech.fastj.graphics.util.io.SupportedFileFormats;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class ModelUtil {

private static final Map<String, Function<List<String>, Polygon2D[]>> ModelParser = Map.of(
SupportedFileFormats.Psdf, PsdfUtil::parse
);

private static final Map<String, BiConsumer<Path, Model2D>> ModelWriter = Map.of(
SupportedFileFormats.Psdf, PsdfUtil::write
);

/**
* Gets a {@code Polygon2D} array, loaded from a {@code .psdf} file.
* <p>
* This method allows the user to load an array of {@code Polygon2D}s from a single file, decreasing the amount of
* models that have to be programmed in.
* <p>
* Furthermore, this allows for easy use of the {@code Model2D} class, allowing you to directly use the resulting
* array from this method to create a {@code Model2D} object.
*
* @param fileLocation Location of the file.
* @return An array of {@code Polygon2D}s.
*/
public static Polygon2D[] loadModel(Path fileLocation) {
if (!Files.exists(fileLocation, LinkOption.NOFOLLOW_LINKS)) {
throw new IllegalArgumentException("A file was not found at \"" + fileLocation.toAbsolutePath() + "\".");
}

String fileExtension = getFileExtension(fileLocation);
checkFileExtension(fileExtension);

List<String> lines;
try {
lines = Files.readAllLines(fileLocation);
} catch (IOException exception) {
throw new IllegalStateException(
CrashMessages.theGameCrashed("an issue while trying to parse file \"" + fileLocation.toAbsolutePath() + "\"."),
exception
);
}

return ModelParser.get(fileExtension).apply(lines);
}

/**
* Writes a model file containing the current state of the {@code Polygon2D}s that make up the specified {@code
* Model2D}.
*
* @param destinationPath The destination path of the model file that will be written.
* @param model The {@code Model2D} that will be written to the file.
*/
public static void writeModel(Path destinationPath, Model2D model) {
String fileExtension = getFileExtension(destinationPath);
checkFileExtension(fileExtension);
ModelWriter.get(fileExtension).accept(destinationPath, model);
}

private static String getFileExtension(Path filePath) {
return filePath.toString().substring(filePath.toString().lastIndexOf(".") + 1);
}

private static void checkFileExtension(String fileExtension) {
if (SupportedFileFormats.valuesStream.noneMatch(fileFormat -> fileFormat.equalsIgnoreCase(fileExtension))) {
throw new IllegalArgumentException(
"Unsupported file extension \"" + fileExtension + "\"."
+ System.lineSeparator()
+ "This engine only supports files of the following extensions: " + SupportedFileFormats.valuesString
);
}
}
}
Loading

0 comments on commit 6521afe

Please sign in to comment.