f@0
|
1 /*
|
f@0
|
2 accessPD - An accessible PD patches editor
|
f@0
|
3
|
f@0
|
4 Copyright (C) 2014 Fiore Martin
|
f@0
|
5
|
f@0
|
6 This program is free software: you can redistribute it and/or modify
|
f@0
|
7 it under the terms of the GNU General Public License as published by
|
f@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
f@0
|
9 (at your option) any later version.
|
f@0
|
10
|
f@0
|
11 This program is distributed in the hope that it will be useful,
|
f@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@0
|
14 GNU General Public License for more details.
|
f@0
|
15
|
f@0
|
16 You should have received a copy of the GNU General Public License
|
f@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@0
|
18 */
|
f@0
|
19
|
f@0
|
20 package uk.ac.qmul.eecs.ccmi.pdsupport;
|
f@0
|
21
|
f@0
|
22 import java.awt.Graphics2D;
|
f@0
|
23 import java.awt.Stroke;
|
f@0
|
24 import java.awt.geom.Line2D;
|
f@0
|
25 import java.awt.geom.Rectangle2D;
|
f@0
|
26 import java.io.InputStream;
|
f@0
|
27
|
f@0
|
28 import uk.ac.qmul.eecs.ccmi.gui.Edge;
|
f@0
|
29 import uk.ac.qmul.eecs.ccmi.gui.GraphElement;
|
f@0
|
30 import uk.ac.qmul.eecs.ccmi.gui.LineStyle;
|
f@0
|
31 import uk.ac.qmul.eecs.ccmi.gui.Node;
|
f@0
|
32 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
|
f@0
|
33
|
f@0
|
34 public class PdConnection extends Edge implements PdElement{
|
f@0
|
35 private static final long serialVersionUID = 1L;
|
f@0
|
36 private static InputStream sound;
|
f@0
|
37 private static String FROM = "from";
|
f@0
|
38 private static String TO = "to";
|
f@0
|
39
|
f@0
|
40 static{
|
f@0
|
41 sound = PdConnection.class.getResourceAsStream("audio/PdConnection.mp3");
|
f@0
|
42 SoundFactory.getInstance().loadSound(sound);
|
f@0
|
43 }
|
f@0
|
44
|
f@0
|
45 public PdConnection(){
|
f@0
|
46 super("Connection", new String[] {FROM, TO}, 2, 2, LineStyle.Solid);
|
f@0
|
47 }
|
f@0
|
48
|
f@0
|
49
|
f@0
|
50 @Override
|
f@0
|
51 public void draw(Graphics2D g2) {
|
f@0
|
52 /* use this edge stroke */
|
f@0
|
53 Stroke oldStroke = g2.getStroke();
|
f@0
|
54 g2.setStroke(getStyle().getStroke());
|
f@0
|
55 /* straight line */
|
f@0
|
56
|
f@0
|
57 if(points.isEmpty()){
|
f@0
|
58 /* just one line from one node to the other */
|
f@0
|
59 Line2D line = getSegment(getNodeAt(0),getNodeAt(1));
|
f@0
|
60 g2.draw(line);
|
f@0
|
61 }else{
|
f@0
|
62 /* the edge has been bended into more lines. *
|
f@0
|
63 * for every inner point neighbour draw a line *
|
f@0
|
64 * and draw the inner point itself */
|
f@0
|
65 for(InnerPoint p : points){
|
f@0
|
66 for(GraphElement ge : p.getNeighbours()){
|
f@0
|
67 g2.draw(getSegment(p,ge));
|
f@0
|
68 }
|
f@0
|
69 p.draw(g2);
|
f@0
|
70 }
|
f@0
|
71
|
f@0
|
72 }
|
f@0
|
73
|
f@0
|
74 /* restore old stroke of g2 */
|
f@0
|
75 g2.setStroke(oldStroke);
|
f@0
|
76 }
|
f@0
|
77
|
f@0
|
78 @Override
|
f@0
|
79 public Rectangle2D getBounds() {
|
f@0
|
80 Rectangle2D bounds = (Rectangle2D)getNodeAt(0).getBounds();
|
f@0
|
81
|
f@0
|
82 for(int i=1; i< getNodesNum(); i++)
|
f@0
|
83 bounds.add(getNodeAt(i).getBounds());
|
f@0
|
84
|
f@0
|
85 for(InnerPoint p : points){
|
f@0
|
86 bounds.add(p.getBounds());
|
f@0
|
87 }
|
f@0
|
88
|
f@0
|
89 return bounds;
|
f@0
|
90 }
|
f@0
|
91
|
f@0
|
92 @Override
|
f@0
|
93 public InputStream getSound() {
|
f@0
|
94 return sound;
|
f@0
|
95 }
|
f@0
|
96
|
f@0
|
97
|
f@0
|
98 @Override
|
f@0
|
99 public String toPdFile() {
|
f@0
|
100
|
f@0
|
101 Node n0 = getNodeAt(0);
|
f@0
|
102 Node n1 = getNodeAt(1);
|
f@0
|
103
|
f@0
|
104 PdElement elemFrom = (PdElement) ( FROM.equals(this.getEndDescription(n0)) ? n0 : n1);
|
f@0
|
105 PdElement elemTo = (PdElement) ( TO.equals(this.getEndDescription(n0)) ? n0 : n1);
|
f@0
|
106
|
f@0
|
107 return getChunckType() +
|
f@0
|
108 " connect " +
|
f@0
|
109 elemFrom.getOrderNumber()+
|
f@0
|
110 " 0 " +
|
f@0
|
111 elemTo.getOrderNumber() +
|
f@0
|
112 " 0"
|
f@0
|
113 ;
|
f@0
|
114 }
|
f@0
|
115
|
f@0
|
116
|
f@0
|
117 @Override
|
f@0
|
118 public String getChunckType() {
|
f@0
|
119 return "#X";
|
f@0
|
120 }
|
f@0
|
121
|
f@0
|
122 @Override
|
f@0
|
123 public int getOrderNumber(){
|
f@0
|
124 throw new UnsupportedOperationException();
|
f@0
|
125 }
|
f@0
|
126
|
f@0
|
127 public Object clone(){
|
f@0
|
128
|
f@0
|
129 return new PdConnection();
|
f@0
|
130 }
|
f@0
|
131
|
f@0
|
132 }
|