77 * @fileoverview Blocks for Arduino Stepper library.
88 * The Arduino Servo functions syntax can be found in the following URL:
99 * http://arduino.cc/en/Reference/Stepper
10- * Additional functions apart from the normal generators have been added to
11- * be able to generate the 'set' drop down menu with all current instances
12- * of the Stepper class:
13- * Blockly.Blocks.stepper.stepperInstances
14- * Blockly.Blocks.stepper.FieldStepperInstance
15- * Blockly.Blocks.stepper.stepperDropdownList
16- *
17- * TODO: Still need to had some kind of handler to refresh the "set" drop down
18- * menu values if an instance in a 'configure' block is renamed.
10+ * Note that this block uses the Blockly.FieldInstance instead of
11+ * Blockly.FieldDropdown which generates a unique instance per setup block
12+ * in the workspace.
1913 */
2014'use strict' ;
2115
@@ -39,8 +33,11 @@ Blockly.Blocks['stepper_config'] = {
3933 this . setColour ( Blockly . Blocks . stepper . HUE ) ;
4034 this . appendDummyInput ( )
4135 . appendField ( Blockly . Msg . ARD_STEPPER_SETUP )
42- . appendField ( new Blockly . Blocks . ComponentFieldVariable (
43- Blockly . Msg . ARD_STEPPER_DEFAULT_NAME , 'Stepper' ) , 'STEPPER_NAME' )
36+ . appendField (
37+ new Blockly . FieldInstance ( 'Stepper' ,
38+ Blockly . Msg . ARD_STEPPER_DEFAULT_NAME ,
39+ true , true , false ) ,
40+ 'STEPPER_NAME' )
4441 . appendField ( Blockly . Msg . ARD_STEPPER_MOTOR ) ;
4542 this . appendDummyInput ( )
4643 . setAlign ( Blockly . ALIGN_RIGHT )
@@ -60,43 +57,6 @@ Blockly.Blocks['stepper_config'] = {
6057 . appendField ( Blockly . Msg . ARD_STEPPER_SPEED ) ;
6158 this . setTooltip ( Blockly . Msg . ARD_STEPPER_SETUP_TIP ) ;
6259 } ,
63- /**
64- * Return the name of the component defined in this block
65- * @return {!<string> } The name of the component
66- * @this Blockly.Block
67- */
68- getComponentName : function ( ) {
69- return 'Stepper' ;
70- } ,
71- /**
72- * Return all variables referenced by this block.
73- * @return {!Array.<string> } List of variable names.
74- * @this Blockly.Block
75- */
76- getVars : function ( ) {
77- return [ this . getFieldValue ( 'STEPPER_NAME' ) ] ;
78- } ,
79- /**
80- * Notification that a variable is renaming.
81- * If the name matches one of this block's variables, rename it.
82- * @param {string } oldName Previous name of variable.
83- * @param {string } newName Renamed variable.
84- * @this Blockly.Block
85- */
86- renameVar : function ( oldName , newName ) {
87- if ( Blockly . Names . equals ( oldName , this . getFieldValue ( 'STEPPER_NAME' ) ) ) {
88- this . setFieldValue ( newName , 'STEPPER_NAME' ) ;
89- }
90- } ,
91- /**
92- * Gets the variable type required.
93- * @param {!string } varName Name of the variable selected in this block to
94- * check.
95- * @return {string } String to indicate the variable type.
96- */
97- getVarType : function ( varName ) {
98- return Blockly . Types . ARRAY ;
99- } ,
10060 /**
10161 * Updates the content of the the pin related fields.
10262 * @this Blockly.Block
@@ -119,8 +79,11 @@ Blockly.Blocks['stepper_step'] = {
11979 this . setColour ( Blockly . Blocks . stepper . HUE ) ;
12080 this . appendDummyInput ( )
12181 . appendField ( Blockly . Msg . ARD_STEPPER_STEP )
122- . appendField ( new Blockly . Blocks . ComponentFieldVariable (
123- Blockly . Msg . ARD_STEPPER_DEFAULT_NAME , 'Stepper' ) , 'STEPPER_NAME' )
82+ . appendField (
83+ new Blockly . FieldInstance ( 'Stepper' ,
84+ Blockly . Msg . ARD_STEPPER_DEFAULT_NAME ,
85+ false , true , false ) ,
86+ 'STEPPER_NAME' ) ;
12487 this . appendValueInput ( 'STEPPER_STEPS' )
12588 . setCheck ( Blockly . Types . NUMBER . checkList ) ;
12689 this . appendDummyInput ( )
@@ -129,50 +92,23 @@ Blockly.Blocks['stepper_step'] = {
12992 this . setNextStatement ( true ) ;
13093 this . setTooltip ( Blockly . Msg . ARD_STEPPER_STEP_TIP ) ;
13194 } ,
132- /**
133- * Return all variables referenced by this block.
134- * @return {!Array.<string> } List of variable names.
135- * @this Blockly.Block
136- */
137- getVars : function ( ) {
138- return [ this . getFieldValue ( 'STEPPER_NAME' ) ] ;
139- } ,
140- /**
141- * Notification that a variable is renaming.
142- * If the name matches one of this block's variables, rename it.
143- * @param {string } oldName Previous name of variable.
144- * @param {string } newName Renamed variable.
145- * @this Blockly.Block
146- */
147- renameVar : function ( oldName , newName ) {
148- if ( Blockly . Names . equals ( oldName , this . getFieldValue ( 'STEPPER_NAME' ) ) ) {
149- this . setFieldValue ( newName , 'STEPPER_NAME' ) ;
150- }
151- } ,
152- /**
153- * Gets the variable type required.
154- * @param {!string } varName Name of the variable selected in this block to
155- * check.
156- * @return {string } String to indicate the variable type.
157- */
158- getVarType : function ( varName ) {
159- return Blockly . Types . ARRAY ;
160- } ,
16195 /**
16296 * Called whenever anything on the workspace changes.
163- * It checks the instances of stepper_config and attaches a warning to this
164- * block if not valid data is found.
97+ * It checks/warns if the selected stepper instance has a config block.
16598 * @this Blockly.Block
16699 */
167100 onchange : function ( ) {
168- if ( ! this . workspace ) { return ; } // Block has been deleted.
101+ if ( ! this . workspace ) return ; // Block has been deleted.
169102
170- var currentDropdown = this . getFieldValue ( 'STEPPER_NAME' ) ;
171- if ( Blockly . Blocks . ComponentFieldVariable . CheckSetupPresent ( this . workspace , currentDropdown , 'Stepper' ) ) {
103+ var instanceName = this . getFieldValue ( 'STEPPER_NAME' )
104+ if ( Blockly . Instances . isInstancePresent ( instanceName , 'Stepper' , this ) ) {
172105 this . setWarningText ( null ) ;
173106 } else {
174- // Set a warning to select a valid stepper config
175- this . setWarningText ( Blockly . Msg . ARD_COMPONENT_WARN1 . replace ( '%1' , Blockly . Msg . ARD_STEPPER_COMPONENT ) . replace ( '%1' , Blockly . Msg . ARD_STEPPER_COMPONENT ) ) ;
107+ // Set a warning to select a valid stepper config block
108+ this . setWarningText (
109+ Blockly . Msg . ARD_COMPONENT_WARN1 . replace (
110+ '%1' , Blockly . Msg . ARD_STEPPER_COMPONENT ) . replace (
111+ '%2' , instanceName ) ) ;
176112 }
177113 }
178114} ;
0 commit comments