X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Felementclass%2FShapeClass.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Felementclass%2FShapeClass.java;h=0afa2bc0251e34f275010a00c1b1babce16a29b8;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java new file mode 100644 index 000000000..0afa2bc02 --- /dev/null +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g2d.elementclass; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Shape; +import java.awt.geom.AffineTransform; +import java.awt.geom.Ellipse2D; +import java.awt.geom.Rectangle2D; + +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.element.handler.Outline; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.element.handler.impl.BorderColorImpl; +import org.simantics.g2d.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.FillColorImpl; +import org.simantics.g2d.element.handler.impl.TextImpl; +import org.simantics.g2d.element.handler.impl.TextPainter; +import org.simantics.g2d.elementclass.wheel.RotatorHandler; +import org.simantics.scenegraph.Node; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + +/** + * @author Toni Kalajainen + */ +public class ShapeClass { + + final static BasicStroke STROKE = new BasicStroke(1.0f); + + public static final ElementClass CIRCLE_CLASS = getShapeClass(new Ellipse2D.Double(-10, -10, 20, 20)); + + public static ElementClass getShapeClass(Shape s) + { + return + ElementClass.compile( + DefaultTransform.INSTANCE, + BorderColorImpl.BLACK, + FillColorImpl.WHITE, + TextImpl.INSTANCE, + new ShapePainter(s), + new TextPainter(), + new RotatorHandler() + ); + + } + + static class ShapePainter implements SceneGraph, Outline, InternalSize + { + /** + * + */ + private static final long serialVersionUID = 2328124349273536527L; + Shape shape; + Rectangle2D bounds; + + public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE"); + + public ShapePainter(Shape shape) { + this.shape = shape; + this.bounds = shape.getBounds2D(); + } + + @Override + public void cleanup(IElement e) { + Node node = e.removeHint(SG_NODE); + if (node != null) + node.remove(); + } + + @Override + public void init(IElement e, G2DParentNode parent) { + G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE); + if (node == null) { + node = parent.addNode(G2DParentNode.class); + e.setHint(SG_NODE, node); + } + AffineTransform at = ElementUtils.getTransform(e); + if(at != null) node.setTransform(at); + + ShapeNode bgnode = node.getOrCreateNode("bgnode", ShapeNode.class); + ShapeNode fgnode = node.getOrCreateNode("fgnode", ShapeNode.class); + + Color fc = ElementUtils.getFillColor(e); + Color bc = ElementUtils.getBorderColor(e); + + fgnode.setColor(fc); + fgnode.setFill(true); + fgnode.setShape(shape); + + bgnode.setColor(bc); + bgnode.setStroke(STROKE); + bgnode.setScaleStroke(true); + bgnode.setShape(shape); + } + + @Override + public Shape getElementShape(IElement e) { + return shape; + } + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + if (size==null) size = new Rectangle2D.Double(); + size.setFrame(bounds); + return size; + } + } +}