From 5b16b1d94b0c046803f8b246d32b74f289eca646 Mon Sep 17 00:00:00 2001 From: Jussi Koskela Date: Wed, 24 Oct 2018 14:52:35 +0300 Subject: [PATCH] Move state changes away from render method to refresh method Backwards compatibility is preserved by calling refresh for each node at the beginning of rendering. This is performed automatically in G2DSceneGraph.render contrary to original requirement. This change is required to support external diagram renderers. Change-Id: I5d91ee97b0c9527f6d4ba562089ffaebdbc01ce0 (cherry picked from commit 512d469c3e490cdfc9a10fbbd6efe92ae6c0358a) --- .../simantics/diagram/elements/TextGridNode.java | 14 +++++++------- .../src/org/simantics/scenegraph/g2d/G2DNode.java | 4 ++++ .../simantics/scenegraph/g2d/G2DParentNode.java | 10 ++++++++++ .../simantics/scenegraph/g2d/G2DSceneGraph.java | 10 ++++++++-- .../src/org/simantics/scenegraph/g2d/IG2DNode.java | 12 +++++++++--- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextGridNode.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextGridNode.java index be0f23a59..20d419ef0 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextGridNode.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextGridNode.java @@ -265,13 +265,13 @@ public class TextGridNode extends G2DParentNode implements Decoration { } @Override - public void render(Graphics2D g2d) { - Vec2d delta = getDelta(FACTOR); - if(delta != null) - setTransform(AffineTransform.getTranslateInstance(delta.x, delta.y-2.1*computeRows()*(up ? 1.0 : 0.0))); - else - setTransform(AffineTransform.getTranslateInstance(0, -2.1*computeRows()*(up ? 1.0 : 0.0))); - super.render(g2d); + public void refresh() { + Vec2d delta = getDelta(FACTOR); + if(delta != null) + setTransform(AffineTransform.getTranslateInstance(delta.x, delta.y-2.1*computeRows()*(up ? 1.0 : 0.0))); + else + setTransform(AffineTransform.getTranslateInstance(0, -2.1*computeRows()*(up ? 1.0 : 0.0))); + super.refresh(); } @Override diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DNode.java index b39d7a058..477b95517 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DNode.java @@ -69,6 +69,10 @@ public abstract class G2DNode extends Node implements IG2DNode { return true; } + @Override + public void refresh() { + } + @Override public void cleanup() { retractMapping(); diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DParentNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DParentNode.java index 838eb893e..de4631e77 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DParentNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DParentNode.java @@ -122,6 +122,15 @@ public class G2DParentNode extends ParentNode implements IG2DNode, Ini g2d.setTransform(ot); } + @Override + public void refresh() { + for (IG2DNode node : getSortedNodes()) { + if (node.validate()) { + node.refresh(); + } + } + } + @Override public void accept(IG2DNodeVisitor visitor) { visitor.enter(this); @@ -602,4 +611,5 @@ public class G2DParentNode extends ParentNode implements IG2DNode, Ini public void synchronizeTransform(double[] data) { this.setTransform(new AffineTransform(data)); } + } diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java index 204181c37..9055a8efe 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java @@ -105,9 +105,9 @@ public class G2DSceneGraph extends G2DParentNode implements ILookupService, INod return focusNode; } - @Override + @Override public void render(Graphics2D g2d) { - performCleanup(); + refresh(); Component rootPane = getRootPane(); if (rootPane != null) g2d.setRenderingHint(G2DRenderingHints.KEY_COMPONENT, rootPane); @@ -116,6 +116,12 @@ public class G2DSceneGraph extends G2DParentNode implements ILookupService, INod } } + @Override + public void refresh() { + performCleanup(); + super.refresh(); + } + /** * Util method for executing updates to scenegraph tree * NOTE: You should really consider performance issues when using this diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/IG2DNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/IG2DNode.java index e181d59ff..2d17e47c5 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/IG2DNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/IG2DNode.java @@ -50,8 +50,9 @@ public interface IG2DNode extends INode, IEventHandler { public boolean validate(); /** - * Perform the actual rendering. Node state changes should be avoided in - * this method. This method should not change the transform of the g2d + * Perform the actual rendering. Node state changes must be avoided in + * this method. Implement node state changes to refresh method instead. + * This method should not change the transform of the g2d * instance. You should call g2d.create or copy the original transform and * put it back at the end of this method according to what is specified in * {@link Graphics2D#setTransform(AffineTransform)}. @@ -70,7 +71,12 @@ public interface IG2DNode extends INode, IEventHandler { * @see #getTransform() */ public void render(Graphics2D g2d); - + + /** + * Refresh the node state. + */ + public void refresh(); + /** * Visit the IG2DNode substructure of this node using the provided visitor. * -- 2.47.0