update loop blocks
This commit is contained in:
parent
40d8301990
commit
2a2c1e1c25
@ -3,54 +3,185 @@ import Blockly from 'blockly';
|
||||
import { getColour } from '../helpers/colour';
|
||||
import { getCompatibleTypes } from '../helpers/types'
|
||||
|
||||
// Blockly.defineBlocksWithJsonArray([
|
||||
// {
|
||||
// type: 'controls_for',
|
||||
// message0: 'count with %1 from %2 to %3 by adding %4',
|
||||
// args0: [
|
||||
// {
|
||||
// type: 'field_variable',
|
||||
// name: 'VAR',
|
||||
// variable: null,
|
||||
// variableTypes: ['Number'],
|
||||
// defaultType: 'Number',
|
||||
// createNewVariable: true,
|
||||
// showOnlyVariableAssigned: false,
|
||||
// },
|
||||
// {
|
||||
// type: 'input_value',
|
||||
// name: 'FROM',
|
||||
// check: 'Number',
|
||||
// align: 'RIGHT',
|
||||
// },
|
||||
// {
|
||||
// type: 'input_value',
|
||||
// name: 'TO',
|
||||
// check: 'Number',
|
||||
// align: 'RIGHT',
|
||||
// },
|
||||
// {
|
||||
// type: 'input_value',
|
||||
// name: 'BY',
|
||||
// check: 'Number',
|
||||
// align: 'RIGHT',
|
||||
// },
|
||||
// ],
|
||||
// message1: '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1',
|
||||
// args1: [
|
||||
// {
|
||||
// type: 'input_statement',
|
||||
// name: 'DO',
|
||||
// },
|
||||
// ],
|
||||
// inputsInline: false,
|
||||
// previousStatement: null,
|
||||
// nextStatement: null,
|
||||
// colour: getColour().loops,
|
||||
// helpUrl: '%{BKY_CONTROLS_FOR_HELPURL}',
|
||||
// extensions: ['contextMenu_newGetVariableBlock', 'controls_for_tooltip'],
|
||||
// },
|
||||
// ]);
|
||||
|
||||
Blockly.Blocks['controls_whileUntil'] = {
|
||||
/**
|
||||
* Block for 'do while/until' loop.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS =
|
||||
[[Blockly.Msg.CONTROLS_WHILEUNTIL_OPERATOR_WHILE, 'WHILE'],
|
||||
[Blockly.Msg.CONTROLS_WHILEUNTIL_OPERATOR_UNTIL, 'UNTIL']];
|
||||
this.setHelpUrl(Blockly.Msg.CONTROLS_WHILEUNTIL_HELPURL);
|
||||
this.setColour(getColour().loops);
|
||||
this.appendValueInput('BOOL')
|
||||
.setCheck(getCompatibleTypes(Boolean))
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.CONTROLS_WHILEUNTIL_INPUT_DO);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
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];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Blocks['controls_for'] = {
|
||||
/**
|
||||
* Block for 'for' loop.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.jsonInit({
|
||||
"message0": Blockly.Msg.CONTROLS_FOR_TITLE,
|
||||
"args0": [
|
||||
{
|
||||
"type": "field_variable",
|
||||
"name": "VAR",
|
||||
"variable": null
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "FROM",
|
||||
"check": getCompatibleTypes(Number),
|
||||
"align": "RIGHT"
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "TO",
|
||||
"check": getCompatibleTypes(Number),
|
||||
"align": "RIGHT"
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "BY",
|
||||
"check": getCompatibleTypes(Number),
|
||||
"align": "RIGHT"
|
||||
}
|
||||
],
|
||||
"inputsInline": true,
|
||||
"previousStatement": null,
|
||||
"nextStatement": null,
|
||||
"colour": getColour().loops,
|
||||
"helpUrl": Blockly.Msg.CONTROLS_FOR_HELPURL
|
||||
});
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.CONTROLS_FOR_INPUT_DO);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
return Blockly.Msg.CONTROLS_FOR_TOOLTIP.replace('%1',
|
||||
thisBlock.getFieldValue('VAR'));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Blocks['controls_forEach'] = {
|
||||
/**
|
||||
* Block for 'for each' loop.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
this.jsonInit({
|
||||
"message0": Blockly.Msg.CONTROLS_FOREACH_TITLE,
|
||||
"args0": [
|
||||
{
|
||||
"type": "field_variable",
|
||||
"name": "VAR",
|
||||
"variable": null
|
||||
},
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "LIST",
|
||||
"check": getCompatibleTypes(Array)
|
||||
}
|
||||
],
|
||||
"previousStatement": null,
|
||||
"nextStatement": null,
|
||||
"colour": getColour().loops,
|
||||
"helpUrl": Blockly.Msg.CONTROLS_FOREACH_HELPURL
|
||||
});
|
||||
this.appendStatementInput('DO')
|
||||
.appendField(Blockly.Msg.CONTROLS_FOREACH_INPUT_DO);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function () {
|
||||
return Blockly.Msg.CONTROLS_FOREACH_TOOLTIP.replace('%1',
|
||||
thisBlock.getFieldValue('VAR'));
|
||||
});
|
||||
},
|
||||
customContextMenu: Blockly.Blocks['controls_for'].customContextMenu,
|
||||
/** @returns {!string} The type of the variable used in this block */
|
||||
getVarType: function (varName) {
|
||||
return Blockly.Types.NUMBER;
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Blocks['controls_flow_statements'] = {
|
||||
/**
|
||||
* Block for flow statements: continue, break.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function () {
|
||||
var OPERATORS =
|
||||
[[Blockly.Msg.CONTROLS_FLOW_STATEMENTS_OPERATOR_BREAK, 'BREAK'],
|
||||
[Blockly.Msg.CONTROLS_FLOW_STATEMENTS_OPERATOR_CONTINUE, 'CONTINUE']];
|
||||
this.setHelpUrl(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_HELPURL);
|
||||
this.setColour(getColour().loops);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'FLOW');
|
||||
this.setPreviousStatement(true);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
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];
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Called whenever anything on the workspace changes.
|
||||
* Add warning if this flow block is not nested inside a loop.
|
||||
* @param {!Blockly.Events.Abstract} e Change event.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
onchange: function (e) {
|
||||
var legal = false;
|
||||
// Is the block nested in a loop?
|
||||
var block = this;
|
||||
do {
|
||||
if (this.LOOP_TYPES.indexOf(block.type) !== -1) {
|
||||
legal = true;
|
||||
break;
|
||||
}
|
||||
block = block.getSurroundParent();
|
||||
} while (block);
|
||||
if (legal) {
|
||||
this.setWarningText(null);
|
||||
} else {
|
||||
this.setWarningText(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* List of block types that are loops and thus do not need warnings.
|
||||
* To add a new loop type add this to your code:
|
||||
* Blockly.Blocks['controls_flow_statements'].LOOP_TYPES.push('custom_loop');
|
||||
*/
|
||||
LOOP_TYPES: ['controls_repeat', 'controls_repeat_ext', 'controls_forEach',
|
||||
'controls_for', 'controls_whileUntil']
|
||||
};
|
||||
|
||||
Blockly.Blocks['controls_repeat_ext'] = {
|
||||
/**
|
||||
|
@ -79,6 +79,20 @@
|
||||
"headline": "Verwendung von Schleifen",
|
||||
"text": "Lass die LED genau 5 mal in einem Abstand von 1000 Millisekunden blinken. Anschließend soll die LED ausgeschaltet werden. ",
|
||||
"xml": "<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <block type=\"arduino_functions\" id=\"QWW|$jB8+*EL;}|#uA\" deletable=\"false\" x=\"18\" y=\"18\">\n <statement name=\"SETUP_FUNC\">\n <block type=\"controls_repeat_ext\" id=\"_d{J^FWUT8M?}o[/6Fpj\">\n <value name=\"TIMES\">\n <block type=\"math_number\" id=\"qao_;lzo~kE?25HM*kJ+\">\n <field name=\"NUM\">5</field>\n </block>\n </value>\n <statement name=\"DO\">\n <block type=\"sensebox_led\" id=\"A)}4:wR_79zAHUBq5?1j\">\n <field name=\"PIN\">1</field>\n <field name=\"STAT\">HIGH</field>\n <next>\n <block type=\"time_delay\" id=\"Yfn;,|wxaRhium=T[-wM\">\n <value name=\"DELAY_TIME_MILI\">\n <block type=\"math_number\" id=\"z=1f4hMm_Q~e-+Wvh,S|\">\n <field name=\"NUM\">1000</field>\n </block>\n </value>\n <next>\n <block type=\"sensebox_led\" id=\"nu2%x%_iigf]r$FJ7XEw\">\n <field name=\"PIN\">1</field>\n <field name=\"STAT\">LOW</field>\n <next>\n <block type=\"time_delay\" id=\"4@Y4E|ewWB)([vf/4Ttn\">\n <value name=\"DELAY_TIME_MILI\">\n <block type=\"math_number\" id=\"l,ZAG8;|Uv^:P5/FOjwD\">\n <field name=\"NUM\">1000</field>\n </block>\n </value>\n </block>\n </next>\n </block>\n </next>\n </block>\n </next>\n </block>\n </statement>\n </block>\n </statement>\n </block>\n</xml>"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"type": "instruction",
|
||||
"headline": "Schleife mit Laufzeitvariable",
|
||||
"text": "Mit diesem Block lässt sich die Schleife noch genauer Steuern und beeinflussen. Hierbei wird nicht nur angegeben wie oft die Anweisungen innerhalb der Schleife wiederholt werden sondern auch welche Variable zum zählen verwendet wird und in welchen Schritten gezählt werden soll. Der Vorteil bei diesem Block ist, dass die Wert der Variable auch innerhalb der Anweisungen verwendet werden kann. So kannst zum Beispiel die Variable \"i\" verwenden um die Blinkfrequenz zu beeinflussen.",
|
||||
"xml": "<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <variables>\n <variable id=\")~{j#yh!vpAj!r)xZ%r`\">i</variable>\n </variables>\n <block type=\"arduino_functions\" id=\"QWW|$jB8+*EL;}|#uA\" x=\"27\" y=\"16\">\n <statement name=\"LOOP_FUNC\">\n <block type=\"controls_for\" id=\"FYj`$_+6-llMr!1pbbGa\">\n <field name=\"VAR\" id=\")~{j#yh!vpAj!r)xZ%r`\">i</field>\n <value name=\"FROM\">\n <block type=\"math_number\" id=\"d`8}:TQSpqu@$),hheqW\">\n <field name=\"NUM\">100</field>\n </block>\n </value>\n <value name=\"TO\">\n <block type=\"math_number\" id=\"SugsqbJBjnV.+wt,l*os\">\n <field name=\"NUM\">1000</field>\n </block>\n </value>\n <value name=\"BY\">\n <block type=\"math_number\" id=\"A.r{E[gVmy`GOH[/UjOd\">\n <field name=\"NUM\">100</field>\n </block>\n </value>\n <statement name=\"DO\">\n <block type=\"sensebox_led\" id=\",|)Qs}dfbh`hTL#2:vEr\">\n <field name=\"PIN\">1</field>\n <field name=\"STAT\">HIGH</field>\n <next>\n <block type=\"time_delay\" id=\"P!noJ-RN{(E{=P!1c-un\">\n <value name=\"DELAY_TIME_MILI\">\n <block type=\"variables_get_dynamic\" id=\"bd;B*4HgU:~Vb2kQ9qh.\">\n <field name=\"VAR\" id=\")~{j#yh!vpAj!r)xZ%r`\">i</field>\n </block>\n </value>\n <next>\n <block type=\"sensebox_led\" id=\"_k?%f6^b0WNYifw]yrd7\">\n <field name=\"PIN\">1</field>\n <field name=\"STAT\">LOW</field>\n <next>\n <block type=\"time_delay\" id=\"#BYOOJXBj%)0op!76)z=\">\n <value name=\"DELAY_TIME_MILI\">\n <block type=\"variables_get_dynamic\" id=\"(cg$kq?jc~Zi`6WosPN5\">\n <field name=\"VAR\" id=\")~{j#yh!vpAj!r)xZ%r`\">i</field>\n </block>\n </value>\n </block>\n </next>\n </block>\n </next>\n </block>\n </next>\n </block>\n </statement>\n </block>\n </statement>\n </block>\n</xml>"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"type": "task",
|
||||
"headline": "Schleifen mit Laufzeitvariable",
|
||||
"text": "Verwende die Schleife mit Laufzeitvariable und beeinflusse die Blinkfrequenz mithilfe dem Wert der Variable.",
|
||||
"xml": "<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <variables>\n <variable id=\"*$)UUMo%aBec4cQTwHGx\">i</variable>\n </variables>\n <block type=\"arduino_functions\" id=\"QWW|$jB8+*EL;}|#uA\" deletable=\"false\" x=\"53\" y=\"10\">\n <statement name=\"LOOP_FUNC\">\n <block type=\"controls_for\" id=\"K(eQur,H3CCpZ4aKVehO\">\n <field name=\"VAR\" id=\"*$)UUMo%aBec4cQTwHGx\">i</field>\n <value name=\"FROM\">\n <block type=\"math_number\" id=\"J^UJb^1-SukkH7yBbynj\">\n <field name=\"NUM\">100</field>\n </block>\n </value>\n <value name=\"TO\">\n <block type=\"math_number\" id=\"azP7sh9f,v$9VH[.9BCh\">\n <field name=\"NUM\">1000</field>\n </block>\n </value>\n <value name=\"BY\">\n <block type=\"math_number\" id=\"#j`dag~sK!]D#{_;._)%\">\n <field name=\"NUM\">100</field>\n </block>\n </value>\n <statement name=\"DO\">\n <block type=\"sensebox_led\" id=\"H6+{(4h(}a[yr5w,f(`,\">\n <field name=\"PIN\">1</field>\n <field name=\"STAT\">HIGH</field>\n <next>\n <block type=\"time_delay\" id=\"vPfaX^j~7;YwT+A!y@**\">\n <value name=\"DELAY_TIME_MILI\">\n <block type=\"variables_get_dynamic\" id=\"1}5O.el5^EHJCM`,oa4-\">\n <field name=\"VAR\" id=\"*$)UUMo%aBec4cQTwHGx\">i</field>\n </block>\n </value>\n <next>\n <block type=\"sensebox_led\" id=\"TRn{}$-@^eXI?eGfWJ/{\">\n <field name=\"PIN\">1</field>\n <field name=\"STAT\">LOW</field>\n <next>\n <block type=\"time_delay\" id=\"x@]F,29zp}87PA|2+eoJ\">\n <value name=\"DELAY_TIME_MILI\">\n <block type=\"variables_get_dynamic\" id=\"WF.bP/9w[WlFoF*2;*{G\">\n <field name=\"VAR\" id=\"*$)UUMo%aBec4cQTwHGx\">i</field>\n </block>\n </value>\n </block>\n </next>\n </block>\n </next>\n </block>\n </next>\n </block>\n </statement>\n </block>\n </statement>\n </block>\n <block type=\"math_number\" id=\"!42y97D|+Vi9*DKE^5p`\" disabled=\"true\" x=\"198\" y=\"184\">\n <field name=\"NUM\">1000</field>\n </block>\n</xml>"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user