comparison java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/TriangularNode.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78b7fc5391a2
1 /*
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
3
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package uk.ac.qmul.eecs.ccmi.simpletemplate;
21
22 import java.awt.Shape;
23 import java.awt.geom.AffineTransform;
24 import java.awt.geom.GeneralPath;
25 import java.awt.geom.Path2D;
26 import java.awt.geom.Point2D;
27 import java.awt.geom.Rectangle2D;
28 import java.awt.geom.Rectangle2D.Double;
29 import java.io.InputStream;
30 import java.util.List;
31
32 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
33 import uk.ac.qmul.eecs.ccmi.gui.Direction;
34 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
35
36 /**
37 *
38 * A triangular shaped diagram node.
39 *
40 */
41 @SuppressWarnings("serial")
42 public class TriangularNode extends SimpleShapeNode {
43
44
45 public TriangularNode(String typeName, NodeProperties properties) {
46 super(typeName, properties);
47 Rectangle2D dataBounds = getMinBounds();
48 dataDisplayBounds.setFrame(dataBounds);
49 tShape = getOutShape(dataBounds);
50 /* by building the shape around dataBounds which was at (0,0) the new bounds */
51 /* are now negative, so we need to bring the new bounds back at (0,0) */
52 Rectangle2D bounds = getBounds();
53 translateImplementation(new Point2D.Double(),0-bounds.getX(),0-bounds.getY());
54 }
55
56 @Override
57 protected Rectangle2D getMinBounds(){
58 Rectangle2D minBounds = super.getMinBounds();
59 return new Rectangle2D.Double(minBounds.getX(),minBounds.getY(),minBounds.getWidth()/2,minBounds.getHeight()/2);
60 }
61
62 @Override
63 public ShapeType getShapeType() {
64 return ShapeType.Triangle;
65 }
66
67 @Override
68 protected void translateImplementation(Point2D p, double dx, double dy){
69 /* if we clicked on a property node, just move that one */
70 for(List<PropertyNode> pnList : propertyNodesMap.values())
71 for(PropertyNode pn : pnList)
72 if(pn.contains(p)){
73 pn.translate(dx, dy);
74 return;
75 }
76 super.translateImplementation(p,dx, dy);
77 tShape.transform(AffineTransform.getTranslateInstance(dx, dy));
78 }
79
80 public static Path2D.Double getOutShape(Rectangle2D r){
81 Path2D.Double triangle = new Path2D.Double(GeneralPath.WIND_EVEN_ODD,3);
82 double minEdge = Math.min(r.getWidth(), r.getHeight());
83 triangle.moveTo(r.getCenterX(), r.getY()-minEdge);
84
85 double angle = Math.atan(minEdge/(r.getWidth()/2));
86 double w = r.getHeight()/ Math.tan(angle);
87 triangle.lineTo(r.getX()-w, r.getMaxY());
88 triangle.lineTo(r.getMaxX()+w, r.getMaxY());
89 triangle.closePath();
90 return triangle;
91 }
92
93 @Override
94 protected void reshapeInnerProperties(List<String> insidePropertyTypes){
95 nameLabel = new MultiLineString();
96 nameLabel.setText(getName().isEmpty() ? " " : getName());
97 nameLabel.setBold(true);
98
99 if(!super.anyInsideProperties()){
100 dataDisplayBounds.setFrame(dataDisplayBounds.getX(),
101 dataDisplayBounds.getY(),
102 nameLabel.getBounds().getWidth(),
103 nameLabel.getBounds().getHeight());
104 Rectangle2D minBounds = getMinBounds();
105 dataDisplayBounds.add(new Rectangle2D.Double(dataDisplayBounds.getX(), dataDisplayBounds.getY(), minBounds.getWidth(),minBounds.getHeight()));
106 tShape = getOutShape(dataDisplayBounds);
107 }else {
108 Rectangle2D r = nameLabel.getBounds();
109
110 for(int i=0; i<insidePropertyTypes.size();i++){
111 propertyLabels[i] = new MultiLineString();
112 if(getProperties().getValues(insidePropertyTypes.get(i)).size() == 0){
113 propertyLabels[i].setText(" ");
114 }else{
115 propertyLabels[i].setJustification(MultiLineString.LEFT);
116 String[] a = new String[getProperties().getValues(insidePropertyTypes.get(i)).size()];
117 propertyLabels[i].setText(getProperties().getValues(insidePropertyTypes.get(i)).toArray(a), getProperties().getModifiers(insidePropertyTypes.get(i)));
118 }
119 r.add(new Rectangle2D.Double(r.getX(),r.getMaxY(),propertyLabels[i].getBounds().getWidth(),propertyLabels[i].getBounds().getHeight()));
120 }
121 /* set a gap to uniformly distribute the extra space among property rectangles to reach the minimum bound's height */
122 boundsGap = 0;
123 Rectangle2D.Double minBounds = (Rectangle2D.Double)getMinBounds();
124 if(r.getHeight() < minBounds.height){
125 boundsGap = minBounds.height - r.getHeight();
126 boundsGap /= insidePropertyTypes.size();
127 }
128 r.add(minBounds);
129 dataDisplayBounds.setFrame(new Rectangle2D.Double(dataDisplayBounds.x,dataDisplayBounds.y,r.getWidth(),r.getHeight()));
130
131 tShape = getOutShape(dataDisplayBounds);
132 }
133
134 }
135
136 @Override
137 public Double getBounds() {
138 return (Double)tShape.getBounds2D();
139 }
140
141 @Override
142 public InputStream getSound(){
143 return sound;
144 }
145
146 @Override
147 public Point2D getConnectionPoint(Direction d) {
148 return calculateConnectionPoint(d,getBounds());
149 }
150
151 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds) {
152 if(d.getX() == 0){
153 return new Point2D.Double(bounds.getCenterX(),
154 d.getY() > 0 ? bounds.getY() : bounds.getMaxY());
155 }
156
157 boolean left = false;
158 boolean right = false;
159 double dirTan = d.getY()/d.getX();
160 double boundsTan = bounds.getHeight()/bounds.getWidth();
161 double alfa = Math.atan(dirTan);
162 double alfaDegrees = Math.toDegrees(alfa);
163
164 if(d.getY() < 0){ //from the top
165 if(alfaDegrees < 0)
166 right = true;
167 else
168 left = true;
169 }else{ //from the bottom
170 if(dirTan < boundsTan && d.getX() > 0)
171 right = true;
172 else if(dirTan > -boundsTan && d.getX() < 0)
173 left = true;
174 }
175
176 if(right){
177 double beta = Math.atan(bounds.getHeight()/(bounds.getWidth()/2));
178 double py = bounds.getHeight()/2;
179 double x = py/ (Math.tan(alfa)-Math.tan(beta));
180 double y = x * Math.tan(alfa);
181 return new Point2D.Double(bounds.getCenterX()-x, bounds.getCenterY()-y);
182 }
183 else if(left){
184 double beta = - Math.atan(bounds.getHeight()/(bounds.getWidth()/2));
185 double py = bounds.getHeight()/2;
186 double x = py/ (Math.tan(alfa)-Math.tan(beta));
187 double y = x * Math.tan(alfa);
188 return new Point2D.Double(bounds.getCenterX()-x, bounds.getCenterY()-y);
189 }
190 else{
191 return new Point2D.Double(
192 bounds.getCenterX() + ((bounds.getHeight()/2) * (d.getX()/d.getY()) ),
193 bounds.getMaxY());
194 }
195 }
196
197 @Override
198 public Shape getShape() {
199 return tShape;
200 }
201
202 public Object clone(){
203 return new TriangularNode(getType(),(NodeProperties)getProperties().clone());
204 }
205
206 private Path2D.Double tShape;
207 private static InputStream sound;
208
209 static {
210 sound = TriangularNode.class.getResourceAsStream("audio/Triangle.mp3");
211 SoundFactory.getInstance().loadSound(sound);
212 }
213 }