From c1065ec175a69dcb20e89660b630cc5768e0a0c2 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 1 Nov 2011 11:09:54 +0000 Subject: [PATCH] Correct bounds for HoverTextNodes even in the initialization phase when flows are initialized before text nodes git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23142 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/elements2/HoverTextElementHandler.java | 25 +++++++++++-- .../elements2/HoverTextElementNoBounds.java | 4 ++- .../RouteFlowConnectionFactory.java | 35 ++----------------- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java index 713a1022..9063b203 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java @@ -11,8 +11,13 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2; +import java.awt.Font; +import java.awt.font.FontRenderContext; +import java.awt.font.TextLayout; +import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.handler.InternalSize; import org.simantics.g2d.utils.Alignment; @@ -20,7 +25,7 @@ import org.simantics.g2d.utils.Alignment; public class HoverTextElementHandler extends HoverTextElementNoBounds implements InternalSize { private static final long serialVersionUID = 8800738238681432901L; - + public static final HoverTextElementHandler INSTANCE = new HoverTextElementHandler(); public HoverTextElementHandler() { @@ -47,8 +52,22 @@ public class HoverTextElementHandler extends HoverTextElementNoBounds implements size = new Rectangle2D.Double(); if (node != null) size.setRect(node.getBoundsInLocal()); - else - size.setFrame(0, 0, 0, 0); + else { + String text = e.getHint(ElementHints.KEY_TEXT); + Font font = e.getHint(ElementHints.KEY_FONT); + if(text == null || font == null) + size.setFrame(0, 0, 0, 0); + else { + FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true); + TextLayout tl = new TextLayout(text, font, FRC); + Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D(); + size.setFrame( + bounds.getX() * SCALE - paddingX, + bounds.getY() * SCALE -paddingY, + bounds.getWidth()* SCALE + paddingX + paddingX, + bounds.getHeight()* SCALE + paddingY + paddingY); + } + } return size; } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java index 61a7a398..7e2f3545 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java @@ -154,6 +154,8 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { } }; } + + public static double SCALE = 0.235; @Override public void init(final IElement e, G2DParentNode parent) { @@ -167,7 +169,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { String text = ElementUtils.getText(e); AffineTransform at = ElementUtils.getTransform(e); Alignment hAlign = ElementUtils.getHintOrDefault(e, ElementHints.KEY_HORIZONTAL_ALIGN, horizontalAlignment); - node.init(text, font, color, originX, originY, 0.235); + node.init(text, font, color, originX, originY, SCALE); node.setBackgroundColor(fillColor); node.setBorderColor(borderColor); node.setHorizontalAlignment((byte) hAlign.ordinal()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/RouteFlowConnectionFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/RouteFlowConnectionFactory.java index f9097492..9a2691f2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/RouteFlowConnectionFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/RouteFlowConnectionFactory.java @@ -84,13 +84,8 @@ import org.simantics.sysdyn.ui.elements2.ValveFactory.ValveSceneGraph; * An element class for Sysdyn Flow elements. * Copied from RouteGraphConnectionClassFactory and adapted to Flow needs * - * @author Tuukka Lehtonen + * @author Teemu Lempinen * - * - * -ts. oma ehdotukseni on tämä: -- etsi miten pystyt syöttämään jokaiselle sysdynin "terminaalille" oman ILineEndStyle:n joka sisältää viitteen mokkulan mittoihin -- lisää direction-parametri getLineEndLength:iin niin että voit palauttaa siellä mokkulan mittojen mukaisen etäisyyden */ public class RouteFlowConnectionFactory extends SyncElementFactory { @@ -226,8 +221,8 @@ public class RouteFlowConnectionFactory extends SyncElementFactory { // Get element bounds to decide allowed terminal direction(s) IElement te = graph.syncRequest(DiagramRequests.getElement(canvas, diagram, terminalElement, null)); - ElementUtils.getElementBounds(te, bounds); + { Shape shp = org.simantics.g2d.utils.GeometryUtils.transformShape(bounds, terminalElementTr); bounds.setFrame(shp.getBounds2D()); @@ -428,32 +423,6 @@ public class RouteFlowConnectionFactory extends SyncElementFactory { } } -// private int directionSetToToDirectionMask(DirectionSet directions) { -// if (directions == DirectionSet.ANY) -// return 0xf; -// int mask = 0; -// for (Double d : directions) { -// mask |= compassAngleToToDirectionMask(d); -// } -// return mask; -// } -// -// private int compassAngleToToDirectionMask(double compassAngle) { -// Double d = DirectionSet.NESW.getClosestDirection(compassAngle); -// int id = (int) Math.round(d); -// // bits: -// // 0 right (1,0) -// // 1 down (0,1) -// // 2 left (-1,0) -// // 3 up (0,-1) -// switch (id) { -// case 0: return (1 << 3); -// case 90: return (1 << 2); -// case 180: return (1 << 1); -// case 270: return (1 << 0); -// } -// return 0xf; -// } protected void scheduleSynchronize(Session session, Resource connection, RouteGraphChangeEvent event) { session.asyncRequest(RouteGraphConnection.synchronizer(connection, event)); -- 2.47.1