From 49b6c995cb2045cc9c64cf5f00e6b71e570109d3 Mon Sep 17 00:00:00 2001 From: Rinde van Lon Date: Mon, 2 Mar 2015 21:32:13 +0100 Subject: [PATCH] RoadUserRenderer now also defines a Builder, constructors are deprecated --- .../rinsim/examples/core/SimpleExample.java | 2 +- .../rinsim/ui/renderers/RoadUserRenderer.java | 152 ++++++++++++++++-- 2 files changed, 140 insertions(+), 14 deletions(-) diff --git a/example/src/main/java/com/github/rinde/rinsim/examples/core/SimpleExample.java b/example/src/main/java/com/github/rinde/rinsim/examples/core/SimpleExample.java index c4e1ae4ae..51914a0c6 100644 --- a/example/src/main/java/com/github/rinde/rinsim/examples/core/SimpleExample.java +++ b/example/src/main/java/com/github/rinde/rinsim/examples/core/SimpleExample.java @@ -92,7 +92,7 @@ public static void run(boolean testing) { // dots. final View.Builder viewBuilder = View.create(sim) .with(PlaneRoadModelRenderer.create()) - .with(new RoadUserRenderer()); + .with(RoadUserRenderer.builder()); if (testing) { final int speedUp = 16; diff --git a/ui/src/main/java/com/github/rinde/rinsim/ui/renderers/RoadUserRenderer.java b/ui/src/main/java/com/github/rinde/rinsim/ui/renderers/RoadUserRenderer.java index 94c0d7ebc..86d18592a 100644 --- a/ui/src/main/java/com/github/rinde/rinsim/ui/renderers/RoadUserRenderer.java +++ b/ui/src/main/java/com/github/rinde/rinsim/ui/renderers/RoadUserRenderer.java @@ -23,40 +23,62 @@ import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.RGB; import com.github.rinde.rinsim.core.model.ModelProvider; import com.github.rinde.rinsim.core.model.road.RoadModel; import com.github.rinde.rinsim.core.model.road.RoadUser; import com.github.rinde.rinsim.geom.Point; +import com.google.common.base.Optional; /** + * Renderer that draws simple circles for {@link RoadUser}s in a + * {@link RoadModel}. Use {@link #builder()} for obtaining instances. * @author Rinde van Lon (rinde.vanlon@cs.kuleuven.be) - * @author Bartosz Michalik changes in - * handling colors - * + * @author Bartosz Michalik changes in handling colors */ public final class RoadUserRenderer implements ModelRenderer { @Nullable - private RoadModel rs; + private RoadModel model; private final boolean useEncirclement; private final UiSchema uiSchema; - @Nullable - private final ViewRect viewRect; + private final Optional viewRect; + /** + * @deprecated Use {@link #builder()} instead. + */ + @Deprecated public RoadUserRenderer() { - this(null, null, false); + this(null, Optional. absent(), new UiSchema(), false); } + /** + * @deprecated Use {@link #builder()} instead. + */ + @Deprecated public RoadUserRenderer(UiSchema schema, boolean useEncirclement) { - this(null, schema, useEncirclement); + this(null, Optional. absent(), schema, useEncirclement); } + /** + * @deprecated Use {@link #builder()} instead. + */ + @Deprecated public RoadUserRenderer(@Nullable ViewRect rect, @Nullable UiSchema schema, boolean useEncirclement) { + this(null, + Optional.fromNullable(rect), + schema == null ? new UiSchema() : schema, + useEncirclement); + } + + RoadUserRenderer(@Nullable RoadModel rm, Optional rect, + UiSchema schema, boolean encirclement) { + model = rm; viewRect = rect; - this.useEncirclement = useEncirclement; - uiSchema = schema == null ? new UiSchema() : schema; + useEncirclement = encirclement; + uiSchema = schema; } @Override @@ -66,7 +88,7 @@ public void renderDynamic(GC gc, ViewPort vp, long time) { uiSchema.initialize(gc.getDevice()); gc.setBackground(uiSchema.getDefaultColor()); - final Map objects = rs.getObjectsAndPositions(); + final Map objects = model.getObjectsAndPositions(); synchronized (objects) { for (final Entry entry : objects.entrySet()) { final Point p = entry.getValue(); @@ -107,11 +129,115 @@ public void renderStatic(GC gc, ViewPort vp) {} @Nullable @Override public ViewRect getViewRect() { - return viewRect; + if (viewRect.isPresent()) { + return viewRect.get(); + } + return null; } @Override public void registerModelProvider(ModelProvider mp) { - rs = mp.tryGetModel(RoadModel.class); + model = mp.tryGetModel(RoadModel.class); + } + + /** + * Constructs a {@link Builder} for building {@link RoadUserRenderer} + * instances. + * @return A new builder. + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Builder for {@link RoadUserRenderer}. + * @author Rinde van Lon + */ + public static class Builder implements CanvasRendererBuilder { + private boolean useEncirclement; + private final UiSchema uiSchema; + private Optional minPoint; + private Optional maxPoint; + + Builder(UiSchema uis) { + useEncirclement = false; + uiSchema = uis; + minPoint = Optional.absent(); + maxPoint = Optional.absent(); + } + + Builder() { + this(new UiSchema()); + } + + /** + * Sets the minimum point used to determine the area to draw. + * @param min The left top corner. + * @return This, as per the builder pattern. + */ + public Builder setMinPoint(Point min) { + minPoint = Optional.of(min); + return this; + } + + /** + * Sets the maximum point used to determine the area to draw. + * @param max The right bottom corner. + * @return This, as per the builder pattern. + */ + public Builder setMaxPoint(Point max) { + maxPoint = Optional.of(max); + return this; + } + + /** + * Draws a wide circle around all objects. + * @return This, as per the builder pattern. + */ + public Builder showCircleAroundObjects() { + useEncirclement = true; + return this; + } + + /** + * Associate a {@link RGB} to a {@link Class}. This color association works + * through super classes as well. An example:
+ * consider the following class hierarchy
+ * class A{}
+ * class AA extends A{}
+ * class AAA extends AA{}
+ * When adding a color named C1 to AA, both + * AA and AAA will have color C1. + * When adding another color named C2 to A + * A will have color C2 and AA and + * AAA will have color C1. + * @param type The {@link Class} used as identifier. + * @param rgb The {@link RGB} instance used as color. + * @return This, as per the builder pattern. + */ + public Builder setColorAssociation(Class type, RGB rgb) { + uiSchema.add(type, rgb); + return this; + } + + @Override + public CanvasRenderer build(ModelProvider mp) { + Optional viewRect = Optional.absent(); + if (minPoint.isPresent() && maxPoint.isPresent()) { + viewRect = Optional.of(new ViewRect(minPoint.get(), maxPoint.get())); + } + final RoadModel rm = mp.getModel(RoadModel.class); + + return new RoadUserRenderer(rm, viewRect, uiSchema, useEncirclement); + } + + @Override + public CanvasRendererBuilder copy() { + final Builder copy = new Builder(uiSchema); + copy.useEncirclement = useEncirclement; + copy.minPoint = minPoint; + copy.maxPoint = maxPoint; + return copy; + } } }