forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.js
6272 lines (5651 loc) · 230 KB
/
blocks.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
// Copyright (c) 2014-19 Walter Bender
//
// 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
// Minimum distance (squared) between two docks required before
// connecting them.
const MINIMUMDOCKDISTANCE = 400;
// Soft limit on the number of blocks in a single stack.
const LONGSTACK = 300;
// Special value flags to uniquely identify these media blocks.
const CAMERAVALUE = "##__CAMERA__##";
const VIDEOVALUE = "##__VIDEO__##";
const NOTEBLOCKS = ["newnote", "osctime"];
const PITCHBLOCKS = [
"pitch",
"steppitch",
"hertz",
"pitchnumber",
"nthmodalpitch",
"playdrum"
];
// Blocks holds the list of blocks and most of the block-associated
// methods, since most block manipulations are inter-block.
/**
* The class for managing the collection of blocks
* @public
* @returns {void}
*/
function Blocks(activity) {
if (sugarizerCompatibility.isInsideSugarizer()) {
storage = sugarizerCompatibility.data;
} else {
storage = localStorage;
}
this.activity = activity;
this.canvas = null;
this.stage = null;
this.refreshCanvas = null;
this.trashcan = null;
this.updateStage = null;
this.getStageScale = null;
// Did the user right cick?
this.stageClick = false;
// We keep a list of stacks in the trash.
this.trashStacks = [];
// We keep a dictionary for the proto blocks,
this.protoBlockDict = {};
// and a list of the blocks we create.
this.blockList = [];
this.blockArt = {};
this.blockCollapseArt = {};
// Track the time with mouse down.
this.mouseDownTime = 0;
this.longPressTimeout = null;
// Paste offset is used to ensure pasted blocks don't overlap.
this.pasteDx = 0;
this.pasteDy = 0;
// What did we select?
this.selectedStack = null;
// and a copy of the selected stack for pasting.
this.selectedBlocksObj = null;
// If we somehow have a malformed block database (for example,
// from importing a corrupted datafile, we need to avoid infinite
// loops while crawling the block list.
this._loopCounter = 0;
this._sizeCounter = 0;
this._searchCounter = 0;
// We need a reference to the palettes.
this.palettes = null;
// Which block, if any, is highlighted?
this.highlightedBlock = null;
// Which block, if any, is active?
this.activeBlock = null;
// Are the blocks visible?
this.visible = true;
// The group of blocks being dragged or moved together
this.dragGroup = [];
// The blocks at the tops of stacks
this.stackList = [];
// The blocks that need expanding
this._expandablesList = [];
// Number of blocks to load
this._loadCounter = 0;
// Stacks of blocks that need adjusting as blocks are repositioned
// due to expanding and contracting or insertion into the flow.
this._adjustTheseDocks = [];
// Blocks that need collapsing after load.
this.blocksToCollapse = [];
// Arg blocks that need expanding after load.
this._checkTwoArgBlocks = [];
// Arg clamp blocks that need expanding after load.
this._checkArgClampBlocks = [];
// Clamp blocks that need expanding after load.
this.clampBlocksToCheck = [];
// Blocks that don't run when clicked.
this.noRunBlocks = [];
this._homeButtonContainers = [];
this.blockScale = DEFAULTBLOCKSCALE;
// We need to know if we are processing a copy or save stack command.
this.inLongPress = false;
this.customTemperamentDefined = false;
// We stage deletion of prototype action blocks on the palette so
// as to avoid palette refresh race conditions.
this.deleteActionTimeout = 0;
/**
* Returns the long press status
* @public
* @returns this.inLongPress
*/
this.getLongPressStatus = function() {
return this.inLongPress;
};
/**
* Sets long press status to false
* @public
* @returns {void}
*/
this.clearLongPress = function() {
this.inLongPress = false;
};
/**
* We need access to the canvas.
* @param - canvas
* @public
* @returns this
*/
this.setCanvas = function(canvas) {
this.canvas = canvas;
return this;
};
/**
* We need access to the stage.
* @param - stage - staging area
* @public
* @returns this
*/
this.setStage = function(stage) {
this.stage = stage;
return this;
};
/**
* We need to be able to refreshe the canvas.
* @param - refreshCanvas - new variable
* @public
* @returns this
*/
this.setRefreshCanvas = function(refreshCanvas) {
this.refreshCanvas = refreshCanvas;
return this;
};
/**
* We need to access the trashcan.
* @param - trashcan - new variable
* @public
* @returns this
*/
this.setTrashcan = function(trashcan) {
this.trashcan = trashcan;
return this;
};
/**
* We need to be able to update the stage.
* @param - updateStage - new variable
* @public
* @returns this
*/
this.setUpdateStage = function(updateStage) {
this.updateStage = updateStage;
return this;
};
/**
* We need to access the stage scale.
* @param - getStageScale - new variable
* @public
* @returns this
*/
this.setGetStageScale = function(getStageScale) {
this.getStageScale = getStageScale;
return this;
};
/**
* Change the scale of the blocks (and the protoblocks on the palette).
* @param - scale -new variable
* @public
* @returns {void}
*/
this.setBlockScale = async function(scale) {
console.debug("New block scale is " + scale);
this.blockScale = scale;
let blk;
let stack;
let palette;
// Regenerate all of the artwork at the new scale.
for (blk = 0; blk < this.blockList.length; blk++) {
this.blockList[blk].resize(scale);
}
this.findStacks();
for (stack = 0; stack < this.stackList.length; stack++) {
this.adjustDocks(this.stackList[stack], true);
}
// Make sure trash is still hidden.
for (blk = 0; blk < this.blockList.length; blk++) {
if (this.blockList[blk].trash) {
this.blockList[blk].hide();
}
}
// We reset the protoblock scale on the palettes, but don't
// modify the palettes themselves.
for (palette in this.palettes.dict) {
for (blk = 0; blk < this.palettes.dict[palette].protoList.length; blk++) {
this.palettes.dict[palette].protoList[blk].scale = scale;
}
}
// Force a refresh.
await delayExecution(500);
this.refreshCanvas();
};
/**
* We need to access the message object.
* @param - msgText -new variable
* @public
* @returns {void}
*/
this.setMsgText = function(msgText) {
this.msgText = msgText;
};
/**
* We need to access the error message object.
* @param - errorMsg -new variable
* @public
* @returns this
*/
this.setErrorMsg = function(errorMsg) {
this.errorMsg = errorMsg;
return this;
};
/**
* We need to access the macro dictionary to add data to it.
* @param - obj - object
* @public
* @returns this
*/
this.setMacroDictionary = function(obj) {
this.macroDict = obj;
return this;
};
/**
* We need access to the turtles list because we associate a turtle
* with each start block.
* @param - turtles
* @public
* @returns this
*/
this.setTurtles = function(turtles) {
this.turtles = turtles;
return this;
};
/**
* We need to access "Logo interpreter" when we click on blocks.
* @param - logo
* @public
* @returns this
*/
this.setLogo = function(logo) {
this.logo = logo;
return this;
};
/**
* We need to access the right-click (and long press) context menu.
* @param - contextMenu
* @public
* @returns this
*/
this.setContextMenu = function(contextMenu) {
this.contextMenu = contextMenu;
return this;
};
/**
* The scale of the graphics is determined by screen size.
* @param - scale -new variable
* @public
* @returns this
*/
this.setScale = function(scale) {
// this.blockScale = scale;
return this;
};
/**
* Extract the blocks
* @public
* @returns {void}
*/
this.extract = function() {
if (this.activeBlock != null) {
// Don't extract silence blocks.
if (this.blockList[this.activeBlock].name !== "rest2") {
this._extractBlock(this.activeBlock, true);
}
}
};
/**
* Pull a block from a stack, then readjust the docks.
* @param - blk - blocks
* @param - adjustDock - new variable
* @private
* @returns {void}
*/
this._extractBlock = async function(blk, adjustDock) {
// Remove a single block from within a stack.
const blkObj = this.blockList[blk];
const firstConnection = blkObj.connections[0];
let connectionIdx;
if (SPECIALINPUTS.indexOf(blkObj.name) === -1) {
const clampList = [];
this.findNestedClampBlocks(blk, clampList);
let lastConnection = last(blkObj.connections);
if (firstConnection != null) {
connectionIdx = this.blockList[firstConnection].connections.indexOf(blk);
} else {
connectionIdx = null;
}
blkObj.connections[0] = null;
if (lastConnection != null) {
// Is it a hidden block? Keep it attached.
if (this.blockList[lastConnection].name === "hidden") {
lastConnection = last(this.blockList[lastConnection].connections);
this.blockList[last(blkObj.connections)].connections[this.blockList[last(blkObj.connections)].connections.length - 1] = null;
} else {
blkObj.connections[blkObj.connections.length - 1] = null;
}
if (lastConnection != null) {
this.blockList[lastConnection].connections[0] = firstConnection;
}
}
if (firstConnection != null) {
this.blockList[firstConnection].connections[connectionIdx] = lastConnection;
}
this.moveStackRelative(blk, 4 * STANDARDBLOCKHEIGHT, 0);
this.blockMoved(blk);
if (adjustDock && firstConnection != null) {
this.adjustDocks(firstConnection, true);
if (clampList.length > 0) {
this.clampBlocksToCheck = clampList;
this.adjustExpandableClampBlock();
}
}
} else {
if (firstConnection != null) {
connectionIdx = this.blockList[firstConnection].connections.indexOf(blk);
this.blockList[firstConnection].connections[connectionIdx] = null;
blkObj.connections[0] = null;
}
this.moveStackRelative(blk, 4 * STANDARDBLOCKHEIGHT, 0);
this.blockMoved(blk);
}
};
/**
* Find the y position of the bottom-most block
* @public
* @returns maxy
*/
this.bottomMostBlock = function() {
let maxy = -1000;
for (const blk in this.blockList) {
if (this.blockList[blk].container.y > maxy) {
maxy = this.blockList[blk].container.y;
}
}
return maxy;
};
/**
* Toggle state of collapsible blocks, except for note blocks,
* which are handled separately.
* @public
* @returns {void}
*/
this.toggleCollapsibles = function() {
let allCollapsed = true;
let someCollapsed = false;
let blk;
let myBlock;
for (blk in this.blockList) {
myBlock = this.blockList[blk];
if (["newnote", "interval", "osctime"].indexOf(myBlock.name) !== -1) {
continue;
}
if (COLLAPSIBLES.indexOf(myBlock.name) !== -1 && !myBlock.trash) {
if (myBlock.collapsed) {
someCollapsed = true;
} else {
allCollapsed = false;
}
}
}
if (allCollapsed || !someCollapsed) {
// If all blocks are collapsed, uncollapse them all.
// If any blocks are collapsed, collapse them all.
for (blk in this.blockList) {
myBlock = this.blockList[blk];
if (["newnote", "interval", "osctime"].indexOf(myBlock.name) !== -1) {
continue;
}
if (COLLAPSIBLES.indexOf(myBlock.name) !== -1 && !myBlock.trash) {
myBlock.collapseToggle();
}
}
} else {
// If no blocks are collapsed, collapse them all.
for (blk in this.blockList) {
myBlock = this.blockList[blk];
if (["newnote", "interval", "osctime"].indexOf(myBlock.name) !== -1) {
continue;
}
if (COLLAPSIBLES.indexOf(myBlock.name) !== -1 && !myBlock.trash) {
if (!myBlock.collapsed) {
myBlock.collapseToggle();
}
}
}
}
};
/**
* We need to access the go-home button and boundary.
* @param - setHomeContainers
* @param - boundary
* @public
* @returns this
*/
this.setHomeContainers = function(setContainers, boundary) {
this._setHomeButtonContainers = setContainers;
this.boundary = boundary;
return this;
};
/**
* Is this an action block?
* @param - name - block name
* @private
* @returns boolean
*/
this._actionBlock = function(name) {
return ["do", "doArg", "calc", "calcArg"].indexOf(name) !== -1;
};
/**
* Is this a named action block?
* @param - name - block name
* @private
* @returns boolean
*/
this._namedActionBlock = function(name) {
return ["nameddo", "nameddoArg", "namedcalc", "namedcalcArg"].indexOf(name) !== -1;
};
/**
* Adjust the dock positions of all blocks in the current drag group.
* @private
* @returns {void}
*/
this._adjustBlockPositions = function() {
if (this.dragGroup.length < 2) {
return;
}
this.adjustDocks(this.dragGroup[0], true);
};
/**
* Adjust the size of the clamp in an expandable block when
* block are inserted into (or removed from) the child flow.
* This is a common operation for start and action blocks,
* but also for repeat, forever, if, etc.
* @public
* @returns {void}
*/
this.adjustExpandableClampBlock = function() {
if (this.clampBlocksToCheck.length === 0) {
return;
}
const that = this;
const obj = this.clampBlocksToCheck.pop();
const blk = obj[0];
const clamp = obj[1];
const myBlock = this.blockList[blk];
if (myBlock.isArgFlowClampBlock() || myBlock.isLeftClampBlock()) {
// Make sure myBlock is a clamp block.
} else if (myBlock.isArgBlock() || myBlock.isTwoArgBlock()) {
return;
} else if (myBlock.isArgClamp()) {
// We handle ArgClamp blocks elsewhere.
this._adjustArgClampBlock([blk]);
}
/**
* Adjusts the clamp size
* @param - blk - block number
* @param - myBlock - block
* @param - clamp (1 or 2)?
* @private
* @returns {void}
*/
const __clampAdjuster = function(blk, myBlock, clamp) {
// First we need to count up the number of (and size of) the
// blocks inside the clamp; The child flow is usually the
// second-to-last argument.
let c;
if (myBlock.isArgFlowClampBlock() || myBlock.isLeftClampBlock()) {
c = 1; // 0: outie; and 1: child flow
} else if (clamp === 0) {
c = myBlock.connections.length - 2;
} else {
// top clamp in if-then-else
c = myBlock.connections.length - 3;
}
that._sizeCounter = 0;
let childFlowSize = 1;
if (c > 0 && myBlock.connections[c] != null) {
this._sizeCounter = 0;
childFlowSize = Math.max(that._getStackSize(myBlock.connections[c]), 1);
}
// Adjust the clamp size to match the size of the child
// flow.
const plusMinus = childFlowSize - myBlock.clampCount[clamp];
if (plusMinus !== 0) {
if (!(childFlowSize === 0 && myBlock.clampCount[clamp] === 1)) {
myBlock.updateSlots(clamp, plusMinus);
}
}
// Recurse through the list.
if (that.clampBlocksToCheck.length > 0) {
that.adjustExpandableClampBlock();
}
};
__clampAdjuster(blk, myBlock, clamp);
};
/**
* Returns depth of nesting, which is used to rank order the
* blocks when adjusting their sizes.
* @param - blk - block number
* @private
* @returns depth
*/
this._getNestingDepth = function(blk) {
let rank = 0;
while(blk !== null) {
blk = this.insideExpandableBlock(blk);
rank += 1;
}
return rank;
};
/**
* Return the block size in units of standard block size.
* @param - blk - block number
* @private
* @returns block size
*/
this._getBlockSize = function(blk) {
const myBlock = this.blockList[blk];
// Special case for collapsed note blocks.
if (["newnote", "interval", "osctime"].indexOf(myBlock.name) !== -1 && myBlock.collapsed) {
return 1;
}
return myBlock.size;
};
/**
* Adjust the slot sizes of arg clamps.
* @param - argBlocksToCheck -new variable
* @private
* @returns {void}
*/
this._adjustArgClampBlock = function(argBlocksToCheck) {
if (argBlocksToCheck.length === 0) {
return;
}
const blk = argBlocksToCheck.pop();
const myBlock = this.blockList[blk];
let update = false;
// Which connection do we start with?
let ci;
if (["doArg", "calcArg", "makeblock"].indexOf(myBlock.name) !== -1) {
ci = 2;
} else {
ci = 1;
}
// Get the current slot list.
const slotList = myBlock.argClampSlots;
// Determine the size of each argument.
for (let i = 0; i < slotList.length; i++) {
const c = myBlock.connections[ci + i];
let size = 1; // Minimum size
if (c != null) {
size = Math.max(this._getBlockSize(c), 1);
}
if (slotList[i] !== size) {
slotList[i] = size;
update = true;
}
}
if (update) {
myBlock.updateArgSlots(slotList);
}
};
/**
* We also adjust the size of twoarg blocks. It is similar to how
* we adjust clamps, but enough different that it is in its own function.
* @param - argBlocksToCheck - a list of blocks that need to be
checked for changes to their clamp sizes.
* @private
* @returns {void}
*/
this._adjustExpandableTwoArgBlock = function(argBlocksToCheck) {
if (argBlocksToCheck.length === 0) {
return;
}
const blk = argBlocksToCheck.pop();
const myBlock = this.blockList[blk];
// Determine the size of the first argument.
const c = myBlock.connections[1];
let firstArgumentSize = 1; // Minimum size
if (c != null) {
firstArgumentSize = Math.max(this._getBlockSize(c), 1);
}
// Expand/contract block by plusMinus.
const plusMinus = firstArgumentSize - myBlock.clampCount[0];
if (plusMinus !== 0) {
if (!(firstArgumentSize === 0)) {
myBlock.updateSlots(0, plusMinus);
}
}
};
/**
* Add or remove the vspace blocks
* @param - blk - block number
* @private
* @returns void
*/
this._addRemoveVspaceBlock = function(blk) {
const myBlock = this.blockList[blk];
const c = myBlock.connections[myBlock.connections.length - 2];
let secondArgumentSize = 1;
if (c != null) {
secondArgumentSize = Math.max(this._getBlockSize(c), 1);
}
const that = this;
/**
* Checks the number of VSpace blocks are below the block we are checking against
* @param - blk - block number
* @private
* @returns number of vspace blocks found below this block
*/
const __howManyVSpaceBlocksBelow = function(blk) {
const nextBlock = last(that.blockList[blk].connections);
if (nextBlock && that.blockList[nextBlock].name === "vspace") {
return 1 + __howManyVSpaceBlocksBelow(nextBlock);
// Recurse until it isn't a vspace
}
return 0;
};
const vSpaceCount = __howManyVSpaceBlocksBelow(blk);
if (secondArgumentSize < vSpaceCount + 1) {
// Remove a vspace block
const n = Math.abs(secondArgumentSize - vSpaceCount - 1);
for (let i = 0; i < n; i++) {
const lastConnection = myBlock.connections.length - 1;
const vspaceBlock = this.blockList[myBlock.connections[lastConnection]];
const nextBlockIndex = vspaceBlock.connections[1];
myBlock.connections[lastConnection] = nextBlockIndex;
if (nextBlockIndex != null) {
this.blockList[nextBlockIndex].connections[0] = blk;
}
vspaceBlock.connections = [null, null];
vspaceBlock.trash = true;
vspaceBlock.hide();
}
} else if (secondArgumentSize > vSpaceCount + 1) {
// Add vspace blocks
/**
* Adjusts the docks of the blocks connected to the vspace block
* @param - args - [this block, the next block, the vspace block
* @private
* @returns {void}
*/
const __vspaceAdjuster = function(args) {
let thisBlock = args[0];
let nextBlock = args[1];
const vspace = args[2];
const i = args[3];
const n = args[4];
let newpos;
const vspaceBlock = that.blockList[vspace];
const lastDock = last(thisBlock.docks);
const dx = lastDock[0] - vspaceBlock.docks[0][0];
const dy = lastDock[1] - vspaceBlock.docks[0][1];
vspaceBlock.container.x = thisBlock.container.x + dx; // Math.floor(thisBlock.container.x + dx + 0.5);
vspaceBlock.container.y = thisBlock.container.y + dy; // Math.floor(thisBlock.container.y + dy + 0.5);
vspaceBlock.connections[0] = that.blockList.indexOf(thisBlock);
vspaceBlock.connections[1] = nextBlock;
thisBlock.connections[thisBlock.connections.length - 1] = vspace;
if (nextBlock) {
that.blockList[nextBlock].connections[0] = vspace;
}
if (i + 1 < n) {
newPos = that.blockList.length;
thisBlock = last(that.blockList);
nextBlock = last(thisBlock.connections);
that._makeNewBlockWithConnections(
"vspace",
newPos,
[null, null],
__vspaceAdjuster,
[thisBlock, nextBlock, newPos, i + 1, n]
);
}
};
this._makeNewBlockWithConnections(
"vspace",
this.blockList.length,
[null, null],
__vspaceAdjuster,
[myBlock, last(myBlock.connections), this.blockList.length, 0, secondArgumentSize - vSpaceCount - 1]
);
}
};
/**
* Calculate the sum of the sizes of all the blocks a stack.
* @param - blk - block number
* @private
* @returns int
*/
this._getStackSize = function(blk) {
// How many block units in this stack?
let size = 0;
this._sizeCounter += 1;
if (this._sizeCounter > this.blockList.length * 2) {
console.debug("Infinite loop encountered detecting size of expandable block? " + blk);
return size;
}
if (blk == null) {
return size;
}
const myBlock = this.blockList[blk];
if (myBlock == null) {
console.debug("Something very broken in _getStackSize.");
}
let c;
let csize;
let cblk;
if (myBlock.isClampBlock()) {
c = myBlock.connections.length - 2;
csize = 0;
if (c > 0) {
cblk = myBlock.connections[c];
if (cblk != null) {
csize = this._getStackSize(cblk);
}
if (csize === 0) {
size = 1; // minimum of 1 slot in clamp
} else {
size = csize;
}
}
if (myBlock.isDoubleClampBlock()) {
c = myBlock.connections.length - 3;
csize = 0;
if (c > 0) {
cblk = myBlock.connections[c];
if (cblk != null) {
csize = this._getStackSize(cblk);
}
if (csize === 0) {
size += 1; // minimum of 1 slot in clamp
} else {
size += csize;
}
}
}
// add top and bottom of clamp
size += myBlock.size;
} else {
size = myBlock.size;
}
// If the note value block is collapsed, spoof size.
if (this.blocksToCollapse.indexOf(blk) != -1) {
size = 1;
} else if (["newnote", "interval", "osctime"].indexOf(myBlock.name) !== -1 && myBlock.collapsed) {
size = 1;
}
// check on any connected block
if (myBlock.connections.length > 1) {
cblk = last(myBlock.connections);
if (cblk != null) {
size += this._getStackSize(cblk);
}
}
return size;
};
/**
* Given a block, adjust the dock position of all its connections.
* @param - blk - block
* @param - resetLoopCounter (to prevent infinite loops in the
case the connections are broken).
* @public
* @returns {void}
*/
this.adjustDocks = function(blk, resetLoopCounter) {
const myBlock = this.blockList[blk];
// For when we come in from makeBlock
if (resetLoopCounter != null) {
this._loopCounter = 0;
}
// These checks are to test for malformed data. All blocks
// should have connections.
if (myBlock == null) {
console.debug("Saw a null block: " + blk);
return;
}
if (myBlock.connections == null) {
console.debug("Saw a block with null connections: " + blk);
return;
}
if (myBlock.connections.length === 0) {
console.debug("Saw a block with [] connections: " + blk);
return;
}
// Value blocks only have one dock.
if (myBlock.docks.length === 1) {
return;
}
this._loopCounter += 1;
if (this._loopCounter > this.blockList.length * 2) {
console.debug("Infinite loop encountered while adjusting docks: " + blk + " " + this.blockList);
return;
}
// Walk through each connection except the parent block; the
// exception being the parent block of boolean 2arg blocks,
// since the dock[0] position can change; and only check the
// last connection of collapsed blocks.
let start = 1;
if (myBlock.isTwoArgBooleanBlock()) {
start = 0;
} else if (myBlock.isInlineCollapsible() && myBlock.collapsed) {
start = myBlock.connections.length - 1;
}
for (let c = start; c < myBlock.connections.length; c++) {
// Get the dock position for this connection.
const bdock = myBlock.docks[c];
// Find the connecting block.
const cblk = myBlock.connections[c];
// Nothing connected here so continue to the next connection.
if (cblk === null) {
continue;
}
// Another database integrety check.
if (this.blockList[cblk] === null) {
console.debug("This is not good: we encountered a null block: " + cblk);
continue;
}
// Find the dock position in the connected block.
let foundMatch = false;
let b;
for (b = 0; b < this.blockList[cblk].connections.length; b++) {
if (this.blockList[cblk].connections[b] === blk) {
foundMatch = true;
break;
}
}
// Yet another database integrety check.
if (!foundMatch) {
console.debug("Did not find match for " + myBlock.name + " (" + blk + ") and " +
this.blockList[cblk].name + " (" + cblk + ")");
console.debug(myBlock.connections);
console.debug(this.blockList[cblk].connections);
break;
}
const cdock = this.blockList[cblk].docks[b];
let dx;
let dy;
let nx;