Skip to content

Commit 5e65f52

Browse files
committed
Merge pull request #58 from rschamp/feature/pants-rendering
Fix pants blocks width, height and connection positions
2 parents 02160ec + 61c9842 commit 5e65f52

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

core/block_horizontal_scratch.js

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ Blockly.BlockSvg.prototype.getHeightWidth = function() {
330330
var nextBlock = this.getNextBlock();
331331
if (nextBlock) {
332332
var nextHeightWidth = nextBlock.getHeightWidth();
333-
height += nextHeightWidth.height - 4; // Height of tab.
334-
width = Math.max(width, nextHeightWidth.width);
333+
width += nextHeightWidth.width;
334+
height = Math.max(height, nextHeightWidth.height);
335335
} else if (!this.nextConnection && !this.outputConnection) {
336336
// Add a bit of margin under blocks with no bottom tab.
337337
height += 2;
@@ -814,6 +814,11 @@ Blockly.BlockSvg.NOTCH_BASE_HEIGHT = 32;
814814
* @const
815815
*/
816816
Blockly.BlockSvg.NOTCH_HEIGHT = Blockly.BlockSvg.NOTCH_BASE_HEIGHT + Blockly.BlockSvg.NOTCH_RADIUS;
817+
/**
818+
* Width of connector notch
819+
* @const
820+
*/
821+
Blockly.BlockSvg.NOTCH_WIDTH = Blockly.BlockSvg.NOTCH_BASE_HEIGHT/4;
817822
/**
818823
* SVG path for drawing next/previous notch from top to bottom.
819824
* @const
@@ -1320,6 +1325,8 @@ Blockly.BlockSvg.prototype.render = function(opt_bubble) {
13201325
this.rendered = true;
13211326

13221327
var metrics = this.renderCompute_();
1328+
this.height = metrics.height;
1329+
this.width = metrics.width;
13231330
this.renderDraw_(metrics);
13241331

13251332
if (opt_bubble !== false) {
@@ -1387,16 +1394,31 @@ Blockly.BlockSvg.prototype.render = function(opt_bubble) {
13871394
*/
13881395
Blockly.BlockSvg.prototype.renderCompute_ = function() {
13891396
var metrics = {
1390-
hasStatement: false,
1397+
statement: null,
13911398
icon: null,
13921399
width: 0,
1393-
height: 0
1400+
height: 0,
1401+
bayHeight: 0,
1402+
bayWidth: 0
13941403
};
13951404

13961405
// Does block have a statement?
13971406
for (var i = 0, input; input = this.inputList[i]; i++) {
13981407
if (input.type == Blockly.NEXT_STATEMENT) {
1399-
metrics.hasStatement = true;
1408+
metrics.statement = input;
1409+
// Compute minimum input size.
1410+
// @todo Why 3?
1411+
metrics.bayHeight = Blockly.BlockSvg.NOTCH_HEIGHT + 16 +
1412+
Blockly.BlockSvg.CORNER_RADIUS * 3;
1413+
metrics.bayWidth = Blockly.BlockSvg.NOTCH_WIDTH * 2 +
1414+
Blockly.BlockSvg.SEP_SPACE_X;
1415+
// Expand input size if there is a connection.
1416+
if (input.connection && input.connection.targetConnection) {
1417+
var linkedBlock = input.connection.targetBlock();
1418+
var bBox = linkedBlock.getHeightWidth();
1419+
metrics.bayHeight = Math.max(metrics.bayHeight, bBox.height);
1420+
metrics.bayWidth = Math.max(metrics.bayWidth, bBox.width);
1421+
}
14001422
}
14011423

14021424
// Find icon
@@ -1408,12 +1430,16 @@ Blockly.BlockSvg.prototype.renderCompute_ = function() {
14081430
}
14091431

14101432
var iconSize = (metrics.icon) ? metrics.icon.getSize() : new goog.math.Size(0,0);
1411-
metrics.width = Blockly.BlockSvg.SEP_SPACE_X * 2 + iconSize.width;
1433+
metrics.width =
1434+
Blockly.BlockSvg.SEP_SPACE_X * 2 + iconSize.width + metrics.bayWidth;
1435+
if (metrics.statement) {
1436+
metrics.width += 2 * Blockly.BlockSvg.CORNER_RADIUS + 8;
1437+
}
14121438
metrics.height = Math.max(
14131439
Blockly.BlockSvg.SEP_SPACE_Y * 2 + iconSize.height,
1414-
Blockly.BlockSvg.NOTCH_HEIGHT + 16 + Blockly.BlockSvg.CORNER_RADIUS * 2
1440+
Blockly.BlockSvg.NOTCH_HEIGHT + 16 + Blockly.BlockSvg.CORNER_RADIUS * 2,
1441+
metrics.bayHeight + Blockly.BlockSvg.SEP_SPACE_Y
14151442
);
1416-
14171443
return metrics;
14181444

14191445
// var inputList = this.inputList;
@@ -1587,8 +1613,10 @@ Blockly.BlockSvg.prototype.renderDraw_ = function(metrics) {
15871613
// Position icon
15881614
if (metrics.icon) {
15891615
var icon = metrics.icon.getSvgRoot();
1616+
var iconSize = metrics.icon.getSize();
15901617
icon.setAttribute('transform',
1591-
'translate(' + (metrics.width - metrics.icon.getSize().width - Blockly.BlockSvg.SEP_SPACE_X / 2) + ',' + Blockly.BlockSvg.SEP_SPACE_Y + ')');
1618+
'translate(' + (metrics.width - iconSize.width - Blockly.BlockSvg.SEP_SPACE_X / 2) + ',' +
1619+
(metrics.height - iconSize.height - Blockly.BlockSvg.SEP_SPACE_Y) + ')');
15921620
// @todo RTL
15931621
}
15941622
};
@@ -1625,7 +1653,6 @@ Blockly.BlockSvg.prototype.renderDrawLeft_ =
16251653
steps.push(Blockly.BlockSvg.HAT_TOP_LEFT_CORNER);
16261654
steps.push('V', metrics.height - Blockly.BlockSvg.HAT_CORNER_RADIUS);
16271655
}
1628-
this.height = metrics.height;
16291656
};
16301657

16311658
/**
@@ -1654,32 +1681,43 @@ Blockly.BlockSvg.prototype.renderDrawBottom_ = function(steps,
16541681
}
16551682

16561683
// Has statement
1657-
if (metrics.hasStatement) {
1684+
if (metrics.statement) {
16581685
steps.push('h', 8);
16591686
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
16601687
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,0 ' +
16611688
Blockly.BlockSvg.CORNER_RADIUS + ',-' +
16621689
Blockly.BlockSvg.CORNER_RADIUS);
16631690
steps.push('v', -8);
16641691
steps.push(Blockly.BlockSvg.NOTCH_PATH_UP);
1665-
steps.push('v', -50 + (Blockly.BlockSvg.CORNER_RADIUS * 2) + Blockly.BlockSvg.NOTCH_HEIGHT + 8);
1692+
// @todo Why 3?
1693+
steps.push('v', -metrics.bayHeight + (Blockly.BlockSvg.CORNER_RADIUS * 3) + Blockly.BlockSvg.NOTCH_HEIGHT + 8);
16661694
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
16671695
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,1 ' +
16681696
Blockly.BlockSvg.CORNER_RADIUS + ',-' +
16691697
Blockly.BlockSvg.CORNER_RADIUS);
1670-
steps.push('h', 20 - (Blockly.BlockSvg.CORNER_RADIUS * 2));
1698+
steps.push('h', metrics.bayWidth - (Blockly.BlockSvg.CORNER_RADIUS * 2));
16711699
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
16721700
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,1 ' +
16731701
Blockly.BlockSvg.CORNER_RADIUS + ',' +
16741702
Blockly.BlockSvg.CORNER_RADIUS);
1675-
steps.push('v', 50 - (Blockly.BlockSvg.CORNER_RADIUS * 2) - Blockly.BlockSvg.NOTCH_HEIGHT - 8);
1703+
steps.push('v', metrics.bayHeight - (Blockly.BlockSvg.CORNER_RADIUS * 3) - Blockly.BlockSvg.NOTCH_HEIGHT - 8);
16761704
steps.push(Blockly.BlockSvg.NOTCH_PATH_DOWN);
16771705
steps.push('v', 8);
16781706
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
16791707
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,0 ' +
16801708
Blockly.BlockSvg.CORNER_RADIUS + ',' +
16811709
Blockly.BlockSvg.CORNER_RADIUS);
16821710

1711+
// Create statement connection.
1712+
// @todo RTL
1713+
// var connectionX = connectionsXY.x + (this.RTL ? -cursorX : cursorX + 1);
1714+
var connectionX = connectionsXY.x + Blockly.BlockSvg.CORNER_RADIUS * 2 + 8;
1715+
var connectionY = connectionsXY.y + metrics.height - Blockly.BlockSvg.CORNER_RADIUS * 2;
1716+
metrics.statement.connection.moveTo(connectionX, connectionY);
1717+
if (metrics.statement.connection.targetConnection) {
1718+
metrics.statement.connection.tighten_();
1719+
}
1720+
16831721
// // Nested statement.
16841722
// var input = row[0];
16851723
// if (y == 0) {

0 commit comments

Comments
 (0)