xushiting 2 年 前
コミット
6e3dbf8477

+ 170 - 45
src/graphic/History/Change.js

@@ -3,6 +3,7 @@ import { roadService } from "../Service/RoadService";
 import { historyUtil } from "./HistoryUtil";
 import HistoryEvents from "../enum/HistoryEvents";
 import { coordinate } from "../Coordinate";
+import { mathUtil } from "../Util/MathUtil";
 
 export default class Change {
   constructor() {
@@ -19,19 +20,27 @@ export default class Change {
     this.lastData.lines = JSON.parse(JSON.stringify(dataService.getLines()));
     this.lastData.texts = JSON.parse(JSON.stringify(dataService.getTexts()));
     this.lastData.points = JSON.parse(JSON.stringify(dataService.getPoints()));
+    this.lastData.simpleLines = JSON.parse(
+      JSON.stringify(dataService.getSimpleLines())
+    );
+    this.lastData.circles = JSON.parse(JSON.stringify(dataService.getPoints()));
   }
 
   operate() {
     //
     this.currentData = {};
-
+    // this.compareRoads();
     this.comparePoints();
-    this.compareRoads();
+    this.compareLines();
+    this.compareSimpleLines();
+    this.compareCircles();
     this.compareTexts();
 
     if (
       this.currentData.points.length == 0 &&
-      this.currentData.roads.length == 0 &&
+      this.currentData.lines.length == 0 &&
+      this.currentData.simpleLines.length == 0 &&
+      this.currentData.circles.length == 0 &&
       this.currentData.texts.length == 0
     ) {
       this.saveCurrentInfo();
@@ -43,7 +52,7 @@ export default class Change {
   }
 
   comparePoints() {
-    const points = dataService.getRoadPoints();
+    const points = dataService.getPoints();
     this.currentData.points = [];
 
     for (const key in points) {
@@ -57,11 +66,7 @@ export default class Change {
         this.currentData.points.push(item);
       } else {
         const lastPoint = this.lastData.points[key];
-        if (
-          point.x == lastPoint.x &&
-          point.y == lastPoint.y &&
-          JSON.stringify(point.parent) == JSON.stringify(lastPoint.parent)
-        ) {
+        if (!historyUtil.isDifferentForPoints(point, lastPoint)) {
           delete this.lastData.points[key];
           continue;
         } else {
@@ -85,44 +90,44 @@ export default class Change {
     }
   }
 
-  compareRoads() {
-    this.currentData.roads = [];
-    const roads = dataService.getRoads();
-    for (const key in roads) {
-      const road = roads[key];
-      const lastRoad = this.lastData.roads[key];
+  // compareRoads() {
+  //   this.currentData.roads = [];
+  //   const roads = dataService.getRoads();
+  //   for (const key in roads) {
+  //     const road = roads[key];
+  //     const lastRoad = this.lastData.roads[key];
 
-      // 不存在意味着增加
-      if (!lastRoad) {
-        const item = {
-          handle: HistoryEvents.AddRoad,
-          road: historyUtil.getDataForRoad(road),
-        };
-        this.currentData.roads.push(item);
-      } else {
-        if (!historyUtil.isDifferentForRoads(road, lastRoad)) {
-          delete this.lastData.roads[key];
-          continue;
-        } else {
-          const item = {
-            handle: HistoryEvents.ModifyRoad,
-            preRoad: historyUtil.getDataForRoad(lastRoad),
-            curRoad: historyUtil.getDataForRoad(road),
-          };
-          this.currentData.roads.push(item);
-        }
-      }
-      delete this.lastData.roads[key];
-    }
+  //     // 不存在意味着增加
+  //     if (!lastRoad) {
+  //       const item = {
+  //         handle: HistoryEvents.AddRoad,
+  //         road: historyUtil.getDataForRoad(road),
+  //       };
+  //       this.currentData.roads.push(item);
+  //     } else {
+  //       if (!historyUtil.isDifferentForRoads(road, lastRoad)) {
+  //         delete this.lastData.roads[key];
+  //         continue;
+  //       } else {
+  //         const item = {
+  //           handle: HistoryEvents.ModifyRoad,
+  //           preRoad: historyUtil.getDataForRoad(lastRoad),
+  //           curRoad: historyUtil.getDataForRoad(road),
+  //         };
+  //         this.currentData.roads.push(item);
+  //       }
+  //     }
+  //     delete this.lastData.roads[key];
+  //   }
 
-    for (const key in this.lastData.roads) {
-      const item = {
-        handle: HistoryEvents.DeleteRoad,
-        road: historyUtil.getDataForRoad(this.lastData.roads[key]),
-      };
-      this.currentData.roads.push(item);
-    }
-  }
+  //   for (const key in this.lastData.roads) {
+  //     const item = {
+  //       handle: HistoryEvents.DeleteRoad,
+  //       road: historyUtil.getDataForRoad(this.lastData.roads[key]),
+  //     };
+  //     this.currentData.roads.push(item);
+  //   }
+  // }
 
   compareTexts() {
     this.currentData.texts = [];
@@ -163,6 +168,126 @@ export default class Change {
       this.currentData.texts.push(item);
     }
   }
+
+  compareLines() {
+    const lines = dataService.getLines();
+    this.currentData.lines = [];
+
+    for (const key in lines) {
+      const line = lines[key];
+      // 不存在意味着增加
+      if (!this.lastData.lines[key]) {
+        const item = {
+          handle: HistoryEvents.AddLine,
+          line: historyUtil.getDataForLine(line),
+        };
+        this.currentData.lines.push(item);
+      } else {
+        const lastLine = this.lastData.lines[key];
+        if (!historyUtil.isDifferentForLines(line, lastLine)) {
+          delete this.lastData.lines[key];
+          continue;
+        } else {
+          const item = {
+            handle: HistoryEvents.ModifyLine,
+            preLine: historyUtil.getDataForLine(lastLine),
+            curLine: historyUtil.getDataForLine(line),
+          };
+          this.currentData.lines.push(item);
+        }
+      }
+      delete this.lastData.lines[key];
+    }
+
+    for (const key in this.lastData.lines) {
+      const item = {
+        handle: HistoryEvents.DeleteLine,
+        line: historyUtil.getDataForLine(this.lastData.lines[key]),
+      };
+      this.currentData.lines.push(item);
+    }
+  }
+  compareSimpleLines() {
+    const simpleLines = dataService.getSimpleLines();
+    this.currentData.simpleLines = [];
+
+    for (const key in simpleLines) {
+      const simpleLine = simpleLines[key];
+      // 不存在意味着增加
+      if (!this.lastData.simpleLines[key]) {
+        const item = {
+          handle: HistoryEvents.AddSimpleLine,
+          simpleLine: historyUtil.getDataForSimpleLine(simpleLine),
+        };
+        this.currentData.simpleLines.push(item);
+      } else {
+        const lastSimpleLine = this.lastData.simpleLines[key];
+        if (
+          !historyUtil.isDifferentForSimpleLines(simpleLine, lastSimpleLine)
+        ) {
+          delete this.lastData.simpleLines[key];
+          continue;
+        } else {
+          const item = {
+            handle: HistoryEvents.ModifysSmpleLine,
+            preSimpleLine: historyUtil.getDataForSimpleLine(lastSimpleLine),
+            curSimpleLine: historyUtil.getDataForSimpleLine(simpleLine),
+          };
+          this.currentData.simpleLines.push(item);
+        }
+      }
+      delete this.lastData.simpleLines[key];
+    }
+
+    for (const key in this.lastData.simpleLines) {
+      const item = {
+        handle: HistoryEvents.DeleteSimpleLine,
+        simpleLine: historyUtil.getDataForSimpleLine(
+          this.lastData.simpleLines[key]
+        ),
+      };
+      this.currentData.simpleLines.push(item);
+    }
+  }
+
+  compareCircles() {
+    const circles = dataService.getCircles();
+    this.currentData.circles = [];
+
+    for (const key in circles) {
+      const circle = circles[key];
+      // 不存在意味着增加
+      if (!this.lastData.circles[key]) {
+        const item = {
+          handle: HistoryEvents.AddCircle,
+          circle: historyUtil.getDataForCircle(circle),
+        };
+        this.currentData.circles.push(item);
+      } else {
+        const lastCircle = this.lastData.circles[key];
+        if (!historyUtil.isDifferentForCircles(circle, lastCircle)) {
+          delete this.lastData.circles[key];
+          continue;
+        } else {
+          const item = {
+            handle: HistoryEvents.ModifyCircle,
+            preCircle: historyUtil.getDataForCircle(lastCircle),
+            curCircle: historyUtil.getDataForCircle(circle),
+          };
+          this.currentData.circles.push(item);
+        }
+      }
+      delete this.lastData.circles[key];
+    }
+
+    for (const key in this.lastData.circles) {
+      const item = {
+        handle: HistoryEvents.DeleteCircle,
+        circle: historyUtil.getDataForCircle(this.lastData.circles[key]),
+      };
+      this.currentData.circles.push(item);
+    }
+  }
 }
 
 const change = new Change();

+ 149 - 76
src/graphic/History/History.js

@@ -7,6 +7,8 @@ import { historyService } from "../Service/HistoryService";
 import { textService } from "../Service/TextService";
 import { roadService } from "../Service/RoadService";
 import { roadPointService } from "../Service/RoadPointService";
+import { lineService } from "../Service/LineService";
+import { circleService } from "../Service/CircleService";
 
 export default class History {
   constructor(layer) {
@@ -25,24 +27,11 @@ export default class History {
     historyService.addHistoryRecord(change.currentData);
     change.saveCurrentInfo();
     this.setState();
-    const historyState = historyService.getHistoryState();
-    // if (historyState.pre) {
-    //   this.layer.$xui.toolbar.recall = true;
-    // }
-    // this.layer.$xui.toolbar.recover = false;
-
-    const points = dataService.getRoadPoints();
-    // if (Object.keys(points).length > 0) {
-    //   this.layer.$xui.toolbar.clear = true;
-    //   this.layer.$xui.toolbar.download = true;
-    // } else {
-    //   this.layer.$xui.toolbar.clear = false;
-    //   this.layer.$xui.toolbar.download = false;
-    // }
-
-    //给UI发送事件
-    this.layer.emit("change");
-    return change.currentData;
+    // const historyState = historyService.getHistoryState();
+    // const points = dataService.getRoadPoints();
+    // //给UI发送事件
+    // this.layer.emit("change");
+    // return change.currentData;
   }
 
   setState() {
@@ -101,26 +90,17 @@ export default class History {
   goPreState() {
     const item = historyService.getHistoryRecord();
     if (item) {
-      stateService.clearFocusItem();
-      stateService.clearEventName();
-      //this.layer.uiControl.currentUI = null;
+      stateService.clear();
       item.type = "pre";
       this.goPreForPoints(item.points);
-      this.goPreForRoads(item.roads);
+      this.goPreForLines(item.lines);
+      this.goPreForSimpleLines(item.simpleLines);
+      this.goPreForCircles(item.circles);
       this.goPreForTexts(item.texts);
 
       historyService.undoHistoryRecord();
       change.saveCurrentInfo();
       this.setState();
-
-      // const points = dataService.getRoadPoints();
-      // if (Object.keys(points).length > 0) {
-      //   this.layer.$xui.toolbar.clear = true;
-      //   this.layer.$xui.toolbar.download = true;
-      // } else {
-      //   this.layer.$xui.toolbar.clear = false;
-      //   this.layer.$xui.toolbar.download = false;
-      // }
     } else {
       console.error("goPreState超出范围!");
     }
@@ -142,23 +122,75 @@ export default class History {
     }
   }
 
-  goPreForRoads(itemForRoads) {
-    for (let i = 0; i < itemForRoads.length; ++i) {
-      const item = itemForRoads[i];
-      if (item.handle == HistoryEvents.AddRoad) {
-        dataService.deleteRoad(item.road.id);
-      } else if (item.handle == HistoryEvents.DeleteRoad) {
-        const preRoad = item.road;
-        let newRoad = roadService.create(
-          preRoad.start,
-          preRoad.end,
-          preRoad.id
+  goPreForLines(itemForLines) {
+    for (let i = 0; i < itemForLines.length; ++i) {
+      const item = itemForLines[i];
+      if (item.handle == HistoryEvents.AddLine) {
+        dataService.deleteLine(item.line.id);
+      } else if (item.handle == HistoryEvents.DeleteLine) {
+        const preLine = item.line;
+        let newLine = lineService.createLine(
+          preLine.start,
+          preLine.end,
+          preLine.id
+        );
+        historyUtil.assignLineFromLine(newLine, preLine);
+      } else if (item.handle == HistoryEvents.ModifyLine) {
+        const preLine = item.preLine;
+        let currentLine = dataService.getLine(item.curLine.id);
+        historyUtil.assignLineFromLine(currentLine, preLine);
+      }
+    }
+  }
+
+  goPreForSimpleLines(itemForSimpleLines) {
+    for (let i = 0; i < itemForSimpleLines.length; ++i) {
+      const item = itemForSimpleLines[i];
+      if (item.handle == HistoryEvents.AddSimpleLine) {
+        dataService.deleteSimpleLine(item.simpleLine.id);
+      } else if (item.handle == HistoryEvents.DeleteSimpleLine) {
+        const preSimpleLine = item.simpleLine;
+        let newSimpleLine = lineService.createSimpleLine(
+          preSimpleLine.start,
+          preSimpleLine.end,
+          preSimpleLine.category,
+          preSimpleLine.id
+        );
+        historyUtil.assignSimpleLineFromSimpleLine(
+          newSimpleLine,
+          preSimpleLine
+        );
+      } else if (item.handle == HistoryEvents.ModifySimpleLine) {
+        const preSimpleLine = item.preSimpleLine;
+        let currentSimpleLine = dataService.getSimpleLine(
+          item.curSimpleLine.id
+        );
+        historyUtil.assignSimpleLineFromSimpleLine(
+          currentSimpleLine,
+          preSimpleLine
+        );
+      }
+    }
+  }
+
+  goPreForCircles(itemForCircles) {
+    for (let i = 0; i < itemForCircles.length; ++i) {
+      const item = itemForCircles[i];
+      if (item.handle == HistoryEvents.AddCircle) {
+        dataService.deleteCircle(item.circle.id);
+      } else if (item.handle == HistoryEvents.DeleteCircle) {
+        const preCircle = item.circle;
+        let newCircle = lineService.createCircle(
+          preCircle.start,
+          preCircle.end,
+          preCircle.category,
+          preCircle.id
         );
-        historyUtil.assignRoadFromRoad(newRoad, preRoad);
-      } else if (item.handle == HistoryEvents.ModifyRoad) {
-        const preRoad = item.preRoad;
-        let currentRoad = dataService.getRoad(item.curRoad.id);
-        historyUtil.assignRoadFromRoad(currentRoad, preRoad);
+        historyUtil.assignCircleFromCircle(newCircle, preCircle);
+      } else if (item.handle == HistoryEvents.ModifyCircle) {
+        const preCircle = item.preCircle;
+        let currentCircle = dataService.getCircle(item.curCircle.id);
+        historyUtil.assignCircleFromCircle(currentCircle, preCircle);
       }
     }
   }
@@ -195,23 +227,72 @@ export default class History {
     }
   }
 
-  goNextForRoads(itemForRoads) {
-    for (let i = 0; i < itemForRoads.length; ++i) {
-      const item = itemForRoads[i];
-      if (item.handle == HistoryEvents.AddRoad) {
-        const preRoad = item.road;
-        let newRoad = roadService.create(
-          preRoad.start,
-          preRoad.end,
-          preRoad.id
+  goNextForLines(itemForLines) {
+    for (let i = 0; i < itemForLines.length; ++i) {
+      const item = itemForLines[i];
+      if (item.handle == HistoryEvents.AddLine) {
+        const preLine = item.line;
+        let newLine = lineService.createLine(
+          preLine.start,
+          preLine.end,
+          preLine.id
+        );
+        historyUtil.assignLineFromLine(newLine, preLine);
+      } else if (item.handle == HistoryEvents.DeleteLine) {
+        dataService.deleteLine(item.line.id);
+      } else if (item.handle == HistoryEvents.ModifyLine) {
+        const currentLine = item.curLine;
+        let preLine = dataService.getLine(item.preLine.id);
+        historyUtil.assignLineFromLine(preLine, currentLine);
+      }
+    }
+  }
+
+  goNextForSimpleLines(itemForSimpleLines) {
+    for (let i = 0; i < itemForSimpleLines.length; ++i) {
+      const item = itemForSimpleLines[i];
+      if (item.handle == HistoryEvents.AddSimpleLine) {
+        const preSimpleLine = item.simpleLine;
+        let newSimpleLine = lineService.createSimpleLine(
+          preSimpleLine.start,
+          preSimpleLine.end,
+          preSimpleLine.category,
+          preSimpleLine.id
+        );
+        historyUtil.assignSimpleLineFromSimpleLine(
+          newSimpleLine,
+          preSimpleLine
+        );
+      } else if (item.handle == HistoryEvents.DeleteSimpleLine) {
+        dataService.deleteSimpleLine(item.simpleLine.id);
+      } else if (item.handle == HistoryEvents.ModifySimpleLine) {
+        const currentSimpleLine = item.curSimpleLine;
+        let preSimpleLine = dataService.getSimpleLine(item.preSimpleLine.id);
+        historyUtil.assignSimpleLineFromSimpleLine(
+          preSimpleLine,
+          currentSimpleLine
         );
-        historyUtil.assignRoadFromRoad(newRoad, preRoad);
-      } else if (item.handle == HistoryEvents.DeleteRoad) {
-        dataService.deleteRoad(item.road.id);
-      } else if (item.handle == HistoryEvents.ModifyRoad) {
-        const currentRoad = item.curRoad;
-        let preRoad = dataService.getRoad(item.preRoad.id);
-        historyUtil.assignRoadFromRoad(preRoad, currentRoad);
+      }
+    }
+  }
+
+  goNextForCircles(itemForCircles) {
+    for (let i = 0; i < itemForCircles.length; ++i) {
+      const item = itemForCircles[i];
+      if (item.handle == HistoryEvents.AddCircle) {
+        const preCircle = item.circle;
+        let newCircle = circleService.create(
+          preCircle.center,
+          preCircle.radius,
+          preCircle.id
+        );
+        historyUtil.assignCircleFromCircle(newCircle, preCircle);
+      } else if (item.handle == HistoryEvents.DeleteCircle) {
+        dataService.deleteCircle(item.circle.id);
+      } else if (item.handle == HistoryEvents.ModifyCircle) {
+        const currentCircle = item.curCircle;
+        let preCircle = dataService.getCircle(item.preCircle.id);
+        historyUtil.assignCircleFromCircle(preCircle, currentCircle);
       }
     }
   }
@@ -237,23 +318,15 @@ export default class History {
     historyService.redoHistoryRecord();
     const item = historyService.getHistoryRecord();
     if (item) {
-      stateService.clearFocusItem();
-      stateService.clearEventName();
-      //this.layer.uiControl.currentUI = null;
+      stateService.clear();
+
       this.goNextForPoints(item.points);
-      this.goNextForRoads(item.roads);
+      this.goNextForLines(item.lines);
+      this.goNextForSimpleLines(item.simpleLines);
+      this.goNextForCircles(item.circles);
       this.goNextForTexts(item.texts);
       change.saveCurrentInfo();
       this.setState();
-
-      // const points = dataService.getRoadPoints();
-      // if (Object.keys(points).length > 0) {
-      //   this.layer.$xui.toolbar.clear = true;
-      //   this.layer.$xui.toolbar.download = true;
-      // } else {
-      //   this.layer.$xui.toolbar.clear = false;
-      //   this.layer.$xui.toolbar.download = false;
-      // }
     } else {
       historyService.undoHistoryRecord();
       console.error("goNextState超出范围!");

+ 42 - 0
src/graphic/History/HistoryUtil.js

@@ -13,6 +13,48 @@ export default class HistoryUtil {
     }
   }
 
+  isDifferentForPoints(point1, point2) {
+    if (
+      point1.x == point2.x &&
+      point1.y == point2.y &&
+      JSON.stringify(point1.parent) == JSON.stringify(point2.parent)
+    ) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  isDifferentForLines(line1, line2) {
+    if (line1.startId == line2.startId && line1.endId == line2.endId) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  isDifferentForSimpleLines(simpleLine1, simpleLine2) {
+    if (
+      mathUtil.equalPoint(simpleLine1.start, simpleLine2.start) &&
+      mathUtil.equalPoint(simpleLine1.end, simpleLine2.end)
+    ) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  isDifferentForCircles(circle1, circle2) {
+    if (
+      mathUtil.equalPoint(circle1.center, circle2.center) &&
+      circle1.radius == circle2.radius
+    ) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
   isDifferentForTexts(text1, text2) {
     if (
       mathUtil.equalPoint(text1.center, text2.center) &&

+ 31 - 0
src/graphic/Service/DataService.js

@@ -52,6 +52,7 @@ export class DataService {
     this.vectorData.curvelines = {};
     //线段(完全或者直线)上的端点
     this.vectorData.points = {};
+    this.vectorData.circles = {};
     //基准点
     this.vectorData.basePointIds = [];
     this.vectorData.texts = {};
@@ -115,6 +116,10 @@ export class DataService {
     return this.vectorData.lines[lineId];
   }
 
+  deleteLine(lineId) {
+    delete this.vectorData.lines[lineId];
+  }
+
   getSimpleLines() {
     return this.vectorData.simpleLines;
   }
@@ -127,6 +132,11 @@ export class DataService {
     this.vectorData.simpleLines[simpleLine.vectorId] = simpleLine;
   }
 
+  deleteSimpleLine(simpleLineId) {
+    delete this.vectorData.simpleLines[simpleLineId];
+  }
+
+  //直线的端点
   getPoints() {
     return this.vectorData.points;
   }
@@ -139,6 +149,27 @@ export class DataService {
     this.vectorData.points[point.vectorId] = point;
   }
 
+  deletePoint(pointId) {
+    delete this.vectorData.points[pointId];
+  }
+
+  //圆圈
+  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;

+ 16 - 0
src/graphic/enum/HistoryEvents.js

@@ -10,5 +10,21 @@ const HistoryEvents = {
   AddText: "addText",
   DeleteText: "deleteText",
   ModifyText: "modifyText",
+
+  AddPoint: "addPoint",
+  DeletePoint: "deletePoint",
+  ModifyPoint: "modifyPoint",
+
+  AddLine: "addLine",
+  DeleteLine: "deleteLine",
+  ModifyLine: "modifyLine",
+
+  AddSimpleLine: "addSimpleLine",
+  DeleteSimpleLine: "deleteSimpleLine",
+  ModifySimpleLine: "modifySimpleLine",
+
+  AddCircle: "addCircle",
+  DeleteCirclee: "deleteCircle",
+  ModifyCircle: "modifyCircle",
 };
 export default HistoryEvents;