Skip to content

Commit 6e9787a

Browse files
committed
Merge remote-tracking branch 'origin/customizable-matrix' into develop
2 parents 637e01c + 964bf63 commit 6e9787a

File tree

1 file changed

+68
-25
lines changed

1 file changed

+68
-25
lines changed

core/field_matrix.js

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ goog.require('Blockly.DropDownDiv');
3535
* @extends {Blockly.Field}
3636
* @constructor
3737
*/
38-
Blockly.FieldMatrix = function(matrix) {
38+
Blockly.FieldMatrix = function(matrix, width = 5, height = 5) {
3939
Blockly.FieldMatrix.superClass_.constructor.call(this, matrix);
4040
this.addArgType('matrix');
41+
42+
this.matrixWidth = width
43+
this.matrixHeight = height
44+
4145
/**
4246
* Array of SVGElement<rect> for matrix thumbnail image on block field.
4347
* @type {!Array<SVGElement>}
@@ -52,7 +56,7 @@ Blockly.FieldMatrix = function(matrix) {
5256
this.ledButtons_ = [];
5357
/**
5458
* String for storing current matrix value.
55-
* @type {!String]
59+
* @type {!String}
5660
* @private
5761
*/
5862
this.matrix_ = '';
@@ -128,16 +132,30 @@ goog.inherits(Blockly.FieldMatrix, Blockly.Field);
128132
* @nocollapse
129133
*/
130134
Blockly.FieldMatrix.fromJson = function(options) {
131-
return new Blockly.FieldMatrix(options['matrix']);
135+
return new Blockly.FieldMatrix(options['matrix'], options['width'], options['height']);
132136
};
133137

134138
/**
135139
* Fixed size of the matrix thumbnail in the input field, in px.
136140
* @type {number}
141+
* @deprecated
137142
* @const
138143
*/
139144
Blockly.FieldMatrix.THUMBNAIL_SIZE = 26;
140145

146+
/**
147+
* Width of the matrix thumbnail in the input field, in px.
148+
* @returns {number}
149+
*/
150+
Blockly.FieldMatrix.prototype.THUMBNAIL_WIDTH = function() {
151+
return (Blockly.FieldMatrix.THUMBNAIL_NODE_SIZE + Blockly.FieldMatrix.THUMBNAIL_NODE_PAD) * this.matrixWidth + Blockly.FieldMatrix.THUMBNAIL_NODE_PAD;
152+
}
153+
154+
155+
Blockly.FieldMatrix.prototype.THUMBNAIL_HEIGHT = function() {
156+
return (Blockly.FieldMatrix.THUMBNAIL_NODE_SIZE + Blockly.FieldMatrix.THUMBNAIL_NODE_PAD) * this.matrixHeight + Blockly.FieldMatrix.THUMBNAIL_NODE_PAD;
157+
}
158+
141159
/**
142160
* Fixed size of each matrix thumbnail node, in px.
143161
* @type {number}
@@ -185,6 +203,7 @@ Blockly.FieldMatrix.MATRIX_NODE_PAD = 5;
185203
* Used for clearing a matrix or filling an LED node array.
186204
* @type {string}
187205
* @const
206+
* @deprecated
188207
*/
189208
Blockly.FieldMatrix.ZEROS = '0000000000000000000000000';
190209

@@ -193,9 +212,28 @@ Blockly.FieldMatrix.ZEROS = '0000000000000000000000000';
193212
* Used for filling a matrix.
194213
* @type {string}
195214
* @const
215+
* @deprecated
196216
*/
197217
Blockly.FieldMatrix.ONES = '1111111111111111111111111';
198218

219+
/**
220+
* A string of all zeros depending on the current matrix.
221+
* Used for clearing a matrix.
222+
* @return {string}
223+
*/
224+
Blockly.FieldMatrix.prototype.ZEROS = function() {
225+
return "0".repeat(this.matrixWidth*this.matrixHeight);
226+
}
227+
228+
/**
229+
* A string of all zeros depending on the current matrix.
230+
* Used for filling a matrix.
231+
* @return {string}
232+
*/
233+
Blockly.FieldMatrix.prototype.ONES = function() {
234+
return "1".repeat(this.matrixWidth*this.matrixHeight);
235+
}
236+
199237
/**
200238
* Called when the field is placed on a block.
201239
* @param {Block} block The owning block.
@@ -208,22 +246,26 @@ Blockly.FieldMatrix.prototype.init = function() {
208246

209247
// Build the DOM.
210248
this.fieldGroup_ = Blockly.utils.createSvgElement('g', {}, null);
211-
this.size_.width = Blockly.FieldMatrix.THUMBNAIL_SIZE +
212-
Blockly.FieldMatrix.ARROW_SIZE + (Blockly.BlockSvg.DROPDOWN_ARROW_PADDING * 1.5);
249+
this.size_.width = this.THUMBNAIL_WIDTH() +
250+
Blockly.FieldMatrix.ARROW_SIZE + this.THUMBNAIL_HEIGHT() - 8;
251+
this.size_.height = Math.max(
252+
this.size_.height,
253+
this.THUMBNAIL_HEIGHT() + 8
254+
);
213255

214256
this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
215257

216-
var thumbX = Blockly.BlockSvg.DROPDOWN_ARROW_PADDING / 2;
217-
var thumbY = (this.size_.height - Blockly.FieldMatrix.THUMBNAIL_SIZE) / 2;
258+
var thumbX = this.THUMBNAIL_HEIGHT() / 2 - 8;
259+
var thumbY = (this.size_.height - this.THUMBNAIL_HEIGHT()) / 2;
218260
var thumbnail = Blockly.utils.createSvgElement('g', {
219261
'transform': 'translate(' + thumbX + ', ' + thumbY + ')',
220262
'pointer-events': 'bounding-box', 'cursor': 'pointer'
221263
}, this.fieldGroup_);
222264
this.ledThumbNodes_ = [];
223265
var nodeSize = Blockly.FieldMatrix.THUMBNAIL_NODE_SIZE;
224266
var nodePad = Blockly.FieldMatrix.THUMBNAIL_NODE_PAD;
225-
for (var i = 0; i < 5; i++) {
226-
for (var n = 0; n < 5; n++) {
267+
for (var i = 0; i < this.matrixHeight; i++) {
268+
for (var n = 0; n < this.matrixWidth; n++) {
227269
var attr = {
228270
'x': ((nodeSize + nodePad) * n) + nodePad,
229271
'y': ((nodeSize + nodePad) * i) + nodePad,
@@ -239,8 +281,7 @@ Blockly.FieldMatrix.prototype.init = function() {
239281
}
240282

241283
if (!this.arrow_) {
242-
var arrowX = Blockly.FieldMatrix.THUMBNAIL_SIZE +
243-
Blockly.BlockSvg.DROPDOWN_ARROW_PADDING * 1.5;
284+
var arrowX = this.THUMBNAIL_WIDTH() + thumbX + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING;
244285
var arrowY = (this.size_.height - Blockly.FieldMatrix.ARROW_SIZE) / 2;
245286
this.arrow_ = Blockly.utils.createSvgElement('image', {
246287
'height': Blockly.FieldMatrix.ARROW_SIZE + 'px',
@@ -270,7 +311,7 @@ Blockly.FieldMatrix.prototype.setValue = function(matrix) {
270311
Blockly.Events.fire(new Blockly.Events.Change(
271312
this.sourceBlock_, 'field', this.name, this.matrix_, matrix));
272313
}
273-
matrix = matrix + Blockly.FieldMatrix.ZEROS.substr(0, 25 - matrix.length);
314+
matrix = matrix + this.ZEROS().substr(0, this.matrixWidth*this.matrixHeight - matrix.length);
274315
this.matrix_ = matrix;
275316
this.updateMatrix_();
276317
};
@@ -293,20 +334,22 @@ Blockly.FieldMatrix.prototype.showEditor_ = function() {
293334
Blockly.DropDownDiv.clearContent();
294335
var div = Blockly.DropDownDiv.getContentDiv();
295336
// Build the SVG DOM.
296-
var matrixSize = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * 5) +
297-
(Blockly.FieldMatrix.MATRIX_NODE_PAD * 6);
337+
var matrixSizeWidth = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * this.matrixWidth) +
338+
(Blockly.FieldMatrix.MATRIX_NODE_PAD * (this.matrixWidth + 1));
339+
var matrixSizeHeight = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * this.matrixHeight) +
340+
(Blockly.FieldMatrix.MATRIX_NODE_PAD * (this.matrixHeight + 1));
298341
this.matrixStage_ = Blockly.utils.createSvgElement('svg', {
299342
'xmlns': 'http://www.w3.org/2000/svg',
300343
'xmlns:html': 'http://www.w3.org/1999/xhtml',
301344
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
302345
'version': '1.1',
303-
'height': matrixSize + 'px',
304-
'width': matrixSize + 'px'
346+
'height': matrixSizeHeight + 'px',
347+
'width': matrixSizeWidth + 'px'
305348
}, div);
306349
// Create the 5x5 matrix
307350
this.ledButtons_ = [];
308-
for (var i = 0; i < 5; i++) {
309-
for (var n = 0; n < 5; n++) {
351+
for (var i = 0; i < this.matrixHeight; i++) {
352+
for (var n = 0; n < this.matrixWidth; n++) {
310353
var x = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * n) +
311354
(Blockly.FieldMatrix.MATRIX_NODE_PAD * (n + 1));
312355
var y = (Blockly.FieldMatrix.MATRIX_NODE_SIZE * i) +
@@ -413,7 +456,7 @@ Blockly.FieldMatrix.prototype.updateMatrix_ = function() {
413456
*/
414457
Blockly.FieldMatrix.prototype.clearMatrix_ = function(e) {
415458
if (e.button != 0) return;
416-
this.setValue(Blockly.FieldMatrix.ZEROS);
459+
this.setValue(this.ZEROS());
417460
};
418461

419462
/**
@@ -422,7 +465,7 @@ Blockly.FieldMatrix.prototype.clearMatrix_ = function(e) {
422465
*/
423466
Blockly.FieldMatrix.prototype.fillMatrix_ = function(e) {
424467
if (e.button != 0) return;
425-
this.setValue(Blockly.FieldMatrix.ONES);
468+
this.setValue(this.ONES());
426469
};
427470

428471
/**
@@ -437,23 +480,23 @@ Blockly.FieldMatrix.prototype.fillMatrixNode_ = function(node, index, fill) {
437480
};
438481

439482
Blockly.FieldMatrix.prototype.setLEDNode_ = function(led, state) {
440-
if (led < 0 || led > 24) return;
483+
if (led < 0 || led > (this.matrixWidth*this.matrixHeight-1)) return;
441484
var matrix = this.matrix_.substr(0, led) + state + this.matrix_.substr(led + 1);
442485
this.setValue(matrix);
443486
};
444487

445488
Blockly.FieldMatrix.prototype.fillLEDNode_ = function(led) {
446-
if (led < 0 || led > 24) return;
489+
if (led < 0 || led > (this.matrixWidth*this.matrixHeight-1)) return;
447490
this.setLEDNode_(led, '1');
448491
};
449492

450493
Blockly.FieldMatrix.prototype.clearLEDNode_ = function(led) {
451-
if (led < 0 || led > 24) return;
494+
if (led < 0 || led > (this.matrixWidth*this.matrixHeight-1)) return;
452495
this.setLEDNode_(led, '0');
453496
};
454497

455498
Blockly.FieldMatrix.prototype.toggleLEDNode_ = function(led) {
456-
if (led < 0 || led > 24) return;
499+
if (led < 0 || led > (this.matrixWidth*this.matrixHeight-1)) return;
457500
if (this.matrix_.charAt(led) === '0') {
458501
this.setLEDNode_(led, '1');
459502
} else {
@@ -529,7 +572,7 @@ Blockly.FieldMatrix.prototype.checkForLED_ = function(e) {
529572
}
530573
var xDiv = Math.trunc((dx - nodePad / 2) / (nodeSize + nodePad));
531574
var yDiv = Math.trunc((dy - nodePad / 2) / (nodeSize + nodePad));
532-
return xDiv + (yDiv * nodePad);
575+
return xDiv + (yDiv * this.matrixWidth);
533576
};
534577

535578
/**

0 commit comments

Comments
 (0)