Skip to content

Commit

Permalink
DefaultShouldRender(), transform guards/set method
Browse files Browse the repository at this point in the history
- `Drawable#DefaultShouldRender` replaces the use of separate `DefaultShow` fields in each Drawable subclass.
- guards have been added to each transformation method to return immediately if the modification is irrelevant.
    - In the case of set methods, if the set is equal to the current value.
    - In the case of other methods, if the modifier is 0.0.
- a `setTransform` method was also added to reduce code bloat while trying to set several transformation values at once.
  • Loading branch information
lucasstarsz committed Jul 4, 2021
1 parent c535bba commit 036ebb9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 44 deletions.
42 changes: 42 additions & 0 deletions src/main/java/tech/fastj/graphics/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
public abstract class Drawable extends TaggableEntity {

/** {@code boolean} representing the default value for if a {@code Drawable} should be rendered as {@code true}. */
public static final boolean DefaultShouldRender = true;

private final UUID rawID;
private final String id;

Expand Down Expand Up @@ -170,6 +173,10 @@ public Pointf getTranslation() {
* @return The {@code Drawable}, for method chaining.
*/
public Drawable setTranslation(Pointf setTranslation) {
if (getTranslation().equals(setTranslation)) {
return this;
}

transform.setTranslation(setTranslation);
updateTransformedCollisionPath();
return this;
Expand All @@ -191,6 +198,10 @@ public float getRotation() {
* @return The {@code Drawable}, for method chaining.
*/
public Drawable setRotation(float setRotation) {
if (getRotation() == setRotation) {
return this;
}

transform.setRotation(setRotation);
updateTransformedCollisionPath();
return this;
Expand All @@ -212,18 +223,41 @@ public Pointf getScale() {
* @return The {@code Drawable}, for method chaining.
*/
public Drawable setScale(Pointf setScale) {
if (getScale().equals(setScale)) {
return this;
}

transform.setScale(setScale);
updateTransformedCollisionPath();
return this;
}

/**
* Sets the {@code Drawable}'s translation, rotation, and scale to the specified values.
*
* @param setTranslation {@code Pointf} parameter that the {@code Drawable}'s translation will be set to.
* @param setRotation float parameter that the {@code Drawable}'s rotation will be set to.
* @param setScale {@code Pointf} parameter that the {@code Drawable}'s scale will be set to.
* @return The {@code Drawable}, for method chaining.
*/
public Drawable setTransform(Pointf setTranslation, float setRotation, Pointf setScale) {
setTranslation(setTranslation);
setRotation(setRotation);
setScale(setScale);
return this;
}

/**
* Translates the {@code Drawable}'s position by the specified translation.
*
* @param translationMod {@code Pointf} parameter that the {@code Drawable}'s x and y location will be translated
* by.
*/
public void translate(Pointf translationMod) {
if (Transform2D.DefaultTranslation.equals(translationMod)) {
return;
}

transform.translate(translationMod);
updateTransformedCollisionPath();
}
Expand All @@ -235,6 +269,10 @@ public void translate(Pointf translationMod) {
* @param centerpoint {@code Pointf} parameter that the {@code Drawable} will be rotated about.
*/
public void rotate(float rotationMod, Pointf centerpoint) {
if (rotationMod == Transform2D.DefaultRotation) {
return;
}

transform.rotate(rotationMod, centerpoint);
updateTransformedCollisionPath();
}
Expand All @@ -246,6 +284,10 @@ public void rotate(float rotationMod, Pointf centerpoint) {
* @param centerpoint {@code Pointf} parameter that the {@code Drawable} will be scaled about.
*/
public void scale(Pointf scaleMod, Pointf centerpoint) {
if (Transform2D.DefaultScale.equals(scaleMod)) {
return;
}

transform.scale(scaleMod, centerpoint);
updateTransformedCollisionPath();
}
Expand Down
39 changes: 17 additions & 22 deletions src/main/java/tech/fastj/graphics/game/Model2D.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package tech.fastj.graphics.game;

import tech.fastj.math.Pointf;
import tech.fastj.graphics.Drawable;
import tech.fastj.graphics.util.DrawUtil;
import tech.fastj.graphics.util.PsdfUtil;

import tech.fastj.systems.control.Scene;

Expand All @@ -12,62 +12,57 @@

/**
* {@code Drawable} subclass for grouping an array of {@code Polygon2D}s under a single object.
* <p>
* This class is compatible with loading from a .PSDF file, using the {@link PsdfUtil#loadPsdf(String)} method.
*
* @author Andrew Dey
* @version 1.0.0
*/
public class Model2D extends GameObject {

/** {@code boolean} representing the default "should render" of {@code true}. */
public static final boolean DefaultShow = true;

private Polygon2D[] polyArr;

/**
* Model2D constructor that takes in an array of {@link Polygon2D} objects.
* <p>
* This takes an array of {@code Pointf} values (which make up the points of the polygon), and defaults whether the
* {@code Model2D} should be shown to {@link #DefaultShow}.
* {@code Model2D} should be shown to {@link Drawable#DefaultShouldRender}.
*
* @param polygonArray Array of {@code Polygon2D}s used to create the Model2D.
*/
public Model2D(Polygon2D[] polygonArray) {
this(polygonArray, DefaultShow);
this(polygonArray, Drawable.DefaultShouldRender);
}

/**
* Model2D constructor that takes in an array of {@link Polygon2D} objects and a show variable.
* Model2D constructor that takes in an array of {@link Polygon2D} objects and a shouldRender variable.
* <p>
* This takes an array of {@code Pointf} values (which make up the points of the polygon), and a boolean to
* determine whether the Model2D should be drawn.
* determine whether the Model2D should be rendered.
*
* @param polygonArray Array of {@code Polygon2D}s used to create the Model2D.
* @param show Boolean that determines whether this Model2D should be drawn to the screen.
* @param shouldRender Boolean that determines whether this Model2D should be drawn to the screen.
*/
public Model2D(Polygon2D[] polygonArray, boolean show) {
public Model2D(Polygon2D[] polygonArray, boolean shouldRender) {
polyArr = polygonArray;

setCollisionPath(DrawUtil.createPath(DrawUtil.createCollisionOutline(polyArr)));
setShouldRender(show);
setShouldRender(shouldRender);
}

/**
* {@code Model2D} constructor that takes in an array of {@code Polygon2D}s, a show variable, and an initial
* {@code Model2D} constructor that takes in an array of {@code Polygon2D}s, a shouldRender variable, and an initial
* translation, rotation, and scale for the model.
* <p>
* Alongside the normal constructor, this allows you to set a location, rotation, and scale for the object,
* alongside the normal values needed for a Model2D.
*
* @param polygonArray Array of {@code Polygon2D}s used to create the Model2D.
* @param location {@code Pointf} that defines the x and y location of the created Model2D. All Polygon2D
* objects will be translated to that location, relative to where the objects are.
* @param rotVal Float value that defines the value that the Model2D will be rotated to, on creation.
* @param scaleVal {@code Pointf} that defines the values that the Model2D will be scaled to, on creation.
* @param shouldBeShown Boolean that determines whether this Model2D should be drawn to the screen.
* @param polygonArray Array of {@code Polygon2D}s used to create the Model2D.
* @param location {@code Pointf} that defines the x and y location of the created Model2D. All Polygon2D
* objects will be translated to that location, relative to where the objects are.
* @param rotVal Float value that defines the value that the Model2D will be rotated to, on creation.
* @param scaleVal {@code Pointf} that defines the values that the Model2D will be scaled to, on creation.
* @param shouldRender Boolean that determines whether this Model2D should be drawn to the screen.
*/
public Model2D(Polygon2D[] polygonArray, Pointf location, float rotVal, Pointf scaleVal, boolean shouldBeShown) {
public Model2D(Polygon2D[] polygonArray, Pointf location, float rotVal, Pointf scaleVal, boolean shouldRender) {
polyArr = polygonArray;

setCollisionPath(DrawUtil.createPath(DrawUtil.createCollisionOutline(polyArr)));
Expand All @@ -76,7 +71,7 @@ public Model2D(Polygon2D[] polygonArray, Pointf location, float rotVal, Pointf s
setRotation(rotVal);
setScale(scaleVal);

setShouldRender(shouldBeShown);
setShouldRender(shouldRender);
}

/**
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/tech/fastj/graphics/game/Text2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Pointf;
import tech.fastj.graphics.Drawable;

import tech.fastj.systems.control.Scene;

import java.awt.Color;
import java.awt.Paint;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
Expand All @@ -27,8 +28,6 @@ public class Text2D extends GameObject {
public static final Paint DefaultPaint = Color.black;
/** {@link Font} representing the default font of {@code Segoe UI Plain, 12px}. */
public static final Font DefaultFont = new Font("Segoe UI", Font.PLAIN, 12);
/** {@code boolean} representing the default "should render" value of {@code true}. */
public static final boolean DefaultShow = true;

private String text;
private Paint color;
Expand All @@ -39,72 +38,73 @@ public class Text2D extends GameObject {
* {@code Text2D} Constructor that takes in a string of text.
* <p>
* This constructor defaults the color to {@link #DefaultPaint}, the font to {@link #DefaultFont}, and sets the
* {@code show} boolean to {@link #DefaultShow}.
* {@code show} boolean to {@link Drawable#DefaultShouldRender}.
*
* @param setText Sets the displayed text.
*/
public Text2D(String setText) {
this(setText, DefaultPaint, DefaultFont, DefaultShow);
this(setText, DefaultPaint, DefaultFont, Drawable.DefaultShouldRender);
}

/**
* {@code Text2D} Constructor that takes in a string of text and an initial translation.
* <p>
* This constructor defaults the color to {@link #DefaultPaint}, the font to {@link #DefaultFont}, and sets the
* {@code show} boolean to {@link #DefaultShow}.
* {@code shouldRender} boolean to {@link Drawable#DefaultShouldRender}.
*
* @param setText Sets the displayed text.
* @param setTranslation Sets the initial x and y translation of the text.
*/
public Text2D(String setText, Pointf setTranslation) {
this(setText, DefaultPaint, DefaultFont, DefaultShow);
this(setText, DefaultPaint, DefaultFont, Drawable.DefaultShouldRender);
setTranslation(setTranslation);
}

/**
* {@code Text2D} Constructor that takes in a string of text, a color, a font, and a show variable.
* {@code Text2D} Constructor that takes in a string of text, a color, a font, and a shouldRender variable.
*
* @param setText Sets the displayed text.
* @param setPaint Sets the text's color.
* @param setFont Sets the text's font.
* @param show Sets whether the text will be drawn to the screen.
* @param setText Sets the displayed text.
* @param setPaint Sets the text's color.
* @param setFont Sets the text's font.
* @param shouldRender Sets whether the text will be drawn to the screen.
*/
public Text2D(String setText, Paint setPaint, Font setFont, boolean show) {
public Text2D(String setText, Paint setPaint, Font setFont, boolean shouldRender) {
text = setText;
font = setFont;

setPaint(setPaint);
setFont(setFont);
setShouldRender(show);
setShouldRender(shouldRender);
}

/**
* {@code Text2D} Constructor that takes in a string of text, a translation, a color, a font, and a show variable.
* {@code Text2D} Constructor that takes in a string of text, a translation, a color, a font, and a shouldRender
* variable.
*
* @param setText Sets the displayed text.
* @param setTranslation Sets the initial x and y translation of the text.
* @param setPaint Sets the text's color.
* @param setFont Sets the text's font.
* @param show Sets whether the text will be drawn to the screen.
* @param shouldRender Sets whether the text will be drawn to the screen.
*/
public Text2D(String setText, Pointf setTranslation, Paint setPaint, Font setFont, boolean show) {
this(setText, setPaint, setFont, show);
public Text2D(String setText, Pointf setTranslation, Paint setPaint, Font setFont, boolean shouldRender) {
this(setText, setPaint, setFont, shouldRender);
setTranslation(setTranslation);
}

/**
* {@code Text2D} Constructor that takes in a string of text, a translation/rotation/scale, a color, a font, and a
* show variable.
* shouldRender variable.
*
* @param setText Sets the displayed text.
* @param setTranslation Sets the initial x and y translation of the text.
* @param setRotation Sets the initial rotation of the text.
* @param setScale Sets the initial scale of the text.
* @param setPaint Sets the text's color.
* @param setFont Sets the text's font.
* @param show Sets whether the text will be drawn to the screen.
* @param shouldRender Sets whether the text will be drawn to the screen.
*/
public Text2D(String setText, Pointf setTranslation, float setRotation, Pointf setScale, Paint setPaint, Font setFont, boolean show) {
public Text2D(String setText, Pointf setTranslation, float setRotation, Pointf setScale, Paint setPaint, Font setFont, boolean shouldRender) {
text = setText;
font = setFont;

Expand All @@ -114,7 +114,7 @@ public Text2D(String setText, Pointf setTranslation, float setRotation, Pointf s

setPaint(setPaint);
setFont(setFont);
setShouldRender(show);
setShouldRender(shouldRender);
}

/**
Expand Down

0 comments on commit 036ebb9

Please sign in to comment.