Skip to content

Commit 9f041a1

Browse files
authored
custom block shape addon -- notch support
1 parent f8267dc commit 9f041a1

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/addons/addons/custom-block-shape/userscript.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ export default async function ({ addon, console }) {
77

88
const { GRID_UNIT } = BlockSvg;
99

10+
function scalePathXY(path, scaleX, scaleY) {
11+
const util = BlockSvg.CUSTOM_NOTCH_UTIL;
12+
const tokens = util.path2TokenList(pathStr);
13+
const result = [];
14+
let i = 0;
15+
while (i < tokens.length) {
16+
const cmd = tokens[i++];
17+
result.push(cmd);
18+
19+
const expected = util.supportedCommands[cmd];
20+
const xIndexes = util.commandXpos[cmd] || [];
21+
22+
while (i + expected <= tokens.length && !/^[a-z]$/i.test(tokens[i])) {
23+
for (let j = 0; j < expected; j++) {
24+
let val = parseFloat(tokens[i + j]);
25+
if (isNaN(val)) throw new Error(`Invalid number '${tokens[i + j]}'`);
26+
27+
if (xIndexes.includes(j)) val *= scaleX;
28+
else val *= scaleY;
29+
result.push(+val.toFixed(6));
30+
}
31+
i += expected;
32+
}
33+
}
34+
return result.join(' ');
35+
}
36+
1037
function updateAllBlocks() {
1138
const workspace = Blockly.getMainWorkspace();
1239
if (workspace) {
@@ -64,25 +91,18 @@ export default async function ({ addon, console }) {
6491
`l ${-4 * multiplier * notchSize} ${-4 * multiplier * notchSize} ` +
6592
`c -1 ${-1 * notchSize} -2 ${-2 * notchSize} -4 ${-2 * notchSize} `
6693

67-
BlockSvg.NOTCH_SWITCH_PATH_LEFT =
68-
`c 2 0 3 ${1 * notchSize} 4 ${2 * notchSize} ` +
69-
`l ${4 * multiplier * notchSize} ${4 * multiplier * notchSize} ` +
70-
`c 1 ${1 * notchSize} 2 ${2 * notchSize} 4 ${2 * notchSize} ` +
71-
`c ${4 * (multiplier - 0.5)} 0 ${8 * (multiplier - 0.5)} ${-8 * (multiplier - 0.5) * notchSize} ${12 * (multiplier - 0.5)} ${-8 * (multiplier - 0.5) * notchSize} ` +
72-
`c ${4 * (multiplier - 0.5)} 0 ${8 * (multiplier - 0.5)} ${8 * (multiplier - 0.5) * notchSize} ${12 * (multiplier - 0.5)} ${8 * (multiplier - 0.5) * notchSize} ` +
73-
`c 2 0 3 ${-1 * notchSize} 4 ${-2 * notchSize} ` +
74-
`l ${4 * multiplier * notchSize} ${-4 * multiplier * notchSize} ` +
75-
`c 1 ${-1 * notchSize} 2 ${-2 * notchSize} 4 ${-2 * notchSize} `
76-
BlockSvg.NOTCH_SWITCH_PATH_RIGHT =
77-
`h ${(-4 * (cornerSize - 1) - 5 * (1 - notchSize))} ` +
78-
`c -2 0 -3 ${1 * notchSize} -4 ${2 * notchSize} ` +
79-
`l ${-4 * multiplier * notchSize} ${4 * multiplier * notchSize} ` +
80-
`c -1 ${1 * notchSize} -2 ${2 * notchSize} -4 ${2 * notchSize} ` +
81-
`c ${-4 * (multiplier - 0.5)} 0 ${-8 * (multiplier - 0.5)} ${-8 * (multiplier - 0.5) * notchSize} ${-12 * (multiplier - 0.5)} ${-8 * (multiplier - 0.5) * notchSize} ` +
82-
`c ${-4 * (multiplier - 0.5)} 0 ${-8 * (multiplier - 0.5)} ${8 * (multiplier - 0.5) * notchSize} ${-12 * (multiplier - 0.5)} ${8 * (multiplier - 0.5) * notchSize} ` +
83-
`c -2 0 -3 ${-1 * notchSize} -4 ${-2 * notchSize} ` +
84-
`l ${-4 * multiplier * notchSize} ${-4 * multiplier * notchSize} ` +
85-
`c -1 ${-1 * notchSize} -2 ${-2 * notchSize} -4 ${-2 * notchSize} `
94+
/* Custom Notch API Support */
95+
const adjustedNotchSize = notchSize === 1 ? 1 :
96+
notchSize > 1 ? notchSize - 0.05 : notchSize + 0.05;
97+
BlockSvg.CUSTOM_NOTCHES.forEach((notch) => {
98+
if (!notch.ogLeft) notch.ogLeft = notch.left;
99+
if (!notch.ogRight) notch.ogRight = notch.right;
100+
notch.left = scalePathXY(notch.ogLeft, adjustedNotchSize, notchSize);
101+
notch.right = scalePathXY(notch.ogRight, adjustedNotchSize, notchSize);
102+
});
103+
104+
/* Custom Shape API Support */
105+
// TODO here...
86106

87107
BlockSvg.INPUT_SHAPE_HEXAGONAL =
88108
"M " +

0 commit comments

Comments
 (0)