Mercurial > hg > accesspd
comparison java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/EllipticalNode.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.Ellipse2D; | |
24 import java.awt.geom.Point2D; | |
25 import java.awt.geom.Rectangle2D; | |
26 import java.io.IOException; | |
27 import java.io.InputStream; | |
28 import java.util.List; | |
29 | |
30 import org.w3c.dom.Document; | |
31 import org.w3c.dom.Element; | |
32 | |
33 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties; | |
34 import uk.ac.qmul.eecs.ccmi.gui.Direction; | |
35 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory; | |
36 | |
37 /** | |
38 * | |
39 * An elliptical shaped diagram node. | |
40 * | |
41 */ | |
42 @SuppressWarnings("serial") | |
43 public class EllipticalNode extends SimpleShapeNode { | |
44 | |
45 public EllipticalNode(String typeName, NodeProperties properties){ | |
46 super(typeName, properties); | |
47 Rectangle2D r = getMinBounds(); | |
48 eShape = new Ellipse2D.Double(0,0,r.getWidth(),r.getHeight()); | |
49 } | |
50 | |
51 @Override | |
52 public ShapeType getShapeType() { | |
53 return ShapeType.Ellipse; | |
54 } | |
55 | |
56 public static Rectangle2D getOutBounds(Rectangle2D r){ | |
57 double h = r.getHeight()/2; | |
58 double w = r.getWidth()/2; | |
59 | |
60 double anglew = Math.atan(h/w); | |
61 double angleh = Math.atan(w/h); | |
62 | |
63 double a = w + h * Math.tan(angleh)/2; | |
64 double b = h + w * Math.tan(anglew)/2; | |
65 | |
66 return new Rectangle2D.Double( | |
67 r.getCenterX() - a, | |
68 r.getCenterY() - b, | |
69 2*a, | |
70 2*b | |
71 ); | |
72 } | |
73 | |
74 @Override | |
75 protected void reshapeInnerProperties(List<String> insidePropertyTypes){ | |
76 super.reshapeInnerProperties(insidePropertyTypes); | |
77 eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds ); | |
78 } | |
79 | |
80 @Override | |
81 public void decode(Document doc, Element nodeTag) throws IOException{ | |
82 super.decode(doc, nodeTag); | |
83 eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds ); | |
84 } | |
85 | |
86 @Override | |
87 protected void translateImplementation(Point2D p,double dx, double dy){ | |
88 /* if we clicked on a property node, just move that one */ | |
89 for(List<PropertyNode> pnList : propertyNodesMap.values()) | |
90 for(PropertyNode pn : pnList) | |
91 if(pn.contains(p)){ | |
92 pn.translate(dx, dy); | |
93 return; | |
94 } | |
95 eShape.setFrame(eShape.getX() + dx, | |
96 eShape.getY() + dy, | |
97 eShape.getWidth(), | |
98 eShape.getHeight()); | |
99 super.translateImplementation(p,dx, dy); | |
100 } | |
101 | |
102 @Override | |
103 public Rectangle2D getBounds() { | |
104 return eShape.getBounds2D(); | |
105 } | |
106 | |
107 @Override | |
108 public Point2D getConnectionPoint(Direction d) { | |
109 return calculateConnectionPoint(d, getBounds()); | |
110 } | |
111 | |
112 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds){ | |
113 double a = bounds.getWidth() / 2; | |
114 double b = bounds.getHeight() / 2; | |
115 double x = d.getX(); | |
116 double y = d.getY(); | |
117 double cx = bounds.getCenterX(); | |
118 double cy = bounds.getCenterY(); | |
119 | |
120 if (a != 0 && b != 0 && !(x == 0 && y == 0)){ | |
121 double t = Math.sqrt((x * x) / (a * a) + (y * y) / (b * b)); | |
122 return new Point2D.Double(cx + x / t, cy + y / t); | |
123 } | |
124 else{ | |
125 return new Point2D.Double(cx, cy); | |
126 } | |
127 } | |
128 | |
129 @Override | |
130 public Shape getShape() { | |
131 return eShape; | |
132 } | |
133 | |
134 @Override | |
135 public InputStream getSound(){ | |
136 return sound; | |
137 } | |
138 | |
139 @Override | |
140 public Object clone(){ | |
141 EllipticalNode n = (EllipticalNode)super.clone(); | |
142 n.eShape = (Ellipse2D.Double)eShape.clone(); | |
143 return n; | |
144 } | |
145 | |
146 private Ellipse2D.Double eShape; | |
147 private static InputStream sound; | |
148 static{ | |
149 sound = EllipticalNode.class.getResourceAsStream("audio/Ellipse.mp3"); | |
150 SoundFactory.getInstance().loadSound(sound); | |
151 } | |
152 } | |
153 |