-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathliquidGraphUI.js
1516 lines (1182 loc) · 36.5 KB
/
liquidGraphUI.js
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//globals
var globalAccel = {y:50,x:0.5};
/*****************CLASSES*******************/
function BulkAnimator() {
this.timeout = null;
this.functionsToCall = [];
this.interval = 1000 * 1/40;
this.animating = false;
};
BulkAnimator.prototype.add = function(functionToAdd) {
this.functionsToCall.push(functionToAdd);
this.checkOrSetTimeout();
};
BulkAnimator.prototype.checkOrSetTimeout = function() {
if(this.animating)
{
return;
}
this.animating = true;
var _this = this;
var toCall = function() {
_this.animateAll();
};
//now we can add functions to this for the next interval amount of time
//and they will all be updated at once. I'm hoping this severely improves performance, because
//it will essentially be more like "frame draws" than all this sync drawing. So it's going
//Async particles -> async animating -> collection into bulkAnimator -> sync drawing
this.timeout = setTimeout(toCall,this.interval);
};
BulkAnimator.prototype.stopAnimating = function() {
this.animating = false;
this.functionsToCall = [];
};
BulkAnimator.prototype.animateAll = function() {
//we have to reset our vars here because the functions we call might
//start adding new functions
var functions = this.functionsToCall;
this.animating = false;
this.functionsToCall = [];
for(var i = 0; i < functions.length; i++)
{
functions[i]();
}
};
function GravityArrow() {
var height = $j(window).height();
var pos = vecMake(40,height-60);
this.pos = pos;
this.rArrow = new rArrow(pos,globalAccel);
this.text = p.text(pos.x,pos.y + 40,"Gravity");
this.text.attr({
'fill':'#FFF',
'stroke':'#FFF',
'stroke-width':0,
'font-size':20
});
};
function GravityTweener(gStart,gEnd,time,doneFunction) {
//the "rotationLayer" is the HTML node we need to rotate to show the gravity direction.
if(!gStart || !gEnd || !time)
{
debugger;
throw new Error("bad args in gravity tweener");
}
//check for bad vectors
if( String(gStart.x) == 'NaN' || String(gEnd.x) == 'NaN')
{ throw new Error("NAN in gt"); }
this.doneFunction = doneFunction;
this.totalTime = time;
this.gEnd = gEnd;
var startSituation = rotationLayerRad;
//lets compare the start and end situations to avoid "snapping" the grid. first, we need to understand
//how gravity directions correspond to their "angles" of rotation. In this situation, an angle of rotation
//of 0 corresponds to a gravity direction of "down" when viewing the screen. unfortunately, the screen
//coordinates are flipped so this acceleration vector is actually pointing up! so the
//atan2 of the acceleration vector is + Pi / 2, and the rotation angle is just 0
this.direction = -1;
//hence, in order to get a gravity vector for a rotation angle, we add Math.PI * 0.5
var currentGravDirection = this.degToVec(startSituation - Math.PI * 0.5);
//then we get our normalize gravity vector
var gStartN = vecNormalize(vecMake(gStart.x,-gStart.y));
//now we can see if the current degree of rotation of the grid
//corresponds to our starting gravity direction:
if(vecDot(gStartN,currentGravDirection) < 0.98)
{
console.warn("Current situation and our starting grav direction don't agree!!");
}
//ok, lets get some values here
this.startAngle = this.gToDeg(gStart);
this.endAngle = this.gToDeg(gEnd);
//there is one tricky situation with wraparound of angles where we might rotate in the wrong direction.
//this is why we can't use Tween.js because it doesn't recognize trig wraparounds. so, if we are animating
//between two vectors like this:
//
// n |
// \|
// --------+---------
// /|
// v |
//
//we will accidentally rotate way more degrees than necessary. to detect this...
if(Math.abs(this.endAngle - this.startAngle) > Math.PI)
{
//we are rotating too much. there are a couple of situations here....
//we want to modify one of the angles by 2 pi to wrap it around so a linear interp
//or cubic easeinout works correctly.
if(this.startAngle > Math.PI)
{
this.startAngle -= Math.PI * 2;
}
else if(this.endAngle > Math.PI)
{
this.endAngle -= Math.PI * 2;
}
else if(this.startAngle < -1*Math.PI)
{
this.startAngle += Math.PI * 2;
}
else if(this.endAngle < -1 * Math.PI)
{
this.endAngle += Math.PI * 2;
}
}
//wraparound should be good, go interp between these two angles
};
GravityTweener.prototype.start = function() {
this.animateStep(0);
};
GravityTweener.prototype.degToVec = function(deg) {
return vecMake(Math.cos(deg),Math.sin(deg));
};
GravityTweener.prototype.gToDeg = function(vector) {
var angle = Math.atan2(-vector.y,vector.x);
angle += Math.PI * 0.5;
return angle;
};
GravityTweener.prototype.cubicEaseInOut = function(x) {
//here x is just between 0 and 1... b = 0, c = 1, etc
var t = x / 0.5;
if(t < 1)
{
return 0.5 * t * t * t;
}
t -= 2;
return 0.5 * (t * t * t + 2);
};
GravityTweener.prototype.animateStep = function(timeVal) {
var progressX = timeVal / this.totalTime;
if(progressX > 1)
{
rotationLayerRad = this.gToDeg(this.gEnd);
globalAccel = this.gEnd;
if(this.doneFunction) {
this.doneFunction();
}
return;
}
var progressY = this.cubicEaseInOut(progressX);
var rotAngleNow = this.startAngle + progressY * (this.endAngle - this.startAngle);
var gVec = this.degToVec(rotAngleNow + Math.PI * 0.5);
if(gArrow)
{
gArrow.rArrow.remove();
gArrow.rArrow = new rArrow(gArrow.pos,vecScale(gVec,vecLength(globalAccel)));
}
var toSet = 'rotate3d(0,0,1,' + String(rotAngleNow) + 'rad)';
//set it
$j(rotationLayer).css('-webkit-transform',toSet);
$j(rotationLayer).css('-moz-transform',toSet);
//add ourselves :D
var _this = this;
var f = function() {
_this.animateStep(timeVal + globalAnimateSpeed);
};
bAnimator.add(f);
};
function uiControl(parentObj) {
this.active = false;
this.UIbutton = null;
this.parentObj = parentObj;
//we have to bind the "this" scope to our object for the
//event handlers
var _this = this;
var cc = function(e) {
_this.canvasClick(e);
};
var cm = function(e) {
_this.canvasMove(e);
};
var crc = function(e) {
_this.canvasRightClick(e);
};
var mu = function(e) {
_this.canvasMouseUp(e);
};
var kd = function(e) {
_this.canvasKeyDown(e);
};
//register event handlers
$j('#canvasHolder').bind('mousedown',cc);
$j('#canvasHolder').bind('mousemove',cm);
$j('#canvasHolder').bind('contextmenu',crc);
$j('#canvasHolder').bind('mouseup',mu);
$j(document).bind('keydown',kd);
};
uiControl.prototype.canvasKeyDown = function(e) {
if(this.parentObj.active)
{
var which = e.which;
this.parentObj.keyDown(which,e);
}
};
uiControl.prototype.canvasRightClick = function(e) {
if(this.parentObj.active)
{
e.preventDefault();
}
};
uiControl.prototype.canvasMouseUp = function(e) {
if(this.parentObj.active)
{
this.parentObj.mouseUp(e.offsetX,e.offsetY);
}
};
uiControl.prototype.canvasMove = function(e) {
if(this.parentObj.active)
{
var x = e.offsetX; var y = e.offsetY;
if(!x) { x = e.pageX; y = e.pageY; } //FF
this.parentObj.mouseMove(x,y,e);
}
};
uiControl.prototype.canvasClick = function(e) {
if(!this.parentObj.active)
{
return;
}
var x = e.offsetX;
var y = e.offsetY;
if(!x) //FF
{
x = e.pageX;
y = e.pageY;
}
if(e.which == 1)
{
this.parentObj.leftClick(x,y);
}
else
{
this.parentObj.rightClick(x,y);
}
};
//stubs
uiControl.prototype.mouseUp = function(x,y) {
return;
};
uiControl.prototype.rightClick = function(x,y) {
return;
};
uiControl.prototype.mouseMove = function(x,y) {
return;
};
uiControl.prototype.leftClick = function(x,y) {
return;
};
uiControl.prototype.keyDown = function(which) {
return;
};
function UIButton(parentObj,id,text,activeText,buttonsToShow) {
this.active = false;
this.parentObj = parentObj;
this.text = text;
this.activeText = activeText;
this.id = id;
if(buttonsToShow) {
var buttons = buttonsToShow.map(function(id) { return "#" + id; });
this.buttonsToShow = buttons.join(',');
} else {
this.buttonsToShow = "";
}
this.mainButtons = ['addPolyButton','traceButton','editPolyButton',
'importExportButton','clearButton','solveButton'];
this.mainButtons = this.mainButtons.map(function(id) { return "#" + id; });
this.mainButtons = this.mainButtons.join(",");
var _this = this;
var cHandler = function(e) {
_this.anchorClick();
};
$j('#' + this.id).click(cHandler);
};
UIButton.prototype.hideAllButtons = function() {
$j('.uiButton').slideUp();
};
UIButton.prototype.showMainButtons = function() {
$j(this.mainButtons).slideDown();
};
UIButton.prototype.anchorClick = function() {
var nots = ":not(#" + this.id + ")";
if(!this.active)
{
this.parentObj.activate();
$j('#' + this.id).text(this.activeText);
$j('.uiButton').filter(nots).slideUp();
$j(this.buttonsToShow).slideDown();
}
else
{
this.parentObj.deactivate();
$j('#' + this.id).text(this.text);
$j(this.buttonsToShow).slideUp();
$j(this.mainButtons).filter(nots).slideDown();
}
};
function rArrow(pos,vel) {
if (!pos || !vel) { throw new Error("null arguments!"); }
//ok so we want to essentially make a path that looks like an arrow
//this consists of making a path. first we start at our position, and then
//we move some fraction of our velocity in the velocity direction to get our second point.
//then we draw the arrow heads. this is done by doing some vector rotations and the like
this.pos = pos;
this.vel = vel;
this.path = null;
this.buildPath();
};
rArrow.prototype.buildPath = function() {
//ok get the first point, that's easy
var points = [];
var tail = this.pos;
points.push(tail);
//now get the head
var fraction = 0.5;
var velScaled = vecScale(this.vel,fraction);
var head = vecAdd(velScaled,tail);
points.push(head);
//ok so we need to do something simple. first get the angle from the head to the tail
//aka, the atan2 of the negated velocity
var fromHeadToTail = vecNegate(this.vel);
var angle = vecAtan2(fromHeadToTail);
//now add 45 to get chevron1 (i know these aren't chevrons but i dont have a good name
//for the little dangly things off the arrow head).
//and subtract 45 to get chevron2
var chev1Angle = angle + Math.PI * 0.25;
var chev2Angle = angle - Math.PI * 0.25;
//get these vectors
var chev1Vec = vecScale(angleToVec(chev1Angle),vecLength(this.vel) * 0.1);
var chev2Vec = vecScale(angleToVec(chev2Angle),vecLength(this.vel) * 0.1);
//get these points
var chev1point = vecAdd(head,chev1Vec);
var chev2point = vecAdd(head,chev2Vec);
points.push(chev1point,head,chev2point,head);
//now the path. god this is a long process
var pathStr = constructPathStringFromCoords(points);
var velMag = vecLength(this.vel);
var extra = map(velMag,0,1000,0,10);
var strokeWidth = 2 + Math.round(extra);
this.path = p.path(pathStr);
this.path.attr({
'stroke-width':strokeWidth,
'stroke':velocityHue(this.vel),
'stroke-linecap':'round',
'stroke-linejoin':'round'
});
};
rArrow.prototype.update = function(pos,vel) {
if(this.path)
{
this.path.remove();
}
this.pos = pos;
this.vel = vel;
this.buildPath();
};
rArrow.prototype.remove = function() {
if(this.path)
{
this.path.remove();
}
};
rArrow.prototype.highlight = function() {
if(this.path)
{
this.path.attr({
'stroke':'#F00',
'stroke-width':5
});
}
};
/*^^^^^ general UI classes ^^^^*/
/*
*-> specific UI classes <- */
function polygonUIControl() {
this.uiPoints = [];
this.uiPath = null;
this.currentPoint = null;
this.firstTime = true;
this.active = false;
this.prototype = new uiControl(this);
this.UIbutton = new UIButton(this,'addPolyButton','Add Polygon','Stop Adding Polygons');
};
polygonUIControl.prototype.deactivate = function() {
//remove our path and points from the screen
$j.each(this.uiPoints,function(index,point) {
point.remove();
});
if(this.uiPath) { this.uiPath.remove(); }
if(this.currentPoint) { this.currentPoint.remove(); }
this.active = false;
//set our button as well
this.UIbutton.active = false;
$j('#canvasHolder').css('cursor','default');
};
polygonUIControl.prototype.activate = function() {
//just reset some variables
this.uiPoints = [];
this.currentPoint = null;
this.uiPath = null;
if(this.firstTime)
{
//explain what to do if its our first time
topNotifyTemp("Left Click to Add, Right Click / Space to close",5000);
this.firstTime = false;
}
this.active = true;
this.UIbutton.active = true;
$j('#canvasHolder').css('cursor','crosshair');
};
polygonUIControl.prototype.keyDown = function(which,e) {
if(!this.currentPoint || which != 32)
{
//we aren't inserting, dont do anything. or hit wrong button
return;
}
var x = this.currentPoint.attr('cx');
var y = this.currentPoint.attr('cy');
//if it's a space, then go close the polygon?
this.rightClick(x,y);
};
polygonUIControl.prototype.rightClick = function(x,y) {
var shouldAdd = true;
//check if we should add this, this is a UI thing where users make
//mistakes
for(var i = 0; i < this.uiPoints.length; i++)
{
var uX = this.uiPoints[i].attr('cx');
var uY = this.uiPoints[i].attr('cy');
var dist = distBetween(vecMake(uX,uY),vecMake(x,y));
if(dist < 5)
{
shouldAdd = false;
}
}
if(shouldAdd)
{
//close the path basically and make a polygon with this
var c = cuteSmallCircle(x,y);
this.uiPoints.push(c);
}
var aPathString = constructPathStringFromPoints(this.uiPoints,true);
//to dump this
var polyPath = cutePath(aPathString,true);
var clonedPoints = [];
for(var i = 0; i < this.uiPoints.length; i++ ) { clonedPoints.push(this.uiPoints[i].clone()); }
var results = polyController.makePolygon(clonedPoints,polyPath);
//dump the ui stuff
this.resetUIVars();
};
polygonUIControl.prototype.resetUIVars = function() {
this.uiPath.remove();
this.currentPoint.remove();
$j.each(this.uiPoints,function(i,point) {
point.remove();
});
this.uiPoints = [];
this.uiPath = null;
this.currentPoint = null;
};
polygonUIControl.prototype.leftClick = function(x,y) {
var c = cuteSmallCircle(x,y);
if(this.uiPath)
{
this.uiPath.remove();
}
this.uiPoints.push(c);
//do a move to restore the path so it doesn't flicker when we are clicking
this.mouseMove(x,y);
};
polygonUIControl.prototype.mouseUp = function(x,y) {
return;
};
polygonUIControl.prototype.mouseMove = function(x,y) {
//only do this when there is already one point
if(!this.uiPoints.length) { return; }
if(this.currentPoint)
{
this.currentPoint.remove();
}
//make a point underneath the mouse
this.currentPoint = cuteSmallCircle(x,y,true);
//append this point to the current points we have created
var pointsCopy = this.uiPoints.slice(0);
pointsCopy.push(this.currentPoint);
//construct a path from this and draw it
var pathString = constructPathStringFromPoints(pointsCopy,true);
if(this.uiPath) { this.uiPath.remove(); }
this.uiPath = cutePath(pathString);
};
function SolveUIControl() {
this.active = false;
this.firstTime = true;
this.isAnimating = false;
this.verticesToSolve = {};
this.prototype = new uiControl(this);
this.UIbutton = new UIButton(this,'solveButton','Solve!','Stop Solving');
};
//for some reason the stubs from the prototype dont get inherited
SolveUIControl.prototype.mouseMove = function() { return; }
SolveUIControl.prototype.mouseUp = function() { return; }
SolveUIControl.prototype.leftClick = function(x,y) {
if (this.isAnimating) { return; }
if (this.isSearching) { return; }
// ok so make a particle here and wait for it to settle...
var pos = {x: x, y: y};
var vel = {x: 0, y: 0};
var accel = globalAccel;
var kState = new KineticState(pos, vel, accel);
var particle = partController.makeParticle(kState, accel);
var name = particle.settleResults.endLocationName;
var obj = particle.settleResults.endLocationObj;
if (name !== 'offScreen') {
this.verticesToSolve[name] = obj;
}
return;
}
SolveUIControl.prototype.keyDown = function(which) {
if (which !== 32) {
return;
}
if (_.keys(this.verticesToSolve).length == 0) {
topNotifyTemp("No particles settled in concave vertices, nothing to solve!", 3000);
return;
}
this.startSearch();
return;
}
SolveUIControl.prototype.rightClick = function() { };
SolveUIControl.prototype.vertexClick = function(vertex) { };
SolveUIControl.prototype.searchFinished = function() {
LAST_SOLVED = this.verticesToSolve;
this.verticesToSolve = {};
this.isAnimating = false;
this.isSearching = false;
};
SolveUIControl.prototype.searchOn = function() {
this.isAnimating = true;
this.isSearching = true;
};
SolveUIControl.prototype.startSearch = function() {
this.isAnimating = true;
this.isSearching = true;
partController.clearAll();
topNotifyTemp('Searching for solution...', 3000);
var vertices = [];
_.each(this.verticesToSolve, function(vertex) {
vertices.push(vertex);
}, this);
setTimeout(_.bind(function() {
searcher = new GraphSearcher(vertices);
searcher.search();
}, this), 200);
};
SolveUIControl.prototype.activate = function() {
if(this.firstTime) {
topNotifyTemp("Click to drop particles, spacebar to solve!",3000);
this.firstTime = false;
}
this.verticesToSolve = {};
this.active = true;
this.UIbutton.active = true;
//reset all particles
partController.clearAll();
this.setCursor('pointer');
};
SolveUIControl.prototype.deactivate = function() {
this.active = false;
this.isAnimating = false;
this.isSearching = false;
this.UIbutton.active = false;
if (_.keys(this.verticesToSolve).length) {
LAST_SOLVED = this.verticesToSolve;
}
this.verticesToSolve = {};
this.setCursor('default');
};
SolveUIControl.prototype.setCursor = function(type) {
$j('#canvasHolder').css('cursor',type);
};
function EditUIControl() {
//this might be pretty simple
this.active = false;
this.firstTime = true;
this.prototype = new uiControl(this);
this.UIbutton = new UIButton(this,'editPolyButton','Edit Polygons','Stop Editing Polygons');
};
//for some reason the stubs from the prototype dont get inherited
EditUIControl.prototype.mouseMove = function() { return; }
EditUIControl.prototype.mouseUp = function() { return; }
EditUIControl.prototype.leftClick = function() { return; }
EditUIControl.prototype.keyDown = function() { return; }
EditUIControl.prototype.rightClick = function() { return; }
EditUIControl.prototype.activate = function() {
if(this.firstTime)
{
topNotifyTemp("Drag polygons / individual vertices",3000);
this.firstTime = false;
}
this.active = true;
this.UIbutton.active = true;
//reset all particles
partController.clearAll();
this.setCursor('move','pointer');
};
EditUIControl.prototype.deactivate = function() {
this.active = false;
this.UIbutton.active = false;
this.setCursor('default','default');
};
EditUIControl.prototype.setCursor = function(pathType,pointType) {
//go make all the polygon rPaths have the right cursor
for(var i = 0; i < polyController.polys.length; i++)
{
var poly = polyController.polys[i];
var path = poly.rPath;
var vertices = poly.vertices;
$j(path.node).css('cursor',pathType);
for(var j = 0; j < vertices.length; j++)
{
$j(vertices[j].rPoint.node).css('cursor',pointType);
}
}
};
function TraceUIControl() {
this.resetVars();
this.firstTime = true;
this.accel = globalAccel;
this.prototype = new uiControl(this);
this.UIbutton = new UIButton(this,'traceButton',
'Trace Particle','Stop Tracing Particles',
['clearParticlesButton','bombardButton','togglePathsButton']);
};
TraceUIControl.prototype.clearScreen = function() {
//clear screen
if(this.startPoint) { this.startPoint.remove(); }
if(this.endPoint) { this.endPoint.remove(); }
if(this.parab) { this.parab.removePath(); }
if(this.path) { this.path.remove(); }
};
TraceUIControl.prototype.deactivate = function() {
this.clearScreen();
this.resetVars();
this.active = false;
//set our button as well
this.UIbutton.active = false;
$j('#canvasHolder').css('cursor','default');
};
TraceUIControl.prototype.resetVars = function() {
this.startPoint= null
this.endPoint = null;
this.parab = null;
this.path = null;
this.s = null;
this.vel = null;
};
TraceUIControl.prototype.activate = function() {
this.accel = globalAccel;
//just reset some variables
this.resetVars();
if(this.firstTime)
{
this.firstTime = false;
topNotifyTemp("Left click, drag, and release to shoot",4000);
}
this.active = true;
this.UIbutton.active = true;
$j('#canvasHolder').css('cursor','crosshair');
};
TraceUIControl.prototype.rightClick = function(x,y) {
//just return I think? or do a random arc from here
return;
};
TraceUIControl.prototype.keyDown = function(which,e) {
return;
};
TraceUIControl.prototype.mouseUp = function(x,y) {
//make the particle and advance it once
var k = new KineticState(this.s,this.vel,this.accel);
var inside = false;
var polys = polyController.polys;
for(var j = 0; j < polys.length; j++)
{
if(polys[j].rPath.isPointInside(this.s.x,this.s.y))
{
inside = true;
break;
}
}
if(inside)
{
this.clearScreen();
this.resetVars();
return; //dont launch particles inside polygons
}
var particle = partController.makeParticle(k,this.accel);
//DEBUG
part = particle;
//make sure to reset our vars
this.clearScreen();
this.resetVars();
};
TraceUIControl.prototype.leftClick = function(x,y) {
this.accel = globalAccel;
//this is essentially the mousedown left click
this.startPoint = cuteSmallCircle(x,y);
this.s = {
'x':x,
'y':y
};
var now = new Date();
this.startTime = now.getTime();
this.mouseMove(x,y);
};
TraceUIControl.prototype.mouseMove = function(x,y,e) {
if(e && !e.which)
{
this.clearScreen();
this.resetVars();
return;
}
//only do the moving if our mouse is down
if(!this.startPoint)
{
return;
}
var now = new Date();
var time = now.getTime();
if(time - this.startTime < 1000 && false)
{
return;
}
this.startTime = time;
if(this.parab) { this.parab.removePath(); }
if(this.path) { this.path.remove(); }
if(this.endPoint) { this.endPoint.remove(); }
//for mouse move, set the second point and make the velocity
this.endPoint = cuteSmallCircle(x,y);
this.e = {x:x,y:y};
//make a path connecting them
var pathString = constructPathStringFromPoints([this.startPoint,this.endPoint],false);
this.path = cutePath(pathString);
this.vel = {
'x':x - this.s.x,
'y':y - this.s.y
};
//now we have start, vel, and accel