wire hover + disconnect

This commit is contained in:
Patrick Haßel 2025-02-04 08:55:55 +01:00
parent f835f7364c
commit 38ed739f46
7 changed files with 22 additions and 7 deletions

View File

@ -85,15 +85,14 @@ export class Circuit {
} }
connect(start: Junction, end: Junction) { connect(start: Junction, end: Junction) {
const wire = new Wire(start, end, RESISTANCE_MIN); const wire = new Wire(start, end, RESISTANCE_MIN, "", (wire) => this.disconnect(wire));
this.wires.push(wire); this.wires.push(wire);
console.log(`Wire connected: ${wire}`); console.log(`Wire connected: ${wire}`);
this.calculate(); this.calculate();
} }
disconnect(wire: Wire) { private disconnect(wire: Wire) {
this.wires.splice(this.wires.indexOf(wire), 1); this.wires.splice(this.wires.indexOf(wire), 1);
wire.disconnect();
console.log("Wire disconnected: ", wire); console.log("Wire disconnected: ", wire);
this.calculate(); this.calculate();
} }

View File

@ -2,7 +2,7 @@
<g inner-part *ngFor="let part of circuit.parts" [part]="part" [parts]="parts" [wires]="wires"></g> <g inner-part *ngFor="let part of circuit.parts" [part]="part" [parts]="parts" [wires]="wires"></g>
<g inner-wire *ngFor="let wire of circuit.wires" [globalWire]="wire"></g> <g inner-wire *ngFor="let wire of circuit.wires" [globalWire]="wire" extraClassesBack="wireBackGlobal"></g>
<ng-container *ngIf="wires.dragStart && wires.dragCursor && wires.dragStartJunction"> <ng-container *ngIf="wires.dragStart && wires.dragCursor && wires.dragStartJunction">
<ng-container *ngIf="wires.dragEnd && wires.dragEndJunction"> <ng-container *ngIf="wires.dragEnd && wires.dragEndJunction">

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -21,7 +21,7 @@ export class EditorComponent implements OnInit {
private _circuit: Circuit = Circuit.new(); private _circuit: Circuit = Circuit.new();
ngOnInit(): void { ngOnInit(): void {
this.circuit = DEMO_004; this.circuit = DEMO_003;
} }
set circuit(circuit: Circuit) { set circuit(circuit: Circuit) {

View File

@ -13,6 +13,7 @@ export class Wire {
readonly end: Junction, readonly end: Junction,
public resistance: number, public resistance: number,
public name: string | null = null, public name: string | null = null,
readonly disconnectCallback?: (wire: Wire) => any,
) { ) {
if (this.resistance === 0) { if (this.resistance === 0) {
this.resistance = RESISTANCE_MIN; this.resistance = RESISTANCE_MIN;
@ -24,6 +25,9 @@ export class Wire {
disconnect() { disconnect() {
this.start.wires.splice(this.start.wires.indexOf(this), 1); this.start.wires.splice(this.start.wires.indexOf(this), 1);
this.end.wires.splice(this.end.wires.indexOf(this), 1); this.end.wires.splice(this.end.wires.indexOf(this), 1);
if (this.disconnectCallback) {
this.disconnectCallback(this);
}
} }
toString() { toString() {

View File

@ -2,7 +2,11 @@
stroke-linecap: round; stroke-linecap: round;
stroke-width: 11px; stroke-width: 11px;
stroke: black; stroke: black;
pointer-events: none; }
.wireBackGlobal:hover {
stroke: darkgray;
stroke-width: 15px;
} }
.wire { .wire {

View File

@ -1,6 +1,6 @@
<svg width="100%" height="100%"> <svg width="100%" height="100%">
<line class="wireBack {{extraClassesBack}}" [attr.x1]="x1" [attr.y1]="y1" [attr.x2]="x2" [attr.y2]="y2"> <line class="wireBack {{extraClassesBack}}" [attr.x1]="x1" [attr.y1]="y1" [attr.x2]="x2" [attr.y2]="y2" (contextmenu)="click($event)">
<!-- nothing --> <!-- nothing -->
</line> </line>

Before

Width:  |  Height:  |  Size: 462 B

After

Width:  |  Height:  |  Size: 492 B

View File

@ -169,4 +169,12 @@ export class WireComponent {
return fadeColor((ratio - 0.5) * 2, 'magenta', 'red'); return fadeColor((ratio - 0.5) * 2, 'magenta', 'red');
} }
click($event: MouseEvent) {
console.log($event);
$event.preventDefault();
if ($event.button === 2 && this.wire) {
this.wire.disconnect();
}
}
} }