-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshapes.js
1574 lines (1066 loc) · 34.5 KB
/
shapes.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
// By Simon Sarris
// www.simonsarris.com
// sarris@acm.org
//
// Last update December 2011
//
// Free to use and distribute at will
// So long as you are nice to people, etc
// Constructor for Shape objects to hold data for all drawn objects.
// For now they will just be defined as rectangles.
function Shape(x, y, shape, text) {
// This is a very simple and unsafe constructor. All we're doing is checking if the values exist.
// "x || 0" just means "if there is a value for x, use that. Otherwise use 0."
// But we aren't checking anything else! We could put "Lalala" for the value of x
this.shapeId = 0;
this.x = x || 0;
this.y = y || 0;
this.shape = shape || "square";
this.fill = '#EEEEEE';
this.lineWidth = 1;
this.lineHeight = 20;
if(this.shape === 'square'){
this.w = 100;
this.h = 50;
}
this.connector = new Connector(this.x + this.w/2.0 - 10/2.0,
this.y + this.h,
10);
this.deleteBox = new DeleteBox(this.x + this.w - 14, y);
this.text = text || 'sampleText';
//this.setText(text);
}
Shape.prototype.setWidth = function(value){
this.w = value;
this.connector.x = this.x + this.w/2.0 - 5;
this.deleteBox.x = this.x + this.w - 14;
}
Shape.prototype.setHeight = function(value){
this.h = value;
this.connector.y = this.y + this.h;
}
Shape.prototype.move = function(x, y){
//rejig to move everything as an offset
//vector based...?
this.x += x;
this.y += y;
/*
var oldX = this.x;
var oldY = this.y;
this.x = x;
this.y = y;
offsetX = this.x - oldX;
offsetY = this.y - oldY;
*/
var connectorOldX = this.connector.x;
var connectorOldY = this.connector.y;
var connectorOldTargetX = this.connector.targetX;
var connectorOldTargetY = this.connector.targetY;
this.connector.x += x;
this.connector.y += y;
this.connector.targetX += x;
this.connector.targetY += y;
this.deleteBox.x += x;
this.deleteBox.y += y;
}
Shape.prototype.highlight = function(ctx, mode) {
if (this.shape === "square"){
var minX = this.x;
var minY = this.y;
var maxX = this.x + this.w;
var maxY = this.y + this.h;
var offset = 10;
if (mode === "hover")
ctx.strokeStyle = '#FF0000';
else
ctx.strokeStyle = '#00FF00';
ctx.beginPath();
ctx.moveTo(minX + offset, minY);
ctx.lineTo(maxX - offset, minY);
ctx.quadraticCurveTo(maxX, minY, maxX, minY + offset);
ctx.lineTo(maxX, maxY - offset);
ctx.quadraticCurveTo(maxX, maxY, maxX - offset, maxY);
ctx.lineTo(minX + offset, maxY);
ctx.quadraticCurveTo(minX, maxY, minX, maxY - offset);
ctx.lineTo(minX, minY + offset);
ctx.quadraticCurveTo(minX, minY, minX + offset, minY);
ctx.stroke();
ctx.closePath();
}
}
Shape.prototype.centerPos = function(){
var centerPos = [0, 0];
centerPos[0] = this.x + this.w/2.0;
centerPos[1] = this.y + this.h/2.0;
return centerPos;
}
// Draws this shape to a given context
Shape.prototype.draw = function(ctx) {
if (this.shape === "square"){
ctx.save();
//ctx.rect(this.x, this.y, this.w, this.h);
var minX = this.x;
var minY = this.y;
var maxX = this.x + this.w;
var maxY = this.y + this.h;
var offset = 10;
ctx.beginPath();
ctx.moveTo(minX + offset, minY);
ctx.lineTo(maxX - offset, minY);
ctx.quadraticCurveTo(maxX, minY, maxX, minY + offset);
ctx.lineTo(maxX, maxY - offset);
ctx.quadraticCurveTo(maxX, maxY, maxX - offset, maxY);
ctx.lineTo(minX + offset, maxY);
ctx.quadraticCurveTo(minX, maxY, minX, maxY - offset);
ctx.lineTo(minX, minY + offset);
ctx.quadraticCurveTo(minX, minY, minX + offset, minY);
ctx.fillStyle = this.fill;
ctx.shadowColor = "#999";
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.fill();
ctx.closePath();
ctx.restore();
//DRAW THE CONNECTOR
ctx.fillStyle = 'black';
ctx.fillRect(this.connector.x,
this.connector.y,
this.connector.w,
this.connector.w);
this.deleteBox.draw(ctx);
}
// DRAW THE TEXT
var text = this.text;
var offsetX = 0;
var offsetY = 0;
if (this.shape === "square"){
offsetX = 15;
offsetY = 30;
}
else if (this.shape === "circle"){
offsetX = -10;
}
//ctx.font="14px Avenir";
//ctx.fillText(text, this.x + offsetX, this.y + offsetY);
//var metrics = ctx.measureText(text);
//var width = metrics.width;
this.wrapText(ctx, text, this.x + offsetX, this.y + offsetY, 100, 20);
}
Shape.prototype.setText = function(text, ctx) {
this.text = text;
/* sets the size dependent on text content -
issue, need to get the CTX in order to determine
the proper dimensions for it */
ctx.font="14px Avenir";
//ctx.fillText(text, this.x, this.y);
var lines = text.split('\n');
var lineHeight = 20;
var maxHeight = 0;
var maxWidth = 0;
for (i in lines){
var line = lines[i];
var metrics = ctx.measureText(line);
var width = metrics.width;
if (width > maxWidth)
maxWidth = width;
maxHeight += lineHeight;
}
this.setWidth(maxWidth + 30);
this.setHeight(maxHeight + 30);
//this.wrapText(ctx, text, this.x + offsetX, this.y + offsetY, 100, 20);
//var metrics = ctx.measureText(text);
//var width = metrics.width;
//this.setWidth(width + 30);
}
// Determine if a point is inside the shape's bounds
Shape.prototype.contains = function(mx, my) {
//need to extend for circle code
if (this.shape === "square"){
return (this.x <= mx) && (this.x + this.w >= mx) &&
(this.y <= my) && (this.y + this.h >= my);
}
}
Shape.prototype.containedInRect = function(points) {
var x1 = Math.min(this.x, this.x + this.w);
var y1 = Math.min(this.y, this.y + this.h);
var x2 = Math.max(this.x, this.x + this.w);
var y2 = Math.max(this.y, this.y + this.h);
var rect_x1 = Math.min(points[0], points[2]);
var rect_y1 = Math.min(points[1], points[3]);
var rect_x2 = Math.max(points[0], points[2]);
var rect_y2 = Math.max(points[1], points[3]);
if (x1 < rect_x2 && x2 > rect_x1 &&
y1 < rect_y2 && y2 > rect_y1)
return true;
return false;
}
Shape.prototype.wrapText = function(context, text, x, y, maxWidth, lineHeight) {
/* need to rework this...
new line needs to happen when enter is pressed in the textarea
allow long run ons
only add new line when new line is pressed in box
also need to get the longest element so as to resize the whole box
*/
context.font="14px Avenir";
var lines = text.split('\n');
var height = y;
var maxWidth = 0;
for (i in lines){
context.fillText(lines[i], x, height);
height += this.lineHeight;
}
//this.setWidth(maxWidth);
//reset width AND height (!!) here after max line length
}
/////////////////////////////////////////////////////////////////
// Connector class, attached to a shape
function Connector(x, y, w){
this.x = x || 0;
this.y = y || 0;
this.targetX = x || 0;
this.targetY = y || 0;
this.w = w || 5;
}
// Determine if a point is inside the shape's bounds
Connector.prototype.contains = function(mx, my) {
return (this.x <= mx) && (this.x + this.w >= mx) &&
(this.y <= my) && (this.y + this.w >= my);
}
////////////////////////////////////////////////////////////////////////////
function Connection(origShape, destShape){
/* class for keeping track of connections,
kept independent to avoid recursive objects */
//original shape has to be there
this.origShape = origShape;
//dest shape is optional
this.destShape = destShape || null;
//custom coords in case of no destShape
this.targetX = 0;
this.targetY = 0;
this.resetTarget();
}
Connection.prototype.draw = function(ctx){
/* draw a line either between two shapes or between the original shape
and a target position */
origX = this.origShape.centerPos()[0];
origY = this.origShape.centerPos()[1];
if (this.destShape){
destX = this.destShape.centerPos()[0];
destY = this.destShape.centerPos()[1];
}
else {
destX = this.targetX;
destY = this.targetY;
}
ctx.beginPath();
ctx.moveTo(origX, origY)
ctx.lineTo(destX, destY);
ctx.stroke();
ctx.closePath();
}
Connection.prototype.contains = function(mx, my){
/* should probably clean this up a little bit */
//figure out the points
var eps = 4;
var x1 = Math.min(this.origShape.centerPos()[0], this.destShape.centerPos()[0]);
var y1 = Math.min(this.origShape.centerPos()[1], this.destShape.centerPos()[1]);
var x2 = Math.max(this.origShape.centerPos()[0], this.destShape.centerPos()[0]);
var y2 = Math.max(this.origShape.centerPos()[1], this.destShape.centerPos()[1]);
//first, check if mouse is in the rectangle described by the 2 points...
if ((x1 <= mx + eps) && (x2 >= mx - eps) && (y1 <= my + eps) && (y2 >= my - eps)){
//define the definition of the line
//need to find a way to deal with big numbers!!!
var x1 = this.origShape.centerPos()[0];
var y1 = this.origShape.centerPos()[1];
var x2 = this.destShape.centerPos()[0];
var y2 = this.destShape.centerPos()[1];
var xDiff = x2 - x1;
var yDiff = y2 - y1;
var m = yDiff/xDiff;
//equation of a line
var d = y1 - m * x1;
//figure out the compensator
var val = m * mx + d - my;
//invert values below 1
if (m < 1 && m >= 0)
m = Math.pow(m, -1);
if (m > -1 && m < 0)
m = Math.pow(m, -1);
eps = Math.abs(eps * m);
if (val < eps &&
val > -(eps))
return true;
return false;
}
}
Connection.prototype.highlight = function(ctx, mode){
origX = this.origShape.centerPos()[0];
origY = this.origShape.centerPos()[1];
destX = this.destShape.centerPos()[0];
destY = this.destShape.centerPos()[1];
ctx.lineWidth = 6;
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(origX, origY)
ctx.lineTo(destX, destY);
ctx.stroke();
ctx.closePath();
ctx.lineWidth = 4;
ctx.strokeStyle = 'lightgrey';
}
Connection.prototype.setTarget = function(x, y) {
this.targetX = x;
this.targetY = y;
}
Connection.prototype.resetTarget = function() {
this.targetX = this.origShape.connector.x;
this.targetY = this.origShape.connector.y;
}
Connection.prototype.removeClosestShape = function(mx, my) {
var x1 = this.origShape.centerPos()[0];
var y1 = this.origShape.centerPos()[1];
var x2 = this.destShape.centerPos()[0];
var y2 = this.destShape.centerPos()[1];
//a^2 + b^2 = c^2
var a1 = Math.pow(mx - x1, 2);
var b1 = Math.pow(my - y1, 2);
var c1 = a1 + b1;
var a2 = Math.pow(mx - x2, 2);
var b2 = Math.pow(my - y2, 2);
var c2 = a2 + b2;
if (c2 > c1){
//remove origShape
this.origShape = this.destShape;
this.destShape = null;
}
else{
this.destShape = null;
}
//reset the target points...
this.targetX = mx;
this.targetY = my;
}
////////////////////////////////////////////////////////////////////////////
function DeleteBox(x, y){
this.x = x;
this.y = y;
this.w = 8;
this.offset = 5;
}
DeleteBox.prototype.contains = function(mx, my) {
return (this.x <= mx) && (this.x + this.w >= mx) &&
(this.y + this.offset <= my) && (this.y + this.offset + this.w >= my);
}
DeleteBox.prototype.draw = function(ctx){
ctx.strokeStyle = 'lightgrey';
ctx.lineWidth = 4;
ctx.beginPath();
var y = this.y + this.offset;
ctx.moveTo(this.x, y);
ctx.lineTo(this.x + this.w, y + this.w);
ctx.moveTo(this.x + this.w, y);
ctx.lineTo(this.x, y + this.w);
ctx.closePath();
ctx.stroke();
}
////////////////////////////////////////////////////////////////////////////
function Modal(){
/* access to the bootstrap model */
this.jqModal = $('#myModal');
this.jqTextField = this.jqModal.find('#nodeText');
this.jqOkButton = this.jqModal.find('#modalok');
//colorButtons
this.colors = ['#EEEEEE', 'white', 'grey', 'lightgreen', '#FF6666'];
this.colorButtons = [
this.jqModal.find('#colorBox1'),
this.jqModal.find('#colorBox2'),
this.jqModal.find('#colorBox3'),
this.jqModal.find('#colorBox4'),
this.jqModal.find('#colorBox5')
];
//set default col to GREY
this.currentColor = this.colors[0];
this.setButtonClickEvents();
}
Modal.prototype.show = function(){
this.jqModal.modal('show');
}
Modal.prototype.setButtonClickEvents = function(){
var _this = this;
this.colorButtons[0].on(
'click',
function(evt){
_this.currentColor = _this.colors[0];
_this.setColorBtnActive(_this.colorButtons[0]);
}
);
this.colorButtons[1].on(
'click',
function(evt){
_this.currentColor = _this.colors[1];
_this.setColorBtnActive(_this.colorButtons[1]);
}
);
this.colorButtons[2].on(
'click',
function(evt){
_this.currentColor = _this.colors[2];
_this.setColorBtnActive(_this.colorButtons[2]);
}
);
this.colorButtons[3].on(
'click',
function(evt){
_this.currentColor = _this.colors[3];
_this.setColorBtnActive(_this.colorButtons[3]);
}
);
this.colorButtons[4].on(
'click',
function(evt){
_this.currentColor = _this.colors[4];
_this.setColorBtnActive(_this.colorButtons[4]);
}
);
}
Modal.prototype.setNode = function(node){
/* sets up the contents of a modal according to a node */
this.setText(node.text);
this.setColor(node.fill);
}
Modal.prototype.setText = function(text){
this.jqTextField.val(text);
}
Modal.prototype.setColor = function(fill){
for (i in this.colors){
if (this.colors[i] === fill){
this.currentColor = this.colors[i];
this.setColorBtnActive(this.colorButtons[i]);
}
}
}
Modal.prototype.setColorBtnActive = function(button){
for (i in this.colorButtons)
this.colorButtons[i].removeClass('color-box-selected');
button.addClass('color-box-selected');
}
Modal.prototype.setCallback = function(state, node, ctx){
var _this = this;
this.jqOkButton.on(
'click',
function(evt)
{
node.setText(_this.jqTextField.val(), ctx);
node.fill = _this.currentColor;
_this.jqModal.modal('hide');
state.valid = false; // Something's dragging so we must redraw
_this.resetCallback();
}
);
}
Modal.prototype.resetCallback = function(){
this.jqOkButton.off();
}
Modal.prototype.setPosition = function(x, y){
var width = document.body.clientWidth;
var val = width/2 - x;
//this.jqModal.css( "margin-left", -val);
//this.jqModal.css( "margin-right", val);
var doc = document.documentElement;
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
this.jqModal.css( "top", top);
this.jqModal.css( "margin-bottom", -top);
}
////////////////////////////////////////////////////////////////////////////
function CanvasState(canvas) {
// **** First some setup! ****
this.canvas = canvas;
this.canvas.width = document.body.clientWidth; //document.width is obsolete
this.canvas.height = document.body.clientHeight * 2; //document.height is obsolete
this.width = canvas.width;
this.height = canvas.height;
this.ctx = canvas.getContext('2d');
//this.ctx.scale(2,2);
//this.ctxScale = 2;
this.currentShapeId = 1;
this.modal = new Modal();
// This complicates things a little but but fixes mouse co-ordinate problems
// when there's a border or padding. See getMouse for more detail
var stylePaddingLeft, stylePaddingTop, styleBorderLeft, styleBorderTop;
if (document.defaultView && document.defaultView.getComputedStyle) {
this.stylePaddingLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingLeft'], 10) || 0;
this.stylePaddingTop = parseInt(document.defaultView.getComputedStyle(canvas, null)['paddingTop'], 10) || 0;
this.styleBorderLeft = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderLeftWidth'], 10) || 0;
this.styleBorderTop = parseInt(document.defaultView.getComputedStyle(canvas, null)['borderTopWidth'], 10) || 0;
}
// Some pages have fixed-position bars (like the stumbleupon bar) at the top or left of the page
// They will mess up mouse coordinates and this fixes that
var html = document.body.parentNode;
this.htmlTop = html.offsetTop;
this.htmlLeft = html.offsetLeft;
// **** Keep track of state! ****
this.valid = false; // when set to false, the canvas will redraw everything
this.shapes = []; // the collection of things to be drawn
this.connections = []; // the collection of things to be drawn
this.dragging = false; // Keep track of when we are dragging
// the current selected object. In the future we could turn this into an array for multiple selection
this.selection = [];
this.hoverSelection = [];
this.connectionSelection = null;
this.dragSelect = false;
this.dragoffx = 0; // See mousedown and mousemove events for explanation
this.dragoffy = 0;
this.mx = 0;
this.my = 0;
// **** Then events! ****
// This is an example of a closure!
// Right here "this" means the CanvasState. But we are making events on the Canvas itself,
// and when the events are fired on the canvas the variable "this" is going to mean the canvas!
// Since we still want to use this particular CanvasState in the events we have to save a reference to it.
// This is our reference!
var myState = this;
//fixes a problem where double clicking causes text to get selected on the canvas
canvas.addEventListener('selectstart', function(e) { e.preventDefault(); return false; }, false);
// Up, down, and move are for dragging
canvas.addEventListener('mousedown', function(e) {
//need to figure out the cases here
// 1.) if no selection, start a drag selection
// 2.) if hover selection, start a select/delete/move
// 3.) if connector, start a connection drag
var mouse = myState.getMouse(e);
var mx = mouse.x;
var my = mouse.y;
myState.mx = mouse.x;
myState.my = mouse.y;
// CHECK FOR CONNECTIONS ///////
if (myState.hoverSelection.length){
if (myState.hoverSelection[0] instanceof Connection){
//figure out closer shape...
var mySel = myState.hoverSelection[0];
mySel.removeClosestShape(mx, my);
myState.connectionDragging = true;
myState.connectionSelection = mySel;
myState.hoverSelection = [];
//remove current connection from connections
var index = myState.connections.indexOf(mySel);
if(index > -1)
myState.connections.splice(index, 1);
myState.valid = false;
return;
}
}
// CHECK FOR CONNECTORS ///////
var shapes = myState.shapes;
var l = shapes.length;
for (var i = l-1; i >= 0; i--) {
//check the connectors first
if (shapes[i].connector.contains(mx, my)){
var mySel = new Connection(shapes[i]);
myState.connectionDragging = true;
myState.connectionSelection = mySel;
myState.valid = false;
return;
}
}
// 0.) if selection and no hover, clear the selection
if (!myState.hoverSelection.length && myState.selection.length){
myState.selection = [];
}
// 1.) if no hoverSelection, start a drag select
if (!myState.hoverSelection.length){
myState.dragSelect = true;
myState.dragSelectCoords = [mx, my, mx, my];
return;
}
// 2.) if hoverSelection, check for delete
if (myState.hoverSelection.length){
mySel = myState.hoverSelection[0];
//check for deleteBox
if (mySel.deleteBox.contains(mx, my)){
myState.deleteShape(mySel);
myState.hoverSelection = [];
myState.selection = [];
myState.valid = false;
return;
}
//if hoverSel not in sel
// clear sel
// add hoverSel to sel
if (myState.selection.indexOf(mySel) == -1){
myState.selection = [];
if(!myState.selection.length)
myState.selection.push(mySel);
}
}
if (myState.hoverSelection.length && myState.selection.length){
//myState.dragoffx = mx - mySel.x;
//myState.dragoffy = my - mySel.y;
myState.dragging = true;
myState.valid = false;
return;
}
}, true);
canvas.addEventListener('mousemove', function(e) {
//detect shapes here
//if shape found, set it to selected
var mouse = myState.getMouse(e);
var mx = mouse.x;
var my = mouse.y;
if(!myState.mx)
myState.mx = mouse.x;
if(!myState.my)
myState.my = mouse.y;
var x_diff = mouse.x - myState.mx;
var y_diff = mouse.y - myState.my;
myState.mx = mouse.x;
myState.my = mouse.y;
//if drag in process...
if (myState.dragging){
for (i in myState.selection){
myState.selection[i].move(x_diff, y_diff);
}
//myState.selection.x = mouse.x - myState.dragoffx;
//myState.selection.y = mouse.y - myState.dragoffy;
myState.valid = false; // Something's dragging so we must redraw
return;
}
//if dragSelect in process...
if (myState.dragSelect){
//set the coords
myState.dragSelectCoords[2] = mx;
myState.dragSelectCoords[3] = my;
myState.valid = false;
// put detection code in here...
var shapes = myState.shapes;
var selectedShapes = [];
var l = shapes.length;
for (var i = l-1; i >= 0; i--) {
if (shapes[i].containedInRect(myState.dragSelectCoords)){
//add to selected shapes
if (!selectedShapes.indexOf(shapes[i]) > -1)
selectedShapes.push(shapes[i]);
}
}
myState.hoverSelection = selectedShapes;
return;
}
if (!myState.hoverSelection.length){
//check for shapes and connectors!!
var shapes = myState.shapes;
var l = shapes.length;
for (var i = l-1; i >= 0; i--) {
//check object
if (shapes[i].contains(mx, my)) {
// Keep track of where in the object we clicked
// so we can move it smoothly (see mousemove)
var mySel = shapes[i];
myState.hoverSelection[0] = mySel;
myState.valid = false;
return;
}
//check connector and block other selection
var connector = shapes[i].connector;
if (shapes[i].connector.contains(mx, my))
return;
}
//check for connections...
if(!myState.connectionDragging){
var cons = myState.connections;
for (var i = cons.length-1; i >= 0; i--) {
var val = cons[i].contains(mx, my);
if (val) {
// Keep track of where in the object we clicked
// so we can move it smoothly (see mousemove)
var mySel = cons[i];
myState.hoverSelection[0] = mySel;
myState.valid = false;
return;