@@ -28,64 +28,6 @@ goog.require('Blockly.Types');
2828/** Common HSV hue for all blocks in this category. */
2929Blockly . Blocks . stepper . HUE = 80 ;
3030
31- /** Strings for easy reference. */
32- Blockly . Blocks . stepper . noInstance = 'No_Instances' ;
33- Blockly . Blocks . stepper . noName = 'Empty_input_name' ;
34-
35- /**
36- * Finds all user-created instances of the Stepper block config.
37- * @return {!Array.<string> } Array of instance names.
38- */
39- Blockly . Blocks . stepper . stepperInstances = function ( ) {
40- var stepperList = [ ] ;
41- var blocks = Blockly . mainWorkspace . getTopBlocks ( ) ;
42- for ( var x = 0 ; x < blocks . length ; x ++ ) {
43- var getStepperSetupInstance = blocks [ x ] . getStepperSetupInstance ;
44- if ( getStepperSetupInstance ) {
45- var stepperInstance = getStepperSetupInstance . call ( blocks [ x ] ) ;
46- if ( stepperInstance ) {
47- stepperList . push ( stepperInstance ) ;
48- }
49- }
50- }
51- return stepperList ;
52- } ;
53-
54- /**
55- * Return a sorted list of instances names for set dropdown menu.
56- * @return {!Array.<string> } Array of stepper instances names.
57- */
58- Blockly . Blocks . stepper . stepperDropdownList = function ( ) {
59- var stepperList = Blockly . Blocks . stepper . stepperInstances ( ) ;
60- var options = [ ] ;
61- if ( stepperList . length > 0 ) {
62- stepperList . sort ( goog . string . caseInsensitiveCompare ) ;
63- // Variables are not language-specific, use the name as both the
64- // user-facing text and the internal representation.
65- for ( var x = 0 ; x < stepperList . length ; x ++ ) {
66- options [ x ] = [ stepperList [ x ] , stepperList [ x ] ] ;
67- }
68- } else {
69- // There are no config blocks in the work area
70- options [ 0 ] = [ Blockly . Blocks . stepper . noInstance ,
71- Blockly . Blocks . stepper . noInstance ] ;
72- }
73- return options ;
74- } ;
75-
76- /**
77- * Class for a variable's dropdown field.
78- * @extends {Blockly.FieldDropdown }
79- * @constructor
80- */
81- Blockly . Blocks . stepper . FieldStepperInstance = function ( ) {
82- Blockly . Blocks . stepper . FieldStepperInstance . superClass_ . constructor
83- . call ( this , Blockly . Blocks . stepper . stepperDropdownList ) ;
84- } ;
85- goog . inherits (
86- Blockly . Blocks . stepper . FieldStepperInstance , Blockly . FieldDropdown ) ;
87-
88-
8931Blockly . Blocks [ 'stepper_config' ] = {
9032 /**
9133 * Block for for the stepper generator configuration including creating
@@ -97,7 +39,8 @@ Blockly.Blocks['stepper_config'] = {
9739 this . setColour ( Blockly . Blocks . stepper . HUE ) ;
9840 this . appendDummyInput ( )
9941 . appendField ( Blockly . Msg . ARD_STEPPER_SETUP )
100- . appendField ( new Blockly . FieldTextInput ( 'MyStepper' ) , 'STEPPER_NAME' )
42+ . appendField ( new Blockly . Blocks . ComponentFieldVariable (
43+ Blockly . Msg . ARD_STEPPER_DEFAULT_NAME , 'Stepper' ) , 'STEPPER_NAME' )
10144 . appendField ( Blockly . Msg . ARD_STEPPER_MOTOR ) ;
10245 this . appendDummyInput ( )
10346 . setAlign ( Blockly . ALIGN_RIGHT )
@@ -118,18 +61,41 @@ Blockly.Blocks['stepper_config'] = {
11861 this . setTooltip ( Blockly . Msg . ARD_STEPPER_SETUP_TIP ) ;
11962 } ,
12063 /**
121- * Returns the stepper instance name, defined in the 'STEPPER_NAME' input
122- * String block attached to this block.
123- * @return {!string } List with the instance name.
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.
12474 * @this Blockly.Block
12575 */
126- getStepperSetupInstance : function ( ) {
127- var InstanceName = this . getFieldValue ( 'STEPPER_NAME' ) ;
128- if ( ! InstanceName ) {
129- InstanceName = Blockly . Blocks . stepper . noName ;
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' ) ;
13089 }
131- // Replace all spaces with underscores
132- return InstanceName . replace ( / / g, '_' ) ;
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 ;
13399 } ,
134100 /**
135101 * Updates the content of the the pin related fields.
@@ -153,8 +119,8 @@ Blockly.Blocks['stepper_step'] = {
153119 this . setColour ( Blockly . Blocks . stepper . HUE ) ;
154120 this . appendDummyInput ( )
155121 . appendField ( Blockly . Msg . ARD_STEPPER_STEP )
156- . appendField ( new Blockly . Blocks . stepper . FieldStepperInstance ( ) ,
157- 'STEPPER_NAME' ) ;
122+ . appendField ( new Blockly . Blocks . ComponentFieldVariable (
123+ Blockly . Msg . ARD_STEPPER_DEFAULT_NAME , 'Stepper' ) , 'STEPPER_NAME' )
158124 this . appendValueInput ( 'STEPPER_STEPS' )
159125 . setCheck ( Blockly . Types . NUMBER . checkList ) ;
160126 this . appendDummyInput ( )
@@ -163,6 +129,35 @@ Blockly.Blocks['stepper_step'] = {
163129 this . setNextStatement ( true ) ;
164130 this . setTooltip ( Blockly . Msg . ARD_STEPPER_STEP_TIP ) ;
165131 } ,
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+ } ,
166161 /**
167162 * Called whenever anything on the workspace changes.
168163 * It checks the instances of stepper_config and attaches a warning to this
@@ -173,46 +168,11 @@ Blockly.Blocks['stepper_step'] = {
173168 if ( ! this . workspace ) { return ; } // Block has been deleted.
174169
175170 var currentDropdown = this . getFieldValue ( 'STEPPER_NAME' ) ;
176- var instances = Blockly . Blocks . stepper . stepperDropdownList ( ) ;
177-
178- // Check for configuration block presence
179- if ( instances [ 0 ] [ 0 ] === Blockly . Blocks . stepper . noInstance ) {
180- // Ensure dropdown menu says there is no config block
181- if ( currentDropdown !== Blockly . Blocks . stepper . noInstance ) {
182- this . setFieldValue ( Blockly . Blocks . stepper . noInstance , 'STEPPER_NAME' ) ;
183- }
184- this . setWarningText ( Blockly . Msg . ARD_STEPPER_STEP_WARN1 ) ;
171+ if ( Blockly . Blocks . ComponentFieldVariable . CheckSetupPresent ( this . workspace , currentDropdown , 'Stepper' ) ) {
172+ this . setWarningText ( null ) ;
185173 } else {
186- // Configuration blocks present, check if any selected and contains name
187- var existingConfigSelected = false ;
188- for ( var x = 0 ; x < instances . length ; x ++ ) {
189- // Check if any of the config blocks does not have a name
190- if ( instances [ x ] [ 0 ] === Blockly . Blocks . stepper . noName ) {
191- // If selected config has no name either, set warning and exit func
192- if ( currentDropdown === Blockly . Blocks . stepper . noName ) {
193- this . setWarningText ( Blockly . Msg . ARD_STEPPER_STEP_WARN2 ) ;
194- return ;
195- }
196- } else if ( instances [ x ] [ 0 ] === currentDropdown ) {
197- existingConfigSelected = true ;
198- }
199- }
200-
201- // At this point select config has a name, check if it exist
202- if ( existingConfigSelected ) {
203- // All good, just remove any warnings and exit the function
204- this . setWarningText ( null ) ;
205- } else {
206- if ( ( currentDropdown === Blockly . Blocks . stepper . noName ) ||
207- ( currentDropdown === Blockly . Blocks . stepper . noInstance ) ) {
208- // Just pick the first config block
209- this . setFieldValue ( instances [ 0 ] [ 0 ] , 'STEPPER_NAME' ) ;
210- this . setWarningText ( null ) ;
211- } else {
212- // Al this point just set a warning to select a valid stepper config
213- this . setWarningText ( Blockly . Msg . ARD_STEPPER_STEP_WARN3 ) ;
214- }
215- }
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 ) ) ;
216176 }
217177 }
218178} ;
0 commit comments