-
Notifications
You must be signed in to change notification settings - Fork 625
/
debugRenderer.js
423 lines (390 loc) · 12.1 KB
/
debugRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Visualizer for debugging custom renderers in Blockly.
* @author [email protected] (Rachel Fenichel)
*/
import Blockly from 'blockly/core';
/**
* A basic visualizer for debugging custom renderers.
*/
export class DebugRenderer {
/**
* An object that renders rectangles and dots for debugging rendering code.
* @param {!Blockly.blockRendering.ConstantProvider} constants The renderer's
* constants.
* @package
* @constructor
*/
constructor(constants) {
/**
* An array of SVG elements that have been created by this object.
* @type {Array.<!SVGElement>}
* @private
*/
this.debugElements_ = [];
/**
* The SVG root of the block that is being rendered. Debug elements will
* be attached to this root.
* @type {SVGElement}
* @private
*/
this.svgRoot_ = null;
/**
* The renderer's constant provider.
* @type {!Blockly.blockRendering.ConstantProvider}
* @private
*/
this.constants_ = constants;
}
/**
* Initialize the debug renderer.
* @public
*/
static init() {
Blockly.blockRendering.useDebugger = true;
Blockly.blockRendering.Debug = DebugRenderer;
}
/**
* Remove all elements the this object created on the last pass.
* @package
*/
clearElems() {
for (let i = 0, elem; (elem = this.debugElements_[i]); i++) {
Blockly.utils.dom.removeNode(elem);
}
this.debugElements_ = [];
}
/**
* Draw a debug rectangle for a spacer (empty) row.
* @param {!Blockly.blockRendering.Row} row The row to render.
* @param {number} cursorY The y position of the top of the row.
* @param {boolean} isRtl Whether the block is rendered RTL.
* @package
*/
drawSpacerRow(row, cursorY, isRtl) {
if (!Blockly.blockRendering.Debug.config.rowSpacers) {
return;
}
const height = Math.abs(row.height);
const isNegativeSpacing = row.height < 0;
if (isNegativeSpacing) {
cursorY -= height;
}
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'rowSpacerRect blockRenderDebug',
'x': isRtl ? -(row.xPos + row.width) : row.xPos,
'y': cursorY,
'width': row.width,
'height': height,
'stroke': isNegativeSpacing ? 'black' : 'blue',
'fill': 'blue',
'fill-opacity': '0.5',
'stroke-width': '1px',
},
this.svgRoot_));
}
/**
* Draw a debug rectangle for a horizontal spacer.
* @param {!Blockly.blockRendering.InRowSpacer} elem The spacer to render.
* @param {number} rowHeight The height of the container row.
* @param {boolean} isRtl Whether the block is rendered RTL.
* @package
*/
drawSpacerElem(elem, rowHeight, isRtl) {
if (!Blockly.blockRendering.Debug.config.elemSpacers) {
return;
}
const width = Math.abs(elem.width);
const isNegativeSpacing = elem.width < 0;
let xPos = isNegativeSpacing ? elem.xPos - width : elem.xPos;
if (isRtl) {
xPos = -(xPos + width);
}
const yPos = elem.centerline - elem.height / 2;
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'elemSpacerRect blockRenderDebug',
'x': xPos,
'y': yPos,
'width': width,
'height': elem.height,
'stroke': 'pink',
'fill': isNegativeSpacing ? 'black' : 'pink',
'fill-opacity': '0.5',
'stroke-width': '1px',
},
this.svgRoot_));
}
/**
* Draw a debug rectangle for an in-row element.
* @param {!Blockly.blockRendering.Measurable} elem The element to render.
* @param {boolean} isRtl Whether the block is rendered RTL.
* @package
*/
drawRenderedElem(elem, isRtl) {
if (Blockly.blockRendering.Debug.config.elems) {
let xPos = elem.xPos;
if (isRtl) {
xPos = -(xPos + elem.width);
}
const yPos = elem.centerline - elem.height / 2;
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'rowRenderingRect blockRenderDebug',
'x': xPos,
'y': yPos,
'width': elem.width,
'height': elem.height,
'stroke': 'black',
'fill': 'none',
'stroke-width': '1px',
},
this.svgRoot_));
if (Blockly.blockRendering.Types.isField(elem) &&
elem.field instanceof Blockly.FieldLabel) {
const baseline = this.constants_.FIELD_TEXT_BASELINE;
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'rowRenderingRect blockRenderDebug',
'x': xPos,
'y': yPos + baseline,
'width': elem.width,
'height': '0.1px',
'stroke': 'red',
'fill': 'none',
'stroke-width': '0.5px',
},
this.svgRoot_));
}
}
if (Blockly.blockRendering.Types.isInput(elem) &&
Blockly.blockRendering.Debug.config.connections) {
this.drawConnection(elem.connectionModel);
}
}
/**
* Draw a circle at the location of the given connection. Inputs and outputs
* share the same colours, as do previous and next. When positioned correctly
* a connected pair will look like a bullseye.
* @param {Blockly.RenderedConnection} conn The connection to circle.
* @suppress {visibility} Suppress visibility of conn.offsetInBlock_ since
* this is a debug module.
* @package
*/
drawConnection(conn) {
if (!Blockly.blockRendering.Debug.config.connections) {
return;
}
let colour;
let size;
let fill;
if (conn.type == Blockly.INPUT_VALUE) {
size = 4;
colour = 'magenta';
fill = 'none';
} else if (conn.type == Blockly.OUTPUT_VALUE) {
size = 2;
colour = 'magenta';
fill = colour;
} else if (conn.type == Blockly.NEXT_STATEMENT) {
size = 4;
colour = 'goldenrod';
fill = 'none';
} else if (conn.type == Blockly.PREVIOUS_STATEMENT) {
size = 2;
colour = 'goldenrod';
fill = colour;
}
this.debugElements_.push(Blockly.utils.dom.createSvgElement('circle',
{
'class': 'blockRenderDebug',
'cx': conn.offsetInBlock_.x,
'cy': conn.offsetInBlock_.y,
'r': size,
'fill': fill,
'stroke': colour,
},
this.svgRoot_));
}
/**
* Draw a debug rectangle for a non-empty row.
* @param {!Blockly.blockRendering.Row} row The non-empty row to render.
* @param {number} cursorY The y position of the top of the row.
* @param {boolean} isRtl Whether the block is rendered RTL.
* @package
*/
drawRenderedRow(row, cursorY, isRtl) {
if (!Blockly.blockRendering.Debug.config.rows) {
return;
}
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'elemRenderingRect blockRenderDebug',
'x': isRtl ? -(row.xPos + row.width) : row.xPos,
'y': row.yPos,
'width': row.width,
'height': row.height,
'stroke': 'red',
'fill': 'none',
'stroke-width': '1px',
},
this.svgRoot_));
if (Blockly.blockRendering.Types.isTopOrBottomRow(row)) {
return;
}
if (Blockly.blockRendering.Debug.config.connectedBlockBounds) {
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'connectedBlockWidth blockRenderDebug',
'x': isRtl ? -(row.xPos + row.widthWithConnectedBlocks) : row.xPos,
'y': row.yPos,
'width': row.widthWithConnectedBlocks,
'height': row.height,
'stroke': this.randomColour_,
'fill': 'none',
'stroke-width': '1px',
'stroke-dasharray': '3,3',
},
this.svgRoot_));
}
}
/**
* Draw debug rectangles for a non-empty row and all of its subcomponents.
* @param {!Blockly.blockRendering.Row} row The non-empty row to render.
* @param {number} cursorY The y position of the top of the row.
* @param {boolean} isRtl Whether the block is rendered RTL.
* @package
*/
drawRowWithElements(row, cursorY, isRtl) {
for (let i = 0, l = row.elements.length; i < l; i++) {
const elem = row.elements[i];
if (!elem) {
console.warn('A row has an undefined or null element.', row, elem);
continue;
}
if (Blockly.blockRendering.Types.isSpacer(elem)) {
this.drawSpacerElem(
/** @type {!Blockly.blockRendering.InRowSpacer} */ (elem),
row.height, isRtl);
} else {
this.drawRenderedElem(elem, isRtl);
}
}
this.drawRenderedRow(row, cursorY, isRtl);
}
/**
* Draw a debug rectangle around the entire block.
* @param {!Blockly.blockRendering.RenderInfo} info Rendering information
* about the block to debug.
* @package
*/
drawBoundingBox(info) {
if (!Blockly.blockRendering.Debug.config.blockBounds) {
return;
}
// Bounding box without children.
let xPos = info.RTL ? -info.width : 0;
const yPos = 0;
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'blockBoundingBox blockRenderDebug',
'x': xPos,
'y': yPos,
'width': info.width,
'height': info.height,
'stroke': 'black',
'fill': 'none',
'stroke-width': '1px',
'stroke-dasharray': '5,5',
},
this.svgRoot_));
if (Blockly.blockRendering.Debug.config.connectedBlockBounds) {
// Bounding box with children.
xPos = info.RTL ? -info.widthWithChildren : 0;
this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect',
{
'class': 'blockRenderDebug',
'x': xPos,
'y': yPos,
'width': info.widthWithChildren,
'height': info.height,
'stroke': '#DF57BC',
'fill': 'none',
'stroke-width': '1px',
'stroke-dasharray': '3,3',
},
this.svgRoot_));
}
}
/**
* Do all of the work to draw debug information for the whole block.
* @param {!Blockly.BlockSvg} block The block to draw debug information for.
* @param {!Blockly.blockRendering.RenderInfo} info Rendering information
* about the block to debug.
* @package
*/
drawDebug(block, info) {
this.clearElems();
this.svgRoot_ = block.getSvgRoot();
this.randomColour_ =
'#' + Math.floor(Math.random() * 16777215).toString(16);
let cursorY = 0;
for (let i = 0, row; (row = info.rows[i]); i++) {
if (Blockly.blockRendering.Types.isBetweenRowSpacer(row)) {
this.drawSpacerRow(row, cursorY, info.RTL);
} else {
this.drawRowWithElements(row, cursorY, info.RTL);
}
cursorY += row.height;
}
if (block.previousConnection) {
this.drawConnection(block.previousConnection);
}
if (block.nextConnection) {
this.drawConnection(block.nextConnection);
}
if (block.outputConnection) {
this.drawConnection(block.outputConnection);
}
if (info.rightSide) {
this.drawRenderedElem(info.rightSide, info.RTL);
}
this.drawBoundingBox(info);
this.drawRender(block.pathObject.svgPath);
}
/**
* Show a debug filter to highlight that a block has been rendered.
* @param {!SVGElement} svgPath The block's svg path.
* @package
*/
drawRender(svgPath) {
if (!Blockly.blockRendering.Debug.config.render) {
return;
}
svgPath.setAttribute('filter',
'url(#' + this.constants_.debugFilterId + ')');
setTimeout(function() {
svgPath.setAttribute('filter', '');
}, 100);
}
}
/**
* Configuration object containing booleans to enable and disable debug
* rendering of specific rendering components.
* @type {!Object.<string, boolean>}
*/
DebugRenderer.config = {
rowSpacers: true,
elemSpacers: true,
rows: true,
elems: true,
connections: true,
blockBounds: true,
connectedBlockBounds: true,
render: true,
};