Skip to content

Commit 97c7f7a

Browse files
committed
Add builder-pattern to all Markers, MarkerSet, Line and Shape
1 parent 8265cdf commit 97c7f7a

File tree

12 files changed

+890
-33
lines changed

12 files changed

+890
-33
lines changed

src/main/java/de/bluecolored/bluemap/api/markers/DistanceRangedMarker.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,42 @@ public int hashCode() {
101101
return result;
102102
}
103103

104+
public static abstract class Builder<T extends DistanceRangedMarker, B extends DistanceRangedMarker.Builder<T, B>>
105+
extends Marker.Builder<T, B> {
106+
107+
Double minDistance, maxDistance;
108+
109+
/**
110+
* Sets the minimum distance of the camera to the position of the {@link Marker} for it to be displayed.<br>
111+
* If the camera is closer to this {@link Marker} than this distance, it will be hidden!
112+
*
113+
* @param minDistance the new minimum distance
114+
* @return this builder for chaining
115+
*/
116+
public B minDistance(double minDistance) {
117+
this.minDistance = minDistance;
118+
return self();
119+
}
120+
121+
/**
122+
* Sets the maximum distance of the camera to the position of the {@link Marker} for it to be displayed.<br>
123+
* If the camera is further to this {@link Marker} than this distance, it will be hidden!
124+
*
125+
* @param maxDistance the new maximum distance
126+
* @return this builder for chaining
127+
*/
128+
public B maxDistance(double maxDistance) {
129+
this.maxDistance = maxDistance;
130+
return self();
131+
}
132+
133+
@Override
134+
T build(T marker) {
135+
if (minDistance != null) marker.setMinDistance(minDistance);
136+
if (maxDistance != null) marker.setMaxDistance(maxDistance);
137+
return super.build(marker);
138+
}
139+
140+
}
141+
104142
}

src/main/java/de/bluecolored/bluemap/api/markers/ExtrudeMarker.java

Lines changed: 125 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ private ExtrudeMarker() {
6464
* @see #setShape(Shape, float, float)
6565
*/
6666
public ExtrudeMarker(String label, Shape shape, float shapeMinY, float shapeMaxY) {
67-
this(label, calculateShapeCenter(Objects.requireNonNull(shape, "shape must not be null"), shapeMinY, shapeMaxY), shape, shapeMinY, shapeMaxY);
67+
this(
68+
label,
69+
calculateShapeCenter(Objects.requireNonNull(shape, "shape must not be null"), shapeMinY, shapeMaxY),
70+
shape, shapeMinY, shapeMaxY
71+
);
6872
}
6973

7074
/**
7175
* Creates a new {@link ExtrudeMarker}.
72-
* <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>
76+
* <p><i>(Since the shape has its own positions, the position is only used to determine
77+
* e.g. the distance to the camera)</i></p>
7378
*
7479
* @param label the label of the marker
7580
* @param position the coordinates of the marker
@@ -90,7 +95,8 @@ public ExtrudeMarker(String label, Vector3d position, Shape shape, float shapeMi
9095

9196
/**
9297
* Getter for {@link Shape} of this {@link ExtrudeMarker}.
93-
* <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>
98+
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points are the
99+
* z-coordinates in the map.</p>
94100
* @return the {@link Shape}
95101
*/
96102
public Shape getShape() {
@@ -117,7 +123,8 @@ public float getShapeMaxY() {
117123

118124
/**
119125
* Sets the {@link Shape} of this {@link ExtrudeMarker}.
120-
* <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>
126+
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points will be
127+
* the z-coordinates in the map.</p>
121128
* <i>(The shape will be extruded from minY to maxY on the map)</i>
122129
* @param shape the new {@link Shape}
123130
* @param minY the new min-height (y-coordinate) of the shape on the map
@@ -140,15 +147,17 @@ public void centerPosition() {
140147
}
141148

142149
/**
143-
* 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.
150+
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled,
151+
* you'll only see the marker when it is not behind anything.
144152
* @return <code>true</code> if the depthTest is enabled
145153
*/
146154
public boolean isDepthTestEnabled() {
147155
return depthTest;
148156
}
149157

150158
/**
151-
* 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.
159+
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled,
160+
* you'll only see the marker when it is not behind anything.
152161
* @param enabled if the depth-test should be enabled for this {@link ExtrudeMarker}
153162
*/
154163
public void setDepthTestEnabled(boolean enabled) {
@@ -251,4 +260,114 @@ private static Vector3d calculateShapeCenter(Shape shape, float shapeMinY, float
251260
return new Vector3d(center.getX(), centerY, center.getY());
252261
}
253262

263+
/**
264+
* Creates a Builder for {@link ExtrudeMarker}s.
265+
* @return a new Builder
266+
*/
267+
public static Builder builder() {
268+
return new Builder();
269+
}
270+
271+
public static class Builder extends ObjectMarker.Builder<ExtrudeMarker, Builder> {
272+
273+
Shape shape;
274+
float shapeMinY, shapeMaxY;
275+
Boolean depthTest;
276+
Integer lineWidth;
277+
Color lineColor;
278+
Color fillColor;
279+
280+
/**
281+
* Sets the {@link Shape} of the {@link ExtrudeMarker}.
282+
* <p>The shape is placed on the xz-plane of the map, so the y-coordinates of the {@link Shape}'s points will
283+
* be the z-coordinates in the map.</p>
284+
* <i>(The shape will be extruded from minY to maxY on the map)</i>
285+
* @param shape the new {@link Shape}
286+
* @param minY the new min-height (y-coordinate) of the shape on the map
287+
* @param maxY the new max-height (y-coordinate) of the shape on the map
288+
* @return this builder for chaining
289+
*/
290+
public Builder shape(Shape shape, float minY, float maxY) {
291+
this.shape = shape;
292+
this.shapeMinY = minY;
293+
this.shapeMaxY = maxY;
294+
return this;
295+
}
296+
297+
/**
298+
* Sets the position of the {@link ExtrudeMarker} to the center of the {@link Shape} (it's bounding box).
299+
* @return this builder for chaining
300+
*/
301+
public Builder centerPosition() {
302+
position(null);
303+
return this;
304+
}
305+
306+
/**
307+
* If the depth-test is disabled, you can see the marker fully through all objects on the map. If it is enabled,
308+
* you'll only see the marker when it is not behind anything.
309+
* @param enabled if the depth-test should be enabled for this {@link ExtrudeMarker}
310+
* @return this builder for chaining
311+
*/
312+
public Builder depthTestEnabled(boolean enabled) {
313+
this.depthTest = enabled;
314+
return this;
315+
}
316+
317+
/**
318+
* Sets the width of the lines for the {@link ExtrudeMarker}.
319+
* @param width the new width in pixels
320+
* @return this builder for chaining
321+
*/
322+
public Builder lineWidth(int width) {
323+
this.lineWidth = width;
324+
return this;
325+
}
326+
327+
/**
328+
* Sets the {@link Color} of the border-line of the shape.
329+
* @param color the new line-color
330+
* @return this builder for chaining
331+
*/
332+
public Builder lineColor(Color color) {
333+
this.lineColor = color;
334+
return this;
335+
}
336+
337+
/**
338+
* Sets the fill-{@link Color} of the shape.
339+
* @param color the new fill-color
340+
* @return this builder for chaining
341+
*/
342+
public Builder fillColor(Color color) {
343+
this.fillColor = color;
344+
return this;
345+
}
346+
347+
/**
348+
* Creates a new {@link ExtrudeMarker} with the current builder-settings.<br>
349+
* The minimum required settings to build this marker are:
350+
* <ul>
351+
* <li>{@link #label(String)}</li>
352+
* <li>{@link #shape(Shape, float, float)}</li>
353+
* </ul>
354+
* @return The new {@link ExtrudeMarker}-instance
355+
*/
356+
@Override
357+
public ExtrudeMarker build() {
358+
ExtrudeMarker marker = new ExtrudeMarker(
359+
checkNotNull(label, "label"),
360+
checkNotNull(shape, "shape"),
361+
shapeMinY,
362+
shapeMaxY
363+
);
364+
if (depthTest != null) marker.setDepthTestEnabled(depthTest);
365+
if (lineWidth != null) marker.setLineWidth(lineWidth);
366+
if (lineColor != null) marker.setLineColor(lineColor);
367+
if (fillColor != null) marker.setFillColor(fillColor);
368+
return build(marker);
369+
}
370+
371+
}
372+
254373
}

src/main/java/de/bluecolored/bluemap/api/markers/HtmlMarker.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public String getHtml() {
120120
*
121121
* <p>
122122
* <b>Important:</b><br>
123-
* 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!
123+
* Make sure you escape all html-tags from possible user inputs to prevent possible
124+
* <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS-Attacks</a> on the web-client!
124125
* </p>
125126
*
126127
* @param html the html that will be inserted as the marker.
@@ -149,4 +150,77 @@ public int hashCode() {
149150
return result;
150151
}
151152

153+
/**
154+
* Creates a Builder for {@link HtmlMarker}s.
155+
* @return a new Builder
156+
*/
157+
public static Builder builder() {
158+
return new Builder();
159+
}
160+
161+
public static class Builder extends DistanceRangedMarker.Builder<HtmlMarker, Builder> {
162+
163+
Vector2i anchor;
164+
String html;
165+
166+
/**
167+
* Sets the position (in pixels) where the html-element is anchored to the map.
168+
* @param anchor the anchor-position in pixels
169+
* @return this builder for chaining
170+
*/
171+
public Builder anchor(Vector2i anchor) {
172+
this.anchor = anchor;
173+
return this;
174+
}
175+
176+
/**
177+
* Sets the position (in pixels) where the html-element is anchored to the map.
178+
* @param x the anchor-x-position in pixels
179+
* @param y the anchor-y-position in pixels
180+
* @return this builder for chaining
181+
*/
182+
public Builder anchor(int x, int y) {
183+
this.anchor = new Vector2i(x, y);
184+
return this;
185+
}
186+
187+
/**
188+
* Sets the html for the {@link HtmlMarker}.
189+
*
190+
* <p>
191+
* <b>Important:</b><br>
192+
* 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!
193+
* </p>
194+
*
195+
* @param html the html that will be inserted as the marker.
196+
* @return this builder for chaining
197+
*/
198+
public Builder html(String html) {
199+
this.html = html;
200+
return this;
201+
}
202+
203+
/**
204+
* Creates a new {@link HtmlMarker} with the current builder-settings.<br>
205+
* The minimum required settings to build this marker are:
206+
* <ul>
207+
* <li>{@link #setLabel(String)}</li>
208+
* <li>{@link #setPosition(Vector3d)}</li>
209+
* <li>{@link #setHtml(String)}</li>
210+
* </ul>
211+
* @return The new {@link HtmlMarker}-instance
212+
*/
213+
@Override
214+
public HtmlMarker build() {
215+
HtmlMarker marker = new HtmlMarker(
216+
checkNotNull(label, "label"),
217+
checkNotNull(position, "position"),
218+
checkNotNull(html, "html")
219+
);
220+
if (anchor != null) marker.setAnchor(anchor);
221+
return build(marker);
222+
}
223+
224+
}
225+
152226
}

0 commit comments

Comments
 (0)