Skip to content

Commit

Permalink
✨ feat: add SelectNodeStep
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Nov 3, 2024
1 parent 6ef3b03 commit fa46813
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
8 changes: 7 additions & 1 deletion packages/chili-core/src/selectionFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { INode } from "./model";
import { INode, ShapeNode } from "./model";
import { IShape } from "./shape";

export interface IShapeFilter {
Expand All @@ -10,3 +10,9 @@ export interface IShapeFilter {
export interface INodeFilter {
allow(node: INode): boolean;
}

export class ShapeNodeFilter implements INodeFilter {
allow(node: INode): boolean {
return node instanceof ShapeNode;
}
}
1 change: 0 additions & 1 deletion packages/chili-vis/src/nodeSelectionEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import {
AsyncController,
GeometryNode,
IDocument,
INodeFilter,
IView,
Expand Down
6 changes: 3 additions & 3 deletions packages/chili/src/commands/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { GeometryNode, IShape, Result, ShapeNode, command } from "chili-core";
import { BooleanNode } from "../bodys/boolean";
import { IStep, SelectNodeStep } from "../step";
import { IStep, SelectShapeNodeStep } from "../step";
import { CreateCommand } from "./createCommand";

export abstract class BooleanOperate extends CreateCommand {
Expand Down Expand Up @@ -30,8 +30,8 @@ export abstract class BooleanOperate extends CreateCommand {

protected override getSteps(): IStep[] {
return [
new SelectNodeStep("prompt.select.shape", false),
new SelectNodeStep("prompt.select.shape", false, {
new SelectShapeNodeStep("prompt.select.shape", false),
new SelectShapeNodeStep("prompt.select.shape", false, {
allow: (shape) => {
return !this.stepDatas[0].nodes
?.map((x) => (x as ShapeNode).shape.value)
Expand Down
4 changes: 2 additions & 2 deletions packages/chili/src/commands/create/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "chili-core";
import { FaceNode } from "../../bodys/face";
import { WireNode } from "../../bodys/wire";
import { SelectNodeStep } from "../../step";
import { SelectShapeNodeStep } from "../../step";

abstract class ConvertCommand extends CancelableCommand {
async executeAsync(): Promise<void> {
Expand Down Expand Up @@ -49,7 +49,7 @@ abstract class ConvertCommand extends CancelableCommand {
let models = this._getSelectedModels(document, filter);
document.selection.clearSelection();
if (models.length > 0) return models;
let step = new SelectNodeStep("prompt.select.models", true, filter);
let step = new SelectShapeNodeStep("prompt.select.models", true, filter);
this.controller = new AsyncController();
let data = await step.execute(document, this.controller);
document.selection.clearSelection();
Expand Down
9 changes: 3 additions & 6 deletions packages/chili/src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ export class Selection implements ISelection, IDisposable {
}

async pickNode(prompt: I18nKeys, controller: AsyncController, multiMode: boolean) {
try {
let handler = new NodeSelectionHandler(this.document, multiMode, controller, this.nodeFilter);
await this.pickAsync(handler, prompt, controller, multiMode);
return handler.nodes();
} finally {
}
let handler = new NodeSelectionHandler(this.document, multiMode, controller, this.nodeFilter);
await this.pickAsync(handler, prompt, controller, multiMode);
return handler.nodes();
}

async pickAsync(
Expand Down
47 changes: 40 additions & 7 deletions packages/chili/src/step/selectStep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { AsyncController, I18nKeys, IDocument, IShapeFilter, ShapeType } from "chili-core";
import {
AsyncController,
I18nKeys,
IDocument,
INodeFilter,
IShapeFilter,
ShapeNodeFilter,
ShapeType,
} from "chili-core";
import { SnapedData } from "../snap";
import { IStep } from "./step";

Expand All @@ -13,15 +21,15 @@ export abstract class SelectStep implements IStep {
) {}

async execute(document: IDocument, controller: AsyncController): Promise<SnapedData | undefined> {
let oldShapeType = document.selection.shapeType;
let oldFilter = document.selection.shapeFilter;
const { shapeType, shapeFilter, nodeFilter } = document.selection;
try {
document.selection.shapeType = this.snapeType;
document.selection.shapeFilter = this.filter;
return await this.select(document, controller);
} finally {
document.selection.shapeType = oldShapeType;
document.selection.shapeFilter = oldFilter;
document.selection.shapeType = shapeType;
document.selection.shapeFilter = shapeFilter;
document.selection.nodeFilter = nodeFilter;
}
}

Expand All @@ -42,7 +50,7 @@ export class SelectShapeStep extends SelectStep {
}
}

export class SelectNodeStep extends SelectStep {
export class SelectShapeNodeStep extends SelectStep {
constructor(prompt: I18nKeys, multiple: boolean, filter?: IShapeFilter) {
super(ShapeType.Shape, prompt, multiple, filter);
}
Expand All @@ -51,12 +59,37 @@ export class SelectNodeStep extends SelectStep {
document: IDocument,
controller: AsyncController,
): Promise<SnapedData | undefined> {
document.selection.nodeFilter = new ShapeNodeFilter();
let nodes = await document.selection.pickNode(this.prompt, controller, this.multiple);
if (nodes.length === 0) return undefined;
return {
view: document.application.activeView!,
shapes: [],
nodes: nodes,
nodes,
};
}
}

export class SelectNodeStep implements IStep {
constructor(
readonly prompt: I18nKeys,
readonly multiple: boolean = false,
readonly filter?: INodeFilter,
) {}

async execute(document: IDocument, controller: AsyncController): Promise<SnapedData | undefined> {
const oldFilter = document.selection.nodeFilter;
try {
document.selection.nodeFilter = this.filter;
let nodes = await document.selection.pickNode(this.prompt, controller, this.multiple);
if (nodes.length === 0) return undefined;
return {
view: document.application.activeView!,
shapes: [],
nodes,
};
} finally {
document.selection.nodeFilter = oldFilter;
}
}
}

0 comments on commit fa46813

Please sign in to comment.