forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piemenus.js
3893 lines (3345 loc) · 141 KB
/
piemenus.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 max-len */
// Copyright (c) 2014-21 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
//
/*
global
_, platformColor, docById, Singer, slicePath, wheelnav,
DEFAULTVOICE, getDrumName, getNote, MUSICALMODES last, SHARP, FLAT,
PREVIEWVOLUME, DEFAULTVOLUME, MODE_PIE_MENUS, HelpWidget,
INTERVALVALUES, INTERVALS, getDrumSynthName, getVoiceSynthName,
getMunsellColor, COLORS40, frequencyToPitch, instruments,
DOUBLESHARP, NATURAL, DOUBLEFLAT, EQUIVALENTACCIDENTALS,
FIXEDSOLFEGE, NOTENAMES, FIXEDSOLFEGE, NOTENAMES, numberToPitch,
nthDegreeToPitch, SOLFEGENAMES, buildScale, _THIS_IS_TURTLE_BLOCKS_,
CHORDNAMES
*/
/*
Globals location
- lib/wheelnav
slicePath, wheelnav
- js/utils/musicutils.js
FLAT, SHARP, DEFAULTVOICE, getDrumName, getNote, MODE_PIE_MENUS, MUSICALMODES, INTERVALVALUES,
INTERVALS, getDrumSynthName, getVoiceSynthName, frequencyToPitch, DOUBLESHARP, NATURAL,
DOUBLEFLAT, EQUIVALENTACCIDENTALS, FIXEDSOLFEGE, NOTENAMES, FIXEDSOLFEGE, NOTENAMES, numberToPitch,
nthDegreeToPitch, SOLFEGENAMES, buildScale
- js/utils/utils.js
_, last, docById
- js/turtle-singer.js
Singer
- js/utils/munsell.js
getMunsellColor, COLORS40
- js/widgets/help.js
HelpWidget
- js/utils/platformstyle.js
platformColorcl
- js/utils/synthutils.js
instruments
- js/logo.js
PREVIEWVOLUME, DEFAULTVOLUME
*/
/*
exported
piemenuModes, piemenuPitches, piemenuCustomNotes, piemenuGrid,
piemenuBlockContext, piemenuIntervals, piemenuVoices, piemenuBoolean,
piemenuBasic, piemenuColor, piemenuNumber, piemenuNthModalPitch,
piemenuNoteValue, piemenuAccidentals, piemenuKey, piemenuChords
*/
const piemenuPitches = function (
block,
noteLabels,
noteValues,
accidentals,
note,
accidental,
custom
) {
let prevPitch = null;
// wheelNav pie menu for pitch selection
if (block.blocks.stageClick) {
return;
}
if (custom === undefined) {
custom = false;
}
// Some blocks have both pitch and octave, so we can modify
// both at once.
const hasOctaveWheel =
block.connections[0] !== null &&
["pitch", "setpitchnumberoffset", "invert1", "tofrequency"].indexOf(
block.blocks.blockList[block.connections[0]].name
) !== -1;
// If we are attached to a set key block, we want to order
// pitch by fifths.
if (
block.connections[0] !== null &&
["setkey", "setkey2"].indexOf(block.blocks.blockList[block.connections[0]].name) !== -1
) {
noteLabels = ["C", "G", "D", "A", "E", "B", "F"];
noteValues = ["C", "G", "D", "A", "E", "B", "F"];
}
docById("wheelDiv").style.display = "";
// The pitch selector
block._pitchWheel = new wheelnav("wheelDiv", null, 600, 600);
if (!custom) {
// The accidental selector
block._accidentalsWheel = new wheelnav("_accidentalsWheel", block._pitchWheel.raphael);
}
// The octave selector
if (hasOctaveWheel) {
block._octavesWheel = new wheelnav("_octavesWheel", block._pitchWheel.raphael);
}
// The exit button
block._exitWheel = new wheelnav("_exitWheel", block._pitchWheel.raphael);
wheelnav.cssMode = true;
block._pitchWheel.keynavigateEnabled = false;
block._pitchWheel.colors = platformColor.pitchWheelcolors;
block._pitchWheel.slicePathFunction = slicePath().DonutSlice;
block._pitchWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._pitchWheel.slicePathCustom.minRadiusPercent = 0.2;
if (!custom) {
block._pitchWheel.slicePathCustom.maxRadiusPercent = 0.5;
} else {
block._pitchWheel.slicePathCustom.maxRadiusPercent = 0.75;
}
block._pitchWheel.sliceSelectedPathCustom = block._pitchWheel.slicePathCustom;
block._pitchWheel.sliceInitPathCustom = block._pitchWheel.slicePathCustom;
block._pitchWheel.animatetime = 0; // 300;
block._pitchWheel.createWheel(noteLabels);
block._exitWheel.colors = platformColor.exitWheelcolors;
block._exitWheel.slicePathFunction = slicePath().DonutSlice;
block._exitWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._exitWheel.slicePathCustom.minRadiusPercent = 0.0;
block._exitWheel.slicePathCustom.maxRadiusPercent = 0.2;
block._exitWheel.sliceSelectedPathCustom = block._exitWheel.slicePathCustom;
block._exitWheel.sliceInitPathCustom = block._exitWheel.slicePathCustom;
block._exitWheel.clickModeRotate = false;
block._exitWheel.initWheel(["×", " "]);
block._exitWheel.navItems[1].enabled = false;
block._exitWheel.navItems[0].sliceSelectedAttr.cursor = "pointer";
block._exitWheel.navItems[0].sliceHoverAttr.cursor = "pointer";
block._exitWheel.navItems[0].titleSelectedAttr.cursor = "pointer";
block._exitWheel.navItems[0].titleHoverAttr.cursor = "pointer";
block._exitWheel.createWheel();
if (!custom) {
block._accidentalsWheel.colors = platformColor.accidentalsWheelcolors;
block._accidentalsWheel.slicePathFunction = slicePath().DonutSlice;
block._accidentalsWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._accidentalsWheel.slicePathCustom.minRadiusPercent = 0.5;
block._accidentalsWheel.slicePathCustom.maxRadiusPercent = 0.75;
block._accidentalsWheel.sliceSelectedPathCustom = block._accidentalsWheel.slicePathCustom;
block._accidentalsWheel.sliceInitPathCustom = block._accidentalsWheel.slicePathCustom;
const accidentalLabels = [];
for (let i = 0; i < accidentals.length; i++) {
accidentalLabels.push(accidentals[i]);
}
for (let i = 0; i < 9; i++) {
accidentalLabels.push(null);
block._accidentalsWheel.colors.push(platformColor.accidentalsWheelcolorspush);
}
block._accidentalsWheel.animatetime = 0; // 300;
block._accidentalsWheel.createWheel(accidentalLabels);
block._accidentalsWheel.setTooltips([
_("double sharp"),
_("sharp"),
_("natural"),
_("flat"),
_("double flat")
]);
}
if (hasOctaveWheel) {
block._octavesWheel.colors = platformColor.octavesWheelcolors;
block._octavesWheel.slicePathFunction = slicePath().DonutSlice;
block._octavesWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._octavesWheel.slicePathCustom.minRadiusPercent = 0.75;
block._octavesWheel.slicePathCustom.maxRadiusPercent = 0.95;
block._octavesWheel.sliceSelectedPathCustom = block._octavesWheel.slicePathCustom;
block._octavesWheel.sliceInitPathCustom = block._octavesWheel.slicePathCustom;
const octaveLabels = [
"8",
"7",
"6",
"5",
"4",
"3",
"2",
"1",
null,
null,
null,
null,
null,
null
];
block._octavesWheel.animatetime = 0; // 300;
block._octavesWheel.createWheel(octaveLabels);
}
// Position the widget over the note block.
const x = block.container.x;
const y = block.container.y;
const canvasLeft = block.activity.canvas.offsetLeft + 28 * block.blocks.blockScale;
const canvasTop = block.activity.canvas.offsetTop + 6 * block.blocks.blockScale;
docById("wheelDiv").style.position = "absolute";
docById("wheelDiv").style.height = "300px";
docById("wheelDiv").style.width = "300px";
docById("wheelDiv").style.left =
Math.min(
block.blocks.turtles._canvas.width - 300,
Math.max(
0,
Math.round(
(x + block.activity.blocksContainer.x) * block.activity.getStageScale() + canvasLeft
) - 200
)
) + "px";
docById("wheelDiv").style.top =
Math.min(
block.blocks.turtles._canvas.height - 350,
Math.max(
0,
Math.round(
(y + block.activity.blocksContainer.y) * block.activity.getStageScale() + canvasTop
) - 200
)
) + "px";
// Navigate to a the current note value.
let i = noteValues.indexOf(note);
if (i === -1) {
if (custom) {
i = 0;
} else {
i = 4;
}
}
prevPitch = i;
block._pitchWheel.navigateWheel(i);
const OFFSET = {
major: 1,
dorian: 2,
phrygian: 3,
lydian: 4,
mixolydian: 5,
minor: 6,
locrian: 7
};
const ROTATION = { A: 2, B: 1, C: 0, D: 6, E: 5, F: 4, G: 3 };
const k = OFFSET[block.activity.KeySignatureEnv[1]];
let key;
const attrList = ["", SHARP, FLAT];
for (const j in attrList) {
for (const i in NOTENAMES) {
const tempScale = buildScale(NOTENAMES[i] + attrList[j] + " major")[0];
if (tempScale[k - 1] === block.activity.KeySignatureEnv[0]) {
key = NOTENAMES[i] + attrList[j];
break;
}
}
if (key !== undefined) {
break;
}
}
let scale = buildScale(key + " major")[0];
scale = scale.splice(0, scale.length - 1);
for (let j = 0; j < ROTATION[key[0]]; j++) {
scale.push(scale.shift());
}
// Auto-selection of sharps and flats in fixed solfege handles the
// case of opening the pie-menu, not whilst in the pie-menu.
if (
(!block.activity.KeySignatureEnv[2] && block.name === "solfege") ||
(block.name === "notename" &&
(block.connections[0] != undefined
? ["setkey", "setkey2"].indexOf(
block.blocks.blockList[block.connections[0]].name
) === -1
: true))
) {
if (scale[6 - i][0] === FIXEDSOLFEGE[note] || scale[6 - i][0] === note) {
accidental = scale[6 - i].substr(1);
} else {
accidental = EQUIVALENTACCIDENTALS[scale[6 - i]].substr(1);
}
block.value = block.value.replace(SHARP, "").replace(FLAT, "");
block.value += accidental;
block.text.text = block.value;
}
if (!custom) {
// Navigate to a the current accidental value.
if (accidental === "") {
block._accidentalsWheel.navigateWheel(2);
} else {
switch (accidental) {
case DOUBLEFLAT:
block._accidentalsWheel.navigateWheel(4);
break;
case FLAT:
block._accidentalsWheel.navigateWheel(3);
break;
case NATURAL:
block._accidentalsWheel.navigateWheel(2);
break;
case SHARP:
block._accidentalsWheel.navigateWheel(1);
break;
case DOUBLESHARP:
block._accidentalsWheel.navigateWheel(0);
break;
default:
block._accidentalsWheel.navigateWheel(2);
break;
}
}
}
if (hasOctaveWheel) {
// Use the octave associated with block block, if available.
const pitchOctave = block.blocks.findPitchOctave(block.connections[0]);
// Navigate to current octave.
block._octavesWheel.navigateWheel(8 - pitchOctave);
// const prevOctave = 8 - pitchOctave;
}
// Set up event handlers.
const that = block;
const selection = {
note: note,
attr: accidental
};
/*
* Preview the selected pitch using the synth
* @return{void}
* @private
*/
const __pitchPreview = function () {
const label = that._pitchWheel.navItems[that._pitchWheel.selectedNavItemIndex].title;
const i = noteLabels.indexOf(label);
// Are we wrapping across C? We need to compare with the previous pitch
if (prevPitch === null) {
prevPitch = i;
}
const deltaPitch = i - prevPitch;
let delta;
if (deltaPitch > 3) {
delta = deltaPitch - 7;
} else if (deltaPitch < -3) {
delta = deltaPitch + 7;
} else {
delta = deltaPitch;
}
// If we wrapped across C, we need to adjust the octave.
let deltaOctave = 0;
if (prevPitch + delta > 6) {
deltaOctave = -1;
} else if (prevPitch + delta < 0) {
deltaOctave = 1;
}
let attr;
prevPitch = i;
let note = noteValues[i];
if (!custom) {
attr =
that._accidentalsWheel.navItems[that._accidentalsWheel.selectedNavItemIndex].title;
if (label === " ") {
return;
} else if (attr !== "♮") {
note += attr;
}
}
let octave;
if (hasOctaveWheel) {
octave = Number(
that._octavesWheel.navItems[that._octavesWheel.selectedNavItemIndex].title
);
} else {
octave = 4;
}
octave += deltaOctave;
if (octave < 1) {
octave = 1;
} else if (octave > 8) {
octave = 8;
}
if (hasOctaveWheel && deltaOctave !== 0) {
that._octavesWheel.navigateWheel(8 - octave);
that.blocks.setPitchOctave(that.connections[0], octave);
}
const keySignature =
block.activity.KeySignatureEnv[0] + " " + block.activity.KeySignatureEnv[1];
let obj;
if (that.name == "scaledegree2") {
note = note.replace(attr, "");
note = SOLFEGENAMES[note - 1];
note += attr;
obj = getNote(
note,
octave,
0,
keySignature,
true,
null,
that.activity.errorMsg,
that.activity.logo.synth.inTemperament
);
} else {
obj = getNote(
note,
octave,
0,
keySignature,
block.activity.KeySignatureEnv[2],
null,
that.activity.errorMsg,
that.activity.logo.synth.inTemperament
);
}
if (!custom) {
obj[0] = obj[0].replace(SHARP, "#").replace(FLAT, "b");
}
const tur = that.activity.turtles.ithTurtle(0);
if (
tur.singer.instrumentNames.length === 0 ||
tur.singer.instrumentNames.indexOf(DEFAULTVOICE) === -1
) {
tur.singer.instrumentNames.push(DEFAULTVOICE);
that.activity.logo.synth.createDefaultSynth(0);
that.activity.logo.synth.loadSynth(0, DEFAULTVOICE);
}
that.activity.logo.synth.setMasterVolume(PREVIEWVOLUME);
Singer.setSynthVolume(that.activity.logo, 0, DEFAULTVOICE, PREVIEWVOLUME);
if (!that._triggerLock) {
that._triggerLock = true;
that.activity.logo.synth.trigger(0, [obj[0] + obj[1]], 1 / 8, DEFAULTVOICE, null, null);
}
setTimeout(function () {
that._triggerLock = false;
}, 1 / 8);
};
const __selectionChangedSolfege = function () {
selection["note"] = that._pitchWheel.navItems[that._pitchWheel.selectedNavItemIndex].title;
const i = noteLabels.indexOf(selection["note"]);
that.value = noteValues[i];
// Auto-selection of sharps and flats in fixed solfege handles
// the case of opening the pie-menu, not whilst in the
// pie-menu. FIXEDSOLFEGE converts solfege to alphabet, needed
// for solfege pie-menu In. case of alphabet, direct comparison
// is performed.
if (
(!block.activity.KeySignatureEnv[2] && that.name === "solfege") ||
(that.name === "notename" &&
(that.connections[0] != undefined
? ["setkey", "setkey2"].indexOf(
that.blocks.blockList[that.connections[0]].name
) === -1
: true))
) {
let i = NOTENAMES.indexOf(FIXEDSOLFEGE[selection["note"]]);
if (i === -1) {
i = NOTENAMES.indexOf(FIXEDSOLFEGE[that.value]);
}
if (
scale[i][0] === FIXEDSOLFEGE[selection["note"]] ||
scale[i][0] === FIXEDSOLFEGE[that.value] ||
scale[i][0] === selection["note"]
) {
selection["attr"] = scale[i].substr(1);
} else {
selection["attr"] = EQUIVALENTACCIDENTALS[scale[i]].substr(1);
}
switch (selection["attr"]) {
case DOUBLEFLAT:
that._accidentalsWheel.navigateWheel(4);
break;
case FLAT:
that._accidentalsWheel.navigateWheel(3);
break;
case NATURAL:
that._accidentalsWheel.navigateWheel(2);
break;
case SHARP:
that._accidentalsWheel.navigateWheel(1);
break;
case DOUBLESHARP:
that._accidentalsWheel.navigateWheel(0);
break;
default:
that._accidentalsWheel.navigateWheel(2);
break;
}
}
that.text.text = selection["note"];
if (selection["attr"] !== "♮") {
that.text.text += selection["attr"];
}
// Make sure text is on top.
that.container.setChildIndex(that.text, that.container.children.length - 1);
that.updateCache();
if (hasOctaveWheel) {
// Set the octave of the pitch block, if available.
const octave = Number(
that._octavesWheel.navItems[that._octavesWheel.selectedNavItemIndex].title
);
that.blocks.setPitchOctave(that.connections[0], octave);
}
if (
that.connections[0] !== null &&
["setkey", "setkey2"].indexOf(that.blocks.blockList[that.connections[0]].name) !== -1
) {
// We may need to update the mode widget.
that.activity.logo.modeBlock = that.blocks.blockList.indexOf(that);
}
__pitchPreview();
};
const __selectionChangedOctave = () => {
const octave = Number(
that._octavesWheel.navItems[that._octavesWheel.selectedNavItemIndex].title
);
that.blocks.setPitchOctave(that.connections[0], octave);
__pitchPreview();
};
const __selectionChangedAccidental = () => {
selection["note"] = that._pitchWheel.navItems[that._pitchWheel.selectedNavItemIndex].title;
selection["attr"] =
that._accidentalsWheel.navItems[that._accidentalsWheel.selectedNavItemIndex].title;
if (selection["attr"] === "♮") {
that.text.text = selection["note"];
} else {
that.value += selection["attr"];
that.text.text = selection["note"] + selection["attr"];
}
that.container.setChildIndex(that.text, that.container.children.length - 1);
that.updateCache();
__pitchPreview();
};
// Set up handlers for pitch preview.
for (let i = 0; i < noteValues.length; i++) {
block._pitchWheel.navItems[i].navigateFunction = __selectionChangedSolfege;
}
if (!custom) {
for (let i = 0; i < accidentals.length; i++) {
block._accidentalsWheel.navItems[i].navigateFunction = __selectionChangedAccidental;
}
}
if (hasOctaveWheel) {
for (let i = 0; i < 8; i++) {
block._octavesWheel.navItems[i].navigateFunction = __selectionChangedOctave;
}
}
// Hide the widget when the exit button is clicked.
block._exitWheel.navItems[0].navigateFunction = function () {
that._piemenuExitTime = new Date().getTime();
docById("wheelDiv").style.display = "none";
that._pitchWheel.removeWheel();
if (!custom) {
that._accidentalsWheel.removeWheel();
}
that._exitWheel.removeWheel();
if (hasOctaveWheel) {
that._octavesWheel.removeWheel();
}
};
};
const piemenuCustomNotes = function (
block,
noteLabels,
customLabels,
selectedCustom,
selectedNote
) {
// pie menu for customNote selection
if (block.blocks.stageClick) {
return;
}
docById("wheelDiv").style.display = "";
// Some blocks have both pitch and octave, so we can modify
// both at once.
const hasOctaveWheel =
block.connections[0] !== null &&
["pitch", "setpitchnumberoffset", "invert1", "tofrequency"].indexOf(
block.blocks.blockList[block.connections[0]].name
) !== -1;
// Use advanced constructor for more wheelnav on same div
block._customWheel = new wheelnav("wheelDiv", null, 800, 800);
block._cusNoteWheel = new wheelnav("_cusNoteWheel", block._customWheel.raphael);
// exit button
block._exitWheel = new wheelnav("_exitWheel", block._customWheel.raphael);
// the octave selector
if (hasOctaveWheel) {
block._octavesWheel = new wheelnav("_octavesWheel", block._customWheel.raphael);
}
wheelnav.cssMode = true;
block._customWheel.keynavigateEnabled = false;
//Customize slicePaths for proper size
block._customWheel.colors = platformColor.intervalNameWheelcolors;
block._customWheel.slicePathFunction = slicePath().DonutSlice;
block._customWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._customWheel.slicePathCustom.minRadiusPercent = 0.1;
block._customWheel.slicePathCustom.maxRadiusPercent = 0.5;
block._customWheel.sliceSelectedPathCustom = block._customWheel.slicePathCustom;
block._customWheel.sliceInitPathCustom = block._customWheel.slicePathCustom;
block._customWheel.titleRotateAngle = 0;
block._customWheel.animatetime = 0; // 300;
block._customWheel.clickModeRotate = false;
block._customWheel.createWheel(customLabels);
block._cusNoteWheel.colors = platformColor.intervalWheelcolors;
block._cusNoteWheel.slicePathFunction = slicePath().DonutSlice;
block._cusNoteWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._cusNoteWheel.slicePathCustom.minRadiusPercent = 0.5;
block._cusNoteWheel.slicePathCustom.maxRadiusPercent = 0.85;
//block._cusNoteWheel.titleRotateAngle = 0;
block._cusNoteWheel.titleFont = "100 24px Impact, sans-serif";
block._cusNoteWheel.sliceSelectedPathCustom = block._cusNoteWheel.slicePathCustom;
block._cusNoteWheel.sliceInitPathCustom = block._cusNoteWheel.slicePathCustom;
if (hasOctaveWheel) {
block._octavesWheel.colors = platformColor.octavesWheelcolors;
block._octavesWheel.slicePathFunction = slicePath().DonutSlice;
block._octavesWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._octavesWheel.slicePathCustom.minRadiusPercent = 0.85;
block._octavesWheel.slicePathCustom.maxRadiusPercent = 1;
block._octavesWheel.sliceSelectedPathCustom = block._octavesWheel.slicePathCustom;
block._octavesWheel.sliceInitPathCustom = block._octavesWheel.slicePathCustom;
const octaveLabels = [
"8",
"7",
"6",
"5",
"4",
"3",
"2",
"1",
null,
null,
null,
null,
null,
null
];
block._octavesWheel.animatetime = 0; // 300;
block._octavesWheel.createWheel(octaveLabels);
}
//Disable rotation, set navAngle and create the menus
block._cusNoteWheel.clickModeRotate = false;
block._cusNoteWheel.titleRotateAngle = 180;
block._cusNoteWheel.animatetime = 0; // 300;
const labelsDict = {};
let labels = [];
let blockCustom = 0;
let max = 0;
for (const t of customLabels) {
max = max > noteLabels[t]["pitchNumber"] ? max : noteLabels[t]["pitchNumber"];
}
// There seems to be two different representations... maybe because
// of some legacy projects restoring customTemperamentNotes
for (const t of customLabels) {
labelsDict[t] = [];
for (const k in noteLabels[t]) {
if (k !== "pitchNumber" && k !== "interval") {
if (typeof(noteLabels[t][k]) === "number") {
// labels.push(k);
labelsDict[t].push(k);
blockCustom++;
} else {
if (noteLabels[t][k].length === 3) {
// labels.push(noteLabels[t][k][1]);
labelsDict[t].push(noteLabels[t][k][1]);
blockCustom++;
} else {
for (let ii = 0; ii < noteLabels[t][k].length; ii++) {
// labels.push(noteLabels[t][k][ii][1]);
labelsDict[t].push(noteLabels[t][k][1]);
blockCustom++;
}
}
}
}
}
for (let extra = max - blockCustom; extra > 0; extra--) {
// labels.push("");
}
blockCustom = 0;
}
if (!(selectedCustom in labelsDict)) {
selectedCustom = labelsDict[0];
}
for (let i = 0; i < max; i++) {
if (i < labelsDict[selectedCustom].length) {
labels.push(labelsDict[selectedCustom][i]);
} else {
labels.push("");
}
}
block._cusNoteWheel.navAngle = -(180 / customLabels.length) + 180 / labels.length;
block._cusNoteWheel.createWheel(labels);
block._exitWheel.colors = platformColor.exitWheelcolors;
block._exitWheel.slicePathFunction = slicePath().DonutSlice;
block._exitWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._exitWheel.slicePathCustom.minRadiusPercent = 0.0;
block._exitWheel.slicePathCustom.maxRadiusPercent = 0.1;
block._exitWheel.sliceSelectedPathCustom = block._exitWheel.slicePathCustom;
block._exitWheel.sliceInitPathCustom = block._exitWheel.slicePathCustom;
block._exitWheel.clickModeRotate = false;
block._exitWheel.initWheel(["×", " "]);
block._exitWheel.navItems[1].enabled = false;
block._exitWheel.navItems[0].sliceSelectedAttr.cursor = "pointer";
block._exitWheel.navItems[0].sliceHoverAttr.cursor = "pointer";
block._exitWheel.navItems[0].titleSelectedAttr.cursor = "pointer";
block._exitWheel.navItems[0].titleHoverAttr.cursor = "pointer";
block._exitWheel.createWheel();
const that = block;
// position widget
const x = block.container.x;
const y = block.container.y;
const canvasLeft = block.activity.canvas.offsetLeft + 28 * block.blocks.blockScale;
const canvasTop = block.activity.canvas.offsetTop + 6 * block.blocks.blockScale;
docById("wheelDiv").style.position = "absolute";
docById("wheelDiv").style.height = "400px";
docById("wheelDiv").style.width = "400px";
docById("wheelDiv").style.left =
Math.min(
block.blocks.turtles._canvas.width - 400,
Math.max(
0,
Math.round(
(x + block.activity.blocksContainer.x) * block.activity.getStageScale() + canvasLeft
) - 200
)
) + "px";
docById("wheelDiv").style.top =
Math.min(
block.blocks.turtles._canvas.height - 450,
Math.max(
0,
Math.round(
(y + block.activity.blocksContainer.y) * block.activity.getStageScale() + canvasTop
) - 200
)
) + "px";
if (hasOctaveWheel) {
// Use the octave associated with block block, if available.
const pitchOctave = block.blocks.findPitchOctave(block.connections[0]);
// Navigate to current octave
block._octavesWheel.navigateWheel(8 - pitchOctave);
}
// Add function to each main menu for show/hide sub menus
const __setupAction = function (i) {
that._customWheel.navItems[i].navigateFunction = function () {
that.customID =
that._customWheel.navItems[that._customWheel.selectedNavItemIndex].title;
labels = [];
for (let ii = 0; ii < max; ii++) {
if (ii < labelsDict[that._customWheel.navItems[i].title].length) {
labels.push(labelsDict[that._customWheel.navItems[i].title][ii]);
} else {
labels.push("");
}
}
// Update the navItems
for (let ii = 0; ii < labels.length; ii++) {
if (labels[ii].length === 0) {
that._cusNoteWheel.navItems[ii].navItem.hide();
} else {
that._cusNoteWheel.navItems[ii].title = labels[ii];
that._cusNoteWheel.navItems[ii].navItem.title = labels[ii];
that._cusNoteWheel.navItems[ii].basicNavTitleMax.title = labels[ii];
that._cusNoteWheel.navItems[ii].basicNavTitleMin.title = labels[ii];
that._cusNoteWheel.navItems[ii].hoverNavTitleMax.title = labels[ii];
that._cusNoteWheel.navItems[ii].hoverNavTitleMin.title = labels[ii];
that._cusNoteWheel.navItems[ii].selectedNavTitleMax.title = labels[ii];
that._cusNoteWheel.navItems[ii].selectedNavTitleMin.title = labels[ii];
that._cusNoteWheel.navItems[ii].initNavTitle.title = labels[ii];
that._cusNoteWheel.navItems[ii].navItem.show();
}
}
that._customWheel.refreshWheel();
that._cusNoteWheel.navigateWheel(0);
};
};
// Set up action for interval name so number tabs will
// initialize on load.
for (let ii = 0; ii < customLabels.length; ii++) {
__setupAction(ii);
}
// navigate to a specific starting point
let ii;
for (ii = 0; ii < customLabels.length; ii++) {
if (selectedCustom === customLabels[ii]) {
break;
}
}
if (ii === customLabels.length) {
ii = 0;
}
block._customWheel.navigateWheel(ii);
let j = selectedNote;
for (const x in noteLabels[selectedCustom]) {
if (x != "pitchNumber" && noteLabels[selectedCustom][x][1] == j) {
j = +x;
break;
}
}
if (typeof j === "number" && !isNaN(j)) {
block._cusNoteWheel.navigateWheel(max * customLabels.indexOf(selectedCustom) + j);
}
const __exitMenu = function () {
that._piemenuExitTime = new Date().getTime();
docById("wheelDiv").style.display = "none";
};
const __selectionChanged = function () {
const label = that._customWheel.navItems[that._customWheel.selectedNavItemIndex].title;
const note = that._cusNoteWheel.navItems[that._cusNoteWheel.selectedNavItemIndex].title;
that.value = note;
that.text.text = note;
let octave = 4;
if (hasOctaveWheel) {
// Set the octave of the pitch block if available
octave = Number(
that._octavesWheel.navItems[that._octavesWheel.selectedNavItemIndex].title
);
that.blocks.setPitchOctave(that.connections[0], octave);
}
// Make sure text is on top.
that.container.setChildIndex(that.text, that.container.children.length - 1);
that.updateCache();
const obj = getNote(note, octave, 0, "C major", false, null, that.activity.errorMsg, label);
const tur = that.activity.turtles.ithTurtle(0);
if (
tur.singer.instrumentNames.length === 0 ||
tur.singer.instrumentNames.indexOf(DEFAULTVOICE) === -1
) {
tur.singer.instrumentNames.push(DEFAULTVOICE);
that.activity.logo.synth.createDefaultSynth(0);
that.activity.logo.synth.loadSynth(0, DEFAULTVOICE);
}
that.activity.logo.synth.setMasterVolume(PREVIEWVOLUME);
Singer.setSynthVolume(that.activity.logo, 0, DEFAULTVOICE, PREVIEWVOLUME);
if (!that._triggerLock) {
that._triggerLock = true;
// Get the frequency of the custom note for the preview.
const no = that.activity.logo.synth.getCustomFrequency([note + obj[1]], that.customID);
if (no !== undefined && no !== "undefined") {
instruments[0][DEFAULTVOICE].triggerAttackRelease(no, 1 / 8);
}
}
setTimeout(function () {
that._triggerLock = false;
}, 1 / 8);
};
if (hasOctaveWheel) {
for (let i = 0; i < 8; i++) {
block._octavesWheel.navItems[i].navigateFunction = __selectionChanged;
}
}
// Set up handlers for preview.
for (let i = 0; i < labels.length; i++) {
block._cusNoteWheel.navItems[i].navigateFunction = __selectionChanged;
}
block._exitWheel.navItems[0].navigateFunction = __exitMenu;
};
const piemenuNthModalPitch = function (block, noteValues, note) {
// wheelNav pie menu for scale degree pitch selection
// check if a non-integer value is connected to note argument
// Pie menu would crash; so in such case navigate to closest integer
if (note % 1 !== 0) {
note = Math.floor(note + 0.5);
}
if (block.blocks.stageClick) {
return;
}
const noteLabels = [];
for (let i = 0; i < noteValues.length; i++) {
noteLabels.push(noteValues[i].toString());
}
noteLabels.push(null);
docById("wheelDiv").style.display = "";
block._pitchWheel = new wheelnav("wheelDiv", null, 600, 600);
block._octavesWheel = new wheelnav("_octavesWheel", block._pitchWheel.raphael);
block._exitWheel = new wheelnav("_exitWheel", block._pitchWheel.raphael);
wheelnav.cssMode = true;
block._pitchWheel.keynavigateEnabled = false;
block._pitchWheel.colors = platformColor.pitchWheelcolors;
block._pitchWheel.slicePathFunction = slicePath().DonutSlice;
block._pitchWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._pitchWheel.slicePathCustom.minRadiusPercent = 0.35;
block._pitchWheel.slicePathCustom.maxRadiusPercent = 0.72;
block._pitchWheel.sliceSelectedPathCustom = block._pitchWheel.slicePathCustom;
block._pitchWheel.sliceInitPathCustom = block._pitchWheel.slicePathCustom;
block._pitchWheel.animatetime = 0; // 300;
block._pitchWheel.createWheel(noteLabels);
block._exitWheel.colors = platformColor.exitWheelcolors;
block._exitWheel.slicePathFunction = slicePath().DonutSlice;
block._exitWheel.slicePathCustom = slicePath().DonutSliceCustomization();
block._exitWheel.slicePathCustom.minRadiusPercent = 0.0;
block._exitWheel.slicePathCustom.maxRadiusPercent = 0.2;
block._exitWheel.sliceSelectedPathCustom = block._exitWheel.slicePathCustom;
block._exitWheel.sliceInitPathCustom = block._exitWheel.slicePathCustom;
block._exitWheel.clickModeRotate = false;
block._exitWheel.initWheel(["×", " "]);