-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathGraphicsBlocks.js
More file actions
1266 lines (1157 loc) · 47.5 KB
/
GraphicsBlocks.js
File metadata and controls
1266 lines (1157 loc) · 47.5 KB
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) 2019 Bottersnike
//
// 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
last, _, ValueBlock, FlowClampBlock, FlowBlock, MusicBlocks, Mouse,
NOINPUTERRORMSG, NANERRORMSG, toFixed2, _THIS_IS_MUSIC_BLOCKS_
*/
/*
Global locations
- js/utils/utils.js
_
- js/protoblocks.js
ValueBlock, FlowClampBlock
- js/js-export/export.js
MusicBlocks, Mouse
- js/logo.js
NOINPUTERRORMSG, NANERRORMSG
- js/utils/utils.js
toFixed2
*/
/* exported setupGraphicsBlocks */
function setupGraphicsBlocks(activity) {
/**
* Represents a block for retrieving the orientation or compass direction.
* @extends {ValueBlock}
*/
class HeadingBlock extends ValueBlock {
/**
* Constructs a HeadingBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("heading", _("heading"));
// Set the palette, activity, and beginner block for the heading block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
this.parameter = true;
// Set the help string for the heading block based on the context
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The Heading block returns the orientation of the mouse."),
"documentation",
""
]);
} else {
this.setHelpString([
_("The Heading block returns the orientation of the turtle."),
"documentation",
""
]);
}
}
/**
* Sets the heading value for the turtle.
* @param {object} logo - The logo object.
* @param {number} value - The heading value.
* @param {object} turtle - The turtle object.
*/
setter(logo, value, turtle) {
const turtleObj = activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle));
turtleObj.painter.doSetHeading(value);
}
/**
* Updates the parameter value for the heading block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @returns {number} - The updated heading value.
*/
updateParameter(logo, turtle) {
return toFixed2(
activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle)).orientation
);
}
/**
* Retrieves the argument value for the heading block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {number} blk - The block number.
* @returns {number} - The heading value.
*/
arg(logo, turtle, blk) {
const connections = activity?.blocks?.blockList?.[blk]?.connections;
const parentId = connections?.[0];
if (
logo.inStatusMatrix &&
parentId != null &&
parentId in activity.blocks.blockList &&
activity.blocks.blockList[parentId]?.name === "print"
) {
logo.statusFields.push([blk, "heading"]);
} else {
return activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle))
.orientation;
}
}
}
/**
* Represents a block for retrieving the vertical position (Y-coordinate).
* @extends {ValueBlock}
*/
class YBlock extends ValueBlock {
/**
* Constructs a YBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("y");
// Set the palette, activity, and beginner block for the Y block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
this.parameter = true;
// Set the help string for the Y block based on the context
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The Y block returns the vertical position of the mouse."),
"documentation",
null,
"xyhelp"
]);
} else {
this.setHelpString([
_("The Y block returns the vertical position of the turtle."),
"documentation",
null,
"xyhelp"
]);
}
// Form the block with specific parameters
this.formBlock({
name: this.lang === "ja" ? _("y3") : _("y")
});
}
/**
* Sets the Y-coordinate value for the turtle.
* @param {object} logo - The logo object.
* @param {number} value - The Y-coordinate value.
* @param {object} turtle - The turtle object.
*/
setter(logo, value, turtle) {
const turtleObj = activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle));
turtleObj.painter.doSetXY(turtleObj.x, value);
}
/**
* Updates the parameter value for the Y block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @returns {number} - The updated Y-coordinate value.
*/
updateParameter(logo, turtle) {
return toFixed2(activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle)).y);
}
/**
* Retrieves the argument value for the Y block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {number} blk - The block number.
* @returns {number} - The Y-coordinate value.
*/
arg(logo, turtle, blk) {
const blocks = logo?.activity?.blocks || logo?.blocks || activity?.blocks;
const connections = blocks?.blockList?.[blk]?.connections;
const parentId = connections?.[0];
if (
logo.inStatusMatrix &&
parentId != null &&
parentId in activity.blocks.blockList &&
activity.blocks.blockList[parentId]?.name === "print"
) {
logo.statusFields.push([blk, "y"]);
} else {
return activity.turtles.screenY2turtleY(
activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle)).container.y
);
}
}
}
/**
* Represents a block for retrieving the horizontal position (X-coordinate).
* @extends {ValueBlock}
*/
class XBlock extends ValueBlock {
/**
* Constructs an XBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("x");
// Set the palette, activity, and beginner block for the X block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
this.parameter = true;
// Set the help string for the X block based on the context
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The X block returns the horizontal position of the mouse."),
"documentation",
null,
"xyhelp"
]);
} else {
this.setHelpString([
_("The X block returns the horizontal position of the turtle."),
"documentation",
null,
"xyhelp"
]);
}
// Form the block with specific parameters
this.formBlock({
name: this.lang === "ja" ? _("x3") : _("x")
});
}
/**
* Sets the X-coordinate value for the turtle.
* @param {object} logo - The logo object.
* @param {number} value - The X-coordinate value.
* @param {object} turtle - The turtle object.
*/
setter(logo, value, turtle) {
const turtleObj = activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle));
turtleObj.painter.doSetXY(value, turtleObj.y);
}
/**
* Updates the parameter value for the X block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @returns {number} - The updated X-coordinate value.
*/
updateParameter(logo, turtle) {
return toFixed2(activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle)).x);
}
/**
* Retrieves the argument value for the X block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {number} blk - The block number.
* @returns {number} - The X-coordinate value.
*/
arg(logo, turtle, blk) {
const blocks = logo?.activity?.blocks || logo?.blocks || activity?.blocks;
const connections = blocks?.blockList?.[blk]?.connections;
const parentId = connections?.[0];
if (
logo.inStatusMatrix &&
parentId != null &&
parentId in activity.blocks.blockList &&
activity.blocks.blockList[parentId]?.name === "print"
) {
logo.statusFields.push([blk, "x"]);
} else {
return activity.turtles.screenX2turtleX(
activity.turtles.getTurtle(activity.turtles.companionTurtle(turtle)).container.x
);
}
}
}
/**
* Represents a block for scrolling the canvas by x, y position.
* @extends {FlowBlock}
*/
class ScrollXYBlock extends FlowBlock {
/**
* Constructs a ScrollXYBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("scrollxy", _("scroll xy"));
// Set the palette, activity, and beginner block for the Scroll XY block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
// Set the help string for the Scroll XY block
this.setHelpString([
_("The Scroll XY block moves the canvas."),
"documentation",
null,
"everybeathelp"
]);
// Form the block with specific parameters
this.formBlock({
args: 2,
defaults: [100, 0],
argLabels: this.lang === "ja" ? [_("x2"), _("y2")] : [_("x"), _("y")]
});
}
/**
* Executes the flow of the Scroll XY block.
* @param {number[]} args - The arguments for scrolling (x, y position).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 2) {
if (typeof args[0] === "string" || typeof args[1] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (logo.inMatrix) {
logo.phraseMaker.addRowBlock(blk);
if (!logo.pitchBlocks.includes(blk)) {
logo.pitchBlocks.push(blk);
}
logo.phraseMaker.rowLabels.push(activity.blocks.blockList[blk].name);
logo.phraseMaker.rowArgs.push([args[0], args[1]]);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
const requiredTurtle = activity.turtles.getTurtle(
activity.turtles.companionTurtle(turtle)
);
if (tur.singer.suppressOutput) {
const savedPenState = requiredTurtle.painter.penState;
requiredTurtle.painter.penState = false;
requiredTurtle.painter.doScrollXY(args[0], args[1]);
requiredTurtle.painter.penState = savedPenState;
} else {
requiredTurtle.painter.doScrollXY(args[0], args[1]);
}
}
}
}
}
/**
* Represents a block for clearing the screen and returning the turtle to the center position.
* @extends {FlowBlock}
*/
class ClearBlock extends FlowBlock {
/**
* Constructs a ClearBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("clear", _("Clear").toLowerCase());
// Set the palette and activity for the Clear block
this.setPalette("graphics", activity);
// Set the help string for the Clear block
this.setHelpString();
}
/**
* Executes the flow of the Clear block.
* @param {number[]} args - The arguments for clearing (unused).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (logo.inMatrix) {
// Ignore clear block in matrix
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
if (tur.singer.suppressOutput) {
const savedPenState = tur.painter.penState;
tur.painter.penState = false;
tur.painter.doSetXY(0, 0);
tur.painter.doSetHeading(0);
tur.painter.penState = savedPenState;
} else {
logo.svgBackground = true;
tur.painter.doClear(true, true, true);
}
}
}
}
/**
* Represents a block for setting the second control point for the Bezier curve.
* @extends {FlowBlock}
*/
class ControlPoint2Block extends FlowBlock {
/**
* Constructs a ControlPoint2Block instance.
*/
constructor() {
// Call the constructor of the parent class
super("controlpoint2");
// Set the palette, activity, and help string for the Control-point 2 block
this.setPalette("graphics", activity);
this.setHelpString([
_("The Control-point 2 block sets the second control point for the Bezier curve."),
"documentation",
""
]);
// Form the block with specific parameters
this.formBlock({
name: _("control point 2"),
args: 2,
defaults: [100, 25],
argLabels: this.lang === "ja" ? [_("x1"), _("y1")] : [_("x"), _("y")]
});
}
/**
* Executes the flow of the Control-point 2 block.
* @param {number[]} args - The arguments for the control point (x, y position).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 2) {
if (typeof args[0] === "string" || typeof args[1] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
tur.painter.setControlPoint2(args);
}
}
}
}
/**
* Represents a block for setting the first control point for the Bezier curve.
* @extends {FlowBlock}
*/
class ControlPoint1Block extends FlowBlock {
/**
* Constructs a ControlPoint1Block instance.
*/
constructor() {
// Call the constructor of the parent class
super("controlpoint1");
// Set the palette, activity, and help string for the Control-point 1 block
this.setPalette("graphics", activity);
this.setHelpString([
_("The Control-point 1 block sets the first control point for the Bezier curve."),
"documentation",
""
]);
// Form the block with specific parameters
this.formBlock({
name: _("control point 1"),
args: 2,
defaults: [100, 75],
argLabels: this.lang === "ja" ? [_("x1"), _("y1")] : [_("x"), _("y")]
});
}
/**
* Executes the flow of the Control-point 1 block.
* @param {number[]} args - The arguments for the control point (x, y position).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 2) {
if (typeof args[0] === "string" || typeof args[1] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
tur.painter.setControlPoint1(args);
}
}
}
}
/**
* Represents a block for drawing a Bezier curve.
* @extends {FlowBlock}
*/
class BezierBlock extends FlowBlock {
/**
* Constructs a BezierBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("bezier");
// Set the palette, activity, and help string for the Bezier block
this.setPalette("graphics", activity);
this.setHelpString([_("The Bezier block draws a Bezier curve."), "documentation", ""]);
// Form the block with specific parameters
this.formBlock({
name: _("bezier"),
args: 2,
defaults: [0, 100],
argLabels: this.lang === "ja" ? [_("x1"), _("y1")] : [_("x"), _("y")]
});
}
/**
* Executes the flow of the Bezier block.
* @param {number[]} args - The arguments for the Bezier curve (x, y position).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 2) {
if (typeof args[0] === "string" || typeof args[1] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
if (tur.singer.suppressOutput) {
const savedPenState = tur.painter.penState;
tur.painter.penState = false;
tur.painter.doBezier(args[0], args[1]);
tur.painter.penState = savedPenState;
} else {
tur.painter.doBezier(args[0], args[1]);
}
}
}
}
}
/**
* Represents a block for moving the turtle or mouse in an arc.
* @extends {FlowBlock}
*/
class ArcBlock extends FlowBlock {
/**
* Constructs an ArcBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("arc");
// Set the palette, activity, and beginner block for the Arc block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
// Set piemenu values for C1 and C2
this.piemenuValuesC1 = [
15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270,
285, 300, 315, 330, 345, 360
];
this.piemenuValuesC2 = [25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300];
// Set the help string for the Arc block
this.setHelpString([
_("The Arc block moves the turtle in an arc."),
"documentation",
null,
"archelp"
]);
// Form the block with specific parameters
this.formBlock({
name: _("arc"),
args: 2,
defaults: [90, 100],
argLabels: [_("angle"), _("radius")]
});
}
/**
* Executes the flow of the Arc block.
* @param {number[]} args - The arguments for the arc (angle, radius).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
const isWrap = activity.turtles.ithTurtle(turtle).painter.wrap;
if (args.length === 2) {
if ((args[1] > 5000 || args[1] < -5000) && (isWrap == false || isWrap == null)) {
activity.errorMsg(
_("Value must be within -5000 to 5000 when Wrap Mode is off."),
blk
);
} else if ((args[1] > 20000 || args[1] < -20000) && isWrap == true) {
activity.errorMsg(
_("Value must be within -20000 to 20000 when Wrap Mode is on."),
blk
);
} else {
if (typeof args[0] === "string" || typeof args[1] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (logo.inMatrix) {
logo.phraseMaker.addRowBlock(blk);
if (!logo.pitchBlocks.includes(blk)) {
logo.pitchBlocks.push(blk);
}
logo.phraseMaker.rowLabels.push(activity.blocks.blockList[blk].name);
logo.phraseMaker.rowArgs.push([args[0], args[1]]);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
if (tur.singer.suppressOutput) {
const savedPenState = tur.painter.penState;
tur.painter.penState = false;
tur.painter.doArc(args[0], args[1]);
tur.painter.penState = savedPenState;
} else {
tur.painter.doArc(args[0], args[1]);
}
}
}
}
}
}
/**
* Represents a block for setting the heading of the turtle or mouse.
* @extends {FlowBlock}
*/
class SetHeadingBlock extends FlowBlock {
/**
* Constructs a SetHeadingBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("setheading", _("set heading"));
// Set the palette, activity, and beginner block for the Set Heading block
this.setPalette("graphics", activity);
this.beginnerBlock(this.lang !== "ja");
// Set piemenu values for C1
this.piemenuValuesC1 = [
0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 240, 270, 300, 315, 330
];
// Set the help string for the Set Heading block
this.setHelpString([
_("The Set heading block sets the heading of the turtle."),
"documentation",
""
]);
// Form the block with specific parameters
this.formBlock({
args: 1,
defaults: [0]
});
}
/**
* Executes the flow of the Set Heading block.
* @param {number[]} args - The arguments for the heading (angle).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 1) {
if (typeof args[0] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (logo.inMatrix) {
logo.phraseMaker.addRowBlock(blk);
if (!logo.pitchBlocks.includes(blk)) {
logo.pitchBlocks.push(blk);
}
logo.phraseMaker.rowLabels.push(activity.blocks.blockList[blk].name);
logo.phraseMaker.rowArgs.push(args[0]);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
tur.painter.doSetHeading(args[0]);
}
}
}
}
/**
* Represents a block for setting the XY position of the turtle or mouse.
* @extends {FlowBlock}
*/
class SetXYBlock extends FlowBlock {
/**
* Constructs a SetXYBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("setxy");
// Set the palette, activity, and beginner block for the Set XY block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
// Set the help string for the Set XY block
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The Set XY block moves the mouse to a specific position on the screen."),
"documentation",
""
]);
} else {
this.setHelpString([
_("The Set XY block moves the turtle to a specific position on the screen."),
"documentation",
""
]);
}
// Form the block with specific parameters
this.formBlock({
name: _("set xy"),
args: 2,
defaults: [0, 0],
argLabels: this.lang === "ja" ? [_("x1"), _("y1")] : [_("x"), _("y")]
});
}
/**
* Executes the flow of the Set XY block.
* @param {number[]} args - The arguments for the XY position (x, y).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
const isWrap = activity.turtles.ithTurtle(turtle).painter.wrap;
if (args.length === 2) {
if (
(args[0] > 5000 || args[1] > 5000 || args[0] < -5000 || args[1] < -5000) &&
(isWrap == false || isWrap == null)
) {
activity.errorMsg(
_("Value must be within -5000 to 5000 when Wrap Mode is off."),
blk
);
} else if (
(args[0] > 20000 || args[1] > 20000 || args[0] < -20000 || args[1] < -20000) &&
isWrap == true
) {
activity.errorMsg(
_("Value must be within -20000 to 20000 when Wrap Mode is on."),
blk
);
} else {
if (typeof args[0] === "string" || typeof args[1] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (logo.inMatrix) {
logo.phraseMaker.addRowBlock(blk);
if (!logo.pitchBlocks.includes(blk)) {
logo.pitchBlocks.push(blk);
}
logo.phraseMaker.rowLabels.push(activity.blocks.blockList[blk].name);
logo.phraseMaker.rowArgs.push([args[0], args[1]]);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
if (tur.singer.suppressOutput) {
const savedPenState = tur.painter.penState;
tur.painter.penState = false;
tur.painter.doSetXY(args[0], args[1]);
tur.painter.penState = savedPenState;
} else {
tur.painter.doSetXY(args[0], args[1]);
}
}
}
}
}
}
/**
* Represents a block for turning the turtle or mouse to the right.
* @extends {FlowBlock}
*/
class RightBlock extends FlowBlock {
/**
* Constructs a RightBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("right");
// Set the palette, activity, and beginner block for the Right block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
// Set piemenu values for C1
this.piemenuValuesC1 = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330];
// Set the help string for the Right block
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The Right block turns the mouse to the right."),
"documentation",
null,
"forwardhelp"
]);
} else {
this.setHelpString([
_("The Right block turns the turtle to the right."),
"documentation",
null,
"forwardhelp"
]);
}
// Form the block with specific parameters
this.formBlock({
name: this.lang === "ja" ? _("right1") : _("right"),
args: 1,
defaults: [90]
});
}
/**
* Executes the flow of the Right block.
* @param {number[]} args - The arguments for turning right (angle).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 1) {
if (typeof args[0] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (logo.inMatrix) {
logo.phraseMaker.addRowBlock(blk);
if (!logo.pitchBlocks.includes(blk)) {
logo.pitchBlocks.push(blk);
}
logo.phraseMaker.rowLabels.push(activity.blocks.blockList[blk].name);
logo.phraseMaker.rowArgs.push(args[0]);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
if (tur.singer.suppressOutput) {
const savedPenState = tur.painter.penState;
tur.painter.penState = false;
tur.painter.doRight(args[0]);
tur.painter.penState = savedPenState;
} else {
tur.painter.doRight(args[0]);
}
}
}
}
}
/**
* Represents a block for turning the turtle or mouse to the left.
* @extends {FlowBlock}
*/
class MLeftBlock extends FlowBlock {
/**
* Constructs an MLeftBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("left");
// Set the palette, activity, and beginner block for the Left block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
// Set piemenu values for C1
this.piemenuValuesC1 = [330, 300, 270, 240, 210, 180, 150, 120, 90, 60, 30, 0];
// Set the help string for the Left block
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The Left block turns the mouse to the left."),
"documentation",
null,
"forwardhelp"
]);
} else {
this.setHelpString([
_("The Left block turns the turtle to the left."),
"documentation",
null,
"forwardhelp"
]);
}
// Form the block with specific parameters
this.formBlock({
name: this.lang === "ja" ? _("left1") : _("left"),
args: 1,
defaults: [90]
});
}
/**
* Executes the flow of the Left block.
* @param {number[]} args - The arguments for turning left (angle).
* @param {object} logo - The logo object.
* @param {number} turtle - The turtle number.
* @param {number} blk - The block number.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(activity.turtles.companionTurtle(turtle));
if (args.length === 1) {
if (typeof args[0] === "string") {
activity.errorMsg(NANERRORMSG, blk);
} else if (logo.inMatrix) {
logo.phraseMaker.addRowBlock(blk);
if (!logo.pitchBlocks.includes(blk)) {
logo.pitchBlocks.push(blk);
}
logo.phraseMaker.rowLabels.push(activity.blocks.blockList[blk].name);
logo.phraseMaker.rowArgs.push(args[0]);
} else if (tur.singer.inNoteBlock.length > 0) {
tur.singer.embeddedGraphics[last(tur.singer.inNoteBlock)].push(blk);
} else {
if (tur.singer.suppressOutput) {
const savedPenState = tur.painter.penState;
tur.painter.penState = false;
tur.painter.doRight(-args[0]);
tur.painter.penState = savedPenState;
} else {
tur.painter.doRight(-args[0]);
}
}
}
}
}
/**
* Represents a block for moving the turtle or mouse backward.
* @extends {FlowBlock}
*/
class BackBlock extends FlowBlock {
/**
* Constructs a BackBlock instance.
*/
constructor() {
// Call the constructor of the parent class
super("back", _("back"));
// Set the palette, activity, and beginner block for the Back block
this.setPalette("graphics", activity);
this.beginnerBlock(true);
// Set the help string for the Back block
if (_THIS_IS_MUSIC_BLOCKS_) {
this.setHelpString([
_("The Back block moves the mouse backward."),
"documentation",
null,
"forwardhelp"
]);
} else {
this.setHelpString([
_("The Back block moves the turtle backward."),
"documentation",