1 /************************************************************************
2 ************************************************************************
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
23 #include "blockSchema.h"
28 static double quantize(int n
)
31 return dLetter
* (q
*((n
+q
-1)/q
));
35 * Build a simple colored blockSchema with a certain number of
36 * inputs and outputs, a text to be displayed, and an optional link.
37 * Computes the size of the box according to the length of the text
38 * and the maximum number of ports.
40 schema
* makeBlockSchema ( unsigned int inputs
,
46 // determine the optimal size of the box
47 double minimal
= 3*dWire
;
48 double w
= 2*dHorz
+ max( minimal
, quantize(text
.size()) );
49 double h
= 2*dVert
+ max( minimal
, max(inputs
, outputs
) * dWire
);
51 return new blockSchema(inputs
, outputs
, w
, h
, text
, color
, link
);
55 * Build a simple colored blockSchema with a certain number of
56 * inputs and outputs, a text to be displayed, and an optional link.
57 * The length of the text as well as th number of inputs and outputs
58 * are used to compute the size of the blockSchema
60 blockSchema::blockSchema ( unsigned int inputs
,
68 : schema( inputs
, outputs
, width
, height
),
73 for (unsigned int i
=0; i
<inputs
; i
++) fInputPoint
.push_back(point(0,0));
74 for (unsigned int i
=0; i
<outputs
; i
++) fOutputPoint
.push_back(point(0,0));
78 * Define the graphic position of the blockSchema. Computes the graphic
79 * position of all the elements, in particular the inputs and outputs.
80 * This method must be called before draw(), otherwise draw is not allowed
82 void blockSchema::place(double x
, double y
, int orientation
)
84 beginPlace(x
, y
, orientation
);
93 * Returns an input point
95 point
blockSchema::inputPoint(unsigned int i
) const
98 assert (i
< inputs());
99 return fInputPoint
[i
];
103 * Returns an output point
105 point
blockSchema::outputPoint(unsigned int i
) const
108 assert (i
< outputs());
109 return fOutputPoint
[i
];
113 * Computes the input points according to the position and the
114 * orientation of the blockSchema
116 void blockSchema::placeInputPoints()
120 if (orientation() == kLeftRight
) {
123 double py
= y() + (height() - dWire
*(N
-1))/2;
125 for (int i
=0; i
<N
; i
++) {
126 fInputPoint
[i
] = point(px
, py
+i
*dWire
);
131 double px
= x() + width();
132 double py
= y() + height() - (height() - dWire
*(N
-1))/2;
134 for (int i
=0; i
<N
; i
++) {
135 fInputPoint
[i
] = point(px
, py
-i
*dWire
);
142 * Computes the output points according to the position and the
143 * orientation of the blockSchema
145 void blockSchema::placeOutputPoints()
149 if (orientation() == kLeftRight
) {
151 double px
= x() + width();
152 double py
= y() + (height() - dWire
*(N
-1))/2;
154 for (int i
=0; i
<N
; i
++) {
155 fOutputPoint
[i
] = point(px
, py
+ i
*dWire
);
161 double py
= y() + height() - (height() - dWire
*(N
-1))/2;
163 for (int i
=0; i
<N
; i
++) {
164 fOutputPoint
[i
] = point(px
, py
- i
*dWire
);
171 * Draw the blockSchema on the device. This methos can only
172 * be called after the blockSchema have been placed
174 void blockSchema::draw(device
& dev
)
180 drawOrientationMark(dev
);
182 drawOutputWires(dev
);
186 * Draw the colored rectangle with the optional link
188 void blockSchema::drawRectangle(device
& dev
)
190 dev
.rect( x() + dHorz
,
201 * Draw the text centered on the box
203 void blockSchema::drawText(device
& dev
)
205 dev
.text( x() + width()/2,
214 * Draw the orientation mark, a small point that indicates
215 * the first input (like integrated circuits)
217 void blockSchema::drawOrientationMark(device
& dev
)
221 if (orientation() == kLeftRight
) {
225 px
= x() + width() - dHorz
;
226 py
= y() + height() - dVert
;
229 dev
.markSens( px
, py
, orientation() );
233 * Draw horizontal arrows from the input points to the
234 * blockSchema rectangle
236 void blockSchema::drawInputWires(device
& dev
)
238 double dx
= (orientation() == kLeftRight
) ? dHorz
: -dHorz
;
240 for (unsigned int i
=0; i
<inputs(); i
++) {
241 point p
= fInputPoint
[i
];
242 //dev.trait(p.x, p.y, p.x+dx, p.y);
243 dev
.fleche(p
.x
+dx
, p
.y
, 0, orientation());
248 * Draw horizontal line from the blockSchema rectangle to the
251 void blockSchema::drawOutputWires(device
& dev
)
253 //double dx = (orientation() == kLeftRight) ? dHorz : -dHorz;
255 for (unsigned int i
=0; i
<outputs(); i
++) {
256 point p
= fOutputPoint
[i
];
257 //dev.trait(p.x, p.y, p.x-dx, p.y);
263 * Draw horizontal arrows from the input points to the
264 * blockSchema rectangle
266 void blockSchema::collectTraits(collector
& c
)
268 collectInputWires(c
);
269 collectOutputWires(c
);
273 * Draw horizontal arrows from the input points to the
274 * blockSchema rectangle
276 void blockSchema::collectInputWires(collector
& c
)
278 double dx
= (orientation() == kLeftRight
) ? dHorz
: -dHorz
;
280 for (unsigned int i
=0; i
<inputs(); i
++) {
281 point p
= fInputPoint
[i
];
282 c
.addTrait(trait(point(p
.x
, p
.y
), point(p
.x
+dx
, p
.y
))); // in->out direction
283 c
.addInput(point(p
.x
+dx
, p
.y
));
288 * Draw horizontal line from the blockSchema rectangle to the
291 void blockSchema::collectOutputWires(collector
& c
)
293 double dx
= (orientation() == kLeftRight
) ? dHorz
: -dHorz
;
295 for (unsigned int i
=0; i
<outputs(); i
++) {
296 point p
= fOutputPoint
[i
];
297 c
.addTrait(trait(point(p
.x
-dx
, p
.y
), point(p
.x
, p
.y
))); // in->out direction
298 c
.addOutput(point(p
.x
-dx
, p
.y
));