Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / draw / schema / enlargedSchema.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
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.
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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22 #include "enlargedSchema.h"
23 #include <assert.h>
24 #include <iostream>
25
26 using namespace std;
27
28 /**
29 * Returns an enlarged schema, but only if really needed
30 * that is if the requiered width is greater that the schema width.
31 */
32 schema* makeEnlargedSchema ( schema* s, double width )
33 {
34 if (width > s->width()) {
35 return new enlargedSchema(s, width);
36 } else {
37 return s;
38 }
39 }
40
41 /**
42 * Put additional space left and right of a schema so that the result has
43 * a certain width. The wires are prolonged accordingly.
44 */
45 enlargedSchema::enlargedSchema( schema* s, double width )
46 : schema(s->inputs(), s->outputs(), width, s->height()),
47 fSchema(s)
48 {
49 for (unsigned int i=0; i<inputs(); i++) fInputPoint.push_back(point(0,0));
50 for (unsigned int i=0; i<outputs(); i++) fOutputPoint.push_back(point(0,0));
51 }
52
53
54 /**
55 * Define the graphic position of the schema. Computes the graphic
56 * position of all the elements, in particular the inputs and outputs.
57 * This method must be called before draw(), otherwise draw is not allowed
58 */
59 void enlargedSchema::place(double ox, double oy, int orientation)
60 {
61 beginPlace(ox, oy, orientation);
62
63 double dx = (width() - fSchema->width())/2;
64 fSchema->place(ox+dx, oy, orientation);
65
66 if (orientation == kRightLeft) {
67 dx = -dx;
68 }
69
70 for (unsigned int i=0; i < inputs(); i++) {
71 point p = fSchema->inputPoint(i);
72 fInputPoint[i] = point(p.x-dx, p.y); //, p.z);
73 }
74
75 for (unsigned int i=0; i < outputs(); i++) {
76 point p = fSchema->outputPoint(i);
77 fOutputPoint[i] = point(p.x+dx, p.y); //, p.z);
78 }
79
80 endPlace();
81 }
82
83 /**
84 * Returns an input point
85 */
86 point enlargedSchema::inputPoint(unsigned int i) const
87 {
88 assert (placed());
89 assert (i < inputs());
90 return fInputPoint[i];
91 }
92
93 /**
94 * Returns an output point
95 */
96 point enlargedSchema::outputPoint(unsigned int i) const
97 {
98 assert (placed());
99 assert (i < outputs());
100 return fOutputPoint[i];
101 }
102
103 /**
104 * Draw the enlarged schema. This methos can only
105 * be called after the block have been placed
106 */
107 void enlargedSchema::draw(device& dev)
108 {
109 assert(placed());
110
111 fSchema->draw(dev);
112 #if 0
113 // draw enlarge input wires
114 for (unsigned int i=0; i<inputs(); i++) {
115 point p = inputPoint(i);
116 point q = fSchema->inputPoint(i);
117 if ( (p.z>=0) && (q.z>=0) ) dev.trait(p.x, p.y, q.x, q.y);
118 }
119
120 // draw enlarge output wires
121 for (unsigned int i=0; i<outputs(); i++) {
122 point p = outputPoint(i);
123 point q = fSchema->outputPoint(i);
124 if ( (p.z>=0) && (q.z>=0) ) dev.trait(p.x, p.y, q.x, q.y);
125 }
126 #endif
127 }
128
129 /**
130 * Draw the enlarged schema. This methos can only
131 * be called after the block have been placed
132 */
133 void enlargedSchema::collectTraits(collector& c)
134 {
135 assert(placed());
136
137 fSchema->collectTraits(c);
138
139 // draw enlarge input wires
140 for (unsigned int i=0; i<inputs(); i++) {
141 point p = inputPoint(i);
142 point q = fSchema->inputPoint(i);
143 c.addTrait(trait(p,q)); // in->out direction
144 }
145
146 // draw enlarge output wires
147 for (unsigned int i=0; i<outputs(); i++) {
148 point q = fSchema->outputPoint(i);
149 point p = outputPoint(i);
150 c.addTrait(trait(q,p)); // in->out direction
151 }
152 }