feat: 引入图层管理服务以优化层级顺序管理,并更新相关方法以确保元素正确显示

This commit is contained in:
xudan 2025-08-15 15:44:42 +08:00
parent f3b993d31d
commit 3f1c44304c
2 changed files with 170 additions and 11 deletions

View File

@ -29,6 +29,8 @@ import { clone, get, isEmpty, isNil, isString, nth, omitBy, pick, remove, some }
import { BehaviorSubject, debounceTime, filter, map, Subject, switchMap } from 'rxjs'; import { BehaviorSubject, debounceTime, filter, map, Subject, switchMap } from 'rxjs';
import { reactive, watch } from 'vue'; import { reactive, watch } from 'vue';
import { LayerManagerService } from './layer-manager.service';
/** /**
* *
* Meta2D * Meta2D
@ -41,6 +43,9 @@ import { reactive, watch } from 'vue';
* - * -
*/ */
export class EditorService extends Meta2d { export class EditorService extends Meta2d {
/** 图层管理服务实例 */
private readonly layerManager: LayerManagerService;
//#region 场景文件管理 //#region 场景文件管理
/** /**
* *
@ -71,8 +76,8 @@ export class EditorService extends Meta2d {
this.#loadSceneRoutes(routes, isImport); this.#loadSceneRoutes(routes, isImport);
await this.#loadSceneAreas(areas, isImport); await this.#loadSceneAreas(areas, isImport);
// 确保加载的点位在路线上方 // 确保正确的层级顺序:路线 < 点位 < 机器人
this.#ensurePointsOnTop(); this.#ensureCorrectLayerOrder();
this.store.historyIndex = undefined; this.store.historyIndex = undefined;
this.store.histories = []; this.store.histories = [];
@ -502,14 +507,11 @@ export class EditorService extends Meta2d {
} }
/** /**
* 线 *
* *
*/ */
#ensurePointsOnTop(): void { #ensureCorrectLayerOrder(): void {
const points = this.find('point'); this.layerManager.ensureCorrectLayerOrder();
if (points.length > 0) {
this.top(points);
}
} }
//#endregion //#endregion
@ -984,8 +986,8 @@ export class EditorService extends Meta2d {
pen.y! -= pen.height! / 2; pen.y! -= pen.height! / 2;
const addedPen = await this.addPen(pen, false, true, true); const addedPen = await this.addPen(pen, false, true, true);
// 将点位移到最上层,确保它们始终显示在路线之上 // 将新创建的点位移到最上层
this.top([addedPen]); this.layerManager.adjustElementLayer(addedPen);
} }
public updatePoint(id: string, info: Partial<MapPointInfo>): void { public updatePoint(id: string, info: Partial<MapPointInfo>): void {
@ -1195,6 +1197,9 @@ export class EditorService extends Meta2d {
constructor(container: HTMLDivElement) { constructor(container: HTMLDivElement) {
super(container, EDITOR_CONFIG); super(container, EDITOR_CONFIG);
// 初始化图层管理服务
this.layerManager = new LayerManagerService(this);
// 禁用第6个子元素的拖放功能 // 禁用第6个子元素的拖放功能
(<HTMLDivElement>container.children.item(5)).ondrop = null; (<HTMLDivElement>container.children.item(5)).ondrop = null;
// 监听所有画布事件 // 监听所有画布事件

View File

@ -0,0 +1,154 @@
/* eslint-disable indent */
import type { MapPen } from '@api/map';
import type { Meta2d } from '@meta2d/core';
/**
*
*
*
*
* 1. area-
* 2. 线route-
* 3. pointrobot-
*/
export class LayerManagerService {
constructor(private editor: Meta2d) {}
/**
*
*
* -
* -
* -
*/
public ensureCorrectLayerOrder(): void {
const areas = this.editor.find('area');
const routes = this.editor.find('route');
const points = this.editor.find('point');
const robots = this.editor.find('robot');
// 将区域移到最底层(作为背景)
if (areas.length > 0) {
this.editor.bottom(areas);
}
// 将路线移到中间层(在区域之上,但在点位之下)
if (routes.length > 0) {
this.editor.top(routes);
}
// 将点位和机器人都移到最上层(在路线之上)
const topElements = [...points, ...robots];
if (topElements.length > 0) {
this.editor.top(topElements);
}
}
/**
*
*
* @param points
*/
public movePointsToTop(points: MapPen[]): void {
if (points.length > 0) {
this.editor.top(points);
// 确保机器人仍然与点位在同一层级
this.ensureRobotsAtTop();
}
}
/**
* 线
* 线
* @param routes 线
*/
public moveRoutesToMiddle(routes: MapPen[]): void {
if (routes.length > 0) {
// 先移到顶层,然后通过完整的层级管理确保正确顺序
this.editor.top(routes);
this.ensureCorrectLayerOrder();
}
}
/**
*
*
* @param areas
*/
public moveAreasToBottom(areas: MapPen[]): void {
if (areas.length > 0) {
this.editor.bottom(areas);
}
}
/**
*
*
*/
public ensureRobotsAtTop(): void {
const robots = this.editor.find('robot');
if (robots.length > 0) {
this.editor.top(robots);
}
}
/**
*
*
* @param pen
*/
public adjustElementLayer(pen: MapPen): void {
switch (pen.name) {
case 'area':
this.moveAreasToBottom([pen]);
break;
case 'route':
this.moveRoutesToMiddle([pen]);
break;
case 'point':
this.movePointsToTop([pen]);
break;
case 'robot':
this.ensureRobotsAtTop();
break;
default:
// 对于未知类型,使用完整的层级管理
this.ensureCorrectLayerOrder();
break;
}
}
/**
*
*
*
*/
public resetAllLayers(): void {
this.ensureCorrectLayerOrder();
}
/**
*
* @returns
*/
public getLayerInfo(): {
areas: number;
routes: number;
points: number;
robots: number;
total: number;
} {
const areas = this.editor.find('area');
const routes = this.editor.find('route');
const points = this.editor.find('point');
const robots = this.editor.find('robot');
return {
areas: areas.length,
routes: routes.length,
points: points.length,
robots: robots.length,
total: areas.length + routes.length + points.length + robots.length,
};
}
}