add math types
This commit is contained in:
		
							parent
							
								
									5304907fd0
								
							
						
					
					
						commit
						d64512ed9e
					
				| @ -11,7 +11,7 @@ Blockly.Blocks['io_tone'] = { | ||||
|             .appendField(new Blockly.FieldDropdown( | ||||
|                 selectedBoard().digitalPins), "TONEPIN"); | ||||
|         this.appendValueInput("FREQUENCY") | ||||
|             .setCheck(Types.NUMBER.checkList) | ||||
|             .setCheck(Types.getCompatibleTypes('int')) | ||||
|             .appendField(Blockly.Msg.ARD_TONEFREQ); | ||||
|         this.appendDummyInput() | ||||
|             .appendField("Hz"); | ||||
| @ -39,10 +39,6 @@ Blockly.Blocks['io_tone'] = { | ||||
|         } else { | ||||
|             this.setWarningText(null, 'io_tone'); | ||||
|         } | ||||
|     }, | ||||
|     /** @return {!string} The type of input value for the block, an integer. */ | ||||
|     getBlockType: function () { | ||||
|         return Blockly.Types.NUMBER; | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
|  | ||||
| @ -0,0 +1,528 @@ | ||||
| import * as Blockly from 'blockly/core'; | ||||
| import { getColour } from '../helpers/colour'; | ||||
| import * as Types from '../helpers/types' | ||||
| 
 | ||||
| Blockly.Blocks['math_number'] = { | ||||
|     /** | ||||
|      * Block for numeric value. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.setHelpUrl(Blockly.Msg.MATH_NUMBER_HELPURL); | ||||
|         this.setColour(getColour().math); | ||||
|         this.appendDummyInput() | ||||
|             .appendField( | ||||
|                 new Blockly.FieldTextInput( | ||||
|                     '0', Blockly.FieldTextInput.numberValidator), | ||||
|                 'NUM'); | ||||
|         this.setOutput(true, Types.NUMBER.typeName); | ||||
|         // Assign 'this' to a variable for use in the tooltip closure below.
 | ||||
|         var thisBlock = this; | ||||
|         // Number block is trivial.  Use tooltip of parent block if it exists.
 | ||||
|         this.setTooltip(function () { | ||||
|             var parent = thisBlock.getParent(); | ||||
|             return (parent && parent.getInputsInline() && parent.tooltip) || | ||||
|                 Blockly.Msg.MATH_NUMBER_TOOLTIP; | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_arithmetic'] = { | ||||
|     /** | ||||
|      * Block for basic arithmetic operator. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": "%1 %2 %3", | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "A", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "field_dropdown", | ||||
|                     "name": "OP", | ||||
|                     "options": | ||||
|                         [[Blockly.Msg.MATH_ADDITION_SYMBOL, 'ADD'], | ||||
|                         [Blockly.Msg.MATH_SUBTRACTION_SYMBOL, 'MINUS'], | ||||
|                         [Blockly.Msg.MATH_MULTIPLICATION_SYMBOL, 'MULTIPLY'], | ||||
|                         [Blockly.Msg.MATH_DIVISION_SYMBOL, 'DIVIDE'], | ||||
|                         [Blockly.Msg.MATH_POWER_SYMBOL, 'POWER']] | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "B", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 } | ||||
|             ], | ||||
|             "inputsInline": true, | ||||
|             "output": Types.NUMBER.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "helpUrl": Blockly.Msg.MATH_ARITHMETIC_HELPURL | ||||
|         }); | ||||
|         // Assign 'this' to a variable for use in the tooltip closure below.
 | ||||
|         var thisBlock = this; | ||||
|         this.setTooltip(function () { | ||||
|             var mode = thisBlock.getFieldValue('OP'); | ||||
|             var TOOLTIPS = { | ||||
|                 'ADD': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_ADD, | ||||
|                 'MINUS': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_MINUS, | ||||
|                 'MULTIPLY': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_MULTIPLY, | ||||
|                 'DIVIDE': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_DIVIDE, | ||||
|                 'POWER': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_POWER | ||||
|             }; | ||||
|             return TOOLTIPS[mode]; | ||||
|         }); | ||||
|     } | ||||
|     //TODO: a getBlockType based on the two input types following C++ rules
 | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_single'] = { | ||||
|     /** | ||||
|      * Block for advanced math operators with single operand. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": "%1 %2", | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "field_dropdown", | ||||
|                     "name": "OP", | ||||
|                     "options": [ | ||||
|                         [Blockly.Msg.MATH_SINGLE_OP_ROOT, 'ROOT'], | ||||
|                         [Blockly.Msg.MATH_SINGLE_OP_ABSOLUTE, 'ABS'], | ||||
|                         ['-', 'NEG'], | ||||
|                         ['ln', 'LN'], | ||||
|                         ['log10', 'LOG10'], | ||||
|                         ['e^', 'EXP'], | ||||
|                         ['10^', 'POW10'] | ||||
|                     ] | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "NUM", | ||||
|                     "check": Types.getCompatibleTypes('float') | ||||
|                 } | ||||
|             ], | ||||
|             "output": Types.DECIMAL.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "helpUrl": Blockly.Msg.MATH_SINGLE_HELPURL | ||||
|         }); | ||||
|         // Assign 'this' to a variable for use in the tooltip closure below.
 | ||||
|         var thisBlock = this; | ||||
|         this.setTooltip(function () { | ||||
|             var mode = thisBlock.getFieldValue('OP'); | ||||
|             var TOOLTIPS = { | ||||
|                 'ROOT': Blockly.Msg.MATH_SINGLE_TOOLTIP_ROOT, | ||||
|                 'ABS': Blockly.Msg.MATH_SINGLE_TOOLTIP_ABS, | ||||
|                 'NEG': Blockly.Msg.MATH_SINGLE_TOOLTIP_NEG, | ||||
|                 'LN': Blockly.Msg.MATH_SINGLE_TOOLTIP_LN, | ||||
|                 'LOG10': Blockly.Msg.MATH_SINGLE_TOOLTIP_LOG10, | ||||
|                 'EXP': Blockly.Msg.MATH_SINGLE_TOOLTIP_EXP, | ||||
|                 'POW10': Blockly.Msg.MATH_SINGLE_TOOLTIP_POW10 | ||||
|             }; | ||||
|             return TOOLTIPS[mode]; | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_trig'] = { | ||||
|     /** | ||||
|      * Block for trigonometry operators. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": "%1 %2", | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "field_dropdown", | ||||
|                     "name": "OP", | ||||
|                     "options": [ | ||||
|                         [Blockly.Msg.MATH_TRIG_SIN, 'SIN'], | ||||
|                         [Blockly.Msg.MATH_TRIG_COS, 'COS'], | ||||
|                         [Blockly.Msg.MATH_TRIG_TAN, 'TAN'], | ||||
|                         [Blockly.Msg.MATH_TRIG_ASIN, 'ASIN'], | ||||
|                         [Blockly.Msg.MATH_TRIG_ACOS, 'ACOS'], | ||||
|                         [Blockly.Msg.MATH_TRIG_ATAN, 'ATAN'] | ||||
|                     ] | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "NUM", | ||||
|                     "check": Types.getCompatibleTypes('float') | ||||
|                 } | ||||
|             ], | ||||
|             "output": Types.DECIMAL.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "helpUrl": Blockly.Msg.MATH_TRIG_HELPURL | ||||
|         }); | ||||
|         // Assign 'this' to a variable for use in the tooltip closure below.
 | ||||
|         var thisBlock = this; | ||||
|         this.setTooltip(function () { | ||||
|             var mode = thisBlock.getFieldValue('OP'); | ||||
|             var TOOLTIPS = { | ||||
|                 'SIN': Blockly.Msg.MATH_TRIG_TOOLTIP_SIN, | ||||
|                 'COS': Blockly.Msg.MATH_TRIG_TOOLTIP_COS, | ||||
|                 'TAN': Blockly.Msg.MATH_TRIG_TOOLTIP_TAN, | ||||
|                 'ASIN': Blockly.Msg.MATH_TRIG_TOOLTIP_ASIN, | ||||
|                 'ACOS': Blockly.Msg.MATH_TRIG_TOOLTIP_ACOS, | ||||
|                 'ATAN': Blockly.Msg.MATH_TRIG_TOOLTIP_ATAN | ||||
|             }; | ||||
|             return TOOLTIPS[mode]; | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_constant'] = { | ||||
|     /** | ||||
|      * Block for constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": "%1", | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "field_dropdown", | ||||
|                     "name": "CONSTANT", | ||||
|                     "options": [ | ||||
|                         ['\u03c0', 'PI'], | ||||
|                         ['e', 'E'], | ||||
|                         ['\u03c6', 'GOLDEN_RATIO'], | ||||
|                         ['sqrt(2)', 'SQRT2'], | ||||
|                         ['sqrt(\u00bd)', 'SQRT1_2'], | ||||
|                         ['\u221e', 'INFINITY'] | ||||
|                     ] | ||||
|                 } | ||||
|             ], | ||||
|             "output": Types.DECIMAL.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "tooltip": Blockly.Msg.MATH_CONSTANT_TOOLTIP, | ||||
|             "helpUrl": Blockly.Msg.MATH_CONSTANT_HELPURL | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_number_property'] = { | ||||
|     /** | ||||
|      * Block for checking if a number is even, odd, prime, whole, positive, | ||||
|      * negative or if it is divisible by certain number. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         var PROPERTIES = | ||||
|             [[Blockly.Msg.MATH_IS_EVEN, 'EVEN'], | ||||
|             [Blockly.Msg.MATH_IS_ODD, 'ODD'], | ||||
|             [Blockly.Msg.MATH_IS_PRIME, 'PRIME'], | ||||
|             [Blockly.Msg.MATH_IS_WHOLE, 'WHOLE'], | ||||
|             [Blockly.Msg.MATH_IS_POSITIVE, 'POSITIVE'], | ||||
|             [Blockly.Msg.MATH_IS_NEGATIVE, 'NEGATIVE'], | ||||
|             [Blockly.Msg.MATH_IS_DIVISIBLE_BY, 'DIVISIBLE_BY']]; | ||||
|         this.setColour(getColour().math); | ||||
|         this.appendValueInput('NUMBER_TO_CHECK') | ||||
|             .setCheck(Types.getCompatibleTypes('int')); | ||||
|         var dropdown = new Blockly.FieldDropdown(PROPERTIES, function (option) { | ||||
|             var divisorInput = (option === 'DIVISIBLE_BY'); | ||||
|             this.sourceBlock_.updateShape_(divisorInput); | ||||
|         }); | ||||
|         this.appendDummyInput() | ||||
|             .appendField(dropdown, 'PROPERTY'); | ||||
|         this.setInputsInline(true); | ||||
|         this.setOutput(true, Types.BOOLEAN.typeName); | ||||
|         this.setTooltip(Blockly.Msg.MATH_IS_TOOLTIP); | ||||
|     }, | ||||
|     /** | ||||
|      * Create XML to represent whether the 'divisorInput' should be present. | ||||
|      * @return {Element} XML storage element. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     mutationToDom: function () { | ||||
|         var container = document.createElement('mutation'); | ||||
|         var divisorInput = (this.getFieldValue('PROPERTY') === 'DIVISIBLE_BY'); | ||||
|         container.setAttribute('divisor_input', divisorInput); | ||||
|         return container; | ||||
|     }, | ||||
|     /** | ||||
|      * Parse XML to restore the 'divisorInput'. | ||||
|      * @param {!Element} xmlElement XML storage element. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     domToMutation: function (xmlElement) { | ||||
|         var divisorInput = (xmlElement.getAttribute('divisor_input') === 'true'); | ||||
|         this.updateShape_(divisorInput); | ||||
|     }, | ||||
|     /** | ||||
|      * Modify this block to have (or not have) an input for 'is divisible by'. | ||||
|      * @param {boolean} divisorInput True if this block has a divisor input. | ||||
|      * @private | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     updateShape_: function (divisorInput) { | ||||
|         // Add or remove a Value Input.
 | ||||
|         var inputExists = this.getInput('DIVISOR'); | ||||
|         if (divisorInput) { | ||||
|             if (!inputExists) { | ||||
|                 this.appendValueInput('DIVISOR') | ||||
|                     .setCheck(Types.getCompatibleTypes('int')); | ||||
|             } | ||||
|         } else if (inputExists) { | ||||
|             this.removeInput('DIVISOR'); | ||||
|         } | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_change'] = { | ||||
|     /** | ||||
|      * Block for adding to a variable in place. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": Blockly.Msg.MATH_CHANGE_TITLE, | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "field_variable", | ||||
|                     "name": "VAR", | ||||
|                     "variable": Blockly.Msg.MATH_CHANGE_TITLE_ITEM | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "DELTA", | ||||
|                     "check": Types.getCompatibleTypes('int'), | ||||
|                     "align": "RIGHT" | ||||
|                 } | ||||
|             ], | ||||
|             "previousStatement": null, | ||||
|             "nextStatement": null, | ||||
|             "colour": getColour().math, | ||||
|             "helpUrl": Blockly.Msg.MATH_CHANGE_HELPURL | ||||
|         }); | ||||
|         // Assign 'this' to a variable for use in the tooltip closure below.
 | ||||
|         var thisBlock = this; | ||||
|         this.setTooltip(function () { | ||||
|             return Blockly.Msg.MATH_CHANGE_TOOLTIP.replace('%1', | ||||
|                 thisBlock.getFieldValue('VAR')); | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_round'] = { | ||||
|     /** | ||||
|      * Block for rounding functions. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": "%1 %2", | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "field_dropdown", | ||||
|                     "name": "OP", | ||||
|                     "options": [ | ||||
|                         [Blockly.Msg.MATH_ROUND_OPERATOR_ROUND, 'ROUND'], | ||||
|                         [Blockly.Msg.MATH_ROUND_OPERATOR_ROUNDUP, 'ROUNDUP'], | ||||
|                         [Blockly.Msg.MATH_ROUND_OPERATOR_ROUNDDOWN, 'ROUNDDOWN'] | ||||
|                     ] | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "NUM", | ||||
|                     "check": Types.getCompatibleTypes('float') | ||||
|                 } | ||||
|             ], | ||||
|             "output": Types.DECIMAL.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "tooltip": Blockly.Msg.MATH_ROUND_TOOLTIP, | ||||
|             "helpUrl": Blockly.Msg.MATH_ROUND_HELPURL | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_on_list'] = { | ||||
|     /** | ||||
|      * Block for evaluating a list of numbers to return sum, average, min, max, | ||||
|      * etc.  Some functions also work on text (min, max, mode, median). | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         var OPERATORS = | ||||
|             [[Blockly.Msg.MATH_ONLIST_OPERATOR_SUM, 'SUM'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_MIN, 'MIN'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_MAX, 'MAX'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_AVERAGE, 'AVERAGE'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_MEDIAN, 'MEDIAN'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_MODE, 'MODE'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_STD_DEV, 'STD_DEV'], | ||||
|             [Blockly.Msg.MATH_ONLIST_OPERATOR_RANDOM, 'RANDOM']]; | ||||
|         // Assign 'this' to a variable for use in the closures below.
 | ||||
|         var thisBlock = this; | ||||
|         this.setHelpUrl(Blockly.Msg.MATH_ONLIST_HELPURL); | ||||
|         this.setColour(getColour().math); | ||||
|         this.setOutput(true, Types.NUMBER.typeName); | ||||
|         var dropdown = new Blockly.FieldDropdown(OPERATORS, function (newOp) { | ||||
|             thisBlock.updateType_(newOp); | ||||
|         }); | ||||
|         this.appendValueInput('LIST') | ||||
|             .setCheck(Types.getCompatibleTypes('array')) | ||||
|             .appendField(dropdown, 'OP'); | ||||
|         this.setTooltip(function () { | ||||
|             var mode = thisBlock.getFieldValue('OP'); | ||||
|             var TOOLTIPS = { | ||||
|                 'SUM': Blockly.Msg.MATH_ONLIST_TOOLTIP_SUM, | ||||
|                 'MIN': Blockly.Msg.MATH_ONLIST_TOOLTIP_MIN, | ||||
|                 'MAX': Blockly.Msg.MATH_ONLIST_TOOLTIP_MAX, | ||||
|                 'AVERAGE': Blockly.Msg.MATH_ONLIST_TOOLTIP_AVERAGE, | ||||
|                 'MEDIAN': Blockly.Msg.MATH_ONLIST_TOOLTIP_MEDIAN, | ||||
|                 'MODE': Blockly.Msg.MATH_ONLIST_TOOLTIP_MODE, | ||||
|                 'STD_DEV': Blockly.Msg.MATH_ONLIST_TOOLTIP_STD_DEV, | ||||
|                 'RANDOM': Blockly.Msg.MATH_ONLIST_TOOLTIP_RANDOM | ||||
|             }; | ||||
|             return TOOLTIPS[mode]; | ||||
|         }); | ||||
|     }, | ||||
|     /** | ||||
|      * Modify this block to have the correct output type. | ||||
|      * @param {string} newOp Either 'MODE' or some op than returns a number. | ||||
|      * @private | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     updateType_: function (newOp) { | ||||
|         if (newOp === 'MODE') { | ||||
|             this.outputConnection.setCheck(Types.getCompatibleTypes('array')); | ||||
|         } else { | ||||
|             this.outputConnection.setCheck(Types.getCompatibleTypes('int')); | ||||
|         } | ||||
|     }, | ||||
|     /** | ||||
|      * Create XML to represent the output type. | ||||
|      * @return {Element} XML storage element. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     mutationToDom: function () { | ||||
|         var container = document.createElement('mutation'); | ||||
|         container.setAttribute('op', this.getFieldValue('OP')); | ||||
|         return container; | ||||
|     }, | ||||
|     /** | ||||
|      * Parse XML to restore the output type. | ||||
|      * @param {!Element} xmlElement XML storage element. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     domToMutation: function (xmlElement) { | ||||
|         this.updateType_(xmlElement.getAttribute('op')); | ||||
|     } | ||||
|     //TODO: a getBlockType once the list code is finished.
 | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_modulo'] = { | ||||
|     /** | ||||
|      * Block for remainder of a division. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": Blockly.Msg.MATH_MODULO_TITLE, | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "DIVIDEND", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "DIVISOR", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 } | ||||
|             ], | ||||
|             "inputsInline": true, | ||||
|             "output": Types.NUMBER.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "tooltip": Blockly.Msg.MATH_MODULO_TOOLTIP, | ||||
|             "helpUrl": Blockly.Msg.MATH_MODULO_HELPURL | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_constrain'] = { | ||||
|     /** | ||||
|      * Block for constraining a number between two limits. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": Blockly.Msg.MATH_CONSTRAIN_TITLE, | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "VALUE", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "LOW", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "HIGH", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 } | ||||
|             ], | ||||
|             "inputsInline": true, | ||||
|             "output": Types.NUMBER.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "tooltip": Blockly.Msg.MATH_CONSTRAIN_TOOLTIP, | ||||
|             "helpUrl": Blockly.Msg.MATH_CONSTRAIN_HELPURL | ||||
|         }); | ||||
|     } | ||||
|     //TODO: a getBlockType of the same type as the inputs.
 | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_random_int'] = { | ||||
|     /** | ||||
|      * Block for random integer between [X] and [Y]. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": Blockly.Msg.MATH_RANDOM_INT_TITLE, | ||||
|             "args0": [ | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "FROM", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 }, | ||||
|                 { | ||||
|                     "type": "input_value", | ||||
|                     "name": "TO", | ||||
|                     "check": Types.getCompatibleTypes('int') | ||||
|                 } | ||||
|             ], | ||||
|             "inputsInline": true, | ||||
|             "output": Types.NUMBER.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "tooltip": Blockly.Msg.MATH_RANDOM_INT_TOOLTIP, | ||||
|             "helpUrl": Blockly.Msg.MATH_RANDOM_INT_HELPURL | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| 
 | ||||
| Blockly.Blocks['math_random_float'] = { | ||||
|     /** | ||||
|      * Block for random fraction between 0 and 1. | ||||
|      * @this Blockly.Block | ||||
|      */ | ||||
|     init: function () { | ||||
|         this.jsonInit({ | ||||
|             "message0": Blockly.Msg.MATH_RANDOM_FLOAT_TITLE_RANDOM, | ||||
|             "output": Types.DECIMAL.typeName, | ||||
|             "colour": getColour().math, | ||||
|             "tooltip": Blockly.Msg.MATH_RANDOM_FLOAT_TOOLTIP, | ||||
|             "helpUrl": Blockly.Msg.MATH_RANDOM_FLOAT_HELPURL | ||||
|         }); | ||||
|     } | ||||
| }; | ||||
| @ -2,7 +2,6 @@ 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 | ||||
| @ -17,14 +16,11 @@ 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.DECIMAL.typeId); | ||||
|     this.setOutput(true, Types.DECIMAL.typeName); | ||||
|     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'); | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     return Blockly.Types.DECIMAL; | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| /** | ||||
| @ -41,14 +37,11 @@ Blockly.Blocks['sensebox_sensor_uv_light'] = { | ||||
|       .setAlign(Blockly.ALIGN_RIGHT) | ||||
|       .appendField(Blockly.Msg.senseBox_value) | ||||
|       .appendField(new Blockly.FieldDropdown([[Blockly.Msg.senseBox_light, "Illuminance"], [Blockly.Msg.senseBox_uv, "UvIntensity"]]), "NAME"); | ||||
|     this.setOutput(true, Types.NUMBER.typeId); | ||||
|     this.setOutput(true, Types.DECIMAL.typeName); | ||||
|     this.setColour(getColour().sensebox); | ||||
|     this.setTooltip(Blockly.Msg.senseBox_uv_light_tip); | ||||
|     this.setHelpUrl('https://edu.books.sensebox.de/de/projekte/diy_umweltstation/temp_und_luftfeuchte.html'); | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     return Blockly.Types.DECIMAL; | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| /* | ||||
| @ -67,14 +60,11 @@ Blockly.Blocks['sensebox_sensor_bmx055_accelerometer'] = { | ||||
|       .setAlign(Blockly.ALIGN_LEFT) | ||||
|       .appendField(Blockly.Msg.senseBox_bmx055_accelerometer_range) | ||||
|       .appendField(new Blockly.FieldDropdown([[Blockly.Msg.senseBox_bmx055_accelerometer_range_2g, "0x3"], [Blockly.Msg.senseBox_bmx055_accelerometer_range_4g, "0x5"], [Blockly.Msg.senseBox_bmx055_accelerometer_range_8g, "0x8"], [Blockly.Msg.senseBox_bmx055_accelerometer_range_16g, "0x0C"]]), "RANGE"); | ||||
|     this.setOutput(true, Types.NUMBER.typeId); | ||||
|     this.setOutput(true, Types.DECIMAL.typeName); | ||||
|     this.setColour(getColour().sensebox); | ||||
|     this.setTooltip(Blockly.Msg.senseBox_bmx055_accelerometer_tip); | ||||
|     this.setHelpUrl('https://edu.books.sensebox.de/de/projekte/diy_umweltstation/temp_und_luftfeuchte.html'); | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     return Types.NUMBER.typeId; | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| /** | ||||
| @ -92,14 +82,11 @@ Blockly.Blocks['sensebox_sensor_sds011'] = { | ||||
|       .appendField(new Blockly.FieldDropdown([[Blockly.Msg.senseBox_sds011_pm25, "Pm25"], [Blockly.Msg.senseBox_sds011_pm10, "Pm10"]]), "NAME") | ||||
|       .appendField(Blockly.Msg.senseBox_sds011_dimension) | ||||
|       .appendField(new Blockly.FieldDropdown([[Blockly.Msg.senseBox_sds011_serial1, "Serial1"], [Blockly.Msg.senseBox_sds011_serial2, "Serial2"]]), "SERIAL"); | ||||
|     this.setOutput(true, Types.NUMBER.typeId); | ||||
|     this.setOutput(true, Types.DECIMAL.typeName); | ||||
|     this.setColour(getColour().sensebox); | ||||
|     this.setTooltip(Blockly.Msg.senseBox_sds011_tip); | ||||
|     this.setHelpUrl('https://edu.books.sensebox.de/de/projekte/diy_umweltstation/temp_und_luftfeuchte.html'); | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     return Blockly.Types.DECIMAL; | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| @ -122,11 +109,9 @@ Blockly.Blocks['sensebox_sensor_pressure'] = { | ||||
|       .appendField(Blockly.Msg.senseBox_value) | ||||
|       .appendField(dropdown, "NAME"); | ||||
|     this.setColour(getColour().sensebox); | ||||
|     this.setOutput(true, Types.NUMBER.typeId); | ||||
|     this.setOutput(true, Types.DECIMAL.typeName); | ||||
|     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. | ||||
| @ -170,15 +155,7 @@ Blockly.Blocks['sensebox_sensor_pressure'] = { | ||||
|     if ((input === 'Pressure' || input === 'Temperature') && extraFieldExist !== null) { | ||||
|       this.removeInput('extraField'); | ||||
|     } | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     var input = this.getFieldValue('NAME'); | ||||
|     if (input === 'Temperature') { | ||||
|       return Types.DECIMAL.typeId; | ||||
|     } else { | ||||
|       return Types.LARGE_NUMBER.typeId; | ||||
|     } | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| @ -196,13 +173,10 @@ Blockly.Blocks['sensebox_sensor_bme680_bsec'] = { | ||||
|       .setAlign(Blockly.ALIGN_RIGHT) | ||||
|       .appendField(Blockly.Msg.senseBox_value) | ||||
|       .appendField(new Blockly.FieldDropdown(dropdownOptions), "dropdown") | ||||
|     this.setOutput(true, Types.NUMBER.typeId); | ||||
|     this.setOutput(true, Types.DECIMAL.typeName); | ||||
|     this.setColour(getColour().sensebox); | ||||
|     this.setTooltip(Blockly.Msg.senseBox_bme_tip); | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     return Types.DECIMAL.typeId; | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| /** | ||||
| @ -234,7 +208,7 @@ Blockly.Blocks['sensebox_sensor_ultrasonic_ranger'] = { | ||||
|       .appendField(Blockly.Msg.senseBox_ultrasonic_echo) | ||||
|       .appendField(new Blockly.FieldDropdown( | ||||
|         selectedBoard().digitalPins), 'ultrasonic_echo'); | ||||
|     this.setOutput(true, 'int'); | ||||
|     this.setOutput(true, Types.NUMBER.typeName); | ||||
|     this.setTooltip(Blockly.Msg.senseBox_ultrasonic_tip); | ||||
|     this.setHelpUrl('https://sensebox.de/books'); | ||||
|   }, | ||||
| @ -283,10 +257,6 @@ Blockly.Blocks['sensebox_sensor_ultrasonic_ranger'] = { | ||||
|       default: | ||||
|         break; | ||||
|     } | ||||
|   }, | ||||
| 
 | ||||
|   getBlockType: function () { | ||||
|     return Blockly.Types.SHORT_NUMBER; | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| @ -303,13 +273,10 @@ Blockly.Blocks['sensebox_sensor_sound'] = { | ||||
|       .appendField(Blockly.Msg.senseBox_sound) | ||||
|       .appendField("Pin:") | ||||
|       .appendField(new Blockly.FieldDropdown(selectedBoard().analogPins), "PIN") | ||||
|     this.setOutput(true, Types.NUMBER.typeId); | ||||
|     this.setOutput(true, Types.NUMBER.typeName); | ||||
|     this.setHelpUrl(Blockly.Msg.senseBox_sound_tip); | ||||
|     this.setTooltip('Dieser Sensor mist den Geräuschpegel.'); | ||||
|   }, | ||||
|   getBlockType: function () { | ||||
|     return Types.NUMBER.typeId; | ||||
|   }, | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| import Blockly, { FieldVariable } from 'blockly/core'; | ||||
| import Blockly from 'blockly/core'; | ||||
| import { getColour } from '../helpers/colour'; | ||||
| import { getCompatibleTypes } from '../helpers/types' | ||||
| 
 | ||||
| @ -12,12 +12,14 @@ Blockly.Blocks['variables_set_dynamic'] = { | ||||
|         this.setNextStatement(true, null); | ||||
|         this.appendValueInput('VALUE') | ||||
|             .appendField('set', 'set') | ||||
|             .appendField('', 'type') | ||||
|             .appendField(new Blockly.FieldVariable('VAR'), 'VAR') | ||||
|             .appendField('to'); | ||||
|     }, | ||||
|     onchange: function (e) { | ||||
|         let variableID = this.getFieldValue('VAR'); | ||||
|         let variable = Blockly.getMainWorkspace().getVariableMap().getVariableById(variableID) | ||||
|         this.getField('type').setValue(variable.type); | ||||
|         this.getInput('VALUE').setCheck(getCompatibleTypes(variable.type)); | ||||
| 
 | ||||
|     } | ||||
|  | ||||
| @ -14,20 +14,18 @@ export const CHARACTER = { | ||||
|     typeId: 'Character', | ||||
|     typeName: 'char', | ||||
|     typeMsgName: 'ARD_TYPE_CHAR', | ||||
|     compatibleTypes: [] | ||||
| } | ||||
| 
 | ||||
| export const BOOLEAN = { | ||||
|     typeId: 'Boolean', | ||||
|     typeMsgName: 'ARD_TYPE_BOOL', | ||||
|     compatibleTypes: ['Boolean'] | ||||
| } | ||||
| 
 | ||||
| /** Text string. */ | ||||
| export const TEXT = { | ||||
|     typeId: 'Text', | ||||
|     typeName: 'String', | ||||
|     typeMsgName: 'ARD_TYPE_TEXT', | ||||
|     compatibleTypes: [] | ||||
| } | ||||
| 
 | ||||
| /** Short integer number. */ | ||||
| @ -35,36 +33,33 @@ export const SHORT_NUMBER = { | ||||
|     typeId: 'Short_Number', | ||||
|     typeName: 'int', | ||||
|     typeMsgName: 'ARD_TYPE_SHORT', | ||||
|     compatibleTypes: ['int', 'float',]    // Circular dependencies, add after all declarations
 | ||||
| } | ||||
| 
 | ||||
| /** Integer number. */ | ||||
| export const NUMBER = { | ||||
|     typeId: 'int', | ||||
|     typeId: 'Number', | ||||
|     typeName: 'int', | ||||
|     typeMsgName: 'ARD_TYPE_NUMBER', | ||||
|     compatibleTypes: ['Number']    // Circular dependencies, add after all declarations
 | ||||
| } | ||||
| 
 | ||||
| /** Large integer number. */ | ||||
| export const LARGE_NUMBER = { | ||||
|     typeId: 'Large Number', | ||||
|     typeName: 'long', | ||||
|     typeMsgName: 'ARD_TYPE_LONG', | ||||
|     compatibleTypes: []    // Circular dependencies, add after all declarations
 | ||||
| } | ||||
| 
 | ||||
| /** Decimal/floating point number. */ | ||||
| export const DECIMAL = { | ||||
|     typeId: 'float', | ||||
|     typeId: 'Decimal', | ||||
|     typeName: 'float', | ||||
|     typeMsgName: 'ARD_TYPE_DECIMAL', | ||||
|     compatibleTypes: [BOOLEAN.typeId, | ||||
|     NUMBER.typeId, | ||||
|     SHORT_NUMBER.typeId, | ||||
|     LARGE_NUMBER.typeId] | ||||
| } | ||||
| 
 | ||||
| /** Array/List of items. */ | ||||
| export const ARRAY = { | ||||
|     typeId: 'Array', | ||||
|     typeName: 'array', | ||||
|     typeMsgName: 'ARD_TYPE_ARRAY', | ||||
|     compatibleTypes: [] | ||||
| } | ||||
| @ -72,15 +67,15 @@ export const ARRAY = { | ||||
| /** Null indicate there is no type. */ | ||||
| export const NULL = { | ||||
|     typeId: 'Null', | ||||
|     typeName: 'void', | ||||
|     typeMsgName: 'ARD_TYPE_NULL', | ||||
|     compatibleTypes: [] | ||||
| } | ||||
| 
 | ||||
| /** Type not defined, or not yet defined. */ | ||||
| export const UNDEF = { | ||||
|     typeId: 'Undefined', | ||||
|     typeName: 'undef', | ||||
|     typeMsgName: 'ARD_TYPE_UNDEF', | ||||
|     compatibleTypes: [] | ||||
| } | ||||
| 
 | ||||
| /** Set when no child block (meant to define the variable type) is connected. */ | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user