From 5304907fd02738a88a95a5c0edc46c033bfbaf8c Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 29 Sep 2020 10:30:35 +0200 Subject: [PATCH] add block type to setter and getter --- src/components/Blockly/blocks/io.js | 2 +- .../Blockly/blocks/sensebox-display.js | 2 + .../Blockly/blocks/sensebox-sensors.js | 9 +- src/components/Blockly/blocks/variables.js | 200 ++--------- src/components/Blockly/generator/variables.js | 2 +- src/components/Blockly/helpers/types.js | 33 +- src/components/Blockly/toolbox/Toolbox.js | 330 +++++++++--------- 7 files changed, 237 insertions(+), 341 deletions(-) diff --git a/src/components/Blockly/blocks/io.js b/src/components/Blockly/blocks/io.js index 0e231e9..aab7dd2 100644 --- a/src/components/Blockly/blocks/io.js +++ b/src/components/Blockly/blocks/io.js @@ -56,7 +56,7 @@ Blockly.Blocks['io_digitalread'] = { .appendField(Blockly.Msg.ARD_DIGITALREAD) .appendField(new Blockly.FieldDropdown( selectedBoard().digitalPins), 'PIN'); - this.setOutput(true, Types.BOOLEAN); + this.setOutput(true, 'boolean'); this.setTooltip(Blockly.Msg.ARD_DIGITALREAD_TIP); }, /** @return {!string} The type of return value for the block, an integer. */ diff --git a/src/components/Blockly/blocks/sensebox-display.js b/src/components/Blockly/blocks/sensebox-display.js index 4b6220b..fc51aab 100644 --- a/src/components/Blockly/blocks/sensebox-display.js +++ b/src/components/Blockly/blocks/sensebox-display.js @@ -50,6 +50,8 @@ Blockly.Blocks['sensebox_display_printDisplay'] = { .setCheck(null); this.setPreviousStatement(true, null); this.setNextStatement(true, null); + let variableName = this.getField('COLOR'); + console.log(variableName.getValue()); this.setTooltip(Blockly.Msg.senseBox_display_printDisplay_tip); this.setHelpUrl('https://sensebox.de/books'); }, diff --git a/src/components/Blockly/blocks/sensebox-sensors.js b/src/components/Blockly/blocks/sensebox-sensors.js index 0308d7e..e581e0b 100644 --- a/src/components/Blockly/blocks/sensebox-sensors.js +++ b/src/components/Blockly/blocks/sensebox-sensors.js @@ -2,6 +2,7 @@ import Blockly from 'blockly'; import { getColour } from '../helpers/colour' import * as Types from '../helpers/types' import { selectedBoard } from '../helpers/board' +import { colors } from '@material-ui/core'; /** * HDC1080 Temperature and Humidity Sensor @@ -16,7 +17,7 @@ Blockly.Blocks['sensebox_sensor_temp_hum'] = { .setAlign(Blockly.ALIGN_RIGHT) .appendField(Blockly.Msg.senseBox_value) .appendField(new Blockly.FieldDropdown([[Blockly.Msg.senseBox_temp, "Temperature"], [Blockly.Msg.senseBox_hum, "Humidity"]]), "NAME"); - this.setOutput(true, Types.NUMBER.typeId); + this.setOutput(true, Types.DECIMAL.typeId); this.setColour(getColour().sensebox); this.setTooltip(Blockly.Msg.senseBox_temp_hum_tip); this.setHelpUrl('https://edu.books.sensebox.de/de/projekte/diy_umweltstation/temp_und_luftfeuchte.html'); @@ -124,6 +125,8 @@ Blockly.Blocks['sensebox_sensor_pressure'] = { this.setOutput(true, Types.NUMBER.typeId); this.setTooltip(Blockly.Msg.senseBox_pressure_tip); this.setHelpUrl('https://edu.books.sensebox.de/de/projekte/diy_umweltstation/luftdruck.html'); + var input = this.getFieldValue('NAME'); + console.log(input); }, /** * Parse XML to restore the number of pins available. @@ -231,7 +234,7 @@ Blockly.Blocks['sensebox_sensor_ultrasonic_ranger'] = { .appendField(Blockly.Msg.senseBox_ultrasonic_echo) .appendField(new Blockly.FieldDropdown( selectedBoard().digitalPins), 'ultrasonic_echo'); - this.setOutput(true, Types.NUMBER.typeId); + this.setOutput(true, 'int'); this.setTooltip(Blockly.Msg.senseBox_ultrasonic_tip); this.setHelpUrl('https://sensebox.de/books'); }, @@ -283,7 +286,7 @@ Blockly.Blocks['sensebox_sensor_ultrasonic_ranger'] = { }, getBlockType: function () { - return Blockly.Types.NUMBER; + return Blockly.Types.SHORT_NUMBER; } }; diff --git a/src/components/Blockly/blocks/variables.js b/src/components/Blockly/blocks/variables.js index 30ec057..ea4013d 100644 --- a/src/components/Blockly/blocks/variables.js +++ b/src/components/Blockly/blocks/variables.js @@ -1,167 +1,41 @@ -import Blockly from 'blockly/core'; +import Blockly, { FieldVariable } from 'blockly/core'; import { getColour } from '../helpers/colour'; +import { getCompatibleTypes } from '../helpers/types' -Blockly.defineBlocksWithJsonArray([ - // BEGIN JSON EXTRACT - { - type: 'variables_get_number', - message0: '= number variable %1', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['Number'], - defaultType: 'Number', - }, - ], - output: 'Number', - colour: getColour().variables, - tooltip: '', - helpUrl: '', + +Blockly.Blocks['variables_set_dynamic'] = { + init: function () { + + // const type = myVar.type; + this.setColour(getColour().variables); + this.setPreviousStatement(true, null); + this.setNextStatement(true, null); + this.appendValueInput('VALUE') + .appendField('set', 'set') + .appendField(new Blockly.FieldVariable('VAR'), 'VAR') + .appendField('to'); }, - { - type: 'variables_set_number', - message0: 'Number variable %1 = %2', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['Number'], - defaultType: 'Number', - createNewVariable: false, - }, - { - type: 'input_value', - name: 'VALUE', - check: 'Number', - }, - ], - previousStatement: null, - nextStatement: null, - colour: getColour().variables, - tooltip: '', - helpUrl: '', + onchange: function (e) { + let variableID = this.getFieldValue('VAR'); + let variable = Blockly.getMainWorkspace().getVariableMap().getVariableById(variableID) + this.getInput('VALUE').setCheck(getCompatibleTypes(variable.type)); + + } +} + +Blockly.Blocks['variables_get_dynamic'] = { + init: function () { + this.setColour(getColour().variables); + this.appendDummyInput() + .appendField('', 'type') + .appendField(new Blockly.FieldVariable('VAR'), 'VAR'); + this.setOutput(true); }, - { - type: 'variables_get_colour', - message0: '= color variable %1', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['Colour'], - defaultType: 'Colour', - }, - ], - output: 'Colour', - colour: getColour().variables, - tooltip: '', - helpUrl: '', - }, - { - type: 'variables_set_colour', - message0: 'Color variable %1 = %2', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['Colour'], - defaultType: 'Colour', - }, - { - type: 'input_value', - name: 'VALUE', - check: 'Colour', - }, - ], - previousStatement: null, - nextStatement: null, - colour: getColour().variables, - tooltip: '', - helpUrl: '', - }, - { - type: 'variables_get_string', - message0: '= text variable %1', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['String'], - defaultType: 'String', - }, - ], - output: 'String', - colour: getColour().variables, - tooltip: '', - helpUrl: '', - }, - { - type: 'variables_set_string', - message0: 'Text variable %1 = %2', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['String'], - defaultType: 'String', - }, - { - type: 'input_value', - name: 'VALUE', - check: 'String', - }, - ], - previousStatement: null, - nextStatement: null, - colour: getColour().variables, - tooltip: '', - helpUrl: '', - }, - { - type: 'variables_get_boolean', - message0: '= boolean variable %1', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['Boolean'], - defaultType: 'Boolean', - }, - ], - output: 'Boolean', - colour: getColour().variables, - tooltip: '', - helpUrl: '', - }, - { - type: 'variables_set_boolean', - message0: 'Boolean variable %1 = %2', - args0: [ - { - type: 'field_variable', - name: 'VAR', - variable: null, - variableTypes: ['Boolean'], - defaultType: 'Boolean', - }, - { - type: 'input_value', - name: 'VALUE', - check: 'Boolean', - }, - ], - previousStatement: null, - nextStatement: null, - colour: getColour().variables, - tooltip: '', - helpUrl: '', - }, -]); // END JSON EXTRACT (Do not delete this comment.) \ No newline at end of file + onchange: function (e) { + let variableID = this.getFieldValue('VAR'); + let variable = Blockly.getMainWorkspace().getVariableMap().getVariableById(variableID) + this.getField('type').setValue(variable.type); + + } +} + diff --git a/src/components/Blockly/generator/variables.js b/src/components/Blockly/generator/variables.js index 6ff9aad..2c75d60 100644 --- a/src/components/Blockly/generator/variables.js +++ b/src/components/Blockly/generator/variables.js @@ -15,7 +15,7 @@ const setVariableFunction = function (defaultValue) { const allVars = Blockly.getMainWorkspace().getVariableMap().getAllVariables(); const myVar = allVars.filter(v => v.name === variableName)[0] - Blockly.Arduino.setupCode_[''] = myVar.type + " " + myVar.name + ';\n'; + Blockly.Arduino.variables_[myVar] = myVar.type + " " + myVar.name + ';\n'; return variableName + ' = ' + (variableValue || defaultValue) + ';\n'; }; }; diff --git a/src/components/Blockly/helpers/types.js b/src/components/Blockly/helpers/types.js index 339d033..85f21d1 100644 --- a/src/components/Blockly/helpers/types.js +++ b/src/components/Blockly/helpers/types.js @@ -12,6 +12,7 @@ /** Single character. */ export const CHARACTER = { typeId: 'Character', + typeName: 'char', typeMsgName: 'ARD_TYPE_CHAR', compatibleTypes: [] } @@ -31,14 +32,15 @@ export const TEXT = { /** Short integer number. */ export const SHORT_NUMBER = { - typeId: 'Short Number', + typeId: 'Short_Number', + typeName: 'int', typeMsgName: 'ARD_TYPE_SHORT', - compatibleTypes: [] // Circular dependencies, add after all declarations + compatibleTypes: ['int', 'float',] // Circular dependencies, add after all declarations } /** Integer number. */ export const NUMBER = { - typeId: 'Number', + typeId: 'int', typeMsgName: 'ARD_TYPE_NUMBER', compatibleTypes: ['Number'] // Circular dependencies, add after all declarations } @@ -52,12 +54,12 @@ export const LARGE_NUMBER = { /** Decimal/floating point number. */ export const DECIMAL = { - typeId: 'Decimal', + typeId: 'float', typeMsgName: 'ARD_TYPE_DECIMAL', compatibleTypes: [BOOLEAN.typeId, - NUMBER, - SHORT_NUMBER, - LARGE_NUMBER] + NUMBER.typeId, + SHORT_NUMBER.typeId, + LARGE_NUMBER.typeId] } /** Array/List of items. */ @@ -88,8 +90,23 @@ export const CHILD_BLOCK_MISSING = { compatibleTypes: [] } +const compatibleTypes = { + boolean: ['boolean'], + int: ['int'], + char: ['char'], + string: ['String'], + void: ['void'], + long: ['int', 'long'], + double: ['int', 'long', 'double'], + float: ['int', 'long', 'double', 'float'] -export const VARIABLE_TYPES = ['Number', 'String', 'Boolean', 'Colour']; +} + +export const getCompatibleTypes = (type) => { + return compatibleTypes[type]; +}; + +export const VARIABLE_TYPES = [['SHORT_NUMBER', 'char'], ['NUMBER', 'int'], ['DECIMAL', 'long'], ['TEXT', 'String'], ['CHARACTER', 'char'], ['BOOLEAN', 'boolean'], ['NULL', 'void'], ['UNDEF', 'undefined']]; // /** // * Some Types have circular dependencies on their compatibilities, so add them diff --git a/src/components/Blockly/toolbox/Toolbox.js b/src/components/Blockly/toolbox/Toolbox.js index b72e380..50396b1 100644 --- a/src/components/Blockly/toolbox/Toolbox.js +++ b/src/components/Blockly/toolbox/Toolbox.js @@ -14,9 +14,9 @@ class Toolbox extends React.Component { componentDidUpdate() { this.props.workspace.registerToolboxCategoryCallback('CREATE_TYPED_VARIABLE', this.createFlyout); - - const typedVarModal = new TypedVariableModal(this.props.workspace, 'callbackName', [['SHORT_NUMBER', 'char'], ['NUMBER', 'int'], ['DECIMAL', 'long'], ['TEXT', 'String'], ['CHARACTER', 'char'], ['BOOLEAN', 'boolean'], ['NULL', 'void'], ['UNDEF', 'undefined']]); - typedVarModal.init(); + + const typedVarModal = new TypedVariableModal(this.props.workspace, 'callbackName', [['SHORT_NUMBER', 'char'], ['NUMBER', 'int'], ['DECIMAL', 'float'], ['TEXT', 'String'], ['CHARACTER', 'char'], ['BOOLEAN', 'boolean'], ['NULL', 'void'], ['UNDEF', 'undefined']]); + typedVarModal.init(); } createFlyout(workspace) { @@ -25,9 +25,9 @@ class Toolbox extends React.Component { const button = document.createElement('button'); button.setAttribute('text', 'Create Typed Variable'); button.setAttribute('callbackKey', 'callbackName'); - + xmlList.push(button); - + // This gets all the variables that the user creates and adds them to the // flyout. const blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace); @@ -38,169 +38,169 @@ class Toolbox extends React.Component { render() { return ( - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + + + + + 0 + + + + + 0 + + + + + + + + + + + + + + + + + + + 0 + + + + + 15 + + + + + 0 + + + + + 50 + + + + + 5 + + + + + 0 + + + + + 15 + + + + + + + 0 + + + + + 0 + + + + + 0 + + + + + + + 0 + + + + + 0 + + + + + 0 + + + + + 0 + + + + + + + + + + + + + + + + + + - - - - + + + - - - - - - - - - - - - 1 - - - - - 0 - - - - - 0 - - - - - - - - - - - - - - - - - - - 0 - - - - - 15 - - - - - 0 - - - - - 50 - - - - - 5 - - - - - 0 - - - - - 15 - - - - - - - 0 - - - - - 0 - - - - - 0 - - - - - - - 0 - - - - - 0 - - - - - 0 - - - - - 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + +