Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": "0.2",
"language": "en",
"words": [
"FREEFALL",
"gapi",
"googleusercontent",
"Hira",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/containers/ruby-tab/microbit-more-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[どれかの向き▼] に傾いた",
Expand Down
48 changes: 28 additions & 20 deletions src/lib/ruby-generator/microbit_more.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,46 @@ 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`;
};

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];
};

Expand Down
46 changes: 33 additions & 13 deletions src/lib/ruby-to-blocks-converter/microbit_more.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const GestureMenu = {
G6: '6G',
G8: '8G',
SHAKE: 'shake',
JUMPED: 'jumped',
MOVED: 'moved',
TILTED: 'tilted_any'
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions test/integration/ruby-tab/extension_microbit_more.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions test/unit/lib/ruby-generator/microbit_more.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions test/unit/lib/ruby-to-blocks-converter/microbit_more.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading