From 5b2197c6953403d52dd90cfc031f5a597ee945c9 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Thu, 18 Jul 2019 13:05:25 +0300 Subject: [PATCH] Adjustments to map diagram info label rendering * 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 --- .../simantics/maps/MapScalingTransform.java | 3 +- .../network/ui/NetworkDrawingParticipant.java | 22 ++++++---- .../network/ui/nodes/DeferredNode.java | 7 +++ .../ui/nodes/DeferredRenderingNode.java | 43 +++++++++++++++++++ .../nodes/DistrictNetworkHoverInfoNode.java | 28 +++++++++++- .../nodes/DistrictNetworkStaticInfoNode.java | 32 +++++++++----- .../ui/nodes/DistrictNetworkVertexNode.java | 9 ++++ .../network/ui/nodes/HoverSensitiveNode.java | 3 ++ .../styles/DistrictNetworkHoverInfoStyle.java | 14 +++++- .../DistrictNetworkStaticInfoStyle.java | 12 ++++++ 10 files changed, 151 insertions(+), 22 deletions(-) create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredNode.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredRenderingNode.java diff --git a/org.simantics.district.maps/src/org/simantics/maps/MapScalingTransform.java b/org.simantics.district.maps/src/org/simantics/maps/MapScalingTransform.java index e29e8e55..7a47777d 100644 --- a/org.simantics.district.maps/src/org/simantics/maps/MapScalingTransform.java +++ b/org.simantics.district.maps/src/org/simantics/maps/MapScalingTransform.java @@ -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(); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java index 91e48251..bd81bd01 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java @@ -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 elements, boolean hover, boolean isConnectionTool, boolean changed) { + private boolean hoverVertexNodes(List 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 elements, boolean hover, boolean isConnectionTool, boolean changed) { + private boolean hoverEdgeNodes(List 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 index 00000000..c2afab76 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredNode.java @@ -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 index 00000000..f2430718 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DeferredRenderingNode.java @@ -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> queue = new LinkedList<>(); + + @Override + public void render(Graphics2D g2d) { + AffineTransform currentTransform = g2d.getTransform(); + + ListIterator> listIterator = queue.listIterator(); + while (listIterator.hasNext()) { + Pair 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)); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkHoverInfoNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkHoverInfoNode.java index 4fc1beba..7a75ad5f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkHoverInfoNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkHoverInfoNode.java @@ -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 labels; + @SuppressWarnings("unused") private Point2D origin; private boolean hover = false; + + private Point2D mousePosition; private static AtomicReference 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(); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java index 347261c4..b86d0acb 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java @@ -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); } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java index 0533e311..061a883e 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java @@ -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; diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/HoverSensitiveNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/HoverSensitiveNode.java index bf767851..cd6631be 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/HoverSensitiveNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/HoverSensitiveNode.java @@ -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) {}; } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkHoverInfoStyle.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkHoverInfoStyle.java index c5e3a6f6..77be9c90 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkHoverInfoStyle.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkHoverInfoStyle.java @@ -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 { - 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 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()); } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java index 8a4a143c..27c2fc84 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java @@ -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 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; -- 2.47.1