Guest User

Blockly multiple and block

a guest
Jun 28th, 2016
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Blockly.Blocks['controls_logical_and'] = {
  2.   init: function() {
  3.     this.appendDummyInput().appendField('AND');
  4.     this.appendValueInput('AND1').setCheck('Boolean');
  5.     this.appendValueInput('AND2').setCheck('Boolean');
  6.     this.setInputsInline(false);
  7.     this.setOutput(true, "Boolean");
  8.     this.setColour(30);
  9.     this.setTooltip('Logical AND');
  10.     this.setMutator(new Blockly.Mutator(['controls_condition_block']));
  11.     this.numberOfChildren = 2;
  12.   },
  13.   mutationToDom: function()
  14.   {
  15.       if(!this.numberOfChildren)
  16.       {
  17.           return null;
  18.       }
  19.       var container = document.createElement('mutation');
  20.       if(this.numberOfChildren)
  21.       {
  22.           container.setAttribute('children', this.numberOfChildren);
  23.       }
  24.       return container;
  25.   },
  26.   domToMutation: function(xmlElement)
  27.   {
  28.       this.numberOfChildren = parseInt(xmlElement.getAttribute('children'), 10) || 0;
  29.       for(var i = 3; i <= this.numberOfChildren; i++)
  30.       {
  31.           this.appendValueInput('AND' + i)
  32.               .setCheck('Boolean');
  33.       }
  34.   },
  35.   decompose: function(workspace)
  36.   {
  37.       var containerBlock = Blockly.Block.obtain(workspace, 'controls_and_tooltip');
  38.       containerBlock.initSvg();
  39.       var connection = containerBlock.getInput('STACK').connection;
  40.       for(var i = 1; i <= this.numberOfChildren; i++)
  41.       {
  42.           var conditionBlock = Blockly.Block.obtain(workspace, 'controls_condition_block');
  43.           conditionBlock.initSvg();
  44.           connection.connect(conditionBlock.previousConnection);
  45.           connection = conditionBlock.nextConnection;
  46.       }
  47.       return containerBlock;
  48.   },
  49.   compose: function(containerBlock)
  50.   {
  51.       for(var i = this.numberOfChildren; i > 0; i--)
  52.       {
  53.           this.removeInput('AND' + i);
  54.       }
  55.       this.numberOfChildren = 0;
  56.       var clauseBlock = containerBlock.getInputTargetBlock('STACK');
  57.       while(clauseBlock)
  58.       {
  59.           this.numberOfChildren++;
  60.           var andInput = this.appendValueInput('AND' + this.numberOfChildren)
  61.                              .setCheck('Boolean');
  62.                              //.appendField('condition' + this.numberOfChildren);
  63.           if(clauseBlock.valueConnection_)
  64.           {
  65.               andInput.connection.connect(clauseBlock.valueConnection_);
  66.           }
  67.           clauseBlock = clauseBlock.nextConnection &&
  68.           clauseBlock.nextConnection.targetBlock();
  69.       }
  70.   },
  71.   saveConnections: function(containerBlock)
  72.   {
  73.       var clauseBlock = containerBlock.getInputTargetBlock('STACK');
  74.       var i = 1;
  75.       while(clauseBlock)
  76.       {
  77.           var andInput = this.getInput('AND' + i);
  78.           clauseBlock.valueConnection_ = andInput && andInput.connection.targetConnection;
  79.           i++;
  80.           clauseBlock = clauseBlock.nextConnection &&
  81.           clauseBlock.nextConnection.targetBlock();
  82.       }
  83.   }
  84. }
  85.  
  86. Blockly.Blocks['controls_and_tooltip'] =
  87. {
  88.     init: function() {
  89.     this.setColour(Blockly.Blocks.logic.HUE);
  90.     this.appendDummyInput()
  91.         .appendField('AND');
  92.     this.appendStatementInput('STACK');
  93.     this.setTooltip('multiple and control');
  94.     this.contextMenu = false;
  95.     }
  96. }
  97.  
  98. Blockly.Blocks['controls_condition_block'] =
  99. {
  100.     init: function() {
  101.     this.setColour(Blockly.Blocks.logic.HUE);
  102.     this.appendDummyInput()
  103.         .appendField('condition');
  104.     this.setPreviousStatement(true);
  105.     this.setNextStatement(true);
  106.     this.setTooltip('conditionalStatement');
  107.     this.contextMenu = false;
  108.     }
  109. };
Advertisement
Add Comment
Please, Sign In to add comment