]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ShapeNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / ShapeNode.java
diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ShapeNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ShapeNode.java
new file mode 100644 (file)
index 0000000..96e7dad
--- /dev/null
@@ -0,0 +1,186 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.g2d.nodes;
+
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.Paint;\r
+import java.awt.Shape;\r
+import java.awt.Stroke;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+import org.simantics.scenegraph.utils.GeometryUtils;\r
+import org.simantics.scenegraph.utils.InitValueSupport;\r
+
+/**
+ * A scene graph node that renders a specified AWT {@link Shape} by optionally
+ * filling or drawing it.
+ * 
+ * If filling is enabled, it will be performed before stroking the shape.
+ * 
+ * @author J-P Laine\r
+ * @author Tuukka Lehtonen
+ * 
+ * TODO: separate paint for stroke and fill
+ */
+public class ShapeNode extends G2DNode implements InitValueSupport {
+
+    private static final long serialVersionUID = 8508750881358776559L;
+\r
+    protected static final Stroke DEFAULT_STROKE = new BasicStroke(2);\r
+
+    protected Shape shape = null;\r
+    protected Stroke stroke = DEFAULT_STROKE;
+    protected Paint color = Color.BLACK;
+    protected boolean fill = false;
+    protected boolean scaleStroke = false;
+    protected boolean scaleShape = false;
+\r
+    protected transient Shape dynamicShape = null;\r
+    protected transient Stroke dynamicStroke = null;\r
+    protected transient Paint dynamicColor = null;\r
+    protected transient Boolean dynamicFill = null;\r
+    protected transient Boolean dynamicScaleStroke = null;\r
+    protected transient Boolean dynamicScaleShape = null;\r
+\r
+    @PropertySetter("shape")
+    @SyncField("shape")
+    public void setShape(Shape shape) {
+        this.shape = shape;
+        repaint();
+    }
+\r
+    @PropertySetter("stroke")
+    @SyncField("stroke")
+    public void setStroke(Stroke stroke) {
+        this.stroke = stroke;
+    }
+\r
+    @PropertySetter("color")
+    @SyncField("color")
+    public void setColor(Paint color) {
+        this.color = color;
+    }
+
+    @SyncField("fill")
+    public void setFill(boolean fill) {
+        this.fill = fill;
+    }
+
+    @SyncField("scaleStroke")
+    public void setScaleStroke(boolean scaleStroke) {
+        this.scaleStroke = scaleStroke;
+    }
+
+    @SyncField("scaleShape")
+    public void setScaleShape(boolean scaleShape) {
+        this.scaleShape = scaleShape;
+    }
+
+    @Override
+    public void render(Graphics2D g2d) {
+        Shape shape = dynamicShape != null ? dynamicShape : this.shape;\r
+        if (shape == null)
+            return;
+\r
+        AffineTransform ot = setupRender(g2d);\r
+        renderShape(g2d, shape);\r
+        if (ot != null)\r
+            g2d.setTransform(ot);
+    }
+
+    /**\r
+     * @param g2d\r
+     * @return current transform\r
+     */\r
+    protected AffineTransform setupRender(Graphics2D g2d) {\r
+        AffineTransform old = null;
+        if (!transform.isIdentity()) {\r
+            old = g2d.getTransform();\r
+            g2d.transform(transform);\r
+        }\r
+\r
+        Paint color = dynamicColor != null ? dynamicColor : this.color;
+        if (color != null) g2d.setPaint(color);\r
+\r
+        Stroke stroke = dynamicStroke != null ? dynamicStroke : this.stroke;
+        if (stroke != null) {\r
+            boolean scaleStroke = Boolean.TRUE.equals(dynamicScaleStroke) ? true : this.scaleStroke;
+            if (scaleStroke && stroke instanceof BasicStroke) {
+                BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
+                g2d.setStroke(bs);
+            } else {
+                g2d.setStroke(stroke);
+            }
+        }\r
+
+        boolean scaleShape = Boolean.TRUE.equals(dynamicScaleShape) ? true : this.scaleShape;\r
+        if (scaleShape) {
+            double xs = g2d.getTransform().getScaleX();
+            double ys = g2d.getTransform().getScaleY();
+            g2d.scale(1/xs, 1/ys);
+        }\r
+\r
+        return old;\r
+    }
+
+    protected void renderShape(Graphics2D g2d, Shape s) {\r
+        boolean fill = Boolean.TRUE.equals(dynamicFill) ? true : this.fill;\r
+        if (fill)
+            g2d.fill(s);\r
+
+        Stroke stroke = dynamicStroke != null ? dynamicStroke : this.stroke;\r
+        if (stroke != null)
+            g2d.draw(s);
+    }
+
+    @Override
+    public Rectangle2D getBoundsInLocal() {
+        if(shape == null) return null;
+        return shape.getBounds2D();
+    }\r
+\r
+    public void setValue(String key, Object value) {\r
+        if ("shape".equals(key))\r
+            dynamicShape = (Shape) value;\r
+        else if ("stroke".equals(key))\r
+            dynamicStroke = (Stroke) value;\r
+        else if ("color".equals(key))\r
+            dynamicColor = (Paint) value;\r
+        else if ("fill".equals(key))\r
+            dynamicFill = (Boolean) value;\r
+        else if ("scaleStroke".equals(key))\r
+            dynamicScaleStroke = (Boolean) value;\r
+        else if ("scaleShape".equals(key))\r
+            dynamicScaleShape = (Boolean) value;\r
+//        else super.setValue(key, value);\r
+    }\r
+\r
+    @Override\r
+    public void initValues() {\r
+        dynamicShape = null;\r
+        dynamicStroke = null;\r
+        dynamicColor = null;\r
+        dynamicFill = null;\r
+        dynamicScaleStroke = null;\r
+        dynamicScaleShape = null;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return super.toString() + " [shape=" + shape + ",color=" + color + "]";\r
+    }\r
+
+}