|
| 1 | +/* global Opal */ |
| 2 | +import _ from 'lodash'; |
| 3 | + |
| 4 | +const Effect = [ |
| 5 | + 'PITCH', |
| 6 | + 'PAN' |
| 7 | +]; |
| 8 | + |
| 9 | +/** |
| 10 | + * Sound converter |
| 11 | + */ |
| 12 | +const SoundConverter = { |
| 13 | + // eslint-disable-next-line no-unused-vars |
| 14 | + onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) { |
| 15 | + let block; |
| 16 | + if ((this._isSelf(receiver) || receiver === Opal.nil) && !rubyBlock) { |
| 17 | + switch (name) { |
| 18 | + case 'play_until_done': |
| 19 | + case 'play': |
| 20 | + if (args.length === 1 && this._isStringOrBlock(args[0])) { |
| 21 | + let opcode; |
| 22 | + if (name === 'play_until_done') { |
| 23 | + opcode = 'sound_playuntildone'; |
| 24 | + } else { |
| 25 | + opcode = 'sound_play'; |
| 26 | + } |
| 27 | + const menuBlock = this._createBlock('sound_sounds_menu', 'value', { |
| 28 | + shadow: true |
| 29 | + }); |
| 30 | + let inputBlock; |
| 31 | + let shadowBlock; |
| 32 | + if (this._isString(args[0])) { |
| 33 | + this._addField(menuBlock, 'SOUND_MENU', args[0]); |
| 34 | + inputBlock = menuBlock; |
| 35 | + shadowBlock = menuBlock; |
| 36 | + } else { |
| 37 | + this._addField(menuBlock, 'SOUND_MENU', ''); |
| 38 | + inputBlock = args[0]; |
| 39 | + shadowBlock = menuBlock; |
| 40 | + } |
| 41 | + block = this._createBlock(opcode, 'statement'); |
| 42 | + this._addInput(block, 'SOUND_MENU', inputBlock, shadowBlock); |
| 43 | + } |
| 44 | + break; |
| 45 | + case 'stop_all_sounds': |
| 46 | + case 'clear_sound_effects': |
| 47 | + case 'volume': |
| 48 | + if (args.length === 0) { |
| 49 | + let opcode; |
| 50 | + let blockType; |
| 51 | + switch (name) { |
| 52 | + case 'stop_all_sounds': |
| 53 | + opcode = 'sound_stopallsounds'; |
| 54 | + blockType = 'statement'; |
| 55 | + break; |
| 56 | + case 'clear_sound_effects': |
| 57 | + opcode = 'sound_cleareffects'; |
| 58 | + blockType = 'statement'; |
| 59 | + break; |
| 60 | + case 'volume': |
| 61 | + opcode = 'sound_volume'; |
| 62 | + blockType = 'value'; |
| 63 | + break; |
| 64 | + } |
| 65 | + block = this._createBlock(opcode, blockType); |
| 66 | + } |
| 67 | + break; |
| 68 | + case 'change_sound_effect_by': |
| 69 | + case 'set_sound_effect': |
| 70 | + if (args.length === 2 && this._isString(args[0]) && Effect.indexOf(args[0].toString()) >= 0 && |
| 71 | + this._isNumberOrBlock(args[1])) { |
| 72 | + let opcode; |
| 73 | + let value; |
| 74 | + if (name === 'change_sound_effect_by') { |
| 75 | + opcode = 'sound_changeeffectby'; |
| 76 | + value = 10; |
| 77 | + } else { |
| 78 | + opcode = 'sound_seteffectto'; |
| 79 | + value = 100; |
| 80 | + } |
| 81 | + block = this._createBlock(opcode, 'statement'); |
| 82 | + this._addField(block, 'EFFECT', args[0]); |
| 83 | + this._addNumberInput(block, 'VALUE', 'math_number', args[1], value); |
| 84 | + } |
| 85 | + break; |
| 86 | + case 'volume=': |
| 87 | + if (args.length === 1 && this._isNumberOrBlock(args[0])) { |
| 88 | + block = this._createBlock('sound_setvolumeto', 'statement'); |
| 89 | + this._addNumberInput(block, 'VOLUME', 'math_number', args[0], 100); |
| 90 | + } |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + return block; |
| 95 | + }, |
| 96 | + |
| 97 | + // eslint-disable-next-line no-unused-vars |
| 98 | + onOpAsgn: function (lh, operator, rh) { |
| 99 | + let block; |
| 100 | + if (this._isBlock(lh) && lh.opcode === 'sound_volume' && operator === '+' && this._isNumberOrBlock(rh)) { |
| 101 | + block = this._changeBlock(lh, 'sound_changevolumeby', 'statement'); |
| 102 | + this._addNumberInput(block, 'VOLUME', 'math_number', rh, -10); |
| 103 | + } |
| 104 | + return block; |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +export default SoundConverter; |
0 commit comments