~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/editor/draw2d/BezierConnectionRouter.js

  • Committer: parra
  • Date: 2010-03-15 15:56:56 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1448
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This notice must be untouched at all times.
 
2
 
 
3
Open-jACOB Draw2D
 
4
The latest version is available at
 
5
http://www.openjacob.org
 
6
 
 
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
 
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
 
9
 
 
10
LICENSE: LGPL
 
11
 
 
12
This library is free software; you can redistribute it and/or
 
13
modify it under the terms of the GNU Lesser General Public
 
14
License (LGPL) as published by the Free Software Foundation; either
 
15
version 2.1 of the License, or (at your option) any later version.
 
16
 
 
17
This library is distributed in the hope that it will be useful,
 
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
20
Lesser General Public License for more details.
 
21
 
 
22
You should have received a copy of the GNU Lesser General Public
 
23
License along with this library; if not, write to the Free Software
 
24
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
 
25
or see http://www.gnu.org/copyleft/lesser.html
 
26
*/
 
27
 
 
28
/**
 
29
 * @class Routes a {@link draw2d.Connection}, possibly using a constraint.
 
30
 *
 
31
 * @version 0.9.14
 
32
 * @author Andreas Herz
 
33
 * @constructor
 
34
 */
 
35
draw2d.BezierConnectionRouter=function(/*:boolean*/ noCheapRouter)
 
36
{
 
37
   if(!noCheapRouter)
 
38
      this.cheapRouter = new draw2d.ManhattanConnectionRouter();
 
39
   else
 
40
      this.cheapRouter=null;
 
41
   this.iteration=5;
 
42
//   this.setTargetDecorator(new draw2d.ArrowConnectionDecorator());
 
43
}
 
44
 
 
45
draw2d.BezierConnectionRouter.prototype = new draw2d.ConnectionRouter;
 
46
draw2d.BezierConnectionRouter.prototype.type="BezierConnectionRouter";
 
47
 
 
48
 
 
49
draw2d.BezierConnectionRouter.prototype.drawBezier=function(/*:Array*/pointArray,/*:Array*/resultArray, /*:float*/t, /*:int*/iter)
 
50
{
 
51
 
 
52
  var n = pointArray.length-1;
 
53
 
 
54
  var q = new Array();
 
55
  var n_plus_1= n+1;
 
56
  for (var i = 0; i < n_plus_1; i++)
 
57
  {
 
58
    q[i]=new Array();
 
59
    q[i][0] = pointArray[i];
 
60
  }
 
61
 
 
62
  for (var j = 1; j <= n; j++)
 
63
  {
 
64
    for (var i = 0; i <= (n-j); i++)
 
65
    {
 
66
      q[i][j] = new draw2d.Point((1 - t) * q[i][j - 1].x + t * q[i + 1][j - 1].x,
 
67
                          (1 - t) * q[i][j - 1].y + t * q[i + 1][j - 1].y);
 
68
    }
 
69
  }
 
70
    //Arrays für die Punkte der geteilten Kontrollpolygone C1, C2)
 
71
    var c1 = new Array();
 
72
    var c2 = new Array();
 
73
 
 
74
    for (var i=0;i<n+1;i++)
 
75
    {
 
76
      c1[i] = q[0][i];
 
77
      c2[i] = q[i][n-i];
 
78
    }
 
79
 
 
80
    if(iter>=0)
 
81
    {
 
82
      this.drawBezier(c1,resultArray, t, --iter);
 
83
      this.drawBezier(c2,resultArray, t, --iter);
 
84
    }
 
85
    else
 
86
    {
 
87
      for (var i=0;i<n;i++)
 
88
      {
 
89
        resultArray.push(q[i][n-i]);
 
90
      }
 
91
   }
 
92
}
 
93
 
 
94
/**
 
95
 * @see draw2d.ConnectionRouter#route(Connection)
 
96
 */
 
97
draw2d.BezierConnectionRouter.prototype.route=function(/*:draw2d.Connection*/ conn)
 
98
{
 
99
 
 
100
   if(this.cheapRouter!=null && (conn.getSource().getParent().isMoving==true || conn.getTarget().getParent().isMoving==true ))
 
101
   {
 
102
     this.cheapRouter.route(conn);
 
103
     return;
 
104
   }
 
105
 
 
106
   var pointList = new Array();
 
107
   var fromPt  = conn.getStartPoint();
 
108
   var toPt    = conn.getEndPoint();
 
109
 
 
110
   // create the Manhattan line stroke
 
111
   //
 
112
   this._route(pointList, conn, toPt, this.getEndDirection(conn), fromPt, this.getStartDirection(conn));
 
113
   var resultList = new Array();
 
114
   // create the Bezier spline from the ManhattanLineStroke
 
115
   //
 
116
   this.drawBezier(pointList,resultList, 0.5, this.iteration);
 
117
   for(var i=0;i<resultList.length;i++)
 
118
   {
 
119
     conn.addPoint(resultList[i]);
 
120
   }
 
121
   conn.addPoint(toPt);
 
122
}
 
123
 
 
124
/**
 
125
 * @private
 
126
 **/
 
127
draw2d.BezierConnectionRouter.prototype._route=function(pointList, /*:draw2d.Connection*/ conn,/*:draw2d.Point*/ fromPt, /*:int*/fromDir, /*:draw2d.Point*/toPt, /*:int*/toDir)
 
128
{
 
129
   var TOL     = 0.1;
 
130
   var TOLxTOL = 0.01;
 
131
   var MINDIST = 90;
 
132
 
 
133
   // fromPt is an x,y to start from.  
 
134
   // fromDir is an angle that the first link must 
 
135
   //
 
136
   var UP   = 0;
 
137
   var RIGHT= 1;
 
138
   var DOWN = 2;
 
139
   var LEFT = 3;
 
140
 
 
141
   var xDiff = fromPt.x - toPt.x;
 
142
   var yDiff = fromPt.y - toPt.y;
 
143
   var point;
 
144
   var dir;
 
145
 
 
146
   if (((xDiff * xDiff) < (TOLxTOL)) && ((yDiff * yDiff) < (TOLxTOL))) 
 
147
   {
 
148
      pointList.push(new draw2d.Point(toPt.x, toPt.y));
 
149
      return;
 
150
   }
 
151
 
 
152
   if (fromDir == LEFT)
 
153
   {
 
154
      if ((xDiff > 0) && ((yDiff * yDiff) < TOL) && (toDir == RIGHT))
 
155
      {
 
156
         point = toPt;
 
157
         dir = toDir;
 
158
      }
 
159
      else
 
160
      {
 
161
         if (xDiff < 0) 
 
162
         {
 
163
            point = new draw2d.Point(fromPt.x - MINDIST, fromPt.y);
 
164
         }
 
165
         else if (((yDiff > 0) && (toDir == DOWN)) || ((yDiff < 0) && (toDir == UP))) 
 
166
         {
 
167
            point = new draw2d.Point(toPt.x, fromPt.y);
 
168
         }
 
169
         else if (fromDir == toDir)
 
170
         {
 
171
            var pos = Math.min(fromPt.x, toPt.x) - MINDIST;
 
172
            point = new draw2d.Point(pos, fromPt.y);
 
173
         }
 
174
         else
 
175
         {
 
176
            point = new draw2d.Point(fromPt.x - (xDiff / 2), fromPt.y);
 
177
         }
 
178
 
 
179
         if (yDiff > 0) 
 
180
         {
 
181
            dir = UP;
 
182
         }
 
183
         else
 
184
         {
 
185
            dir = DOWN;
 
186
         }
 
187
      }
 
188
   }
 
189
   else if (fromDir == RIGHT)
 
190
   {
 
191
      if ((xDiff < 0) && ((yDiff * yDiff) < TOL)&& (toDir == LEFT)) 
 
192
      {
 
193
         point = toPt;
 
194
         dir = toDir;
 
195
      } 
 
196
      else 
 
197
      {
 
198
         if (xDiff > 0) 
 
199
         {
 
200
           point = new draw2d.Point(fromPt.x + MINDIST, fromPt.y);
 
201
         } 
 
202
         else if (((yDiff > 0) && (toDir == DOWN)) || ((yDiff < 0) && (toDir == UP))) 
 
203
         {
 
204
            point = new draw2d.Point(toPt.x, fromPt.y);
 
205
         } 
 
206
         else if (fromDir == toDir) 
 
207
         {
 
208
            var pos = Math.max(fromPt.x, toPt.x) + MINDIST;
 
209
            point = new draw2d.Point(pos, fromPt.y);
 
210
         } 
 
211
         else 
 
212
         {
 
213
               point = new draw2d.Point(fromPt.x - (xDiff / 2), fromPt.y);
 
214
         }
 
215
 
 
216
         if (yDiff > 0) 
 
217
            dir = UP;
 
218
         else
 
219
            dir = DOWN;
 
220
      }
 
221
   } 
 
222
   else if (fromDir == DOWN) 
 
223
   {
 
224
      if (((xDiff * xDiff) < TOL) && (yDiff < 0)&& (toDir == UP)) 
 
225
      {
 
226
         point = toPt;
 
227
         dir = toDir;
 
228
      } 
 
229
      else 
 
230
      {
 
231
         if (yDiff > 0) 
 
232
         {
 
233
            point = new draw2d.Point(fromPt.x, fromPt.y + MINDIST);
 
234
         } 
 
235
         else if (((xDiff > 0) && (toDir == RIGHT)) || ((xDiff < 0) && (toDir == LEFT))) 
 
236
         {
 
237
           point = new draw2d.Point(fromPt.x, toPt.y);
 
238
         } 
 
239
         else if (fromDir == toDir) 
 
240
         {
 
241
            var pos = Math.max(fromPt.y, toPt.y) + MINDIST;
 
242
            point = new draw2d.Point(fromPt.x, pos);
 
243
         } 
 
244
         else 
 
245
         {
 
246
            point = new draw2d.Point(fromPt.x, fromPt.y - (yDiff / 2));
 
247
         }
 
248
 
 
249
         if (xDiff > 0) 
 
250
            dir = LEFT;
 
251
         else 
 
252
            dir = RIGHT;
 
253
      }
 
254
   } 
 
255
   else if (fromDir == UP) 
 
256
   {
 
257
      if (((xDiff * xDiff) < TOL) && (yDiff > 0) && (toDir == DOWN)) 
 
258
      {
 
259
         point = toPt;
 
260
         dir = toDir;
 
261
      }
 
262
      else
 
263
      {
 
264
         if (yDiff < 0) 
 
265
         {
 
266
            point = new draw2d.Point(fromPt.x, fromPt.y - MINDIST);
 
267
         } 
 
268
         else if (((xDiff > 0) && (toDir == RIGHT)) || ((xDiff < 0) && (toDir == LEFT))) 
 
269
         {
 
270
            point = new draw2d.Point(fromPt.x, toPt.y);
 
271
         } 
 
272
         else if (fromDir == toDir) 
 
273
         {
 
274
            var pos = Math.min(fromPt.y, toPt.y) - MINDIST;
 
275
            point = new draw2d.Point(fromPt.x, pos);
 
276
         } 
 
277
         else 
 
278
         {
 
279
            point = new draw2d.Point(fromPt.x, fromPt.y - (yDiff / 2));
 
280
         }
 
281
 
 
282
         if (xDiff > 0)
 
283
            dir = LEFT;
 
284
         else
 
285
            dir = RIGHT;
 
286
      }
 
287
   }
 
288
   this._route(pointList, conn,point, dir, toPt, toDir);
 
289
   pointList.push(fromPt);
 
290
}
 
291