Skip to content

Commit

Permalink
feat: box select action when pressing space
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed May 21, 2024
1 parent 89048ca commit e0a1132
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
34 changes: 18 additions & 16 deletions packages/core/src/tools/tool_draw_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,6 @@ export abstract class DrawGraphTool implements ITool {
}
};
hotkeysManager.on('shiftToggle', updateRect);
hotkeysManager.on('altToggle', updateRect);

// 记录空格键按下时的坐标位置
const handleSpaceToggle = (isDown: boolean) => {
if (this.isDragging && isDown) {
this.startPointWhenSpaceDown = this.startPoint;
this.lastDragPointWhenSpaceDown = this.lastMousePoint;
this.updateRect();
} else {
this.startPointWhenSpaceDown = null;
this.lastDragPointWhenSpaceDown = null;
}
};
hotkeysManager.on('spaceToggle', handleSpaceToggle);

const updateRectWhenViewportTranslate = () => {
if (editor.hostEventManager.isDraggingCanvasBySpace) {
Expand All @@ -78,11 +64,27 @@ export abstract class DrawGraphTool implements ITool {

this.unbindEvent = () => {
hotkeysManager.off('shiftToggle', updateRect);
hotkeysManager.off('altToggle', updateRect);
editor.viewportManager.off('xOrYChange', updateRectWhenViewportTranslate);
hotkeysManager.off('spaceToggle', handleSpaceToggle);
};
}

onSpaceToggle(isSpacePressing: boolean) {
if (this.isDragging && isSpacePressing) {
this.startPointWhenSpaceDown = this.startPoint;
this.lastDragPointWhenSpaceDown = this.lastMousePoint;
this.updateRect();
} else {
this.startPointWhenSpaceDown = null;
this.lastDragPointWhenSpaceDown = null;
}
}

onAltToggle() {
if (this.isDragging) {
this.updateRect();
}
}

onInactive() {
this.unbindEvent();
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/tools/tool_select/tool_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,10 @@ export class SelectTool implements ITool {
}
// TODO: resetHoverItem after drag canvas end
}

onSpaceToggle(isSpacePressing: boolean) {
if (this.currStrategy?.onSpaceToggle) {
this.currStrategy.onSpaceToggle(isSpacePressing);
}
}
}
54 changes: 48 additions & 6 deletions packages/core/src/tools/tool_select/tool_select_selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { getRectByTwoPoint, type IPoint } from '@suika/geo';

import { type Editor } from '../../editor';
import { type Graph } from '../../graphs';
import { SnapHelper } from '../../snap';
import { type IBaseTool } from '../type';

/**
* draw selection box
*/
export class DrawSelection implements IBaseTool {
private lastPoint: IPoint = { x: -1, y: -1 };
private startPoint: IPoint = { x: -1, y: -1 };
private isShiftPressingWhenStart = false;
private startSelectedGraphs: Graph[] = [];
private startPointWhenSpaceDown: IPoint | null = null;
private lastDragPointWhenSpaceDown: IPoint | null = null;
private lastMouseScenePoint!: IPoint;
private lastMousePoint!: IPoint;

constructor(private editor: Editor) {}
onActive() {
Expand All @@ -29,16 +34,40 @@ export class DrawSelection implements IBaseTool {
this.editor.selectedElements.clear();
}

const pos = this.editor.getCursorXY(e);
this.lastPoint = this.editor.viewportCoordsToScene(pos.x, pos.y);
this.startPoint = SnapHelper.getSnapPtBySetting(
this.editor.getSceneCursorXY(e),
this.editor.setting,
);

this.editor.render();
this.editor.sceneGraph.setSelection(this.lastPoint);
this.editor.sceneGraph.setSelection(this.startPoint);
}
onDrag(e: PointerEvent) {
const point = this.editor.getSceneCursorXY(e);
this.lastMouseScenePoint = this.editor.getSceneCursorXY(e);

const box = getRectByTwoPoint(this.lastPoint, point);
this.lastMousePoint = SnapHelper.getSnapPtBySetting(
this.lastMouseScenePoint,
this.editor.setting,
);

this.updateSelection();
}

private updateSelection() {
const { x, y } = this.lastMouseScenePoint;

if (this.startPointWhenSpaceDown && this.lastDragPointWhenSpaceDown) {
const { x: sx, y: sy } = this.startPointWhenSpaceDown;
const { x: lx, y: ly } = this.lastDragPointWhenSpaceDown;
const dx = x - lx;
const dy = y - ly;
this.startPoint = {
x: sx + dx,
y: sy + dy,
};
}

const box = getRectByTwoPoint(this.startPoint, this.lastMouseScenePoint);
this.editor.sceneGraph.setSelection(box);

const graphsInSelection = this.editor.sceneGraph.getElementsInSelection();
Expand All @@ -60,5 +89,18 @@ export class DrawSelection implements IBaseTool {
this.startSelectedGraphs = [];
this.editor.sceneGraph.selection = null;
this.editor.render();
this.startPointWhenSpaceDown = null;
this.lastDragPointWhenSpaceDown = null;
}

onSpaceToggle(isSpacePressing: boolean) {
if (this.editor.toolManager.isDragging() && isSpacePressing) {
this.startPointWhenSpaceDown = this.startPoint;
this.lastDragPointWhenSpaceDown = this.lastMousePoint;
this.updateSelection();
} else {
this.startPointWhenSpaceDown = null;
this.lastDragPointWhenSpaceDown = null;
}
}
}

0 comments on commit e0a1132

Please sign in to comment.