Skip to content

Commit 5fed390

Browse files
committed
[ADD] some function's converter in music.js
1 parent dcee540 commit 5fed390

File tree

1 file changed

+78
-0
lines changed
  • src/lib/ruby-to-blocks-converter

1 file changed

+78
-0
lines changed

src/lib/ruby-to-blocks-converter/music.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,84 @@ import _ from 'lodash';
55
* Music converter
66
*/
77
const MusicConverter = {
8+
onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) {
9+
let block;
10+
if ((this._isSelf(receiver) || receiver === Opal.nil) && !rubyBlock) {
11+
console.log(name);
12+
console.log(args);
13+
switch (name) {
14+
case 'play_drum':
15+
if (args.length === 1 && this._isHash(args[0]) && args[0].size === 2) {
16+
const drum = args[0].get('sym:drum');
17+
const beats = args[0].get('sym:beats');
18+
if (this._isNumberOrBlock(drum) && this._isNumberOrBlock(beats)) {
19+
block = this._createBlock('music_playDrumForBeats', 'statement');
20+
this._addInput(
21+
block,
22+
'DRUM',
23+
this._createFieldBlock('music_menu_DRUM', 'DRUM', drum)
24+
);
25+
this._addNumberInput(block, 'BEATS', 'math_number', beats, 0.25);
26+
}
27+
}
28+
break;
29+
case 'rest':
30+
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
31+
block = this._createBlock('music_restForBeats', 'statement');
32+
this._addNumberInput(block, 'BEATS', 'math_number', args[0], 0.25);
33+
}
34+
break;
35+
case 'play_note':
36+
if (args.length === 1 && this._isHash(args[0]) && args[0].size === 2) {
37+
const note = args[0].get('sym:note');
38+
const beats = args[0].get('sym:beats');
39+
if (this._isNumberOrBlock(note) && this._isNumberOrBlock(beats)) {
40+
block = this._createBlock('music_playNoteForBeats', 'statement');
41+
this._addNumberInput(block, 'NOTE', 'math_number', note, 60);
42+
this._addNumberInput(block, 'BEATS', 'math_number', beats, 0.25);
43+
}
44+
}
45+
break;
46+
case 'instrument=':
47+
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
48+
block = this._createBlock('music_setInstrument', 'statement');
49+
this._addInput(
50+
block,
51+
'INSTRUMENT',
52+
this._createFieldBlock('music_menu_INSTRUMENT', 'INSTRUMENT', args[0])
53+
);
54+
}
55+
break;
56+
case 'tempo=':
57+
if (args.length === 1 && this._isNumberOrBlock(args[0])) {
58+
block = this._createBlock('music_setTempo', 'statement');
59+
this._addNumberInput(block, 'TEMPO', 'math_number', args[0], 60);
60+
}
61+
break;
62+
case 'tempo':
63+
if (args.length === 0) {
64+
block = this._createBlock('music_getTempo', 'value');
65+
}
66+
break;
67+
}
68+
}
69+
return block;
70+
},
71+
72+
// eslint-disable-next-line no-unused-vars
73+
onOpAsgn: function (lh, operator, rh) {
74+
let block;
75+
if (this._isBlock(lh) && operator === '+' && this._isNumberOrBlock(rh)) {
76+
let xy;
77+
switch (lh.opcode) {
78+
case 'music_getTempo':
79+
block = this._changeBlock(lh, 'music_changeTempo', 'statement');
80+
this._addNumberInput(block, 'TEMPO', 'math_number', rh, 20);
81+
break;
82+
}
83+
}
84+
return block;
85+
}
886
};
987

1088
export default MusicConverter;

0 commit comments

Comments
 (0)