123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- import Point from "../Geometry/Point.js";
- import Line from "../Geometry/Line.js";
- import Road from "../Geometry/Road.js";
- import RoadEdge from "../Geometry/RoadEdge.js";
- import ElementEvents from "../enum/ElementEvents.js";
- import VectorCategory from "../enum/VectorCategory.js";
- import { listenLayer } from "../ListenLayer";
- import Constant from "../Constant";
- import { dataService } from "./DataService.js";
- import { mathUtil } from "../Util/MathUtil";
- import { coordinate } from "../Coordinate.js";
- export class ElementService {
- constructor() {
- this.point = null;
- this.newLine = null;
- this.newLineStart = null;
- this.newLineEnd = null;
- this.newRoad = null;
- this.checkLinesXStart = null;
- this.checkLinesXEnd = null;
- this.checkLinesYStart = null;
- this.checkLinesYEnd = null;
- this.checkLines = {
- X: null,
- Y: null,
- };
- this.vCheckLinesXStart = null;
- this.vCheckLinesXEnd = null;
- this.vCheckLinesYStart = null;
- this.vCheckLinesYEnd = null;
- this.vCheckLines = {
- X: null,
- Y: null,
- };
- this.init();
- }
- init() {
- this.point = new Point({ x: 0, y: 0 });
- this.point.name = ElementEvents.AddingPoint;
- this.newRoad = this.createTempRoad();
- this.newRoad.name = ElementEvents.NewRoad;
- this.newLineStart = new Point({ x: 0, y: 0 });
- this.newLineEnd = new Point({ x: 1, y: 1 });
- this.newLine = new Line(this.newLineStart, this.newLineEnd);
- this.newLine.setCategory(VectorCategory.Line.NormalLine);
- this.newLine.name = ElementEvents.NewLine;
- this.checkLinesXStart = new Point({ x: 0, y: 0 });
- this.checkLinesXEnd = new Point({ x: 1, y: 1 });
- this.checkLines.X = new Line(this.checkLinesXStart, this.checkLinesXEnd);
- this.newLine.setCategory(VectorCategory.Line.GuideLine);
- this.checkLines.X.name = ElementEvents.CheckLinesX;
- this.checkLinesYStart = new Point({ x: 0, y: 0 });
- this.checkLinesYEnd = new Point({ x: 1, y: 1 });
- this.checkLines.Y = new Line(this.checkLinesYStart, this.checkLinesYEnd);
- this.newLine.setCategory(VectorCategory.Line.GuideLine);
- this.checkLines.Y.name = ElementEvents.CheckLinesY;
- this.vCheckLinesXStart = new Point({ x: 0, y: 0 });
- this.vCheckLinesXEnd = new Point({ x: 1, y: 1 });
- this.vCheckLines.X = new Line(this.vCheckLinesXStart, this.vCheckLinesXEnd);
- this.newLine.setCategory(VectorCategory.Line.GuideLine);
- this.vCheckLines.X.name = ElementEvents.VCheckLinesX;
- this.vCheckLinesYStart = new Point({ x: 0, y: 0 });
- this.vCheckLinesYEnd = new Point({ x: 1, y: 1 });
- this.vCheckLines.Y = new Line(this.vCheckLinesYStart, this.vCheckLinesYEnd);
- this.newLine.setCategory(VectorCategory.Line.GuideLine);
- this.vCheckLines.Y.name = ElementEvents.VCheckLinesY;
- this.hideAll();
- }
- //临时的
- createTempRoad() {
- let p1 = new Point({ x: 0, y: 0 });
- let p2 = new Point({ x: 1, y: 1 });
- this.newRoad = new Road(p1.vectorId, p2.vectorId);
- this.newRoad.start = p1;
- this.newRoad.end = p2;
- let edgePoints;
- if (this.newRoad.way == Constant.oneWay) {
- edgePoints = mathUtil.RectangleVertex(p1, p2, newRoad.singleRoadWidth);
- } else {
- edgePoints = mathUtil.RectangleVertex(
- p1,
- p2,
- this.newRoad.leftWidth +
- this.newRoad.rightWidth +
- this.newRoad.midDivide.midDivideWidth
- );
- }
- let leftEdge = new RoadEdge(
- edgePoints.leftEdgeStart,
- edgePoints.leftEdgeEnd,
- null,
- this.newRoad.vectorId
- );
- let rightEdge = new RoadEdge(
- edgePoints.rightEdgeStart,
- edgePoints.rightEdgeEnd,
- null,
- this.newRoad.vectorId
- );
- this.newRoad.setLeftEdge(leftEdge.vectorId);
- this.newRoad.setRightEdge(rightEdge.vectorId);
- leftEdge.setEdgeParent(this.newRoad.vectorId);
- rightEdge.setEdgeParent(this.newRoad.vectorId);
- this.newRoad.leftEdge = leftEdge;
- this.newRoad.rightEdge = rightEdge;
- this.hideNewRoad();
- return this.newRoad;
- }
- setNewLineCategory(value) {
- this.newLine.setCategory(value);
- }
- showPoint() {
- this.point.display = true;
- }
- hidePoint() {
- this.point.display = false;
- }
- setPoint(position) {
- this.point.setPosition(position);
- }
- showNewRoad() {
- this.newRoad.display = true;
- }
- hideNewRoad() {
- this.newRoad.display = false;
- }
- setNewRoad(point1, point2) {
- this.newRoad.start.setPosition(point1);
- this.newRoad.end.setPosition(point2);
- //需要更新Edge坐标
- if (!mathUtil.equalPoint(point1, point2)) {
- let edgePoints = mathUtil.RectangleVertex(
- point1,
- point2,
- Constant.defaultRoadWidth
- );
- this.newRoad.leftEdge.setPositions(
- edgePoints.leftEdgeStart,
- edgePoints.leftEdgeEnd
- );
- this.newRoad.rightEdge.setPositions(
- edgePoints.rightEdgeStart,
- edgePoints.rightEdgeEnd
- );
- }
- }
- setNewLine(point1, point2) {
- this.newLineStart.setPositions(point1);
- this.newLineEnd.setPositions(point2);
- }
- showNewLine() {
- this.newLine.display = true;
- }
- hideNewLine() {
- this.newLine.display = false;
- }
- setNewRoadState(state) {
- this.newRoad.state = state;
- }
- showCheckLinesX() {
- this.checkLines.X.display = true;
- }
- hideCheckLinesX() {
- this.checkLines.X.display = false;
- }
- setCheckLinesX(point1, point2) {
- this.checkLinesXStart.setPositions(point1);
- this.checkLinesXEnd.setPositions(point2);
- }
- showCheckLinesY() {
- this.checkLines.Y.display = true;
- }
- hideCheckLinesY() {
- this.checkLines.Y.display = false;
- }
- setCheckLinesY(point1, point2) {
- this.checkLinesYStart.setPositions(point1);
- this.checkLinesYEnd.setPositions(point2);
- }
- showVCheckLinesX() {
- this.vCheckLines.X.display = true;
- }
- hideVCheckLinesX() {
- this.vCheckLines.X.display = false;
- }
- setVCheckLinesX(point1, point2) {
- this.vCheckLinesXStart.setPositions(point1);
- this.vCheckLinesXEnd.setPositions(point2);
- }
- showVCheckLinesY() {
- this.vCheckLines.Y.display = true;
- }
- hideVCheckLinesY() {
- this.vCheckLines.Y.display = false;
- }
- setVCheckLinesY(point1, point2) {
- this.vCheckLinesYStart.setPositions(point1);
- this.vCheckLinesYEnd.setPositions(point2);
- }
- hideAll() {
- this.hideCheckLinesX();
- this.hideCheckLinesY();
- this.hidePoint();
- this.hideNewRoad();
- this.hideNewLine();
- this.hideVCheckLinesX();
- this.hideVCheckLinesY();
- }
- execute(startPosition, position) {
- this.hideVCheckLinesX();
- this.hideVCheckLinesY();
- this.hideCheckLinesX();
- this.hideCheckLinesY();
- //垂直校验
- if (startPosition) {
- let start = coordinate.getXYFromScreen({
- x: 0,
- y: 0,
- });
- let end = coordinate.getXYFromScreen({
- x: coordinate.width,
- y: coordinate.height,
- });
- if (Math.abs(position.x - startPosition.x) < Constant.minAdsorbPix) {
- position.x = startPosition.x;
- start.x = position.x;
- end.x = position.x;
- this.setVCheckLinesX(start, end);
- this.showVCheckLinesX();
- }
- if (Math.abs(position.y - startPosition.y) < Constant.minAdsorbPix) {
- position.y = startPosition.y;
- start.y = position.y;
- end.y = position.y;
- this.setVCheckLinesY(start, end);
- this.showVCheckLinesY();
- }
- if (mathUtil.equalPoint(position, startPosition)) {
- this.hideVCheckLinesX();
- this.hideVCheckLinesY();
- }
- }
- if (!this.vCheckLines.Y.display && !this.vCheckLines.Y.display) {
- if (listenLayer.modifyPoint) {
- if (listenLayer.modifyPoint.linkedRoadPointIdX) {
- let linkedPointX = dataService.getRoadPoint(
- listenLayer.modifyPoint.linkedRoadPointIdX
- );
- this.setCheckLinesX(linkedPointX, position);
- this.showCheckLinesX();
- } else if (listenLayer.modifyPoint.linkedCurveRoadPointIdX) {
- let linkedPointX = dataService.getCurveRoadPoint(
- listenLayer.modifyPoint.linkedCurveRoadPointIdX
- );
- this.setCheckLinesX(linkedPointX, position);
- this.showCheckLinesX();
- } else if (listenLayer.modifyPoint.linkedPointIdX) {
- let linkedPointX = dataService.getPoint(
- listenLayer.modifyPoint.linkedPointIdX
- );
- this.setCheckLinesX(linkedPointX, position);
- this.showCheckLinesX();
- }
- if (listenLayer.modifyPoint.linkedRoadPointIdY) {
- let linkedPointY = dataService.getRoadPoint(
- listenLayer.modifyPoint.linkedRoadPointIdY
- );
- this.setCheckLinesY(linkedPointY, position);
- this.showCheckLinesY();
- } else if (listenLayer.modifyPoint.linkedCurveRoadPointIdY) {
- let linkedPointY = dataService.getCurveRoadPoint(
- listenLayer.modifyPoint.linkedCurveRoadPointIdY
- );
- this.setCheckLinesY(linkedPointY, position);
- this.showCheckLinesY();
- } else if (listenLayer.modifyPoint.linkedPointIdY) {
- let linkedPointY = dataService.getPoint(
- listenLayer.modifyPoint.linkedPointIdY
- );
- this.setCheckLinesY(linkedPointY, position);
- this.showCheckLinesY();
- }
- }
- }
- }
- // //pointId是角度的顶点
- // //exceptPointId表示position对应的pointId(如果有的话)
- // checkAngle(position, pointId, exceptPointId) {
- // //type:1表示90°,2表示180°
- // function ajust(position, point1, point2, type) {
- // let line = mathUtil.createLine1(point1, point2);
- // let join = null;
- // if (type == 1) {
- // let vLine = mathUtil.getVerticalLine(line, point1);
- // join = mathUtil.getJoinLinePoint(position, vLine);
- // } else if (type == 2) {
- // join = mathUtil.getJoinLinePoint(position, line);
- // }
- // return join;
- // }
- // let points = roadService.getNeighPoints(pointId, exceptPointId);
- // let point = dataService.getRoadPoint(pointId);
- // let newPosition = null;
- // for (let i = 0; i < points.length; ++i) {
- // let angle = mathUtil.Angle(point, position, points[i]);
- // if (Math.abs((angle - 90) < 5) {
- // newPosition = ajust(position, point, points[i], 1);
- // } else if (
- // Math.abs(angle < 5 ||
- // Math.abs(angle - 180) < 5
- // ) {
- // newPosition = ajust(position, point, points[i], 2);
- // }
- // if (newPosition != null) {
- // return newPosition;
- // }
- // }
- // return newPosition;
- // }
- }
- const elementService = new ElementService();
- window.elementService = elementService;
- export { elementService };
|