2025-10-20 18:31:20 +08:00
|
|
|
import {
|
|
|
|
|
type MapAreaInfo,
|
|
|
|
|
MapAreaType,
|
|
|
|
|
type MapPen,
|
|
|
|
|
MapPointType,
|
2025-10-15 15:19:14 +08:00
|
|
|
type Point,
|
|
|
|
|
} from '@api/map';
|
2025-10-20 18:31:20 +08:00
|
|
|
import { DOOR_AREA_TYPE } from '@api/map/door-area';
|
|
|
|
|
import { LockState, s8 } from '@meta2d/core';
|
|
|
|
|
|
|
|
|
|
import type { EditorService } from '../editor.service';
|
|
|
|
|
import type { AutoStorageGenerator } from '../utils/auto-storage-generator';
|
|
|
|
|
|
|
|
|
|
export class AreaManager {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly editor: EditorService,
|
|
|
|
|
private readonly autoStorageGenerator: AutoStorageGenerator,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
public getBoundAreas(id: string, name: 'point' | 'line', type: MapAreaType): MapPen[] {
|
|
|
|
|
if (!id) return [];
|
|
|
|
|
return this.editor.find(`area-${type}`).filter(({ area }) => {
|
|
|
|
|
if (name === 'point') return area?.points?.includes(id);
|
|
|
|
|
if (name === 'line') return area?.routes?.includes(id);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async addArea(p1: Point, p2: Point, type = MapAreaType.库区, id?: string): Promise<void> {
|
|
|
|
|
const w = Math.abs(p1.x - p2.x);
|
|
|
|
|
const h = Math.abs(p1.y - p2.y);
|
|
|
|
|
if (w < 50 || h < 60) return;
|
|
|
|
|
|
|
|
|
|
const points: string[] = [];
|
|
|
|
|
const routes: string[] = [];
|
|
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
|
id = s8();
|
|
|
|
|
const selected = this.editor.store.active as MapPen[] | undefined;
|
|
|
|
|
switch (type) {
|
|
|
|
|
case MapAreaType.库区:
|
|
|
|
|
selected
|
|
|
|
|
?.filter(({ point }) => point?.type === MapPointType.动作点)
|
|
|
|
|
.forEach(({ id: pointId }) => points.push(pointId!));
|
|
|
|
|
break;
|
|
|
|
|
case MapAreaType.互斥区:
|
|
|
|
|
case MapAreaType.非互斥区:
|
|
|
|
|
case MapAreaType.约束区:
|
|
|
|
|
selected?.filter(({ point }) => point?.type).forEach(({ id: pointId }) => points.push(pointId!));
|
|
|
|
|
break;
|
|
|
|
|
case MapAreaType.描述区:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 门区域:自动绑定矩形内的路段(采用路段两端点均在区域内的简单判定)
|
|
|
|
|
if ((type as any) === (DOOR_AREA_TYPE as any)) {
|
|
|
|
|
const areaRect = {
|
|
|
|
|
x: Math.min(p1.x, p2.x),
|
|
|
|
|
y: Math.min(p1.y, p2.y),
|
|
|
|
|
width: Math.abs(p1.x - p2.x),
|
|
|
|
|
height: Math.abs(p1.y - p2.y),
|
|
|
|
|
};
|
|
|
|
|
const isInRect = (x: number, y: number) =>
|
|
|
|
|
x >= areaRect.x && x <= areaRect.x + areaRect.width && y >= areaRect.y && y <= areaRect.y + areaRect.height;
|
|
|
|
|
|
|
|
|
|
const allRoutes = this.editor.find('route');
|
|
|
|
|
allRoutes.forEach((r) => {
|
|
|
|
|
const [a1, a2] = r.anchors ?? [];
|
|
|
|
|
if (!a1?.connectTo || !a2?.connectTo) return;
|
|
|
|
|
const pA = this.editor.getPenById(a1.connectTo) as MapPen | undefined;
|
|
|
|
|
const pB = this.editor.getPenById(a2.connectTo) as MapPen | undefined;
|
|
|
|
|
if (!pA || !pB) return;
|
|
|
|
|
const ra = this.editor.getPointRect(pA);
|
|
|
|
|
const rb = this.editor.getPointRect(pB);
|
|
|
|
|
if (!ra || !rb) return;
|
|
|
|
|
const cax = (ra.x ?? 0) + (ra.width ?? 0) / 2;
|
|
|
|
|
const cay = (ra.y ?? 0) + (ra.height ?? 0) / 2;
|
|
|
|
|
const cbx = (rb.x ?? 0) + (rb.width ?? 0) / 2;
|
|
|
|
|
const cby = (rb.y ?? 0) + (rb.height ?? 0) / 2;
|
|
|
|
|
if (isInRect(cax, cay) && isInRect(cbx, cby)) {
|
|
|
|
|
routes.push(r.id!);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const areaInfo: MapAreaInfo = { type, points, routes };
|
|
|
|
|
if (type === MapAreaType.库区) {
|
|
|
|
|
areaInfo.inoutflag = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pen: MapPen = {
|
|
|
|
|
id,
|
|
|
|
|
name: 'area',
|
|
|
|
|
tags: ['area', `area-${type}`],
|
|
|
|
|
label: `A${id}`,
|
|
|
|
|
x: Math.min(p1.x, p2.x),
|
|
|
|
|
y: Math.min(p1.y, p2.y),
|
|
|
|
|
width: w,
|
|
|
|
|
height: h,
|
|
|
|
|
lineWidth: 1,
|
|
|
|
|
area: areaInfo,
|
|
|
|
|
locked: LockState.None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const area = await this.editor.addPen(pen, true, true, true);
|
|
|
|
|
this.editor.bottom(area);
|
|
|
|
|
|
|
|
|
|
if (type === MapAreaType.库区 && points.length > 0) {
|
|
|
|
|
this.autoStorageGenerator.triggerAutoCreateStorageDialog(pen, points);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateArea(id: string, info: Partial<MapAreaInfo>): void {
|
|
|
|
|
const { area } = this.editor.getPenById(id) ?? {};
|
|
|
|
|
if (!area?.type) return;
|
|
|
|
|
const mergedArea = { ...area, ...info };
|
|
|
|
|
this.editor.setValue({ id, area: mergedArea }, { render: true, history: true, doEvent: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|