]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Adjustments to map diagram info label rendering
authorReino Ruusu <reino.ruusu@semantum.fi>
Thu, 18 Jul 2019 10:05:25 +0000 (13:05 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Sat, 31 Aug 2019 20:08:43 +0000 (23:08 +0300)
* Removed white background from static info labels
* Hover info rendered at cursor location
* Info labels drawn on top of map elements

gitlab #44
gitlab #47

Change-Id: I9dda11b80cfe76bae1605ddb46f7d3ece183c719

org.simantics.district.maps/src/org/simantics/maps/MapScalingTransform.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredNode.java [new file with mode: 0644]
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredRenderingNode.java [new file with mode: 0644]
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkHoverInfoNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/HoverSensitiveNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkHoverInfoStyle.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java

index e29e8e553fc9fadc90c326dcb28f704c3fabf171..7a47777dfec4cfe238e8f51267921494dce93f08 100644 (file)
@@ -7,7 +7,8 @@ public class MapScalingTransform {
     private MapScalingTransform() {
     }
     public static final double scale = 256.0d / 360.0d;
-    public static final AffineTransform INSTANCE = new AffineTransform(scale, 0, 0, scale, 0, 0);
+    public static final AffineTransform INSTANCE = AffineTransform.getScaleInstance(scale, scale);
+    public static final AffineTransform INVERSE = AffineTransform.getScaleInstance(1/scale, 1/scale);
 
     public static double getScaleX() {
         return INSTANCE.getScaleX();
index 91e48251bb7ed89efc0cf9fb5625a46b6dbbe335..bd81bd01aac6e0683c2ec904c0987143a2578322 100644 (file)
@@ -28,7 +28,9 @@ import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
 
 public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
 
-    @Dependency
+    public static final String NETWORK_DRAWING_NODE = "networkDrawingNode";
+
+       @Dependency
     PickContext pick;
     
     /**
@@ -53,7 +55,7 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
 
     @SGInit
     public void initSG(G2DParentNode parent) {
-        node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class);
+        node = parent.addNode(NETWORK_DRAWING_NODE, NetworkDrawingNode.class);
         node.setTransform(transform);
         node.setNetworkDrawingParticipant(this);
     }
@@ -73,30 +75,34 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
         // snap.removeAll(pickables);
         
         boolean changed = false;
-        changed = hoverVertexNodes(snap, false, isConnectionTool, changed);
-        changed = hoverEdgeNodes(snap, false, isConnectionTool, changed);
-        changed = hoverVertexNodes(pickables, true, isConnectionTool, changed);
-        changed = hoverEdgeNodes(pickables, true, isConnectionTool, changed);
+        changed = hoverVertexNodes(snap, false, isConnectionTool, changed, currentMousePos);
+        changed = hoverEdgeNodes(snap, false, isConnectionTool, changed, currentMousePos);
+        changed = hoverVertexNodes(pickables, true, isConnectionTool, changed, currentMousePos);
+        changed = hoverEdgeNodes(pickables, true, isConnectionTool, changed, currentMousePos);
         return changed;
     }
 
-    private boolean hoverVertexNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, boolean changed) {
+    private boolean hoverVertexNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, boolean changed, Point2D p) {
         for (IElement elem : elements) {
             Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
             if (node instanceof DistrictNetworkVertexNode) {
                 changed = ((DistrictNetworkVertexNode) node).hover(hover, isConnectionTool) || changed;
+                if (hover)
+                    ((DistrictNetworkVertexNode) node).setMousePosition(p);
             }
         }
         return changed;
     }
     
-    private boolean hoverEdgeNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, boolean changed) {
+    private boolean hoverEdgeNodes(List<IElement> elements, boolean hover, boolean isConnectionTool, boolean changed, Point2D p) {
         for (IElement elem : elements) {
             Node node = elem.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
             if (node instanceof DistrictNetworkEdgeNode) {
                 for (IG2DNode n : ((DistrictNetworkEdgeNode) node).getNodes()) {
                     if (n instanceof HoverSensitiveNode) {
                         changed = ((HoverSensitiveNode)n).hover(hover, isConnectionTool) || changed;
+                        if (hover)
+                            ((HoverSensitiveNode)n).setMousePosition(p);
                     }
                 }
             }
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredNode.java
new file mode 100644 (file)
index 0000000..c2afab7
--- /dev/null
@@ -0,0 +1,7 @@
+package org.simantics.district.network.ui.nodes;
+
+import java.awt.Graphics2D;
+
+public interface DeferredNode {
+       void renderDeferred(Graphics2D g);
+}
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredRenderingNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredRenderingNode.java
new file mode 100644 (file)
index 0000000..f243071
--- /dev/null
@@ -0,0 +1,43 @@
+package org.simantics.district.network.ui.nodes;
+
+import java.awt.Graphics2D;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.simantics.scenegraph.g2d.G2DNode;
+import org.simantics.utils.datastructures.Pair;
+
+public class DeferredRenderingNode extends G2DNode {
+
+       private static final long serialVersionUID = 1L;
+       
+       List<Pair<AffineTransform, DeferredNode>> queue = new LinkedList<>();
+       
+       @Override
+       public void render(Graphics2D g2d) {
+               AffineTransform currentTransform = g2d.getTransform();
+               
+               ListIterator<Pair<AffineTransform, DeferredNode>> listIterator = queue.listIterator();
+               while (listIterator.hasNext()) {
+                       Pair<AffineTransform, DeferredNode> node = listIterator.next();
+                       listIterator.remove();
+                       
+                       g2d.setTransform(node.first);
+                       node.second.renderDeferred(g2d);
+               }
+               
+               g2d.setTransform(currentTransform);
+       }
+
+       @Override
+       public Rectangle2D getBoundsInLocal() {
+               return null;
+       }
+
+       public void deferNode(AffineTransform transform, DeferredNode node) {
+               queue.add(new Pair<>(transform, node));
+       }
+}
index 4fc1bebae4ec12f3e013ff1f42ad187c11589447..7a75ad5f2e4382390f798ebc0ea38ea7c7614da5 100644 (file)
@@ -11,13 +11,17 @@ import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReference;
 
+import org.simantics.district.network.ui.styles.DistrictNetworkHoverInfoStyle;
 import org.simantics.maps.MapScalingTransform;
+import org.simantics.scenegraph.ParentNode;
 import org.simantics.scenegraph.g2d.G2DNode;
+import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
 import org.simantics.scenegraph.utils.DPIUtil;
+import org.simantics.scenegraph.utils.NodeUtil;
 import org.simantics.scl.runtime.Lists;
 import org.simantics.scl.runtime.tuple.Tuple3;
 
-public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensitiveNode {
+public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensitiveNode, DeferredNode {
 
        private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
 
@@ -31,9 +35,12 @@ public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensit
 
        private List<Tuple3> labels;
 
+       @SuppressWarnings("unused")
        private Point2D origin;
 
        private boolean hover = false;
+
+       private Point2D mousePosition;
        
        private static AtomicReference<DistrictNetworkHoverInfoNode> activeNode = new AtomicReference<>();
 
@@ -42,6 +49,16 @@ public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensit
                if (!hover || activeNode.get() != this)
                        return;
 
+               ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
+               DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(DistrictNetworkHoverInfoStyle.HOVER_INFO_DEFERRED) : null;
+               if (deferred != null)
+                       deferred.deferNode(g.getTransform(), this);
+               else
+                       renderDeferred(g);
+       }
+       
+       @Override
+       public void renderDeferred(Graphics2D g) {
                AffineTransform ot = g.getTransform();
                Font of = g.getFont();
                doRender(g);
@@ -50,7 +67,9 @@ public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensit
        }
 
        private void doRender(Graphics2D g) {
-               g.translate(origin.getX(), origin.getY());
+               g.transform(MapScalingTransform.INVERSE);
+               g.translate(mousePosition.getX(), mousePosition.getY());
+               //g.translate(origin.getX(), origin.getY());
                double scale = 1.5 / g.getTransform().getScaleX();
                g.scale(scale, scale);
 
@@ -112,6 +131,11 @@ public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensit
                return changed;
        }
        
+       @Override
+       public void setMousePosition(Point2D p) {
+               mousePosition = p;
+       }
+       
        @Override
        public void delete() {
                super.delete();
index 347261c43a9dc462e8e4c56b77947c9eaa26690a..b86d0acb291b7c9355e97b9f5936154016861115 100644 (file)
@@ -9,14 +9,16 @@ import java.awt.geom.NoninvertibleTransformException;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 
-import org.simantics.district.network.DistrictNetworkUtil;
-import org.simantics.district.network.ui.DistrictNetworkUIUtil;
+import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle;
 import org.simantics.maps.MapScalingTransform;
+import org.simantics.scenegraph.ParentNode;
 import org.simantics.scenegraph.g2d.G2DNode;
 import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
 import org.simantics.scenegraph.utils.DPIUtil;
+import org.simantics.scenegraph.utils.NodeUtil;
 
-public class DistrictNetworkStaticInfoNode extends G2DNode {
+public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
 
        private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
        
@@ -32,6 +34,16 @@ public class DistrictNetworkStaticInfoNode extends G2DNode {
        
        @Override
        public void render(Graphics2D g) {
+               ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
+               DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(DistrictNetworkStaticInfoStyle.STATIC_INFO_DEFERRED) : null;
+               if (deferred != null)
+                       deferred.deferNode(g.getTransform(), this);
+               else
+                       renderDeferred(g);
+       }
+
+       @Override
+       public void renderDeferred(Graphics2D g) {
                if (info == null || "".equals(info))
                        return;
                
@@ -68,18 +80,18 @@ public class DistrictNetworkStaticInfoNode extends G2DNode {
                double scale = 1.5 / g.getTransform().getScaleX();
                g.scale(scale, scale);
 
-               int width1 = g.getFontMetrics().stringWidth(info);
-               int height = g.getFontMetrics().getHeight();
-               
-               g.setColor(Color.WHITE);
-               g.fillRect(-width1/2 - 5, -2, width1, height);
-               
                g.setFont(FONT);
-               g.setColor(Color.BLACK);
+               int width1 = g.getFontMetrics().stringWidth(info);
+//             int height = g.getFontMetrics().getHeight();
                
                g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
                g.translate(0, 10);
                
+//             g.setColor(Color.WHITE);
+//             g.fillRect(-width1/2 - 5, -height-2, width1+10, height+4);
+               
+               g.setColor(Color.BLACK);
+               
                g.drawString(info, -width1/2, 0);
        }
 
index 0533e3117bf38b32a4f94191139fe55d8876e148..061a883e427f52a3f096098d3db01d0f4c0253d8 100644 (file)
@@ -115,6 +115,7 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti
                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
                 g2dNode.setTransform(symbolTransform);
             }
+            
             g2dNode.render(g2d);
         }
         
@@ -186,6 +187,14 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti
         
         return changed;
     }
+    
+    @Override
+    public void setMousePosition(Point2D p) {
+        for (IG2DNode child : getNodes()) {
+            if (child instanceof HoverSensitiveNode)
+                ((HoverSensitiveNode) child).setMousePosition(p);
+        }
+    }
 
     public void setColor(Color color) {
         this.color = color;
index bf7678517f26f5939e342a2c88489d978c49bf79..cd6631bed044296e3b8006ef35a5e6bf20b14d82 100644 (file)
@@ -1,5 +1,8 @@
 package org.simantics.district.network.ui.nodes;
 
+import java.awt.geom.Point2D;
+
 public interface HoverSensitiveNode {
     boolean hover(boolean hover, boolean isConnectionTool);
+    default void setMousePosition(Point2D p) {};
 }
index c5e3a6f6a19dc7df6bd50092b20b6f7cac3a5bc5..77be9c90cc392a37df569b5c917b31e6ad61b76f 100644 (file)
@@ -21,13 +21,17 @@ import org.simantics.diagram.profile.StyleBase;
 import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.district.network.ui.nodes.DeferredRenderingNode;
 import org.simantics.district.network.ui.nodes.DistrictNetworkHoverInfoNode;
 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingResources;
 import org.simantics.scenegraph.INode;
+import org.simantics.scenegraph.ParentNode;
+import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
 import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
+import org.simantics.scenegraph.utils.NodeUtil;
 import org.simantics.scl.compiler.top.ValueNotFound;
 import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.SCLContext;
@@ -39,7 +43,9 @@ import org.slf4j.LoggerFactory;
 
 public class DistrictNetworkHoverInfoStyle extends StyleBase<DistrictNetworkHoverInfoStyle.StyleResult> {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkHoverInfoStyle.class);
+    public static final String HOVER_INFO_DEFERRED = "hoverInfo";
+
+       private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkHoverInfoStyle.class);
 
        private static final String ACTIONS_MODULE = "Actions";
        private static final String HOVER_CONTRIBUTION = "hoverContribution";
@@ -147,6 +153,12 @@ public class DistrictNetworkHoverInfoStyle extends StyleBase<DistrictNetworkHove
                if (node == null)
                        return;
                
+               ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
+               if (root != null) {
+                       DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", HOVER_INFO_DEFERRED, DeferredRenderingNode.class, observer);
+                       deferred.setZIndex(Integer.MAX_VALUE);
+               }
+               
                node.setLabels(results.getLabels());
                node.setOrigin(results.getOrigin());
        }
index 8a4a143ce39050875d4d930b2f015420015024c8..27c2fc8496dc4e0908a65b18b32236ca3b794cd2 100644 (file)
@@ -20,12 +20,16 @@ import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.network.profile.MidBranchEdgeSetRequest;
+import org.simantics.district.network.ui.nodes.DeferredRenderingNode;
 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
 import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode;
 import org.simantics.layer0.Layer0;
 import org.simantics.scenegraph.INode;
+import org.simantics.scenegraph.ParentNode;
+import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
 import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
+import org.simantics.scenegraph.utils.NodeUtil;
 import org.simantics.scl.compiler.top.ValueNotFound;
 import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.runtime.SCLContext;
@@ -40,6 +44,8 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkSta
 
        private static final String ACTIONS_MODULE = "Actions";
        private static final String PIPELINE_INFO = "pipelineInfo";
+
+       public static final String STATIC_INFO_DEFERRED = "staticInfo";
        
        public static class StyleResult {
                public StyleResult(Point2D p1, Point2D p2, String info) {
@@ -132,6 +138,12 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkSta
                        return;
                }
                
+               ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
+               if (root != null) {
+                       DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", STATIC_INFO_DEFERRED, DeferredRenderingNode.class, evaluationContext);
+                       deferred.setZIndex(Integer.MAX_VALUE-1);
+               }
+               
                DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
                if (node == null)
                        return;