123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737 |
- import Load from "./Load";
- import { stateService } from "./Service/StateService";
- import { elementService } from "./Service/ElementService";
- import { dataService } from "./Service/DataService";
- import { measureService } from "./Service/MeasureService";
- import { tagService } from "./Service/TagService";
- import { historyService } from "./Service/HistoryService";
- import UIControl from "./Controls/UIControl";
- // import { moveRectangle } from "./Controls/MoveRectangle";
- import { moveTag } from "./Controls/MoveTag";
- import { addRoad } from "./Controls/AddRoad";
- import { moveRoad } from "./Controls/MoveRoad";
- import { coordinate } from "./Coordinate";
- import Render from "./Renderer/Render";
- import { draw } from "./Renderer/Draw";
- import { listenLayer } from "./ListenLayer";
- import LayerEvents from "./enum/LayerEvents.js";
- import UIEvents from "./enum/UIEvents.js";
- import SelectState from "./enum/SelectState.js";
- import VectorType from "./enum/VectorType";
- import { mathUtil } from "./Util/MathUtil";
- import History from "./History/History";
- import mitt from "mitt";
- import { roadService } from "./Service/RoadService";
- import { edgeService } from "./Service/EdgeService";
- import { pointService } from "./Service/PointService";
- import { curveRoadService } from "./Service/CurveRoadService";
- const minDragDis = 10;
- export default class Layer {
- constructor(canvas) {
- this.canvas = canvas;
- this.load = new Load(this);
- this.uiControl = new UIControl(this);
- this.renderer = new Render(this);
- this.history = new History(this);
- this.dragging = false; // 当前是否正在拖拽
- this.start();
- Object.setPrototypeOf(Object.getPrototypeOf(this), mitt());
- }
- start() {
- if (this.canvas) {
- this.canvas.width = this.canvas.clientWidth;
- this.canvas.height = this.canvas.clientHeight;
- coordinate.init(this.canvas);
- draw.initContext(this.canvas);
- dataService.initVectorData();
- this.history.init();
- this.bindEvents();
- }
- window.vectorData = dataService.vectorData;
- }
- bindEvents() {
- this.canvas.addEventListener("contextmenu", function (e) {
- e.preventDefault();
- });
- this.canvas.addEventListener("mousedown", this.onMouseDown.bind(this));
- this.canvas.addEventListener("mousemove", this.onMouseMove.bind(this));
- this.canvas.addEventListener("mouseup", this.onMouseUp.bind(this));
- this.canvas.addEventListener("mousewheel", this.onWheel.bind(this));
- this.canvas.addEventListener("DOMMouseScroll", this.onWheel.bind(this));
- this.canvas.addEventListener("resize", this.reSize.bind(this));
- document.addEventListener("keydown", this.onKeydown.bind(this));
- }
- reSize = function () {
- console.log("resize");
- coordinate.updateForCanvas();
- this.renderer.autoRedraw();
- };
- onMouseDown(e) {
- this.startX = e.offsetX || e.layerX;
- this.startY = e.offsetY || e.layerY;
- this.lastX = e.offsetX || e.layerX;
- this.lastY = e.offsetY || e.layerY;
- // 右键
- if (e.button == 2) {
- this.stopAddVector();
- this.uiControl.currentUI = null;
- this.renderer.autoRedraw();
- return;
- }
- this.dragging = false;
- this.setEventName("mouseDown");
- const selectItem = stateService.getSelectItem();
- const eventName = stateService.getEventName();
- stateService.setDraggingItem(selectItem);
- // 清除上一个状态
- // 设置当前事件名称
- e.preventDefault();
- e.stopPropagation();
- }
- onMouseMove(e) {
- const X = e.offsetX || e.layerX;
- const Y = e.offsetY || e.layerY;
- let dx = X - this.lastX;
- let dy = Y - this.lastY;
- let position = coordinate.getXYFromScreen({
- x: X,
- y: Y,
- });
- if (
- Math.abs(X - this.startX) > minDragDis ||
- Math.abs(Y - this.startY) > minDragDis
- ) {
- // 是否拖拽了
- this.dragging = true;
- }
- const eventName = stateService.getEventName();
- // 是否需要重绘
- let needAutoRedraw = false;
- let point = null;
- const draggingItem = stateService.getDraggingItem();
- switch (eventName) {
- case null:
- //监控
- needAutoRedraw = listenLayer.start(position);
- break;
- case LayerEvents.PanBackGround:
- stateService.clearItems();
- coordinate.center.x =
- coordinate.center.x - (dx * coordinate.defaultZoom) / coordinate.zoom;
- coordinate.center.y =
- coordinate.center.y + (dy * coordinate.defaultZoom) / coordinate.zoom;
- this.lastX = X;
- this.lastY = Y;
- needAutoRedraw = true;
- break;
- case LayerEvents.AddRoad:
- needAutoRedraw = true;
- listenLayer.start(position);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- elementService.hideAll();
- elementService.setPoint(position);
- elementService.showPoint();
- if (listenLayer.modifyPoint) {
- elementService.execute(listenLayer.modifyPoint, position);
- }
- break;
- case LayerEvents.AddingRoad:
- needAutoRedraw = true;
- listenLayer.start(position);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- elementService.execute(addRoad.startInfo.position, position);
- elementService.setPoint(position);
- elementService.setNewRoad(addRoad.startInfo.position, position);
- elementService.showNewRoad();
- elementService.setNewRoadGeoType(VectorType.Road);
- addRoad.setNewRoadPoint("end", position);
- addRoad.canAdd = addRoad.canAddRoadForEnd(position);
- if (!addRoad.canAdd) {
- elementService.setNewRoadState("error");
- } else {
- elementService.setNewRoadState("normal");
- }
- break;
- case LayerEvents.MoveRoad:
- needAutoRedraw = true;
- //只允许拖拽一条公路
- let road = dataService.getRoad(draggingItem.vectorId);
- let start = dataService.getPoint(road.startId);
- let end = dataService.getPoint(road.endId);
- if (
- Object.keys(start.getParent()).length == 1 &&
- Object.keys(end.getParent()).length == 1
- ) {
- //拖拽的路只有一条
- moveRoad.moveRoad(
- draggingItem.vectorId,
- (dx * coordinate.defaultZoom) / coordinate.zoom,
- (dy * coordinate.defaultZoom) / coordinate.zoom
- );
- }
- this.lastX = X;
- this.lastY = Y;
- break;
- case LayerEvents.MoveRoadPoint:
- point = dataService.getPoint(draggingItem.vectorId);
- //listenLayer.start(position, draggingItem.vectorId, point.parent);
- listenLayer.start(position, draggingItem.vectorId, point.parent);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- let flag = moveRoad.moveingRoadPoint(
- draggingItem.vectorId,
- position,
- listenLayer.modifyPoint
- );
- if (!flag) {
- elementService.hideAll();
- } else {
- point = dataService.getPoint(draggingItem.vectorId);
- listenLayer.start(point, draggingItem.vectorId, point.parent);
- let otherPoint = null;
- if (
- listenLayer.modifyPoint &&
- listenLayer.modifyPoint.linkedPointId
- ) {
- otherPoint = dataService.getPoint(
- listenLayer.modifyPoint.linkedPointId
- );
- } else if (
- listenLayer.modifyPoint &&
- listenLayer.modifyPoint.linkedPointIdX
- ) {
- otherPoint = dataService.getPoint(
- listenLayer.modifyPoint.linkedPointIdX
- );
- } else if (
- listenLayer.modifyPoint &&
- listenLayer.modifyPoint.linkedPointIdY
- ) {
- otherPoint = dataService.getPoint(
- listenLayer.modifyPoint.linkedPointIdY
- );
- }
- elementService.execute(otherPoint, point);
- }
- needAutoRedraw = true;
- break;
- case LayerEvents.AddCurveRoad:
- needAutoRedraw = true;
- listenLayer.start(position);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- elementService.hideAll();
- elementService.setPoint(position);
- elementService.showPoint();
- if (listenLayer.modifyPoint) {
- elementService.execute(listenLayer.modifyPoint, position);
- }
- break;
- case LayerEvents.AddingCurveRoad:
- needAutoRedraw = true;
- listenLayer.start(position);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- elementService.execute(addRoad.startInfo.position, position);
- elementService.setPoint(position);
- elementService.setNewRoad(addRoad.startInfo.position, position);
- elementService.showNewRoad();
- elementService.setNewRoadGeoType(VectorType.Road);
- addRoad.setNewRoadPoint("end", position);
- addRoad.canAdd = addRoad.canAddRoadForEnd(position);
- if (!addRoad.canAdd) {
- elementService.setNewRoadState("error");
- } else {
- elementService.setNewRoadState("normal");
- }
- break;
- case LayerEvents.AddMeasureLine:
- needAutoRedraw = true;
- listenLayer.start(position);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- elementService.setPoint(position);
- elementService.showPoint();
- addRoad.setNewRoadPoint("start", position);
- break;
- case LayerEvents.MoveCurveRoadPoint:
- point = dataService.getCurvePoint(draggingItem.vectorId);
- listenLayer.start(position, draggingItem.vectorId, null, point.parent);
- if (listenLayer.modifyPoint) {
- position = {
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- };
- }
- if (draggingItem) {
- moveRoad.moveCurveRoadPoint(draggingItem.vectorId, position);
- needAutoRedraw = true;
- }
- break;
- case LayerEvents.AddTag:
- needAutoRedraw = true;
- if (draggingItem == null) {
- const tag = tagService.createTag(position);
- if (tag.vectorId) {
- stateService.setSelectItem(
- tag.vectorId,
- VectorType.Tag,
- SelectState.All
- );
- stateService.setDraggingItem(stateService.selectItem);
- }
- } else {
- moveTag.moveFullTag(position, draggingItem.vectorId);
- }
- break;
- case LayerEvents.MoveTag:
- needAutoRedraw = true;
- if (draggingItem != null) {
- moveTag.moveFullTag(position, draggingItem.vectorId);
- }
- break;
- }
- if (needAutoRedraw) {
- this.renderer.autoRedraw();
- }
- }
- onMouseUp(e) {
- // 右键
- if (e.button == 2) {
- return;
- }
- const X = e.offsetX || e.layerX;
- const Y = e.offsetY || e.layerY;
- let eventName = stateService.getEventName();
- const draggingItem = stateService.getDraggingItem();
- const selectItem = stateService.getSelectItem();
- let focusItem = null;
- if (!this.dragging && selectItem) {
- focusItem = {
- vectorId: selectItem.vectorId,
- type: selectItem.type,
- cursor: { x: this.lastX, y: this.lastY },
- };
- stateService.setFocusItem(focusItem);
- stateService.clearDraggingItem();
- }
- let position = coordinate.getXYFromScreen({
- x: X,
- y: Y,
- });
- let needAutoRedraw = false;
- switch (eventName) {
- case null:
- return;
- case LayerEvents.PanBackGround:
- needAutoRedraw = true;
- stateService.clearFocusItem();
- this.uiControl.currentUI = null;
- break;
- case LayerEvents.MoveRoadPoint:
- needAutoRedraw = true;
- elementService.hideAll();
- let point = dataService.getPoint(draggingItem.vectorId);
- if (point) {
- listenLayer.start(point, draggingItem.vectorId, point.parent);
- if (
- listenLayer.modifyPoint &&
- listenLayer.modifyPoint.hasOwnProperty("linkedPointId")
- ) {
- moveRoad.moveTo(
- draggingItem.vectorId,
- listenLayer.modifyPoint.linkedPointId
- );
- } else if (
- listenLayer.modifyPoint &&
- (listenLayer.modifyPoint.linkedPointIdX ||
- listenLayer.modifyPoint.linkedPointIdY)
- ) {
- mathUtil.clonePoint(point, listenLayer.modifyPoint);
- } else if (
- listenLayer.modifyPoint &&
- listenLayer.modifyPoint.hasOwnProperty("linkedRoadId")
- ) {
- point = pointService.create({
- x: listenLayer.modifyPoint.x,
- y: listenLayer.modifyPoint.y,
- });
- roadService.splitRoad(
- listenLayer.modifyPoint.linkedRoadId,
- point.vectorId,
- "start"
- );
- moveRoad.moveTo(draggingItem.vectorId, point.vectorId);
- } else if (moveRoad.splitRoadId != null) {
- roadService.splitRoad(
- moveRoad.splitRoadId,
- draggingItem.vectorId,
- "start"
- );
- }
- //draggingItem.vectorId所在的墙面与其他墙角相交
- moveRoad.updateForAbsorbRoadPoints();
- let parent = point.getParent();
- for (let key in parent) {
- roadService.setLanes(key);
- }
- this.history.save();
- }
- break;
- case LayerEvents.AddRoad:
- addRoad.setNewRoadPoint("start", position);
- break;
- case LayerEvents.AddingRoad:
- needAutoRedraw = true;
- if (addRoad.canAdd) {
- addRoad.buildRoad();
- addRoad.clear();
- this.history.save();
- elementService.hideAll();
- }
- break;
- case LayerEvents.AddCurveRoad:
- addRoad.setNewRoadPoint("start", position);
- break;
- case LayerEvents.AddingCurveRoad:
- needAutoRedraw = true;
- if (addRoad.canAdd) {
- addRoad.buildCurveRoad();
- addRoad.clear();
- this.history.save();
- elementService.hideAll();
- }
- break;
- case LayerEvents.MoveRoad:
- needAutoRedraw = true;
- if (focusItem != null && focusItem.type == VectorType.Road) {
- const road = dataService.getRoad(focusItem.vectorId);
- this.uiControl.currentUI = focusItem.type;
- }
- this.history.save();
- moveRoad.setStartMoving(false);
- break;
- case LayerEvents.MoveTag:
- needAutoRedraw = true;
- if (focusItem != null && focusItem.type == VectorType.Tag) {
- this.uiControl.currentUI = focusItem.type;
- }
- this.history.save();
- break;
- case LayerEvents.AddTag:
- needAutoRedraw = true;
- let tag = dataService.getTag(draggingItem.vectorId);
- tag.setAdding(false);
- focusItem = {
- vectorId: draggingItem.vectorId,
- type: draggingItem.type,
- cursor: { x: this.lastX, y: this.lastY },
- };
- stateService.setFocusItem(focusItem);
- this.history.save();
- this.uiControl.currentUI = focusItem.type;
- break;
- }
- this.setEventName("mouseUp");
- stateService.clearDraggingItem();
- this.renderer.autoRedraw();
- }
- onWheel(e) {
- e.preventDefault();
- const type = e.type;
- if (type == "DOMMouseScroll" || type == "mousewheel") {
- // 当在canvas用滚轮滚动时
- const delta = e.wheelDelta
- ? (e.wheelDelta / 120) * 20
- : (-(e.detail || 0) / 3) * 20;
- const zoom = coordinate.zoom + delta;
- if (zoom < 14) {
- return;
- }
- coordinate.updateZoom(zoom);
- this.renderer.autoRedraw();
- }
- }
- //测试用
- onKeydown(e) {
- let focusItem = stateService.getFocusItem();
- if (focusItem) {
- console.log("键盘(foucus有效):" + e.code);
- if (e.code == "Delete") {
- //删除
- const road = dataService.getRoad(focusItem.vectorId);
- roadService.subtraRoadFromIntersect(road.startId, focusItem.vectorId);
- roadService.subtraRoadFromIntersect(road.endId, focusItem.vectorId);
- //dataService.deleteControlPoint()
- dataService.deleteRoad(focusItem.vectorId);
- this.renderer.autoRedraw();
- this.history.save();
- }
- //加宽
- else if (e.code == "KeyA") {
- const road = dataService.getRoad(focusItem.vectorId);
- road.width += 100;
- edgeService.updateEdgeForMovePoint(road.startId);
- edgeService.updateEdgeForMovePoint(road.endId);
- roadService.setLanes(road.vectorId);
- this.renderer.autoRedraw();
- this.history.save();
- }
- //变窄
- else if (e.code == "KeyB") {
- const road = dataService.getRoad(focusItem.vectorId);
- road.width -= 50;
- edgeService.updateEdgeForMovePoint(road.startId);
- edgeService.updateEdgeForMovePoint(road.endId);
- roadService.setLanes(road.vectorId);
- this.renderer.autoRedraw();
- this.history.save();
- }
- //添加左车道
- else if (e.code == "KeyQ") {
- let road = dataService.getRoad(focusItem.vectorId);
- if (road) {
- roadService.updateForAddSubtractLanesCount(
- focusItem.vectorId,
- road.leftDrivewayCount + 1,
- "left"
- );
- } else {
- road = dataService.getCurveRoad(focusItem.vectorId);
- curveRoadService.updateForAddSubtractLanesCount(
- road,
- road.leftDrivewayCount + 1, //rightDrivewayCount
- "left"
- );
- }
- this.renderer.autoRedraw();
- this.history.save();
- }
- //减少左车道
- else if (e.code == "KeyW") {
- let road = dataService.getRoad(focusItem.vectorId);
- if (road) {
- roadService.updateForAddSubtractLanesCount(
- focusItem.vectorId,
- road.leftDrivewayCount - 1,
- "left"
- );
- } else {
- road = dataService.getCurveRoad(focusItem.vectorId);
- curveRoadService.updateForAddSubtractLanesCount(
- road,
- road.leftDrivewayCount - 1, //rightDrivewayCount
- "left"
- );
- }
- this.renderer.autoRedraw();
- this.history.save();
- }
- //添加右车道
- else if (e.code == "KeyE") {
- let road = dataService.getRoad(focusItem.vectorId);
- if (road) {
- roadService.updateForAddSubtractLanesCount(
- focusItem.vectorId,
- road.rightDrivewayCount + 1,
- "right"
- );
- } else {
- road = dataService.getCurveRoad(focusItem.vectorId);
- curveRoadService.updateForAddSubtractLanesCount(
- road,
- road.rightDrivewayCount + 1, //rightDrivewayCount
- "right"
- );
- }
- this.renderer.autoRedraw();
- this.history.save();
- }
- //减少右车道
- else if (e.code == "KeyR") {
- let road = dataService.getRoad(focusItem.vectorId);
- if (road) {
- roadService.updateForAddSubtractLanesCount(
- focusItem.vectorId,
- road.rightDrivewayCount - 1,
- "right"
- );
- } else {
- road = dataService.getCurveRoad(focusItem.vectorId);
- curveRoadService.updateForAddSubtractLanesCount(
- road,
- road.rightDrivewayCount - 1, //rightDrivewayCount
- "right"
- );
- }
- this.renderer.autoRedraw();
- this.history.save();
- }
- } else {
- console.log("键盘(foucus无效):" + e.code);
- }
- }
- setEventName(eventType) {
- let eventName = stateService.getEventName();
- if (eventType == "mouseDown") {
- if (eventName == null) {
- const selectItem = stateService.getSelectItem();
- if (selectItem == null) {
- stateService.setEventName(LayerEvents.PanBackGround);
- } else if (selectItem.type == VectorType.Road) {
- stateService.setEventName(LayerEvents.MoveRoad);
- } else if (selectItem.type == VectorType.Point) {
- stateService.setEventName(LayerEvents.MoveRoadPoint);
- } else if (selectItem.type == VectorType.CurveRoad) {
- stateService.setEventName(LayerEvents.MoveCurveRoad);
- } else if (selectItem.type == VectorType.CurvePoint) {
- stateService.setEventName(LayerEvents.MoveCurveRoadPoint);
- } else if (selectItem.type == VectorType.Tag) {
- stateService.setEventName(LayerEvents.MoveTag);
- } else if (selectItem.type == VectorType.MeasureLine) {
- stateService.setEventName(LayerEvents.MoveMeasureLine);
- } else if (selectItem.type == VectorType.MeasureArrow) {
- stateService.setEventName(LayerEvents.MoveMeasureArrow);
- }
- }
- } else if (eventType == "mouseUp") {
- if (eventName == LayerEvents.AddTag) {
- //可连续添加
- //stateService.clearEventName()
- } else if (eventName == LayerEvents.AddRoad) {
- stateService.setEventName(LayerEvents.AddingRoad);
- } else if (eventName == LayerEvents.AddingRoad) {
- stateService.setEventName(LayerEvents.AddRoad);
- } else if (eventName == LayerEvents.AddCurveRoad) {
- stateService.setEventName(LayerEvents.AddingCurveRoad);
- } else if (eventName == LayerEvents.AddingCurveRoad) {
- stateService.setEventName(LayerEvents.AddCurveRoad);
- } else if (eventName == LayerEvents.AddMeasureLine) {
- stateService.setEventName(LayerEvents.AddingMeasureLine);
- } else if (eventName == LayerEvents.AddingMeasureLine) {
- stateService.setEventName(LayerEvents.AddMeasureLine);
- } else {
- stateService.clearEventName();
- }
- }
- }
- exit() {
- stateService.clear();
- this.uiControl.clearUI();
- }
- stopAddVector() {
- let eventName = stateService.getEventName();
- if (eventName != LayerEvents.AddingRoad) {
- stateService.clearEventName();
- const draggingItem = stateService.getDraggingItem();
- if (eventName == LayerEvents.AddTag) {
- if (draggingItem && draggingItem.vectorId) {
- dataService.deleteTag(draggingItem.vectorId);
- this.uiControl.currentUI = null;
- }
- }
- } else {
- stateService.setEventName(LayerEvents.AddRoad);
- }
- this.uiControl.clearUI();
- elementService.hideAll();
- }
- update() {}
- revokeHistory() {
- this.history.goPreState();
- this.renderer.autoRedraw();
- }
- recoveryHistory() {
- this.history.goNextState();
- this.renderer.autoRedraw();
- }
- deleteItem() {
- let item = stateService.getFocusItem();
- if (item) {
- if (item.type == VectorType.Road) {
- dataService.deleteRoad(item.vectorId);
- } else if (item.type == VectorType.Tag) {
- dataService.deleteTag(item.vectorId);
- } else if (item.type == VectorType.Point) {
- //这个比较复杂,参考deleteRoadCorner
- //dataService.deletePoint(item.vectorId);
- }
- this.history.save();
- this.renderer.autoRedraw();
- }
- }
- }
|