Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 148 additions & 7 deletions Blockly/html/agc/blocks/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Blockly.defineBlocksWithJsonArray([
{
'type': 'agc_controls_if',
'type': 'controls_if',
'message0': '%{BKY_CONTROLS_IF_MSG_IF} %1',
'args0': [
{
Expand All @@ -28,7 +28,7 @@ Blockly.defineBlocksWithJsonArray([
'helpUrl': '%{BKY_CONTROLS_IF_HELPURL}',
},
{
'type': 'agc_controls_ifelse',
'type': 'controls_ifelse',
'message0': '%{BKY_CONTROLS_IF_MSG_IF} %1',
'args0': [
{
Expand Down Expand Up @@ -59,7 +59,7 @@ Blockly.defineBlocksWithJsonArray([
},
// Block for boolean data type: true and false.
{
'type': 'agc_logic_boolean',
'type': 'logic_boolean',
'message0': '%1',
'args0': [
{
Expand All @@ -78,7 +78,7 @@ Blockly.defineBlocksWithJsonArray([
},
// Block for comparison operator.
{
'type': 'agc_logic_compare',
'type': 'logic_compare',
'message0': '%1 %2 %3',
'args0': [
{
Expand Down Expand Up @@ -110,7 +110,7 @@ Blockly.defineBlocksWithJsonArray([
},
// Block for logical operations: 'and', 'or'.
{
'type': 'agc_logic_operation',
'type': 'logic_operation',
'message0': '%1 %2 %3',
'args0': [
{
Expand Down Expand Up @@ -140,7 +140,7 @@ Blockly.defineBlocksWithJsonArray([
},
// Block for negation.
{
'type': 'agc_logic_negate',
'type': 'logic_negate',
'message0': '%{BKY_LOGIC_NEGATE_TITLE}',
'args0': [
{
Expand All @@ -156,7 +156,7 @@ Blockly.defineBlocksWithJsonArray([
},
// Block for ternary operator.
{
'type': 'agc_logic_ternary',
'type': 'logic_ternary',
'message0': '%{BKY_LOGIC_TERNARY_CONDITION} %1',
'args0': [
{
Expand Down Expand Up @@ -186,3 +186,144 @@ Blockly.defineBlocksWithJsonArray([
'extensions': ['logic_ternary'],
},
]);

(function() {
/**
* Adds dynamic type validation for the left and right sides of a logic_compare
* block.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
const LOGIC_COMPARE_ONCHANGE_MIXIN = {
/**
* Called whenever anything on the workspace changes.
* Prevent mismatched types from being compared.
* @param {!Blockly.Events.Abstract} e Change event.
* @this {Blockly.Block}
*/
onchange: function(e) {
if (!this.prevBlocks_) {
this.prevBlocks_ = [null, null];
}

const blockA = this.getInputTargetBlock('A');
const blockB = this.getInputTargetBlock('B');
// Disconnect blocks that existed prior to this change if they don't match.
if (blockA && blockB &&
!this.workspace.connectionChecker.doTypeChecks(
blockA.outputConnection, blockB.outputConnection)) {
// Mismatch between two inputs. Revert the block connections,
// bumping away the newly connected block(s).
Blockly.Events.setGroup(e.group);
const prevA = this.prevBlocks_[0];
if (prevA !== blockA) {
blockA.unplug();
if (prevA && !prevA.isDisposed() && !prevA.isShadow()) {
// The shadow block is automatically replaced during unplug().
this.getInput('A').connection.connect(prevA.outputConnection);
}
}
const prevB = this.prevBlocks_[1];
if (prevB !== blockB) {
blockB.unplug();
if (prevB && !prevB.isDisposed() && !prevB.isShadow()) {
// The shadow block is automatically replaced during unplug().
this.getInput('B').connection.connect(prevB.outputConnection);
}
}
this.bumpNeighbours();
Blockly.Events.setGroup(false);
}
this.prevBlocks_[0] = this.getInputTargetBlock('A');
this.prevBlocks_[1] = this.getInputTargetBlock('B');
},
};

/**
* "logic_compare" extension function. Adds type left and right side type
* checking to "logic_compare" blocks.
* @this {Blockly.Block}
* @package
* @readonly
*/
const LOGIC_COMPARE_EXTENSION = function() {
// Add onchange handler to ensure types are compatible.
this.mixin(LOGIC_COMPARE_ONCHANGE_MIXIN);
};

Blockly.Extensions.register('logic_compare', LOGIC_COMPARE_EXTENSION);

/**
* Tooltip text, keyed by block OP value. Used by logic_compare and
* logic_operation blocks.
* @see {Blockly.Extensions#buildTooltipForDropdown}
* @package
* @readonly
*/
const TOOLTIPS_BY_OP = {
// logic_compare
'EQ': '%{BKY_LOGIC_COMPARE_TOOLTIP_EQ}',
'NEQ': '%{BKY_LOGIC_COMPARE_TOOLTIP_NEQ}',
'LT': '%{BKY_LOGIC_COMPARE_TOOLTIP_LT}',
'LTE': '%{BKY_LOGIC_COMPARE_TOOLTIP_LTE}',
'GT': '%{BKY_LOGIC_COMPARE_TOOLTIP_GT}',
'GTE': '%{BKY_LOGIC_COMPARE_TOOLTIP_GTE}',

// logic_operation
'AND': '%{BKY_LOGIC_OPERATION_TOOLTIP_AND}',
'OR': '%{BKY_LOGIC_OPERATION_TOOLTIP_OR}',
};

Blockly.Extensions.register('logic_op_tooltip',
Blockly.Extensions.buildTooltipForDropdown('OP', TOOLTIPS_BY_OP));

/**
* Adds type coordination between inputs and output.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
const LOGIC_TERNARY_ONCHANGE_MIXIN = {
prevParentConnection_: null,

/**
* Called whenever anything on the workspace changes.
* Prevent mismatched types.
* @param {!Blockly.Events.Abstract} e Change event.
* @this {Blockly.Block}
*/
onchange: function(e) {
const blockA = this.getInputTargetBlock('THEN');
const blockB = this.getInputTargetBlock('ELSE');
const parentConnection = this.outputConnection.targetConnection;
// Disconnect blocks that existed prior to this change if they don't match.
if ((blockA || blockB) && parentConnection) {
for (let i = 0; i < 2; i++) {
const block = (i === 1) ? blockA : blockB;
if (block &&
!block.workspace.connectionChecker.doTypeChecks(
block.outputConnection, parentConnection)) {
// Ensure that any disconnections are grouped with the causing event.
Blockly.Events.setGroup(e.group);
if (parentConnection === this.prevParentConnection_) {
this.unplug();
parentConnection.getSourceBlock().bumpNeighbours();
} else {
block.unplug();
block.bumpNeighbours();
}
Blockly.Events.setGroup(false);
}
}
}
this.prevParentConnection_ = parentConnection;
},
};

Blockly.Extensions.registerMixin('logic_ternary',
LOGIC_TERNARY_ONCHANGE_MIXIN);

})();
10 changes: 9 additions & 1 deletion Blockly/html/agc/blocks/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Blockly.defineBlocksWithJsonArray([
// Block for 'do while/until' loop.
{
'type': 'agc_controls_whileUntil',
'type': 'controls_whileUntil',
'message0': '%1 %2',
'args0': [
{
Expand Down Expand Up @@ -37,3 +37,11 @@ Blockly.defineBlocksWithJsonArray([
'extensions': ['controls_whileUntil_tooltip'],
},
]);


Blockly.Extensions.register('controls_whileUntil_tooltip',
Blockly.Extensions.buildTooltipForDropdown(
'MODE', {
'WHILE': '%{BKY_CONTROLS_WHILEUNTIL_TOOLTIP_WHILE}',
'UNTIL': '%{BKY_CONTROLS_WHILEUNTIL_TOOLTIP_UNTIL}',
}));
85 changes: 81 additions & 4 deletions Blockly/html/agc/blocks/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Blockly.defineBlocksWithJsonArray([
// Block for numeric value.
{
'type': 'agc_math_number',
'type': 'math_number',
'message0': '%1',
'args0': [{
'type': 'field_number',
Expand All @@ -25,7 +25,7 @@ Blockly.defineBlocksWithJsonArray([

// Block for basic arithmetic operator.
{
'type': 'agc_math_arithmetic',
'type': 'math_arithmetic',
'message0': '%1 %2 %3',
'args0': [
{
Expand Down Expand Up @@ -57,7 +57,7 @@ Blockly.defineBlocksWithJsonArray([

// Block for random integer between 0 and [X-1].
{
"type": "agc_math_random_int_0",
"type": "math_random_int_0",
"message0": "random integer to %1",
"args0": [
{
Expand All @@ -75,7 +75,7 @@ Blockly.defineBlocksWithJsonArray([

// Block for random integer between [X] and [Y].
{
"type": "agc_math_random_int",
"type": "math_random_int",
"message0": "%{BKY_MATH_RANDOM_INT_TITLE}",
"args0": [
{
Expand All @@ -95,4 +95,81 @@ Blockly.defineBlocksWithJsonArray([
"tooltip": "%{BKY_MATH_RANDOM_INT_TOOLTIP}",
"helpUrl": "%{BKY_MATH_RANDOM_INT_HELPURL}",
},
// Block for adding to a variable in place.
{
"type": "math_change",
"message0": "%{BKY_MATH_CHANGE_TITLE}",
"args0": [
{
"type": "field_variable",
"name": "VAR",
"variable": "%{BKY_MATH_CHANGE_TITLE_ITEM}",
},
{
"type": "input_value",
"name": "DELTA",
"check": "Number",
},
],
"previousStatement": null,
"nextStatement": null,
"style": "variable_blocks",
"helpUrl": "%{BKY_MATH_CHANGE_HELPURL}",
"extensions": ["math_change_tooltip"],
},
]);

// Update the tooltip of 'math_change' block to reference the variable.
Blockly.Extensions.register('math_change_tooltip',
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_MATH_CHANGE_TOOLTIP}', 'VAR'));


(function() {
/**
* Mapping of math block OP value to tooltip message for blocks
* math_arithmetic, math_simple, math_trig, and math_on_lists.
* @see {Blockly.Extensions#buildTooltipForDropdown}
* @package
* @readonly
*/
const TOOLTIPS_BY_OP = {
// math_arithmetic
'ADD': '%{BKY_MATH_ARITHMETIC_TOOLTIP_ADD}',
'MINUS': '%{BKY_MATH_ARITHMETIC_TOOLTIP_MINUS}',
'MULTIPLY': '%{BKY_MATH_ARITHMETIC_TOOLTIP_MULTIPLY}',
'DIVIDE': '%{BKY_MATH_ARITHMETIC_TOOLTIP_DIVIDE}',
'POWER': '%{BKY_MATH_ARITHMETIC_TOOLTIP_POWER}',

// math_simple
'ROOT': '%{BKY_MATH_SINGLE_TOOLTIP_ROOT}',
'ABS': '%{BKY_MATH_SINGLE_TOOLTIP_ABS}',
'NEG': '%{BKY_MATH_SINGLE_TOOLTIP_NEG}',
'LN': '%{BKY_MATH_SINGLE_TOOLTIP_LN}',
'LOG10': '%{BKY_MATH_SINGLE_TOOLTIP_LOG10}',
'EXP': '%{BKY_MATH_SINGLE_TOOLTIP_EXP}',
'POW10': '%{BKY_MATH_SINGLE_TOOLTIP_POW10}',

// math_trig
'SIN': '%{BKY_MATH_TRIG_TOOLTIP_SIN}',
'COS': '%{BKY_MATH_TRIG_TOOLTIP_COS}',
'TAN': '%{BKY_MATH_TRIG_TOOLTIP_TAN}',
'ASIN': '%{BKY_MATH_TRIG_TOOLTIP_ASIN}',
'ACOS': '%{BKY_MATH_TRIG_TOOLTIP_ACOS}',
'ATAN': '%{BKY_MATH_TRIG_TOOLTIP_ATAN}',

// math_on_lists
'SUM': '%{BKY_MATH_ONLIST_TOOLTIP_SUM}',
'MIN': '%{BKY_MATH_ONLIST_TOOLTIP_MIN}',
'MAX': '%{BKY_MATH_ONLIST_TOOLTIP_MAX}',
'AVERAGE': '%{BKY_MATH_ONLIST_TOOLTIP_AVERAGE}',
'MEDIAN': '%{BKY_MATH_ONLIST_TOOLTIP_MEDIAN}',
'MODE': '%{BKY_MATH_ONLIST_TOOLTIP_MODE}',
'STD_DEV': '%{BKY_MATH_ONLIST_TOOLTIP_STD_DEV}',
'RANDOM': '%{BKY_MATH_ONLIST_TOOLTIP_RANDOM}',
};

Blockly.Extensions.register('math_op_tooltip',
Blockly.Extensions.buildTooltipForDropdown(
'OP', TOOLTIPS_BY_OP));
})();
Loading