File size: 7,922 Bytes
fbe84a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
/**
* @fileoverview Polygon block.
* @author @RedMan13 (godslayerakp) & SharkPool
*/
'use strict';
// TODO set the default value for checkboxes so the arrow doesnt point the wrong way on duplication/flyout click
goog.provide('Blockly.Blocks.polygon');
goog.require('Blockly.Blocks');
goog.require('Blockly.Colours');
goog.require('Blockly.constants');
const getXYForPoint = (point, points, opt_offset, opt_scale) => {
const offset = Array.isArray(opt_offset)
? opt_offset
: [0,0];
const scale = typeof opt_scale === 'number'
? opt_scale
: 10;
const origin = 360 / points;
const dir = Blockly.utils.toRadians(((origin * point) - (origin / 2)) - 90);
const x = Math.cos(dir) * scale;
const y = Math.sin(dir) * scale;
return [x + offset[0],y + offset[1]];
}
Blockly.Blocks['polygon'] = {
/**
* Block for complex shapes.
* @this Blockly.Block
*/
init: function() {
this.color = Blockly.Colours.pen.primary;
this.needsInit = true;
this.expanded = true;
this.points = 0;
this.offset = [0,0];
this.scale = 50;
this.oldConnections = {};
this.myBlocks = {};
this.initGenerate();
},
mutationToDom: function() {
const container = document.createElement('mutation');
container.setAttribute('points', JSON.stringify(this.points));
container.setAttribute('color', this.color);
container.setAttribute('midle', JSON.stringify(this.offset));
container.setAttribute('scale', JSON.stringify(this.scale));
container.setAttribute('expanded', JSON.stringify(this.expanded));
container.setAttribute('needsInit', JSON.stringify(this.needsInit));
return container;
},
domToMutation: function(xmlElement) {
const newPoints = JSON.parse(xmlElement.getAttribute('points'));
const newColor = xmlElement.getAttribute('color') || '';
const newOffset = JSON.parse(xmlElement.getAttribute('midle') || '""');
const newScale = JSON.parse(xmlElement.getAttribute('scale') || '""');
const isInit = JSON.parse(xmlElement.getAttribute('needsInit') || 'true');
const newExpanded = false;
if (newPoints !== this.points) {
this.clear();
this.points = newPoints;
if (isInit) this.initGenerate();
else this.generate();
}
if (typeof newColor === 'string') {
this.color = newColor;
}
if (newOffset && Array.isArray(newOffset)) {
this.offset = newOffset;
}
if (typeof newScale === 'number') {
this.length = newScale;
}
if (typeof newExpanded === 'boolean' && newExpanded !== this.expanded) {
this.setExpanded(newExpanded);
this.setFieldValue(newExpanded, 'button');
}
},
clear: function() {
const connections = {};
for (let point = 1; point <= this.points; point++) {
const xName = `x${point}`;
const yName = `y${point}`;
const xInput = this.getInput(xName);
const yInput = this.getInput(yName);
connections[xName] = xInput.connection.targetConnection;
connections[yName] = yInput.connection.targetConnection;
this.removeInput(xName);
this.removeInput(yName);
}
this.removeInput('buttonContainer');
this.oldConnections = connections;
},
initGenerate: function() {
this.setColour(this.color, this.color, this.color);
this.setOutputShape(Blockly.OUTPUT_SHAPE_SQUARE);
this.setOutput(true, 'math_polygon');
this.setShadow(true);
const thisBlock = this;
const button = new Blockly.FieldCheckbox(
this.expanded,
newState => {
if (this.isInFlyout) return;
this.needsInit = false;
this.generate();
this.inputList[0].setVisible(false);
this.render();
return newState;
}
)
this.appendDummyInput('buttonContainer')
.appendField(button, 'button');
this.render(true);
},
generate: function() {
const connections = this.oldConnections;
// create all the node inputs
for (let point = 1; point <= this.points; point++) {
const xName = `x${point}`;
const yName = `y${point}`;
const xInput = this.appendValueInput(xName);
const yInput = this.appendValueInput(yName);
const xConnection = xInput.connection;
const yConnection = yInput.connection;
// dispose of any free-floating blocks that where created by this block
if (this.myBlocks[xName] && !this.myBlocks[xName].outputConnection.targetConnection) {
this.myBlocks[xName].dispose();
delete this.myBlocks[xName];
}
if (this.myBlocks[yName] && !this.myBlocks[yName].outputConnection.targetConnection) {
this.myBlocks[yName].dispose();
delete this.myBlocks[yName];
}
// if this point isnt filled in, fill it in
if (!this.getInput(xName).connection.targetConnection || !this.getInput(yName).connection.targetConnection) {
const newxBlock = this.workspace.newBlock('math_number');
const newyBlock = this.workspace.newBlock('math_number');
const initialValue = getXYForPoint(point - 1, this.points, this.offset, this.scale)
newxBlock.setFieldValue(String(initialValue[0]), 'NUM');
newyBlock.setFieldValue(String(initialValue[1]), 'NUM');
newxBlock.setShadow(true);
newyBlock.setShadow(true);
newxBlock.initSvg();
newyBlock.initSvg();
newxBlock.render(true);
newyBlock.render(true);
newxBlock.outputConnection.connect(xConnection);
newyBlock.outputConnection.connect(yConnection);
this.myBlocks[xName] = newxBlock;
this.myBlocks[yName] = newyBlock;
}
// if we have a cached connection for this point then connect it
if (connections[xName] || connections[yName]) {
const xBlock = connections[xName].getSourceBlock();
const yBlock = connections[yName].getSourceBlock();
connections[xName].connect(xConnection);
connections[yName].connect(yConnection);
// re-render the blocks after connecting them
xBlock.initSvg();
yBlock.initSvg();
xBlock.render(false);
yBlock.render(false);
}
xInput.appendField('x: ');
yInput.appendField('y: ');
}
this.setColour(this.color, this.color, this.color);
this.setOutputShape(Blockly.OUTPUT_SHAPE_SQUARE);
this.setOutput(true, 'math_polygon');
this.setShadow(true);
const thisBlock = this;
const button = new Blockly.FieldCheckbox(
this.expanded,
newState => {
this.needsInit = false;
thisBlock.setExpanded(newState);
return newState;
}
)
this.appendDummyInput('buttonContainer')
.appendField(button, 'button');
this.render(true);
},
setExpanded: function(bool) {
if (this.needsInit) return;
this.expanded = bool;
for (let point = 1; point <= this.points; point++) {
const xName = `x${point}`;
const yName = `y${point}`;
const xInput = this.getInput(xName);
const yInput = this.getInput(yName);
xInput.visible_ = !bool;
xInput.setVisible(bool);
yInput.visible_ = !bool;
yInput.setVisible(bool);
}
this.initSvg();
// we dont need to re-render this block since renderChildren will render all parents aswell
this.rerenderChildBlocks();
},
rerenderChildBlocks: function() {
const renderInputs = (block) => {
const children = block.childBlocks_;
// once we hit a bottom block, rerender the whole tree
if (children.length) block.render(true);
for (var i = 0, child; child = children[i]; i++) {
child.render(false);
renderInputs(child);
}
}
renderInputs(this);
}
};
|