初始化提交
This commit is contained in:
1163
boards/default_src/python/blocks/class.js
Normal file
1163
boards/default_src/python/blocks/class.js
Normal file
File diff suppressed because it is too large
Load Diff
993
boards/default_src/python/blocks/control.js
Normal file
993
boards/default_src/python/blocks/control.js
Normal file
@@ -0,0 +1,993 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const LOOPS_HUE = 120;
|
||||
|
||||
export const controls_main = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_NAME_MAIN);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField('');
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_CONTROL_SETUP);
|
||||
}
|
||||
};
|
||||
|
||||
export const base_setup = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_SETUP);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField('');
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_CONTROL_SETUP);
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_delay = {
|
||||
init: function () {
|
||||
var UNIT = [
|
||||
[Blockly.Msg.MIXLY_mSecond, 'delay'],
|
||||
[Blockly.Msg.MIXLY_uSecond, 'delayMicroseconds']
|
||||
];
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendValueInput("DELAY_TIME", Number)
|
||||
.appendField(Blockly.Msg.MIXLY_DELAY)
|
||||
.appendField(new Blockly.FieldDropdown(UNIT), 'UNIT')
|
||||
.setCheck(Number);
|
||||
this.setPreviousStatement(true, null);
|
||||
this.setNextStatement(true, null);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_CONTROL_DELAY);
|
||||
this.setHelpUrl("https://mixly.readthedocs.io/zh_CN/latest/arduino/03.Control.html#id9");
|
||||
this.wiki = {
|
||||
'zh-hans': {
|
||||
page: ['Arduino AVR', '控制', '延时']
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_end_program = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_CONTROL_END_PROGRAM);
|
||||
this.setPreviousStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_CONTROL_END_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_if = {
|
||||
/**
|
||||
* Block for if/elseif/else condition.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
//this.setHelpUrl(Blockly.Msg.CONTROLS_IF_HELPURL);
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendValueInput('IF0')
|
||||
.setCheck([Boolean, Number])
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_MSG_IF);
|
||||
this.appendStatementInput('DO0')
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setHelpUrl("https://mixly.readthedocs.io/zh_CN/latest/arduino/03.Control.html#if");
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['controls_if_elseif',
|
||||
'controls_if_else'], this));
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
if (!thisBlock.elseifCount_ && !thisBlock.elseCount_) {
|
||||
return Blockly.Msg.CONTROLS_IF_TOOLTIP_1;
|
||||
} else if (!thisBlock.elseifCount_ && thisBlock.elseCount_) {
|
||||
return Blockly.Msg.CONTROLS_IF_TOOLTIP_2;
|
||||
} else if (thisBlock.elseifCount_ && !thisBlock.elseCount_) {
|
||||
return Blockly.Msg.CONTROLS_IF_TOOLTIP_3;
|
||||
} else if (thisBlock.elseifCount_ && thisBlock.elseCount_) {
|
||||
return Blockly.Msg.CONTROLS_IF_TOOLTIP_4;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
this.elseifCount_ = 0;
|
||||
this.elseCount_ = 0;
|
||||
},
|
||||
/**
|
||||
* Create XML to represent the number of else-if and else inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
if (!this.elseifCount_ && !this.elseCount_) {
|
||||
return null;
|
||||
}
|
||||
var container = document.createElement('mutation');
|
||||
if (this.elseifCount_) {
|
||||
container.setAttribute('elseif', this.elseifCount_);
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
container.setAttribute('else', 1);
|
||||
}
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the else-if and else inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
var containerBlock = this;
|
||||
var valueConnections = [];
|
||||
var statementConnections = [];
|
||||
// var elseStatementConnection = null;
|
||||
if (this.elseCount_) {
|
||||
// if (containerBlock.getInputTargetBlock('ELSE') && containerBlock.getInputTargetBlock('ELSE').previousConnection)
|
||||
// elseStatementConnection = containerBlock.getInputTargetBlock('ELSE').previousConnection;
|
||||
this.removeInput('ELSE');
|
||||
}
|
||||
for (var i = this.elseifCount_; i > 0; i--) {
|
||||
if (containerBlock.getInputTargetBlock('IF' + i) && containerBlock.getInputTargetBlock('IF' + i).previousConnection)
|
||||
valueConnections[i] = (containerBlock.getInputTargetBlock('IF' + i).previousConnection);
|
||||
else
|
||||
valueConnections[i] = null;
|
||||
this.removeInput('IF' + i);
|
||||
if (containerBlock.getInputTargetBlock('DO' + i) && containerBlock.getInputTargetBlock('DO' + i).previousConnection)
|
||||
statementConnections[i] = (containerBlock.getInputTargetBlock('DO' + i).previousConnection);
|
||||
else
|
||||
statementConnections[i] = null;
|
||||
this.removeInput('DO' + i);
|
||||
}
|
||||
this.elseifCount_ = parseInt(xmlElement.getAttribute('elseif'), 10);
|
||||
this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10);
|
||||
//this.compose(containerBlock);
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
this.appendValueInput('IF' + i)
|
||||
.setCheck([Boolean, Number])
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSEIF);
|
||||
this.appendStatementInput('DO' + i)
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_MSG_THEN);
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
this.appendStatementInput('ELSE')
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE);
|
||||
}
|
||||
for (var i = valueConnections.length - 2; i > 0; i--) {
|
||||
if (valueConnections[i])
|
||||
valueConnections[i].reconnect(this, 'IF' + i);
|
||||
}
|
||||
for (var i = statementConnections.length - 2; i > 0; i--) {
|
||||
if (statementConnections[i])
|
||||
statementConnections[i].reconnect(this, 'DO' + i);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock = workspace.newBlock('controls_if_if');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
var elseifBlock = workspace.newBlock('controls_if_elseif');
|
||||
elseifBlock.initSvg();
|
||||
connection.connect(elseifBlock.previousConnection);
|
||||
connection = elseifBlock.nextConnection;
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
var elseBlock = workspace.newBlock('controls_if_else');
|
||||
elseBlock.initSvg();
|
||||
connection.connect(elseBlock.previousConnection);
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
// Disconnect the else input blocks and remove the inputs.
|
||||
if (this.elseCount_) {
|
||||
this.removeInput('ELSE');
|
||||
}
|
||||
this.elseCount_ = 0;
|
||||
// Disconnect all the elseif input blocks and remove the inputs.
|
||||
for (var i = this.elseifCount_; i > 0; i--) {
|
||||
this.removeInput('IF' + i);
|
||||
this.removeInput('DO' + i);
|
||||
}
|
||||
this.elseifCount_ = 0;
|
||||
// Rebuild the block's optional inputs.
|
||||
var clauseBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var valueConnections = [null];
|
||||
var statementConnections = [null];
|
||||
var elseStatementConnection = null;
|
||||
while (clauseBlock) {
|
||||
switch (clauseBlock.type) {
|
||||
case 'controls_if_elseif':
|
||||
this.elseifCount_++;
|
||||
valueConnections.push(clauseBlock.valueConnection_);
|
||||
statementConnections.push(clauseBlock.statementConnection_);
|
||||
break;
|
||||
case 'controls_if_else':
|
||||
this.elseCount_++;
|
||||
elseStatementConnection = clauseBlock.statementConnection_;
|
||||
break;
|
||||
default:
|
||||
throw Error('Unknown block type: ' + clauseBlock.type);
|
||||
}
|
||||
clauseBlock = clauseBlock.nextConnection &&
|
||||
clauseBlock.nextConnection.targetBlock();
|
||||
}
|
||||
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
this.reconnectChildBlocks_(valueConnections, statementConnections, elseStatementConnection);
|
||||
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var clauseBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 1;
|
||||
while (clauseBlock) {
|
||||
switch (clauseBlock.type) {
|
||||
case 'controls_if_elseif':
|
||||
var inputIf = this.getInput('IF' + i);
|
||||
var inputDo = this.getInput('DO' + i);
|
||||
clauseBlock.valueConnection_ =
|
||||
inputIf && inputIf.connection.targetConnection;
|
||||
clauseBlock.statementConnection_ =
|
||||
inputDo && inputDo.connection.targetConnection;
|
||||
i++;
|
||||
break;
|
||||
case 'controls_if_else':
|
||||
var inputDo = this.getInput('ELSE');
|
||||
clauseBlock.statementConnection_ =
|
||||
inputDo && inputDo.connection.targetConnection;
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown block type.';
|
||||
}
|
||||
clauseBlock = clauseBlock.nextConnection &&
|
||||
clauseBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Reconstructs the block with all child blocks attached.
|
||||
*/
|
||||
rebuildShape_: function () {
|
||||
var valueConnections = [null];
|
||||
var statementConnections = [null];
|
||||
var elseStatementConnection = null;
|
||||
|
||||
if (this.getInput('ELSE')) {
|
||||
elseStatementConnection = this.getInput('ELSE').connection.targetConnection;
|
||||
}
|
||||
var i = 1;
|
||||
while (this.getInput('IF' + i)) {
|
||||
var inputIf = this.getInput('IF' + i);
|
||||
var inputDo = this.getInput('DO' + i);
|
||||
console.log(inputIf.connection.targetConnection);
|
||||
valueConnections.push(inputIf.connection.targetConnection);
|
||||
statementConnections.push(inputDo.connection.targetConnection);
|
||||
i++;
|
||||
}
|
||||
this.updateShape_();
|
||||
this.reconnectChildBlocks_(valueConnections, statementConnections, elseStatementConnection);
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @this Blockly.Block
|
||||
* @private
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('ELSE')) {
|
||||
this.removeInput('ELSE');
|
||||
}
|
||||
var i = 1;
|
||||
while (this.getInput('IF' + i)) {
|
||||
this.removeInput('IF' + i);
|
||||
this.removeInput('DO' + i);
|
||||
i++;
|
||||
}
|
||||
// Rebuild block.
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
this.appendValueInput('IF' + i)
|
||||
.setCheck([Number, Boolean])
|
||||
.appendField(Blockly.Msg['CONTROLS_IF_MSG_ELSEIF']);
|
||||
this.appendStatementInput('DO' + i)
|
||||
.appendField(Blockly.Msg['CONTROLS_IF_MSG_THEN']);
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
this.appendStatementInput('ELSE')
|
||||
.appendField(Blockly.Msg['CONTROLS_IF_MSG_ELSE']);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Reconnects child blocks.
|
||||
* @param {!Array<?Blockly.RenderedConnection>} valueConnections List of value
|
||||
* connectsions for if input.
|
||||
* @param {!Array<?Blockly.RenderedConnection>} statementConnections List of
|
||||
* statement connections for do input.
|
||||
* @param {?Blockly.RenderedConnection} elseStatementConnection Statement
|
||||
* connection for else input.
|
||||
*/
|
||||
reconnectChildBlocks_: function (valueConnections, statementConnections,
|
||||
elseStatementConnection) {
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
valueConnections[i] && valueConnections[i].reconnect(this, 'IF' + i);
|
||||
statementConnections[i] && statementConnections[i].reconnect(this, 'DO' + i);
|
||||
}
|
||||
elseStatementConnection && elseStatementConnection.reconnect(this, 'ELSE');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const controls_range = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendValueInput('FROM')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.PYTHON_RANGE)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_FROM);
|
||||
this.appendValueInput('TO')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_TO);
|
||||
this.appendValueInput('STEP')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.PYTHON_RANGE_STEP);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_CONTROLS_RANGE_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const controls_forEach = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendValueInput('LIST')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.CONTROLS_FOREACH_INPUT);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.CONTROLS_FOREACH_INPUT_ITEM)
|
||||
// .appendField(new Blockly.FieldTextInput('i'), 'VAR');
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.MIXLY_DO);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(function () {
|
||||
return Blockly.Msg.CONTROLS_FOR_TOOLTIP.replace('“%1”', '');
|
||||
});
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_whileUntil = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendValueInput('BOOL')
|
||||
.setCheck([Boolean, Number])
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_CURRENT)
|
||||
.appendField(new Blockly.FieldDropdown(this.OPERATORS), 'MODE')
|
||||
// this.appendDummyInput()
|
||||
// .appendField(Blockly.Msg.CONTROLS_WHILE_SHI);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_WHILEUNTIL_TITLE_REPEAT + Blockly.Msg.MIXLY_DO);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var op = thisBlock.getFieldValue('MODE');
|
||||
var TOOLTIPS = {
|
||||
'WHILE': Blockly.Msg.CONTROLS_WHILEUNTIL_TOOLTIP_WHILE,
|
||||
'UNTIL': Blockly.Msg.CONTROLS_WHILEUNTIL_TOOLTIP_UNTIL
|
||||
};
|
||||
return TOOLTIPS[op];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_try_finally = {
|
||||
/**
|
||||
* Block for if/elseif/else condition.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_TRY);
|
||||
this.appendStatementInput('try');
|
||||
this.appendValueInput('IF1')
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_EXCEPT);
|
||||
this.appendStatementInput('DO1')
|
||||
.appendField('');
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['controls_except', 'controls_finally'], this));
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_CONTROL_TRY_TOOLTIP);
|
||||
this.elseifCount_ = 1;
|
||||
this.elseCount_ = 0;
|
||||
},
|
||||
/**
|
||||
* Create XML to represent the number of else-if and else inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
if (!this.elseifCount_ && !this.elseCount_) {
|
||||
return null;
|
||||
}
|
||||
var container = document.createElement('mutation');
|
||||
if (this.elseifCount_) {
|
||||
container.setAttribute('elseif', this.elseifCount_);
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
container.setAttribute('else', 1);
|
||||
}
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the else-if and else inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
var containerBlock = this;
|
||||
var valueConnections = [];
|
||||
var statementConnections = [];
|
||||
// var elseStatementConnection = null;
|
||||
if (this.elseCount_) {
|
||||
// if (containerBlock.getInputTargetBlock('ELSE') && containerBlock.getInputTargetBlock('ELSE').previousConnection)
|
||||
// elseStatementConnection = containerBlock.getInputTargetBlock('ELSE').previousConnection;
|
||||
this.removeInput('ELSE');
|
||||
}
|
||||
for (var i = this.elseifCount_; i > 0; i--) {
|
||||
if (containerBlock.getInputTargetBlock('IF' + i) && containerBlock.getInputTargetBlock('IF' + i).previousConnection)
|
||||
valueConnections[i] = (containerBlock.getInputTargetBlock('IF' + i).previousConnection);
|
||||
else
|
||||
valueConnections[i] = null;
|
||||
this.removeInput('IF' + i);
|
||||
if (containerBlock.getInputTargetBlock('DO' + i) && containerBlock.getInputTargetBlock('DO' + i).previousConnection)
|
||||
statementConnections[i] = (containerBlock.getInputTargetBlock('DO' + i).previousConnection);
|
||||
else
|
||||
statementConnections[i] = null;
|
||||
this.removeInput('DO' + i);
|
||||
}
|
||||
this.elseifCount_ = parseInt(xmlElement.getAttribute('elseif'), 10);
|
||||
this.elseCount_ = parseInt(xmlElement.getAttribute('else'), 10);
|
||||
//this.compose(containerBlock);
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
this.appendValueInput('IF' + i)
|
||||
.setCheck([Boolean, Number])
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_EXCEPT);
|
||||
this.appendStatementInput('DO' + i)
|
||||
.appendField("");
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
this.appendStatementInput('ELSE')
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_FINALLY);
|
||||
}
|
||||
for (var i = valueConnections.length - 2; i > 0; i--) {
|
||||
if (valueConnections[i])
|
||||
valueConnections[i].reconnect(this, 'IF' + i);
|
||||
}
|
||||
for (var i = statementConnections.length - 2; i > 0; i--) {
|
||||
if (statementConnections[i])
|
||||
statementConnections[i].reconnect(this, 'DO' + i);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock = workspace.newBlock('controls_try');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
var elseifBlock = workspace.newBlock('controls_except');
|
||||
elseifBlock.initSvg();
|
||||
connection.connect(elseifBlock.previousConnection);
|
||||
connection = elseifBlock.nextConnection;
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
var elseBlock = workspace.newBlock('controls_finally');
|
||||
elseBlock.initSvg();
|
||||
connection.connect(elseBlock.previousConnection);
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
// Disconnect the else input blocks and remove the inputs.
|
||||
if (this.elseCount_) {
|
||||
this.removeInput('ELSE');
|
||||
}
|
||||
this.elseCount_ = 0;
|
||||
// Disconnect all the elseif input blocks and remove the inputs.
|
||||
for (var i = this.elseifCount_; i > 0; i--) {
|
||||
this.removeInput('IF' + i);
|
||||
this.removeInput('DO' + i);
|
||||
}
|
||||
this.elseifCount_ = 0;
|
||||
// Rebuild the block's optional inputs.
|
||||
var clauseBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var valueConnections = [null];
|
||||
var statementConnections = [null];
|
||||
var elseStatementConnection = null;
|
||||
while (clauseBlock) {
|
||||
switch (clauseBlock.type) {
|
||||
case 'controls_except':
|
||||
this.elseifCount_++;
|
||||
valueConnections.push(clauseBlock.valueConnection_);
|
||||
statementConnections.push(clauseBlock.statementConnection_);
|
||||
break;
|
||||
case 'controls_finally':
|
||||
this.elseCount_++;
|
||||
elseStatementConnection = clauseBlock.statementConnection_;
|
||||
break;
|
||||
default:
|
||||
throw Error('Unknown block type: ' + clauseBlock.type);
|
||||
}
|
||||
clauseBlock = clauseBlock.nextConnection &&
|
||||
clauseBlock.nextConnection.targetBlock();
|
||||
}
|
||||
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
this.reconnectChildBlocks_(valueConnections, statementConnections, elseStatementConnection);
|
||||
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var clauseBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 1;
|
||||
while (clauseBlock) {
|
||||
switch (clauseBlock.type) {
|
||||
case 'controls_except':
|
||||
var inputIf = this.getInput('IF' + i);
|
||||
var inputDo = this.getInput('DO' + i);
|
||||
clauseBlock.valueConnection_ =
|
||||
inputIf && inputIf.connection.targetConnection;
|
||||
clauseBlock.statementConnection_ =
|
||||
inputDo && inputDo.connection.targetConnection;
|
||||
i++;
|
||||
break;
|
||||
case 'controls_finally':
|
||||
var inputDo = this.getInput('ELSE');
|
||||
clauseBlock.statementConnection_ =
|
||||
inputDo && inputDo.connection.targetConnection;
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown block type.';
|
||||
}
|
||||
clauseBlock = clauseBlock.nextConnection &&
|
||||
clauseBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Reconstructs the block with all child blocks attached.
|
||||
*/
|
||||
rebuildShape_: function () {
|
||||
var valueConnections = [null];
|
||||
var statementConnections = [null];
|
||||
var elseStatementConnection = null;
|
||||
|
||||
if (this.getInput('ELSE')) {
|
||||
elseStatementConnection = this.getInput('ELSE').connection.targetConnection;
|
||||
}
|
||||
var i = 1;
|
||||
while (this.getInput('IF' + i)) {
|
||||
var inputIf = this.getInput('IF' + i);
|
||||
var inputDo = this.getInput('DO' + i);
|
||||
console.log(inputIf.connection.targetConnection);
|
||||
valueConnections.push(inputIf.connection.targetConnection);
|
||||
statementConnections.push(inputDo.connection.targetConnection);
|
||||
i++;
|
||||
}
|
||||
this.updateShape_();
|
||||
this.reconnectChildBlocks_(valueConnections, statementConnections, elseStatementConnection);
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @this Blockly.Block
|
||||
* @private
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('ELSE')) {
|
||||
this.removeInput('ELSE');
|
||||
}
|
||||
var i = 1;
|
||||
while (this.getInput('IF' + i)) {
|
||||
this.removeInput('IF' + i);
|
||||
this.removeInput('DO' + i);
|
||||
i++;
|
||||
}
|
||||
// Rebuild block.
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
this.appendValueInput('IF' + i)
|
||||
.setCheck([Number, Boolean])
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_EXCEPT);
|
||||
this.appendStatementInput('DO' + i)
|
||||
.appendField('');
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
this.appendStatementInput('ELSE')
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_FINALLY);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Reconnects child blocks.
|
||||
* @param {!Array<?Blockly.RenderedConnection>} valueConnections List of value
|
||||
* connectsions for if input.
|
||||
* @param {!Array<?Blockly.RenderedConnection>} statementConnections List of
|
||||
* statement connections for do input.
|
||||
* @param {?Blockly.RenderedConnection} elseStatementConnection Statement
|
||||
* connection for else input.
|
||||
*/
|
||||
reconnectChildBlocks_: function (valueConnections, statementConnections,
|
||||
elseStatementConnection) {
|
||||
for (var i = 1; i <= this.elseifCount_; i++) {
|
||||
valueConnections[i] && valueConnections[i].reconnect(this, 'IF' + i);
|
||||
statementConnections[i] && statementConnections[i].reconnect(this, 'DO' + i);
|
||||
}
|
||||
elseStatementConnection && elseStatementConnection.reconnect(this, 'ELSE');
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_flow_statements = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
var dropdown = new Blockly.FieldDropdown(this.OPERATORS);
|
||||
this.appendDummyInput()
|
||||
.appendField(dropdown, 'FLOW')
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FLOW_STATEMENTS_INPUT_OFLOOP);
|
||||
this.setPreviousStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_CONTROLS_FLOW_STATEMENTS_TOOLTIP);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var op = thisBlock.getFieldValue('FLOW');
|
||||
var TOOLTIPS = {
|
||||
'BREAK': Blockly.Msg.CONTROLS_FLOW_STATEMENTS_TOOLTIP_BREAK,
|
||||
'CONTINUE': Blockly.Msg.CONTROLS_FLOW_STATEMENTS_TOOLTIP_CONTINUE
|
||||
};
|
||||
return TOOLTIPS[op];
|
||||
});
|
||||
},
|
||||
onchange: function () {
|
||||
if (!this.workspace) {
|
||||
// Block has been deleted.
|
||||
return;
|
||||
}
|
||||
var legal = false;
|
||||
// Is the block nested in a control statement?
|
||||
var block = this;
|
||||
do {
|
||||
if (block.type == 'controls_repeat' ||
|
||||
block.type == 'controls_for' ||
|
||||
block.type == 'controls_forEach' ||
|
||||
block.type == 'controls_repeat_ext' ||
|
||||
block.type == 'controls_whileUntil' ||
|
||||
block.type == 'do_while') {
|
||||
legal = true;
|
||||
break;
|
||||
}
|
||||
block = block.getSurroundParent();
|
||||
} while (block);
|
||||
if (legal) {
|
||||
this.setWarningText(null);
|
||||
} else {
|
||||
this.setWarningText(Blockly.Msg.LANG_CONTROLS_FLOW_STATEMENTS_WARNING);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const controls_for = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_WITH)
|
||||
.appendField(new Blockly.FieldTextInput('i'), 'VAR');
|
||||
this.appendValueInput('FROM')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_FROM);
|
||||
this.appendValueInput('TO')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_TO);
|
||||
this.appendValueInput('STEP')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_STEP);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.MIXLY_DO);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
return Blockly.Msg.CONTROLS_FOR_TOOLTIP.replace('%1',
|
||||
thisBlock.getFieldValue('VAR'));
|
||||
});
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_for_range = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_WITH)
|
||||
.appendField(new Blockly.FieldTextInput('i'), 'VAR');
|
||||
this.appendValueInput('FROM')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_FROM);
|
||||
this.appendValueInput('TO')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_TO);
|
||||
this.appendValueInput('STEP')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_STEP);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.MIXLY_DO);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
return Blockly.Msg.MIXLY_PYTHON_CONTROLS_FOR_RANGE_TOOLTIP.replace('%1',
|
||||
thisBlock.getFieldValue('VAR'));
|
||||
});
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
controls_whileUntil.OPERATORS =
|
||||
[[Blockly.Msg.LANG_CONTROLS_WHILEUNTIL_OPERATOR_WHILE, 'WHILE'],
|
||||
[Blockly.Msg.LANG_CONTROLS_WHILEUNTIL_OPERATOR_UNTIL, 'UNTIL']];
|
||||
|
||||
|
||||
|
||||
controls_flow_statements.OPERATORS =
|
||||
[[Blockly.Msg.LANG_CONTROLS_FLOW_STATEMENTS_OPERATOR_BREAK, 'BREAK'],
|
||||
[Blockly.Msg.LANG_CONTROLS_FLOW_STATEMENTS_OPERATOR_CONTINUE, 'CONTINUE']];
|
||||
|
||||
|
||||
|
||||
export const controls_if_if = {
|
||||
/**
|
||||
* Mutator block for if container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_IF_TITLE_IF);
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.CONTROLS_IF_IF_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_if_elseif = {
|
||||
/**
|
||||
* Mutator bolck for else-if condition.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_ELSEIF_TITLE_ELSEIF);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.CONTROLS_IF_ELSEIF_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_if_else = {
|
||||
/**
|
||||
* Mutator block for else condition.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.CONTROLS_IF_ELSE_TITLE_ELSE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setTooltip(Blockly.Msg.CONTROLS_IF_ELSE_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
export const controls_try = {
|
||||
/**
|
||||
* Mutator block for if container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('try');
|
||||
this.appendStatementInput('STACK');
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_except = {
|
||||
/**
|
||||
* Mutator bolck for else-if condition.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_EXCEPT);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.contextMenu = false;
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_CONTROL_EXCEPT_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_finally = {
|
||||
/**
|
||||
* Mutator block for else condition.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_FINALLY);
|
||||
this.setPreviousStatement(true);
|
||||
this.contextMenu = false;
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_CONTROL_FINALLY_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
export const controls_repeat_ext = {
|
||||
/**
|
||||
* Block for repeat n times (external number).
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.jsonInit({
|
||||
"message0": Blockly.Msg.CONTROLS_REPEAT_TITLE,
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "TIMES",
|
||||
// "check": "Number"
|
||||
}
|
||||
],
|
||||
"previousStatement": null,
|
||||
"nextStatement": null,
|
||||
"colour": LOOPS_HUE,
|
||||
"tooltip": Blockly.Msg.CONTROLS_REPEAT_TOOLTIP,
|
||||
"helpUrl": Blockly.Msg.CONTROLS_REPEAT_HELPURL
|
||||
});
|
||||
this.appendStatementInput('DO');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
export const controls_lambda = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendValueInput('BOOL')
|
||||
.appendField('lambda')
|
||||
//.appendField(new Blockly.FieldDropdown(this.OPERATORS), 'MODE');
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.MIXLY_STAT);
|
||||
this.setOutput(true);
|
||||
// this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_pass = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_PASS);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_CONTROLS_PASS_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const controls_thread = {
|
||||
init: function () {
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_CONTROLS_THREAD_START)
|
||||
this.appendValueInput('callback')
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_CONTROLS_THREAD_USE)
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_PARAMS);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_CONTROLS_THREAD_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
//do-while循环
|
||||
export const do_while = {
|
||||
init: function () {
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.CONTROLS_REPEAT_TITLE_REPEAT + Blockly.Msg.MIXLY_DO);
|
||||
this.appendStatementInput("input_data")
|
||||
.setCheck(null)
|
||||
this.appendValueInput("select_data")
|
||||
.setCheck(null)
|
||||
.appendField(Blockly.Msg.CONTROLS_OPERATOR_UNTIL)
|
||||
.appendField(new Blockly.FieldDropdown([[Blockly.Msg.LANG_CONTROLS_WHILEUNTIL_OPERATOR_WHILE, "true"], [Blockly.Msg.LANG_CONTROLS_WHILEUNTIL_OPERATOR_UNTIL, "false"]]), "type");
|
||||
this.setPreviousStatement(true, null);
|
||||
this.setNextStatement(true, null);
|
||||
this.setColour(LOOPS_HUE);
|
||||
this.setTooltip("do-while loop");
|
||||
this.setHelpUrl("");
|
||||
}
|
||||
};
|
||||
|
||||
// export const base_type = controls_type;
|
||||
// export const controls_TypeLists = controls_typeLists;
|
||||
743
boards/default_src/python/blocks/dicts.js
Normal file
743
boards/default_src/python/blocks/dicts.js
Normal file
@@ -0,0 +1,743 @@
|
||||
/**
|
||||
* @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 Dictionary blocks for Blockly.
|
||||
* @author acbart@vt.edu (Austin Cory Bart)
|
||||
*/
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const DICTS_HUE = 345;
|
||||
|
||||
export const dicts_create_with = {
|
||||
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(new Blockly.FieldTextInput('mydict'), 'VAR')
|
||||
.appendField(new Blockly.FieldLabel(Blockly.Msg.DICTS_CREATE_WITH_INPUT_WITH), 'TIP')
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['dicts_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_TOOLTIP);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('dicts_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('dicts_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
}
|
||||
|
||||
var keyNames = [];
|
||||
for (var i = 0; this.getInput('ADD' + i); i++) {
|
||||
//this.getInput('VALUE' + i).removeField("KEY"+i);
|
||||
keyNames.push(this.getFieldValue("KEY" + i))
|
||||
this.removeInput('ADD' + i);
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.getField('TIP').setValue(Blockly.Msg.DICTS_CREATE_EMPTY_TITLE);
|
||||
} else {
|
||||
this.getField('TIP').setValue(Blockly.Msg.DICTS_CREATE_WITH_INPUT_WITH);
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
this.appendValueInput('ADD' + i)
|
||||
.setCheck(null)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(
|
||||
new Blockly.FieldTextInput(
|
||||
keyNames.length > i
|
||||
? keyNames[i]
|
||||
: (i == 0 ? '"key"' : '"key' + (i + 1) + '"')),
|
||||
'KEY' + i)
|
||||
.appendField(":")
|
||||
}
|
||||
}
|
||||
}, getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
export const dicts_create_with_container = {
|
||||
|
||||
/**
|
||||
* Mutator block for list container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT);
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_CONTAINER_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_create_with_item = {
|
||||
/**
|
||||
* Mutator bolck for adding items.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.DICTS_CREATE_WITH_ITEM_TITLE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_ITEM_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_keys = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.DICT_KEYS);
|
||||
this.setTooltip(Blockly.Msg.DICTS_KEYS_TOOLTIP);
|
||||
this.setOutput(true, 'List');
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_get = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
// this.appendDummyInput("")
|
||||
|
||||
// .appendField(Blockly.Msg.DICTS_GET_FROM_DICTS)
|
||||
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendValueInput('KEY')
|
||||
.appendField(Blockly.Msg.DICTS_GET_IN)
|
||||
this.appendDummyInput("")
|
||||
// .appendField(new Blockly.FieldTextInput('"key"'), 'KEY')
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.DICTS_GET_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_get_default = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendValueInput('KEY')
|
||||
.appendField(Blockly.Msg.DICTS_GET_IN)
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.DICTS_DEFAULT_VALUE);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.DICTS_GET_DEFAULT_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_add_or_change = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
this.appendValueInput('KEY')
|
||||
.appendField(Blockly.Msg.DICTS_ADD)
|
||||
// .appendField(new Blockly.FieldTextInput('"key"'), 'KEY')
|
||||
this.appendDummyInput()
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.DICTS_ADD_OR_CHANGE_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const dicts_delete = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
this.appendValueInput('KEY')
|
||||
.appendField(Blockly.Msg.DICTS_DELETE_IN)
|
||||
this.appendDummyInput("")
|
||||
// .appendField(new Blockly.FieldTextInput('"key"'), 'KEY')
|
||||
.appendField(Blockly.Msg.DICTS_DELETE_VALUE);
|
||||
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.DICTS_DELETE_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const dicts_update = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT2')
|
||||
.setCheck('Dict')
|
||||
.appendField(Blockly.Msg.MAKE_DICT)
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
.appendField(Blockly.Msg.DICT_UPDATE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_MID);
|
||||
this.setTooltip(Blockly.Msg.DICTS_UPDATE_TOOLTIP);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_clear = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.DICT_CLEAR);
|
||||
this.setTooltip(Blockly.Msg.DICTS_CLEAR_TOOLTIP);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_items = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
|
||||
.appendField(Blockly.Msg.DICT_ITEMS);
|
||||
this.setTooltip(Blockly.Msg.DICTS_ITEMS_TOOLTIP);
|
||||
this.setOutput(true, 'List');
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_values = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
|
||||
.appendField(Blockly.Msg.DICT_VALUES);
|
||||
this.setTooltip(Blockly.Msg.DICTS_VALUES_TOOLTIP);
|
||||
this.setOutput(true, 'List');
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_length = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_LENGTH)
|
||||
|
||||
this.setTooltip(Blockly.Msg.DICT_LENGTH_TOOLTIP);
|
||||
this.setOutput(true, Number);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_deldict = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
|
||||
.appendField(Blockly.Msg.DICT_DELDICT);
|
||||
this.setTooltip(Blockly.Msg.DICTS_DEL_TOOLTIP);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_add_change_del = {
|
||||
/**
|
||||
* Block for getting sublist.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
|
||||
this['MODE'] = [
|
||||
[Blockly.Msg.DICTS_ADD_OR_CHANGE, 'INSERT'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_JS_DELETE_VAR, 'DELETE']
|
||||
];
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput('AT2')
|
||||
this.appendValueInput('KEY')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKE)
|
||||
// .appendField(new Blockly.FieldTextInput('"key"'), 'KEY')
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.updateAt_(true);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(false);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
var b = this;
|
||||
this.setTooltip(function () {
|
||||
var e = b.getFieldValue("WHERE"),
|
||||
d = "";
|
||||
switch (e) {
|
||||
case "INSERT":
|
||||
d = Blockly.Msg.DICTS_ADD_TOOLTIP;
|
||||
break;
|
||||
case "DELETE":
|
||||
d = Blockly.Msg.DICTS_DELETE_TOOLTIP;
|
||||
break;
|
||||
}
|
||||
//if ("FROM_START" == e || "FROM_END" == e) d += " " + Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP.replace("%1", ONE_BASED_INDEXING ? "#1": "#0");
|
||||
return d
|
||||
})
|
||||
|
||||
},
|
||||
/**
|
||||
* Create XML to represent whether there are 'AT' inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
var isAt = this.getInput('AT2').type == Blockly.INPUT_VALUE;
|
||||
container.setAttribute('at2', isAt);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the 'AT' inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
var isAt = (xmlElement.getAttribute('at2') == 'true');
|
||||
this.updateAt_(isAt);
|
||||
},
|
||||
/**
|
||||
* Create or delete an input for a numeric index.
|
||||
* This block has two such inputs, independant of each other.
|
||||
* @param {number} n Specify first or second input (1 or 2).
|
||||
* @param {boolean} isAt True if the input should exist.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateAt_: function (isAt) {
|
||||
// Create or delete an input for the numeric index.
|
||||
// Destroy old 'AT' and 'ORDINAL' inputs.
|
||||
this.removeInput('AT2');
|
||||
this.removeInput('ORDINAL', true);
|
||||
// Create either a value 'AT' input or a dummy input.
|
||||
if (isAt) {
|
||||
this.appendValueInput('AT2').setCheck(Number);
|
||||
} else {
|
||||
this.appendDummyInput('AT2');
|
||||
}
|
||||
var menu = new Blockly.FieldDropdown(this['MODE'],
|
||||
function (value) {
|
||||
var newAt = (value == 'INSERT');
|
||||
// The 'isAt' variable is available due to this function being a
|
||||
// closure.
|
||||
if (newAt != isAt) {
|
||||
var block = this.sourceBlock_;
|
||||
block.updateAt_(newAt);
|
||||
// This menu has been destroyed and replaced.
|
||||
// Update the replacement.
|
||||
block.setFieldValue(value, 'WHERE');
|
||||
return null;
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
this.getInput('AT2')
|
||||
.appendField(menu, 'WHERE');
|
||||
|
||||
// this.moveInputBefore('AT2','LIST');
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_pop = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_DICT_POP)
|
||||
this.appendValueInput('KEY')
|
||||
this.appendDummyInput("")
|
||||
// .appendField(new Blockly.FieldTextInput('"key"'), 'KEY')
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.setTooltip(Blockly.Msg.DICT_POP_TOOLTIP);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_setdefault = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict');
|
||||
this.appendValueInput('KEY')
|
||||
.appendField(Blockly.Msg.DICTS_SET_DEFAULT)
|
||||
this.appendDummyInput("")
|
||||
// .appendField(new Blockly.FieldTextInput('"key"'), 'KEY')
|
||||
.appendField(Blockly.Msg.DICTS_DEFAULT_VALUE);
|
||||
this.appendValueInput('VAR')
|
||||
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.DICTS_SETDEFAULT_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_create_with_noreturn = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
// .appendField(new Blockly.FieldTextInput('mydict'), 'VAR')
|
||||
.appendField(new Blockly.FieldLabel(Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT), 'TIP')
|
||||
.appendField(' ')
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setOutput(true, "Dict")
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['dicts_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.DICTS_CREATE_WITH_TOOLTIP);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('dicts_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('dicts_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
}
|
||||
|
||||
var keyNames = [];
|
||||
for (var i = 0; this.getInput('ADD' + i); i++) {
|
||||
//this.getInput('VALUE' + i).removeField("KEY"+i);
|
||||
keyNames.push(this.getFieldValue("KEY" + i))
|
||||
this.removeInput('ADD' + i);
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.getField('TIP').setValue(Blockly.Msg.LOGIC_NULL + Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT);
|
||||
} else {
|
||||
this.getField('TIP').setValue(Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT);
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
this.appendValueInput('ADD' + i)
|
||||
.setCheck(null)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(
|
||||
new Blockly.FieldTextInput(
|
||||
keyNames.length > i
|
||||
? keyNames[i]
|
||||
: (i == 0 ? '"key"' : '"key' + (i + 1) + '"')),
|
||||
'KEY' + i)
|
||||
.appendField(":")
|
||||
}
|
||||
|
||||
}
|
||||
}, getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_todict = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_TODICT);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TODICT);
|
||||
}
|
||||
};
|
||||
|
||||
export const dicts_to_json = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('DICT')
|
||||
.setCheck('Dict');
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_TO_JSON);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TO_JSON_TOOLTIP);
|
||||
this.setOutput(true, Number);
|
||||
}
|
||||
};
|
||||
|
||||
export const json_to_dicts = {
|
||||
init: function () {
|
||||
this.setColour(DICTS_HUE);
|
||||
this.appendValueInput('VAR');
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_CONVERT_TO_JSON);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_CONVERT_TO_JSON_TOOLTIP);
|
||||
this.setOutput(true, Number);
|
||||
}
|
||||
};
|
||||
177
boards/default_src/python/blocks/html.js
Normal file
177
boards/default_src/python/blocks/html.js
Normal file
@@ -0,0 +1,177 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const HTML_HUE = '#1ec1e4';
|
||||
|
||||
export const html_document = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_DOCUMENT);
|
||||
this.appendStatementInput('HEAD')
|
||||
.appendField(Blockly.Msg.HTML_HEAD);
|
||||
this.appendStatementInput('BODY')
|
||||
.appendField(Blockly.Msg.HTML_BODY);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_title = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_LEVEL)
|
||||
.appendField(new Blockly.FieldDropdown([["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"]]), 'LEVEL');
|
||||
this.appendStatementInput('DO')
|
||||
.appendField('');
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_head_body = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.HTML_HEAD, "head"],
|
||||
[Blockly.Msg.HTML_BODY, "body"]
|
||||
]), 'LEVEL');
|
||||
this.appendStatementInput('DO')
|
||||
.appendField('');
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_content = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.HTML_P, "p"],
|
||||
[Blockly.Msg.HTML_SPAN, "span"],
|
||||
[Blockly.Msg.HTML_FORM, "form"],
|
||||
[Blockly.Msg.HTML_TABLE, "table"],
|
||||
[Blockly.Msg.HTML_LINE, "tr"],
|
||||
[Blockly.Msg.HTML_CELL, "td"],
|
||||
[Blockly.Msg.HTML_OL, "ol"],
|
||||
[Blockly.Msg.HTML_UL, "ul"],
|
||||
[Blockly.Msg.HTML_LI, "li"]
|
||||
]), 'LEVEL')
|
||||
// this.appendValueInput('style')
|
||||
// .appendField(Blockly.Msg.MIXLY_AIP_ATTR)
|
||||
// .setAlign(Blockly.inputs.Align.RIGHT);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField('');
|
||||
this.setInputsInline(false);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_content_more = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('<')
|
||||
.appendField(new Blockly.FieldTextInput('tag'), "LEVEL")
|
||||
.appendField('>')
|
||||
this.appendValueInput('style')
|
||||
.appendField(Blockly.Msg.MIXLY_AIP_ATTR)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT);
|
||||
this.appendStatementInput('DO')
|
||||
.appendField('');
|
||||
this.setInputsInline(false);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_style = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_STYLE)
|
||||
this.appendStatementInput('STYLE');
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_form = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_FORM_CONTENT)
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.HTML_TEXT, "text"],
|
||||
[Blockly.Msg.HTML_EMAIL, "email"],
|
||||
[Blockly.Msg.HTML_NUMBER, "number"],
|
||||
[Blockly.Msg.HTML_PASSWORD, "password"],
|
||||
[Blockly.Msg.HTML_CHECKBOX, "checkbox"],
|
||||
[Blockly.Msg.HTML_RADIOBUTTON, "radiobutton"],
|
||||
[Blockly.Msg.HTML_BUTTON, "button"],
|
||||
[Blockly.Msg.HTML_COLOUR, "colour"],
|
||||
[Blockly.Msg.HTML_DATE, "date"],
|
||||
[Blockly.Msg.HTML_LOCALTIME, "local time"],
|
||||
[Blockly.Msg.HTML_FILE, "file"],
|
||||
[Blockly.Msg.HTML_HIDDEN, "hidden"],
|
||||
[Blockly.Msg.HTML_IMAGE, "image"],
|
||||
[Blockly.Msg.HTML_MONTH, "month"],
|
||||
[Blockly.Msg.HTML_RANGE, "range"],
|
||||
[Blockly.Msg.HTML_RESET, "reset"],
|
||||
[Blockly.Msg.HTML_SEARCH, "search"],
|
||||
[Blockly.Msg.HTML_SUBMIT, "submit"],
|
||||
[Blockly.Msg.HTML_TELEPHONENUMBER, "telephone number"],
|
||||
[Blockly.Msg.HTML_TIME, "time"],
|
||||
[Blockly.Msg.HTML_URL, "url"],
|
||||
[Blockly.Msg.HTML_WEEK, "week"]
|
||||
]), 'LEVEL')
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_NAME)
|
||||
.appendField(new Blockly.FieldTextInput('car'), "NAME")
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_VALUE)
|
||||
.appendField(new Blockly.FieldTextInput('go'), "VALUE")
|
||||
this.appendValueInput('style')
|
||||
.appendField(Blockly.Msg.MIXLY_AIP_ATTR)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_style_content = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTextInput('property'), "KEY")
|
||||
.appendField(':')
|
||||
.appendField(new Blockly.FieldTextInput('value'), "VALUE")
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_style_color = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTextInput('property'), "KEY")
|
||||
.appendField(':')
|
||||
.appendField(new Blockly.FieldColour("#ff0000"), "RGB_LED_COLOR");
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const html_text = {
|
||||
init: function () {
|
||||
this.setColour(HTML_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.HTML_TEXT)
|
||||
.appendField(new Blockly.FieldTextInput('text'), "TEXT");
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
917
boards/default_src/python/blocks/lists.js
Normal file
917
boards/default_src/python/blocks/lists.js
Normal file
@@ -0,0 +1,917 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const LISTS_HUE = 260; //'#70b234'//260;
|
||||
|
||||
export const lists_get_index = {
|
||||
init: function () {
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_INDEX_HELPURL);
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput("LIST")
|
||||
this.appendValueInput("AT")
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + " " + Blockly.Msg.LISTS_GET_INDEX_FROM_START)
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_FROM);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const lists_get_sublist = {
|
||||
/**
|
||||
* Block for getting sublist.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('LIST')
|
||||
this.appendDummyInput('')
|
||||
this.appendValueInput('AT1')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + " " + Blockly.Msg.LISTS_GET_INDEX_FROM_START);
|
||||
this.appendValueInput('AT2')
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL + " " + Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_START);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, 'List');
|
||||
this.setTooltip(Blockly.Msg.PYTHON_LISTS_GET_SUBLIST_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const lists_2d_get_data_with_col_row = {
|
||||
init: function () {
|
||||
this.appendValueInput("LIST")
|
||||
.setCheck(null);
|
||||
this.appendValueInput("row")
|
||||
.setCheck(null)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + " " + Blockly.Msg.DATAFRAME_RAW);
|
||||
this.appendValueInput("col")
|
||||
.setCheck(null)
|
||||
.appendField(Blockly.Msg.DATAFRAME_COLUMN);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, null);
|
||||
this.setColour(LISTS_HUE);
|
||||
this.setTooltip("");
|
||||
this.setHelpUrl("");
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_2d_get_col_row_data = {
|
||||
init: function () {
|
||||
this.appendValueInput("LIST")
|
||||
.setCheck(null);
|
||||
this.appendValueInput("row_start")
|
||||
.setCheck(null)
|
||||
.appendField(Blockly.Msg.MIXLY_GET + " " + Blockly.Msg.DATAFRAME_RAW + " [");
|
||||
this.appendValueInput("row_end")
|
||||
.setCheck(null)
|
||||
.appendField(",");
|
||||
this.appendValueInput("col_start")
|
||||
.setCheck(null)
|
||||
.appendField(") " + Blockly.Msg.DATAFRAME_COLUMN + " [");
|
||||
this.appendValueInput("col_end")
|
||||
.setCheck(null)
|
||||
.appendField(",");
|
||||
this.appendDummyInput()
|
||||
.appendField(") " + Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, 'List');
|
||||
this.setColour(LISTS_HUE);
|
||||
this.setTooltip("");
|
||||
this.setHelpUrl("");
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_create_with = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>'], [Blockly.Msg.LANG_MATH_STRING, 'Array<string>'], [Blockly.Msg.LANG_MATH_BOOLEAN, 'Array<boolean>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
.appendField(new Blockly.FieldTextInput('mylist'), 'VAR')
|
||||
.appendField('[')
|
||||
//.appendField(new Blockly.FieldTextInput('3',Blockly.FieldTextInput.math_number_validator), 'SIZE')
|
||||
.appendField(']');
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['lists_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_PYTHON_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('lists_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('lists_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.LISTS_CREATE_PYTHON_EMPTY_TITLE);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.blockpy_LISTS_CREATE_WITH_INPUT_WITH);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const lists_create_with_text = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
// .appendField(Blockly.Msg.blockpy_MIXLY_SPLITBYDOU)
|
||||
.appendField(new Blockly.FieldTextInput('mylist'), 'VAR')
|
||||
//.appendField(new Blockly.FieldTextInput('3',Blockly.FieldTextInput.math_number_validator), 'SIZE')
|
||||
// .appendField(Blockly.Msg.MIXLY_MAKELISTFROM)
|
||||
// .appendField(this.newQuote_(true))
|
||||
.appendField(' = [')
|
||||
.appendField(new Blockly.FieldTextInput('0,0,0'), 'TEXT')
|
||||
.appendField(']');
|
||||
// .appendField(this.newQuote_(false))
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_LISTS_CREATE_WITH_TEXT2);
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const lists_create_with_container = {
|
||||
/**
|
||||
* Mutator block for list container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST);
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_CONTAINER_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_create_with_item = {
|
||||
/**
|
||||
* Mutator bolck for adding items.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.LISTS_CREATE_WITH_ITEM_TITLE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_ITEM_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const lists_set_index = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('LIST');
|
||||
this.appendValueInput('AT')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_LIST_ASSIGN_AT);
|
||||
this.appendValueInput('TO')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_LIST_VALUE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.LANG_LISTS_SET_INDEX_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_append_extend = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this['TYPE'] = [
|
||||
[Blockly.Msg.MIXLY_blockpy_set_add, 'append'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_LIST_EXTEND, 'extend']
|
||||
];
|
||||
|
||||
this.appendValueInput('LIST')
|
||||
.setCheck('List')
|
||||
this.appendValueInput('DATA')
|
||||
.appendField(new Blockly.FieldDropdown(this['TYPE']), 'OP')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_LIST_A_ITEM)
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_LIST_TO_END);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'append': Blockly.Msg.MIXLY_TOOLTIP_LIST_APPEND,
|
||||
'extend': Blockly.Msg.LISTS_EXTEND_TOOLTIP
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export const lists_get_random_item = {
|
||||
/**
|
||||
* Block for get a random item from list.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput("LIST");
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + " " + Blockly.Msg.LISTS_GET_INDEX_RANDOM)
|
||||
this.setTooltip(Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_RANDOM);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_get_random_sublist = {
|
||||
/**
|
||||
* Block for get a random item from list.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput("LIST");
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + Blockly.Msg.MIXLY_MICROBIT_RANDOM)
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.LANG_LISTS_GET_INDEX2 + Blockly.Msg.LISTS_GET_RANDOM_SUBLIST)
|
||||
this.setTooltip(Blockly.Msg.LISTS_GET_RANDOM_SUBLIST_TOOLTIP);
|
||||
this.setOutput(true, 'List');
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_insert_value = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('LIST');
|
||||
this.appendValueInput('AT')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_LIST_INSERT_AT);
|
||||
this.appendValueInput('VALUE')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_LIST_VALUE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.LANG_LISTS_SET_INDEX_TOOLTIP);
|
||||
this.setTooltip(Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_INSERT);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_reverse = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck('List') //this.appendDummyInput("")
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_LIST_REVERSE)
|
||||
//.appendField(new Blockly.FieldTextInput('mylist'), 'VAR');
|
||||
this.setTooltip(Blockly.Msg.LANG_LISTS_CLEAR_TOOLTIP);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
export const lists_clear = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROPYTHON_CLEAR)
|
||||
//.appendField(new Blockly.FieldTextInput('mylist'), 'VAR');
|
||||
this.setTooltip(Blockly.Msg.LANG_LISTS_REVERSE_TOOLTIP);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const lists_remove_at = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this['TYPE'] = [
|
||||
[Blockly.Msg.SERIES_INDEX, 'del'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_JS_I2C_VALUE, 'remove']
|
||||
];
|
||||
this.appendValueInput('LIST')
|
||||
.setCheck('List')
|
||||
this.appendValueInput('DATA')
|
||||
.appendField(Blockly.Msg.MIXLY_MIXPY_LISTS_REMOVE)
|
||||
.appendField(new Blockly.FieldDropdown(this['TYPE']), 'OP')
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'del': Blockly.Msg.LISTS_SET_INDEX_TOOLTIP_DELETE,
|
||||
'remove': Blockly.Msg.MIXLY_TOOLTIP_LIST_REMOVE
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
export const lists_pop = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('LIST');
|
||||
this.appendValueInput('VALUE')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_LIST_POP);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FROM);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_find = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_LIST_INDEX, 'INDEX'],
|
||||
[Blockly.Msg.MIXLY_LIST_COUNT, 'COUNT']
|
||||
];
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck('List')
|
||||
this.appendValueInput('data')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
|
||||
.appendField(Blockly.Msg.HTML_VALUE)
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_DE)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
//.appendField(new Blockly.FieldTextInput('mylist'), 'VAR')
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'INDEX': Blockly.Msg.MIXLY_TOOLTIP_LIST_FIND_INDEX,
|
||||
'COUNT': Blockly.Msg.MIXLY_TOOLTIP_LIST_FIND_COUNT
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
export const list_trig = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_LIST_LEN, 'LEN'],
|
||||
[Blockly.Msg.MATH_ONLIST_OPERATOR_SUM, 'SUM'],
|
||||
[Blockly.Msg.MATH_ONLIST_OPERATOR_MAX, 'MAX'],
|
||||
[Blockly.Msg.MATH_ONLIST_OPERATOR_MIN, 'MIN'],
|
||||
[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'],
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.MATH_TRIG_HELPURL);
|
||||
this.setColour(LISTS_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendValueInput('data')
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
// 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 = {
|
||||
'LEN': Blockly.Msg.LISTS_LENGTH_TOOLTIP,
|
||||
'SUM': Blockly.Msg.MATH_ONLIST_TOOLTIP_SUM,
|
||||
'MAX': Blockly.Msg.MATH_ONLIST_TOOLTIP_MAX,
|
||||
'MIN': Blockly.Msg.MATH_ONLIST_TOOLTIP_MIN,
|
||||
'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
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_sort = {
|
||||
/**
|
||||
* Block for sorting a list.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.jsonInit({
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "LIST",
|
||||
"check": "List"
|
||||
},
|
||||
{
|
||||
"type": "field_dropdown",
|
||||
"name": "TYPE",
|
||||
"options": [
|
||||
[Blockly.Msg.LISTS_SORT_TYPE_NUMERIC, "NUMERIC"],
|
||||
[Blockly.Msg.LISTS_SORT_TYPE_TEXT, "TEXT"],
|
||||
[Blockly.Msg.LISTS_SORT_TYPE_IGNORECASE, "IGNORE_CASE"]
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "field_dropdown",
|
||||
"name": "DIRECTION",
|
||||
"options": [
|
||||
[Blockly.Msg.LISTS_SORT_ORDER_ASCENDING, "1"],
|
||||
[Blockly.Msg.LISTS_SORT_ORDER_DESCENDING, "-1"]
|
||||
]
|
||||
},
|
||||
],
|
||||
"message0": Blockly.Msg.LISTS_SORT_TITLE,
|
||||
"inputsInline": true,
|
||||
"output": "List",
|
||||
"colour": LISTS_HUE,
|
||||
"tooltip": Blockly.Msg.LISTS_SORT_TOOLTIP,
|
||||
"helpUrl": Blockly.Msg.LISTS_SORT_HELPURL
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_change_to = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_TUPLE, 'tuple'],
|
||||
[Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD, 'set'],
|
||||
[Blockly.Msg.LISTS_CREATE_WITH_CONTAINER_TITLE_ADD, 'array']
|
||||
];
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck("List")
|
||||
// .appendField(Blockly.Msg.blockpy_USE_LIST);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.A_TO_B)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'tuple': Blockly.Msg.MIXLY_TOOLTIP_CONVERT_LIST_TO_TUPLE,
|
||||
'set': Blockly.Msg.MIXLY_TOOLTIP_CONVERT_LIST_TO_SET,
|
||||
'array': Blockly.Msg.MIXLY_TOOLTIP_CONVERT_LIST_TO_ARRAY
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const list_many_input = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField('[')
|
||||
.appendField(new Blockly.FieldTextInput('0,0,0'), "CONTENT")
|
||||
.appendField(']');
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_create_with_noreturn = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, "List")
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['lists_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_PYTHON_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('lists_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('lists_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.LISTS_CREATE_PYTHON_EMPTY_TITLE);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.blockpy_LISTS_CREATE_WITH_INPUT_WITH);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const lists_change_to_general = {
|
||||
init: function () {
|
||||
var OPERATORS =
|
||||
[
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST, 'list'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_TUPLE, 'tuple'],
|
||||
[Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD, 'set']
|
||||
];
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('VAR');
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.A_TO_B)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_del_general = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('TUP')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.OBJECT_DELETE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_zip = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
|
||||
this.itemCount_ = 2;
|
||||
this.updateShape_();
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, "List")
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['lists_zip_item'], this));
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_TOOLTIP);
|
||||
},
|
||||
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('lists_zip_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('lists_zip_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
export const lists_zip_container = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP)
|
||||
.appendField('[]');
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_INOUT_PRINT_MANY_CONTAINER_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_zip_item = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const list_tolist = {
|
||||
init: function () {
|
||||
this.setColour(LISTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_TOLIST);
|
||||
this.setOutput(true, 'List');
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOLIST);
|
||||
}
|
||||
};
|
||||
|
||||
export const lists_create_with2 = lists_create_with
|
||||
export const lists_create_with_text2 = lists_create_with_text
|
||||
export const lists_getIndex3 = lists_get_index
|
||||
export const lists_getSublist3 = lists_get_sublist
|
||||
export const lists_setIndex3 = lists_set_index
|
||||
export const lists_insert_value2 = lists_insert_value
|
||||
export const lists_remove_at2 = lists_remove_at
|
||||
export const list_tolist2 = list_tolist;
|
||||
|
||||
264
boards/default_src/python/blocks/logic.js
Normal file
264
boards/default_src/python/blocks/logic.js
Normal file
@@ -0,0 +1,264 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const LOGIC_HUE = 210;
|
||||
|
||||
export const logic_compare = {
|
||||
/**
|
||||
* Block for comparison operator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS = Blockly.RTL ? [
|
||||
['=', 'EQ'],
|
||||
['\u2260', 'NEQ'],
|
||||
['>', 'LT'],
|
||||
['\u2265', 'LTE'],
|
||||
['<', 'GT'],
|
||||
['\u2264', 'GTE']
|
||||
] : [
|
||||
['=', 'EQ'],
|
||||
['\u2260', 'NEQ'],
|
||||
['<', 'LT'],
|
||||
['\u2264', 'LTE'],
|
||||
['>', 'GT'],
|
||||
['\u2265', 'GTE']
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.LOGIC_COMPARE_HELPURL);
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.setOutput(true, Boolean);
|
||||
this.appendValueInput('A');
|
||||
this.appendValueInput('B')
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var op = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'EQ': Blockly.Msg.LOGIC_COMPARE_TOOLTIP_EQ,
|
||||
'NEQ': Blockly.Msg.LOGIC_COMPARE_TOOLTIP_NEQ,
|
||||
'LT': Blockly.Msg.LOGIC_COMPARE_TOOLTIP_LT,
|
||||
'LTE': Blockly.Msg.LOGIC_COMPARE_TOOLTIP_LTE,
|
||||
'GT': Blockly.Msg.LOGIC_COMPARE_TOOLTIP_GT,
|
||||
'GTE': Blockly.Msg.LOGIC_COMPARE_TOOLTIP_GTE
|
||||
};
|
||||
return TOOLTIPS[op];
|
||||
});
|
||||
this.prevBlocks_ = [null, null];
|
||||
},
|
||||
/**
|
||||
* Called whenever anything on the workspace changes.
|
||||
* Prevent mismatched types from being compared.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
/*onchange: function(e) {
|
||||
var blockA = this.getInputTargetBlock('A');
|
||||
var blockB = this.getInputTargetBlock('B');
|
||||
// Disconnect blocks that existed prior to this change if they don't match.
|
||||
if (blockA && blockB &&
|
||||
!blockA.outputConnection.checkType_(blockB.outputConnection)) {
|
||||
// Mismatch between two inputs. Disconnect previous and bump it away.
|
||||
// Ensure that any disconnections are grouped with the causing event.
|
||||
Blockly.Events.setGroup(e.group);
|
||||
for (var i = 0; i < this.prevBlocks_.length; i++) {
|
||||
var block = this.prevBlocks_[i];
|
||||
if (block === blockA || block === blockB) {
|
||||
block.unplug();
|
||||
block.bumpNeighbours_();
|
||||
}
|
||||
}
|
||||
Blockly.Events.setGroup(false);
|
||||
}
|
||||
this.prevBlocks_[0] = blockA;
|
||||
this.prevBlocks_[1] = blockB;
|
||||
}*/
|
||||
};
|
||||
|
||||
export const logic_compare_continous = {
|
||||
|
||||
init: function () {
|
||||
var OPERATORS1 = Blockly.RTL ? [
|
||||
['>', 'LT'],
|
||||
['\u2265', 'LTE'],
|
||||
['<', 'GT'],
|
||||
['\u2264', 'GTE']
|
||||
] : [
|
||||
['<', 'LT'],
|
||||
['\u2264', 'LTE'],
|
||||
['>', 'GT'],
|
||||
['\u2265', 'GTE']
|
||||
];
|
||||
var OPERATORS2 = Blockly.RTL ? [
|
||||
['>', 'LT'],
|
||||
['\u2265', 'LTE'],
|
||||
['<', 'GT'],
|
||||
['\u2264', 'GTE']
|
||||
] : [
|
||||
['<', 'LT'],
|
||||
['\u2264', 'LTE'],
|
||||
['>', 'GT'],
|
||||
['\u2265', 'GTE']
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.LOGIC_COMPARE_HELPURL);
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.setOutput(true, Boolean);
|
||||
this.appendValueInput('A');
|
||||
this.appendValueInput('B')
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS1), 'OP1');
|
||||
this.appendValueInput('C')
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS2), 'OP2');
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LOGIC_COMPARE_CONTINOUS_TOOLTIP);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_operation = {
|
||||
/**
|
||||
* Block for logical operations: 'and', 'or'.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.LOGIC_OPERATION_AND, 'AND'],
|
||||
[Blockly.Msg.LOGIC_OPERATION_OR, 'OR'],
|
||||
[Blockly.Msg.LOGIC_OPERATION_NOR, 'NOR'],
|
||||
[Blockly.Msg.LOGIC_OPERATION_XOR, 'XOR']
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.setOutput(true, Boolean);
|
||||
this.appendValueInput('A')
|
||||
.setCheck([Boolean, Number]);
|
||||
this.appendValueInput('B')
|
||||
.setCheck([Boolean, Number])
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var op = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'AND': Blockly.Msg.LOGIC_OPERATION_TOOLTIP_AND,
|
||||
'OR': Blockly.Msg.LOGIC_OPERATION_TOOLTIP_OR,
|
||||
'NOR': Blockly.Msg.LOGIC_OPERATION_TOOLTIP_NOR,
|
||||
'XOR': Blockly.Msg.LOGIC_OPERATION_TOOLTIP_XOR
|
||||
};
|
||||
return TOOLTIPS[op];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_negate = {
|
||||
/**
|
||||
* Block for negation.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
//this.setHelpUrl(Blockly.Msg.LOGIC_NEGATE_HELPURL);
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.setOutput(true, Boolean);
|
||||
this.appendValueInput('BOOL')
|
||||
.setCheck([Number, Boolean])
|
||||
.appendField(Blockly.Msg.LOGIC_NEGATE_TITLE);
|
||||
//this.interpolateMsg(Blockly.Msg.LOGIC_NEGATE_TITLE,
|
||||
// ['BOOL', Boolean, Blockly.inputs.Align.RIGHT],
|
||||
// Blockly.inputs.Align.RIGHT);
|
||||
this.setTooltip(Blockly.Msg.LOGIC_NEGATE_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_boolean = {
|
||||
/**
|
||||
* Block for boolean data type: true and false.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var BOOLEANS = [
|
||||
[Blockly.Msg.LOGIC_BOOLEAN_TRUE, 'TRUE'],
|
||||
[Blockly.Msg.LOGIC_BOOLEAN_FALSE, 'FALSE']
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.LOGIC_BOOLEAN_HELPURL);
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.setOutput(true, Boolean);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown(BOOLEANS), 'BOOL');
|
||||
this.setTooltip(Blockly.Msg.LOGIC_BOOLEAN_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_null = {
|
||||
/**
|
||||
* Block for null data type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
//this.setHelpUrl(Blockly.Msg.LOGIC_NULL_HELPURL);
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.setOutput(true);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.LOGIC_NULL);
|
||||
this.setTooltip(Blockly.Msg.LOGIC_NULL_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_true_or_false = {
|
||||
init: function () {
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.appendValueInput('A');
|
||||
this.appendValueInput('B')
|
||||
.appendField(Blockly.Msg.LOGIC_TERNARY_IF_TRUE);
|
||||
this.appendValueInput('C')
|
||||
.appendField(Blockly.Msg.LOGIC_TERNARY_IF_FALSE);
|
||||
this.setOutput(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_LOGIT_TRUEORFALSE);
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_is_in = {
|
||||
init: function () {
|
||||
var BOOLEANS = [
|
||||
[Blockly.Msg.TEXT_APPEND_TO, 'in'],
|
||||
[Blockly.Msg.MIXLY_PYTHON_LOGIC_IS_NOT_IN, 'not in']
|
||||
];
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.appendValueInput('A');
|
||||
this.appendValueInput('B')
|
||||
.setCheck([String, 'List'])
|
||||
//.appendField(Blockly.Msg.TEXT_APPEND_TO)
|
||||
.appendField(new Blockly.FieldDropdown(BOOLEANS), 'BOOL');
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MICROBIT_LOGIC_IS_IN);
|
||||
this.setOutput(true, Boolean);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.IN);
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_is = {
|
||||
init: function () {
|
||||
var BOOLEANS = [
|
||||
[Blockly.Msg.MIXLY_PYTHON_LOGIC_IS, 'is'],
|
||||
[Blockly.Msg.MIXLY_PYTHON_LOGIC_IS_NOT, 'is not']
|
||||
];
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.appendValueInput('A');
|
||||
this.appendValueInput('B')
|
||||
.appendField(new Blockly.FieldDropdown(BOOLEANS), 'BOOL');
|
||||
//.appendField(Blockly.Msg.MIXLY_PYTHON_LOGIC_IS);
|
||||
this.setOutput(true, Boolean);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LOGIC_IS_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const logic_tobool = {
|
||||
init: function () {
|
||||
this.setColour(LOGIC_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_TOBOOL);
|
||||
this.setOutput(true, Boolean);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOBOOL);
|
||||
}
|
||||
};
|
||||
530
boards/default_src/python/blocks/math.js
Normal file
530
boards/default_src/python/blocks/math.js
Normal file
@@ -0,0 +1,530 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const MATH_HUE = 230; //'#e49f16';
|
||||
|
||||
Blockly.FieldTextInput.math_number_validator = function (text) {
|
||||
//return window.isNaN(text) ? null : String(text);
|
||||
// var pattern = /^-?(0X|0x|0O|0o|0B|0b)?[a-fA-F0-9]{1,}(\.[a-fA-F0-9]+)?$/;
|
||||
// return pattern.test(text) ? String(text) : null;//校验,二 八 十 十六进制匹配
|
||||
return String(text);//不再校验
|
||||
};
|
||||
|
||||
Blockly.FieldTextInput.math_number_validator_include_blank = function (text) {
|
||||
if (text === "") {
|
||||
return "";
|
||||
}
|
||||
var pattern = /^-?(0X|0x|0O|0o|0B|0b)?[a-fA-F0-9]{1,}(\.[a-fA-F0-9]+)?$/;
|
||||
return pattern.test(text) ? String(text) : null;//校验,二 八 十 十六进制匹配
|
||||
};
|
||||
|
||||
export const math_number = {
|
||||
/**
|
||||
* Block for numeric value.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTextInput('0',
|
||||
Blockly.FieldTextInput.math_number_validator), 'NUM');
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.MATH_NUMBER_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const math_constant = {
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
var constant =
|
||||
[['π', 'pi'], ['e', 'e']];
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_MATH_CONSTANT)
|
||||
.appendField(new Blockly.FieldDropdown(constant), 'CONSTANT')
|
||||
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('CONSTANT');
|
||||
var TOOLTIPS = {
|
||||
'pi': Blockly.Msg.MIXLY_PYTHON_MATH_CONSTANT_PI_TOOLTIP,
|
||||
'e': Blockly.Msg.MIXLY_PYTHON_MATH_CONSTANT_E_TOOLTIP
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export const math_constant_mp = {
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
var constant =
|
||||
[['π', 'pi'], ['e', 'e']];
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_MATH_CONSTANT)
|
||||
.appendField(new Blockly.FieldDropdown(constant), 'CONSTANT')
|
||||
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('CONSTANT');
|
||||
var TOOLTIPS = {
|
||||
'pi': Blockly.Msg.MIXLY_PYTHON_MATH_CONSTANT_PI_MP_TOOLTIP,
|
||||
'e': Blockly.Msg.MIXLY_PYTHON_MATH_CONSTANT_E_MP_TOOLTIP
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export const math_arithmetic = {
|
||||
/**
|
||||
* Block for basic arithmetic operator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
['+', 'ADD'],
|
||||
['-', 'MINUS'],
|
||||
['×', 'MULTIPLY'],
|
||||
['÷', 'DIVIDE'],
|
||||
['%', 'QUYU'],
|
||||
['//', 'ZHENGCHU'],
|
||||
['**', 'POWER']
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.MATH_ARITHMETIC_HELPURL);
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true);
|
||||
this.appendValueInput('A')
|
||||
this.appendValueInput('B')
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
// 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,
|
||||
'QUYU': Blockly.Msg.MATH_MODULO_TOOLTIP,
|
||||
'ZHENGCHU': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_DIVIDE,
|
||||
'POWER': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_POWER
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const math_selfcalcu = {
|
||||
/**
|
||||
* Block for basic arithmetic operator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
['+=', 'ADD'],
|
||||
['-=', 'MINUS'],
|
||||
['×=', 'MULTIPLY'],
|
||||
['÷=', 'DIVIDE'],
|
||||
['%=', 'QUYU'],
|
||||
['//=', 'ZHENGCHU'],
|
||||
['**=', 'POWER']
|
||||
];
|
||||
|
||||
this.setColour(MATH_HUE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.appendValueInput('A')
|
||||
this.appendValueInput('B')
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
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,
|
||||
'QUYU': Blockly.Msg.MATH_MODULO_TOOLTIP,
|
||||
'ZHENGCHU': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_DIVIDE,
|
||||
'POWER': Blockly.Msg.MATH_ARITHMETIC_TOOLTIP_POWER
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const math_bit = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
['&', '&'],
|
||||
['|', '|'],
|
||||
['>>', '>>'],
|
||||
['<<', '<<']
|
||||
];
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendValueInput('A')
|
||||
.setCheck(Number);
|
||||
this.appendValueInput('B')
|
||||
.setCheck(Number)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip("位运算");
|
||||
}
|
||||
};
|
||||
|
||||
export const math_trig = {
|
||||
/**
|
||||
* Block for trigonometry operators.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
['sin', 'SIN'],
|
||||
['cos', 'COS'],
|
||||
['tan', 'TAN'],
|
||||
['asin', 'ASIN'],
|
||||
['acos', 'ACOS'],
|
||||
['atan', 'ATAN'],
|
||||
['-', '-'],
|
||||
['ln', 'LN'],
|
||||
['log10', 'LOG10'],
|
||||
['e^', 'EXP'],
|
||||
['10^', 'POW10']
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.MATH_TRIG_HELPURL);
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendValueInput('NUM')
|
||||
.setCheck(Number)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
// 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,
|
||||
'LN': Blockly.Msg.MATH_SINGLE_TOOLTIP_LN
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const math_dec = {
|
||||
/**
|
||||
* Block for trigonometry operators.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MATH_BIN, 'bin'],
|
||||
[Blockly.Msg.MATH_OCT, 'oct'],
|
||||
[Blockly.Msg.MATH_HEX, 'hex'],
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.MATH_TRIG_HELPURL);
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true, String);
|
||||
this.appendValueInput('NUM')
|
||||
.setCheck(Number)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
// 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 = {
|
||||
'bin': Blockly.Msg.MATH_DEC_TOOLTIP_BIN,
|
||||
'oct': Blockly.Msg.MATH_DEC_TOOLTIP_OCT,
|
||||
'hex': Blockly.Msg.MATH_DEC_TOOLTIP_HEX,
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//取整等
|
||||
export const math_to_int = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.LANG_MATH_TO_ROUND, 'round'],
|
||||
[Blockly.Msg.LANG_MATH_TO_CEIL, 'ceil'],
|
||||
[Blockly.Msg.LANG_MATH_TO_FLOOR, 'floor'],
|
||||
[Blockly.Msg.MATH_ABS, 'fabs'],
|
||||
// [Blockly.Msg.MATH_SQ, 'pow'],
|
||||
[Blockly.Msg.MATH_SQRT, 'sqrt']
|
||||
];
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendValueInput('A')
|
||||
.setCheck(Number)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'sqrt': Blockly.Msg.MATH_SINGLE_TOOLTIP_ROOT,
|
||||
'fabs': Blockly.Msg.MATH_SINGLE_TOOLTIP_ABS,
|
||||
'sq': Blockly.Msg.MATH_SINGLE_TOOLTIP_SQ,
|
||||
'round': Blockly.Msg.MATH_SINGLE_TOOLTIP_ROUND,
|
||||
'ceil': Blockly.Msg.MATH_SINGLE_TOOLTIP_CEIL,
|
||||
'floor': Blockly.Msg.MATH_SINGLE_TOOLTIP_FLOOR
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
//最大最小值
|
||||
export const math_max_min = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_MAX, 'max'],
|
||||
[Blockly.Msg.MIXLY_MIN, 'min'],
|
||||
];
|
||||
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendValueInput('A')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP')
|
||||
.appendField('(');
|
||||
this.appendValueInput('B')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(',');
|
||||
this.appendDummyInput('')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(')');
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'max': Blockly.Msg.MIXLY_TOOLTIP_MATH_MAX,
|
||||
'min': Blockly.Msg.MIXLY_TOOLTIP_MATH_MIN
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const math_number_base_conversion = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MATH_TWO, 'two'],
|
||||
[Blockly.Msg.MATH_EIGHT, 'eight'],
|
||||
[Blockly.Msg.MATH_TEN, 'ten'],
|
||||
[Blockly.Msg.MATH_SIXTEEN, 'sixteen']
|
||||
];
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendDummyInput('')
|
||||
.appendField(Blockly.Msg.MATH_BA)
|
||||
this.appendValueInput("NUM")
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP')
|
||||
.appendField(Blockly.Msg.MATH_JinZhi)
|
||||
.setCheck(Number);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MATH_ZHW)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP2')
|
||||
.appendField(Blockly.Msg.MATH_JinZhi);
|
||||
this.setFieldValue('ten', 'OP2')
|
||||
// this.setPreviousStatement(true, null);
|
||||
// this.setNextStatement(true, null);
|
||||
this.setOutput(true)
|
||||
this.setInputsInline(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'two': Blockly.Msg.MATH_Before_two,
|
||||
'eight': Blockly.Msg.MATH_Before_eight,
|
||||
'ten': Blockly.Msg.MATH_Before_ten,
|
||||
'sixteen': Blockly.Msg.MATH_Before_sixteen,
|
||||
};
|
||||
var mode2 = thisBlock.getFieldValue('OP2');
|
||||
var TOOLTIPS2 = {
|
||||
'two': Blockly.Msg.MATH_Behind_two,
|
||||
'eight': Blockly.Msg.MATH_Behind_eight,
|
||||
'ten': Blockly.Msg.MATH_Behind_ten,
|
||||
'sixteen': Blockly.Msg.MATH_Behind_sixteen,
|
||||
};
|
||||
return TOOLTIPS[mode] + TOOLTIPS2[mode2];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const math_random = {
|
||||
init: function () {
|
||||
var INT_FLOAT = [[Blockly.Msg.LANG_MATH_INT, 'int'], [Blockly.Msg.LANG_MATH_FLOAT, 'float']];
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_RANDOM)
|
||||
.appendField(new Blockly.FieldDropdown(INT_FLOAT), 'TYPE');
|
||||
this.appendValueInput('FROM')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.LANG_CONTROLS_FOR_INPUT_FROM);
|
||||
this.appendValueInput('TO')
|
||||
.setCheck(Number)
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.LANG_MATH_RANDOM_INT_INPUT_TO);
|
||||
this.setInputsInline(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('TYPE');
|
||||
var TOOLTIPS = {
|
||||
'int': Blockly.Msg.LANG_MATH_INT,
|
||||
'float': Blockly.Msg.LANG_MATH_FLOAT_RANDOM
|
||||
};
|
||||
return Blockly.Msg.MATH_RANDOM_INT_TOOLTIP + TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const math_constrain = {
|
||||
/**
|
||||
* Block for constraining a number between two limits.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendValueInput('VALUE')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.LANG_MATH_CONSTRAIN_INPUT_CONSTRAIN);
|
||||
this.appendValueInput('LOW')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.LANG_MATH_CONSTRAIN_INPUT_LOW);
|
||||
this.appendValueInput('HIGH')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.LANG_MATH_CONSTRAIN_INPUT_HIGH);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MATH_CONSTRAIN_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const math_map = {
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendValueInput("NUM", Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MAP)
|
||||
.setCheck(Number);
|
||||
this.appendValueInput("fromLow", Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MAP_FROM)
|
||||
.setCheck(Number);
|
||||
this.appendValueInput("fromHigh", Number)
|
||||
.appendField(",")
|
||||
.setCheck(Number);
|
||||
this.appendValueInput("toLow", Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MAP_TO)
|
||||
.setCheck(Number);
|
||||
this.appendValueInput("toHigh", Number)
|
||||
.appendField(",")
|
||||
.setCheck(Number);
|
||||
this.appendDummyInput("")
|
||||
.appendField("]");
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_MATH_MAP);
|
||||
}
|
||||
};
|
||||
|
||||
export const math_indexer_number = {
|
||||
/**
|
||||
* Block for numeric value.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTextInput('0', Blockly.FieldTextInput.math_number_validator_include_blank), 'NUM');
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MATH_NUMBER_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const math_random_seed = {
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendValueInput('NUM')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.LANG_MATH_RANDOM_SEED);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_MATH_RANDOM_SEED);
|
||||
}
|
||||
};
|
||||
|
||||
export const math_round = {
|
||||
|
||||
init: function () {
|
||||
this.setColour(MATH_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendValueInput('VALUE')
|
||||
.setCheck(Number)
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.MATH_ROUND)
|
||||
.appendField(Blockly.Msg.TEXT_KEEP);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_DECIMAL);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MATH_ROUND_NEW_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const text_to_number = {
|
||||
init: function () {
|
||||
var TO_INT_FLOAT =
|
||||
[[Blockly.Msg.MIXLY_TO_INT, 'int'], [Blockly.Msg.MIXLY_TO_FLOAT, 'float'], [Blockly.Msg.MIXLY_TO_BITES, 'b']];
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(new Blockly.FieldDropdown(TO_INT_FLOAT), 'TOWHAT');
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('TOWHAT');
|
||||
var TOOLTIPS = {
|
||||
'int': Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOINT,
|
||||
'float': Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOFLOAT,
|
||||
'b': Blockly.Msg.MIXLY_TOOLTIP_TEXT_TOBYTE
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const text_to_number_skulpt = {
|
||||
init: function () {
|
||||
var TO_INT_FLOAT =
|
||||
[[Blockly.Msg.MIXLY_TO_INT, 'int'], [Blockly.Msg.MIXLY_TO_FLOAT, 'float']];
|
||||
this.setColour(MATH_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(new Blockly.FieldDropdown(TO_INT_FLOAT), 'TOWHAT');
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('TOWHAT');
|
||||
var TOOLTIPS = {
|
||||
'int': Blockly.Msg.MIXLY_TOOLTIP_TEXT_TOINT,
|
||||
'float': Blockly.Msg.MIXLY_TOOLTIP_TEXT_TOFLOAT
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const base_map = math_map
|
||||
1174
boards/default_src/python/blocks/procedures.js
Normal file
1174
boards/default_src/python/blocks/procedures.js
Normal file
File diff suppressed because it is too large
Load Diff
414
boards/default_src/python/blocks/set.js
Normal file
414
boards/default_src/python/blocks/set.js
Normal file
@@ -0,0 +1,414 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const SET_HUE = 100;
|
||||
|
||||
export const set_create_with = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput("")
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>'], [Blockly.Msg.LANG_MATH_STRING, 'Array<string>'], [Blockly.Msg.LANG_MATH_BOOLEAN, 'Array<boolean>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
.appendField(new Blockly.FieldTextInput('s1'), 'VAR')
|
||||
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['set_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.blockpy_SET_CREATE_WITH_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('set_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('set_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.blockpy_SET_CREATE_EMPTY_TITLE);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.blockpy_SET_CREATE_WITH_INPUT_WITH);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const set_create_with_container = {
|
||||
/**
|
||||
* Mutator block for list container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD);
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const set_create_with_item = {
|
||||
/**
|
||||
* Mutator bolck for adding items.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.blockpy_SET_VARIABLES_NAME);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.blockpy_SET_CREATE_WITH_ITEM_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const set_length = {
|
||||
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendValueInput('SET');
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_LENGTH);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.SET_LENGTH_TOOLTIP);
|
||||
this.setOutput(true, Number);
|
||||
}
|
||||
};
|
||||
|
||||
export const set_pop = {
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendValueInput('SET')
|
||||
.setCheck('Set')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_SET_GET_AND_REMOVE_LAST);
|
||||
this.setTooltip(Blockly.Msg.SET_POP_TOOLTIP);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const set_clear = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendValueInput('SET')
|
||||
.setCheck('Set')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.SET_CLEAR);
|
||||
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const set_operate = {
|
||||
init: function () {
|
||||
|
||||
|
||||
this.appendValueInput('SET1')
|
||||
.setCheck('Set')
|
||||
var operate = [
|
||||
[Blockly.Msg.blockpy_set_union, 'union'],
|
||||
[Blockly.Msg.blockpy_set_intersection, 'intersection'],
|
||||
[Blockly.Msg.blockpy_set_difference, 'difference']
|
||||
];
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_and_set)
|
||||
this.appendValueInput('SET2')
|
||||
.setCheck('Set')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_set_get_operate)
|
||||
.appendField(new Blockly.FieldDropdown(operate), 'OPERATE')
|
||||
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, "set");
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OPERATE');
|
||||
var TOOLTIPS = {
|
||||
'union': Blockly.Msg.MIXLY_TOOLTIP_SET_UNION,
|
||||
'intersection': Blockly.Msg.MIXLY_TOOLTIP_SET_INTERSECTION,
|
||||
'difference': Blockly.Msg.MIXLY_TOOLTIP_SET_DIFFERENCE
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const set_operate_update = {
|
||||
init: function () {
|
||||
|
||||
this.appendValueInput('SET1')
|
||||
.setCheck('Set')
|
||||
var operate_update = [
|
||||
[Blockly.Msg.blockpy_set_union, 'update'],
|
||||
[Blockly.Msg.blockpy_set_intersection, 'intersection_update'],
|
||||
[Blockly.Msg.blockpy_set_difference, 'difference_update']
|
||||
];
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_and_set)
|
||||
this.appendValueInput('SET2')
|
||||
.setCheck('Set')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_set_get_operate)
|
||||
.appendField(new Blockly.FieldDropdown(operate_update), 'OPERATE')
|
||||
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_set_update)
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OPERATE');
|
||||
var TOOLTIPS = {
|
||||
'update': Blockly.Msg.MIXLY_TOOLTIP_SET_UPDATE,
|
||||
'intersection_update': Blockly.Msg.MIXLY_TOOLTIP_SET_INTERSECTION_UPDATE,
|
||||
'difference_update': Blockly.Msg.MIXLY_TOOLTIP_SET_DIFFERENCE_UPDATE
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const set_add_discard = {
|
||||
init: function () {
|
||||
this.appendValueInput('SET')
|
||||
.setCheck('Set')
|
||||
var changenum =
|
||||
[[Blockly.Msg.MIXLY_blockpy_set_add, 'add'], [Blockly.Msg.MIXLY_blockpy_set_discard, 'discard']];
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(new Blockly.FieldDropdown(changenum), 'OPERATE')
|
||||
this.appendValueInput('data')
|
||||
.appendField(Blockly.Msg.blockpy_SET_VARIABLES_NAME)
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OPERATE');
|
||||
var TOOLTIPS = {
|
||||
'add': Blockly.Msg.SET_ADD_TOOLTIP,
|
||||
'discard': Blockly.Msg.SET_DISCARD_TOOLTIP,
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const set_sub = {
|
||||
init: function () {
|
||||
|
||||
this.appendValueInput('SET1')
|
||||
.setCheck('Set')
|
||||
var sub_super = [
|
||||
[Blockly.Msg.blockpy_set_sub, 'issubset'],
|
||||
[Blockly.Msg.blockpy_set_super, 'issuperset']
|
||||
];
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_is_set)
|
||||
this.appendValueInput('SET2')
|
||||
.setCheck('Set')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.blockpy_set_of)
|
||||
.appendField(new Blockly.FieldDropdown(sub_super), 'OPERATE')
|
||||
|
||||
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, Boolean);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OPERATE');
|
||||
var TOOLTIPS = {
|
||||
'issubset': Blockly.Msg.MIXLY_TOOLTIP_SET_SUB,
|
||||
'issuperset': Blockly.Msg.MIXLY_TOOLTIP_SET_SUPER
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const set_update = {
|
||||
init: function () {
|
||||
this.appendValueInput('SET')
|
||||
.setCheck('Set')
|
||||
this.setColour(SET_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck([String, 'List'])
|
||||
.appendField(Blockly.Msg.blockpy_set_add_update);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.SET_UPDATE_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
// export const set_change_to = {
|
||||
// init: function() {
|
||||
// var OPERATORS =
|
||||
// [[Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST, 'list'],
|
||||
// [Blockly.Msg.MIXLY_MICROBIT_TYPE_TUPLE, 'tuple']
|
||||
// ];
|
||||
// this.setColour(SET_HUE);
|
||||
// this.appendValueInput('VAR')
|
||||
// .setCheck("Set")
|
||||
// // .appendField(Blockly.Msg.blockpy_USE_LIST);
|
||||
// this.appendDummyInput("")
|
||||
// .appendField(Blockly.Msg.A_TO_B)
|
||||
// .appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
// this.setInputsInline(true);
|
||||
// this.setOutput(true);
|
||||
// var thisBlock = this;
|
||||
// this.setTooltip(function() {
|
||||
// var mode = thisBlock.getFieldValue('OP');
|
||||
// var TOOLTIPS = {
|
||||
// 'list': Blockly.Msg.SET_TO_LISTS,
|
||||
// 'tuple': Blockly.Msg.SET_TO_TUPLE,
|
||||
// };
|
||||
// return TOOLTIPS[mode];
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
export const set_create_with_text_return = {
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField('{')
|
||||
.appendField(new Blockly.FieldTextInput('0,0,0'), 'TEXT')
|
||||
.appendField('}');
|
||||
// .appendField(this.newQuote_(false))
|
||||
this.setOutput(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXPY_TOOLTIP_SET_CREATE_WITH_TEXT);
|
||||
}
|
||||
}
|
||||
|
||||
export const set_toset = {
|
||||
init: function () {
|
||||
this.setColour(SET_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_TOSET);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOSET);
|
||||
}
|
||||
};
|
||||
511
boards/default_src/python/blocks/storage.js
Normal file
511
boards/default_src/python/blocks/storage.js
Normal file
@@ -0,0 +1,511 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const STORAGE_HUE = 0; //'#5d69c5'//0;
|
||||
|
||||
export const storage_open_file_with_os = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput('fn')
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_STORAGE_OPEN_FILE_WITH_OS + "(For Windows)");
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
}
|
||||
}
|
||||
|
||||
export const storage_fileopen = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILENAME")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_OPEN_FILE);
|
||||
//.appendField(new Blockly.FieldTextInput('filename.txt'), 'FILENAME');
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MODE)
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_READ, 'r'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_WRITE, 'w'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_READ, 'rb'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_WRITE, 'wb']
|
||||
]), 'MODE');
|
||||
this.appendValueInput("FILE")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_AS);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_USE;
|
||||
var mode1 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MODE;
|
||||
var mode2 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_OPEN_FILE;
|
||||
var mode3 = Blockly.Msg.MIXLY_BELONG;
|
||||
var TOOLTIPS = {
|
||||
'r': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_READ,
|
||||
'w': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_WRITE,
|
||||
'rb': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_READ,
|
||||
'wb': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_WRITE
|
||||
};
|
||||
return mode0 + TOOLTIPS[mode] + mode3 + mode1 + mode2;
|
||||
});
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const storage_fileopen_new = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILENAME")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_OPEN_FILE);
|
||||
//.appendField(new Blockly.FieldTextInput('filename.txt'), 'FILENAME');
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MODE)
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_READ, 'r'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_WRITE, 'w'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_READ, 'rb'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_WRITE, 'wb']
|
||||
]), 'MODE');
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_USE;
|
||||
var mode1 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MODE;
|
||||
var mode2 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_OPEN_FILE;
|
||||
var mode3 = Blockly.Msg.MIXLY_BELONG;
|
||||
var mode4 = Blockly.Msg.PY_STORAGE_FILE_OBJECT;
|
||||
var TOOLTIPS = {
|
||||
'r': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_READ,
|
||||
'w': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_WRITE,
|
||||
'rb': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_READ,
|
||||
'wb': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_WRITE
|
||||
};
|
||||
return mode0 + TOOLTIPS[mode] + mode3 + mode1 + mode2 + mode4;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_fileopen_new_encoding = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILENAME")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_OPEN_FILE);
|
||||
//.appendField(new Blockly.FieldTextInput('filename.txt'), 'FILENAME');
|
||||
var code =
|
||||
[['ANSI', 'ANSI'], ['gbk', 'gbk'], ['utf-8', 'utf-8']];
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MODE)
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_READ, 'r'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_WRITE, 'w'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_READ, 'rb'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_WRITE, 'wb']
|
||||
]), 'MODE');
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXPY_TEXT_ENCODE)
|
||||
.appendField(new Blockly.FieldDropdown(code), 'CODE')
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_USE;
|
||||
var mode1 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MODE;
|
||||
var mode2 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_OPEN_FILE;
|
||||
var mode3 = Blockly.Msg.MIXLY_BELONG;
|
||||
var mode4 = Blockly.Msg.PY_STORAGE_FILE_OBJECT;
|
||||
var TOOLTIPS = {
|
||||
'r': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_READ,
|
||||
'w': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_WRITE,
|
||||
'rb': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_READ,
|
||||
'wb': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_BIT_WRITE
|
||||
};
|
||||
return mode0 + TOOLTIPS[mode] + mode3 + mode1 + mode2 + mode4;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_file_write = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput('data')
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKE);
|
||||
this.appendValueInput("FILE")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_WRITE);
|
||||
// .appendField(new Blockly.FieldTextInput('f'), 'FILE');
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKE + Blockly.Msg.MIXLY_MICROBIT_TYPE_STRING + Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_WRITE);
|
||||
}
|
||||
}
|
||||
|
||||
export const storage_get_contents_without_para = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck('Variable')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FROM_FILE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ALL, 'read'], [Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ONE_LINE, 'readline'], [Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ALL_LINES, 'readlines']]), 'MODE');
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false); //in front of the block has something
|
||||
this.setNextStatement(false); //beyond the ... has something
|
||||
this.setOutput(true, String);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_get_contents = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck('Variable')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FROM_FILE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_NO_MORE_THAN_SIZE, 'read'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ONE_LINE_NO_MORE_THAN_SIZE, 'readline'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ALL_LINES_NO_MORE_THAN_SIZE, 'readlines']
|
||||
]), 'MODE');
|
||||
this.appendValueInput("SIZE")
|
||||
.setCheck(Number);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHARACTER);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false); //in front of the block has something
|
||||
this.setNextStatement(false); //beyond the ... has something
|
||||
this.setOutput(true, String);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FROM_FILE;
|
||||
var mode2 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHARACTER;
|
||||
var TOOLTIPS = {
|
||||
'read': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_NO_MORE_THAN_SIZE,
|
||||
'readline': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ONE_LINE_NO_MORE_THAN_SIZE,
|
||||
'readlines': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ALL_LINES_NO_MORE_THAN_SIZE
|
||||
};
|
||||
return mode0 + TOOLTIPS[mode] + 'x' + mode2;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_get_a_line = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FROM_FILE);
|
||||
this.setNextStatement(true);
|
||||
this.appendValueInput("SIZE")
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_ONE_LINE_NO_MORE_THAN_SIZE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHARACTER);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MICROBIT_PYTHON_TYPE);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_can_write_ornot = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.appendField(Blockly.Msg.HTML_FILE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CAN_WRITE_ORNOT);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, Boolean);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CAN_WRITE_ORNOT1);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_get_filename = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILENAME);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET_FILENAME);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_close_file = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CLOSE_FILE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setOutput(false);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CLOSE_FILE);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_list_all_files = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_LIST_ALL_FILES);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, 'List');
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_LIST_ALL_FILES);
|
||||
}
|
||||
};
|
||||
Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_DELETE_FILE
|
||||
export const storage_delete_file = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_DELETE_FILE, 'remove'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_DELETE_DIRS, 'removedirs']
|
||||
]), 'MODE');
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck(String);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setOutput(false);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_DELETE_FILE);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_get_file_size = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET_FILE_SIZE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_SIZE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET_FILE_SIZE + Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_SIZE);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_file_tell = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_RETURN_FILE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_PRESENT_LOCATION);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_TELL);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_file_seek = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck('Variable')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_SET_FILE_POSITION);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CURRENT_POSITION);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_START, 'start'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_CURRENT, 'current'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_END, 'end']
|
||||
]), 'MODE');
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_OFFSET);
|
||||
this.appendValueInput("SIZE")
|
||||
.setCheck(Number);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHARACTER);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true); //in front of the block has something
|
||||
this.setNextStatement(true); //beyond the ... has something
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_SET_FILE_POSITION + Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CURRENT_POSITION;
|
||||
var mode2 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHARACTER;
|
||||
var mode3 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_OFFSET;
|
||||
var TOOLTIPS = {
|
||||
'start': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_START,
|
||||
'current': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_CURRENT,
|
||||
'end': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_FILE_SEEK_END
|
||||
};
|
||||
return mode0 + " " + TOOLTIPS[mode] + mode3 + 'x' + mode2;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_get_current_dir = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET_CURRENT_DIR);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, 'List');
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET_CURRENT_DIR);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_make_dir = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("PATH")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_PATH);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_ESP32_SET);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MKDIR, 'mkdir'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MAKEDIRS, 'makedirs']
|
||||
]), 'MODE');
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true); //in front of the block has something
|
||||
this.setNextStatement(true); //beyond the ... has something
|
||||
this.setOutput(false);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_PATH;
|
||||
var mode2 = Blockly.Msg.MIXLY_ESP32_SET;
|
||||
var TOOLTIPS = {
|
||||
'mkdir': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MKDIR,
|
||||
'makedirs': Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKEDIRS
|
||||
};
|
||||
return mode0 + 'x' + mode2 + TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_rename = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_RENAME);
|
||||
this.appendValueInput("NEWFILE")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_AS);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setOutput(false);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_RENAME);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_change_dir = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHANGE_DIR);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setOutput(false);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHANGE_DIR);
|
||||
}
|
||||
};
|
||||
|
||||
export const storage_is_file = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput("FILE")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_THE_PATH);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_IS_OR_NOT);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.HTML_FILE, 'isfile'],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_IS_DIR, 'isdir']
|
||||
]), 'MODE');
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, Boolean);
|
||||
let thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('MODE');
|
||||
var mode0 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_THE_PATH;
|
||||
var mode2 = Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_IS_OR_NOT;
|
||||
var TOOLTIPS = {
|
||||
'isfile': Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_MKDIR,
|
||||
'isdir': Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKEDIRS
|
||||
};
|
||||
return mode0 + 'x' + mode2 + TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const sdcard_use_spi_init = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput('SPISUB')
|
||||
.appendField(Blockly.Msg.CONTROLS_FOR_INPUT_WITH + "SPI")
|
||||
.setCheck("var");
|
||||
this.appendValueInput('PINSUB')
|
||||
.appendField("CS")
|
||||
this.appendValueInput('SUB')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKE)
|
||||
.setCheck("var");
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_SETUP + Blockly.Msg.LISTS_SET_INDEX_INPUT_TO)
|
||||
.appendField(Blockly.Msg.MIXLY_SD_CARD);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true, null);
|
||||
this.setNextStatement(true, null);
|
||||
}
|
||||
};
|
||||
|
||||
export const sdcard_mount = {
|
||||
init: function () {
|
||||
this.setColour(STORAGE_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_SD_CARD);
|
||||
this.appendValueInput("DIR")
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_SDCARD_MOUNT);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip();
|
||||
}
|
||||
}
|
||||
960
boards/default_src/python/blocks/text.js
Normal file
960
boards/default_src/python/blocks/text.js
Normal file
@@ -0,0 +1,960 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const TEXTS_HUE = 160//'#9ec440'//160;
|
||||
|
||||
export const text = {
|
||||
/**
|
||||
* Block for text value.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
//this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(this.newQuote_(true))
|
||||
.appendField(new Blockly.FieldTextInput(''), 'TEXT')
|
||||
.appendField(this.newQuote_(false));
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.TEXT_TEXT_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create an image of an open or closed quote.
|
||||
* @param {boolean} open True if open quote, false if closed.
|
||||
* @return {!Blockly.FieldImage} The field image of the quote.
|
||||
* @private
|
||||
*/
|
||||
newQuote_: function (open) {
|
||||
if (open == this.RTL) {
|
||||
var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
|
||||
} else {
|
||||
var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
|
||||
}
|
||||
return new Blockly.FieldImage(file, 12, 12, '"');
|
||||
}
|
||||
};
|
||||
|
||||
export const text_textarea = {
|
||||
/**
|
||||
* Block for text value.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
//this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(this.newQuote_(true))
|
||||
.appendField(new Blockly.FieldMultilineInput('Hello\nMixly'), 'VALUE')
|
||||
// .appendField(new Blockly.FieldTextInput(''), 'TEXT')
|
||||
.appendField(this.newQuote_(false));
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.TEXT_LINES_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create an image of an open or closed quote.
|
||||
* @param {boolean} open True if open quote, false if closed.
|
||||
* @return {!Blockly.FieldImage} The field image of the quote.
|
||||
* @private
|
||||
*/
|
||||
newQuote_: function (open) {
|
||||
if (open == this.RTL) {
|
||||
var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
|
||||
} else {
|
||||
var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
|
||||
}
|
||||
return new Blockly.FieldImage(file, 12, 12, '"');
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.FieldTextInput.char_validator = function (text) {
|
||||
if (text.length > 1) {
|
||||
if (text.charAt(0) === "\\") {
|
||||
var charAtOne = text.charAt(1);
|
||||
if (charAtOne === "0" ||
|
||||
charAtOne === "b" ||
|
||||
charAtOne === "f" ||
|
||||
charAtOne === "n" ||
|
||||
charAtOne === "r" ||
|
||||
charAtOne === "t" ||
|
||||
charAtOne === "\\" ||
|
||||
charAtOne === "'") {
|
||||
return String(text).substring(0, 2);
|
||||
} else if (charAtOne === "x" && text.charAt(2) === "0" && text.charAt(3) === "B") {
|
||||
return String(text).substring(0, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
return String(text).substring(0, 1);
|
||||
};
|
||||
|
||||
export const text_char = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(this.newQuote_(true))
|
||||
.appendField(new Blockly.FieldTextInput('', Blockly.FieldTextInput.char_validator), 'TEXT')
|
||||
.appendField(this.newQuote_(false));
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.TEXT_CHAR_TOOLTIP);
|
||||
},
|
||||
newQuote_: function (open) {
|
||||
if (open == true) {
|
||||
var file = '../../media/quote2.png';
|
||||
} else {
|
||||
var file = '../../media/quote3.png';
|
||||
}
|
||||
return new Blockly.FieldImage(file, 7, 12, '"');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const text_join = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('A')
|
||||
.setCheck([String, Number]);
|
||||
this.appendValueInput('B')
|
||||
.setCheck([String, Number])
|
||||
.appendField(Blockly.Msg.MIXLY_TEXT_JOIN);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_TEXT_JOIN);
|
||||
}
|
||||
};
|
||||
|
||||
export const ascii_to_char = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.MIXLY_TOCHAR);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_TEXT_TOCHAR);
|
||||
}
|
||||
};
|
||||
|
||||
export const char_to_ascii = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_TOASCII);
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_TEXT_TOASCII);
|
||||
}
|
||||
};
|
||||
|
||||
export const number_to_text = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_TOSTRING);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOTEXT);
|
||||
}
|
||||
};
|
||||
|
||||
export const text_length = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(Blockly.Msg.MIXLY_LENGTH);
|
||||
this.setOutput(true, Number);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_TEXT_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const text_char_at2 = {
|
||||
init: function () {
|
||||
this.WHERE_OPTIONS = [
|
||||
[Blockly.Msg.LISTS_GET_INDEX_FROM_START, "FROM_START"],
|
||||
[Blockly.Msg.LISTS_GET_INDEX_FROM_END, "FROM_END"],
|
||||
[Blockly.Msg.TEXT_GET_INDEX_RANDOM + 1 + Blockly.Msg.TEXT_CHARAT2, "RANDOM"]
|
||||
];
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_INDEX_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String)
|
||||
// .appendField(Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST)
|
||||
this.appendValueInput("AT")
|
||||
.setCheck(Number)
|
||||
this.appendDummyInput()
|
||||
//.appendField(Blockly.Msg.MIXLY_MID)
|
||||
.appendField(Blockly.Msg.LISTS_GET_INDEX_GET, "MODE");
|
||||
// .appendField("", "SPACE");
|
||||
Blockly.Msg.LISTS_GET_INDEX_TAIL && this.appendDummyInput("TAIL").appendField(Blockly.Msg.LISTS_GET_INDEX_TAIL);
|
||||
// this.appendDummyInput().appendField(Blockly.Msg.MIXLY_DE);
|
||||
this.setInputsInline(!0);
|
||||
this.setOutput(!0);
|
||||
this.updateAt_(!0);
|
||||
var b = this;
|
||||
this.setTooltip(function () {
|
||||
var a = b.getFieldValue("MODE"),
|
||||
e = b.getFieldValue("WHERE"),
|
||||
d = "";
|
||||
switch (a + " " + e) {
|
||||
case "GET FROM_START":
|
||||
case "GET FROM_END":
|
||||
d = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_FROM;
|
||||
break;
|
||||
case "GET RANDOM":
|
||||
d = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_RANDOM;
|
||||
break;
|
||||
case "GET_REMOVE FROM_START":
|
||||
case "GET_REMOVE FROM_END":
|
||||
d = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FROM;
|
||||
break;
|
||||
case "GET_REMOVE RANDOM":
|
||||
d = Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_RANDOM;
|
||||
break;
|
||||
}
|
||||
if ("FROM_START" == e || "FROM_END" == e) d += " " + Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP.replace("%1", Blockly.Msg.ONE_BASED_INDEXING ? "#1" : "#0");
|
||||
return d
|
||||
})
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('WHERE');
|
||||
var TOOLTIPS = {
|
||||
'FROM_START': Blockly.Msg.LISTS_GET_INDEX_FROM_START,
|
||||
'FROM_END': Blockly.Msg.LISTS_GET_INDEX_FROM_END,
|
||||
'RANDOM': Blockly.Msg.TEXT_GET_INDEX_RANDOM
|
||||
};
|
||||
return Blockly.Msg.PROCEDURES_DEFRETURN_RETURN + Blockly.Msg.MIXLY_MICROBIT_TYPE_STRING + TOOLTIPS[mode] + 'n' + Blockly.Msg.TEXT_CHARAT2;
|
||||
});
|
||||
},
|
||||
mutationToDom: function () {
|
||||
var a = document.createElement("mutation");
|
||||
a.setAttribute("statement", !this.outputConnection);
|
||||
var b = this.getInput("AT").type == Blockly.INPUT_VALUE;
|
||||
a.setAttribute("at", b);
|
||||
return a
|
||||
},
|
||||
domToMutation: function (a) {
|
||||
var b = "true" == a.getAttribute("statement");
|
||||
this.updateStatement_(b);
|
||||
a = "false" != a.getAttribute("at");
|
||||
this.updateAt_(a)
|
||||
},
|
||||
updateStatement_: function (a) {
|
||||
a != !this.outputConnection && (this.unplug(!0, !0), a ? (this.setOutput(!1), this.setPreviousStatement(!0), this.setNextStatement(!0)) : (this.setPreviousStatement(!1), this.setNextStatement(!1), this.setOutput(!0)))
|
||||
},
|
||||
updateAt_: function (a) {
|
||||
this.removeInput("AT");
|
||||
this.removeInput("ORDINAL", !0);
|
||||
a ? (this.appendValueInput("AT").setCheck(Number), Blockly.Msg.TEXT_CHARAT2 && this.appendDummyInput("ORDINAL").appendField(Blockly.Msg.TEXT_CHARAT2)) : this.appendDummyInput("AT");
|
||||
var b = new Blockly.FieldDropdown(this.WHERE_OPTIONS,
|
||||
function (b) {
|
||||
var e = "FROM_START" == b || "FROM_END" == b;
|
||||
if (e != a) {
|
||||
var d = this.sourceBlock_;
|
||||
d.updateAt_(e);
|
||||
d.setFieldValue(b, "WHERE");
|
||||
return null
|
||||
}
|
||||
});
|
||||
this.getInput("AT").appendField(b, "WHERE");
|
||||
Blockly.Msg.LISTS_GET_INDEX_TAIL && this.moveInputBefore("TAIL", null)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const text_char_at = {
|
||||
init: function () {
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_INDEX_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String);
|
||||
this.appendValueInput("AT")
|
||||
.setCheck(Number)
|
||||
.appendField(Blockly.Msg.LISTS_GET_INDEX_GET + " " + Blockly.Msg.LISTS_GET_INDEX_FROM_START);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT2);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.PROCEDURES_DEFRETURN_RETURN + Blockly.Msg.MIXLY_MICROBIT_TYPE_STRING + Blockly.Msg.LISTS_GET_INDEX_FROM_START + 'n' + Blockly.Msg.TEXT_CHARAT2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const text_random_char = {
|
||||
init: function () {
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_INDEX_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_RANDOM_CHAR);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.TEXT_RANDOM_CHAR_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const text_substring2 = {
|
||||
/**
|
||||
* Block for getting sublist.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this['WHERE_OPTIONS_1'] =
|
||||
[[Blockly.Msg.LISTS_GET_INDEX_FROM_START, 'FROM_START'],
|
||||
[Blockly.Msg.LISTS_GET_INDEX_FROM_END, 'FROM_END'],
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_START_FIRST, 'FIRST']];
|
||||
this['WHERE_OPTIONS_2'] =
|
||||
[[Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_START, 'FROM_START'],
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_END, 'FROM_END'],
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_END_LAST, 'LAST']];
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String)
|
||||
//.appendField(Blockly.Msg.LISTS_GET_SUBLIST_TAIL)
|
||||
// if (Blockly.Msg.LISTS_GET_SUBLIST_TAIL) {
|
||||
// this.appendDummyInput('TAIL')
|
||||
// .appendField(Blockly.Msg.LISTS_GET_SUBLIST_TAIL);
|
||||
// }
|
||||
this.appendDummyInput('')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET);
|
||||
this.appendDummyInput('AT1');
|
||||
this.appendDummyInput('AT2');
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, 'List');
|
||||
this.updateAt_(1, true);
|
||||
this.updateAt_(2, true);
|
||||
this.setTooltip(Blockly.Msg._GET_TEXT_SUBLIST_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent whether there are 'AT' inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
|
||||
container.setAttribute('at1', isAt1);
|
||||
var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
|
||||
container.setAttribute('at2', isAt2);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the 'AT' inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
var isAt1 = (xmlElement.getAttribute('at1') == 'true');
|
||||
var isAt2 = (xmlElement.getAttribute('at2') == 'true');
|
||||
this.updateAt_(1, isAt1);
|
||||
this.updateAt_(2, isAt2);
|
||||
},
|
||||
/**
|
||||
* Create or delete an input for a numeric index.
|
||||
* This block has two such inputs, independant of each other.
|
||||
* @param {number} n Specify first or second input (1 or 2).
|
||||
* @param {boolean} isAt True if the input should exist.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateAt_: function (n, isAt) {
|
||||
// Create or delete an input for the numeric index.
|
||||
// Destroy old 'AT' and 'ORDINAL' inputs.
|
||||
this.removeInput('AT' + n);
|
||||
this.removeInput('ORDINAL' + n, true);
|
||||
// Create either a value 'AT' input or a dummy input.
|
||||
if (isAt) {
|
||||
this.appendValueInput('AT' + n).setCheck(Number);
|
||||
if (Blockly.Msg.TEXT_CHARAT2) {
|
||||
this.appendDummyInput('ORDINAL' + n)
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT2);
|
||||
}
|
||||
} else {
|
||||
this.appendDummyInput('AT' + n);
|
||||
}
|
||||
var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
|
||||
function (value) {
|
||||
var newAt = (value == 'FROM_START') || (value == 'FROM_END');
|
||||
// The 'isAt' variable is available due to this function being a
|
||||
// closure.
|
||||
if (newAt != isAt) {
|
||||
var block = this.sourceBlock_;
|
||||
block.updateAt_(n, newAt);
|
||||
// This menu has been destroyed and replaced.
|
||||
// Update the replacement.
|
||||
block.setFieldValue(value, 'WHERE' + n);
|
||||
return null;
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
this.getInput('AT' + n)
|
||||
.appendField(menu, 'WHERE' + n);
|
||||
if (n == 1) {
|
||||
this.moveInputBefore('AT1', 'AT2');
|
||||
if (this.getInput('ORDINAL1')) {
|
||||
this.moveInputBefore('ORDINAL1', 'AT2');
|
||||
}
|
||||
}
|
||||
// if (Blockly.Msg.LISTS_GET_SUBLIST_TAIL) {
|
||||
// this.moveInputBefore('TAIL', null);
|
||||
// }
|
||||
}
|
||||
};
|
||||
|
||||
export const text_substring = {
|
||||
/**
|
||||
* Block for getting sublist.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String)
|
||||
this.appendValueInput('AT1')
|
||||
.appendField(Blockly.Msg.LISTS_GET_INDEX_GET + " " + Blockly.Msg.LISTS_GET_INDEX_FROM_START);
|
||||
this.appendValueInput('AT2')
|
||||
.appendField(Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_START);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT2);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, ['List', String]);
|
||||
this.setTooltip(Blockly.Msg._GET_TEXT_SUBLIST_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_equals_starts_ends = {
|
||||
init: function () {
|
||||
var TEXT_DOWHAT =
|
||||
[[Blockly.Msg.MIXLY_EQUALS, '==='],
|
||||
[Blockly.Msg.MIXLY_STARTSWITH, 'startswith'],
|
||||
[Blockly.Msg.MIXLY_ENDSWITH, 'endswith']];
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("STR1")
|
||||
.setCheck(String);
|
||||
this.appendValueInput("STR2")
|
||||
.appendField(new Blockly.FieldDropdown(TEXT_DOWHAT), 'DOWHAT')
|
||||
.setCheck(String);
|
||||
this.setOutput(true, [Boolean, Number]);
|
||||
this.setInputsInline(true);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_compare_to = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("STR1")
|
||||
.setCheck(String);
|
||||
this.appendValueInput("STR2")
|
||||
.appendField(Blockly.Msg.MIXLY_COMPARETO)
|
||||
.setCheck(String);
|
||||
this.setOutput(true, Number);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_COMPARETO_HELP);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_capital = {
|
||||
init: function () {
|
||||
var TEXT_CAPITAL =
|
||||
[[Blockly.Msg.TEXT_UPPER, 'upper'], [Blockly.Msg.TEXT_TITLE, 'title'], [Blockly.Msg.TEXT_CAPITALIZE, 'capitalize'], [Blockly.Msg.TEXT_SWAPCASE, 'swapcase'],
|
||||
[Blockly.Msg.TEXT_LOWER, 'lower']];
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
|
||||
.appendField(new Blockly.FieldDropdown(TEXT_CAPITAL), 'CAPITAL')
|
||||
.setCheck(String);
|
||||
this.setOutput(true, String);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('CAPITAL');
|
||||
var TOOLTIPS = {
|
||||
'upper': Blockly.Msg.MIXLY_MIXPY_TEXT_UPPER_TOOLTIP,
|
||||
'title': Blockly.Msg.MIXLY_MIXPY_TEXT_TITLE_TOOLTIP,
|
||||
'swapcase': Blockly.Msg.MIXLY_MIXPY_TEXT_SWAPCASE_TOOLTIP,
|
||||
'capitalize': Blockly.Msg.MIXLY_MIXPY_TEXT_CAPITALIZE_TOOLTIP,
|
||||
'lower': Blockly.Msg.MIXLY_MIXPY_TEXT_LOWER_TOOLTIP
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const text_center = {
|
||||
init: function () {
|
||||
var TEXT_CENTER =
|
||||
[[Blockly.Msg.TEXT_LJUST, 'ljust'],
|
||||
[Blockly.Msg.TEXT_CENTER, 'center'],
|
||||
[Blockly.Msg.TEXT_RJUST, 'rjust']];
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(new Blockly.FieldDropdown(TEXT_CENTER), 'CENTER')
|
||||
.setCheck(String);
|
||||
this.appendValueInput("WID")
|
||||
.appendField(Blockly.Msg.MIXLY_WIDTH)
|
||||
.setCheck(Number);
|
||||
this.appendValueInput("Symbol")
|
||||
.appendField(Blockly.Msg.MIXLY_RECT_Fill)
|
||||
.setCheck(String);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_TEXT_CENTER_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_find = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
|
||||
.setCheck(String);
|
||||
this.appendValueInput("STR")
|
||||
.appendField(Blockly.Msg.MIXLY_MID + Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_CHARACTER)
|
||||
.setCheck(String);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_LIST_INDEX);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_TEXT_FIND_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_join_seq = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_TEXT_JOIN_SEQ_USE_STR)
|
||||
.setCheck(String);
|
||||
this.appendValueInput('LIST')
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_TEXT_JOIN_SEQ_SEQ)
|
||||
.setCheck('List', 'Tuple', 'Set', 'Dict');
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_TEXT_JOIN_SEQ_GET_STR);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TEXT_JOIN_SEQ_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_replace = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String);
|
||||
this.appendValueInput("STR1")
|
||||
.appendField(Blockly.Msg.MIXLY_MIXPY_REPLACE)
|
||||
.setCheck(String);
|
||||
this.appendValueInput("STR2")
|
||||
.appendField(Blockly.Msg.LISTS_SET_INDEX_INPUT_TO)
|
||||
.setCheck(String);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, String);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_TEXT_REPLACE_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_split = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput("VAR");
|
||||
this.appendValueInput("VAL")
|
||||
.appendField(Blockly.Msg.LIST_SPLIT_AS);
|
||||
this.appendDummyInput('')
|
||||
.appendField(Blockly.Msg.LIST_SPLIT);
|
||||
this.setOutput(true, "List");
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_TEXT_SPLIT_TOOLTIP);
|
||||
this.setInputsInline(true);
|
||||
}
|
||||
}
|
||||
|
||||
export const text_strip = {
|
||||
init: function () {
|
||||
var STRIP =
|
||||
[[Blockly.Msg.TEXT_TRIM_BOTH, 'strip'], [Blockly.Msg.TEXT_TRIM_LEFT, 'lstrip'], [Blockly.Msg.TEXT_TRIM_RIGHT, 'rstrip']];
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
this.appendDummyInput('')
|
||||
.appendField(Blockly.Msg.TEXT_STRIM);
|
||||
this.appendDummyInput('')
|
||||
.appendField(new Blockly.FieldDropdown(STRIP), 'TOWHAT');
|
||||
this.appendDummyInput('')
|
||||
.appendField(Blockly.Msg.TEXT_BLANK);
|
||||
this.setOutput(true, String);
|
||||
this.setInputsInline(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('TOWHAT');
|
||||
var TOOLTIPS = {
|
||||
'strip': Blockly.Msg.TEXT_TRIM_BOTH_TOOLTIP,
|
||||
'lstrip': Blockly.Msg.TEXT_TRIM_LEFT_TOOLTIP,
|
||||
'rstrip': Blockly.Msg.TEXT_TRIM_RIGHT_TOOLTIP
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const text_format = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROPYTHON_FORMAT)
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>'], [Blockly.Msg.LANG_MATH_STRING, 'Array<string>'], [Blockly.Msg.LANG_MATH_BOOLEAN, 'Array<boolean>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
this.appendDummyInput("")
|
||||
.appendField(new Blockly.FieldTextInput('str'), 'VAR');
|
||||
this.itemCount_ = 1;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setInputsInline(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['text_create_with_item'], this));
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_TEXT_FORMAT_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('text_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('text_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField();
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.PROCEDURES_BEFORE_PARAMS);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
if (this.getFieldValue('VAR') != null) {
|
||||
if ((this.getFieldValue('VAR').indexOf("'") == -1) && (this.getFieldValue('VAR').indexOf('"') == -1)) {
|
||||
return [this.getFieldValue('VAR')];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export const text_create_with_container = {
|
||||
/**
|
||||
* Mutator block for list container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.PROCEDURES_MUTATORCONTAINER_TITLE);
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const text_create_with_item = {
|
||||
/**
|
||||
* Mutator bolck for adding items.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.blockpy_SET_VARIABLES_NAME);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const text_substring3 = text_substring
|
||||
export const text_compareTo = text_compare_to
|
||||
export const text_char_at3 = text_char_at
|
||||
|
||||
export const text_format_noreturn = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROPYTHON_FORMAT)
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>'], [Blockly.Msg.LANG_MATH_STRING, 'Array<string>'], [Blockly.Msg.LANG_MATH_BOOLEAN, 'Array<boolean>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
this.appendValueInput("VAR")
|
||||
.setCheck(String);
|
||||
this.itemCount_ = 1;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setInputsInline(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['text_create_with_item'], this));
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_MIXPY_TEXT_FORMAT_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('text_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('text_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField();
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.PROCEDURES_BEFORE_PARAMS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
export const text_encode = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
var encode_decode =
|
||||
[[Blockly.Msg.MIXPY_TEXT_ENCODE, 'encode'], [Blockly.Msg.MIXPY_TEXT_DECODE, 'decode']];
|
||||
var code =
|
||||
[['ASCII', 'ASCII'], ['gb2312', 'gb2312'], ['gbk', 'gbk'], ['utf-8', 'utf-8'], ['utf-16', 'utf-16'], ['utf-32', 'utf-32']];
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown(code), 'CODE')
|
||||
.appendField(' ')
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(new Blockly.FieldDropdown(encode_decode), 'DIR')
|
||||
.appendField(Blockly.Msg.LANG_MATH_STRING);
|
||||
this.setOutput(true, String);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXPY_TEXT_ENCODE_DECODE_TOOLTIP);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const text_eval = {
|
||||
init: function () {
|
||||
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_TEXT_EVAL);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_TEXT_EVAL_RESULT);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TEXT_EVAL_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const os_system = {
|
||||
init: function () {
|
||||
this.setColour(TEXTS_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck(String)
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_OS_SYSTEM);
|
||||
this.setInputsInline(true);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_OS_SYSTEM_TOOLTIP);
|
||||
}
|
||||
};
|
||||
722
boards/default_src/python/blocks/tuple.js
Normal file
722
boards/default_src/python/blocks/tuple.js
Normal file
@@ -0,0 +1,722 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const TUPLE_HUE = 195; //'#5ec73d'//195;
|
||||
|
||||
export const tuple_create_with = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendDummyInput("")
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>'], [Blockly.Msg.LANG_MATH_STRING, 'Array<string>'], [Blockly.Msg.LANG_MATH_BOOLEAN, 'Array<boolean>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
.appendField(new Blockly.FieldTextInput('mytup'), 'VAR');
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['tuple_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('tuple_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('tuple_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.TUPLE_CREATE_WITH_INPUT_WITH);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_create_with_container = {
|
||||
/**
|
||||
* Mutator block for list container.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TITLE_ADD);
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_create_with_item = {
|
||||
/**
|
||||
* Mutator bolck for adding items.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.blockpy_SET_VARIABLES_NAME);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TOOLTIP);
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_create_with_text2 = {
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendDummyInput("")
|
||||
//don't need to specify the data type in Python
|
||||
// .appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'Array<number>']]), 'TYPE')
|
||||
// .appendField(' ')
|
||||
// .appendField(Blockly.Msg.blockpy_MIXLY_TUPLE_CREATE)
|
||||
.appendField(new Blockly.FieldTextInput('mytup'), 'VAR')
|
||||
//.appendField(new Blockly.FieldTextInput('3',Blockly.FieldTextInput.math_number_validator), 'SIZE')
|
||||
// .appendField(Blockly.Msg.MIXLY_MAKELISTFROM)
|
||||
// .appendField(this.newQuote_(true))
|
||||
.appendField(' = (')
|
||||
.appendField(new Blockly.FieldTextInput('0,0,0'), 'TEXT')
|
||||
.appendField(')');
|
||||
// .appendField(this.newQuote_(false))
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.MIXPY_TOOLTIP_TUPLE_CREATE_WITH_TEXT);
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
// newQuote_: function(open) {
|
||||
// if (open == this.RTL) {
|
||||
// var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
|
||||
// } else {
|
||||
// var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
|
||||
// }
|
||||
// return new Blockly.FieldImage(file, 12, 12, '"');
|
||||
// }
|
||||
}
|
||||
|
||||
export const tuple_create_with_text_return = {
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField('(')
|
||||
.appendField(new Blockly.FieldTextInput('0,0,0'), 'TEXT')
|
||||
.appendField(')');
|
||||
// .appendField(this.newQuote_(false))
|
||||
this.setOutput(true);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.MIXPY_TOOLTIP_TUPLE_CREATE_WITH_TEXT);
|
||||
// },
|
||||
// getVars: function() {
|
||||
// return [this.getFieldValue('VAR')];
|
||||
// },
|
||||
// renameVar: function(oldName, newName) {
|
||||
// if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
// this.setTitleValue(newName, 'VAR');
|
||||
// }
|
||||
}
|
||||
// newQuote_: function(open) {
|
||||
// if (open == this.RTL) {
|
||||
// var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==';
|
||||
// } else {
|
||||
// var file = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC';
|
||||
// }
|
||||
// return new Blockly.FieldImage(file, 12, 12, '"');
|
||||
// }
|
||||
}
|
||||
|
||||
export const tuple_getIndex = {
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.setOutput(true);
|
||||
this.appendValueInput('TUP')
|
||||
.setCheck('Tuple')
|
||||
this.appendValueInput('AT')
|
||||
.setCheck(Number)
|
||||
|
||||
.appendField(Blockly.Msg.LANG_LISTS_GET_INDEX1);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.LANG_LISTS_GET_INDEX2);
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.TUPLE_GET_INDEX_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_length = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('TUP');
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_LENGTH);
|
||||
|
||||
this.setTooltip(Blockly.Msg.TUPLE_LENGTH_TOOLTIP);
|
||||
this.setOutput(true, Number);
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_del = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('TUP')
|
||||
.setCheck('Tuple')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.TUPLE_DEL);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.TUPLE_DEL_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_join = {
|
||||
/**
|
||||
* Block for list length.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('TUP1')
|
||||
.setCheck('Tuple')
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.TUPLE_JOIN)
|
||||
this.appendValueInput('TUP2')
|
||||
.setCheck('Tuple')
|
||||
this.setInputsInline(true);
|
||||
this.setTooltip(Blockly.Msg.TUPLE_JOIN_TOOLTIP);
|
||||
this.setOutput(true, "Tuple");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const tuple_max = {
|
||||
init: function () {
|
||||
this.appendValueInput('TUP')
|
||||
.setCheck('Tuple')
|
||||
var max_min =
|
||||
[[Blockly.Msg.blockpy_TUPLE_MAX, 'max'], [Blockly.Msg.blockpy_TUPLE_MIN, 'min'], [Blockly.Msg.MATH_ONLIST_OPERATOR_SUM, 'sum']];
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_JS_GET)
|
||||
.appendField(new Blockly.FieldDropdown(max_min), 'DIR')
|
||||
|
||||
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('DIR');
|
||||
var TOOLTIPS = {
|
||||
'max': Blockly.Msg.MIXLY_TOOLTIP_TUPLE_MAX,
|
||||
'min': Blockly.Msg.MIXLY_TOOLTIP_TUPLE_MIN,
|
||||
'sum': Blockly.Msg.MIXLY_TOOLTIP_TUPLE_SUM
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_change_to = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST, 'list'],
|
||||
[Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD, 'set']
|
||||
];
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck("Tuple")
|
||||
// .appendField(Blockly.Msg.blockpy_USE_LIST);
|
||||
this.appendDummyInput("")
|
||||
.appendField(Blockly.Msg.A_TO_B)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'list': Blockly.Msg.TUPLE_TO_LISTS,
|
||||
'set': Blockly.Msg.TUPLE_TO_SET,
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_find = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_LIST_INDEX, 'INDEX'],
|
||||
[Blockly.Msg.MIXLY_LIST_COUNT, 'COUNT']
|
||||
];
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.setCheck('List')
|
||||
this.appendValueInput('data')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
|
||||
.appendField(Blockly.Msg.HTML_VALUE)
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_DE)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
//.appendField(new Blockly.FieldTextInput('mylist'), 'VAR')
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, Number);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('OP');
|
||||
var TOOLTIPS = {
|
||||
'INDEX': Blockly.Msg.MIXLY_TOOLTIP_TUPLE_FIND_INDEX,
|
||||
'COUNT': Blockly.Msg.MIXLY_TOOLTIP_TUPLE_FIND_COUNT
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_trig = {
|
||||
init: function () {
|
||||
var OPERATORS = [
|
||||
[Blockly.Msg.MIXLY_LIST_LEN, 'LEN'],
|
||||
[Blockly.Msg.MATH_ONLIST_OPERATOR_SUM, 'SUM'],
|
||||
[Blockly.Msg.MATH_ONLIST_OPERATOR_MAX, 'MAX'],
|
||||
[Blockly.Msg.MATH_ONLIST_OPERATOR_MIN, 'MIN'],
|
||||
[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'],
|
||||
];
|
||||
//this.setHelpUrl(Blockly.Msg.MATH_TRIG_HELPURL);
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.setOutput(true, Number);
|
||||
this.appendValueInput('data')
|
||||
.setCheck('List')
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'OP');
|
||||
this.setInputsInline(true);
|
||||
// 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 = {
|
||||
'LEN': Blockly.Msg.TUPLE_LENGTH_TOOLTIP,
|
||||
'SUM': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_SUM,
|
||||
'MAX': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_MAX,
|
||||
'MIN': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_MIN,
|
||||
'AVERAGE': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_AVERAGE,
|
||||
'MEDIAN': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_MEDIAN,
|
||||
'MODE': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_MODE,
|
||||
'STD_DEV': Blockly.Msg.MATH_ONLIST_TOOLTIP_TUPLE_STD_DEV
|
||||
|
||||
};
|
||||
return TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_getSublist = {
|
||||
/**
|
||||
* Block for getting sublist.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this['WHERE_OPTIONS_1'] = [
|
||||
[Blockly.Msg.LISTS_GET_INDEX_FROM_START, 'FROM_START'],
|
||||
[Blockly.Msg.LISTS_GET_INDEX_FROM_END, 'FROM_END'],
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_START_FIRST, 'FIRST']
|
||||
];
|
||||
this['WHERE_OPTIONS_2'] = [
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_START, 'FROM_START'],
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_END, 'FROM_END'],
|
||||
[Blockly.Msg.LISTS_GET_SUBLIST_END_LAST, 'LAST']
|
||||
];
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('LIST')
|
||||
.setCheck('List')
|
||||
//.appendField(Blockly.Msg.LISTS_GET_SUBLIST_TAIL)
|
||||
// if (Blockly.Msg.LISTS_GET_SUBLIST_TAIL) {
|
||||
// this.appendDummyInput('TAIL')
|
||||
// .appendField(Blockly.Msg.LISTS_GET_SUBLIST_TAIL);
|
||||
// }
|
||||
this.appendDummyInput('')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET);
|
||||
this.appendDummyInput('AT1');
|
||||
this.appendDummyInput('AT2');
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, 'List');
|
||||
this.updateAt_(1, true);
|
||||
this.updateAt_(2, true);
|
||||
this.setTooltip(Blockly.Msg.PYTHON_TUPLE_GET_SUBLIST_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent whether there are 'AT' inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
|
||||
container.setAttribute('at1', isAt1);
|
||||
var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
|
||||
container.setAttribute('at2', isAt2);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the 'AT' inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
var isAt1 = (xmlElement.getAttribute('at1') == 'true');
|
||||
var isAt2 = (xmlElement.getAttribute('at2') == 'true');
|
||||
this.updateAt_(1, isAt1);
|
||||
this.updateAt_(2, isAt2);
|
||||
},
|
||||
/**
|
||||
* Create or delete an input for a numeric index.
|
||||
* This block has two such inputs, independant of each other.
|
||||
* @param {number} n Specify first or second input (1 or 2).
|
||||
* @param {boolean} isAt True if the input should exist.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateAt_: function (n, isAt) {
|
||||
// Create or delete an input for the numeric index.
|
||||
// Destroy old 'AT' and 'ORDINAL' inputs.
|
||||
this.removeInput('AT' + n);
|
||||
this.removeInput('ORDINAL' + n, true);
|
||||
// Create either a value 'AT' input or a dummy input.
|
||||
if (isAt) {
|
||||
this.appendValueInput('AT' + n).setCheck(Number);
|
||||
if (Blockly.Msg.TEXT_CHARAT_TAIL) {
|
||||
this.appendDummyInput('ORDINAL' + n)
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
|
||||
}
|
||||
} else {
|
||||
this.appendDummyInput('AT' + n);
|
||||
}
|
||||
var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
|
||||
function (value) {
|
||||
var newAt = (value == 'FROM_START') || (value == 'FROM_END');
|
||||
// The 'isAt' variable is available due to this function being a
|
||||
// closure.
|
||||
if (newAt != isAt) {
|
||||
var block = this.sourceBlock_;
|
||||
block.updateAt_(n, newAt);
|
||||
// This menu has been destroyed and replaced.
|
||||
// Update the replacement.
|
||||
block.setFieldValue(value, 'WHERE' + n);
|
||||
return null;
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
this.getInput('AT' + n)
|
||||
.appendField(menu, 'WHERE' + n);
|
||||
if (n == 1) {
|
||||
this.moveInputBefore('AT1', 'AT2');
|
||||
if (this.getInput('ORDINAL1')) {
|
||||
this.moveInputBefore('ORDINAL1', 'AT2');
|
||||
}
|
||||
}
|
||||
// if (Blockly.Msg.LISTS_GET_SUBLIST_TAIL) {
|
||||
// this.moveInputBefore('TAIL', null);
|
||||
// }
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_create_with_noreturn = {
|
||||
/**
|
||||
* Block for creating a list with any number of elements of any type.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.itemCount_ = 3;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(false);
|
||||
this.setNextStatement(false);
|
||||
this.setOutput(true, "Tuple")
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['tuple_create_with_item'], this));
|
||||
this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent list inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock =
|
||||
workspace.newBlock('tuple_create_with_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var itemBlock = workspace.newBlock('tuple_create_with_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ADD' + i);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
i++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ADD' + i)) {
|
||||
this.removeInput('ADD' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('ADD' + i);
|
||||
if (i == 0) {
|
||||
input.appendField(Blockly.Msg.TUPLE_CREATE_WITH_INPUT_WITH);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setTitleValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_get_sublist = {
|
||||
/**
|
||||
* Block for getting sublist.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setHelpUrl(Blockly.Msg.LISTS_GET_SUBLIST_HELPURL);
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('LIST')
|
||||
this.appendDummyInput('')
|
||||
this.appendValueInput('AT1')
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + " " + Blockly.Msg.LISTS_GET_INDEX_FROM_START);
|
||||
this.appendValueInput('AT2')
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL + " " + Blockly.Msg.LISTS_GET_SUBLIST_END_FROM_START);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, 'Tuple');
|
||||
this.setTooltip(Blockly.Msg.PYTHON_TUPLE_GET_SUBLIST_TOOLTIP);
|
||||
}
|
||||
}
|
||||
|
||||
export const tuple_get_random_item = {
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput("TUP");
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET + " " + Blockly.Msg.LISTS_GET_INDEX_RANDOM)
|
||||
this.setTooltip(Blockly.Msg.LISTS_GET_INDEX_TOOLTIP_GET_RANDOM);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const tuple_totuple = {
|
||||
init: function () {
|
||||
this.setColour(TUPLE_HUE);
|
||||
this.appendValueInput('VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_TOTUPLE);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOTUPLE);
|
||||
}
|
||||
};
|
||||
445
boards/default_src/python/blocks/utility.js
Normal file
445
boards/default_src/python/blocks/utility.js
Normal file
@@ -0,0 +1,445 @@
|
||||
/**
|
||||
* @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 Utility blocks for Blockly.
|
||||
* @author acbart@vt.edu (Austin Cory Bart)
|
||||
*/
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
const UTILITY_HUE = 160;
|
||||
|
||||
export const raw_table = {
|
||||
// Container.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.appendDummyInput()
|
||||
.appendField('Tabular Abstraction:');
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTable(''), 'TEXT');
|
||||
}
|
||||
};
|
||||
|
||||
export const raw_block = {
|
||||
// Container.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.appendDummyInput()
|
||||
.appendField('Code Block:');
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldMultilineInput(''), 'TEXT');
|
||||
}
|
||||
};
|
||||
|
||||
export const raw_expression = {
|
||||
// Container.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('Code Expression:');
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldMultilineInput(''), 'TEXT');
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const raw_empty = {
|
||||
// Container.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.appendValueInput('VALUE')
|
||||
.appendField('');
|
||||
this.setInputsInline(false);
|
||||
}
|
||||
};
|
||||
|
||||
export const text_comment = {
|
||||
// Text value.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendTitle('Comment:')
|
||||
.appendTitle(new Blockly.FieldTextInput(''), 'TEXT');
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip('This comment will be ignored by Python');
|
||||
}
|
||||
};
|
||||
|
||||
export const type_check = {
|
||||
// Set element at index.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendValueInput('VALUE')
|
||||
.appendField(Blockly.Msg.TYPE_CHECK);
|
||||
this.setInputsInline(false);
|
||||
this.setOutput(true, 'Type');
|
||||
//this.setPreviousStatement(true);
|
||||
//this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const text_print_multiple = {
|
||||
/**
|
||||
* Block for printing multiple things (including nothing)
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.itemCount_ = 1;
|
||||
this.updateShape_();
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['text_print_multiple_item'], this));
|
||||
this.setTooltip(Blockly.Msg.TEXT_PRINT_TOOLTIP);
|
||||
},
|
||||
/**
|
||||
* Create XML to represent print inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock = Blockly.Block.obtain(workspace,
|
||||
'text_print_multiple_container');
|
||||
containerBlock.initSvg();
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var x = 0; x < this.itemCount_; x++) {
|
||||
var itemBlock = workspace.newBlock('text_print_multiple_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('PRINT' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
// Store a pointer to any connected child blocks.
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var x = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('PRINT' + x);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
x++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('PRINT' + i)) {
|
||||
this.removeInput('PRINT' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild block.
|
||||
if (this.itemCount_ == 0) {
|
||||
this.appendDummyInput('EMPTY')
|
||||
.appendField("print");
|
||||
} else {
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
var input = this.appendValueInput('PRINT' + i);
|
||||
if (i == 0) {
|
||||
input.appendField("print");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const text_print_multiple_container = {
|
||||
// Container.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('print');
|
||||
this.appendStatementInput('STACK');
|
||||
this.setTooltip('');
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
export const text_print_multiple_item = {
|
||||
// Add items.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('item');
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip('');
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const function_call = {
|
||||
/**
|
||||
* Block for printing multiple things (including nothing)
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.itemCount_ = 1;
|
||||
this.hasReturn_ = false;
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTextInput("str"), 'NAME');
|
||||
this.updateShape_();
|
||||
this.setMutator(new Blockly.icons.MutatorIcon(['function_call_item'], this));
|
||||
this.setTooltip("Can be used to call any function");
|
||||
},
|
||||
/**
|
||||
* Create XML to represent print inputs.
|
||||
* @return {Element} XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
mutationToDom: function () {
|
||||
var container = document.createElement('mutation');
|
||||
container.setAttribute('items', this.itemCount_);
|
||||
container.setAttribute('hasReturn', this.hasReturn_ ? "TRUE" : "FALSE");
|
||||
return container;
|
||||
},
|
||||
/**
|
||||
* Parse XML to restore the list inputs.
|
||||
* @param {!Element} xmlElement XML storage element.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
domToMutation: function (xmlElement) {
|
||||
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||
this.hasReturn_ = xmlElement.getAttribute('hasReturn') === "TRUE";
|
||||
this.updateShape_();
|
||||
},
|
||||
/**
|
||||
* Populate the mutator's dialog with this block's components.
|
||||
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||
* @return {!Blockly.Block} Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
decompose: function (workspace) {
|
||||
var containerBlock = Blockly.Block.obtain(workspace,
|
||||
'function_call_container');
|
||||
containerBlock.initSvg();
|
||||
|
||||
containerBlock.setFieldValue(this.hasStatements_ ? 'TRUE' : 'FALSE',
|
||||
'RETURN');
|
||||
|
||||
var connection = containerBlock.getInput('STACK').connection;
|
||||
for (var x = 0; x < this.itemCount_; x++) {
|
||||
var itemBlock = workspace.newBlock('function_call_item');
|
||||
itemBlock.initSvg();
|
||||
connection.connect(itemBlock.previousConnection);
|
||||
connection = itemBlock.nextConnection;
|
||||
}
|
||||
return containerBlock;
|
||||
},
|
||||
/**
|
||||
* Notification that the procedure's return state has changed.
|
||||
* @param {boolean} returnState New return state
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
setReturn: function (returnState) {
|
||||
this.unplug(true, true);
|
||||
this.setOutput(returnState);
|
||||
this.setPreviousStatement(!returnState);
|
||||
this.setNextStatement(!returnState);
|
||||
if (this.rendered) {
|
||||
this.render();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Reconfigure this block based on the mutator dialog's components.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
compose: function (containerBlock) {
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
// Count number of inputs.
|
||||
var connections = [];
|
||||
var i = 0;
|
||||
while (itemBlock) {
|
||||
connections[i] = itemBlock.valueConnection_;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
i++;
|
||||
}
|
||||
this.itemCount_ = i;
|
||||
|
||||
this.hasReturn_ = containerBlock.getFieldValue("RETURN") === "TRUE";
|
||||
|
||||
this.updateShape_();
|
||||
// Reconnect any child blocks.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
if (connections[i]) {
|
||||
this.getInput('ARGUMENT' + i).connection.connect(connections[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Store pointers to any connected child blocks.
|
||||
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
saveConnections: function (containerBlock) {
|
||||
// Store a pointer to any connected child blocks.
|
||||
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||
var x = 0;
|
||||
while (itemBlock) {
|
||||
var input = this.getInput('ARGUMENT' + x);
|
||||
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||
x++;
|
||||
itemBlock = itemBlock.nextConnection &&
|
||||
itemBlock.nextConnection.targetBlock();
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Modify this block to have the correct number of inputs.
|
||||
* @private
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
updateShape_: function () {
|
||||
// Delete everything.
|
||||
if (this.getInput('EMPTY')) {
|
||||
this.removeInput('EMPTY');
|
||||
} else {
|
||||
var i = 0;
|
||||
while (this.getInput('ARGUMENT' + i)) {
|
||||
this.removeInput('ARGUMENT' + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild block.
|
||||
for (var i = 0; i < this.itemCount_; i++) {
|
||||
this.appendValueInput('ARGUMENT' + i);
|
||||
}
|
||||
|
||||
// Set whether returns anything
|
||||
this.setReturn(this.hasReturn_);
|
||||
}
|
||||
};
|
||||
|
||||
export const function_call_container = {
|
||||
// Container.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('Arguments');
|
||||
this.appendStatementInput('STACK');
|
||||
this.appendDummyInput()
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField('has return')
|
||||
.appendField(new Blockly.FieldCheckbox('TRUE'),
|
||||
'RETURN');
|
||||
this.setTooltip('');
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
export const function_call_item = {
|
||||
// Add items.
|
||||
init: function () {
|
||||
this.setColour(UTILITY_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField('argument');
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip('');
|
||||
this.contextMenu = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const attribute_access = {
|
||||
init: function () {
|
||||
this.appendValueInput("MODULE")
|
||||
.setCheck(null);
|
||||
this.appendValueInput("NAME")
|
||||
.setCheck(null)
|
||||
.appendField(".");
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true, null);
|
||||
this.setColour(230);
|
||||
this.setTooltip('');
|
||||
this.setHelpUrl('');
|
||||
}
|
||||
};
|
||||
202
boards/default_src/python/blocks/variables.js
Normal file
202
boards/default_src/python/blocks/variables.js
Normal file
@@ -0,0 +1,202 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
import Names from '../others/names';
|
||||
|
||||
const VARIABLES_HUE = 330//'#af5180'//330;
|
||||
|
||||
// ************************************************************************
|
||||
// THIS SECTION IS INSERTED INTO BLOCKLY BY BLOCKLYDUINO.
|
||||
// export const variables_declare = {
|
||||
// // Variable setter.
|
||||
// init: function() {
|
||||
// this.setColour(VARIABLES_HUE);
|
||||
// this.appendValueInput('VALUE', null)
|
||||
// .appendField(Blockly.Msg.MIXLY_DECLARE)
|
||||
// .appendField(new Blockly.FieldTextInput(''), 'VAR')
|
||||
// //.appendField(Blockly.Msg.MIXLY_AS)
|
||||
// //.appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_NUMBER, 'number'], [Blockly.Msg.LANG_MATH_STRING, 'string'], [Blockly.Msg.LANG_MATH_BOOLEAN, 'boolean']]), 'TYPE')
|
||||
// .appendField(Blockly.Msg.MIXLY_VALUE);
|
||||
// this.setPreviousStatement(true);
|
||||
// this.setNextStatement(true);
|
||||
// this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_VARIABLES_DECLARE);
|
||||
// },
|
||||
// getVars: function() {
|
||||
// return [this.getFieldValue('VAR')];
|
||||
// },
|
||||
// renameVar: function(oldName, newName) {
|
||||
// if (Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
// this.setTitleValue(newName, 'VAR');
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// ************************************************************************
|
||||
|
||||
export const variables_get = {
|
||||
init: function () {
|
||||
this.setColour(VARIABLES_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldTextInput(''), 'VAR')
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
|
||||
},
|
||||
getVars: function () {
|
||||
return [this.getFieldValue('VAR')];
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setFieldValue(newName, 'VAR');
|
||||
}
|
||||
}/*,
|
||||
onchange: function() {
|
||||
var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),Blockly.Variables.NAME_TYPE);
|
||||
if(Blockly.Arduino.definitions_['var_declare'+varName]){
|
||||
this.setWarningText(null);
|
||||
}else{
|
||||
this.setWarningText(Blockly.Msg.MIXLY_WARNING_NOT_DECLARE);
|
||||
}
|
||||
}*/
|
||||
};
|
||||
|
||||
// export const variables_set = {
|
||||
// init: function() {
|
||||
// this.setColour(VARIABLES_HUE);
|
||||
// this.appendValueInput('VALUE')
|
||||
// .appendField(new Blockly.FieldTextInput(''), 'VAR')
|
||||
// .appendField(Blockly.Msg.MIXLY_VALUE2);
|
||||
// this.setPreviousStatement(true);
|
||||
// this.setNextStatement(true);
|
||||
// this.setTooltip(Blockly.Msg.VARIABLES_SET_TOOLTIP);
|
||||
// },
|
||||
// getVars: function() {
|
||||
// return [this.getFieldValue('VAR')];
|
||||
// },
|
||||
// renameVar: function(oldName, newName) {
|
||||
// if (Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
// this.setFieldValue(newName, 'VAR');
|
||||
// }
|
||||
// }/*,
|
||||
// onchange: function() {
|
||||
// var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),Blockly.Variables.NAME_TYPE);
|
||||
// if(Blockly.Arduino.definitions_['var_declare'+varName]){
|
||||
// this.setWarningText(null);
|
||||
// }else{
|
||||
// this.setWarningText(Blockly.Msg.MIXLY_WARNING_NOT_DECLARE);
|
||||
// }
|
||||
// }*/
|
||||
// };
|
||||
export const variables_set = {
|
||||
init: function () {
|
||||
this.setColour(VARIABLES_HUE);
|
||||
this.appendValueInput('VALUE')
|
||||
.appendField(new Blockly.FieldTextInput(''), 'VAR')
|
||||
.appendField(Blockly.Msg.MIXLY_VALUE2);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
this.setTooltip(Blockly.Msg.VARIABLES_SET_TOOLTIP);
|
||||
},
|
||||
getVars: function () {
|
||||
var varValue = this.getFieldValue('VAR');
|
||||
if (varValue == null) {
|
||||
return [];
|
||||
}
|
||||
return varValue.split(",");
|
||||
},
|
||||
renameVar: function (oldName, newName) {
|
||||
if (Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||
this.setFieldValue(newName, 'VAR');
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Block for basic data type change.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
export const variables_change = {
|
||||
init: function () {
|
||||
this.setColour(VARIABLES_HUE);
|
||||
var DATATYPES =
|
||||
[
|
||||
[Blockly.Msg.LANG_MATH_INT, "int"],
|
||||
[Blockly.Msg.LANG_MATH_FLOAT, "float"],
|
||||
[Blockly.Msg.LANG_MATH_BOOLEAN, "bool"],
|
||||
// [Blockly.Msg.MIXLY_MICROPYTHON_TYPE_COMPLEX, "complex"],
|
||||
[Blockly.Msg.LANG_MATH_STRING, "str"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST, "list"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_TUPLE, "tuple"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT, "dict"],
|
||||
[Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD, "set"],
|
||||
[Blockly.Msg.LANG_MATH_BYTE, "bytes"]
|
||||
];
|
||||
this.appendValueInput('MYVALUE')
|
||||
.appendField(new Blockly.FieldDropdown(DATATYPES), 'OP');
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
this.setOutput(true);
|
||||
// this.setInputsInline(true);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const variables_global = {
|
||||
init: function () {
|
||||
this.setColour(VARIABLES_HUE);
|
||||
this.appendValueInput("VAR")
|
||||
.appendField(Blockly.Msg.MIXLY_PYTHON_GLOBAL)
|
||||
.setCheck("var");
|
||||
this.setPreviousStatement(true, null);
|
||||
this.setNextStatement(true, null);
|
||||
this.setTooltip(Blockly.Msg.TEXT_PRINT_TOOLTIP);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const controls_type = {
|
||||
init: function () {
|
||||
this.setColour(VARIABLES_HUE);
|
||||
this.appendValueInput("DATA")
|
||||
.appendField(Blockly.Msg.MICROBIT_PYTHON_TYPE);
|
||||
// this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
this.setTooltip(Blockly.Msg.MICROBIT_PYTHON_TYPE);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const controls_typeLists = {
|
||||
init: function () {
|
||||
this.setColour(VARIABLES_HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_MICROBIT_PY_CONTORL_GET_TYPE)
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.LANG_MATH_INT, "int"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_FLOAT, "float"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_STRING, "str"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST, "list"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_TUPLE, "tuple"],
|
||||
[Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT, "dict"],
|
||||
[Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD, "set"],
|
||||
[Blockly.Msg.LANG_MATH_BYTE, "bytes"],
|
||||
// [Blockly.Msg.MIXLY_MICROBIT_IMAGE,"image"],
|
||||
[Blockly.Msg.LOGIC_NULL, "type(None)"]]), "type");
|
||||
//整数、浮点数、字符串、列表、元组、字典、集合、图像不太对, unfinished
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
var mode = thisBlock.getFieldValue('type');
|
||||
var mode0 = Blockly.Msg.MICROBIT_controls_TypeLists;
|
||||
var TOOLTIPS = {
|
||||
'int': Blockly.Msg.LANG_MATH_INT,
|
||||
'float': Blockly.Msg.MIXLY_MICROBIT_TYPE_FLOAT,
|
||||
'str': Blockly.Msg.MIXLY_MICROBIT_TYPE_STRING,
|
||||
'list': Blockly.Msg.MIXLY_MICROBIT_TYPE_LIST,
|
||||
'tuple': Blockly.Msg.MIXLY_MICROBIT_TYPE_TUPLE,
|
||||
'dict': Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT,
|
||||
'set': Blockly.Msg.blockpy_SET_CREATE_WITH_CONTAINER_TITLE_ADD,
|
||||
'image': Blockly.Msg.MIXLY_MICROBIT_IMAGE,
|
||||
'bytes': Blockly.Msg.LANG_MATH_BYTE,
|
||||
'NoneType': Blockly.Msg.LOGIC_NULL
|
||||
};
|
||||
return mode0 + TOOLTIPS[mode];
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user