-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLoopbackTransitionObj.java
More file actions
executable file
·633 lines (512 loc) · 17.3 KB
/
Copy pathLoopbackTransitionObj.java
File metadata and controls
executable file
·633 lines (512 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import java.awt.*;
import java.awt.geom.CubicCurve2D;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Vector;
//Written by: Michael Zimmer - mike@zimmerdesignservices.com
/*
Copyright 2007 Zimmer Design Services
This file is part of Fizzim.
Fizzim is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Fizzim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class LoopbackTransitionObj extends TransitionObj implements Cloneable {
private int selectStatus = 0;
public Point startPt, endPt, startCtrlPt,endCtrlPt;
public int startStateIndex,endStateIndex;
public StateObj state;
public CubicCurve2D.Double loop;
private Vector<Point> stateBorderPts;
private int x,y;
private boolean ready = false;
String startS;
public static final int NONE = 0;
public static final int START = 1;
public static final int STARTCTRL = 2;
public static final int ENDCTRL = 3;
public static final int END = 4;
public static final int ALL = 5;
public static final int TXT = 6;
private int xTemp, yTemp, tempSI,tempEI,lengthS,lengthE;
private double ctrlAngleS, ctrlAngleE;
public LoopbackTransitionObj(int _x, int _y, int numb, int page, Color c)
{
objName = "trans" + numb;
x = _x;
y = _y;
myPage = page;
color = c;
}
public LoopbackTransitionObj(Point sp, Point ep, Point scp, Point ecp,
LinkedList<ObjAttribute> newList,
String name, String start, String end, int sIndex, int eIndex, int page, Color c) {
startPt = sp;
endPt = ep;
startCtrlPt = scp;
endCtrlPt = ecp;
startStateIndex = sIndex;
endStateIndex = eIndex;
attrib = newList;
objName = name;
loop = new CubicCurve2D.Double(startPt.getX(),startPt.getY(),startCtrlPt.getX(),startCtrlPt.getY(),endCtrlPt.getX(),endCtrlPt.getY(),endPt.getX(),endPt.getY());
ready = true;
startS = start;
myPage = page;
color = c;
}
public void initTrans(StateObj _state)
{
if(_state != state)
{
state = _state;
setEndPts(x,y);
loop = new CubicCurve2D.Double(startPt.getX(),startPt.getY(),startCtrlPt.getX(),startCtrlPt.getY(),endCtrlPt.getX(),endCtrlPt.getY(),endPt.getX(),endPt.getY());
ready = true;
}
}
@SuppressWarnings("unchecked")
public Object clone ()
throws CloneNotSupportedException
{
LoopbackTransitionObj copy = (LoopbackTransitionObj)super.clone();
copy.loop = (CubicCurve2D.Double)loop.clone();
copy.startPt = (Point) startPt.clone();
copy.endPt = (Point) endPt.clone();
copy.startCtrlPt = (Point) startCtrlPt.clone();
copy.endCtrlPt = (Point) endCtrlPt.clone();
copy.stateBorderPts = (Vector<Point>) stateBorderPts.clone();
if(attrib != null)
{
copy.attrib = (LinkedList<ObjAttribute>)copy.attrib.clone();
for(int i = 0; i < attrib.size(); i++)
{
copy.attrib.set(i,(ObjAttribute)attrib.get(i).clone());
}
}
return copy;
}
private void setEndPts(int x, int y)
{
//find start point on oval closest to click point
stateBorderPts = state.getBorderPts();
Point createPt = new Point(x,y);
double temp;
double max = 1000000;
for(int i = 0; i < 36; i++)
{
temp = createPt.distanceSq(stateBorderPts.get(i));
if (temp < max)
{
startStateIndex = i;
max = temp;
}
}
// store two points on oval
startPt = stateBorderPts.get(startStateIndex);
endStateIndex = startStateIndex + 5;
if(endStateIndex > 35)
endStateIndex = endStateIndex - 36;
endPt = stateBorderPts.get(endStateIndex);
//angle control points are from points on oval
double angleStart = startStateIndex*2*Math.PI/36;
double angleEnd = endStateIndex*2*Math.PI/36;
//find distance from points on oval to control point
int dist = (int) Math.sqrt(state.getSize());
//scaling
dist = (int) (dist*.65);
//set up control points
startCtrlPt = new Point();
endCtrlPt = new Point();
startCtrlPt.setLocation((int) (dist*Math.cos(angleStart)) + startPt.getX(), dist*Math.sin(angleStart) + startPt.getY());
endCtrlPt.setLocation((int) (dist*Math.cos(angleEnd)) + endPt.getX(), dist*Math.sin(angleEnd) + endPt.getY());
}
public void paintComponent(Graphics g) {
if(ready && currPage == myPage)
{
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(color);
g2D.draw(loop);
//draw arrow head
// find angle between end point and end control point
double alpha = getAngle(endCtrlPt,endPt);
double adj = Math.PI/6;
//g2D.drawLine((int)endPt.getX(),(int)endPt.getY(),(int)endPt.getX() + (int)(arrowLength*Math.cos(alpha + adj)),(int)endPt.getY() - (int)(arrowLength*Math.sin(alpha + adj)));
//g2D.drawLine((int)endPt.getX(),(int)endPt.getY(),(int)endPt.getX() + (int)(arrowLength*Math.cos(alpha - adj)),(int)endPt.getY() - (int)(arrowLength*Math.sin(alpha - adj)));
int[] xP = {(int)endPt.getX(),(int)endPt.getX() + (int)(13*Math.cos(alpha + adj)),(int)endPt.getX() + (int)(13*Math.cos(alpha - adj))};
int[] yP = {(int)endPt.getY(),(int)endPt.getY() - (int)(13*Math.sin(alpha + adj)),(int)endPt.getY() - (int)(13*Math.sin(alpha - adj))};
g2D.drawPolygon(xP,yP,3);
g2D.fillPolygon(xP,yP,3);
/*
int eX = (int)endPt.getX();
int eY = (int)endPt.getY();
int dx = (int)endCtrlPt.getX() - eX;
int dy = eY - (int)endCtrlPt.getY();
double theta = 0;
if(dx == 0)
{
if(dy >= 0)
theta = Math.PI/2;
else
theta = 3*Math.PI/2;
}
else if (dx > 0 && dy >= 0)
theta = Math.atan(dy/dx);
else if (dx > 0 && dy <= 0)
theta = 2*Math.PI + Math.atan(dy/dx);
else if (dx < 0 && dy >= 0)
theta = Math.PI + Math.atan(dy/dx);
else if (dx < 0 && dy <= 0)
theta = Math.PI + Math.atan(dy/dx);
double adj = Math.PI/6;
g2D.drawLine(eX,eY,eX + (int)(20*Math.cos(theta + adj)),eY - (int)(20*Math.sin(theta + adj)));
g2D.drawLine(eX,eY,eX + (int)(20*Math.cos(theta - adj)),eY - (int)(20*Math.sin(theta - adj)));
*/
//draw control points
if(selectStatus != NONE)
{
g2D.setColor(Color.red);
g2D.fillRect((int)startPt.getX()-3,(int)startPt.getY()-3,7,7);
g2D.fillRect((int)endPt.getX()-3,(int)endPt.getY()-3,7,7);
g2D.fillRect((int)startCtrlPt.getX()-3,(int)startCtrlPt.getY()-3,7,7);
g2D.fillRect((int)endCtrlPt.getX()-3,(int)endCtrlPt.getY()-3,7,7);
g2D.drawLine((int)startPt.getX(),(int)startPt.getY(),(int)startCtrlPt.getX(),(int)startCtrlPt.getY());
g2D.drawLine((int)endPt.getX(),(int)endPt.getY(),(int)endCtrlPt.getX(),(int)endCtrlPt.getY());
}
}
}
@Override
public void adjustShapeOrPosition(int x, int y) {
if(currPage == myPage)
{
if(selectStatus == START)
{
Point currPt = new Point(x,y);
double temp;
double max = 1000000;
for(int i = 0; i < 36; i++)
{
temp = currPt.distanceSq(stateBorderPts.get(i));
if (temp < max)
{
startStateIndex = i;
max = temp;
}
}
startPt.setLocation(stateBorderPts.get(startStateIndex).getX(),stateBorderPts.get(startStateIndex).getY());
}
if(selectStatus == STARTCTRL)
startCtrlPt.setLocation(x,y);
if(selectStatus == ENDCTRL)
endCtrlPt.setLocation(x,y);
if(selectStatus == END)
{
Point currPt = new Point(x,y);
double temp;
double max = 1000000;
for(int i = 0; i < 36; i++)
{
temp = currPt.distanceSq(stateBorderPts.get(i));
if (temp < max)
{
endStateIndex = i;
max = temp;
}
}
endPt.setLocation(stateBorderPts.get(endStateIndex).getX(),stateBorderPts.get(endStateIndex).getY());
}
if(selectStatus == TXT)
{
if(attrib != null)
{
for (int i = 0; i < attrib.size(); i++)
{
ObjAttribute s = attrib.get(i);
if(s.getSelectStatus() != 0)
{
s.adjustShapeOrPosition(x, y);
break;
}
}
}
}
// allow loopback to rotate around state
if(selectStatus == 5)
{
// get number of index transition should move
Point c = state.getRealCenter(myPage);
double initAngle = getAngle(new Point(xTemp, yTemp),c);
double currAngle = getAngle(new Point(x,y),c);
int step = (int)-Math.round((currAngle-initAngle)/((2*Math.PI)/36));
if(step != 0)
{
startStateIndex = tempSI + step;
endStateIndex = tempEI + step;
if(startStateIndex > 35)
startStateIndex -= 36;
if(endStateIndex > 35)
endStateIndex -= 36;
if(startStateIndex < 0)
startStateIndex += 36;
if(endStateIndex < 0)
endStateIndex += 36;
//set up points
startPt = stateBorderPts.get(startStateIndex);
endPt = stateBorderPts.get(endStateIndex);
startCtrlPt.setLocation((int) (lengthS*Math.cos(ctrlAngleS-step*(2*Math.PI/36)) + startPt.getX()), (int)(-lengthS*Math.sin(ctrlAngleS-step*(2*Math.PI/36)) + startPt.getY()));
endCtrlPt.setLocation((int) (lengthE*Math.cos(ctrlAngleE-step*(2*Math.PI/36)) + endPt.getX()), (int)(-lengthE*Math.sin(ctrlAngleE-step*(2*Math.PI/36)) + endPt.getY()));
}
}
loop.setCurve(startPt.getX(),startPt.getY(),startCtrlPt.getX(),startCtrlPt.getY(),endCtrlPt.getX(),endCtrlPt.getY(),endPt.getX(),endPt.getY());
modified = true;
}
}
@Override
public int getSelectStatus() {
return selectStatus;
}
@Override
public boolean setSelectStatus(int x, int y) {
if(currPage == myPage)
{
if(selectStatus != 5 || !loop.contains(x,y))
{
selectStatus = NONE;
xTemp = x;
yTemp = y;
tempSI = startStateIndex;
tempEI = endStateIndex;
//ctrlAngleS = getAngle(startCtrlPt,startPt)-(36-startStateIndex)*2*Math.PI/36;
//ctrlAngleE = getAngle(endCtrlPt,endPt)-(36-endStateIndex)*2*Math.PI/36;
ctrlAngleS = getAngle(startCtrlPt,startPt);
ctrlAngleE = getAngle(endCtrlPt,endPt);
lengthS = (int)Math.round(startCtrlPt.distance(startPt));
lengthE = (int)Math.round(endCtrlPt.distance(endPt));
}
//check for txt
if(attrib != null)
{
for(int j = 0; j < attrib.size(); j++)
{
ObjAttribute s = attrib.get(j);
s.unselect();
}
for (int i = 0; i < attrib.size(); i++)
{
ObjAttribute s = attrib.get(i);
if(s.setSelectStatus(x,y))
{
selectStatus = TXT;
break;
}
}
}
if(selectStatus != TXT)
{
//check control points
if(startPt.getX()-x <= 4 && startPt.getX()-x >= -4 && startPt.getY()-y <= 4 && startPt.getY()-y >= -4)
selectStatus = START;
if(startCtrlPt.getX()-x <= 4 && startCtrlPt.getX()-x >= -4 && startCtrlPt.getY()-y <= 4 && startCtrlPt.getY()-y >= -4)
selectStatus = STARTCTRL;
if(endCtrlPt.getX()-x <= 4 && endCtrlPt.getX()-x >= -4 && endCtrlPt.getY()-y <= 4 && endCtrlPt.getY()-y >= -4)
selectStatus = ENDCTRL;
if(endPt.getX()-x <= 4 && endPt.getX()-x >= -4 && endPt.getY()-y <= 4 && endPt.getY()-y >= -4)
selectStatus = END;
// if not a control point, search around line
if(selectStatus == NONE)
// for(int i = -4; i < 5; i++)
//{
//for(int j = -4; j < 5; j++)
//{
if(loop.contains(x,y)) //+i+j
{
selectStatus = 5;
//break;
}
//}
//}
}
if(selectStatus == NONE)
return false;
else
return true;
}
else
return false;
}
public int getType()
{
return 2;
}
public boolean isModified()
{
if(modified)
return true;
else
return false;
}
//sets modified back to false
public void setModifiedFalse()
{
modified = false;
}
public void setModifiedTrue()
{
modified = true;
}
public void updateObj()
{
int newPage = state.getPage();
//moeve all related objects to new page
if(newPage != myPage)
{
for(int i = 0; i < attrib.size();i++)
{
ObjAttribute obj = attrib.get(i);
if(obj.getPage() == myPage)
obj.setPage(newPage);
}
myPage = state.getPage();
}
if(state.getSelectStatus() != StateObj.TXT)
{
double angleS = getAngle(startCtrlPt,startPt);
double angleE = getAngle(endCtrlPt,endPt);
int lenS = (int)Math.round(startCtrlPt.distance(startPt));
int lenE = (int)Math.round(endCtrlPt.distance(endPt));
stateBorderPts = state.getBorderPts();
startPt = stateBorderPts.get(startStateIndex);
endPt = stateBorderPts.get(endStateIndex);
startCtrlPt.setLocation((int) (lenS*Math.cos(angleS)) + startPt.getX(), -lenS*Math.sin(angleS) + startPt.getY());
endCtrlPt.setLocation((int) (lenE*Math.cos(angleE)) + endPt.getX(), -lenE*Math.sin(angleE) + endPt.getY());
loop.setCurve(startPt.getX(),startPt.getY(),startCtrlPt.getX(),startCtrlPt.getY(),endCtrlPt.getX(),endCtrlPt.getY(),endPt.getX(),endPt.getY());
}
}
public void notifyChange(GeneralObj old, GeneralObj clone) {
if(old.equals(state))
{
state = (StateObj) clone;
}
}
public boolean isParentModified()
{
if(modifiedParent)
return true;
else
return false;
}
public void unselect()
{
selectStatus = NONE;
}
public boolean containsParent(GeneralObj oldObj)
{
if(oldObj.equals(state))
return true;
else
return false;
}
public Point getCenter(int page)
{
if(!ready)
return new Point(0,0);
int large;
int small;
int index;
if(startStateIndex > endStateIndex)
{
large = startStateIndex;
small = endStateIndex;
}
else
{
small = startStateIndex;
large = endStateIndex;
}
if(large-small > 18)
{
index = small-(36-large+small)/2;
if(index < 0)
index += 36;
}
else
index = (large-small)/2 + small;
double len = startPt.distance(startCtrlPt);
Point bord = stateBorderPts.get(index);
return new Point((int) (len*Math.cos(index*2*Math.PI/36) + bord.getX()), (int) (len*Math.sin(index*2*Math.PI/36) + bord.getY()));
}
@Override
public Point getStart() {
// TODO make better
return startCtrlPt;
}
public StateObj getStartState()
{
return state;
}
public void save(BufferedWriter writer) throws IOException {
writer.write("## START LOOPBACK TRANSITION OBJECT\n");
writer.write("<transition>\n");
writer.write(i(1) + "<attributes>\n");
for(int i = 0; i < attrib.size(); i++)
{
ObjAttribute obj = attrib.get(i);
obj.save(writer,1);
}
writer.write(i(1) + "</attributes>\n");
writer.write(i(1) + "<startState>\n" + i(1) + state.getName() + "\n" + i(1) + "</startState>\n");
writer.write(i(1) + "<endState>\n" + i(1) + state.getName() + "\n" + i(1) + "</endState>\n");
writer.write(i(1) + "<startPtX>\n" + i(1) + startPt.getX() + "\n" + i(1) + "</startPtX>\n");
writer.write(i(1) + "<startPtY>\n" + i(1) + startPt.getY() + "\n" + i(1) + "</startPtY>\n");
writer.write(i(1) + "<endPtX>\n" + i(1) + endPt.getX() + "\n" + i(1) + "</endPtX>\n");
writer.write(i(1) + "<endPtY>\n" + i(1) + endPt.getY() + "\n" + i(1) + "</endPtY>\n");
writer.write(i(1) + "<startCtrlPtX>\n" + i(1) + startCtrlPt.getX() + "\n" + i(1) + "</startCtrlPtX>\n");
writer.write(i(1) + "<startCtrlPtY>\n" + i(1) + startCtrlPt.getY() + "\n" + i(1) + "</startCtrlPtY>\n");
writer.write(i(1) + "<endCtrlPtY>\n" + i(1) + endCtrlPt.getX() + "\n" + i(1) + "</endCtrlPtY>\n");
writer.write(i(1) + "<endCtrlPtY>\n" + i(1) + endCtrlPt.getY() + "\n" + i(1) + "</endCtrlPtY>\n");
writer.write(i(1) + "<startStateIndex>\n" + i(1) + startStateIndex + "\n" + i(1) + "</startStateIndex>\n");
writer.write(i(1) + "<endStateIndex>\n" + i(1) + endStateIndex + "\n" + i(1) + "</endStateIndex>\n");
writer.write(i(1) + "<page>\n" + i(1) + myPage + "\n" + i(1) + "</page>\n");
writer.write(i(1) + "<color>\n" + i(1) + color.getRGB() + "\n" + i(1) + "</color>\n");
writer.write("</transition>\n");
writer.write("## START LOOPBACK TRANSITION OBJECT\n");
}
public void makeConnections(Vector<Object> objList)
{
for(int i = 1; i < objList.size(); i++)
{
GeneralObj obj = (GeneralObj)objList.get(i);
if(obj.getType() == 0)
{
if(obj.getName().equals(startS))
state = (StateObj) obj;
}
}
stateBorderPts = state.getBorderPts();
}
@Override
public boolean setBoxSelectStatus(int x0, int y0, int x1, int y1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void updateObjPages(int page) {
myPage = page;
if(attrib != null)
{
for(int i = 0; i < attrib.size(); i++)
{
ObjAttribute obj = attrib.get(i);
obj.setPage(page);
}
}
}
}