Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend default connection previewer #9903

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions pxtblocks/plugins/renderer/connectionPreviewer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as Blockly from "blockly";

export class ConnectionPreviewer implements Blockly.IConnectionPreviewer {
export class ConnectionPreviewer extends Blockly.InsertionMarkerPreviewer {
static CONNECTION_INDICATOR_RADIUS = 9;

protected connectionLine: SVGLineElement;
protected staticConnectionIndicator: SVGElement;
protected draggedConnectionIndicator: SVGElement;
protected staticConnection: Blockly.RenderedConnection;

previewReplacement(draggedConn: Blockly.RenderedConnection, staticConn: Blockly.RenderedConnection, replacedBlock: Blockly.BlockSvg): void {
super.previewReplacement(draggedConn, staticConn, replacedBlock);
if (!this.connectionLine) {
this.connectionLine = Blockly.utils.dom.createSvgElement(
'line',
Expand All @@ -25,14 +25,6 @@ export class ConnectionPreviewer implements Blockly.IConnectionPreviewer {
this.draggedConnectionIndicator = this.createConnectionIndicator(draggedConn);
}

if (this.staticConnection !== staticConn) {
if (this.staticConnectionIndicator) {
this.staticConnectionIndicator.remove();
}
this.staticConnection = staticConn;
this.staticConnectionIndicator = this.createConnectionIndicator(staticConn);
}

const radius = ConnectionPreviewer.CONNECTION_INDICATOR_RADIUS;
const offset = draggedConn.getOffsetInBlock();

Expand Down Expand Up @@ -64,26 +56,17 @@ export class ConnectionPreviewer implements Blockly.IConnectionPreviewer {
}
}

previewConnection(draggedConn: Blockly.RenderedConnection, staticConn: Blockly.RenderedConnection): void {

}

hidePreview(): void {
super.hidePreview();
if (this.connectionLine) {
this.connectionLine.remove();
this.connectionLine = null;
this.staticConnectionIndicator.remove();
this.staticConnectionIndicator = null;
this.draggedConnectionIndicator.remove();
this.draggedConnectionIndicator = null;
this.staticConnection = null;
}
}

dispose(): void {
this.hidePreview();
}

protected createConnectionIndicator(connection: Blockly.RenderedConnection): SVGElement {
const result = Blockly.utils.dom.createSvgElement('g',
{ 'class': 'blocklyInputConnectionIndicator' },
Expand Down
32 changes: 32 additions & 0 deletions pxtblocks/plugins/renderer/pathObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const DOTTED_OUTLINE_HOVER_CLASS = "blockly-dotted-outline-on-hover"
const HOVER_CLASS = "hover"

export class PathObject extends Blockly.zelos.PathObject {
static CONNECTION_INDICATOR_RADIUS = 9;

protected svgPathHighlighted: SVGElement;
protected hasError: boolean;

Expand All @@ -13,6 +15,9 @@ export class PathObject extends Blockly.zelos.PathObject {
protected mouseOverData: Blockly.browserEvents.Data;
protected mouseLeaveData: Blockly.browserEvents.Data;

protected connectionPointIndicators = new WeakMap<Blockly.RenderedConnection, SVGElement>();


override updateHighlighted(enable: boolean) {
// this.setClass_('blocklySelected', enable);
if (enable) {
Expand Down Expand Up @@ -42,6 +47,33 @@ export class PathObject extends Blockly.zelos.PathObject {
super.updateSelected(enable);
}

override addConnectionHighlight(connection: Blockly.RenderedConnection, connectionPath: string, offset: Blockly.utils.Coordinate, rtl: boolean): void {
super.addConnectionHighlight(connection, connectionPath, offset, rtl);

if (connection.type === Blockly.INPUT_VALUE || connection.type === Blockly.OUTPUT_VALUE) {
const indicator = Blockly.utils.dom.createSvgElement('g',
{ 'class': 'blocklyInputConnectionIndicator' }
);
Blockly.utils.dom.createSvgElement('circle',
{ 'r': PathObject.CONNECTION_INDICATOR_RADIUS }, indicator);

const offset = connection.getOffsetInBlock();
indicator.setAttribute('transform',
'translate(' + offset.x + ',' + offset.y + ')');
this.connectionPointIndicators.set(connection, indicator);
this.svgRoot.appendChild(indicator);
}
}

override removeConnectionHighlight(connection: Blockly.RenderedConnection): void {
super.removeConnectionHighlight(connection);

if (this.connectionPointIndicators.has(connection)) {
this.connectionPointIndicators.get(connection).remove();
this.connectionPointIndicators.delete(connection);
}
}

setHasDottedOutllineOnHover(enabled: boolean) {
this.hasDottedOutlineOnHover = enabled;

Expand Down