This commit is contained in:
chndfang 2025-05-17 16:56:01 +08:00
parent 7da2ac2f2a
commit e3edde1b16
3 changed files with 46 additions and 28 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,4 @@
import {
type AnchorPosition,
EDITOR_CONFIG,
type MapAreaInfo,
MapAreaType,
@ -30,13 +29,18 @@ import { reactive, watch } from 'vue';
export class EditorService extends Meta2d {
//#region 场景文件
public async load(map?: string, editable = false, detail?: Partial<GroupSceneDetail>): Promise<void> {
let data = map ? JSON.parse(map) : undefined;
if (!isNil(detail)) {
data ??= this.data();
data.robotGroups = [detail.group];
data.robots = detail.robots;
const scene: StandardScene = map ? JSON.parse(map) : {};
if (!isEmpty(detail?.group)) {
scene.robotGroups = [detail.group];
scene.robots = detail.robots;
}
this.open(data);
const { robotGroups, robots, points, routes, areas } = scene;
this.open(undefined, false);
await this.#loadScenePoints(points);
const data = this.data();
data.robotGroups = robotGroups;
data.robots = robots;
this.open(data, true);
this.setState(editable);
}
public save(): string {
@ -51,10 +55,24 @@ export class EditorService extends Meta2d {
return JSON.stringify(scene);
}
async #loadScenePoints(points?: StandardScenePoint[]): Promise<void> {
if (!points?.length) return;
await Promise.all(
points.map(async (v) => {
const { id, name, desc, x, y, type, robots, actions, properties } = v;
await this.addPoint({ x, y }, type, id);
this.setValue(
{ id, label: name, desc, properties, point: { type, robots, actions } },
{ render: false, history: false, doEvent: false },
);
}),
);
}
#mapScenePoint(pen?: MapPen): StandardScenePoint | null {
if (!pen?.id || isEmpty(pen?.point)) return null;
const { id, x = 0, y = 0, label, desc, properties } = pen;
const { id, label, desc, properties } = pen;
const { type, robots, actions } = pen.point;
const { x, y } = this.getPenRect(pen);
const point: StandardScenePoint = {
id: id,
name: label || id,
@ -103,8 +121,9 @@ export class EditorService extends Meta2d {
}
#mapSceneArea(pen: MapPen): StandardSceneArea | null {
if (!pen.id || isEmpty(pen.area)) return null;
const { id, x = 0, y = 0, width = 0, height = 0, label, desc, properties } = pen;
const { id, label, desc, properties } = pen;
const { type, points, routes } = pen.area;
const { x, y, width, height } = this.getPenRect(pen);
const area: StandardSceneArea = {
id,
name: label || id,
@ -298,8 +317,8 @@ export class EditorService extends Meta2d {
{ initialValue: new Array<MapPen>() },
);
public async addPoint(p: Point, type = MapPointType.): Promise<void> {
const id = s8();
public async addPoint(p: Point, type = MapPointType., id?: string): Promise<MapPen> {
id ||= s8();
const pen: MapPen = {
...p,
...this.#mapPoint(type),
@ -310,8 +329,7 @@ export class EditorService extends Meta2d {
label: `P${id}`,
point: { type },
};
await this.addPen(pen, false, true, true);
// this.pushHistory({ type: EditType.Add, pens: [cloneDeep(pen)] });
return await this.addPen(pen, false, true, true);
}
public updatePoint(id: string, info: Partial<MapPointInfo>): void {
@ -364,17 +382,16 @@ export class EditorService extends Meta2d {
return `${p1.label}${(d ?? direction) > 0 ? '→' : '←'}${p2.label}`;
}
public addRoute(p: [string, string], type = MapRouteType.线, from?: AnchorPosition, to?: AnchorPosition): void {
public addRoute(p: [string, string], type = MapRouteType.线, id?: string): void {
const [p1, p2] = p.map((v) => this.getPenById(v));
if (!p1?.anchors?.length || !p2?.anchors?.length) return;
const a1 = p1.anchors.find(({ id }) => id === from);
const a2 = p2.anchors.find(({ id }) => id === to);
const line = this.connectLine(p1, p2, a1, a2, false);
const line = this.connectLine(p1, p2, undefined, undefined, false);
id ||= line.id!;
this.changePenId(line.id!, id);
const pen: MapPen = { tags: ['route'], route: { type }, lineWidth: 2, iconSize: 10 };
this.setValue({ id: line.id, ...pen }, { render: false, history: false, doEvent: false });
this.setValue({ id, ...pen }, { render: false, history: false, doEvent: false });
this.updateLineType(line, type);
// this.pushHistory({ type: EditType.Add, pens: [cloneDeep(line)] });
this.active([line]);
this.active(id);
this.render();
}
@ -411,13 +428,13 @@ export class EditorService extends Meta2d {
});
}
public async addArea(p1: Point, p2: Point, type = MapAreaType.) {
public async addArea(p1: Point, p2: Point, type = MapAreaType., id?: string) {
const scale = this.data().scale ?? 1;
const w = Math.abs(p1.x - p2.x);
const h = Math.abs(p1.y - p2.y);
if (w * scale < 50 || h * scale < 60) return;
const selected = <MapPen[]>this.store.active;
const id = s8();
id ||= s8();
const points = new Array<string>();
const routes = new Array<string>();
switch (type) {
@ -618,10 +635,11 @@ function drawPoint(ctx: CanvasRenderingContext2D, pen: MapPen): void {
}
function anchorPoint(pen: MapPen): void {
pen.anchors = [
{ penId: pen.id, id: 't', x: 0.5, y: 0 },
{ penId: pen.id, id: 'b', x: 0.5, y: 1 },
{ penId: pen.id, id: 'l', x: 0, y: 0.5 },
{ penId: pen.id, id: 'r', x: 1, y: 0.5 },
{ penId: pen.id, id: '0', x: 0.5, y: 0.5 },
// { penId: pen.id, id: 't', x: 0.5, y: 0 },
// { penId: pen.id, id: 'b', x: 0.5, y: 1 },
// { penId: pen.id, id: 'l', x: 0, y: 0.5 },
// { penId: pen.id, id: 'r', x: 1, y: 0.5 },
];
}