Skip to content

Commit

Permalink
deferred connection rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Jul 25, 2022
1 parent aa92ba5 commit a0f6c38
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"env": {
"es6": true,
"mocha": true
"mocha": true,
"browser": true
}
}
}
26 changes: 16 additions & 10 deletions src/view/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ export class ConnectionView extends Emitter<EventsTypes> {
this.el.style.position = 'absolute';
this.el.style.zIndex = '-1';

this.trigger('renderconnection', {
el: this.el,
connection: this.connection,
this.trigger('renderconnection', {
el: this.el,
connection: this.connection,
points: this.getPoints()
});
}

getPoints() {
const [x1, y1] = this.outputNode.getSocketPosition(this.connection.output);
const [x2, y2] = this.inputNode.getSocketPosition(this.connection.input);
const { input, output } = this.connection

return [x1, y1, x2, y2];
if (this.inputNode.hasSocket(input) && this.outputNode.hasSocket(output)) {
const [x1, y1] = this.outputNode.getSocketPosition(output);
const [x2, y2] = this.inputNode.getSocketPosition(input);

return [x1, y1, x2, y2];
}

return [0, 0, 0, 0]
}

update() {
this.trigger('updateconnection', {
el: this.el,
connection: this.connection,
this.trigger('updateconnection', {
el: this.el,
connection: this.connection,
points: this.getPoints()
});
}
}
}
19 changes: 15 additions & 4 deletions src/view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class EditorView extends Emitter<EventsTypes> {
connections = new Map<Connection, ConnectionView>();
area: Area;

// eslint-disable-next-line max-statements
constructor(container: HTMLElement, components: Map<string, Component>, emitter: Emitter<EventsTypes>) {
super(emitter);

Expand All @@ -28,9 +29,19 @@ export class EditorView extends Emitter<EventsTypes> {
this.container.addEventListener('contextmenu', e => this.trigger('contextmenu', { e, view: this }));
emitter.on('destroy', listenWindow('resize', this.resize.bind(this)));
emitter.on('destroy', () => this.nodes.forEach(view => view.destroy()));

this.on('nodetranslated', this.updateConnections.bind(this));

this.on('rendersocket', ({ socket }) => {
const connections = Array.from(this.connections.entries())
const relatedConnections = connections.filter(([connection]) => {
const { input, output } = connection

return [input.socket, output.socket].includes(socket)
})

relatedConnections.forEach(([_, view]) => requestAnimationFrame(() => view.update()))
})

this.area = new Area(container, this);
this.container.appendChild(this.area.el);
}
Expand All @@ -39,7 +50,7 @@ export class EditorView extends Emitter<EventsTypes> {
const component = this.components.get(node.name);

if (!component) throw new Error(`Component ${node.name} not found`);

const nodeView = new NodeView(node, component, this);

this.nodes.set(node, nodeView);
Expand Down Expand Up @@ -105,7 +116,7 @@ export class EditorView extends Emitter<EventsTypes> {

click(e: Event) {
const container = this.container;

if (container !== e.target) return;
if (!this.trigger('click', { e, container })) return;
}
Expand Down
16 changes: 10 additions & 6 deletions src/view/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export class NodeView extends Emitter<EventsTypes> {
});

this.trigger('rendernode', {
el: this.el,
node,
component: component.data,
el: this.el,
node,
component: component.data,
bindSocket: this.bindSocket.bind(this),
bindControl: this.bindControl.bind(this)
});
Expand All @@ -47,7 +47,7 @@ export class NodeView extends Emitter<EventsTypes> {

clearSockets() {
const ios: IO[] = [ ...this.node.inputs.values(), ...this.node.outputs.values()];

this.sockets.forEach(s => {
if (!ios.includes(s.io)) this.sockets.delete(s.io);
});
Expand All @@ -62,6 +62,10 @@ export class NodeView extends Emitter<EventsTypes> {
this.controls.set(control, new ControlView(el, control, this));
}

hasSocket(io: IO) {
return this.sockets.has(io)
}

getSocketPosition(io: IO) {
const socket = this.sockets.get(io);

Expand All @@ -72,7 +76,7 @@ export class NodeView extends Emitter<EventsTypes> {

onSelect(e: MouseEvent) {
const payload = { node: this.node, accumulate: e.ctrlKey, e };

this.onStart();
this.trigger('multiselectnode', payload);
this.trigger('selectnode', payload);
Expand Down Expand Up @@ -116,7 +120,7 @@ export class NodeView extends Emitter<EventsTypes> {
}

remove() {

}

destroy() {
Expand Down

0 comments on commit a0f6c38

Please sign in to comment.