FAUST compiler  0.9.9.6b8
blockSchema.cpp
Go to the documentation of this file.
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 
23 #include "blockSchema.h"
24 #include <assert.h>
25 
26 using namespace std;
27 
28 static double quantize(int n)
29 {
30  int q = 3;
31  return dLetter * (q *((n+q-1)/q));
32 }
33 
34 
41 schema* makeBlockSchema ( unsigned int inputs,
42  unsigned int outputs,
43  const string& text,
44  const string& color,
45  const string& link )
46 {
47  // determine the optimal size of the box
48  double minimal = 3*dWire;
49  double w = 2*dHorz + max( minimal, quantize((int)text.size()) );
50  double h = 2*dVert + max( minimal, max(inputs, outputs) * dWire );
51 
52  return new blockSchema(inputs, outputs, w, h, text, color, link);
53 }
54 
55 
62 blockSchema::blockSchema ( unsigned int inputs,
63  unsigned int outputs,
64  double width,
65  double height,
66  const string& text,
67  const string& color,
68  const string& link)
69 
70  : schema( inputs, outputs, width, height ),
71  fText(text),
72  fColor(color),
73  fLink(link)
74 {
75  for (unsigned int i=0; i<inputs; i++) fInputPoint.push_back(point(0,0));
76  for (unsigned int i=0; i<outputs; i++) fOutputPoint.push_back(point(0,0));
77 }
78 
79 
85 void blockSchema::place(double x, double y, int orientation)
86 {
87  beginPlace(x, y, orientation);
88 
91 
92  endPlace();
93 }
94 
95 
99 point blockSchema::inputPoint(unsigned int i) const
100 {
101  assert (placed());
102  assert (i < inputs());
103  return fInputPoint[i];
104 }
105 
106 
110 point blockSchema::outputPoint(unsigned int i) const
111 {
112  assert (placed());
113  assert (i < outputs());
114  return fOutputPoint[i];
115 }
116 
117 
123 {
124  int N = inputs();
125 
126  if (orientation() == kLeftRight) {
127 
128  double px = x();
129  double py = y() + (height() - dWire*(N-1))/2;
130 
131  for (int i=0; i<N; i++) {
132  fInputPoint[i] = point(px, py+i*dWire);
133  }
134 
135  } else {
136 
137  double px = x() + width();
138  double py = y() + height() - (height() - dWire*(N-1))/2;
139 
140  for (int i=0; i<N; i++) {
141  fInputPoint[i] = point(px, py-i*dWire);
142  }
143  }
144 }
145 
146 
152 {
153  int N = outputs();
154 
155  if (orientation() == kLeftRight) {
156 
157  double px = x() + width();
158  double py = y() + (height() - dWire*(N-1))/2;
159 
160  for (int i=0; i<N; i++) {
161  fOutputPoint[i] = point(px, py + i*dWire);
162  }
163 
164  } else {
165 
166  double px = x();
167  double py = y() + height() - (height() - dWire*(N-1))/2;
168 
169  for (int i=0; i<N; i++) {
170  fOutputPoint[i] = point(px, py - i*dWire);
171  }
172  }
173 }
174 
175 
181 {
182  assert(placed());
183 
184  drawRectangle(dev);
185  drawText(dev);
186  drawOrientationMark(dev);
187  drawInputArrows(dev);
188 }
189 
194 {
195  dev.rect( x() + dHorz,
196  y() + dVert,
197  width() - 2*dHorz,
198  height() - 2*dVert,
199  fColor.c_str(),
200  fLink.c_str()
201  );
202 }
203 
204 
209 {
210  dev.text( x() + width()/2,
211  y() + height()/2,
212  fText.c_str(),
213  fLink.c_str()
214  );
215 }
216 
217 
223 {
224  double px, py;
225 
226  if (orientation() == kLeftRight) {
227  px = x() + dHorz;
228  py = y() + dVert;
229  } else {
230  px = x() + width() - dHorz;
231  py = y() + height() - dVert;
232  }
233 
234  dev.markSens( px, py, orientation() );
235 }
241 {
242  double dx = (orientation() == kLeftRight) ? dHorz : -dHorz;
243 
244  for (unsigned int i=0; i<inputs(); i++) {
245  point p = fInputPoint[i];
246  dev.fleche(p.x+dx, p.y, 0, orientation());
247  }
248 }
249 
250 
256 {
259 }
260 
266 {
267  double dx = (orientation() == kLeftRight) ? dHorz : -dHorz;
268 
269  for (unsigned int i=0; i<inputs(); i++) {
270  point p = fInputPoint[i];
271  c.addTrait(trait(point(p.x, p.y), point(p.x+dx, p.y))); // in->out direction
272  c.addInput(point(p.x+dx, p.y));
273  }
274 }
275 
281 {
282  double dx = (orientation() == kLeftRight) ? dHorz : -dHorz;
283 
284  for (unsigned int i=0; i<outputs(); i++) {
285  point p = fOutputPoint[i];
286  c.addTrait(trait(point(p.x-dx, p.y), point(p.x, p.y))); // in->out direction
287  c.addOutput(point(p.x-dx, p.y));
288  }
289 }
290 
291 
vector< point > fInputPoint
input connection points
Definition: blockSchema.h:43
void addOutput(const point &p)
Definition: schema.h:82
void addTrait(const trait &t)
Definition: schema.h:84
void endPlace()
Definition: schema.h:134
const string fLink
option URL link
Definition: blockSchema.h:40
void drawText(device &dev)
Draw the text centered on the box.
Definition: schema.h:56
unsigned int outputs() const
Definition: schema.h:129
void placeInputPoints()
Computes the input points according to the position and the orientation of the blockSchema.
const double dWire
distance between two wires
Definition: schema.h:33
void beginPlace(double x, double y, int orientation)
Definition: schema.h:132
const string fText
Text to be displayed.
Definition: blockSchema.h:38
vector< point > fOutputPoint
output connection points
Definition: blockSchema.h:44
virtual void text(double x, double y, const char *name, const char *link)=0
void placeOutputPoints()
Computes the output points according to the position and the orientation of the blockSchema.
virtual void draw(device &dev)
Draw the blockSchema on the device.
virtual void collectTraits(collector &c)
Draw horizontal arrows from the input points to the blockSchema rectangle.
const double dVert
marge verticale
Definition: schema.h:37
unsigned int inputs() const
Definition: schema.h:128
double max(double x, double y)
Definition: interval.hh:60
double y() const
Definition: schema.h:139
double width() const
Definition: schema.h:126
double y
Definition: schema.h:43
virtual void rect(double x, double y, double l, double h, const char *color, const char *link)=0
bool placed() const
Definition: schema.h:137
static double quantize(int n)
Definition: blockSchema.cpp:28
Definition: device.h:32
A simple rectangular box with a text and inputs and outputs.
Definition: blockSchema.h:35
virtual point outputPoint(unsigned int i) const
Returns an output point.
blockSchema(unsigned int inputs, unsigned int outputs, double width, double height, const string &name, const string &color, const string &link)
Build a simple colored blockSchema with a certain number of inputs and outputs, a text to be displaye...
Definition: blockSchema.cpp:62
void drawInputArrows(device &dev)
Draw horizontal arrows from the input points to the blockSchema rectangle.
double x() const
Definition: schema.h:138
int orientation() const
Definition: schema.h:140
void drawOrientationMark(device &dev)
Draw the orientation mark, a small point that indicates the first input (like integrated circuits) ...
double x
Definition: schema.h:42
Definition: schema.h:40
virtual point inputPoint(unsigned int i) const
Returns an input point.
Definition: blockSchema.cpp:99
An abstract block diagram schema.
Definition: schema.h:97
double height() const
Definition: schema.h:127
void collectOutputWires(collector &c)
Draw horizontal line from the blockSchema rectangle to the output points.
virtual void fleche(double x, double y, double rotation, int sens)=0
const double dHorz
marge horizontale
Definition: schema.h:36
void collectInputWires(collector &c)
Draw horizontal arrows from the input points to the blockSchema rectangle.
void addInput(const point &p)
Definition: schema.h:83
const double dLetter
width of a letter
Definition: schema.h:35
virtual void markSens(double x, double y, int sens)=0
schema * makeBlockSchema(unsigned int inputs, unsigned int outputs, const string &text, const string &color, const string &link)
Build a simple colored blockSchema with a certain number of inputs and outputs, a text to be displaye...
Definition: blockSchema.cpp:41
void drawRectangle(device &dev)
Draw the colored rectangle with the optional link.
virtual void place(double x, double y, int orientation)
Define the graphic position of the blockSchema.
Definition: blockSchema.cpp:85
const string fColor
color of the box
Definition: blockSchema.h:39