From e586dc7bcbee0f7d38805ddbbf6da3d28272ed52 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Fri, 29 Apr 2016 10:24:32 +0200 Subject: [PATCH 01/31] a componentfieldvariable dropdown field that allows to filter the variables seen in the dropdown Conflicts: blockly/blockly4Arduino/index.html blockly/blockly4Arduino/index_en.html --- blockly/blocks/component_variable.js | 127 +++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 blockly/blocks/component_variable.js diff --git a/blockly/blocks/component_variable.js b/blockly/blocks/component_variable.js new file mode 100644 index 0000000000..3a99175f3c --- /dev/null +++ b/blockly/blocks/component_variable.js @@ -0,0 +1,127 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2012 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Variable input field of a specific component type. + * @author fraser@google.com (Neil Fraser) + */ +'use strict'; + +goog.provide('Blockly.Blocks.ComponentFieldVariable'); + +goog.require('Blockly.Blocks'); +//goog.require('Blockly.FieldVariable'); + + +/** + * Class for a variable's dropdown field. + * @param {?string} varname The default name for the variable. If null, + * a unique variable name will be generated. + * @param {Function=} opt_validator A function that is executed when a new + * option is selected. Its sole argument is the new option value. + * @extends {Blockly.FieldVariable} + * @constructor + */ +Blockly.Blocks.ComponentFieldVariable = function(varname, component_type, opt_validator) { + Blockly.Blocks.ComponentFieldVariable['component_type'] = component_type + //override the dropdownCreate to use for this field + Blockly.FieldVariable.dropdownCreate = Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents; + Blockly.Blocks.ComponentFieldVariable.superClass_.constructor.call(this, + Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents, opt_validator); + this.setValue(varname || ''); +}; +goog.inherits(Blockly.Blocks.ComponentFieldVariable, Blockly.FieldVariable); + +/** + * Install this dropdown on a block. + * @param {!Blockly.Block} block The block containing this text. + */ +Blockly.Blocks.ComponentFieldVariable.prototype.init = function(block) { + if (this.sourceBlock_) { + // Dropdown has already been initialized once. + return; + } + Blockly.Blocks.ComponentFieldVariable.superClass_.init.call(this, block); +}; + +Blockly.Blocks.ComponentFieldVariable.ComponentVariables = function(root, component_type) { + var blocks; + if (root.getDescendants) { + // Root is Block. + blocks = root.getDescendants(); + } else if (root.getAllBlocks) { + // Root is Workspace. + blocks = root.getAllBlocks(); + } else { + throw 'Not Block or Workspace: ' + root; + } + var variableHash = Object.create(null); + // Iterate through every block and add each variable to the hash. + for (var x = 0; x < blocks.length; x++) { + if (blocks[x].getComponentName && blocks[x].getVars && + (blocks[x].getComponentName() == component_type)) { + var blockVariables = blocks[x].getVars(); + for (var y = 0; y < blockVariables.length; y++) { + var varName = blockVariables[y]; + // Variable name may be null if the block is only half-built. + if (varName) { + variableHash[varName.toLowerCase()] = varName; + } + } + } + } + // Flatten the hash into a list. + var variableList = []; + for (var name in variableHash) { + variableList.push(variableHash[name]); + } + return variableList; +}; + +/** + * Return a sorted list of variable names for variable dropdown menus. + * Include a special option at the end for creating a new variable name. + * @return {!Array.} Array of variable names. + * @this {!Blockly.FieldVariable} + */ +Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents = function() { + if (this.sourceBlock_ && this.sourceBlock_.workspace) { + var variableList = + Blockly.Blocks.ComponentFieldVariable.ComponentVariables(this.sourceBlock_.workspace, + Blockly.Blocks.ComponentFieldVariable['component_type']); + } else { + var variableList = []; + } + // Ensure that the currently selected variable is an option. + var name = this.getText(); + if (name && variableList.indexOf(name) == -1) { + variableList.push(name); + } + variableList.sort(goog.string.caseInsensitiveCompare); + variableList.push(Blockly.Msg.RENAME_VARIABLE); + variableList.push(Blockly.Msg.NEW_VARIABLE); + // Variables are not language-specific, use the name as both the user-facing + // text and the internal representation. + var options = []; + for (var x = 0; x < variableList.length; x++) { + options[x] = [variableList[x], variableList[x]]; + } + return options; +}; From bb59cbaf465be9ef313f1a1571a52695ed154df5 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Fri, 29 Apr 2016 10:32:10 +0200 Subject: [PATCH 02/31] convert stepper to new componentfield Step 1: the block Todo: onchange for errors in block & generator --- blockly/blocks/arduino/stepper.js | 177 +++++++++----------------- blockly/generators/arduino/stepper.js | 2 +- blockly/msg/json/nl.json | 5 +- blockly/msg/messages.js | 5 +- 4 files changed, 67 insertions(+), 122 deletions(-) diff --git a/blockly/blocks/arduino/stepper.js b/blockly/blocks/arduino/stepper.js index b7248689c1..40707fb5f6 100644 --- a/blockly/blocks/arduino/stepper.js +++ b/blockly/blocks/arduino/stepper.js @@ -28,64 +28,6 @@ goog.require('Blockly.Types'); /** Common HSV hue for all blocks in this category. */ Blockly.Blocks.stepper.HUE = 80; -/** Strings for easy reference. */ -Blockly.Blocks.stepper.noInstance = 'No_Instances'; -Blockly.Blocks.stepper.noName = 'Empty_input_name'; - -/** - * Finds all user-created instances of the Stepper block config. - * @return {!Array.} Array of instance names. - */ -Blockly.Blocks.stepper.stepperInstances = function() { - var stepperList = []; - var blocks = Blockly.mainWorkspace.getTopBlocks(); - for (var x = 0; x < blocks.length; x++) { - var getStepperSetupInstance = blocks[x].getStepperSetupInstance; - if (getStepperSetupInstance) { - var stepperInstance = getStepperSetupInstance.call(blocks[x]); - if (stepperInstance) { - stepperList.push(stepperInstance); - } - } - } - return stepperList; -}; - -/** - * Return a sorted list of instances names for set dropdown menu. - * @return {!Array.} Array of stepper instances names. - */ -Blockly.Blocks.stepper.stepperDropdownList = function() { - var stepperList = Blockly.Blocks.stepper.stepperInstances(); - var options = []; - if (stepperList.length > 0) { - stepperList.sort(goog.string.caseInsensitiveCompare); - // Variables are not language-specific, use the name as both the - // user-facing text and the internal representation. - for (var x = 0; x < stepperList.length; x++) { - options[x] = [stepperList[x], stepperList[x]]; - } - } else { - // There are no config blocks in the work area - options[0] = [Blockly.Blocks.stepper.noInstance, - Blockly.Blocks.stepper.noInstance]; - } - return options; -}; - -/** - * Class for a variable's dropdown field. - * @extends {Blockly.FieldDropdown} - * @constructor - */ -Blockly.Blocks.stepper.FieldStepperInstance = function() { - Blockly.Blocks.stepper.FieldStepperInstance.superClass_.constructor - .call(this, Blockly.Blocks.stepper.stepperDropdownList); -}; -goog.inherits( - Blockly.Blocks.stepper.FieldStepperInstance, Blockly.FieldDropdown); - - Blockly.Blocks['stepper_config'] = { /** * Block for for the stepper generator configuration including creating @@ -97,7 +39,8 @@ Blockly.Blocks['stepper_config'] = { this.setColour(Blockly.Blocks.stepper.HUE); this.appendDummyInput() .appendField(Blockly.Msg.ARD_STEPPER_SETUP) - .appendField(new Blockly.FieldTextInput('MyStepper'), 'STEPPER_NAME') + .appendField(new Blockly.Blocks.ComponentFieldVariable( + Blockly.Msg.ARD_STEPPER_DEFAULT_NAME, 'Stepper'), 'STEPPER_NAME') .appendField(Blockly.Msg.ARD_STEPPER_MOTOR); this.appendDummyInput() .setAlign(Blockly.ALIGN_RIGHT) @@ -118,18 +61,41 @@ Blockly.Blocks['stepper_config'] = { this.setTooltip(Blockly.Msg.ARD_STEPPER_SETUP_TIP); }, /** - * Returns the stepper instance name, defined in the 'STEPPER_NAME' input - * String block attached to this block. - * @return {!string} List with the instance name. + * Return the name of the component defined in this block + * @return {!} The name of the component * @this Blockly.Block */ - getStepperSetupInstance: function() { - var InstanceName = this.getFieldValue('STEPPER_NAME'); - if (!InstanceName) { - InstanceName = Blockly.Blocks.stepper.noName; + getComponentName: function() { + return 'Stepper'; + }, + /** + * Return all variables referenced by this block. + * @return {!Array.} List of variable names. + * @this Blockly.Block + */ + getVars: function() { + return [this.getFieldValue('STEPPER_NAME')]; + }, + /** + * Notification that a variable is renaming. + * If the name matches one of this block's variables, rename it. + * @param {string} oldName Previous name of variable. + * @param {string} newName Renamed variable. + * @this Blockly.Block + */ + renameVar: function(oldName, newName) { + if (Blockly.Names.equals(oldName, this.getFieldValue('STEPPER_NAME'))) { + this.setFieldValue(newName, 'STEPPER_NAME'); } - // Replace all spaces with underscores - return InstanceName.replace(/ /g, '_'); + }, + /** + * Gets the variable type required. + * @param {!string} varName Name of the variable selected in this block to + * check. + * @return {string} String to indicate the variable type. + */ + getVarType: function(varName) { + return Blockly.Types.ARRAY; }, /** * Updates the content of the the pin related fields. @@ -153,8 +119,8 @@ Blockly.Blocks['stepper_step'] = { this.setColour(Blockly.Blocks.stepper.HUE); this.appendDummyInput() .appendField(Blockly.Msg.ARD_STEPPER_STEP) - .appendField(new Blockly.Blocks.stepper.FieldStepperInstance(), - 'STEPPER_NAME'); + .appendField(new Blockly.Blocks.ComponentFieldVariable( + Blockly.Msg.ARD_STEPPER_DEFAULT_NAME, 'Stepper'), 'STEPPER_NAME') this.appendValueInput('STEPPER_STEPS') .setCheck(Blockly.Types.NUMBER.checkList); this.appendDummyInput() @@ -164,55 +130,32 @@ Blockly.Blocks['stepper_step'] = { this.setTooltip(Blockly.Msg.ARD_STEPPER_STEP_TIP); }, /** - * Called whenever anything on the workspace changes. - * It checks the instances of stepper_config and attaches a warning to this - * block if not valid data is found. + * Return all variables referenced by this block. + * @return {!Array.} List of variable names. * @this Blockly.Block */ - onchange: function() { - if (!this.workspace) { return; } // Block has been deleted. - - var currentDropdown = this.getFieldValue('STEPPER_NAME'); - var instances = Blockly.Blocks.stepper.stepperDropdownList(); - - // Check for configuration block presence - if (instances[0][0] === Blockly.Blocks.stepper.noInstance) { - // Ensure dropdown menu says there is no config block - if (currentDropdown !== Blockly.Blocks.stepper.noInstance) { - this.setFieldValue(Blockly.Blocks.stepper.noInstance, 'STEPPER_NAME'); - } - this.setWarningText(Blockly.Msg.ARD_STEPPER_STEP_WARN1); - } else { - // Configuration blocks present, check if any selected and contains name - var existingConfigSelected = false; - for (var x = 0; x < instances.length; x++) { - // Check if any of the config blocks does not have a name - if (instances[x][0] === Blockly.Blocks.stepper.noName) { - // If selected config has no name either, set warning and exit func - if (currentDropdown === Blockly.Blocks.stepper.noName) { - this.setWarningText(Blockly.Msg.ARD_STEPPER_STEP_WARN2); - return; - } - } else if (instances[x][0] === currentDropdown) { - existingConfigSelected = true; - } - } - - // At this point select config has a name, check if it exist - if (existingConfigSelected) { - // All good, just remove any warnings and exit the function - this.setWarningText(null); - } else { - if ((currentDropdown === Blockly.Blocks.stepper.noName) || - (currentDropdown === Blockly.Blocks.stepper.noInstance)) { - // Just pick the first config block - this.setFieldValue(instances[0][0], 'STEPPER_NAME'); - this.setWarningText(null); - } else { - // Al this point just set a warning to select a valid stepper config - this.setWarningText(Blockly.Msg.ARD_STEPPER_STEP_WARN3); - } - } + getVars: function() { + return [this.getFieldValue('STEPPER_NAME')]; + }, + /** + * Notification that a variable is renaming. + * If the name matches one of this block's variables, rename it. + * @param {string} oldName Previous name of variable. + * @param {string} newName Renamed variable. + * @this Blockly.Block + */ + renameVar: function(oldName, newName) { + if (Blockly.Names.equals(oldName, this.getFieldValue('STEPPER_NAME'))) { + this.setFieldValue(newName, 'STEPPER_NAME'); } + }, + /** + * Gets the variable type required. + * @param {!string} varName Name of the variable selected in this block to + * check. + * @return {string} String to indicate the variable type. + */ + getVarType: function(varName) { + return Blockly.Types.ARRAY; } }; diff --git a/blockly/generators/arduino/stepper.js b/blockly/generators/arduino/stepper.js index 7c1b66b3c0..43990054cd 100644 --- a/blockly/generators/arduino/stepper.js +++ b/blockly/generators/arduino/stepper.js @@ -28,7 +28,7 @@ Blockly.Arduino['stepper_config'] = function(block) { var pin1 = block.getFieldValue('STEPPER_PIN1'); var pin2 = block.getFieldValue('STEPPER_PIN2'); var pinType = Blockly.Arduino.PinTypes.STEPPER; - var stepperName = block.getStepperSetupInstance(); + var stepperName = block.getFieldValue('STEPPER_NAME'); var stepperSteps = Blockly.Arduino.valueToCode(block, 'STEPPER_STEPS', Blockly.Arduino.ORDER_ATOMIC) || '360'; var stepperSpeed = Blockly.Arduino.valueToCode(block, 'STEPPER_SPEED', diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json index f1fccd9b80..72ecf932bf 100644 --- a/blockly/msg/json/nl.json +++ b/blockly/msg/json/nl.json @@ -426,8 +426,9 @@ "ARD_SPI_TRANS_WARN1": "Een Defenitie blok voor %1 moet toegevoegd worden aan de canvas om dit blok te gebruiken!", "ARD_SPI_TRANS_WARN2": "Oude pin waarde %1 is niet langer beschikbaar.", "ARD_SPI_TRANSRETURN_TIP": "Stuur een SPI bericht naar het opgegeven slaaf toestel en krijg data terug.", - "ARD_STEPPER_SETUP": "Definieer", - "ARD_STEPPER_MOTOR": "stappenmotor:", + "ARD_STEPPER_SETUP": "Definieer stappenmotor ", + "ARD_STEPPER_MOTOR": "", + "ARD_STEPPER_DEFAULT_NAME": "MijnStappenMotor", "ARD_STEPPER_PIN1": "pin1#", "ARD_STEPPER_PIN2": "pin2#", "ARD_STEPPER_REVOLVS": "aantal stappen per omwenteling", diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js index 586b273740..0da921d991 100644 --- a/blockly/msg/messages.js +++ b/blockly/msg/messages.js @@ -1172,8 +1172,9 @@ Blockly.Msg.ARD_SPI_TRANS_TIP = 'Send a SPI message to an specified slave device Blockly.Msg.ARD_SPI_TRANS_WARN1 = 'A setup block for %1 must be added to the workspace to use this block!'; Blockly.Msg.ARD_SPI_TRANS_WARN2 = 'Old pin value %1 is no longer available.'; Blockly.Msg.ARD_SPI_TRANSRETURN_TIP = 'Send a SPI message to an specified slave device and get data back.'; -Blockly.Msg.ARD_STEPPER_SETUP = 'Setup'; -Blockly.Msg.ARD_STEPPER_MOTOR = 'stepper motor:'; +Blockly.Msg.ARD_STEPPER_SETUP = 'Setup stepper motor'; +Blockly.Msg.ARD_STEPPER_MOTOR = ''; +Blockly.Msg.ARD_STEPPER_DEFAULT_NAME = 'MyStepper'; Blockly.Msg.ARD_STEPPER_PIN1 = 'pin1#'; Blockly.Msg.ARD_STEPPER_PIN2 = 'pin2#'; Blockly.Msg.ARD_STEPPER_REVOLVS = 'how many steps per revolution'; From fc4cfee4645112fee55a85d9472fa7aa089e2a37 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Sat, 30 Apr 2016 13:19:51 +0200 Subject: [PATCH 03/31] step 2 stepper: generator --- blockly/generators/arduino/stepper.js | 11 ++++++++--- blockly/msg/js/nl.js | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/blockly/generators/arduino/stepper.js b/blockly/generators/arduino/stepper.js index 43990054cd..400f174d39 100644 --- a/blockly/generators/arduino/stepper.js +++ b/blockly/generators/arduino/stepper.js @@ -33,6 +33,11 @@ Blockly.Arduino['stepper_config'] = function(block) { Blockly.Arduino.ORDER_ATOMIC) || '360'; var stepperSpeed = Blockly.Arduino.valueToCode(block, 'STEPPER_SPEED', Blockly.Arduino.ORDER_ATOMIC) || '90'; + + //stepper is a variable containing the used pins + Blockly.Arduino.addVariable(stepperName, + 'int ' + stepperName + '[2] = {' + pin1 + ', ' + pin2 + '};', true); + stepperName = 'stepper_' + stepperName Blockly.Arduino.reservePin(block, pin1, pinType, 'Stepper'); Blockly.Arduino.reservePin(block, pin2, pinType, 'Stepper'); @@ -41,10 +46,10 @@ Blockly.Arduino['stepper_config'] = function(block) { var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' + pin1 + ', ' + pin2 + ');'; - Blockly.Arduino.addDeclaration('stepper_' + stepperName, globalCode); + Blockly.Arduino.addDeclaration(stepperName, globalCode); var setupCode = stepperName + '.setSpeed(' + stepperSpeed + ');'; - Blockly.Arduino.addSetup('stepper_' + stepperName, setupCode, true); + Blockly.Arduino.addSetup(stepperName, setupCode, true); return ''; }; @@ -58,7 +63,7 @@ Blockly.Arduino['stepper_config'] = function(block) { * @return {array} Completed code with order of operation. */ Blockly.Arduino['stepper_step'] = function(block) { - var stepperInstanceName = block.getFieldValue('STEPPER_NAME'); + var stepperInstanceName = 'stepper_' + block.getFieldValue('STEPPER_NAME'); var stepperSteps = Blockly.Arduino.valueToCode(block, 'STEPPER_STEPS', Blockly.Arduino.ORDER_ATOMIC) || '0'; var code = stepperInstanceName + '.step(' + stepperSteps + ');\n'; diff --git a/blockly/msg/js/nl.js b/blockly/msg/js/nl.js index a19f51fb37..4ec9a380fa 100644 --- a/blockly/msg/js/nl.js +++ b/blockly/msg/js/nl.js @@ -72,6 +72,7 @@ Blockly.Msg.ARD_SPI_TRANS_TIP = "Stuurt een SPI bericht naar het opgegeven slaaf Blockly.Msg.ARD_SPI_TRANS_VAL = "stuur"; Blockly.Msg.ARD_SPI_TRANS_WARN1 = "Een Defenitie blok voor %1 moet toegevoegd worden aan de canvas om dit blok te gebruiken!"; Blockly.Msg.ARD_SPI_TRANS_WARN2 = "Oude pin waarde %1 is niet langer beschikbaar."; +Blockly.Msg.ARD_STEPPER_DEFAULT_NAME = "MijnStappenMotor"; Blockly.Msg.ARD_STEPPER_MOTOR = "stappenmotor:"; Blockly.Msg.ARD_STEPPER_PIN1 = "pin1#"; Blockly.Msg.ARD_STEPPER_PIN2 = "pin2#"; @@ -491,4 +492,4 @@ Blockly.Msg.LISTS_CREATE_WITH_ITEM_TITLE = Blockly.Msg.VARIABLES_DEFAULT_NAME; Blockly.Msg.TEXT_APPEND_VARIABLE = Blockly.Msg.VARIABLES_DEFAULT_NAME; Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM = Blockly.Msg.VARIABLES_DEFAULT_NAME; Blockly.Msg.LISTS_INDEX_OF_INPUT_IN_LIST = Blockly.Msg.LISTS_INLIST; -Blockly.Msg.PROCEDURES_DEFRETURN_COMMENT = Blockly.Msg.PROCEDURES_DEFNORETURN_COMMENT; \ No newline at end of file +Blockly.Msg.PROCEDURES_DEFRETURN_COMMENT = Blockly.Msg.PROCEDURES_DEFNORETURN_COMMENT; From e258e1e1f95e02e8cebcfd4442b4bca1c40a7f59 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Sat, 30 Apr 2016 14:10:54 +0200 Subject: [PATCH 04/31] create a generic test if setup block of a component is present --- blockly/blocks/arduino/stepper.js | 17 +++++++++++ blockly/blocks/component_variable.js | 45 ++++++++++++++++++++++++++++ blockly/msg/json/nl.json | 4 +-- blockly/msg/messages.js | 4 +-- 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/blockly/blocks/arduino/stepper.js b/blockly/blocks/arduino/stepper.js index 40707fb5f6..fdeb9f4d46 100644 --- a/blockly/blocks/arduino/stepper.js +++ b/blockly/blocks/arduino/stepper.js @@ -157,5 +157,22 @@ Blockly.Blocks['stepper_step'] = { */ getVarType: function(varName) { return Blockly.Types.ARRAY; + }, + /** + * Called whenever anything on the workspace changes. + * It checks the instances of stepper_config and attaches a warning to this + * block if not valid data is found. + * @this Blockly.Block + */ + onchange: function() { + if (!this.workspace) { return; } // Block has been deleted. + + var currentDropdown = this.getFieldValue('STEPPER_NAME'); + if (Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent(currentDropdown, 'Stepper')) { + this.setWarningText(null); + } else { + // Set a warning to select a valid stepper config + this.setWarningText(Blockly.Msg.ARD_STEPPER_STEP_WARN1); + } } }; diff --git a/blockly/blocks/component_variable.js b/blockly/blocks/component_variable.js index 3a99175f3c..be32f4d3ce 100644 --- a/blockly/blocks/component_variable.js +++ b/blockly/blocks/component_variable.js @@ -95,6 +95,7 @@ Blockly.Blocks.ComponentFieldVariable.ComponentVariables = function(root, compon return variableList; }; + /** * Return a sorted list of variable names for variable dropdown menus. * Include a special option at the end for creating a new variable name. @@ -125,3 +126,47 @@ Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents = function() { } return options; }; + + +/** + * Finds all user-created instances of the ComponentFieldVariable block config. + * @return {!Array.} Array of instance names. + */ +Blockly.Blocks.ComponentFieldVariable.Instances = function(component_type) { + var instList = []; + var blocks = Blockly.mainWorkspace.getAllBlocks(); + for (var x = 0; x < blocks.length; x++) { + var getSetupInstance = blocks[x].getComponentName; + if (getSetupInstance) { + var Instances = getSetupInstance.call(); + if (Instances) { + if (Instances == component_type) { + instList.push(blocks[x].getVars()[0]); + } + } + } + } + return instList; +}; + +Blockly.Blocks.ComponentFieldVariable.CheckSetupPresent = function(currentDropdown, component_type) { + var instances = Blockly.Blocks.ComponentFieldVariable.Instances(component_type); + + // Check for configuration block presence + if (! instances) { + return false; + } else { + // Configuration blocks present, check if any selected in this block + var existingConfigSelected = false; + for (var x = 0; x < instances.length; x++) { + if (instances[x] === currentDropdown) { + existingConfigSelected = true; + } + } + if (existingConfigSelected) { + return true; + } else { + return false; + } + } +} diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json index 72ecf932bf..e63aa9a08d 100644 --- a/blockly/msg/json/nl.json +++ b/blockly/msg/json/nl.json @@ -437,9 +437,7 @@ "ARD_STEPPER_STEP": "beweeg stappenmotor", "ARD_STEPPER_STEPS": "stappen", "ARD_STEPPER_STEP_TIP": "Draait de stappenmotor een opgegeven aantal stappen", - "ARD_STEPPER_STEP_WARN1": "Een Stappenmotor start Definieer blok moet toegevoegd worden om dit blok te gebruiken!", - "ARD_STEPPER_STEP_WARN2": "Een naam input moet toegevoegd worden aan een stappenmotor Definieer blok!", - "ARD_STEPPER_STEP_WARN3": "De geselecteerde stappenmotor bestaat niet meer, kies een nieuwe.", + "ARD_STEPPER_STEP_WARN1": "Een Stappenmotor Definieer blok met dezelfde stappenmotor naam moet toegevoegd worden om dit blok te gebruiken!", "ARD_TIME_DELAY": "wacht", "ARD_TIME_MS": "milliseconden", "ARD_TIME_DELAY_TIP": "Wacht de opgegeven tijd in milliseconden (duizendsten van een seconde)", diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js index 0da921d991..4b6c3b7d31 100644 --- a/blockly/msg/messages.js +++ b/blockly/msg/messages.js @@ -1183,9 +1183,7 @@ Blockly.Msg.ARD_STEPPER_SETUP_TIP = 'Configures a stepper motor pinout and other Blockly.Msg.ARD_STEPPER_STEP = 'move stepper'; Blockly.Msg.ARD_STEPPER_STEPS = 'steps'; Blockly.Msg.ARD_STEPPER_STEP_TIP = 'Turns the stepper motor a specific number of steps.'; -Blockly.Msg.ARD_STEPPER_STEP_WARN1 = 'A STEPPER configuration block must be added to use this block!'; -Blockly.Msg.ARD_STEPPER_STEP_WARN2 = 'A Name input must be added to the Stepper configuration block!'; -Blockly.Msg.ARD_STEPPER_STEP_WARN3 = 'Selected stepper does not exist any more, please select a new one.'; +Blockly.Msg.ARD_STEPPER_STEP_WARN1 = 'A STEPPER configuration block with the same stepper name must be added to use this block!'; Blockly.Msg.ARD_TIME_DELAY = 'wait'; Blockly.Msg.ARD_TIME_MS = 'milliseconds'; Blockly.Msg.ARD_TIME_DELAY_TIP = 'Wait specific time in milliseconds'; From 81608573dbfc181f7dd446542ba77fc4ed9c88ed Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Sat, 30 Apr 2016 16:25:49 +0200 Subject: [PATCH 05/31] Fix issues with menu generations: should be class method --- blockly/blocks/arduino/stepper.js | 2 +- blockly/blocks/component_variable.js | 8 ++++---- blockly/core/field_variable.js | 4 ++-- blockly/msg/json/nl.json | 3 ++- blockly/msg/messages.js | 3 ++- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/blockly/blocks/arduino/stepper.js b/blockly/blocks/arduino/stepper.js index fdeb9f4d46..ba30bbbfc6 100644 --- a/blockly/blocks/arduino/stepper.js +++ b/blockly/blocks/arduino/stepper.js @@ -172,7 +172,7 @@ Blockly.Blocks['stepper_step'] = { this.setWarningText(null); } else { // Set a warning to select a valid stepper config - this.setWarningText(Blockly.Msg.ARD_STEPPER_STEP_WARN1); + this.setWarningText(Blockly.Msg.ARD_COMPONENT_WARN1.replace('%1', Blockly.Msg.ARD_STEPPER_COMPONENT).replace('%1', Blockly.Msg.ARD_STEPPER_COMPONENT)); } } }; diff --git a/blockly/blocks/component_variable.js b/blockly/blocks/component_variable.js index be32f4d3ce..7056cb2c9a 100644 --- a/blockly/blocks/component_variable.js +++ b/blockly/blocks/component_variable.js @@ -40,11 +40,11 @@ goog.require('Blockly.Blocks'); * @constructor */ Blockly.Blocks.ComponentFieldVariable = function(varname, component_type, opt_validator) { - Blockly.Blocks.ComponentFieldVariable['component_type'] = component_type + this.component_type = component_type //override the dropdownCreate to use for this field - Blockly.FieldVariable.dropdownCreate = Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents; + this.dropdownCreate = Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents; Blockly.Blocks.ComponentFieldVariable.superClass_.constructor.call(this, - Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents, opt_validator); + varname, opt_validator); this.setValue(varname || ''); }; goog.inherits(Blockly.Blocks.ComponentFieldVariable, Blockly.FieldVariable); @@ -106,7 +106,7 @@ Blockly.Blocks.ComponentFieldVariable.dropdownCreateComponents = function() { if (this.sourceBlock_ && this.sourceBlock_.workspace) { var variableList = Blockly.Blocks.ComponentFieldVariable.ComponentVariables(this.sourceBlock_.workspace, - Blockly.Blocks.ComponentFieldVariable['component_type']); + this.component_type); } else { var variableList = []; } diff --git a/blockly/core/field_variable.js b/blockly/core/field_variable.js index 6cd627abf2..451832253b 100644 --- a/blockly/core/field_variable.js +++ b/blockly/core/field_variable.js @@ -44,7 +44,7 @@ goog.require('goog.string'); */ Blockly.FieldVariable = function(varname, opt_validator) { Blockly.FieldVariable.superClass_.constructor.call(this, - Blockly.FieldVariable.dropdownCreate, opt_validator); + this.dropdownCreate, opt_validator); this.setValue(varname || ''); }; goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown); @@ -124,7 +124,7 @@ Blockly.FieldVariable.prototype.setValue = function(newValue) { * @return {!Array.} Array of variable names. * @this {!Blockly.FieldVariable} */ -Blockly.FieldVariable.dropdownCreate = function() { +Blockly.FieldVariable.prototype.dropdownCreate = function() { if (this.sourceBlock_ && this.sourceBlock_.workspace) { var variableList = Blockly.Variables.allVariables(this.sourceBlock_.workspace); diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json index e63aa9a08d..f5bdcfa997 100644 --- a/blockly/msg/json/nl.json +++ b/blockly/msg/json/nl.json @@ -437,7 +437,8 @@ "ARD_STEPPER_STEP": "beweeg stappenmotor", "ARD_STEPPER_STEPS": "stappen", "ARD_STEPPER_STEP_TIP": "Draait de stappenmotor een opgegeven aantal stappen", - "ARD_STEPPER_STEP_WARN1": "Een Stappenmotor Definieer blok met dezelfde stappenmotor naam moet toegevoegd worden om dit blok te gebruiken!", + "ARD_STEPPER_COMPONENT": "stappenmotor", + "ARD_COMPONENT_WARN1": "Een %1 Definieer blok met dezelfde %1 naam moet toegevoegd worden om dit blok te gebruiken!", "ARD_TIME_DELAY": "wacht", "ARD_TIME_MS": "milliseconden", "ARD_TIME_DELAY_TIP": "Wacht de opgegeven tijd in milliseconden (duizendsten van een seconde)", diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js index 4b6c3b7d31..a049ba3b20 100644 --- a/blockly/msg/messages.js +++ b/blockly/msg/messages.js @@ -1183,7 +1183,8 @@ Blockly.Msg.ARD_STEPPER_SETUP_TIP = 'Configures a stepper motor pinout and other Blockly.Msg.ARD_STEPPER_STEP = 'move stepper'; Blockly.Msg.ARD_STEPPER_STEPS = 'steps'; Blockly.Msg.ARD_STEPPER_STEP_TIP = 'Turns the stepper motor a specific number of steps.'; -Blockly.Msg.ARD_STEPPER_STEP_WARN1 = 'A STEPPER configuration block with the same stepper name must be added to use this block!'; +Blockly.Msg.ARD_STEPPER_COMPONENT = 'stepper'; +Blockly.Msg.ARD_COMPONENT_WARN1 = 'A %1 configuration block with the same %1 name must be added to use this block!'; Blockly.Msg.ARD_TIME_DELAY = 'wait'; Blockly.Msg.ARD_TIME_MS = 'milliseconds'; Blockly.Msg.ARD_TIME_DELAY_TIP = 'Wait specific time in milliseconds'; From 64ff54a97e4fe03697a3665012574f44b9b22507 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Fri, 6 May 2016 15:23:36 +0200 Subject: [PATCH 06/31] Improvements: less code needed, better documentation --- ardublockly/index.html | 1 + blockly/blocks/arduino/stepper.js | 2 +- blockly/blocks/component_variable.js | 46 ++++++++++++---------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/ardublockly/index.html b/ardublockly/index.html index fac392f695..532925a281 100644 --- a/ardublockly/index.html +++ b/ardublockly/index.html @@ -18,6 +18,7 @@