Skip to content

Commit

Permalink
✨ feat: Optimized snap prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Apr 16, 2024
1 parent d5795dd commit 0516ec6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/chili/src/snap/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface SnapedData {
view: IView;
point?: XYZ;
info?: string;
refPoint?: XYZ;
shapes: VisualShapeData[];
models?: IModel[];
}
Expand Down
10 changes: 8 additions & 2 deletions packages/chili/src/snap/objectSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ export class ObjectSnap implements ISnapper {
}

snap(data: MouseAndDetected): SnapedData | undefined {
let snap: SnapedData | undefined;
if (data.shapes.length > 0) {
this.showInvisibleSnaps(data.view, data.shapes[0]);
return this.snapOnShape(data.view, data.mx, data.my, data.shapes);
snap = this.snapOnShape(data.view, data.mx, data.my, data.shapes);
} else {
snap = this.snapeInvisible(data.view, data.mx, data.my);
}
if (this.referencePoint && snap?.point) {
snap.info += " -> " + this.referencePoint?.().distanceTo(snap.point).toFixed(2);
}
return this.snapeInvisible(data.view, data.mx, data.my);
return snap;
}

private snapOnShape(view: IView, x: number, y: number, shapes: VisualShapeData[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export abstract class SnapEventHandler implements IEventHandler {
this.removeTempObject();
this.setSnaped(view, event);
if (this._snaped !== undefined) {
this.switchSnapedPrompt(this.data.prompt?.(this._snaped) ?? this.snaped?.info);
this.switchSnapedPrompt(this.data.prompt?.(this._snaped) ?? this._snaped.info);
} else {
this.clearSnapTip();
}
Expand Down
14 changes: 10 additions & 4 deletions packages/chili/src/snap/snapEventHandler/snapPointEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export class SnapPointEventHandler extends SnapEventHandler {

protected getPointFromInput(view: IView, text: string): XYZ {
let dims = text.split(",").map((x) => Number(x));
let result = this.pointData.refPoint?.() ?? XYZ.zero;
let refPoint = this.getRefPoint();
let result = refPoint ?? XYZ.zero;
let end = this._snaped!.point!;
if (dims.length === 1 && end !== undefined) {
let vector = end.sub(this.pointData.refPoint!()).normalize()!;
let vector = end.sub(refPoint!).normalize()!;
result = result.add(vector.multiply(dims[0]));
} else if (dims.length > 1) {
let plane = this.pointData.plane?.() ?? view.workplane;
Expand All @@ -43,20 +44,25 @@ export class SnapPointEventHandler extends SnapEventHandler {
protected inputError(text: string): I18nKeys | undefined {
let dims = text.split(",").map((x) => Number(x));
let dimension = Dimension.from(dims.length);
let refPoint = this.getRefPoint();
if (!Dimension.contains(this.pointData.dimension!, dimension)) {
return "error.input.unsupportedInputs";
} else if (dims.some((x) => Number.isNaN(x))) {
return "error.input.invalidNumber";
} else if (this.pointData.refPoint === undefined) {
} else if (refPoint === undefined) {
if (dims.length !== 3) {
return "error.input.threeNumberCanBeInput";
}
} else if (
dims.length === 1 &&
(this._snaped === undefined || this._snaped.point!.isEqualTo(this.pointData.refPoint()))
(this._snaped === undefined || this._snaped.point!.isEqualTo(refPoint))
) {
return "error.input.cannotInputANumber";
}
return undefined;
}

private getRefPoint(): XYZ | undefined {
return this.pointData.refPoint?.() ?? this._snaped?.refPoint;
}
}
5 changes: 4 additions & 1 deletion packages/chili/src/snap/tracking/trackingSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ export class TrackingSnap implements ISnapper {
if (trackingDatas.length === 1) {
let distance = point.distanceTo(trackingDatas[0].axis.location);
info = `${trackingDatas[0].axis.name}: ${distance.toFixed(2)}`;
} else if (trackingDatas.length === 2) {
info = I18n.translate("snap.intersection");
}
return { view, point, info, shapes: [] };
let refPoint = trackingDatas[0].axis.location;
return { view, point, info, shapes: [], refPoint };
}

private showTempLine(view: IView, start: XYZ, end: XYZ): number | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/chili/src/step/pointStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StepBase } from "./step";

function defaultSnapedData(): SnapPointData {
return {
dimension: Dimension.D1D2D3,
dimension: Dimension.D1 | Dimension.D1D2D3,
};
}

Expand Down

0 comments on commit 0516ec6

Please sign in to comment.