add block type to setter and getter

This commit is contained in:
Mario
2020-09-29 10:30:35 +02:00
parent da4c8840ea
commit 5304907fd0
7 changed files with 237 additions and 341 deletions
+37 -163
View File
@@ -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.)
onchange: function (e) {
let variableID = this.getFieldValue('VAR');
let variable = Blockly.getMainWorkspace().getVariableMap().getVariableById(variableID)
this.getField('type').setValue(variable.type);
}
}