import Line from "../Geometry/Line.js"; import CurveLine from "../Geometry/CurveLine.js"; export class DataService { constructor() { this.grid = { startX: 0, startY: 0, step1: 50, step2: 250, defalutstep1: 50, defalutstep2: 250, display: true, }; this.vectorData = {}; this.currentId = 0; // 当前可用id } setCurrentId(id) { this.currentId = id; } getCurrentId() { return this.currentId; } updateCurrentId() { ++this.currentId; } getVectorData() { return this.vectorData; } initVectorData() { this.vectorData.backgroundImg = null; //直路 this.vectorData.roadPoints = {}; this.vectorData.roads = {}; this.vectorData.roadEdges = {}; //弯路 this.vectorData.curveRoadPoints = {}; this.vectorData.curveRoads = {}; this.vectorData.curveRoadEdges = {}; //直路交叉的时候,产生的曲线控制点 this.vectorData.controlPoints = {}; //直线,起点和终点都是point的id this.vectorData.lines = {}; //弯曲线条 this.vectorData.curvelines = {}; //线段(完全或者直线)上的端点 this.vectorData.points = {}; this.vectorData.circles = {}; //基准点 this.vectorData.basePointIds = []; this.vectorData.texts = {}; } //网格 getGrid() { return this.grid; } setGridForPan(dx, dy) { this.grid.startX += dx; this.grid.startY += dy; } setGridForZoom(w, h, ratio) { console.log("setGridForZoom:" + ratio); this.grid.startX = w / 2 - (ratio * w) / 2; this.grid.startY = w / 2 - (ratio * h) / 2; this.grid.step1 = this.grid.defalutstep1 * ratio; this.grid.step2 = this.grid.defalutstep2 * ratio; while (this.grid.startX > 0) { this.grid.startX -= this.grid.step1; } while (this.grid.startY > 0) { this.grid.startY -= this.grid.step2; } } setGridDisplay(value) { this.grid.display = value; } getGridDisplay() { return this.grid.display; } //背景图片 addBackgroundImg(img) { this.vectorData.backgroundImg = img; } getBackgroundImg() { return this.vectorData.backgroundImg; } setAngle(angle) { this.vectorData.backgroundImg.angle = angle; } //直线 addLine(line) { this.vectorData.lines[line.vectorId] = line; } getLines() { return this.vectorData.lines; } getLine(lineId) { return this.vectorData.lines[lineId]; } deleteLine(lineId) { delete this.vectorData.lines[lineId]; } //直线的端点 getPoints() { return this.vectorData.points; } getPoint(pointId) { return this.vectorData.points[pointId]; } addPoint(point) { this.vectorData.points[point.vectorId] = point; } // deletePoint(pointId) { // delete this.vectorData.points[pointId]; // } deletePoint(pointId, lineId) { let point = this.getPoint(pointId); //有可能先删除墙,导致点没了 if (point) { if (Object.keys(point.parent).length == 0) { point = null; delete this.vectorData.points[pointId]; } else if (Object.keys(point.parent).length == 1 && !lineId) { delete this.vectorData.points[pointId]; } else if ( Object.keys(point.parent).length == 1 && point.parent[lineId] ) { delete this.vectorData.points[pointId]; } else if ( Object.keys(point.parent).length == 1 && !point.parent[lineId] ) { return; } else { delete point.parent[lineId]; } } } deletePoint(pointId) { const point = dataService.getRoadPoint(pointId); const parent = point.parent; for (const key in parent) { dataService.deleteRoadPoint(pointId, key); } } //圆圈 getCircles() { return this.vectorData.circles; } getCircle(circleId) { return this.vectorData.circles[circleId]; } addCircle(circle) { this.vectorData.circles[circle.vectorId] = circle; } deleteCircle(circleId) { delete this.vectorData.circles[circleId]; } //弯曲线条 addCurveLine(curveLine) { this.vectorData.curvelines[curveLine.vectorId] = curveLine; } addCurvePoint(curvePoint) { this.vectorData.curvePoints[curvePoint.vectorId] = curvePoint; } getCurvePoints() { return this.vectorData.curvePoints; } getCurvePoint(curvePointId) { if (curvePointId) { return this.vectorData.curvePoints[curvePointId]; } else { return null; } } deleteCurvePoint(curvePointId) { delete this.vectorData.curvePoints[curvePointId]; } getCurveLines() { return this.vectorData.curvelines; } getCurveLine(curveLineId) { if (curveLineId) { return this.vectorData.curvelines[curveLineId]; } else { return null; } } /** * 对弯路的操作 */ addCurveRoadPoint(curveRoadPoint) { this.vectorData.curveRoadPoints[curveRoadPoint.vectorId] = curveRoadPoint; } getCurveRoadPoints() { return this.vectorData.curveRoadPoints; } getCurveRoadPoint(curveRoadPointId) { if (curveRoadPointId) { return this.vectorData.curveRoadPoints[curveRoadPointId]; } else { return null; } } deleteCurveRoadPoint(curveRoadPointId) { delete this.vectorData.curveRoadPoints[curveRoadPointId]; } addCurveRoad(curveRoad) { this.vectorData.curveRoads[curveRoad.vectorId] = curveRoad; } getCurveRoads() { return this.vectorData.curveRoads; } getCurveRoad(curveRoadId) { if (curveRoadId) { return this.vectorData.curveRoads[curveRoadId]; } else { return null; } } deleteCurveRoad(curveRoadId) { let curveRoad = this.getCurveRoad(curveRoadId); for (let i = 0; i < curveRoad.points.length; ++i) { this.deleteCurveRoadPoint(curveRoad.points[i].vectorId); } this.deleteCurveRoadEdge(curveRoad.leftEdgeId); this.deleteCurveRoadEdge(curveRoad.rightEdgeId); delete this.vectorData.curveRoads[curveRoadId]; } getCurveRoadEdge(curveRoadEdgeId) { return this.vectorData.curveRoadEdges[curveRoadEdgeId]; } addCurveRoadEdge(curveRoadEdge) { this.vectorData.curveRoadEdges[curveRoadEdge.vectorId] = curveRoadEdge; } deleteCurveRoadEdge(curveRoadEdgeId) { delete this.vectorData.curveRoadEdges[curveRoadEdgeId]; } //直线 getRoads() { return this.vectorData.roads; } getRoad(roadId) { if (roadId) { return this.vectorData.roads[roadId]; } else { return null; } } addRoad(road) { this.vectorData.roads[road.vectorId] = road; } deleteRoad(roadId) { let road = this.getRoad(roadId); this.deleteRoadPoint(road.startId, roadId); this.deleteRoadPoint(road.endId, roadId); this.deleteRoadEdge(road.leftEdgeId); this.deleteRoadEdge(road.rightEdgeId); delete this.vectorData.roads[roadId]; } deleteRoadEdge(edgeId) { delete this.vectorData.roadEdges[edgeId]; } /** * 对端点的操作 */ getRoadPoints() { return this.vectorData.roadPoints; } getRoadPoint(roadPointId) { if (roadPointId) { return this.vectorData.roadPoints[roadPointId]; } else { return null; } } deleteRoadPoint(roadPointId, roadId) { let roadPoint = this.getRoadPoint(roadPointId); //有可能先删除墙,导致点没了 if (roadPoint) { if (Object.keys(roadPoint.parent).length == 0) { roadPoint = null; delete this.vectorData.roadPoints[roadPointId]; } else if (Object.keys(roadPoint.parent).length == 1 && !roadId) { delete this.vectorData.roadPoints[roadPointId]; } else if ( Object.keys(roadPoint.parent).length == 1 && roadPoint.parent[roadId] ) { delete this.vectorData.roadPoints[roadPointId]; } else if ( Object.keys(roadPoint.parent).length == 1 && !roadPoint.parent[roadId] ) { return; } else { delete roadPoint.parent[roadId]; } } } addRoadPoint(roadPoint) { this.vectorData.roadPoints[roadPoint.vectorId] = roadPoint; } getRoadEdge(roadEdgeId) { return this.vectorData.roadEdges[roadEdgeId]; } addRoadEdge(roadEdge) { this.vectorData.roadEdges[roadEdge.vectorId] = roadEdge; } /** * 对控制点的操作 */ getControlPoints() { return this.vectorData.controlPoints; } getControlPoint(edgeId1, edgeId2) { if (this.vectorData.controlPoints[edgeId1 + "-" + edgeId2]) { return this.vectorData.controlPoints[edgeId1 + "-" + edgeId2]; } else if (this.vectorData.controlPoints[edgeId2 + "-" + edgeId1]) { return this.vectorData.controlPoints[edgeId2 + "-" + edgeId1]; } else { return null; } } getControlPoint2(key) { return this.vectorData.controlPoints[key]; } getControlPointForEdgeId(edgeId, dir) { for (let key in this.vectorData.controlPoints) { const controlPoint = this.vectorData.controlPoints[key]; if ( controlPoint.edgeInfo1.id == edgeId && controlPoint.edgeInfo1.dir == dir ) { return this.vectorData.controlPoints[key]; } else if ( controlPoint.edgeInfo2.id == edgeId && controlPoint.edgeInfo2.dir == dir ) { return this.vectorData.controlPoints[key]; } } } addControlPoint(controlPoint) { this.vectorData.controlPoints[ controlPoint.edgeInfo1.id + "-" + controlPoint.edgeInfo2.id ] = controlPoint; } deleteControlPoint(edgeId1, edgeId2) { let key = edgeId1 + "-" + edgeId2; if (!this.vectorData.controlPoints[key]) { key = edgeId2 + "-" + edgeId1; } if (this.vectorData.controlPoints[key]) { delete this.vectorData.controlPoints[key]; } } deleteControlPointForEdge(edgeId, dir) { let dControlPointIds = []; for (let key in this.vectorData.controlPoints) { const controlPoint = this.vectorData.controlPoints[key]; if ( controlPoint.edgeInfo1.id == edgeId || controlPoint.edgeInfo2.id == edgeId ) { dControlPointIds.push(key); } } for (let i = 0; i < dControlPointIds.length; ++i) { const controlPoint = this.vectorData.controlPoints[dControlPointIds[i]]; if ( controlPoint.edgeInfo1.id == edgeId && controlPoint.edgeInfo1.dir == dir ) { delete this.vectorData.controlPoints[dControlPointIds[i]]; } else if ( controlPoint.edgeInfo2.id == edgeId && controlPoint.edgeInfo2.dir == dir ) { delete this.vectorData.controlPoints[dControlPointIds[i]]; } } } /** * 对标注的操作 */ addText(text) { this.vectorData.texts[text.vectorId] = text; } getText(textId) { return this.vectorData.texts[textId]; } deleteText(textId) { delete this.vectorData.texts[textId]; } getTexts() { return this.vectorData.texts; } clear() { this.vectorData = {}; } } const dataService = new DataService(); export { dataService };