123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- import { coordinate } from "../Coordinate.js";
- import LayerEvents from "../enum/LayerEvents.js";
- import UIEvents from "../enum/UIEvents.js";
- import RoadTemplate from "../enum/RoadTemplate.js";
- import RoadStructure from "../enum/RoadStructure.js";
- import VectorType from "../enum/VectorType.js";
- import VectorStyle from "../enum/VectorStyle.js";
- import VectorWeight from "../enum/VectorWeight.js";
- import GeoActions from "../enum/GeoActions.js";
- import VectorEvents from "../enum/VectorEvents.js";
- import SVGType from "../enum/SVGType.js";
- import { stateService } from "../Service/StateService.js";
- import { uiService } from "../Service/UIService.js";
- import { dataService } from "../Service/DataService.js";
- import { historyService } from "../Service/HistoryService.js";
- import { elementService } from "../Service/ElementService";
- import { lineService } from "../Service/LineService.js";
- import { circleService } from "../Service/CircleService.js";
- import { textService } from "../Service/TextService.js";
- import { svgService } from "../Service/SVGService.js";
- import { magnifierService } from "../Service/MagnifierService.js";
- import { mathUtil } from "../Util/MathUtil";
- import Constant from "../Constant";
- // import { roomsUtil } from "../Room/RoomsUtil.js";
- import { addRoad } from "../Controls/AddRoad";
- import { addLine } from "./AddLine.js";
- import VectorCategory from "../enum/VectorCategory.js";
- // import { floorplanData } from "../VectorData.js";
- import { pointService } from "../Service/PointService.js";
- import Settings from "../Settings.js";
- import { addPoint } from "./AddPoint.js";
- import { locationModeControl } from "./LocationModeControl.js";
- import { curveRoadPointService } from "../Service/CurveRoadPointService.js";
- import { roadService } from "../Service/RoadService.js";
- import { curveRoadService } from "../Service/CurveRoadService.js";
- import Msg from "../enum/Msg.js";
- export default class UIControl {
- constructor(layer, newsletter, graphicStateUI) {
- this._prompts = [];
- this.layer = layer;
- this.newsletter = newsletter;
- this.graphicStateUI = graphicStateUI;
- }
- get selectUI() {
- return this.newsletter.selectUI;
- }
- set selectUI(selectUI) {
- this.updateEventNameForSelectUI(selectUI);
- this.newsletter.selectUI = selectUI;
- }
- get focusVector() {
- return this.newsletter.focusVector;
- }
- set focusVector(focusVector) {
- this.newsletter.focusVector = focusVector;
- }
- /**
- * 获取选中要操作的UI
- */
- get currentUI() {}
- /**
- * 设置选中要操作的UI
- */
- set currentUI(value) {
- this.selectUI = value;
- }
- clearUI() {
- this.clearCurrentUI();
- this.clearSelectUI();
- }
- clearFocusVector() {
- this.focusVector = null;
- }
- clearSelectUI() {
- this.selectUI = null;
- }
- clearCurrentUI() {
- this.currentUI = null;
- }
- //点击左侧栏后,更新事件
- updateEventNameForSelectUI(selectUI) {
- console.log(this.selectUI, selectUI);
- if (selectUI != null) {
- if (this.selectUI == selectUI) {
- return;
- } else if (this.selectUI != selectUI) {
- if (this.selectUI != null) {
- //先取消当前事件和进程
- this.layer.exit();
- }
- //执行新的事件
- if (uiService.isBelongRoad(selectUI) || selectUI == "road") {
- stateService.setEventName(LayerEvents.AddRoad);
- } else if (selectUI == UIEvents.CurveRoad) {
- stateService.setEventName(LayerEvents.AddCurveRoad);
- } else if (uiService.isBelongLine(selectUI)) {
- stateService.setEventName(LayerEvents.AddLine);
- }
- // else if (selectUI == UIEvents.CurveLine) {
- // stateService.setEventName(LayerEvents.AddCurveLine);
- // }
- else if (uiService.isBelongPoint(selectUI)) {
- stateService.setEventName(LayerEvents.AddPoint);
- } else if (selectUI == UIEvents.Circle) {
- stateService.setEventName(LayerEvents.AddCircle);
- } else if (selectUI == UIEvents.Text) {
- stateService.setEventName(LayerEvents.AddText);
- } else if (selectUI == UIEvents.Magnifier) {
- stateService.setEventName(LayerEvents.AddMagnifier);
- } else if (SVGType[selectUI]) {
- uiService.setSelectSVGType(selectUI);
- stateService.setEventName(LayerEvents.AddSVG);
- } else if (selectUI == UIEvents.Img) {
- stateService.setEventName(LayerEvents.Img);
- } else if (uiService.isBelongRoadTemplate(selectUI)) {
- stateService.setEventName(LayerEvents.AddRoadTemplate);
- } else if (uiService.isBelongRoadStructure(selectUI)) {
- stateService.setEventName(LayerEvents.AddRoadStructure);
- } else if (selectUI == Constant.angleLocationMode) {
- uiService.setSelectLocationMode(Constant.angleLocationMode);
- let msg = locationModeControl.setAngle();
- if (msg != Msg.OK) {
- uiService.prompt({ msg: msg, time: 1000 });
- } else {
- this.layer.history.save();
- this.layer.renderer.autoRedraw();
- }
- }
- }
- }
- }
- updateVectorForSelectUI(selectUI) {
- console.log("selectUI", selectUI);
- const focusItem = stateService.getFocusItem();
- // if (selectUI == VectorStyle.Bold || selectUI == VectorStyle.Thinning) {
- // if (focusItem.type == VectorType.Line) {
- // let Line = dataService.getLine(focusItem.vectorId);
- // Line.setStyle(selectUI);
- // } else if (focusItem.type == VectorType.RoadEdge) {
- // let roadEdge = dataService.getRoadEdge(focusItem.vectorId);
- // if (roadEdge) {
- // roadEdge.setStyle(selectUI);
- // } else {
- // roadEdge = dataService.getCurveRoadEdge(focusItem.vectorId);
- // roadEdge.setStyle(selectUI);
- // }
- // }
- // this.layer.history.save();
- // this.layer.renderer.autoRedraw();
- // }
- // else
- if (uiService.isBelongRoadEdgeStyle(selectUI)) {
- let key = null;
- if (VectorStyle[selectUI]) {
- key = "setStyle";
- } else if (VectorWeight[selectUI]) {
- key = "setWeight";
- }
- if (focusItem.type == VectorType.Line) {
- let Line = dataService.getLine(focusItem.vectorId);
- // Line.setStyle(selectUI);
- Line[key](selectUI);
- } else if (focusItem.type == VectorType.CurveLine) {
- let Line = dataService.getCurveLine(focusItem.vectorId);
- // Line.setStyle(selectUI);
- Line[key](selectUI);
- } else {
- let roadEdge = dataService.getRoadEdge(focusItem.vectorId);
- if (roadEdge) {
- // roadEdge.setStyle(selectUI);
- roadEdge[key](selectUI);
- let crossPoint = dataService.getCrossPoint4(focusItem.vectorId);
- if (crossPoint) {
- crossPoint[key](selectUI);
- }
- } else {
- roadEdge = dataService.getCurveRoadEdge(focusItem.vectorId);
- // roadEdge.setStyle(selectUI);
- roadEdge[key](selectUI);
- }
- }
- this.layer.history.save();
- this.layer.renderer.autoRedraw();
- } else if (selectUI == VectorEvents.AddLane) {
- if (focusItem && focusItem.vectorId) {
- stateService.setEventName(VectorEvents.AddLane);
- }
- // if (focusItem && focusItem.dir && focusItem.vectorId) {
- // let road = dataService.getRoad(focusItem.vectorId);
- // if (road) {
- // if (focusItem.dir == "left") {
- // roadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.leftDrivewayCount + 1,
- // focusItem.dir
- // );
- // } else {
- // roadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.rightDrivewayCount + 1,
- // focusItem.dir
- // );
- // }
- // } else {
- // road = dataService.getCurveRoad(focusItem.vectorId);
- // if (focusItem.dir == "left") {
- // curveRoadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.leftDrivewayCount + 1,
- // focusItem.dir
- // );
- // } else {
- // curveRoadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.rightDrivewayCount + 1,
- // focusItem.dir
- // );
- // }
- // }
- // }
- } else if (selectUI == VectorEvents.DelLane) {
- if (focusItem && focusItem.vectorId) {
- stateService.setEventName(VectorEvents.DelLane);
- }
- // if (focusItem && focusItem.dir && focusItem.vectorId) {
- // let road = dataService.getRoad(focusItem.vectorId);
- // if (road) {
- // if (focusItem.dir == "left") {
- // roadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.leftDrivewayCount - 1,
- // focusItem.dir
- // );
- // } else {
- // roadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.rightDrivewayCount - 1,
- // focusItem.dir
- // );
- // }
- // } else {
- // road = dataService.getCurveRoad(focusItem.vectorId);
- // if (focusItem.dir == "left") {
- // curveRoadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.leftDrivewayCount - 1,
- // focusItem.dir
- // );
- // } else {
- // curveRoadService.updateForAddSubtractLanesCount(
- // road.vectorId,
- // road.rightDrivewayCount - 1,
- // focusItem.dir
- // );
- // }
- // }
- // }
- } else if (selectUI == VectorEvents.AddCrossPoint) {
- if (focusItem && focusItem.vectorId) {
- stateService.setEventName(VectorEvents.AddCrossPoint);
- }
- // if (focusItem && focusItem.dir && focusItem.vectorId) {
- // const curveRoad = dataService.getCurveRoad(focusItem.vectorId);
- // let index = mathUtil.getIndexForCurvesPoints(
- // this.mousePosition,
- // curveRoad.points
- // );
- // if (index != -1) {
- // curveRoadService.addCPoint(curveRoad, this.mousePosition, index);
- // } else {
- // const dis1 = mathUtil.getDistance(
- // curveRoad.points[0],
- // this.mousePosition
- // );
- // const dis2 = mathUtil.getDistance(
- // curveRoad.points[curveRoad.points.length - 1],
- // this.mousePosition
- // );
- // if (dis1 > dis2) {
- // curveRoadService.addCPoint(
- // curveRoad,
- // this.mousePosition,
- // curveRoad.points.length - 2
- // );
- // } else {
- // curveRoadService.addCPoint(curveRoad, this.mousePosition, 1);
- // }
- // }
- // }
- } else if (selectUI == VectorEvents.MinusCrossPoint) {
- if (focusItem && focusItem.vectorId) {
- stateService.setEventName(VectorEvents.MinusCrossPoint);
- }
- // if (focusItem && focusItem.dir && focusItem.vectorId) {
- // const curvePoint = dataService.getCurveRoadPoint(focusItem.vectorId);
- // const curveRoad = dataService.getCurveRoad(curvePoint.parent);
- // curveRoadService.subCPoint(curveRoad, curvePoint.getIndex());
- // }
- } else if (selectUI == VectorEvents.AddBranchRoad) {
- } else if (selectUI == VectorEvents.AddNarrowRoad) {
- } else if (selectUI == VectorEvents.UnLock) {
- let road = dataService.getRoad(focusItem.vectorId);
- if (road) {
- roadService.convertToLines(focusItem.vectorId);
- } else {
- road = dataService.getCurveRoad(focusItem.vectorId);
- if (road) {
- curveRoadService.convertToCurveLines(focusItem.vectorId);
- }
- }
- if (road) {
- this.deleteVector(focusItem.vectorId, focusItem.type);
- this.layer.history.save();
- this.layer.renderer.autoRedraw();
- }
- }
- }
- async handleGeo(action) {
- let needAutoRedraw = false;
- const item = stateService.getFocusItem();
- if (item && item.vectorId) {
- switch (action) {
- case GeoActions.CopyAction:
- await this.copyVector(item.vectorId, item.type);
- needAutoRedraw = true;
- break;
- case GeoActions.DeleteAction:
- this.deleteVector(item.vectorId, item.type);
- needAutoRedraw = true;
- break;
- }
- }
- if (needAutoRedraw) {
- this.layer.history.save();
- this.layer.renderer.autoRedraw();
- }
- }
- //删除按钮
- deleteVector(vectorId, geoType) {
- switch (geoType) {
- case VectorType.Point:
- pointService.deletePoint(vectorId);
- break;
- case VectorType.Line:
- let line = dataService.getLine(vectorId);
- dataService.deleteLine(vectorId);
- if (vectorId == Settings.baseLineId) {
- this.layer.initLocation();
- } else if (
- line.getCategory() == VectorCategory.Line.ExtendedPositionLine ||
- line.getCategory() == VectorCategory.Line.PositionLine ||
- line.getCategory() == VectorCategory.Line.GuidePositionLine
- ) {
- let startPoint = dataService.getPoint(line.startId);
- let endPoint = dataService.getPoint(line.endId);
- if (startPoint.getCategory() != VectorCategory.Point.BasePoint) {
- pointService.deletePoint(line.startId);
- }
- if (endPoint.getCategory() != VectorCategory.Point.BasePoint) {
- pointService.deletePoint(line.endId);
- }
- } else if (
- line.getCategory() == VectorCategory.Line.LocationLineByFixPoint
- ) {
- let lines = dataService.getLines();
- for (let key in lines) {
- let _line = dataService.getLine(key);
- if (
- _line.getCategory() == VectorCategory.Line.ExtendedPositionLine
- ) {
- if (
- line.startId == _line.startId ||
- line.startId == _line.endId ||
- line.endId == _line.startId ||
- line.endId == _line.endId
- ) {
- dataService.deleteLine(key);
- break;
- }
- }
- }
- } else if (
- line.getCategory() == VectorCategory.Line.LocationLineByBasePoint
- ) {
- let lines = dataService.getLines();
- for (let key in lines) {
- let _line = dataService.getLine(key);
- if (
- _line.getCategory() == VectorCategory.Line.ExtendedPositionLine
- ) {
- if (
- line.startId == _line.startId ||
- line.startId == _line.endId ||
- line.endId == _line.startId ||
- line.endId == _line.endId
- ) {
- dataService.deleteLine(key);
- }
- } else if (
- _line.getCategory() == VectorCategory.Line.GuideLocationLine
- ) {
- if (
- line.startId == _line.startId ||
- line.startId == _line.endId ||
- line.endId == _line.startId ||
- line.endId == _line.endId
- ) {
- dataService.deleteLine(key);
- }
- }
- }
- }
- break;
- case VectorType.CurveLine:
- lineService.deleteCurveLine(vectorId);
- break;
- case VectorType.CurvePoint:
- const curvePoint = dataService.getCurvePoint(vectorId);
- lineService.deleteCrossPointForCurveLine(
- vectorId,
- curvePoint.getParent()
- );
- break;
- case VectorType.Circle:
- dataService.deleteCircle(vectorId);
- break;
- case VectorType.Text:
- dataService.deleteText(vectorId);
- break;
- case VectorType.Magnifier:
- dataService.deleteMagnifier(vectorId);
- break;
- case VectorType.Road:
- roadService.deleteRoadForLinked(vectorId);
- break;
- case VectorType.RoadPoint:
- const roadPoint = dataService.getRoadPoint(vectorId);
- const roadPointParent = roadPoint.getParent();
- for (let key in roadPointParent) {
- roadService.deleteRoadForLinked(key);
- }
- break;
- case VectorType.CurveRoadPoint:
- curveRoadPointService.deleteCurveRoadPoint(vectorId);
- break;
- case VectorType.CurveRoad:
- dataService.deleteCurveRoad(vectorId);
- break;
- case VectorType.SVG:
- dataService.deleteSVG(vectorId);
- break;
- }
- this.layer.exit();
- uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
- uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
- this.clearFocusVector();
- }
- //复制按钮
- async copyVector(vectorId, geoType) {
- let item;
- switch (geoType) {
- case VectorType.Line:
- const lineId = lineService.copy(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = lineId;
- break;
- case VectorType.CurveLine:
- const curveLineId = lineService.copyCurveLine(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = curveLineId;
- break;
- case VectorType.Circle:
- const circleId = circleService.copy(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = circleId;
- break;
- case VectorType.Text:
- const textId = textService.copy(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = textId;
- break;
- case VectorType.Road:
- const roadId = roadService.copyRoad(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = roadId;
- break;
- case VectorType.CurveRoad:
- const curveRoadId = curveRoadService.copyCurveRoad(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = curveRoadId;
- break;
- case VectorType.Magnifier:
- const magnifierId = await magnifierService.copy(vectorId);
- item = stateService.getFocusItem();
- if (magnifierId) {
- item.vectorId = magnifierId;
- }
- break;
- case VectorType.SVG:
- const svgId = svgService.copy(vectorId);
- item = stateService.getFocusItem();
- item.vectorId = svgId;
- break;
- }
- stateService.clearEventName();
- }
- //截图
- async screenShot() {
- let canvas = this.layer.canvas;
- this.menu_view_reset();
- //隐藏grid
- Settings.screenMode = true;
- dataService.setGridDisplay(false);
- this.layer.renderer.autoRedraw();
- // this.downloadCadImg(canvas, "test.jpg");
- const blob = await this.getCadBlob(canvas);
- //显示grid
- dataService.setGridDisplay(true);
- Settings.screenMode = false;
- this.layer.renderer.autoRedraw();
- return blob;
- }
- getCadBlob(canvas) {
- var type = "jpg";
- return new Promise((resolve) => canvas.toBlob(resolve, `${type}/image`));
- }
- // downloadCadImg(canvas, filename) {
- // // 图片导出为 jpg 格式
- // var type = "jpg";
- // var imgData = canvas.toDataURL(type, 3);
- // canvas.toBlob(`${type}/image`);
- // // 加工image data,替换mime type
- // imgData = imgData.replace(this._fixType(type), "image/octet-stream");
- // // 下载后的图片名
- // //var filename = 'cad_' + new Date().getTime() + '.' + type
- // // download
- // this.saveFile(imgData, filename);
- // }
- saveFile(data, filename) {
- var save_link = document.createElementNS(
- "http://www.w3.org/1999/xhtml",
- "a"
- );
- save_link.href = data;
- save_link.download = filename;
- var event = document.createEvent("MouseEvents");
- event.initMouseEvent(
- "click",
- true,
- false,
- window,
- 0,
- 0,
- 0,
- 0,
- 0,
- false,
- false,
- false,
- false,
- 0,
- null
- );
- save_link.dispatchEvent(event);
- }
- _fixType(type) {
- type = type.toLowerCase().replace(/jpg/i, "jpeg");
- var r = type.match(/png|jpeg|bmp|gif/)[0];
- return "image/" + r;
- }
- /****************************************************************************针对菜单*******************************************************************************/
- //撤销
- menu_revoke() {
- this.layer.history.goPreState();
- const historyState = historyService.getHistoryState();
- this.graphicStateUI.canRevoke = historyState.pre;
- this.graphicStateUI.canRecovery = true;
- this.layer.stopAddVector();
- this.layer.renderer.autoRedraw();
- }
- //恢复
- menu_recovery() {
- this.layer.history.goNextState();
- const historyState = historyService.getHistoryState();
- this.graphicStateUI.canRecovery = historyState.next;
- this.graphicStateUI.canRevoke = true;
- this.layer.stopAddVector();
- this.layer.renderer.autoRedraw();
- }
- menu_view_reset() {
- coordinate.reSet(this.layer.canvas);
- this.layer.renderer.autoRedraw();
- }
- // value 为true则开 false则关
- menu_backgroundImg(value) {
- console.log(value);
- //
- const backgroundImg = dataService.getBackgroundImg();
- backgroundImg.setDisplay(value);
- this.graphicStateUI.showBackImage = value;
- this.layer.renderer.autoRedraw();
- }
- menu_clear(isBack) {
- dataService.clear();
- Settings.selectLocationMode = null;
- Settings.baseLineId = null;
- uiService.setSelectBasePointId(null);
- elementService.hideAll();
- this.layer.exit();
- this.layer.initLocation();
- if (!isBack) {
- this.layer.history.save();
- } else {
- historyService.clearHistoryRecord();
- this.layer.uiControl.graphicStateUI.canRevoke = false;
- this.layer.uiControl.graphicStateUI.canRecovery = false;
- }
- this.layer.renderer.autoRedraw();
- }
- /******************************************************************************************************************************************************************/
- // 进入持续添加出确认与取消框
- showConfirm() {
- this.graphicStateUI.continuedMode = true;
- }
- confirmEntry() {
- console.log("确认");
- this.graphicStateUI.continuedMode = false;
- this.layer.exit();
- this.layer.history.save();
- this.layer.renderer.autoRedraw();
- uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
- uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
- }
- confirmCancel() {
- console.log("取消");
- this.graphicStateUI.continuedMode = false;
- this.layer.exit();
- this.layer.history.save();
- this.layer.history.handleUndo();
- this.layer.renderer.autoRedraw();
- uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
- uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
- }
- // 设置默认设置
- setDefaultSetting(setting) {
- console.log("获得设置", setting);
- uiService.setRoadMidDivideWidth(
- setting.roadQuarantineWidth / coordinate.res
- );
- uiService.setCurveRoadMidDivideWidth(
- setting.roadQuarantineWidth / coordinate.res
- );
- Constant.defaultMidDivideWidth = setting.roadQuarantineWidth;
- uiService.setSingleLaneWidth(setting.singleRoadWidth / coordinate.res);
- Constant.defaultSingleLaneWidth = setting.singleRoadWidth;
- uiService.setLineWidth(setting.lineWidth);
- this.layer.renderer.autoRedraw();
- }
- // 获取默认设置
- getDefaultSetting() {
- const singleLaneWidth = Constant.defaultSingleLaneWidth;
- const roadMidDivideWidth = Constant.defaultMidDivideWidth;
- const lineWidth = uiService.getLineWidth();
- return {
- singleRoadWidth: singleLaneWidth,
- roadQuarantineWidth: roadMidDivideWidth,
- lineWidth: lineWidth,
- };
- }
- }
|