diff --git a/cspell.json b/cspell.json index f38a17054a5..e5725632cb8 100644 --- a/cspell.json +++ b/cspell.json @@ -2,6 +2,7 @@ "version": "0.2", "language": "en", "words": [ + "FREEFALL", "gapi", "googleusercontent", "Hira", diff --git a/package-lock.json b/package-lock.json index 2ac050482ff..1f537776029 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29135,7 +29135,7 @@ }, "node_modules/scratch-vm": { "version": "5.0.300", - "resolved": "git+ssh://git@github.com/smalruby/scratch-vm.git#cc73cc0d84886c93c685205a8e3ea0e79832c71b", + "resolved": "git+ssh://git@github.com/smalruby/scratch-vm.git#7a2b8e463be2bdc3531c586ab506597b7dcc15bf", "license": "AGPL-3.0-only", "dependencies": { "@vernier/godirect": "^1.5.0", diff --git a/src/containers/ruby-tab/microbit-more-snippets.json b/src/containers/ruby-tab/microbit-more-snippets.json index 79df2adce1f..cec19666a33 100644 --- a/src/containers/ruby-tab/microbit-more-snippets.json +++ b/src/containers/ruby-tab/microbit-more-snippets.json @@ -34,6 +34,11 @@ "description": "[動いた▼] とき", "type": "event" }, + "microbit_more.when_tilted": { + "snippet": "microbit_more.when_tilted(\"${1:any}\") do\n\t${2}\nend", + "description": "[どこか▼] に傾いたとき", + "type": "event" + }, "microbit_more.tilted?": { "snippet": "microbit_more.tilted?(\"${1:any}\")", "description": "[どれかの向き▼] に傾いた", diff --git a/src/lib/ruby-generator/microbit_more.js b/src/lib/ruby-generator/microbit_more.js index 392901c506d..47fcd2b737c 100644 --- a/src/lib/ruby-generator/microbit_more.js +++ b/src/lib/ruby-generator/microbit_more.js @@ -46,23 +46,24 @@ export default function (Generator) { return `microbit_more.when_pin_connected(${pin}) do\n`; }; - const GestureLabel = { - TILT_UP: 'tilted_front', - TILT_DOWN: 'tilted_back', - TILT_LEFT: 'tilted_left', - TILT_RIGHT: 'tilted_right', - FACE_UP: 'face up', - FACE_DOWN: 'face down', - FREEFALL: 'freefall', - G3: '3G', - G6: '6G', - G8: '8G', - SHAKE: 'shake', - MOVED: 'moved', - TILTED: 'tilted_any' - }; Generator.microbitMore_whenGesture = function (block) { block.isStatement = true; + const GestureLabel = { + TILT_UP: 'tilted_front', + TILT_DOWN: 'tilted_back', + TILT_LEFT: 'tilted_left', + TILT_RIGHT: 'tilted_right', + FACE_UP: 'face up', + FACE_DOWN: 'face down', + FREEFALL: 'freefall', + G3: '3G', + G6: '6G', + G8: '8G', + SHAKE: 'shake', + JUMPED: 'jumped', + MOVED: 'moved', + TILTED: 'tilted_any' + }; const gesture = Generator.getFieldValue(block, 'GESTURE', 'SHAKE'); const gestureLabel = Generator.quote_(GestureLabel[gesture]); return `microbit_more.when(${gestureLabel}) do\n`; @@ -70,14 +71,21 @@ export default function (Generator) { const TiltedDirectionLabel = { ANY: 'any', - TILT_UP: 'front', - TILT_DOWN: 'back', - TILT_LEFT: 'left', - TILT_RIGHT: 'right' + FRONT: 'front', + BACK: 'back', + LEFT: 'left', + RIGHT: 'right' }; + Generator.microbitMore_whenTilted = function (block) { + block.isStatement = true; + const direction = Generator.getFieldValue(block, 'DIRECTION', 'ANY'); + const directionLabel = Generator.quote_(TiltedDirectionLabel[direction] || direction); + return `microbit_more.when_tilted(${directionLabel}) do\n`; + }; + Generator.microbitMore_isTilted = function (block) { const direction = Generator.getFieldValue(block, 'DIRECTION', 'ANY'); - const directionLabel = Generator.quote_(TiltedDirectionLabel[direction]); + const directionLabel = Generator.quote_(TiltedDirectionLabel[direction] || direction); return [`microbit_more.tilted?(${directionLabel})`, Generator.ORDER_FUNCTION_CALL]; }; diff --git a/src/lib/ruby-to-blocks-converter/microbit_more.js b/src/lib/ruby-to-blocks-converter/microbit_more.js index f4a3721bbcb..51f64ca3d8a 100644 --- a/src/lib/ruby-to-blocks-converter/microbit_more.js +++ b/src/lib/ruby-to-blocks-converter/microbit_more.js @@ -41,6 +41,7 @@ const GestureMenu = { G6: '6G', G8: '8G', SHAKE: 'shake', + JUMPED: 'jumped', MOVED: 'moved', TILTED: 'tilted_any' }; @@ -108,22 +109,23 @@ const TouchPinIDMenuLower = Object.keys(TouchPinIDMenu); const TouchPinIDMenuValue = Object.values(TouchPinIDMenu); const TiltDirectionMenu = { - any: 'ANY', - front: 'TILT_UP', - back: 'TILT_DOWN', - left: 'TILT_LEFT', - right: 'TILT_RIGHT' + front: 'FRONT', + back: 'BACK', + left: 'LEFT', + right: 'RIGHT', + any: 'ANY' }; const TiltDirectionMenuLower = Object.keys(TiltDirectionMenu); const TiltDirectionMenuValue = Object.values(TiltDirectionMenu); -const TiltAngleDirectionMenu = [ - 'FRONT', - 'BACK', - 'LEFT', - 'RIGHT' -]; -const TiltAngleDirectionMenuLower = TiltAngleDirectionMenu.map(x => x.toLowerCase()); +const TiltAngleDirectionMenu = { + front: 'FRONT', + back: 'BACK', + left: 'LEFT', + right: 'RIGHT' +}; +const TiltAngleDirectionMenuLower = Object.keys(TiltAngleDirectionMenu); +const TiltAngleDirectionMenuValue = Object.values(TiltAngleDirectionMenu); /** * MicrobitMore converter @@ -259,6 +261,24 @@ const MicrobitMoreConverter = { return block; }); + converter.registerOnSendWithBlock(MicrobitMore, 'when_tilted', 1, 0, params => { + const {receiver, args, rubyBlock} = params; + + if (converter.isString(args[0])) { + const index = TiltDirectionMenuLower.indexOf(args[0].toString().toLowerCase()); + if (index < 0) return null; + + args[0] = new Primitive('str', TiltDirectionMenuValue[index], args[0].node); + } else { + return null; + } + + const block = converter.changeRubyExpressionBlock(receiver, 'microbitMore_whenTilted', 'hat'); + converter.addField(block, 'DIRECTION', args[0]); + converter.setParent(rubyBlock, block); + return block; + }); + converter.registerOnSendWithBlock(MicrobitMore, 'when_pin_connected', 1, 0, params => { const {receiver, args, rubyBlock} = params; @@ -302,7 +322,7 @@ const MicrobitMoreConverter = { const index = TiltAngleDirectionMenuLower.indexOf(args[0].toString().toLowerCase()); if (index < 0) return null; - args[0] = new Primitive('str', TiltAngleDirectionMenu[index], args[0].node); + args[0] = new Primitive('str', TiltAngleDirectionMenuValue[index], args[0].node); } else { return null; } diff --git a/test/integration/ruby-tab/extension_microbit_more.test.js b/test/integration/ruby-tab/extension_microbit_more.test.js index ae326eed381..3bd0a06fe14 100644 --- a/test/integration/ruby-tab/extension_microbit_more.test.js +++ b/test/integration/ruby-tab/extension_microbit_more.test.js @@ -67,6 +67,9 @@ describe('Ruby Tab: Microbit More v2 extension blocks', () => { microbit_more.when("shake") do end + microbit_more.when("jumped") do + end + microbit_more.when("6G") do end @@ -79,6 +82,21 @@ describe('Ruby Tab: Microbit More v2 extension blocks', () => { microbit_more.when("tilted_front") do end + microbit_more.when_tilted("any") do + end + + microbit_more.when_tilted("front") do + end + + microbit_more.when_tilted("back") do + end + + microbit_more.when_tilted("left") do + end + + microbit_more.when_tilted("right") do + end + microbit_more.when("tilted_back") do end diff --git a/test/unit/lib/ruby-generator/microbit_more.test.js b/test/unit/lib/ruby-generator/microbit_more.test.js index 196040d98e6..7110c0cdf25 100644 --- a/test/unit/lib/ruby-generator/microbit_more.test.js +++ b/test/unit/lib/ruby-generator/microbit_more.test.js @@ -23,6 +23,19 @@ describe('RubyGenerator/MicrobitMore', () => { expect(RubyGenerator.microbitMore_whenPinConnected(block)).toEqual(expected); }); + test('microbitMore_whenTilted', () => { + const block = { + opcode: 'microbitMore_whenTilted', + fields: { + DIRECTION: { + value: 'ANY' + } + } + }; + const expected = 'microbit_more.when_tilted("any") do\n'; + expect(RubyGenerator.microbitMore_whenTilted(block)).toEqual(expected); + }); + test('microbitMore_isTilted', () => { const block = { opcode: 'microbitMore_isTilted', diff --git a/test/unit/lib/ruby-to-blocks-converter/microbit_more.test.js b/test/unit/lib/ruby-to-blocks-converter/microbit_more.test.js index 1ae0d879e3a..1dcddeb5a79 100644 --- a/test/unit/lib/ruby-to-blocks-converter/microbit_more.test.js +++ b/test/unit/lib/ruby-to-blocks-converter/microbit_more.test.js @@ -84,6 +84,22 @@ describe('RubyToBlocksConverter/MicrobitMore', () => { convertAndExpectToEqualBlocks(converter, target, code, expected); }); + test('microbit_more.when_tilted', () => { + code = 'microbit_more.when_tilted("any") do; end'; + expected = [ + { + opcode: 'microbitMore_whenTilted', + fields: [ + { + name: 'DIRECTION', + value: 'ANY' + } + ] + } + ]; + convertAndExpectToEqualBlocks(converter, target, code, expected); + }); + test('microbit_more.when(tilted_any)', () => { code = 'microbit_more.when("tilted_any") do; end'; expected = [