Skip to content

Commit ba53185

Browse files
feat: add blocks with text fields (#112)
1 parent 3982e75 commit ba53185

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/blocks/p5_blocks.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,57 @@ const simpleCircle = {
216216
'inputsInline': true,
217217
};
218218

219+
const writeTextWithoutShadow = {
220+
'type': 'write_text_without_shadow',
221+
'tooltip': '',
222+
'helpUrl': '',
223+
'message0': 'write without shadow %1',
224+
'args0': [
225+
{
226+
'type': 'field_input',
227+
'name': 'TEXT',
228+
'text': 'bit'
229+
},
230+
],
231+
'previousStatement': null,
232+
'nextStatement': null,
233+
'colour': 225
234+
};
235+
236+
const writeTextWithShadow = {
237+
'type': 'write_text_with_shadow',
238+
'tooltip': '',
239+
'helpUrl': '',
240+
'message0': 'write with shadow %1',
241+
'args0': [
242+
{
243+
'type': 'input_value',
244+
'name': 'TEXT',
245+
'check': 'String'
246+
}
247+
],
248+
'previousStatement': null,
249+
'nextStatement': null,
250+
'colour': 225
251+
};
252+
253+
const textBlock =
254+
{
255+
'type': 'text_only',
256+
'tooltip': '',
257+
'helpUrl': '',
258+
'message0': '%1',
259+
'args0': [
260+
{
261+
'type': 'field_input',
262+
'name': 'TEXT',
263+
'text': 'micro'
264+
},
265+
],
266+
'output': 'String',
267+
'colour': 225
268+
};
269+
219270
// Create the block definitions for all the JSON-only blocks.
220271
// This does not register their definitions with Blockly.
221272
const jsonBlocks = Blockly.common.createBlockDefinitionsFromJsonArray([
@@ -225,6 +276,9 @@ const jsonBlocks = Blockly.common.createBlockDefinitionsFromJsonArray([
225276
ellipse,
226277
draw_emoji,
227278
simpleCircle,
279+
writeTextWithoutShadow,
280+
writeTextWithShadow,
281+
textBlock
228282
]);
229283

230284
export const blocks = {

src/blocks/p5_generators.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,26 @@ sketch.stroke(${color});
8080
sketch.ellipse(150, 150, 50, 50);`;
8181
return code;
8282
};
83+
84+
forBlock['text_only'] = function (block, generator) {
85+
const code = generator.quote_(block.getFieldValue('TEXT'));
86+
return [code, Order.ATOMIC];
87+
};
88+
89+
forBlock['write_text_with_shadow'] = function (block, generator) {
90+
const text = generator.valueToCode(block, 'TEXT', Order.ATOMIC) || `''`;
91+
const code = `\nsketch.stroke(0x000000);
92+
sketch.fill(0x000000);
93+
sketch.textSize(100);
94+
sketch.text(${text}, 50, 350);\n`;
95+
return code;
96+
};
97+
98+
forBlock['write_text_without_shadow'] = function (block, generator) {
99+
const text = generator.quote_(block.getFieldValue('TEXT'));
100+
const code = `\nsketch.stroke(0x000000);
101+
sketch.fill(0x000000);
102+
sketch.textSize(100);
103+
sketch.text(${text}, 50, 350);\n`;
104+
return code;
105+
};

src/blocks/toolbox.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,20 @@ export const toolbox = {
3737
},
3838
},
3939
},
40+
{
41+
kind: 'block',
42+
type: 'write_text_with_shadow',
43+
inputs: {
44+
TEXT: {
45+
shadow: {
46+
type: 'text_only',
47+
},
48+
},
49+
},
50+
},
51+
{
52+
kind: 'block',
53+
type: 'write_text_without_shadow',
54+
},
4055
],
4156
};

0 commit comments

Comments
 (0)