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