Skip to content

Commit

Permalink
Add builder-pattern to all Markers, MarkerSet, Line and Shape
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Aug 5, 2022
1 parent 8265cdf commit 97c7f7a
Show file tree
Hide file tree
Showing 12 changed files with 890 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,42 @@ public int hashCode() {
return result;
}

public static abstract class Builder<T extends DistanceRangedMarker, B extends DistanceRangedMarker.Builder<T, B>>
extends Marker.Builder<T, B> {

Double minDistance, maxDistance;

/**
* Sets the minimum distance of the camera to the position of the {@link Marker} for it to be displayed.<br>
* If the camera is closer to this {@link Marker} than this distance, it will be hidden!
*
* @param minDistance the new minimum distance
* @return this builder for chaining
*/
public B minDistance(double minDistance) {
this.minDistance = minDistance;
return self();
}

/**
* Sets the maximum distance of the camera to the position of the {@link Marker} for it to be displayed.<br>
* If the camera is further to this {@link Marker} than this distance, it will be hidden!
*
* @param maxDistance the new maximum distance
* @return this builder for chaining
*/
public B maxDistance(double maxDistance) {
this.maxDistance = maxDistance;
return self();
}

@Override
T build(T marker) {
if (minDistance != null) marker.setMinDistance(minDistance);
if (maxDistance != null) marker.setMaxDistance(maxDistance);
return super.build(marker);
}

}

}
131 changes: 125 additions & 6 deletions src/main/java/de/bluecolored/bluemap/api/markers/ExtrudeMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ private ExtrudeMarker() {
* @see #setShape(Shape, float, float)
*/
public ExtrudeMarker(String label, Shape shape, float shapeMinY, float shapeMaxY) {
this(label, calculateShapeCenter(Objects.requireNonNull(shape, "shape must not be null"), shapeMinY, shapeMaxY), shape, shapeMinY, shapeMaxY);
this(
label,
calculateShapeCenter(Objects.requireNonNull(shape, "shape must not be null"), shapeMinY, shapeMaxY),
shape, shapeMinY, shapeMaxY
);
}

/**
* Creates a new {@link ExtrudeMarker}.
* <p><i>(Since the shape has its own positions, the position is only used to determine e.g. the distance to the camera)</i></p>
* <p><i>(Since the shape has its own positions, the position is only used to determine
* e.g. the distance to the camera)</i></p>
*
* @param label the label of the marker
* @param position the coordinates of the marker
Expand All @@ -90,7 +95,8 @@ public ExtrudeMarker(String label, Vector3d position, Shape shape, float shapeMi

/**
* Getter for {@link Shape} of this {@link ExtrudeMarker}.
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points are the z-coordinates in the map.</p>
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points are the
* z-coordinates in the map.</p>
* @return the {@link Shape}
*/
public Shape getShape() {
Expand All @@ -117,7 +123,8 @@ public float getShapeMaxY() {

/**
* Sets the {@link Shape} of this {@link ExtrudeMarker}.
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points will be the z-coordinates in the map.</p>
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points will be
* the z-coordinates in the map.</p>
* <i>(The shape will be extruded from minY to maxY on the map)</i>
* @param shape the new {@link Shape}
* @param minY the new min-height (y-coordinate) of the shape on the map
Expand All @@ -140,15 +147,17 @@ public void centerPosition() {
}

/**
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled, you'll only see the marker when it is not behind anything.
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled,
* you'll only see the marker when it is not behind anything.
* @return <code>true</code> if the depthTest is enabled
*/
public boolean isDepthTestEnabled() {
return depthTest;
}

/**
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled, you'll only see the marker when it is not behind anything.
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled,
* you'll only see the marker when it is not behind anything.
* @param enabled if the depth-test should be enabled for this {@link ExtrudeMarker}
*/
public void setDepthTestEnabled(boolean enabled) {
Expand Down Expand Up @@ -251,4 +260,114 @@ private static Vector3d calculateShapeCenter(Shape shape, float shapeMinY, float
return new Vector3d(center.getX(), centerY, center.getY());
}

/**
* Creates a Builder for {@link ExtrudeMarker}s.
* @return a new Builder
*/
public static Builder builder() {
return new Builder();
}

public static class Builder extends ObjectMarker.Builder<ExtrudeMarker, Builder> {

Shape shape;
float shapeMinY, shapeMaxY;
Boolean depthTest;
Integer lineWidth;
Color lineColor;
Color fillColor;

/**
* Sets the {@link Shape} of the {@link ExtrudeMarker}.
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points will
* be the z-coordinates in the map.</p>
* <i>(The shape will be extruded from minY to maxY on the map)</i>
* @param shape the new {@link Shape}
* @param minY the new min-height (y-coordinate) of the shape on the map
* @param maxY the new max-height (y-coordinate) of the shape on the map
* @return this builder for chaining
*/
public Builder shape(Shape shape, float minY, float maxY) {
this.shape = shape;
this.shapeMinY = minY;
this.shapeMaxY = maxY;
return this;
}

/**
* Sets the position of the {@link ExtrudeMarker} to the center of the {@link Shape} (it's bounding box).
* @return this builder for chaining
*/
public Builder centerPosition() {
position(null);
return this;
}

/**
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled,
* you'll only see the marker when it is not behind anything.
* @param enabled if the depth-test should be enabled for this {@link ExtrudeMarker}
* @return this builder for chaining
*/
public Builder depthTestEnabled(boolean enabled) {
this.depthTest = enabled;
return this;
}

/**
* Sets the width of the lines for the {@link ExtrudeMarker}.
* @param width the new width in pixels
* @return this builder for chaining
*/
public Builder lineWidth(int width) {
this.lineWidth = width;
return this;
}

/**
* Sets the {@link Color} of the border-line of the shape.
* @param color the new line-color
* @return this builder for chaining
*/
public Builder lineColor(Color color) {
this.lineColor = color;
return this;
}

/**
* Sets the fill-{@link Color} of the shape.
* @param color the new fill-color
* @return this builder for chaining
*/
public Builder fillColor(Color color) {
this.fillColor = color;
return this;
}

/**
* Creates a new {@link ExtrudeMarker} with the current builder-settings.<br>
* The minimum required settings to build this marker are:
* <ul>
* <li>{@link #label(String)}</li>
* <li>{@link #shape(Shape, float, float)}</li>
* </ul>
* @return The new {@link ExtrudeMarker}-instance
*/
@Override
public ExtrudeMarker build() {
ExtrudeMarker marker = new ExtrudeMarker(
checkNotNull(label, "label"),
checkNotNull(shape, "shape"),
shapeMinY,
shapeMaxY
);
if (depthTest != null) marker.setDepthTestEnabled(depthTest);
if (lineWidth != null) marker.setLineWidth(lineWidth);
if (lineColor != null) marker.setLineColor(lineColor);
if (fillColor != null) marker.setFillColor(fillColor);
return build(marker);
}

}

}
76 changes: 75 additions & 1 deletion src/main/java/de/bluecolored/bluemap/api/markers/HtmlMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public String getHtml() {
*
* <p>
* <b>Important:</b><br>
* Make sure you escape all html-tags from possible user inputs to prevent possible <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS-Attacks</a> on the web-client!
* Make sure you escape all html-tags from possible user inputs to prevent possible
* <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS-Attacks</a> on the web-client!
* </p>
*
* @param html the html that will be inserted as the marker.
Expand Down Expand Up @@ -149,4 +150,77 @@ public int hashCode() {
return result;
}

/**
* Creates a Builder for {@link HtmlMarker}s.
* @return a new Builder
*/
public static Builder builder() {
return new Builder();
}

public static class Builder extends DistanceRangedMarker.Builder<HtmlMarker, Builder> {

Vector2i anchor;
String html;

/**
* Sets the position (in pixels) where the html-element is anchored to the map.
* @param anchor the anchor-position in pixels
* @return this builder for chaining
*/
public Builder anchor(Vector2i anchor) {
this.anchor = anchor;
return this;
}

/**
* Sets the position (in pixels) where the html-element is anchored to the map.
* @param x the anchor-x-position in pixels
* @param y the anchor-y-position in pixels
* @return this builder for chaining
*/
public Builder anchor(int x, int y) {
this.anchor = new Vector2i(x, y);
return this;
}

/**
* Sets the html for the {@link HtmlMarker}.
*
* <p>
* <b>Important:</b><br>
* Make sure you escape all html-tags from possible user inputs to prevent possible <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS-Attacks</a> on the web-client!
* </p>
*
* @param html the html that will be inserted as the marker.
* @return this builder for chaining
*/
public Builder html(String html) {
this.html = html;
return this;
}

/**
* Creates a new {@link HtmlMarker} with the current builder-settings.<br>
* The minimum required settings to build this marker are:
* <ul>
* <li>{@link #setLabel(String)}</li>
* <li>{@link #setPosition(Vector3d)}</li>
* <li>{@link #setHtml(String)}</li>
* </ul>
* @return The new {@link HtmlMarker}-instance
*/
@Override
public HtmlMarker build() {
HtmlMarker marker = new HtmlMarker(
checkNotNull(label, "label"),
checkNotNull(position, "position"),
checkNotNull(html, "html")
);
if (anchor != null) marker.setAnchor(anchor);
return build(marker);
}

}

}
Loading

0 comments on commit 97c7f7a

Please sign in to comment.