Mercurial > hg > accesspd
comparison java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/RectangularNode.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) 2002 Cay S. Horstmann (http://horstmann.com) | |
5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) | |
6 | |
7 This program is free software: you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation, either version 3 of the License, or | |
10 (at your option) any later version. | |
11 | |
12 This program is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 package uk.ac.qmul.eecs.ccmi.simpletemplate; | |
22 | |
23 import java.awt.Shape; | |
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 A rectangular shaped diagram node. | |
39 */ | |
40 @SuppressWarnings("serial") | |
41 public class RectangularNode extends SimpleShapeNode{ | |
42 | |
43 public RectangularNode(String nodeType, NodeProperties properties){ | |
44 super(nodeType, properties); | |
45 bounds = dataDisplayBounds; | |
46 } | |
47 | |
48 @Override | |
49 public ShapeType getShapeType() { | |
50 return ShapeType.Rectangle; | |
51 } | |
52 | |
53 @Override | |
54 protected void translateImplementation(Point2D p, double dx, double dy){ | |
55 /* if we clicked on a property node, just move that one */ | |
56 for(List<PropertyNode> pnList : propertyNodesMap.values()) | |
57 for(PropertyNode pn : pnList) | |
58 if(pn.contains(p)){ | |
59 pn.translate(dx, dy); | |
60 return; | |
61 } | |
62 | |
63 bounds.setFrame(bounds.getX() + dx, | |
64 bounds.getY() + dy, | |
65 bounds.getWidth(), | |
66 bounds.getHeight()); | |
67 super.translateImplementation(p, dx, dy); | |
68 } | |
69 | |
70 @Override | |
71 protected void reshapeInnerProperties(List<String> insidePropertyTypes){ | |
72 super.reshapeInnerProperties(insidePropertyTypes); | |
73 bounds.setFrame(dataDisplayBounds); | |
74 } | |
75 | |
76 @Override | |
77 public void decode(Document doc, Element nodeTag) throws IOException{ | |
78 super.decode(doc, nodeTag); | |
79 bounds.setFrame(dataDisplayBounds); | |
80 } | |
81 | |
82 @Override | |
83 public Rectangle2D getBounds(){ | |
84 return (Rectangle2D)bounds.clone(); | |
85 } | |
86 | |
87 @Override | |
88 public Point2D getConnectionPoint(Direction d){ | |
89 return calculateConnectionPoint(d, getBounds()); | |
90 } | |
91 | |
92 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds){ | |
93 double slope = bounds.getHeight() / bounds.getWidth(); | |
94 double ex = d.getX(); | |
95 double ey = d.getY(); | |
96 double x = bounds.getCenterX(); | |
97 double y = bounds.getCenterY(); | |
98 | |
99 if (ex != 0 && -slope <= ey / ex && ey / ex <= slope){ | |
100 // intersects at left or right boundary | |
101 if (ex > 0){ | |
102 x = bounds.getMaxX(); | |
103 y += (bounds.getWidth() / 2) * ey / ex; | |
104 }else{ | |
105 x = bounds.getX(); | |
106 y -= (bounds.getWidth() / 2) * ey / ex; | |
107 } | |
108 }else if (ey != 0){ | |
109 // intersects at top or bottom | |
110 if (ey > 0){ | |
111 x += (bounds.getHeight() / 2) * ex / ey; | |
112 y = bounds.getMaxY(); | |
113 }else{ | |
114 x -= (bounds.getHeight() / 2) * ex / ey; | |
115 y = bounds.getY(); | |
116 } | |
117 } | |
118 return new Point2D.Double(x, y); | |
119 } | |
120 | |
121 public Shape getShape(){ | |
122 return bounds; | |
123 } | |
124 | |
125 @Override | |
126 public InputStream getSound(){ | |
127 return sound; | |
128 } | |
129 | |
130 public Object clone(){ | |
131 RectangularNode cloned = (RectangularNode)super.clone(); | |
132 cloned.bounds = (Rectangle2D.Double)bounds.clone(); | |
133 return cloned; | |
134 } | |
135 | |
136 private Rectangle2D.Double bounds; | |
137 private static InputStream sound; | |
138 | |
139 static{ | |
140 sound = RectangularNode.class.getResourceAsStream("audio/Rectangle.mp3"); | |
141 SoundFactory.getInstance().loadSound(sound); | |
142 } | |
143 } |