]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java
Add customisation possibilities for SelectionNode
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLScenegraph.java
index 1e80b832d70aa15fbc2752772792e8202175a08d..630e41abe6326de3d2fd426a1e94cc012a900e72 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.modeling;
 
 import java.awt.BasicStroke;
+import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.RenderingHints;
 import java.awt.RenderingHints.Key;
@@ -45,6 +46,7 @@ import org.simantics.diagram.elements.DiagramNodeUtil;
 import org.simantics.diagram.elements.TextGridNode;
 import org.simantics.diagram.elements.TextNode;
 import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.g2d.canvas.Hints;
 import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.diagram.DiagramHints;
 import org.simantics.g2d.diagram.IDiagram;
@@ -276,6 +278,23 @@ public class SCLScenegraph {
        }
         return "No nodes in scenegraph!";
     }
+    
+    /**
+     * Returns background color of a canvasContext or null.
+     * @param ctx
+     * @return color in RGBA List<Integer> format, or null.
+     */
+    public static List<Integer> getBackgroundColor(ICanvasContext ctx) {
+       Color color = ctx.getDefaultHintContext().getHint(Hints.KEY_BACKGROUND_COLOR);
+       if (color == null)
+               return null;
+       ArrayList<Integer> rgba = new ArrayList<>(4);
+       rgba.add(color.getRed());
+       rgba.add(color.getGreen());
+       rgba.add(color.getBlue());
+       rgba.add(color.getAlpha());
+       return rgba;
+    }
 
     public static String sceneGraphTest (ICanvasContext ctx, String module, String value) {
        
@@ -378,12 +397,14 @@ public class SCLScenegraph {
 
                public static final String svgNS = "http://www.w3.org/2000/svg";
 
-               public Generator(SVGGeneratorContext ctx, boolean joku) {
-                       super(ctx, joku);
+               public Generator(SVGGeneratorContext ctx, boolean textAsShapes) {
+                       super(ctx, textAsShapes);
                }
 
                public Generator(Document document) {
                        super(document);
+                       // prevent batik comment in each g-element
+                       getGeneratorContext().setComment(null);
                }
 
                @Override
@@ -623,6 +644,7 @@ public class SCLScenegraph {
 
                RenderSVGContext result = new RenderSVGContext();
 
+               double[] matrix = new double[6];
                try {
 
             Selection selection = ctx.getAtMostOneItemOfClass(Selection.class);
@@ -649,10 +671,15 @@ public class SCLScenegraph {
                        
                        double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight());
                
-                       svgGenerator.translate(offset, offset);
-                       svgGenerator.scale(scale, scale);
+                       AffineTransform tr = new AffineTransform();
+                       tr.translate(offset, offset);
+                       tr.scale(scale, scale);
+                       tr.translate(-content.getX(), -content.getY());
+                       tr.getMatrix(matrix);
+                       //svgGenerator.translate(offset, offset);
+                       //svgGenerator.scale(scale, scale);
                        // translate svgGenerator to the x and y coordinates of current content
-                       svgGenerator.translate(-content.getX(), -content.getY());
+                       //svgGenerator.translate(-content.getX(), -content.getY());
 
                        svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset));
                        //svgGenerator.setClip(content);
@@ -681,9 +708,11 @@ public class SCLScenegraph {
 
                StringBuilder res = new StringBuilder();
                res.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\">");
+               res.append("<g transform=\"matrix(").append(matrix[0]).append(",").append(matrix[1]).append(",").append(matrix[2]).append(",").append(matrix[3]).append(",").append(matrix[4]).append(",").append(matrix[5]).append(")\">");
                res.append(result.get(MAIN_SECTION));
                res.append(result.get(SELECTION_SECTION));
                res.append(result.get(SELECTION_MASK_SECTION));
+               res.append("</g>");
                res.append("</svg>");
 
 //             System.err.println(" == FINAL RESULT == ");
@@ -792,7 +821,7 @@ public class SCLScenegraph {
                 parentBuilder.append(SELECTION_SECTION, "\n</g>");
                 parentBuilder.append(MAIN_SECTION, "\n</g>");
                 
-            } else if (node instanceof SelectionNode) {
+            } else if (isSelection0(node)) {
                 
                 SelectionNode n = (SelectionNode)node;
                 SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class);
@@ -808,11 +837,7 @@ public class SCLScenegraph {
                     parentBuilder2.append(SELECTION_SECTION, "\n<g style=\"visibility:hidden\" class=\"selection\" id=\"" + key + "\">");
                     parentBuilder2.append(SELECTION_SECTION, svg);
                     parentBuilder2.append(SELECTION_SECTION, "\n</g>");
-                    AffineTransform transform = svgGenerator.getTransform();
-                    double[] matrix = new double[6];
-                    transform.getMatrix(matrix);
-                    String matrixString = String.format("matrix(%f,%f,%f,%f,%f,%f)", matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
-                    parentBuilder2.append(SELECTION_MASK_SECTION, "\n<g class=\"selectionMask\" id=\"" + key + "\" transform=\"" + matrixString + "\">");
+                    parentBuilder2.append(SELECTION_MASK_SECTION, "\n<g class=\"selectionMask\" id=\"" + key /*+ "\" transform=\"" + matrixString + "\"*/+ "\">");
                     Rectangle2D rect = n.getRect();
                     // NaN
                     if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) {
@@ -824,6 +849,14 @@ public class SCLScenegraph {
                     parentBuilder2.append(SELECTION_MASK_SECTION,"\n</g>");
                    
                 }
+            } else if (node instanceof SelectionNode) {
+
+                Element doc = renderSVGNode(svgGenerator, (IG2DNode)node);
+                String svg = printSVGDocument(doc);
+                parentBuilder.append(MAIN_SECTION, "<g>");
+                parentBuilder.append(MAIN_SECTION, svg);
+                parentBuilder.append(MAIN_SECTION, "\n</g>");
+
             } else if (node instanceof SVGNode) {
                 SVGNode svg = (SVGNode)node;
                 parentBuilder.append(MAIN_SECTION, svg.getSVGText());
@@ -857,6 +890,17 @@ public class SCLScenegraph {
 
         }
         
+        private boolean isSelection0(IG2DNode node) {
+            
+            if(node instanceof SelectionNode) {
+                SelectionNode sn = (SelectionNode)node;
+                return sn.getSelectionId() == 0;
+            } else {
+                return false;
+            }
+            
+        }
+        
         private RenderSVGContext getParentBuilder(IG2DNode node) {
             
             INode parentSEN = NodeUtil.getNearestParentOfType(node, SingleElementNode.class);