Skip to content

Commit 40870d7

Browse files
authored
Merge pull request #160 from smalruby/issues/134_event1
support converting Ruby to Event blocks. (refs #134)
2 parents 1bb62de + 9ce0bf7 commit 40870d7

File tree

4 files changed

+317
-51
lines changed

4 files changed

+317
-51
lines changed

src/lib/ruby-generator/event.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ export default function (Generator) {
99
return `${Generator.spriteName()}.when(:flag_clicked) do\n`;
1010
};
1111

12-
Generator.event_whenthisspriteclicked = function (block) {
13-
block.isStatement = true;
14-
return `${Generator.spriteName()}.when(:click) do\n`;
15-
};
16-
1712
Generator.event_whenkeypressed = function (block) {
1813
block.isStatement = true;
1914
const key = Generator.quote_(Generator.getFieldValue(block, 'KEY_OPTION') || null);
2015
return `${Generator.spriteName()}.when(:key_pressed, ${key}) do\n`;
2116
};
2217

18+
Generator.event_whenthisspriteclicked = function (block) {
19+
block.isStatement = true;
20+
return `${Generator.spriteName()}.when(:clicked) do\n`;
21+
};
22+
2323
Generator.event_whenbackdropswitchesto = function (block) {
2424
block.isStatement = true;
2525
const backdrop = Generator.quote_(Generator.getFieldValue(block, 'BACKDROP') || null);
@@ -30,7 +30,7 @@ export default function (Generator) {
3030
block.isStatement = true;
3131
const lh = Generator.quote_(Generator.getFieldValue(block, 'WHENGREATERTHANMENU') || null);
3232
const rh = Generator.valueToCode(block, 'VALUE', Generator.ORDER_NONE) || '0';
33-
return `${Generator.spriteName()}.when(:greater_than, ${lh}, ${rh}) do\n`;
33+
return `${Generator.spriteName()}.when(:greater_than, ${lh}, ${rh}) do\n`;
3434
};
3535

3636
Generator.event_whenbroadcastreceived = function (block) {

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

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,85 @@
11
/* global Opal */
22
import _ from 'lodash';
33

4+
const KeyOptions = [
5+
'space',
6+
'left arrow',
7+
'right arrow',
8+
'down arrow',
9+
'up arrow',
10+
'any',
11+
'a',
12+
'b',
13+
'c',
14+
'd',
15+
'e',
16+
'f',
17+
'g',
18+
'h',
19+
'i',
20+
'j',
21+
'k',
22+
'l',
23+
'm',
24+
'n',
25+
'o',
26+
'p',
27+
'q',
28+
'r',
29+
's',
30+
't',
31+
'u',
32+
'v',
33+
'w',
34+
'x',
35+
'y',
36+
'z',
37+
'0',
38+
'1',
39+
'2',
40+
'3',
41+
'4',
42+
'5',
43+
'6',
44+
'7',
45+
'8',
46+
'9'
47+
];
48+
449
/**
550
* Event converter
651
*/
752
const EventConverter = {
853
// eslint-disable-next-line no-unused-vars
954
onSend: function (receiver, name, args, rubyBlockArgs, rubyBlock) {
1055
let block;
11-
if (this._isSelf(receiver) || receiver === Opal.nil) {
12-
switch (name) {
13-
case 'when':
56+
if ((this._isSelf(receiver) || receiver === Opal.nil) &&
57+
name === 'when' &&
58+
args.length >= 1 && args[0].type === 'sym' &&
59+
rubyBlockArgs && rubyBlockArgs.length === 0 &&
60+
rubyBlock) {
61+
switch (args[0].value) {
62+
case 'flag_clicked':
63+
case 'clicked':
1464
if (args.length === 1) {
15-
if (args[0].type === 'sym' && args[0].value === 'flag_clicked' &&
16-
rubyBlockArgs && rubyBlockArgs.length === 0) {
17-
block = this._createBlock('event_whenflagclicked', 'hat');
18-
if (this._isBlock(rubyBlock)) {
19-
rubyBlock.parent = block.id;
20-
block.next = rubyBlock.id;
21-
}
65+
let opcode;
66+
switch (args[0].value) {
67+
case 'flag_clicked':
68+
opcode = 'event_whenflagclicked';
69+
break;
70+
case 'clicked':
71+
opcode = 'event_whenthisspriteclicked';
72+
break;
2273
}
74+
block = this._createBlock(opcode, 'hat');
75+
this._setParent(rubyBlock, block);
76+
}
77+
break;
78+
case 'key_pressed':
79+
if (args.length === 2 && this._isString(args[1]) && KeyOptions.indexOf(args[1].toString()) >= 0) {
80+
block = this._createBlock('event_whenkeypressed', 'hat');
81+
this._addField(block, 'KEY_OPTION', args[1]);
82+
this._setParent(rubyBlock, block);
2383
}
2484
break;
2585
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,13 @@ class RubyToBlocksConverter {
603603
return true;
604604
}
605605

606+
_setParent (block, parent) {
607+
if (this._isBlock(block)) {
608+
block.parent = parent.id;
609+
parent.next = block.id;
610+
}
611+
}
612+
606613
_matchRubyExpression (block, regexp) {
607614
if (!this._isBlock(block) || block.opcode !== 'ruby_expression') {
608615
return false;

0 commit comments

Comments
 (0)