123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- /**
- * @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)
- */
- 'use strict';
- goog.provide('Blockly.Blocks.utility');
- goog.require('Blockly.Blocks');
- Blockly.Blocks.utility.HUE = 160;
- Blockly.Blocks['raw_table'] = {
- // Container.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.appendDummyInput()
- .appendField('Tabular Abstraction:');
- this.appendDummyInput()
- .appendField(new Blockly.FieldTable(''), 'TEXT');
- }
- };
- Blockly.Blocks['raw_block'] = {
- // Container.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.appendDummyInput()
- .appendField('Code Block:');
- this.appendDummyInput()
- .appendField(new Blockly.FieldTextArea(''), 'TEXT');
- }
- };
- Blockly.Blocks['raw_expression'] = {
- // Container.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.appendDummyInput()
- .appendField('Code Expression:');
- this.appendDummyInput()
- .appendField(new Blockly.FieldTextArea(''), 'TEXT');
- this.setOutput(true);
- }
- };
- Blockly.Blocks['raw_empty'] = {
- // Container.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.appendValueInput('VALUE')
- .appendField('');
- this.setInputsInline(false);
- }
- };
- Blockly.Blocks['text_comment'] = {
- // Text value.
- init: function() {
- this.setColour(Blockly.Blocks.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');
- }
- };
- Blockly.Blocks['type_check'] = {
- // Set element at index.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.appendValueInput('VALUE')
- .appendField(Blockly.Msg.TYPE_CHECK);
- this.setInputsInline(false);
- this.setOutput(true, 'Type');
- //this.setPreviousStatement(true);
- //this.setNextStatement(true);
- }
- };
- Blockly.Blocks['text_print_multiple'] = {
- /**
- * Block for printing multiple things (including nothing)
- * @this Blockly.Block
- */
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.itemCount_ = 1;
- this.updateShape_();
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setMutator(new Blockly.Mutator(['text_print_multiple_item']));
- this.setTooltip(Blockly.Msg.TEXT_PRINT_TOOLTIP);
- },
- /**
- * Create XML to represent print inputs.
- * @return {Element} XML storage element.
- * @this Blockly.Block
- */
- mutationToDom: function(workspace) {
- 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 = Blockly.Block.obtain(workspace, '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");
- }
- }
- }
- }
- };
- Blockly.Blocks['text_print_multiple_container'] = {
- // Container.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.appendDummyInput()
- .appendField('print');
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['text_print_multiple_item'] = {
- // Add items.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.appendDummyInput()
- .appendField('item');
- this.setInputsInline(true);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['function_call'] = {
- /**
- * Block for printing multiple things (including nothing)
- * @this Blockly.Block
- */
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.itemCount_ = 1;
- this.hasReturn_ = false;
- this.appendDummyInput()
- .appendField(new Blockly.FieldTextInput("str"), 'NAME');
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['function_call_item']));
- 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(workspace) {
- 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 = Blockly.Block.obtain(workspace, '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++) {
- var input = this.appendValueInput('ARGUMENT' + i);
- }
-
- // Set whether returns anything
- this.setReturn(this.hasReturn_);
- }
- };
- Blockly.Blocks['function_call_container'] = {
- // Container.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.appendDummyInput()
- .appendField('Arguments');
- this.appendStatementInput('STACK');
- this.appendDummyInput()
- .setAlign(Blockly.ALIGN_RIGHT)
- .appendField('has return')
- .appendField(new Blockly.FieldCheckbox('TRUE'),
- 'RETURN');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['function_call_item'] = {
- // Add items.
- init: function() {
- this.setColour(Blockly.Blocks.utility.HUE);
- this.appendDummyInput()
- .appendField('argument');
- this.setInputsInline(true);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
|