| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
|
|
| 'use strict';
|
|
|
| goog.provide('Blockly.FieldMatrix');
|
|
|
| goog.require('Blockly.DropDownDiv');
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix = function(matrix) {
|
| Blockly.FieldMatrix.superClass_.constructor.call(this, matrix);
|
| this.addArgType('matrix');
|
| |
| |
| |
| |
|
|
| this.ledThumbNodes_ = [];
|
| |
| |
| |
| |
|
|
| this.ledButtons_ = [];
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| this.matrixStage_ = null;
|
| |
| |
| |
| |
|
|
| this.arrow_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.paintStyle_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.mouseDownWrapper_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.clearButtonWrapper_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.fillButtonWrapper_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.matrixTouchWrapper_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.matrixMoveWrapper_ = null;
|
| |
| |
| |
| |
| |
|
|
| this.matrixReleaseWrapper_ = null;
|
| };
|
| goog.inherits(Blockly.FieldMatrix, Blockly.Field);
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.fromJson = function(options) {
|
| return new Blockly.FieldMatrix(options['matrix']);
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.THUMBNAIL_SIZE = 26;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.THUMBNAIL_NODE_SIZE = 4;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.THUMBNAIL_NODE_PAD = 1;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.ARROW_SIZE = 12;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.MATRIX_NODE_SIZE = 18;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.MATRIX_NODE_RADIUS = 4;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.MATRIX_NODE_PAD = 5;
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.ZEROS = '0000000000000000000000000';
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.ONES = '1111111111111111111111111';
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.init = function() {
|
| if (this.fieldGroup_) {
|
|
|
| return;
|
| }
|
|
|
|
|
| this.fieldGroup_ = Blockly.utils.createSvgElement('g', {}, null);
|
| this.size_.width = Blockly.FieldMatrix.THUMBNAIL_SIZE +
|
| Blockly.FieldMatrix.ARROW_SIZE + (Blockly.BlockSvg.DROPDOWN_ARROW_PADDING * 1.5);
|
|
|
| this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
|
|
|
| var thumbX = Blockly.BlockSvg.DROPDOWN_ARROW_PADDING / 2;
|
| var thumbY = (this.size_.height - Blockly.FieldMatrix.THUMBNAIL_SIZE) / 2;
|
| var thumbnail = Blockly.utils.createSvgElement('g', {
|
| 'transform': 'translate(' + thumbX + ', ' + thumbY + ')',
|
| 'pointer-events': 'bounding-box', 'cursor': 'pointer'
|
| }, this.fieldGroup_);
|
| this.ledThumbNodes_ = [];
|
| var nodeSize = Blockly.FieldMatrix.THUMBNAIL_NODE_SIZE;
|
| var nodePad = Blockly.FieldMatrix.THUMBNAIL_NODE_PAD;
|
| for (var i = 0; i < 5; i++) {
|
| for (var n = 0; n < 5; n++) {
|
| var attr = {
|
| 'x': ((nodeSize + nodePad) * n) + nodePad,
|
| 'y': ((nodeSize + nodePad) * i) + nodePad,
|
| 'width': nodeSize, 'height': nodeSize,
|
| 'rx': nodePad, 'ry': nodePad
|
| };
|
| this.ledThumbNodes_.push(
|
| Blockly.utils.createSvgElement('rect', attr, thumbnail)
|
| );
|
| }
|
| thumbnail.style.cursor = 'default';
|
| this.updateMatrix_();
|
| }
|
|
|
| if (!this.arrow_) {
|
| var arrowX = Blockly.FieldMatrix.THUMBNAIL_SIZE +
|
| Blockly.BlockSvg.DROPDOWN_ARROW_PADDING * 1.5;
|
| var arrowY = (this.size_.height - Blockly.FieldMatrix.ARROW_SIZE) / 2;
|
| this.arrow_ = Blockly.utils.createSvgElement('image', {
|
| 'height': Blockly.FieldMatrix.ARROW_SIZE + 'px',
|
| 'width': Blockly.FieldMatrix.ARROW_SIZE + 'px',
|
| 'transform': 'translate(' + arrowX + ', ' + arrowY + ')'
|
| }, this.fieldGroup_);
|
| this.arrow_.setAttributeNS('http://www.w3.org/1999/xlink',
|
| 'xlink:href', Blockly.mainWorkspace.options.pathToMedia +
|
| 'dropdown-arrow.svg');
|
| this.arrow_.style.cursor = 'default';
|
| }
|
|
|
| this.mouseDownWrapper_ = Blockly.bindEventWithChecks_(
|
| this.getClickTarget_(), 'mousedown', this, this.onMouseDown_);
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.setValue = function(matrix) {
|
| if (!matrix || matrix === this.matrix_) {
|
| return;
|
| }
|
| if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
|
| Blockly.Events.fire(new Blockly.Events.Change(
|
| this.sourceBlock_, 'field', this.name, this.matrix_, matrix));
|
| }
|
| matrix = matrix + Blockly.FieldMatrix.ZEROS.substr(0, 25 - matrix.length);
|
| this.matrix_ = matrix;
|
| this.updateMatrix_();
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.getValue = function() {
|
| return String(this.matrix_);
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.showEditor_ = function() {
|
|
|
| Blockly.DropDownDiv.hideWithoutAnimation();
|
| Blockly.DropDownDiv.clearContent();
|
| var div = Blockly.DropDownDiv.getContentDiv();
|
|
|
| var matrixSize = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * 5) +
|
| (Blockly.FieldMatrix.MATRIX_NODE_PAD * 6);
|
| this.matrixStage_ = Blockly.utils.createSvgElement('svg', {
|
| 'xmlns': 'http://www.w3.org/2000/svg',
|
| 'xmlns:html': 'http://www.w3.org/1999/xhtml',
|
| 'xmlns:xlink': 'http://www.w3.org/1999/xlink',
|
| 'version': '1.1',
|
| 'height': matrixSize + 'px',
|
| 'width': matrixSize + 'px'
|
| }, div);
|
|
|
| this.ledButtons_ = [];
|
| for (var i = 0; i < 5; i++) {
|
| for (var n = 0; n < 5; n++) {
|
| var x = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * n) +
|
| (Blockly.FieldMatrix.MATRIX_NODE_PAD * (n + 1));
|
| var y = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * i) +
|
| (Blockly.FieldMatrix.MATRIX_NODE_PAD * (i + 1));
|
| var attr = {
|
| 'x': x + 'px', 'y': y + 'px',
|
| 'width': Blockly.FieldMatrix.MATRIX_NODE_SIZE,
|
| 'height': Blockly.FieldMatrix.MATRIX_NODE_SIZE,
|
| 'rx': Blockly.FieldMatrix.MATRIX_NODE_RADIUS,
|
| 'ry': Blockly.FieldMatrix.MATRIX_NODE_RADIUS
|
| };
|
| var led = Blockly.utils.createSvgElement('rect', attr, this.matrixStage_);
|
| this.matrixStage_.appendChild(led);
|
| this.ledButtons_.push(led);
|
| }
|
| }
|
|
|
| var buttonDiv = document.createElement('div');
|
|
|
| var clearButtonDiv = document.createElement('div');
|
| clearButtonDiv.className = 'scratchMatrixButtonDiv';
|
| var clearButton = this.createButton_(this.sourceBlock_.colourSecondary_);
|
| clearButtonDiv.appendChild(clearButton);
|
|
|
| var fillButtonDiv = document.createElement('div');
|
| fillButtonDiv.className = 'scratchMatrixButtonDiv';
|
| var fillButton = this.createButton_('#FFFFFF');
|
| fillButtonDiv.appendChild(fillButton);
|
|
|
| buttonDiv.appendChild(clearButtonDiv);
|
| buttonDiv.appendChild(fillButtonDiv);
|
| div.appendChild(buttonDiv);
|
|
|
| Blockly.DropDownDiv.setColour(this.sourceBlock_.getColour(),
|
| this.sourceBlock_.getColourTertiary());
|
| Blockly.DropDownDiv.setCategory(this.sourceBlock_.getCategory());
|
| Blockly.DropDownDiv.showPositionedByBlock(this, this.sourceBlock_);
|
|
|
| this.matrixTouchWrapper_ =
|
| Blockly.bindEvent_(this.matrixStage_, 'mousedown', this, this.onMouseDown);
|
| this.clearButtonWrapper_ =
|
| Blockly.bindEvent_(clearButton, 'click', this, this.clearMatrix_);
|
| this.fillButtonWrapper_ =
|
| Blockly.bindEvent_(fillButton, 'click', this, this.fillMatrix_);
|
|
|
|
|
| this.updateMatrix_();
|
|
|
| };
|
|
|
| this.nodeCallback_ = function(e, num) {
|
| console.log(num);
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.createButton_ = function(fill) {
|
| var button = Blockly.utils.createSvgElement('svg', {
|
| 'xmlns': 'http://www.w3.org/2000/svg',
|
| 'xmlns:html': 'http://www.w3.org/1999/xhtml',
|
| 'xmlns:xlink': 'http://www.w3.org/1999/xlink',
|
| 'version': '1.1',
|
| 'height': Blockly.FieldMatrix.MATRIX_NODE_SIZE + 'px',
|
| 'width': Blockly.FieldMatrix.MATRIX_NODE_SIZE + 'px'
|
| });
|
| var nodeSize = Blockly.FieldMatrix.MATRIX_NODE_SIZE / 4;
|
| var nodePad = Blockly.FieldMatrix.MATRIX_NODE_SIZE / 16;
|
| for (var i = 0; i < 3; i++) {
|
| for (var n = 0; n < 3; n++) {
|
| Blockly.utils.createSvgElement('rect', {
|
| 'x': ((nodeSize + nodePad) * n) + nodePad,
|
| 'y': ((nodeSize + nodePad) * i) + nodePad,
|
| 'width': nodeSize, 'height': nodeSize,
|
| 'rx': nodePad, 'ry': nodePad,
|
| 'fill': fill
|
| }, button);
|
| }
|
| }
|
| return button;
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.updateMatrix_ = function() {
|
| for (var i = 0; i < this.matrix_.length; i++) {
|
| if (this.matrix_[i] === '0') {
|
| this.fillMatrixNode_(this.ledButtons_, i, this.sourceBlock_.colourSecondary_);
|
| this.fillMatrixNode_(this.ledThumbNodes_, i, this.sourceBlock_.colour_);
|
| } else {
|
| this.fillMatrixNode_(this.ledButtons_, i, '#FFFFFF');
|
| this.fillMatrixNode_(this.ledThumbNodes_, i, '#FFFFFF');
|
| }
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.clearMatrix_ = function(e) {
|
| if (e.button != 0) return;
|
| this.setValue(Blockly.FieldMatrix.ZEROS);
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.fillMatrix_ = function(e) {
|
| if (e.button != 0) return;
|
| this.setValue(Blockly.FieldMatrix.ONES);
|
| };
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.fillMatrixNode_ = function(node, index, fill) {
|
| if (!node || !node[index] || !fill) return;
|
| node[index].setAttribute('fill', fill);
|
| };
|
|
|
| Blockly.FieldMatrix.prototype.setLEDNode_ = function(led, state) {
|
| if (led < 0 || led > 24) return;
|
| var matrix = this.matrix_.substr(0, led) + state + this.matrix_.substr(led + 1);
|
| this.setValue(matrix);
|
| };
|
|
|
| Blockly.FieldMatrix.prototype.fillLEDNode_ = function(led) {
|
| if (led < 0 || led > 24) return;
|
| this.setLEDNode_(led, '1');
|
| };
|
|
|
| Blockly.FieldMatrix.prototype.clearLEDNode_ = function(led) {
|
| if (led < 0 || led > 24) return;
|
| this.setLEDNode_(led, '0');
|
| };
|
|
|
| Blockly.FieldMatrix.prototype.toggleLEDNode_ = function(led) {
|
| if (led < 0 || led > 24) return;
|
| if (this.matrix_.charAt(led) === '0') {
|
| this.setLEDNode_(led, '1');
|
| } else {
|
| this.setLEDNode_(led, '0');
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.onMouseDown = function(e) {
|
| this.matrixMoveWrapper_ =
|
| Blockly.bindEvent_(document.body, 'mousemove', this, this.onMouseMove);
|
| this.matrixReleaseWrapper_ =
|
| Blockly.bindEvent_(document.body, 'mouseup', this, this.onMouseUp);
|
| var ledHit = this.checkForLED_(e);
|
| if (ledHit > -1) {
|
| if (this.matrix_.charAt(ledHit) === '0') {
|
| this.paintStyle_ = 'fill';
|
| } else {
|
| this.paintStyle_ = 'clear';
|
| }
|
| this.toggleLEDNode_(ledHit);
|
| this.updateMatrix_();
|
| } else {
|
| this.paintStyle_ = null;
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.onMouseUp = function() {
|
| Blockly.unbindEvent_(this.matrixMoveWrapper_);
|
| Blockly.unbindEvent_(this.matrixReleaseWrapper_);
|
| this.paintStyle_ = null;
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.onMouseMove = function(e) {
|
| e.preventDefault();
|
| if (this.paintStyle_) {
|
| var led = this.checkForLED_(e);
|
| if (led < 0) return;
|
| if (this.paintStyle_ === 'clear') {
|
| this.clearLEDNode_(led);
|
| } else if (this.paintStyle_ === 'fill') {
|
| this.fillLEDNode_(led);
|
| }
|
| }
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.checkForLED_ = function(e) {
|
| var bBox = this.matrixStage_.getBoundingClientRect();
|
| var nodeSize = Blockly.FieldMatrix.MATRIX_NODE_SIZE;
|
| var nodePad = Blockly.FieldMatrix.MATRIX_NODE_PAD;
|
| var dx = e.clientX - bBox.left;
|
| var dy = e.clientY - bBox.top;
|
| var min = nodePad / 2;
|
| var max = bBox.width - (nodePad / 2);
|
| if (dx < min || dx > max || dy < min || dy > max) {
|
| return -1;
|
| }
|
| var xDiv = Math.trunc((dx - nodePad / 2) / (nodeSize + nodePad));
|
| var yDiv = Math.trunc((dy - nodePad / 2) / (nodeSize + nodePad));
|
| return xDiv + (yDiv * nodePad);
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldMatrix.prototype.dispose_ = function() {
|
| var thisField = this;
|
| return function() {
|
| Blockly.FieldMatrix.superClass_.dispose_.call(thisField)();
|
| thisField.matrixStage_ = null;
|
| if (thisField.mouseDownWrapper_) {
|
| Blockly.unbindEvent_(thisField.mouseDownWrapper_);
|
| }
|
| if (thisField.matrixTouchWrapper_) {
|
| Blockly.unbindEvent_(thisField.matrixTouchWrapper_);
|
| }
|
| if (thisField.matrixReleaseWrapper_) {
|
| Blockly.unbindEvent_(thisField.matrixReleaseWrapper_);
|
| }
|
| if (thisField.matrixMoveWrapper_) {
|
| Blockly.unbindEvent_(thisField.matrixMoveWrapper_);
|
| }
|
| if (thisField.clearButtonWrapper_) {
|
| Blockly.unbindEvent_(thisField.clearButtonWrapper_);
|
| }
|
| if (thisField.fillButtonWrapper_) {
|
| Blockly.unbindEvent_(thisField.fillButtonWrapper_);
|
| }
|
| };
|
| };
|
|
|
| Blockly.Field.register('field_matrix', Blockly.FieldMatrix);
|
|
|