运行线拖动添加吸附逻辑

This commit is contained in:
fan 2023-06-14 13:50:50 +08:00
parent b40eb6f5e8
commit e62b93d785

View File

@ -5,6 +5,9 @@ import {
GraphicInteractionPlugin,
linePoint,
GraphicApp,
AbsorbablePosition,
DraggablePoint,
GraphicTransformEvent,
} from 'src/jl-graphic';
import {
IRunLineData,
@ -19,6 +22,7 @@ import {
clearWayPoint,
clearWaypointsConfig,
getWaypointRangeIndex,
ILineGraphic,
} from 'src/jl-graphic/plugins/GraphicEditPlugin';
import {
FederatedPointerEvent,
@ -30,6 +34,7 @@ import {
FederatedMouseEvent,
} from 'pixi.js';
import { ContextMenu } from 'src/jl-graphic/ui/ContextMenu';
import { AbsorbableLine } from 'src/jl-graphic/graphic/AbsorbablePosition';
export interface IRunLineDrawOptions {
newData: () => IRunLineData;
@ -118,6 +123,62 @@ export class RunLineGraphicHitArea implements IHitArea {
}
}
function buildAbsorbablePositions(
runLine: RunLine,
dpIndex: number
): AbsorbablePosition[] {
const aps: AbsorbablePosition[] = [];
const canvas = runLine.getCanvas();
if (runLine.linePoints[dpIndex - 1]) {
const preP = runLine.localToCanvasPoint(runLine.linePoints[dpIndex - 1]);
const ala = new AbsorbableLine(
new Point(preP.x, 0),
new Point(preP.x, canvas.height)
);
const alb = new AbsorbableLine(
new Point(0, preP.y),
new Point(canvas.width, preP.y)
);
aps.push(ala);
aps.push(alb);
}
if (runLine.linePoints[dpIndex + 1]) {
const nextP = runLine.localToCanvasPoint(runLine.linePoints[dpIndex + 1]);
const ala = new AbsorbableLine(
new Point(nextP.x, 0),
new Point(nextP.x, canvas.height)
);
const alb = new AbsorbableLine(
new Point(0, nextP.y),
new Point(canvas.width, nextP.y)
);
aps.push(ala);
aps.push(alb);
}
return aps;
}
/**
*
* @param g
* @param dp
* @param index
*/
function onEditPointCreate(
g: ILineGraphic,
dp: DraggablePoint,
index: number
): void {
const runLine = g as RunLine;
dp.on('transformstart', (e: GraphicTransformEvent) => {
if (e.isShift()) {
runLine.getGraphicApp().setOptions({
absorbablePositions: buildAbsorbablePositions(runLine, index),
});
}
});
}
const RunLineEditMenu: ContextMenu = ContextMenu.init({
name: '运行线编辑菜单',
groups: [
@ -202,7 +263,7 @@ export class RunLinePointsEditPlugin extends GraphicInteractionPlugin<RunLine> {
PolylineEditPlugin.Name
);
if (!lep) {
lep = new PolylineEditPlugin(runLine);
lep = new PolylineEditPlugin(runLine, { onEditPointCreate });
runLine.addAssistantAppend(lep);
}
lep.showAll();