| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
|
|
| 'use strict';
|
|
|
| goog.provide('Blockly.ScratchBlocks.ProcedureUtils');
|
|
|
| goog.require('Blockly.Blocks');
|
| goog.require('Blockly.Colours');
|
| goog.require('Blockly.constants');
|
| goog.require('Blockly.ScratchBlocks.VerticalExtensions');
|
|
|
|
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.callerMutationToDom = function() {
|
| var container = document.createElement('mutation');
|
| container.setAttribute('proccode', this.procCode_.replaceAll('<', '<'));
|
| container.setAttribute('argumentids', JSON.stringify(this.argumentIds_));
|
| container.setAttribute('warp', JSON.stringify(this.warp_));
|
| container.setAttribute('returns', JSON.stringify(this.output_));
|
| container.setAttribute('edited', JSON.stringify(this.edited));
|
| container.setAttribute('optype', JSON.stringify(this.outputType));
|
| container.setAttribute('color', JSON.stringify(this.color));
|
| container.innerText = this.image;
|
| return container;
|
| };
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.callerDomToMutation = function(xmlElement) {
|
| this.procCode_ = xmlElement.getAttribute('proccode').replaceAll('<', '<');
|
| this.generateShadows_ =
|
| JSON.parse(xmlElement.getAttribute('generateshadows'));
|
| this.argumentIds_ = JSON.parse(xmlElement.getAttribute('argumentids'));
|
| this.warp_ = JSON.parse(xmlElement.getAttribute('warp'));
|
| this.output_ = JSON.parse(xmlElement.getAttribute('returns'));
|
| this.edited = JSON.parse(xmlElement.getAttribute('edited'));
|
| this.outputType = JSON.parse(xmlElement.getAttribute('optype'));
|
| this.color = JSON.parse(xmlElement.getAttribute('color'));
|
|
|
| if (!this.color) this.color = [Blockly.Colours.more.primary, Blockly.Colours.more.secondary, Blockly.Colours.more.tertiary]
|
| if (this.color && this.color.primary) {
|
| this.color = [this.color.primary, this.color.secondary, this.color.tertiary]
|
| }
|
|
|
| this.color = this.color.map(c => c.slice(0, 7))
|
| this.image = xmlElement.innerText;
|
| this.updateDisplay_();
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.definitionMutationToDom = function(
|
| opt_generateShadows) {
|
| var container = document.createElement('mutation');
|
|
|
| if (opt_generateShadows) {
|
| container.setAttribute('generateshadows', true);
|
| }
|
| container.setAttribute('proccode', this.procCode_.replaceAll('<', '<'));
|
| container.setAttribute('argumentids', JSON.stringify(this.argumentIds_));
|
| container.setAttribute('argumentnames', JSON.stringify(this.displayNames_));
|
| container.setAttribute('argumentdefaults', JSON.stringify(this.argumentDefaults_));
|
| container.setAttribute('warp', JSON.stringify(this.warp_));
|
| container.setAttribute('returns', JSON.stringify(this.output_));
|
| container.setAttribute('edited', JSON.stringify(this.edited));
|
| container.setAttribute('optype', JSON.stringify(this.outputType));
|
| container.setAttribute('color', JSON.stringify(this.color));
|
| container.innerText = this.image;
|
| return container;
|
| };
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.definitionDomToMutation = function(xmlElement) {
|
| this.procCode_ = xmlElement.getAttribute('proccode').replaceAll('<', '<');
|
| this.warp_ = JSON.parse(xmlElement.getAttribute('warp'));
|
|
|
| var prevArgIds = this.argumentIds_;
|
| var prevDisplayNames = this.displayNames_;
|
|
|
| this.argumentIds_ = JSON.parse(xmlElement.getAttribute('argumentids'));
|
| this.displayNames_ = JSON.parse(xmlElement.getAttribute('argumentnames'));
|
| this.argumentDefaults_ = JSON.parse(xmlElement.getAttribute('argumentdefaults'));
|
| this.output_ = JSON.parse(xmlElement.getAttribute('returns'));
|
| this.outputType = JSON.parse(xmlElement.getAttribute('optype'));
|
| this.edited = JSON.parse(xmlElement.getAttribute('edited'));
|
| this.image = xmlElement.innerText;
|
| this.color = JSON.parse(xmlElement.getAttribute('color'));
|
|
|
| if (!this.color) this.color = [Blockly.Colours.more.primary, Blockly.Colours.more.secondary, Blockly.Colours.more.tertiary]
|
| if (this.color && this.color.primary) {
|
| this.color = [this.color.primary, this.color.secondary, this.color.tertiary]
|
| }
|
|
|
| this.color = this.color.map(c => c.slice(0, 7))
|
| this.image = xmlElement.innerText;
|
| this.updateDisplay_();
|
| if (this.updateArgumentReporterNames_) {
|
| this.updateArgumentReporterNames_(prevArgIds, prevDisplayNames);
|
| }
|
| };
|
|
|
|
|
|
|
|
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.getProcCode = function() {
|
| return this.procCode_;
|
| };
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_ = function() {
|
| var wasRendered = this.rendered;
|
|
|
| var ConectionType = (this.outputType || (this.output_ ? 'string' : 'statement')).toLowerCase()
|
| this.rendered = false;
|
|
|
| var connectionMap = this.disconnectOldBlocks_();
|
| this.removeAllInputs_();
|
|
|
| this.createAllInputs_(connectionMap);
|
| this.deleteShadows_(connectionMap);
|
| this.setOutputShape(Blockly.OUTPUT_SHAPE_SQUARE);
|
|
|
| this.setColour(...this.color)
|
| if (
|
| this.outputConnection &&
|
| this.outputConnection.targetConnection &&
|
| this.outputConnection.targetConnection.sourceBlock_.type === 'procedures_definition_return') {
|
| this.outputConnection.targetConnection.sourceBlock_.setColour(...this.color)
|
| }
|
| if (
|
| this.previousConnection &&
|
| this.previousConnection.targetConnection &&
|
| this.previousConnection.targetConnection.sourceBlock_.type === 'procedures_definition') {
|
| this.previousConnection.targetConnection.sourceBlock_.setColour(...this.color)
|
| }
|
| if (this.output_) {
|
| this.setPreviousStatement(false)
|
| this.setNextStatement(false)
|
| switch (ConectionType) {
|
| case 'string':
|
| case 'number':
|
| default:
|
| this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND); break;
|
| case 'boolean':
|
| this.setOutputShape(Blockly.OUTPUT_SHAPE_HEXAGONAL); break;
|
| }
|
| this.setOutput(this.output_, this.isDisplayOnly ? 'procedure' : null);
|
| } else {
|
| this.setOutput(false)
|
| switch (ConectionType) {
|
| case 'statement':
|
| this.setPreviousStatement(!this.output_, this.isDisplayOnly ? 'procedure' : 'normal')
|
| this.setNextStatement(!this.output_, this.isDisplayOnly ? 'procedure' : 'normal')
|
| break
|
| case 'end':
|
| this.setPreviousStatement(!this.output_, this.isDisplayOnly ? 'procedure' : 'normal')
|
| this.setNextStatement(false, 'procedure')
|
| break
|
| }
|
| }
|
|
|
| this.rendered = wasRendered;
|
| if (wasRendered && !this.isInsertionMarker()) {
|
| this.initSvg();
|
| this.render();
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_ = function() {
|
|
|
| var connectionMap = {};
|
| for (var i = 0, input; input = this.inputList[i]; i++) {
|
| if (input.connection) {
|
| var target = input.connection.targetBlock();
|
| var saveInfo = {
|
| shadow: input.connection.getShadowDom(),
|
| block: target
|
| };
|
| connectionMap[input.name] = saveInfo;
|
|
|
|
|
|
|
|
|
| input.connection.setShadowDom(null);
|
| if (target) {
|
| input.connection.disconnect();
|
| }
|
| }
|
| }
|
| return connectionMap;
|
| };
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_ = function() {
|
|
|
|
|
| for (var i = 0, input; input = this.inputList[i]; i++) {
|
| input.dispose();
|
| }
|
| this.inputList = [];
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.createIcon_ = function() {
|
| if (!this.image) return
|
| if (this.image.startsWith('data:') || this.image.startsWith('http')) {
|
| const iconField = new Blockly.FieldImage(this.image, 40, 40);
|
| const separatorField = new Blockly.FieldVerticalSeparator();
|
| this.appendDummyInput()
|
| .appendField(iconField)
|
| .appendField(separatorField);
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_ = function(connectionMap) {
|
| this.createIcon_()
|
|
|
| var procComponents = this.procCode_.split(/(?=[^\\]%[nsbc])/);
|
| procComponents = procComponents.map(function(c) {
|
| return c.trim();
|
| });
|
|
|
| var argumentCount = 0;
|
| for (var i = 0, component; component = procComponents[i]; i++) {
|
| var labelText;
|
| var argumentType = component.substring(1, 2);
|
| var id = this.argumentIds_[argumentCount];
|
|
|
| if (component.substring(0, 1) == '%' && (['n', 's', 'b', 'c'].includes(argumentType)) && id) {
|
| |
| |
| |
| |
| |
|
|
| labelText = component.substring(2).trim();
|
|
|
| if (argumentType == "c") {
|
| var input = this.appendStatementInput(id)
|
| } else {
|
| var input = this.appendValueInput(id);
|
| }
|
| if (argumentType == 'b') {
|
| input.setCheck('Boolean');
|
| }
|
| this.populateArgument_(argumentType, argumentCount, connectionMap, id,
|
| input);
|
| argumentCount++;
|
| } else {
|
| labelText = component.trim();
|
| }
|
| this.addProcedureLabel_(labelText.replace(/\\%/, '%'));
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_ = function(connectionMap) {
|
|
|
| if (connectionMap) {
|
| for (var id in connectionMap) {
|
| var saveInfo = connectionMap[id];
|
| if (saveInfo) {
|
| var block = saveInfo['block'];
|
| if (block && block.isShadow()) {
|
| block.dispose();
|
| connectionMap[id] = null;
|
|
|
|
|
| }
|
| }
|
| }
|
| }
|
| };
|
|
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.addLabelField_ = function(text) {
|
| this.appendDummyInput().appendField(text);
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.addLabelEditor_ = function(text) {
|
| if (text) {
|
| this.appendDummyInput(Blockly.utils.genUid()).
|
| appendField(new Blockly.FieldTextInputRemovable(text));
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.buildShadowDom_ = function(type) {
|
| var shadowDom = goog.dom.createDom('shadow');
|
| if (type == 'n') {
|
| var shadowType = 'math_number';
|
| var fieldName = 'NUM';
|
| var fieldValue = '1';
|
| } else {
|
| var shadowType = 'text';
|
| var fieldName = 'TEXT';
|
| var fieldValue = '';
|
| }
|
| shadowDom.setAttribute('type', shadowType);
|
| var fieldDom = goog.dom.createDom('field', null, fieldValue);
|
| fieldDom.setAttribute('name', fieldName);
|
| shadowDom.appendChild(fieldDom);
|
| return shadowDom;
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.attachShadow_ = function(input,
|
| argumentType) {
|
| if (argumentType == 'n' || argumentType == 's') {
|
| var blockType = argumentType == 'n' ? 'math_number' : 'text';
|
| Blockly.Events.disable();
|
| try {
|
| var newBlock = this.workspace.newBlock(blockType);
|
| if (argumentType == 'n') {
|
| newBlock.setFieldValue('1', 'NUM');
|
| } else {
|
| newBlock.setFieldValue('', 'TEXT');
|
| }
|
| newBlock.setShadow(true);
|
| if (!this.isInsertionMarker()) {
|
| newBlock.initSvg();
|
| newBlock.render(false);
|
| }
|
| } finally {
|
| Blockly.Events.enable();
|
| }
|
| if (Blockly.Events.isEnabled()) {
|
| Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
|
| }
|
| newBlock.outputConnection.connect(input.connection);
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.createArgumentReporter_ = function(
|
| argumentType, displayName) {
|
| switch (argumentType) {
|
| case 'n':
|
| case 's':
|
| var blockType = 'argument_reporter_string_number';
|
| break;
|
| case 'b':
|
| var blockType = 'argument_reporter_boolean';
|
| break;
|
| case 'c':
|
| var blockType = 'argument_reporter_command';
|
| break;
|
| }
|
| Blockly.Events.disable();
|
| try {
|
| var newBlock = this.workspace.newBlock(blockType);
|
| newBlock.setShadow(true);
|
| newBlock.setFieldValue(displayName, 'VALUE');
|
| newBlock.color = this.color
|
| newBlock.setColour(...this.color)
|
| if (!this.isInsertionMarker()) {
|
| newBlock.initSvg();
|
| newBlock.render(false);
|
| }
|
| } finally {
|
| Blockly.Events.enable();
|
| }
|
| if (Blockly.Events.isEnabled()) {
|
| Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
|
| }
|
| return newBlock;
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnCaller_ = function(type,
|
| index, connectionMap, id, input) {
|
| var oldBlock = null;
|
| var oldShadow = null;
|
| if (connectionMap && (id in connectionMap)) {
|
| var saveInfo = connectionMap[id];
|
| oldBlock = saveInfo['block'];
|
| oldShadow = saveInfo['shadow'];
|
| }
|
|
|
| if (connectionMap && oldBlock) {
|
|
|
| connectionMap[input.name] = null;
|
| if (type == "c") {
|
| oldBlock.previousConnection.connect(input.connection);
|
| } else {
|
| oldBlock.outputConnection.connect(input.connection);
|
| }
|
| if (type != 'b' && type != 'c' && this.generateShadows_) {
|
| var shadowDom = oldShadow || this.buildShadowDom_(type);
|
| console.log("setting shadow dom: " + shadowDom);
|
| input.connection.setShadowDom(shadowDom);
|
| }
|
| } else if (this.generateShadows_) {
|
| this.attachShadow_(input, type);
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnPrototype_ = function(
|
| type, index, connectionMap, id, input) {
|
| var oldBlock = null;
|
| if (connectionMap && (id in connectionMap)) {
|
| var saveInfo = connectionMap[id];
|
| oldBlock = saveInfo['block'];
|
| }
|
|
|
| var oldTypeMatches =
|
| Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_(oldBlock, type);
|
| var displayName = this.displayNames_[index];
|
|
|
|
|
| if (connectionMap && oldBlock && oldTypeMatches) {
|
|
|
|
|
| var argumentReporter = oldBlock;
|
| argumentReporter.setFieldValue(displayName, 'VALUE');
|
| connectionMap[input.name] = null;
|
| } else {
|
| var argumentReporter = this.createArgumentReporter_(type, displayName);
|
| }
|
|
|
|
|
| if (type == "c") {
|
| input.connection.connect(argumentReporter.previousConnection);
|
| } else {
|
| input.connection.connect(argumentReporter.outputConnection);
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnDeclaration_ = function(
|
| type, index, connectionMap, id, input) {
|
|
|
| var oldBlock = null;
|
| if (connectionMap && (id in connectionMap)) {
|
| var saveInfo = connectionMap[id];
|
| oldBlock = saveInfo['block'];
|
| }
|
|
|
|
|
|
|
|
|
| var oldTypeMatches =
|
| Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_(oldBlock, type);
|
| var displayName = this.displayNames_[index];
|
|
|
|
|
| if (oldBlock && oldTypeMatches) {
|
| var argumentEditor = oldBlock;
|
| oldBlock.setFieldValue(displayName, 'TEXT');
|
| connectionMap[input.name] = null;
|
| } else {
|
| var argumentEditor = this.createArgumentEditor_(type, displayName);
|
| }
|
|
|
|
|
| if (type == "c") {
|
| input.connection.connect(argumentEditor.previousConnection);
|
| } else {
|
| input.connection.connect(argumentEditor.outputConnection);
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.checkOldTypeMatches_ = function(oldBlock,
|
| type) {
|
| if (!oldBlock) {
|
| return false;
|
| }
|
| if ((type == 'n' || type == 's') &&
|
| oldBlock.type == 'argument_reporter_string_number') {
|
| return true;
|
| }
|
| if (type == 'b' && oldBlock.type == 'argument_reporter_boolean') {
|
| return true;
|
| }
|
|
|
| if (type == 'c' && oldBlock.type == 'argument_reporter_command') {
|
| return true;
|
| }
|
| return false;
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.createArgumentEditor_ = function(
|
| argumentType, displayName) {
|
| Blockly.Events.disable();
|
| try {
|
| switch (argumentType) {
|
| case 'n':
|
| case 's':
|
| var newBlock = this.workspace.newBlock('argument_editor_string_number');
|
| break;
|
| case 'b':
|
| var newBlock = this.workspace.newBlock('argument_editor_boolean');
|
| break;
|
| case 'c':
|
| var newBlock = this.workspace.newBlock('argument_editor_command')
|
| }
|
| newBlock.setFieldValue(displayName, 'TEXT');
|
| newBlock.setShadow(true);
|
| if (!this.isInsertionMarker()) {
|
| newBlock.initSvg();
|
| newBlock.render(false);
|
| }
|
| } finally {
|
| Blockly.Events.enable();
|
| }
|
| if (Blockly.Events.isEnabled()) {
|
| Blockly.Events.fire(new Blockly.Events.BlockCreate(newBlock));
|
| }
|
| return newBlock;
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.updateDeclarationProcCode_ = function() {
|
| this.procCode_ = '';
|
| this.displayNames_ = [];
|
| this.argumentIds_ = [];
|
| for (var i = 0; i < this.inputList.length; i++) {
|
| if (i != 0) {
|
| this.procCode_ += ' ';
|
| }
|
| var input = this.inputList[i];
|
| if (input.type == Blockly.DUMMY_INPUT) {
|
| this.procCode_ += input.fieldRow[0].getValue();
|
| } else if (input.type == Blockly.INPUT_VALUE || input.type == Blockly.NEXT_STATEMENT) {
|
|
|
| var target = input.connection.targetBlock();
|
| this.displayNames_.push(target.getFieldValue('TEXT'));
|
| this.argumentIds_.push(input.name);
|
| switch (target.type) {
|
| case 'argument_editor_string_number':
|
| this.procCode_ += '%s';
|
| break;
|
| case 'argument_editor_boolean':
|
| this.procCode_ += '%b';
|
| break;
|
| case 'argument_editor_command':
|
| this.procCode_ += "%c";
|
| break;
|
| }
|
| } else {
|
| throw new Error(
|
| 'Unexpected input type on a procedure mutator root: ' + input.type);
|
| }
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.focusLastEditor_ = function() {
|
| if (this.inputList.length > 0) {
|
| var newInput = this.inputList[this.inputList.length - 1];
|
| if (newInput.type == Blockly.DUMMY_INPUT) {
|
| newInput.fieldRow[0].showEditor_();
|
| } else if (newInput.type == Blockly.INPUT_VALUE) {
|
|
|
| var target = newInput.connection.targetBlock();
|
| target.getField('TEXT').showEditor_();
|
| }
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.addLabelExternal = function() {
|
| Blockly.WidgetDiv.hide(true);
|
| this.procCode_ = this.procCode_ + ' label text';
|
| this.updateDisplay_();
|
| this.focusLastEditor_();
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.addBooleanExternal = function() {
|
| Blockly.WidgetDiv.hide(true);
|
| this.procCode_ = this.procCode_ + ' %b';
|
| this.displayNames_.push('boolean');
|
| this.argumentIds_.push(Blockly.utils.genUid());
|
| this.argumentDefaults_.push('false');
|
| this.updateDisplay_();
|
| this.focusLastEditor_();
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.addCommandExternal = function () {
|
| Blockly.WidgetDiv.hide(true);
|
| this.procCode_ = this.procCode_ + " %c";
|
| this.displayNames_.push("branch");
|
| this.argumentIds_.push("SUBSTACK" + Blockly.utils.genUid());
|
| this.argumentDefaults_.push("");
|
| this.updateDisplay_();
|
| this.focusLastEditor_();
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.addStringNumberExternal = function() {
|
| Blockly.WidgetDiv.hide(true);
|
| this.procCode_ = this.procCode_ + ' %s';
|
| this.displayNames_.push('number or text');
|
| this.argumentIds_.push(Blockly.utils.genUid());
|
| this.argumentDefaults_.push('');
|
| this.updateDisplay_();
|
| this.focusLastEditor_();
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.getWarp = function() {
|
| return this.warp_;
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.getReturns = function() {
|
| return this.output_;
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.getEdited = function() {
|
| return this.edited;
|
| }
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.setEdited = function(edited) {
|
| this.edited = edited;
|
| }
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.setReturns = function(returns) {
|
| this.output_ = returns;
|
| this.updateDisplay_();
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.setType = function(type) {
|
| this.outputType = type.toLowerCase()
|
| this.updateDisplay_();
|
| }
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.setImage = function(image) {
|
| this.image = image
|
| this.updateDisplay_();
|
| }
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.unsetImage = function() {
|
| this.image = ''
|
| this.updateDisplay_();
|
| }
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.setColor = function(primary, secondary, tertiary) {
|
| this.color = [primary, secondary, tertiary]
|
| this.updateDisplay_();
|
| }
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.removeColor = function() {
|
| this.color = [Blockly.Colours.more.primary, Blockly.Colours.more.secondary, Blockly.Colours.more.tertiary]
|
| this.updateDisplay_();
|
| }
|
|
|
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.setWarp = function(warp) {
|
| this.warp_ = warp;
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.removeFieldCallback = function(field) {
|
|
|
| if (this.inputList.length === 1) {
|
| return;
|
| }
|
| var inputNameToRemove = null;
|
| for (var n = 0; n < this.inputList.length; n++) {
|
| var input = this.inputList[n];
|
| if (input.connection) {
|
| var target = input.connection.targetBlock();
|
| if (target.getField(field.name) == field) {
|
| inputNameToRemove = input.name;
|
| }
|
| } else {
|
| for (var j = 0; j < input.fieldRow.length; j++) {
|
| if (input.fieldRow[j] == field) {
|
| inputNameToRemove = input.name;
|
| }
|
| }
|
| }
|
| }
|
| if (inputNameToRemove) {
|
| Blockly.WidgetDiv.hide(true);
|
| this.removeInput(inputNameToRemove);
|
| this.onChangeFn();
|
| this.updateDisplay_();
|
| }
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_ = function(field) {
|
| if (this.parentBlock_ && this.parentBlock_.removeFieldCallback) {
|
| this.parentBlock_.removeFieldCallback(field);
|
| }
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| Blockly.ScratchBlocks.ProcedureUtils.updateArgumentReporterNames_ = function(prevArgIds, prevDisplayNames) {
|
| var nameChanges = [];
|
| var argReporters = [];
|
| var definitionBlock = this.getParent();
|
| if (!definitionBlock) return;
|
|
|
|
|
| var allBlocks = definitionBlock.getDescendants(false);
|
| for (var i = 0; i < allBlocks.length; i++) {
|
| var block = allBlocks[i];
|
| if ((block.type === 'argument_reporter_string_number' ||
|
| block.type === 'argument_reporter_boolean' ||
|
| block.type === 'argument_reporter_command') &&
|
| !block.isShadow()) {
|
| argReporters.push(block);
|
| }
|
| }
|
|
|
|
|
|
|
| for (var i = 0, id; id = this.argumentIds_[i]; i++) {
|
|
|
| var prevIndex = prevArgIds.indexOf(id);
|
| if (prevIndex == -1) continue;
|
| var prevName = prevDisplayNames[prevIndex];
|
| if (prevName != this.displayNames_[i]) {
|
| nameChanges.push({
|
| newName: this.displayNames_[i],
|
| blocks: argReporters.filter(function(block) {
|
| return block.getFieldValue('VALUE') == prevName;
|
| })
|
| });
|
| }
|
| }
|
|
|
|
|
|
|
| for (var j = 0, nameChange; nameChange = nameChanges[j]; j++) {
|
| for (var k = 0, block; block = nameChange.blocks[k]; k++) {
|
| block.setFieldValue(nameChange.newName, 'VALUE');
|
| }
|
| }
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.argumentReporterMutationToDom = function() {
|
| var dom = document.createElement('mutation');
|
| dom.setAttribute('color', JSON.stringify(this.color))
|
| return dom
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.argumentReporterDomToMutation = function(dom) {
|
| try {
|
| this.color = JSON.parse(dom.getAttribute('color'))
|
| this.updateDisplay_()
|
| } catch (err) {
|
| console.warn('unknown old argument reporter')
|
| }
|
| };
|
|
|
| Blockly.ScratchBlocks.ProcedureUtils.argumentReporterUpdateDisplay = function(dom) {
|
| this.setColour(...this.color)
|
| };
|
|
|
| Blockly.Blocks['procedures_definition'] = {
|
| |
| |
| |
|
|
| init: function() {
|
| this.jsonInit({
|
| "message0": Blockly.Msg.PROCEDURES_DEFINITION,
|
| "args0": [
|
| {
|
| "type": "input_statement",
|
| "name": "custom_block",
|
| "check": 'procedure'
|
| }
|
| ],
|
| "extensions": ["colours_more", "shape_hat", "procedure_def_contextmenu"]
|
| });
|
| }
|
| };
|
|
|
| Blockly.Blocks['procedures_definition_return'] = {
|
| |
| |
| |
|
|
| init: function() {
|
| this.jsonInit({
|
| "message0": Blockly.Msg.PROCEDURES_DEFINITION,
|
| "args0": [
|
| {
|
| "type": "input_value",
|
| "name": "custom_block",
|
| "check": 'procedure'
|
| }
|
| ],
|
| "extensions": ["colours_more", "shape_hat", "procedure_def_contextmenu"]
|
| });
|
| }
|
| };
|
|
|
| Blockly.Blocks['procedures_call'] = {
|
| |
| |
| |
|
|
| init: function() {
|
| this.jsonInit({
|
| "extensions": ["colours_more", "shape_statement", "procedure_call_contextmenu"]
|
| });
|
| this.procCode_ = '';
|
| this.argumentIds_ = [];
|
| this.warp_ = false;
|
| this.output_ = false;
|
| this.isDisplayOnly = false
|
| this.edited = false
|
| this.outputType = 'statement'
|
| this.image = ''
|
| this.color = [Blockly.Colours.more.primary, Blockly.Colours.more.secondary, Blockly.Colours.more.tertiary]
|
| },
|
|
|
| getProcCode: Blockly.ScratchBlocks.ProcedureUtils.getProcCode,
|
| removeAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_,
|
| disconnectOldBlocks_: Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_,
|
| deleteShadows_: Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_,
|
| createIcon_: Blockly.ScratchBlocks.ProcedureUtils.createIcon_,
|
| createAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_,
|
| updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_,
|
|
|
|
|
| mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.callerMutationToDom,
|
| domToMutation: Blockly.ScratchBlocks.ProcedureUtils.callerDomToMutation,
|
| populateArgument_: Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnCaller_,
|
| addProcedureLabel_: Blockly.ScratchBlocks.ProcedureUtils.addLabelField_,
|
|
|
|
|
| attachShadow_: Blockly.ScratchBlocks.ProcedureUtils.attachShadow_,
|
| buildShadowDom_: Blockly.ScratchBlocks.ProcedureUtils.buildShadowDom_
|
| };
|
|
|
| Blockly.Blocks['procedures_prototype'] = {
|
| |
| |
| |
| |
|
|
| init: function() {
|
| this.jsonInit({
|
| "extensions": ["colours_more", 'shape_procedure']
|
| });
|
|
|
|
|
| this.procCode_ = '';
|
| this.displayNames_ = [];
|
| this.argumentIds_ = [];
|
| this.argumentDefaults_ = [];
|
| this.warp_ = false;
|
| this.output_ = false;
|
| this.isDisplayOnly = true
|
| this.edited = false
|
| this.outputType = 'statement'
|
| this.image = ''
|
| this.color = [Blockly.Colours.more.primary, Blockly.Colours.more.secondary, Blockly.Colours.more.tertiary]
|
| },
|
|
|
| getProcCode: Blockly.ScratchBlocks.ProcedureUtils.getProcCode,
|
| removeAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_,
|
| disconnectOldBlocks_: Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_,
|
| deleteShadows_: Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_,
|
| createIcon_: Blockly.ScratchBlocks.ProcedureUtils.createIcon_,
|
| createAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_,
|
| updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_,
|
|
|
|
|
| mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.definitionMutationToDom,
|
| domToMutation: Blockly.ScratchBlocks.ProcedureUtils.definitionDomToMutation,
|
| populateArgument_: Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnPrototype_,
|
| addProcedureLabel_: Blockly.ScratchBlocks.ProcedureUtils.addLabelField_,
|
|
|
|
|
| createArgumentReporter_: Blockly.ScratchBlocks.ProcedureUtils.createArgumentReporter_,
|
| updateArgumentReporterNames_: Blockly.ScratchBlocks.ProcedureUtils.updateArgumentReporterNames_
|
| };
|
|
|
| Blockly.Blocks['procedures_declaration'] = {
|
| |
| |
| |
|
|
| init: function() {
|
| this.jsonInit({
|
| "extensions": ["colours_more", 'shape_procedure']
|
| });
|
|
|
| this.procCode_ = '';
|
| this.displayNames_ = [];
|
| this.argumentIds_ = [];
|
| this.argumentDefaults_ = [];
|
| this.warp_ = false;
|
| this.output_ = false;
|
| this.isDisplayOnly = true
|
| this.edited = false
|
| this.outputType = 'statement'
|
| this.image = ''
|
| this.color = [Blockly.Colours.more.primary, Blockly.Colours.more.secondary, Blockly.Colours.more.tertiary]
|
| },
|
|
|
| getProcCode: Blockly.ScratchBlocks.ProcedureUtils.getProcCode,
|
| removeAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.removeAllInputs_,
|
| disconnectOldBlocks_: Blockly.ScratchBlocks.ProcedureUtils.disconnectOldBlocks_,
|
| deleteShadows_: Blockly.ScratchBlocks.ProcedureUtils.deleteShadows_,
|
| createIcon_: Blockly.ScratchBlocks.ProcedureUtils.createIcon_,
|
| createAllInputs_: Blockly.ScratchBlocks.ProcedureUtils.createAllInputs_,
|
| updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.updateDisplay_,
|
|
|
|
|
| mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.definitionMutationToDom,
|
| domToMutation: Blockly.ScratchBlocks.ProcedureUtils.definitionDomToMutation,
|
| populateArgument_: Blockly.ScratchBlocks.ProcedureUtils.populateArgumentOnDeclaration_,
|
| addProcedureLabel_: Blockly.ScratchBlocks.ProcedureUtils.addLabelEditor_,
|
|
|
|
|
| removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeFieldCallback,
|
|
|
|
|
| createArgumentEditor_: Blockly.ScratchBlocks.ProcedureUtils.createArgumentEditor_,
|
| focusLastEditor_: Blockly.ScratchBlocks.ProcedureUtils.focusLastEditor_,
|
| getWarp: Blockly.ScratchBlocks.ProcedureUtils.getWarp,
|
| setWarp: Blockly.ScratchBlocks.ProcedureUtils.setWarp,
|
| getReturns: Blockly.ScratchBlocks.ProcedureUtils.getReturns,
|
| setReturns: Blockly.ScratchBlocks.ProcedureUtils.setReturns,
|
| getEdited: Blockly.ScratchBlocks.ProcedureUtils.getEdited,
|
| setEdited: Blockly.ScratchBlocks.ProcedureUtils.setEdited,
|
| setType: Blockly.ScratchBlocks.ProcedureUtils.setType,
|
| setImage: Blockly.ScratchBlocks.ProcedureUtils.setImage,
|
| unsetImage: Blockly.ScratchBlocks.ProcedureUtils.unsetImage,
|
| setColor: Blockly.ScratchBlocks.ProcedureUtils.setColor,
|
| removeColor: Blockly.ScratchBlocks.ProcedureUtils.removeColor,
|
| addLabelExternal: Blockly.ScratchBlocks.ProcedureUtils.addLabelExternal,
|
| addBooleanExternal: Blockly.ScratchBlocks.ProcedureUtils.addBooleanExternal,
|
| addCommandExternal: Blockly.ScratchBlocks.ProcedureUtils.addCommandExternal,
|
| addStringNumberExternal: Blockly.ScratchBlocks.ProcedureUtils.addStringNumberExternal,
|
| onChangeFn: Blockly.ScratchBlocks.ProcedureUtils.updateDeclarationProcCode_
|
| };
|
|
|
| Blockly.Blocks['argument_reporter_boolean'] = {
|
| init: function() {
|
| this.jsonInit({
|
| "message0": " %1",
|
| "args0": [
|
| {
|
| "type": "field_label_serializable",
|
| "name": "VALUE",
|
| "text": ""
|
| }
|
| ],
|
| "extensions": ["colours_more", "output_boolean"]
|
| });
|
| },
|
| updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterUpdateDisplay,
|
| mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterMutationToDom,
|
| domToMutation: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterDomToMutation
|
| };
|
|
|
| Blockly.Blocks['argument_reporter_string_number'] = {
|
| init: function() {
|
| this.jsonInit({
|
| "message0": " %1",
|
| "args0": [
|
| {
|
| "type": "field_label_serializable",
|
| "name": "VALUE",
|
| "text": ""
|
| }
|
| ],
|
| "extensions": ["colours_more", "output_number", "output_string"]
|
| });
|
| },
|
| updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterUpdateDisplay,
|
| mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterMutationToDom,
|
| domToMutation: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterDomToMutation
|
| };
|
|
|
| Blockly.Blocks['argument_reporter_command'] = {
|
| init: function () {
|
| this.jsonInit({ "message0": " %1",
|
| "args0": [
|
| {
|
| "type": "field_label_serializable",
|
| "name": "VALUE",
|
| "text": ""
|
| }
|
| ],
|
| "canDragDuplicate": true,
|
| "extensions": ["colours_more", "shape_statement"],
|
| });
|
| },
|
| updateDisplay_: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterUpdateDisplay,
|
| mutationToDom: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterMutationToDom,
|
| domToMutation: Blockly.ScratchBlocks.ProcedureUtils.argumentReporterDomToMutation
|
| };
|
|
|
| Blockly.Blocks['argument_editor_boolean'] = {
|
| init: function() {
|
| this.jsonInit({ "message0": " %1",
|
| "args0": [
|
| {
|
| "type": "field_input_removable",
|
| "name": "TEXT",
|
| "text": "foo"
|
| }
|
| ],
|
| "colour": Blockly.Colours.textField,
|
| "colourSecondary": Blockly.Colours.textField,
|
| "colourTertiary": Blockly.Colours.textField,
|
| "extensions": ["output_boolean"]
|
| });
|
| },
|
|
|
| removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_
|
| };
|
|
|
| Blockly.Blocks['argument_editor_string_number'] = {
|
| init: function() {
|
| this.jsonInit({ "message0": " %1",
|
| "args0": [
|
| {
|
| "type": "field_input_removable",
|
| "name": "TEXT",
|
| "text": "foo"
|
| }
|
| ],
|
| "colour": Blockly.Colours.textField,
|
| "colourSecondary": Blockly.Colours.textField,
|
| "colourTertiary": Blockly.Colours.textField,
|
| "extensions": ["output_number", "output_string"]
|
| });
|
| },
|
|
|
| removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_
|
| };
|
|
|
| Blockly.Blocks['argument_editor_command'] = {
|
| init: function () {
|
| this.jsonInit({ "message0": " %1",
|
| "args0": [
|
| {
|
| "type": "field_input_removable",
|
| "name": "TEXT",
|
| "text": "foo"
|
| }
|
| ],
|
| "colour": Blockly.Colours.textField,
|
| "colourSecondary": Blockly.Colours.textField,
|
| "colourTertiary": Blockly.Colours.textField,
|
| "extensions": ["colours_more", "shape_statement"],
|
| });
|
| },
|
|
|
| removeFieldCallback: Blockly.ScratchBlocks.ProcedureUtils.removeArgumentCallback_,
|
| };
|
|
|
| Blockly.Blocks['procedures_set'] = {
|
| init: function() {
|
| this.jsonInit({
|
| "message0": 'set %1 to %2',
|
| "args0": [
|
| {
|
| "type": "input_value",
|
| "name": "PARAM"
|
| },
|
| {
|
| "type": "input_value",
|
| "name": "VALUE"
|
| }
|
| ],
|
| "extensions": ["colours_more", "shape_statement"]
|
| });
|
| }
|
| };
|
|
|
| Blockly.Blocks['procedures_return'] = {
|
| init: function() {
|
| this.jsonInit({
|
| "message0": 'return %1',
|
| "args0": [
|
| {
|
| "type": "input_value",
|
| "name": "return"
|
| }
|
| ],
|
| "extensions": ["colours_more", "shape_end"]
|
| });
|
| }
|
| };
|
|
|