Skip to content

Commit 9defd1a

Browse files
committed
Merge pull request #79 from rschamp/feature/input-rendering
Match string and number inputs to spec
2 parents ddf2128 + 8b24be0 commit 9defd1a

13 files changed

+324
-77
lines changed

blockly_compressed_horizontal.js

Lines changed: 27 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blockly_compressed_vertical.js

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blockly_uncompressed_horizontal.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blockly_uncompressed_vertical.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks_horizontal/looks.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license
3+
* Visual Blocks Editor
4+
*
5+
* Copyright 2016 MIT
6+
* https://github.com/lkjashdflkjahsdf
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* @fileoverview Looks blocks for Scratch (Horizontal)
23+
* @author [email protected] <Ray Schamp>
24+
*/
25+
'use strict';
26+
27+
goog.provide('Blockly.Blocks.looks');
28+
29+
goog.require('Blockly.Blocks');
30+
31+
Blockly.Blocks['looks_say'] = {
32+
/**
33+
* Block to say something.
34+
* @this Blockly.Block
35+
*/
36+
init: function() {
37+
this.jsonInit({
38+
"id": "looks_say",
39+
"message0": "%1 %2",
40+
"args0": [
41+
{
42+
"type": "field_image",
43+
"src": Blockly.mainWorkspace.options.pathToMedia + "icons/looks_say.svg",
44+
"width": 40,
45+
"height": 40,
46+
"alt": "say"
47+
},
48+
{
49+
"type": "input_value",
50+
"name": "MESSAGE",
51+
"check": "Number"
52+
}
53+
],
54+
"inputsInline": true,
55+
"previousStatement": null,
56+
"nextStatement": null,
57+
"colour": '#6971E7',
58+
"tooltip": ""
59+
});
60+
61+
this.setHelpUrl(function () {
62+
return 'halp me plz. k thx bye.';
63+
});
64+
}
65+
};

core/block_svg.js

Lines changed: 133 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,30 @@ Blockly.BlockSvg.SEP_SPACE_Y = 8;
780780
*/
781781
Blockly.BlockSvg.INLINE_PADDING_Y = 5;
782782
/**
783-
* Minimum height of a block.
783+
* Height of user inputs
784784
* @const
785785
*/
786-
Blockly.BlockSvg.MIN_BLOCK_Y = 25;
786+
Blockly.BlockSvg.FIELD_HEIGHT = 32;
787+
/**
788+
* Width of user inputs
789+
* @const
790+
*/
791+
Blockly.BlockSvg.FIELD_WIDTH = 48;
792+
/**
793+
* Corner radius of number inputs
794+
* @const
795+
*/
796+
Blockly.BlockSvg.NUMBER_FIELD_CORNER_RADIUS = 16;
797+
/**
798+
* Corner radius of text inputs
799+
* @const
800+
*/
801+
Blockly.BlockSvg.TEXT_FIELD_CORNER_RADIUS = 4;
802+
/**
803+
* Minimum width of a block.
804+
* @const
805+
*/
806+
Blockly.BlockSvg.MIN_BLOCK_X = 40;
787807
/**
788808
* Width of horizontal puzzle tab.
789809
* @const
@@ -1122,13 +1142,13 @@ Blockly.BlockSvg.disconnectUiStop_.group = null;
11221142
*/
11231143
Blockly.BlockSvg.prototype.updateColour = function() {
11241144
// Render block fill
1125-
var hexColour = this.getColour();
1145+
var hexColour = this.parentBlock_ ? this.parentBlock_.getColour() : this.getColour();
11261146
var rgb = goog.color.hexToRgb(hexColour);
11271147
if (this.isShadow()) {
1128-
rgb = goog.color.lighten(rgb, 0.6);
1129-
hexColour = goog.color.rgbArrayToHex(rgb);
1148+
this.svgPath_.setAttribute('fill', '#ffffff');
1149+
} else {
1150+
this.svgPath_.setAttribute('fill', hexColour);
11301151
}
1131-
this.svgPath_.setAttribute('fill', hexColour);
11321152

11331153
// Render block stroke
11341154
var colorShift = goog.color.darken(rgb, 0.1);
@@ -1345,13 +1365,26 @@ Blockly.BlockSvg.prototype.render = function(opt_bubble) {
13451365
Blockly.BlockSvg.prototype.renderCompute_ = function() {
13461366
var metrics = {
13471367
statement: null,
1368+
valueInput: null,
13481369
icon: null,
13491370
width: 0,
13501371
height: 0,
13511372
bayHeight: 0,
1352-
bayWidth: 0
1373+
bayWidth: 0,
1374+
fieldWidth: 0,
1375+
fieldHeight: 0,
1376+
fieldRadius: 0,
1377+
startHat: false,
1378+
endHat: false
13531379
};
13541380

1381+
if (this.nextConnection && !this.previousConnection) {
1382+
metrics.startHat = true;
1383+
}
1384+
if (this.previousConnection && !this.nextConnection) {
1385+
metrics.endHat = true;
1386+
}
1387+
13551388
// Does block have a statement?
13561389
for (var i = 0, input; input = this.inputList[i]; i++) {
13571390
if (input.type == Blockly.NEXT_STATEMENT) {
@@ -1361,7 +1394,7 @@ Blockly.BlockSvg.prototype.renderCompute_ = function() {
13611394
metrics.bayHeight = Blockly.BlockSvg.NOTCH_HEIGHT + 16 +
13621395
Blockly.BlockSvg.CORNER_RADIUS * 3;
13631396
metrics.bayWidth = Blockly.BlockSvg.NOTCH_WIDTH * 2 +
1364-
Blockly.BlockSvg.SEP_SPACE_X;
1397+
Blockly.BlockSvg.MIN_BLOCK_X;
13651398
// Expand input size if there is a connection.
13661399
if (input.connection && input.connection.targetConnection) {
13671400
var linkedBlock = input.connection.targetBlock();
@@ -1376,6 +1409,22 @@ Blockly.BlockSvg.prototype.renderCompute_ = function() {
13761409
if (field instanceof Blockly.FieldImage) {
13771410
metrics.icon = field;
13781411
}
1412+
if (field instanceof Blockly.FieldTextInput) {
1413+
var fieldBBox = field.textElement_.getBBox();
1414+
metrics.fieldWidth = fieldBBox.width + Blockly.BlockSvg.SEP_SPACE_X;
1415+
metrics.fieldHeight = fieldBBox.height;
1416+
if (field.sourceBlock_.type === 'math_number') {
1417+
metrics.fieldRadius = Blockly.BlockSvg.NUMBER_FIELD_CORNER_RADIUS;
1418+
} else {
1419+
metrics.fieldRadius = Blockly.BlockSvg.TEXT_FIELD_CORNER_RADIUS;
1420+
}
1421+
}
1422+
}
1423+
}
1424+
1425+
for (var i = 0, child; child = this.childBlocks_[i]; i++) {
1426+
if (child.isShadow()) {
1427+
metrics.valueInput = child;
13791428
}
13801429
}
13811430

@@ -1385,11 +1434,16 @@ Blockly.BlockSvg.prototype.renderCompute_ = function() {
13851434
if (metrics.statement) {
13861435
metrics.width += 2 * Blockly.BlockSvg.CORNER_RADIUS + 8;
13871436
}
1388-
metrics.height = Math.max(
1389-
Blockly.BlockSvg.SEP_SPACE_Y * 2 + iconSize.height,
1390-
Blockly.BlockSvg.NOTCH_HEIGHT + 16 + Blockly.BlockSvg.CORNER_RADIUS * 2,
1391-
metrics.bayHeight + Blockly.BlockSvg.SEP_SPACE_Y
1392-
);
1437+
if (this.outputConnection) {
1438+
metrics.height = Blockly.BlockSvg.FIELD_HEIGHT;
1439+
metrics.width = Blockly.BlockSvg.FIELD_WIDTH;
1440+
} else {
1441+
metrics.height = Math.max(
1442+
Blockly.BlockSvg.SEP_SPACE_Y * 2 + iconSize.height,
1443+
Blockly.BlockSvg.NOTCH_HEIGHT + 16 + Blockly.BlockSvg.CORNER_RADIUS * 2,
1444+
metrics.bayHeight + Blockly.BlockSvg.SEP_SPACE_Y
1445+
);
1446+
}
13931447
return metrics;
13941448
};
13951449

@@ -1433,6 +1487,16 @@ Blockly.BlockSvg.prototype.renderDraw_ = function(metrics) {
14331487
(metrics.height - iconSize.height - Blockly.BlockSvg.SEP_SPACE_Y) + ')');
14341488
// @todo RTL
14351489
}
1490+
1491+
// Position value input
1492+
if (metrics.valueInput) {
1493+
var input = metrics.valueInput.getSvgRoot();
1494+
var inputBBox = input.getBBox();
1495+
var transformation = 'translate(' +
1496+
(Blockly.BlockSvg.NOTCH_WIDTH + (metrics.bayWidth ? 8 + Blockly.BlockSvg.NOTCH_WIDTH*2 : 0) + metrics.bayWidth) + ',' +
1497+
(metrics.height - 8) + ')';
1498+
input.setAttribute('transform', transformation);
1499+
}
14361500
};
14371501

14381502
/**
@@ -1446,7 +1510,15 @@ Blockly.BlockSvg.prototype.renderDrawLeft_ =
14461510
function(steps, connectionsXY, metrics) {
14471511

14481512
// Top edge.
1449-
if (this.previousConnection) {
1513+
if (metrics.startHat) {
1514+
// Hat block
1515+
// Position the cursor at the top-left starting point.
1516+
steps.push(Blockly.BlockSvg.HAT_TOP_LEFT_CORNER_START);
1517+
// Top-left rounded corner.
1518+
steps.push(Blockly.BlockSvg.HAT_TOP_LEFT_CORNER);
1519+
steps.push('V', metrics.height - Blockly.BlockSvg.HAT_CORNER_RADIUS);
1520+
} else if (this.previousConnection) {
1521+
// Regular block
14501522
// Position the cursor at the top-left starting point.
14511523
steps.push(Blockly.BlockSvg.TOP_LEFT_CORNER_START);
14521524
// Top-left rounded corner.
@@ -1461,11 +1533,15 @@ Blockly.BlockSvg.prototype.renderDrawLeft_ =
14611533
// This connection will be tightened when the parent renders.
14621534
steps.push('V', metrics.height - Blockly.BlockSvg.CORNER_RADIUS);
14631535
} else {
1536+
// Input
14641537
// Position the cursor at the top-left starting point.
1465-
steps.push(Blockly.BlockSvg.HAT_TOP_LEFT_CORNER_START);
1538+
steps.push('m', metrics.fieldRadius + ',0');
14661539
// Top-left rounded corner.
1467-
steps.push(Blockly.BlockSvg.HAT_TOP_LEFT_CORNER);
1468-
steps.push('V', metrics.height - Blockly.BlockSvg.HAT_CORNER_RADIUS);
1540+
steps.push(
1541+
'A', metrics.fieldRadius + ',' + metrics.fieldRadius,
1542+
'0', '0,0', '0,' + metrics.fieldRadius);
1543+
steps.push(
1544+
'V', metrics.height - metrics.fieldRadius);
14691545
}
14701546
};
14711547

@@ -1482,16 +1558,21 @@ Blockly.BlockSvg.prototype.renderDrawLeft_ =
14821558
Blockly.BlockSvg.prototype.renderDrawBottom_ = function(steps,
14831559
connectionsXY, metrics) {
14841560

1485-
if (this.previousConnection) {
1561+
if (metrics.startHat) {
1562+
steps.push('a', Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1563+
Blockly.BlockSvg.HAT_CORNER_RADIUS + ' 0 0,0 ' +
1564+
Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1565+
Blockly.BlockSvg.HAT_CORNER_RADIUS);
1566+
} else if (this.previousConnection) {
14861567
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
14871568
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,0 ' +
14881569
Blockly.BlockSvg.CORNER_RADIUS + ',' +
14891570
Blockly.BlockSvg.CORNER_RADIUS);
14901571
} else {
1491-
steps.push('a', Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1492-
Blockly.BlockSvg.HAT_CORNER_RADIUS + ' 0 0,0 ' +
1493-
Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1494-
Blockly.BlockSvg.HAT_CORNER_RADIUS);
1572+
// Input
1573+
steps.push(
1574+
'a', metrics.fieldRadius + ',' + metrics.fieldRadius,
1575+
'0', '0,0', metrics.fieldRadius + ',' + metrics.fieldRadius);
14951576
}
14961577

14971578
// Has statement
@@ -1533,10 +1614,13 @@ Blockly.BlockSvg.prototype.renderDrawBottom_ = function(steps,
15331614
}
15341615
}
15351616

1536-
if (this.nextConnection) {
1617+
if (metrics.endHat) {
1618+
steps.push('H', metrics.width - Blockly.BlockSvg.HAT_CORNER_RADIUS);
1619+
} else if (this.nextConnection) {
15371620
steps.push('H', metrics.width - Blockly.BlockSvg.CORNER_RADIUS);
15381621
} else {
1539-
steps.push('H', metrics.width - Blockly.BlockSvg.HAT_CORNER_RADIUS);
1622+
// input
1623+
steps.push('H', metrics.width - metrics.fieldRadius);
15401624
}
15411625
};
15421626

@@ -1549,20 +1633,29 @@ Blockly.BlockSvg.prototype.renderDrawBottom_ = function(steps,
15491633
*/
15501634
Blockly.BlockSvg.prototype.renderDrawRight_ =
15511635
function(steps, connectionsXY, metrics) {
1552-
if (this.nextConnection) {
1636+
if (metrics.endHat) {
1637+
steps.push('a', Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1638+
Blockly.BlockSvg.HAT_CORNER_RADIUS + ' 0 0,0 ' +
1639+
Blockly.BlockSvg.HAT_CORNER_RADIUS + ',-' +
1640+
Blockly.BlockSvg.HAT_CORNER_RADIUS);
1641+
steps.push('v', -8);
1642+
} else if (this.nextConnection) {
15531643
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
15541644
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,0 ' +
15551645
Blockly.BlockSvg.CORNER_RADIUS + ',-' +
15561646
Blockly.BlockSvg.CORNER_RADIUS);
1647+
steps.push('v', -8);
15571648
} else {
1558-
steps.push('a', Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1559-
Blockly.BlockSvg.HAT_CORNER_RADIUS + ' 0 0,0 ' +
1560-
Blockly.BlockSvg.HAT_CORNER_RADIUS + ',-' +
1561-
Blockly.BlockSvg.HAT_CORNER_RADIUS);
1649+
// Input
1650+
steps.push(
1651+
'a', metrics.fieldRadius + ',' + metrics.fieldRadius,
1652+
'0', '0,0', metrics.fieldRadius + ',' + -1*metrics.fieldRadius);
1653+
steps.push('v', -1*(metrics.height - metrics.fieldRadius*2));
15621654
}
1563-
steps.push('v', -8);
15641655

1565-
if (this.nextConnection) {
1656+
if (metrics.endHat) {
1657+
steps.push('V', Blockly.BlockSvg.HAT_CORNER_RADIUS);
1658+
} else if (this.nextConnection) {
15661659
steps.push(Blockly.BlockSvg.NOTCH_PATH_UP);
15671660

15681661
// Create next block connection.
@@ -1579,8 +1672,6 @@ Blockly.BlockSvg.prototype.renderDrawRight_ =
15791672
}
15801673
this.height += 4; // Height of tab.
15811674
steps.push('V', Blockly.BlockSvg.CORNER_RADIUS);
1582-
} else {
1583-
steps.push('V', Blockly.BlockSvg.HAT_CORNER_RADIUS);
15841675
}
15851676
};
15861677

@@ -1593,16 +1684,20 @@ Blockly.BlockSvg.prototype.renderDrawRight_ =
15931684
*/
15941685
Blockly.BlockSvg.prototype.renderDrawTop_ =
15951686
function(steps, connectionsXY, metrics) {
1596-
if (this.nextConnection) {
1687+
if (metrics.endHat) {
1688+
steps.push('a', Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1689+
Blockly.BlockSvg.HAT_CORNER_RADIUS + ' 0 0,0 -' +
1690+
Blockly.BlockSvg.HAT_CORNER_RADIUS + ',-' +
1691+
Blockly.BlockSvg.HAT_CORNER_RADIUS);
1692+
} else if (this.nextConnection) {
15971693
steps.push('a', Blockly.BlockSvg.CORNER_RADIUS + ',' +
15981694
Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,0 -' +
15991695
Blockly.BlockSvg.CORNER_RADIUS + ',-' +
16001696
Blockly.BlockSvg.CORNER_RADIUS);
16011697
} else {
1602-
steps.push('a', Blockly.BlockSvg.HAT_CORNER_RADIUS + ',' +
1603-
Blockly.BlockSvg.HAT_CORNER_RADIUS + ' 0 0,0 -' +
1604-
Blockly.BlockSvg.HAT_CORNER_RADIUS + ',-' +
1605-
Blockly.BlockSvg.HAT_CORNER_RADIUS);
1698+
steps.push(
1699+
'a', metrics.fieldRadius + ',' + metrics.fieldRadius,
1700+
'0', '0,0', '-' + metrics.fieldRadius + ',-' + metrics.fieldRadius);
16061701
}
16071702
steps.push('z');
16081703
};

core/css.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ Blockly.Css.CONTENT = [
288288
'margin: 0;',
289289
'outline: none;',
290290
'padding: 0 1px;',
291-
'width: 100%',
291+
'width: 100%;',
292+
'text-align: center',
292293
'}',
293294

294295
'.blocklyMainBackground {',

0 commit comments

Comments
 (0)