Skip to content

Commit 3a8ad20

Browse files
committed
Merge branch 'sprite_new_with_block' into develop
2 parents fcdbb02 + 125667f commit 3a8ad20

File tree

2 files changed

+101
-117
lines changed

2 files changed

+101
-117
lines changed

src/lib/ruby-generator/index.js

Lines changed: 96 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ export default function (Blockly) {
102102
this.variableDB_ = new Blockly.Names(Blockly.Ruby.RESERVED_WORDS_);
103103
}
104104
}
105-
this.defineSprite(this.editingTarget);
106105
};
107106

108107
Blockly.Ruby.isString = function (s) {
@@ -139,106 +138,100 @@ export default function (Blockly) {
139138
return code;
140139
};
141140

142-
Blockly.Ruby.defineSprite = function (renderedTarget) {
141+
Blockly.Ruby.spriteNew = function (renderedTarget) {
143142
if (!renderedTarget) {
144143
return null;
145144
}
146-
const RenderedTarget = renderedTarget.constructor;
147-
const name = renderedTarget.sprite.name;
148-
const definitionsId = `sprite_${name}`;
149145

150-
if (!this.definitions_.hasOwnProperty(definitionsId)) {
151-
const attributes = {};
146+
const attributes = {};
147+
if (renderedTarget.x !== 0) {
148+
attributes.x = renderedTarget.x;
149+
}
150+
if (renderedTarget.y !== 0) {
151+
attributes.y = renderedTarget.y;
152+
}
153+
if (renderedTarget.direction !== 90) {
154+
attributes.direction = renderedTarget.direction;
155+
}
156+
if (!renderedTarget.visible) {
157+
attributes.visible = !!renderedTarget.visible;
158+
}
159+
if (renderedTarget.size !== 100) {
160+
attributes.size = renderedTarget.size;
161+
}
162+
if (renderedTarget.currentCostume > 1) {
163+
attributes.current_costume = renderedTarget.currentCostume - 1;
164+
}
165+
const costumes = renderedTarget.sprite.costumes;
166+
if (costumes.length > 0) {
167+
const s = costumes.map(i => {
168+
const h = {
169+
asset_id: this.quote_(i.assetId),
170+
name: this.quote_(i.name),
171+
bitmap_resolution: i.bitmapResolution,
172+
md5: this.quote_(i.md5),
173+
data_format: this.quote_(i.dataFormat),
174+
rotation_center_x: i.rotationCenterX,
175+
rotation_center_y: i.rotationCenterY
176+
};
177+
return this.hashToCode(h);
178+
}).join(',\n');
179+
attributes.costumes = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
180+
}
181+
const RenderedTarget = renderedTarget.constructor;
182+
switch (renderedTarget.rotationStyle) {
183+
case RenderedTarget.ROTATION_STYLE_LEFT_RIGHT:
184+
attributes.rotation_style = ':left_right';
185+
break;
186+
case RenderedTarget.ROTATION_STYLE_NONE:
187+
attributes.rotation_style = ':none';
188+
break;
189+
}
152190

153-
if (renderedTarget.x !== 0) {
154-
attributes.x = renderedTarget.x;
155-
}
156-
if (renderedTarget.y !== 0) {
157-
attributes.y = renderedTarget.y;
158-
}
159-
if (renderedTarget.direction !== 90) {
160-
attributes.direction = renderedTarget.direction;
161-
}
162-
if (!renderedTarget.visible) {
163-
attributes.visible = !!renderedTarget.visible;
164-
}
165-
if (renderedTarget.size !== 100) {
166-
attributes.size = renderedTarget.size;
167-
}
168-
if (renderedTarget.currentCostume > 1) {
169-
attributes.current_costume = renderedTarget.currentCostume - 1;
170-
}
171-
const costumes = renderedTarget.sprite.costumes;
172-
if (costumes.length > 0) {
173-
const s = costumes.map(i => {
174-
const h = {
175-
asset_id: this.quote_(i.assetId),
176-
name: this.quote_(i.name),
177-
bitmap_resolution: i.bitmapResolution,
178-
md5: this.quote_(i.md5),
179-
data_format: this.quote_(i.dataFormat),
180-
rotation_center_x: i.rotationCenterX,
181-
rotation_center_y: i.rotationCenterY
182-
};
183-
return this.hashToCode(h);
184-
}).join(',\n');
185-
attributes.costumes = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
186-
}
187-
switch (renderedTarget.rotationStyle) {
188-
case RenderedTarget.ROTATION_STYLE_LEFT_RIGHT:
189-
attributes.rotation_style = ':left_right';
191+
const variables = [];
192+
const lists = [];
193+
for (const id in renderedTarget.variables) {
194+
const v = renderedTarget.variables[id];
195+
switch (v.type) {
196+
case SCALAR_TYPE:
197+
variables.push(v);
190198
break;
191-
case RenderedTarget.ROTATION_STYLE_NONE:
192-
attributes.rotation_style = ':none';
199+
case LIST_TYPE:
200+
lists.push(v);
193201
break;
194202
}
195-
196-
const variables = [];
197-
const lists = [];
198-
for (const id in renderedTarget.variables) {
199-
const v = renderedTarget.variables[id];
200-
switch (v.type) {
201-
case SCALAR_TYPE:
202-
variables.push(v);
203-
break;
204-
case LIST_TYPE:
205-
lists.push(v);
206-
break;
203+
}
204+
if (variables.length > 0) {
205+
const s = variables.map(i => {
206+
const h = {
207+
name: this.quote_(i.name)
208+
};
209+
if (i.value !== 0) {
210+
h.value = this.scalarToCode(i.value);
207211
}
208-
}
209-
if (variables.length > 0) {
210-
const s = variables.map(i => {
211-
const h = {
212-
name: this.quote_(i.name)
213-
};
214-
if (i.value !== 0) {
215-
h.value = this.scalarToCode(i.value);
216-
}
217-
return this.hashToCode(h);
218-
}).join(',\n');
219-
attributes.variables = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
220-
}
221-
if (lists.length > 0) {
222-
const s = lists.map(i => {
223-
const h = {
224-
name: this.quote_(i.name)
225-
};
226-
if (i.value.length > 0) {
227-
h.value = this.listToCode(i.value);
228-
}
229-
return this.hashToCode(h);
230-
}).join(',\n');
231-
attributes.lists = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
232-
}
212+
return this.hashToCode(h);
213+
}).join(',\n');
214+
attributes.variables = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
215+
}
216+
if (lists.length > 0) {
217+
const s = lists.map(i => {
218+
const h = {
219+
name: this.quote_(i.name)
220+
};
221+
if (i.value.length > 0) {
222+
h.value = this.listToCode(i.value);
223+
}
224+
return this.hashToCode(h);
225+
}).join(',\n');
226+
attributes.lists = `[\n${this.prefixLines(s, this.INDENT)}\n]`;
227+
}
233228

234-
let code = this.hashToCode(attributes, ': ', false);
235-
if (code.length > 0) {
236-
code = `,\n${this.prefixLines(code, ' ')}`;
237-
}
238-
this.definitions_[definitionsId] =
239-
`Sprite.new(${this.quote_(name)}${code})`;
229+
let code = this.hashToCode(attributes, ': ', false);
230+
if (code.length > 0) {
231+
code = `,\n${this.prefixLines(code, ' ')}`;
240232
}
241-
return Blockly.Ruby.definitions_[definitionsId];
233+
const name = renderedTarget.sprite.name;
234+
return `Sprite.new(${this.quote_(name)}${code})`;
242235
};
243236

244237
Blockly.Ruby.characterStack = function () {
@@ -344,9 +337,8 @@ export default function (Blockly) {
344337
const requires = [];
345338
const prepares = [];
346339
const defs = [];
347-
let name;
348340

349-
for (name in Blockly.Ruby.definitions_) {
341+
for (const name in Blockly.Ruby.definitions_) {
350342
const def = this.definitions_[name];
351343
if (this.isString(def)) {
352344
if (name.match(/^require__/)) {
@@ -358,7 +350,11 @@ export default function (Blockly) {
358350
}
359351
}
360352
}
361-
if (requires.length === 0 && prepares.length === 0 && defs.length === 0 && code.length === 0) {
353+
if (!this.editingTarget &&
354+
requires.length === 0 &&
355+
prepares.length === 0 &&
356+
defs.length === 0 &&
357+
code.length === 0) {
362358
return '';
363359
}
364360
let allDefs = '';
@@ -371,6 +367,11 @@ export default function (Blockly) {
371367
if (defs.length > 0) {
372368
allDefs += `${defs.join('\n')}\n\n`;
373369
}
370+
371+
if (this.editingTarget) {
372+
code = `${this.spriteNew(this.editingTarget)} do\n${this.prefixLines(code, this.INDENT)}end`;
373+
}
374+
374375
return allDefs + code;
375376
};
376377

@@ -430,11 +431,8 @@ export default function (Blockly) {
430431
return commentCode + code + nextCode + eventEndCode;
431432
};
432433

433-
Blockly.Ruby.spriteName = function (renderedTarget) {
434-
if (!renderedTarget) {
435-
renderedTarget = this.editingTarget;
436-
}
437-
return `sprite(${this.quote_(renderedTarget.sprite.name)})`;
434+
Blockly.Ruby.spriteName = function () {
435+
return 'self';
438436
};
439437

440438
Blockly.Ruby.broadcastMessageName = function (name) {

test/unit/lib/ruby-generator.test.jsx

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,13 @@ describe('RubyGenerator', () => {
3535
});
3636

3737
describe('spriteName', () => {
38-
test('return sprite("name of sprite")', () => {
38+
test('return self', () => {
3939
Ruby.editingTarget = {
4040
sprite: {
4141
name: 'Sprite1'
4242
}
4343
};
44-
expect(Ruby.spriteName()).toEqual('sprite("Sprite1")');
45-
});
46-
47-
test('"name of sprite" is escaped', () => {
48-
Ruby.editingTarget = {
49-
sprite: {
50-
name: '"Sprite1"'
51-
}
52-
};
53-
expect(Ruby.spriteName()).toEqual('sprite("\\"Sprite1\\"")');
44+
expect(Ruby.spriteName()).toEqual('self');
5445
});
5546
});
5647

@@ -137,7 +128,7 @@ describe('RubyGenerator', () => {
137128
});
138129
});
139130

140-
describe('defineSprite', () => {
131+
describe('spriteNew', () => {
141132
const spriteName = 'Sprite1';
142133
let renderedTarget;
143134

@@ -267,12 +258,7 @@ describe('RubyGenerator', () => {
267258
value: ["a", "b", "c"]
268259
}
269260
])`;
270-
expect(Ruby.defineSprite(renderedTarget)).toEqual(expected);
271-
});
272-
273-
test('add definitions_ the Sprite.new code', () => {
274-
const code = Ruby.defineSprite(renderedTarget);
275-
expect(Ruby.definitions_[`sprite_${spriteName}`]).toEqual(code);
261+
expect(Ruby.spriteNew(renderedTarget)).toEqual(expected);
276262
});
277263

278264
test('suppress default attributes', () => {
@@ -288,7 +274,7 @@ describe('RubyGenerator', () => {
288274
});
289275
renderedTarget.sprite.costumes = [];
290276
const expected = `Sprite.new(${Ruby.quote_(spriteName)})`;
291-
expect(Ruby.defineSprite(renderedTarget)).toEqual(expected);
277+
expect(Ruby.spriteNew(renderedTarget)).toEqual(expected);
292278
});
293279
});
294280
});

0 commit comments

Comments
 (0)