-
Notifications
You must be signed in to change notification settings - Fork 822
/
turtles.js
1286 lines (1134 loc) · 41.8 KB
/
turtles.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
/* eslint-disable no-undef */
/**
* @file This contains the prototype of the Turtles component.
* @author Walter Bender
*
* @copyright 2014-2020 Walter Bender
* @copyright 2020 Anindya Kundu
*
* @license
* This program is free software; you can redistribute it and/or
* modify it under the terms of the The GNU Affero General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library; if not, write to the Free Software
* Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA.
*/
/*
global createjs, platformColor, last, importMembers, setupRhythmActions, setupMeterActions,
setupPitchActions, setupIntervalsActions, setupToneActions, setupOrnamentActions,
setupVolumeActions, setupDrumActions, setupDictActions, _, Turtle, TURTLESVG, METRONOMESVG,
FILLCOLORS, STROKECOLORS, getMunsellColor, DEFAULTVALUE, DEFAULTCHROMA,
jQuery, docById, LEADING, CARTESIANBUTTON, piemenuGrid, CLEARBUTTON, COLLAPSEBUTTON,
EXPANDBUTTON, MBOUNDARY
*/
/* exported Turtles */
// What is the scale factor when stage is shrunk?
const CONTAINERSCALEFACTOR = 4;
/**
* Class for managing all the turtles.
*
* @class
* @classdesc This is the prototype of the Turtles controller which
* acts as a bridge between the Turtles model and the Turtles view,
* and serves as a gateway to any external code.
*
* External code instantiates this class, and can access all the members
* of TurtlesView and TurtlesModel.
*
* This component contains properties and controls relevant to the set
* of all turtles like maintaining the canvases on which turtles draw.
*/
class Turtles {
/**
* @constructor
*/
constructor(activity) {
// Import members of model and view (arguments only for model)
importMembers(this, "", [activity]);
// Inititalize all actions related to blocks executed by Turtle objects
this.initActions();
}
/**
* Inititalizes all supporting action related classes & methods of Turtle.
*
* @returns {void}
*/
initActions() {
setupRhythmActions(this.activity);
setupMeterActions(this.activity);
setupPitchActions(this.activity);
setupIntervalsActions(this.activity);
setupToneActions(this.activity);
setupOrnamentActions(this.activity);
setupVolumeActions(this.activity);
setupDrumActions(this.activity);
setupDictActions(this.activity);
}
/**
* Adds turtle to start block.
*
* @param {Object} startBlock - name of startBlock
* @param {Object} infoDict - contains turtle color, shade, pensize, x, y, heading, etc.
* @returns {void}
*/
addTurtle(startBlock, infoDict) {
this.add(startBlock, infoDict);
if (this.isShrunk()) {
const t = last(this.turtleList);
t.container.scaleX = CONTAINERSCALEFACTOR;
t.container.scaleY = CONTAINERSCALEFACTOR;
t.container.scale = CONTAINERSCALEFACTOR;
}
}
/**
* Add a new turtle for each start block.
* Creates container for each turtle.
*
* @param startBlock - name of startBlock
* @param infoDict - contains turtle color, shade, pensize, x, y, heading, etc.
* @returns {void}
*/
add(startBlock, infoDict) {
if (startBlock !== null) {
// console.debug("adding a new turtle " + startBlock.name);
if (startBlock.value !== this.turtleList.length) {
startBlock.value = this.turtleList.length;
// console.debug("turtle #" + startBlock.value);
}
}
const blkInfoAvailable =
typeof infoDict === "object" && Object.keys(infoDict).length > 0 ? true : false;
// Unique ID of turtle is time of instantiation for the first time
const id =
blkInfoAvailable && "id" in infoDict && infoDict["id"] !== Infinity
? infoDict["id"]
: Date.now();
const turtleName = blkInfoAvailable && "name" in infoDict ? infoDict["name"] : _("start");
// Instantiate a new Turtle object
const turtle = new Turtle(this.activity, id, turtleName, this, startBlock);
// Add turtle model properties and store color index for turtle
this.addTurtleStageProps(turtle, blkInfoAvailable, infoDict);
const turtlesStage = this.activity.stage;
let i = this.turtleList.length % 10; // used for turtle (mouse) skin color
this.turtleList.push(turtle); // add new turtle to turtle list
if (startBlock === null) {
// Hidden start block for when there are no start blocks
return;
}
if (startBlock.name === "start") {
this.createArtwork(turtle, i, true);
} else {
// Search for companion and use that turtle's colors.
for (let j = 0; j < this.turtleList.length; j++) {
if (this.turtleList[j].companionTurtle === this.turtleList.length - 1) {
i = j % 10;
break;
}
}
this.createArtwork(turtle, i, false);
}
this.createHitArea(turtle);
/*
===================================================
Add event handlers
===================================================
*/
turtle.container.on("mousedown", (event) => {
const scale = this.scale;
const offset = {
x: turtle.container.x - event.stageX / scale,
y: turtle.container.y - event.stageY / scale
};
turtlesStage.dispatchEvent("CursorDown" + turtle.id);
// console.debug("--> [CursorDown " + turtle.name + "]");
turtle.container.removeAllEventListeners("pressmove");
turtle.container.on("pressmove", (event) => {
if (this.isShrunk() || turtle.running) {
return;
}
turtle.container.x = event.stageX / scale + offset.x;
turtle.container.y = event.stageY / scale + offset.y;
turtle.x = this.screenX2turtleX(turtle.container.x);
turtle.y = this.screenY2turtleY(turtle.container.y);
this.activity.refreshCanvas();
});
});
turtle.container.on("pressup", () => {
// console.debug("--> [CursorUp " + turtle.name + "]");
turtlesStage.dispatchEvent("CursorUp" + turtle.id);
});
turtle.container.on("click", () => {
// If turtles listen for clicks then they can be used as buttons
// console.debug("--> [click " + turtle.name + "]");
turtlesStage.dispatchEvent("click" + turtle.id);
});
turtle.container.on("mouseover", () => {
// console.debug("--> [mouseover " + turtle.name + "]");
turtlesStage.dispatchEvent("CursorOver" + turtle.id);
if (turtle.running) {
return;
}
turtle.container.scaleX *= 1.2;
turtle.container.scaleY = turtle.container.scaleX;
turtle.container.scale = turtle.container.scaleX;
this.activity.refreshCanvas();
});
turtle.container.on("mouseout", () => {
// console.debug("--> [mouseout " + turtle.name + "]");
turtlesStage.dispatchEvent("CursorOut" + turtle.id);
if (turtle.running) {
return;
}
turtle.container.scaleX /= 1.2;
turtle.container.scaleY = turtle.container.scaleX;
turtle.container.scale = turtle.container.scaleX;
this.activity.refreshCanvas();
});
document.getElementById("loader").className = "";
this.addTurtleGraphicProps(turtle, blkInfoAvailable, infoDict);
this.activity.refreshCanvas();
}
/**
* Toggles 'running' boolean value for all turtles.
*
* @returns {void}
*/
markAllAsStopped() {
for (const turtle in this.turtleList) {
this.turtleList[turtle].running = false;
}
this.activity.refreshCanvas();
}
// ================================ MODEL =================================
// ========================================================================
/**
* @param {Object} stage
*/
set masterStage(stage) {
this._masterStage = stage;
}
/**
* @returns {Object} - master stage object
*/
get masterStage() {
return this._masterStage;
}
/**
* @param {Object} stage
*/
set stage(stage) {
this._stage = stage;
this._stage.addChild(this._borderContainer);
}
/**
* @returns {Object} - stage object
*/
get stage() {
return this._stage;
}
/**
* @param {Object} canvas
*/
set canvas(canvas) {
this._canvas = canvas;
}
/**
* @return {Object} canvas object
*/
get canvas() {
return this._canvas;
}
/**
* @returns {Object} border container object
*/
get borderContainer() {
return this._borderContainer;
}
/**
* @param {Function} hideMenu - hide auxiliary menu
*/
set hideMenu(hideMenu) {
this._hideMenu = hideMenu;
}
/**
* @returns {Function} hide auxiliary menu
*/
get hideMenu() {
return this._hideMenu;
}
/**
* @param {Function} doClear - reset canvas and turtles
*/
set doClear(doClear) {
this._doClear = doClear;
}
/**
* @returns {Function} reset canvas and turtles
*/
get doClear() {
return this._doClear;
}
/**
* @param {Function} hideGrids - hide canvas gridwork
*/
set hideGrids(hideGrids) {
this._hideGrids = hideGrids;
}
/**
* @returns {Function} hide canvas gridwork
*/
get hideGrids() {
return this._hideGrids;
}
/**
* @param {Function} doGrid - show canvas gridwork
*/
set doGrid(doGrid) {
this._doGrid = doGrid;
}
/**
* @returns {Function} show canvas gridwork
*/
get doGrid() {
return this._doGrid;
}
/**
* @returns {Object[]} list of Turtle objects
*/
get turtleList() {
return this._turtleList;
}
// ================================ VIEW ==================================
// ========================================================================
/**
* @returns {Number} scale factor
*/
get scale() {
return this._scale;
}
}
/**
* Class pertaining to Turtles Model.
*
* @class
* @classdesc This is the prototype of the Model for the Turtles component.
* It should store the data structures that control behavior of the model,
* and the methods to interact with them.
*/
Turtles.TurtlesModel = class {
/**
* @constructor
*/
constructor(activity) {
this.activity = activity;
this._masterStage = null; // createjs stage
this._stage = null; // createjs container for turtle
this._canvas = null; // DOM canvas element
// These functions are directly called by TurtlesView
this._hideMenu = null; // function to hide aux menu
this._doClear = null; // function to clear the canvas
this._hideGrids = null; // function to hide all grids
this._doGrid = null; // function that renders Cartesian/Polar
// grids and changes button labels
// createjs border container
this._borderContainer = new createjs.Container();
this._masterStage = this.activity.stage;
this._stage = this.activity.turtleContainer;
this._stage.addChild(this._borderContainer);
this._canvas = this.activity.canvas;
this._hideMenu = this.activity.hideAuxMenu;
this._hideGrids = this.activity.hideGrids;
this._doGrid = this.activity._doCartesianPolar;
// List of all of the turtles, one for each start block
this._turtleList = [];
/**
* @todo Add methods to initialize the turtleList, directly access the
* required turtle rather than having to "get" the turtleList itself,
* and return the length of the turtleList (number of Turtles).
*/
}
/**
* Adds createjs related properties of turtles and turtlesStage.
*
* @param {Object} turtle
* @param {Boolean} blkInfoAvailable
* @param {Object} infoDict
* @returns {void}
*/
addTurtleStageProps(turtle, blkInfoAvailable, infoDict) {
// Add x- and y- coordinates
if (blkInfoAvailable) {
if ("xcor" in infoDict) {
turtle.x = infoDict["xcor"];
}
if ("ycor" in infoDict) {
turtle.y = infoDict["ycor"];
}
}
const turtlesStage = this._stage;
// Each turtle needs its own canvas
turtle.imageContainer = new createjs.Container();
turtlesStage.addChild(turtle.imageContainer);
turtle.penstrokes = new createjs.Bitmap();
turtlesStage.addChild(turtle.penstrokes);
turtle.container = new createjs.Container();
turtlesStage.addChild(turtle.container);
turtle.container.x = this.turtleX2screenX(turtle.x);
turtle.container.y = this.turtleY2screenY(turtle.y);
}
/**
* Creates sensor area for Turtle body.
*
* @param {*} turtle - Turtle object
* @returns {void}
*/
createHitArea(turtle) {
const hitArea = new createjs.Shape();
hitArea.graphics.beginFill("#FFF").drawEllipse(-27, -27, 55, 55);
hitArea.x = 0;
hitArea.y = 0;
turtle.container.hitArea = hitArea;
}
/**
* Adds graphic specific properties of Turtle object.
*
* @param {Object} turtle
* @param {Boolean} blkInfoAvailable
* @param {Object} infoDict
* @returns {void}
*/
addTurtleGraphicProps(turtle, blkInfoAvailable, infoDict) {
setTimeout(() => {
if (blkInfoAvailable) {
if ("heading" in infoDict) {
turtle.painter.doSetHeading(infoDict["heading"]);
}
if ("pensize" in infoDict) {
turtle.painter.doSetPensize(infoDict["pensize"]);
}
if ("grey" in infoDict) {
turtle.painter.doSetChroma(infoDict["grey"]);
}
if ("shade" in infoDict) {
turtle.painter.doSetValue(infoDict["shade"]);
}
if ("color" in infoDict) {
turtle.painter.doSetColor(infoDict["color"]);
}
if ("name" in infoDict) {
turtle.rename(infoDict["name"]);
}
}
}, 2000);
}
/**
* Returns boolean value depending on whether turtle is running.
*
* @return {Boolean} - running
*/
running() {
for (const turtle in this.turtleList) {
if (this.turtleList[turtle].running) {
return true;
}
}
return false;
}
/**
* @param {Number} i - index number
* @returns {Object} ith Turtle object
*/
ithTurtle(i) {
return this._turtleList[Number(i)];
}
/**
* @param {Number} i - index number
* @returns index number of companion turtle or i
*/
companionTurtle(i) {
for (let t = 0; t < this._turtleList.length; t++) {
if (this._turtleList[t].companionTurtle === i) {
return t;
}
}
return i;
}
/**
* @returns number of turtles
* (excluding turtles in the trash and companion turtles)
*/
turtleCount() {
let count = 0;
for (let t = 0; t < this._turtleList.length; t++) {
if (this.companionTurtle(t) === t && !this._turtleList[t].inTrash) {
count += 1;
}
}
return count;
}
};
/**
* Class pertaining to Turtles View.
*
* @class
* @classdesc This is the prototype of the View for the Turtles component.
* It should make changes to the view, while using members of the Model
* through Turtles (controller). An action may require updating the state
* (of the Model), which it can do by calling methods of the Model, also
* through Turtles (controller).
*/
Turtles.TurtlesView = class {
/**
* @constructor
*/
constructor() {
this._scale = 1.0; // scale factor in [0, 1]
this._w = 1200; // stage width
this._h = 900; // stage height
this._isShrunk = false; // whether canvas is collapsed
/**
* @todo write comments to describe each variable
*/
this._expandedBoundary = null;
this._collapsedBoundary = null;
this._expandButton = null; // used by add method
this._collapseButton = null; // used by add method
this._clearButton = null; // used by add method
this.gridButton = null; // used by add method
this.collapse = null;
this.expand = null;
// canvas background color
this._backgroundColor = platformColor.background;
this._locked = false;
this._queue = []; // temporarily stores [w, h, scale]
this.currentGrid = null;
// Attach an event listener to the 'resize' event
window.addEventListener("resize", () => {
// Call the updateDimensions function when resizing occurs
var screenWidth = (
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth
);
var screenHeight = (
window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight
);
// Set a scaling factor to adjust the dimensions based on the screen size
var scale = Math.min(screenWidth / 1200, screenHeight / 900);
// Calculate the new dimensions
var newWidth = Math.round(1200 * scale);
var newHeight = Math.round(900 * scale);
// Update the dimensions
this._w = newWidth;
this._h = newHeight;
});
}
/**
* Sets the scale of the turtle canvas.
*
* @param {Number} scale - scale factor in [0, 1]
* @returns {void}
*/
setStageScale(scale) {
this.stage.scaleX = scale;
this.stage.scaleY = scale;
this.activity.refreshCanvas();
}
/**
* Scales the canvas.
*
* @param {Number} w - width
* @param {Number} h - height
* @param {Number} scale - scale factor in [0, 1]
* @returns {void}
*/
doScale(w, h, scale) {
if (this._locked) {
this._queue = [w, h, scale];
} else {
this._scale = scale;
this._w = w / scale;
this._h = h / scale;
}
this.makeBackground();
}
/**
* @returns {Boolean} - whether canvas is collapsed
*/
isShrunk() {
return this._isShrunk;
}
/**
* @param {String} text
* @returns {void}
*/
setGridLabel(text) {
this._gridLabel = text;
}
/**
* Changes body background in DOM to current colour.
*
* @param {Number} turtle - Turtle index in turtleList
* @returns {void}
*/
setBackgroundColor(turtle) {
const color =
turtle === -1 ? platformColor.background : this.turtleList[turtle].painter.canvasColor;
this._backgroundColor = color;
this.makeBackground();
this.activity.refreshCanvas();
}
/**
* Adds y offset to stage.
*
* @param {Number} dy - delta y
* @returns {void}
*/
deltaY(dy) {
this.stage.y += dy;
}
/**
* Invert y coordinate.
*
* @private
* @param {Number} y - y coordinate
* @returns {Number} inverted y coordinate
*/
_invertY(y) {
return this.canvas.height / (2.0 * this._scale) - y;
}
/**
* Convert on screen x coordinate to turtle x coordinate.
*
* @param {Number} x - screen x coordinate
* @returns {Number} turtle x coordinate
*/
screenX2turtleX(x) {
return x - this.canvas.width / (2.0 * this._scale);
}
/**
* Convert on screen y coordinate to turtle y coordinate.
*
* @param {Number} y - screen y coordinate
* @returns {Number} turtle y coordinate
*/
screenY2turtleY(y) {
return this._invertY(y);
}
/**
* Convert turtle x coordinate to on screen x coordinate.
*
* @param {Number} x - turtle x coordinate
* @returns {Number} screen x coordinate
*/
turtleX2screenX(x) {
return this.canvas.width / (2.0 * this._scale) + x;
}
/**
* Convert turtle y coordinate to on screen y coordinate.
*
* @param {Number} y - turtle y coordinate
* @returns {Number} screen y coordinate
*/
turtleY2screenY(y) {
return this._invertY(y);
}
/**
* Creates the artwork for the turtle (mouse) 's skin.
*
* @param {Object} turtle
* @param {Number} i
* @returns {void}
*/
createArtwork(turtle, i, useTurtleArtwork) {
let artwork = useTurtleArtwork ? TURTLESVG : METRONOMESVG;
artwork = artwork
.replace(/fill_color/g, FILLCOLORS[i])
.replace(/stroke_color/g, STROKECOLORS[i]);
turtle.makeTurtleBitmap(artwork, this.activity, useTurtleArtwork);
turtle.painter.color = i * 10;
turtle.painter.canvasColor = getMunsellColor(
turtle.painter.color,
DEFAULTVALUE,
DEFAULTCHROMA
);
}
/**
* Makes background for canvas: clears containers, renders buttons.
*
* @param setCollapsed - specify whether the background should be collapsed
*/
makeBackground(setCollapsed) {
const activity = this.activity;
const doCollapse = setCollapsed === undefined ? false : setCollapsed;
const borderContainer = this.borderContainer;
// Remove any old background containers
borderContainer.removeAllChildren();
const turtlesStage = this.stage;
// We put the buttons on the stage so they will be on top
const _makeButton = (svg, object, x, y) => {
const container = document.createElement("div");
container.setAttribute("id", "" + object.name);
container.setAttribute("class", "tooltipped");
container.setAttribute("data-tooltip", object.label);
container.setAttribute("data-position", "bottom");
jQuery.noConflict()(".tooltipped").tooltip({
html: true,
delay: 100
});
container.onmouseover = () => {
if (!activity.loading) {
document.body.style.cursor = "pointer";
container.style.transition = "0.1s ease-out";
container.style.transform = "scale(1.15)";
}
};
container.onmouseout = () => {
if (!activity.loading) {
document.body.style.cursor = "default";
container.style.transition = "0.15s ease-out";
container.style.transform = "scale(1)";
}
};
const img = new Image();
img.src = "data:image/svg+xml;base64," + window.btoa(base64Encode(svg));
container.appendChild(img);
container.setAttribute(
"style",
"position: absolute; right:" +
(document.body.clientWidth - x) +
"px; top: " +
y +
"px;"
);
docById("buttoncontainerTOP").appendChild(container);
return container;
};
/**
* Setup dragging of smaller canvas .
*/
const dragCanvas = () => {
let offset;
turtlesStage.removeAllEventListeners("pressmove");
turtlesStage.removeAllEventListeners("mousedown");
turtlesStage.on("mousedown", (event) => {
offset = {
y: event.stageY - turtlesStage.y,
x: event.stageX - turtlesStage.x
};
});
turtlesStage.on("pressmove", (event) => {
const x = event.stageX - offset.x;
const y = event.stageY - offset.y;
turtlesStage.x = Math.max(0, Math.min((this._w * 3) / 4, x));
turtlesStage.y = Math.max(55, Math.min((this._h * 3) / 4, y));
activity.refreshCanvas();
});
};
/**
* Toggles visibility of menu and grids.
* Scales down all 'turtles' in turtleList.
* Removes the stage and adds it back at the top.
*/
const __collapse = () => {
this.hideMenu();
this.activity.hideGrids();
this.setStageScale(0.25);
this._collapsedBoundary.visible = true;
this._expandedBoundary.visible = false;
turtlesStage.x = (this._w * 3) / 4 - 10;
turtlesStage.y = 55 + LEADING + 6;
this._isShrunk = true;
for (let i = 0; i < this.turtleList.length; i++) {
this.turtleList[i].container.scaleX = CONTAINERSCALEFACTOR;
this.turtleList[i].container.scaleY = CONTAINERSCALEFACTOR;
this.turtleList[i].container.scale = CONTAINERSCALEFACTOR;
}
// remove the stage and add it back at the top
this.masterStage.removeChild(turtlesStage);
this.masterStage.addChild(turtlesStage);
dragCanvas();
this.activity.refreshCanvas();
};
/**
* Makes 'cartesian' button by initailising 'CARTESIANBUTTON' SVG.
* Assigns click listener function to doGrid() method.
*/
const __makeGridButton = () => {
this.gridButton = _makeButton(
CARTESIANBUTTON,
{
"name":"Grid",
"label":_("Grid")
},
this._w - 10 - 3 * 55,
70 + LEADING + 6
);
const that = this;
this.gridButton.onclick = () => {
piemenuGrid(that.activity);
};
};
/**
* Makes clear button by initailising 'CLEARBUTTON' SVG.
* Assigns click listener function to call allClear() method.
*/
const __makeClearButton = () => {
this._clearButton = _makeButton(
CLEARBUTTON,
{
"name":"Clean",
"label":_("Clean")
},
this._w - 5 - 2 * 55,
70 + LEADING + 6
);
this._clearButton.onclick = () => {
const clearBox = document.getElementById("ClearButton");
const clearContent = document.getElementById("ClearContent");
clearContent.innerHTML = _("Confirm");
clearBox.style.visibility="visible";
const auxToolbar = docById("aux-toolbar");
const clearBtnPosition = auxToolbar.style.display === "block" ? "183px" : "125px";
clearBox.style.top = clearBtnPosition;
const func = this.activity._allClear;
clearBox.addEventListener("click", function(event) {
if(event.target.id == "clearClose"){
this.style.visibility = "hidden";
}
else{
func();
clearBox.style.visibility = "hidden";
if (auxToolbar.style.display === "block") {
setTimeout(() => {
docById("Grid").style.top = "136px";
docById("Expand").style.top = "136px";
docById("Collapse").style.top = "136px";
docById("Clean").style.top = "136px";
}, 0);
} else {
docById("Grid").style.top = "76px";
docById("Expand").style.top = "76px";
docById("Collapse").style.top = "76px";
docById("Clean").style.top = "76px";
}
}
});
};
if (doCollapse) {
__collapse();
}
};
/**
* Makes collapse button by initailising 'COLLAPSEBUTTON' SVG.
* Assigns click listener function to call __collapse() method.
*/
const __makeCollapseButton = () => {
this._collapseButton = _makeButton(
COLLAPSEBUTTON,
{
"name":"Collapse",
"label":_("Collapse")
},
this._w - 55,
70 + LEADING + 6
);
this._collapseButton.onclick = () => {
// If the aux toolbar is open, close it.
const auxToolbar = docById("aux-toolbar");
if (auxToolbar.style.display === "block") {
const menuIcon = docById("menu");
auxToolbar.style.display = "none";
menuIcon.innerHTML = "menu";
docById("toggleAuxBtn").className -= "blue darken-1";
}
this._expandButton.style.visibility = "visible";
this._collapseButton.style.visibility = "hidden";
this.gridButton.style.visibility = "hidden";
this.activity.helpfulWheelItems.forEach(ele => {
if (ele.label === "Expand") {
ele.display = true;
} else if (ele.label === "Collapse") {
ele.display = false;
} else if (ele.label === "Grid") {
ele.display = false;
}
});
__collapse();
};
};
this.collapse = () => {
const auxToolbar = docById("aux-toolbar");
if (auxToolbar.style.display === "block") {
const menuIcon = docById("menu");
auxToolbar.style.display = "none";
menuIcon.innerHTML = "menu";
docById("toggleAuxBtn").className -= "blue darken-1";
}
this._expandButton.style.visibility = "visible";
this._collapseButton.style.visibility = "hidden";
this.gridButton.style.visibility = "hidden";
this.activity.helpfulWheelItems.forEach(ele => {
if (ele.label === "Expand") {