Skip to content

Commit bf2cd59

Browse files
committed
Added custom rotation angle block for Sphero SPRK+ implementation.
1 parent b32074b commit bf2cd59

File tree

12 files changed

+153
-18
lines changed

12 files changed

+153
-18
lines changed

build/externs/blockly.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ Blockly.Events.UI;
7373
/** @type {Function} */
7474
Blockly.Events.disableOrphans = function() {};
7575

76+
77+
/** @type {Function} */
78+
Blockly.FieldAngle = function() {};
79+
80+
/** @type {number} */
81+
Blockly.FieldAngle.CLOCKWISE = 1;
82+
83+
/** @type {number} */
84+
Blockly.FieldAngle.OFFSET = 90;
85+
86+
7687
/**
7788
* @param {Blockly.Workspace} workspace Workspace to generate code from.
7889
* @return {string} Generated code.

src/blocks/blocks.soy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183

184184
<category name="{msg desc="Math Blocks"}Math{/msg}" colour="230">
185185
<block type="math_number"></block>
186+
<block type="general_input_angle"></block>
186187
<block type="math_arithmetic">
187188
<value name="A">
188189
<shadow type="math_number">

src/blocks/general/blocks.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ Blockly.Blocks['general_infinity_loop'] = {
3737
};
3838

3939

40+
/**
41+
* Input angle block.
42+
*/
43+
Blockly.Blocks['general_input_angle'] = {
44+
init: function() {
45+
this.setHelpUrl('');
46+
this.setColour(230);
47+
this.appendDummyInput()
48+
.appendField(new Blockly.FieldAngle(0), 'angle');
49+
this.setOutput(true, 'Number');
50+
this.setTooltip('');
51+
},
52+
};
53+
54+
4055
/**
4156
* Library file
4257
*/

src/blocks/general/blocks_helper.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ Blockly.BlocksHelper['validate_text'] = function(text) {
2929
.replace(/'/g, '')
3030
.replace(/\\/g, '');
3131
};
32+
33+
34+
/**
35+
* @param {number|string} value
36+
* @return {number}
37+
*/
38+
Blockly.BlocksHelper['validate_number'] = function(value) {
39+
return Number(value.replace(/[^\d.-]/g, ''));
40+
};

src/blocks/general/javascript.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ Blockly.JavaScript['general_infinity_loop'] = function(block) {
3333
};
3434

3535

36+
/**
37+
* Input angle block.
38+
* @param {Blockly.Block} block
39+
* @return {string}
40+
*/
41+
Blockly.JavaScript['general_input_angle'] = function(block) {
42+
let value_angle = block.getFieldValue('angle');
43+
let code = value_angle || 0;
44+
return [code, Blockly.JavaScript.ORDER_NONE];
45+
};
46+
47+
3648
/**
3749
* Library file
3850
* @param {Blockly.Block} block

src/blocks/sphero/sprk_plus/blocks.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ Blockly.Blocks['sphero_sprk_plus_roll_step'] = {
6464
this.appendValueInput('speed').setCheck('Number')
6565
.appendField(Blockly.BlocksTemplate.point())
6666
.appendField(i18t('roll with'));
67-
this.appendDummyInput('heading')
68-
.appendField(i18t('speed and'))
69-
.appendField(new Blockly.FieldAngle(0), 'heading')
67+
this.appendValueInput('heading')
68+
.appendField(i18t('speed and'));
69+
this.appendDummyInput()
7070
.appendField(i18t('heading'));
7171
this.setPreviousStatement(true);
7272
this.setNextStatement(true);
@@ -87,9 +87,9 @@ Blockly.Blocks['sphero_sprk_plus_roll_time'] = {
8787
.appendField(i18t('roll for'));
8888
this.appendValueInput('speed').setCheck('Number')
8989
.appendField(i18t('sec with'));
90-
this.appendDummyInput('heading')
91-
.appendField(i18t('speed and'))
92-
.appendField(new Blockly.FieldAngle(0), 'heading')
90+
this.appendValueInput('heading')
91+
.appendField(i18t('speed and'));
92+
this.appendDummyInput()
9393
.appendField(i18t('heading'));
9494
this.setPreviousStatement(true);
9595
this.setNextStatement(true);
@@ -177,8 +177,8 @@ Blockly.Blocks['sphero_sprk_plus_heading'] = {
177177
this.appendValueInput('heading')
178178
.setCheck('Number')
179179
.appendField(Blockly.BlocksTemplate.point())
180-
.appendField(i18t('set heading('))
181-
.appendField(new Blockly.FieldAngle(0), 'heading')
180+
.appendField(i18t('set heading('));
181+
this.appendDummyInput()
182182
.appendField(')');
183183
this.setPreviousStatement(true);
184184
this.setNextStatement(true);

src/blocks/sphero/sprk_plus/javascript.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ Blockly.JavaScript['sphero_sprk_plus_roll'] = function(block) {
5050
Blockly.JavaScript['sphero_sprk_plus_roll_step'] = function(block) {
5151
let speed = parseInt(Blockly.JavaScript.valueToCode(
5252
block, 'speed', Blockly.JavaScript.ORDER_ATOMIC));
53-
let heading = parseInt(block.getFieldValue('heading'));
53+
let heading = Blockly.JavaScript.valueToCode(
54+
block, 'heading', Blockly.JavaScript.ORDER_ATOMIC);
5455
let duration = 500 + (speed * 20);
55-
return 'sphero.roll(' + speed + ', ' +heading + ', 0x01, ' +
56+
return 'sphero.roll(' + speed + ', ' + heading + ', 0x01, ' +
5657
duration + ');\n';
5758
};
5859

@@ -67,9 +68,10 @@ Blockly.JavaScript['sphero_sprk_plus_roll_time'] = function(block) {
6768
block, 'time', Blockly.JavaScript.ORDER_ATOMIC));
6869
let speed = parseInt(Blockly.JavaScript.valueToCode(
6970
block, 'speed', Blockly.JavaScript.ORDER_ATOMIC));
70-
let heading = parseInt(block.getFieldValue('heading') || 0);
71+
let heading = Blockly.JavaScript.valueToCode(
72+
block, 'heading', Blockly.JavaScript.ORDER_ATOMIC);
7173
return 'sphero.rollTime(' + time + ', ' + speed + ', ' + heading +
72-
',true);\n';
74+
', true);\n';
7375
};
7476

7577

@@ -90,12 +92,10 @@ Blockly.JavaScript['sphero_sprk_plus_stabilization'] = function(block) {
9092
* @return {string}
9193
*/
9294
Blockly.JavaScript['sphero_sprk_plus_heading'] = function(block) {
93-
let angle_heading = parseInt(block.getFieldValue('heading'));
94-
let value_heading = parseInt(Blockly.JavaScript.valueToCode(
95-
block, 'heading', Blockly.JavaScript.ORDER_ATOMIC));
9695
let duration = 500;
97-
return 'sphero.roll(0, ' + (angle_heading || value_heading || 0) +
98-
', 0x01, ' + duration + ');\n';
96+
let heading = Blockly.JavaScript.valueToCode(
97+
block, 'heading', Blockly.JavaScript.ORDER_ATOMIC);
98+
return 'sphero.roll(0, ' + heading + ', 0x01, ' + duration + ');\n';
9999
};
100100

101101

src/blocks/sphero/sprk_plus/toolbox.soy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
<block type="math_number"><field name="NUM">40</field></block>
3333
</value>
3434
</block>
35-
<block type="sphero_sprk_plus_heading"></block>
35+
<block type="sphero_sprk_plus_heading">
36+
<value name="heading">
37+
<block type="general_input_angle"></block>
38+
</value>
39+
</block>
3640
<block type="sphero_sprk_plus_speed"></block>
3741
<block type="sphero_sprk_plus_stop"></block>
3842
</category>
@@ -42,6 +46,9 @@
4246
<value name="speed">
4347
<block type="math_number"><field name="NUM">40</field></block>
4448
</value>
49+
<value name="heading">
50+
<block type="general_input_angle"></block>
51+
</value>
4552
</block>
4653
<block type="sphero_sprk_plus_roll_time">
4754
<value name="time">
@@ -50,6 +57,9 @@
5057
<value name="speed">
5158
<block type="math_number"><field name="NUM">40</field></block>
5259
</value>
60+
<value name="heading">
61+
<block type="general_input_angle"></block>
62+
</value>
5363
</block>
5464
<block type="sphero_sprk_plus_stabilization"></block>
5565

src/ui/blockly/blockly.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ cwc.ui.Blockly.prototype.decorate = function(node, options = this.options_) {
178178
Blockly.HSV_SATURATION = 0.5;
179179
Blockly.HSV_VALUE = 0.7;
180180

181+
// Angle Field definition
182+
Blockly.FieldAngle.CLOCKWISE = 1;
183+
Blockly.FieldAngle.OFFSET = 90;
184+
181185
// Loading user defined settings.
182186
let userConfigInstance = this.helper.getInstance('userConfig');
183187
if (userConfigInstance) {
@@ -527,6 +531,7 @@ cwc.ui.Blockly.prototype.setToolboxFiles = function(files) {
527531
this.toolbox.setFiles(files);
528532
};
529533

534+
530535
/**
531536
* Cleans up the event listener and any other modification.
532537
* @private

src/ui/select_screen/normal/robot/sphero/sprk_plus.soy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
{param opt_icon: 'insert_drive_file' /}
5353
{/call}
5454

55+
{call cwc.soy.SelectScreenTemplate.fileCard data="all"}
56+
{param title: 'Rectangle Example' /}
57+
{param text: 'Try a Sphero example - rectangle' /}
58+
{param file: 'sphero/sprk_plus/blocks/SpheroSPRKplus-rectangle.cwc' /}
59+
{param opt_color_class: 'bg-blue' /}
60+
{param opt_icon: 'perm_scan_wifi' /}
61+
{/call}
62+
5563
{call cwc.soy.SelectScreenTemplate.fileCard data="all"}
5664
{param title: 'Collision Example' /}
5765
{param text: 'Try a Sphero example - change color on collision' /}

0 commit comments

Comments
 (0)