import { coordinate } from '../Coordinate.js' import LayerEvents from '../enum/LayerEvents.js' import UIEvents from '../enum/UIEvents.js' import VectorType from '../enum/VectorType.js' import { stateService } from '../Service/StateService.js' import { floorplanService } from '../Service/FloorplanService.js' import { historyService } from '../Service/HistoryService.js' import { elementService } from '../Service/ElementService' import { mathUtil } from '../MathUtil.js' import { wallService } from '../Service/WallService.js' import { tagService } from '../Service/TagService.js' import Constant from '../Constant' import { addWall } from '../Controls/AddWall' import { floorplanData } from '../FloorplanData.js' import { signService } from '../Service/SignService.js' export default class UIControl { constructor(layer) { this.layer = layer this.selectUI = null; } //点击左侧栏后,更新事件 updateEventNameForSelectUI() { elementService.hideAll() //正在添加tag的时候,需要先删除 const eventName = stateService.getEventName() // if (eventName == LayerEvents.AddTag) { // let item = stateService.getDraggingItem() // if (item && item.type == VectorType.Tag) { // floorplanService.deleteTag(item.vectorId) // } // } // stateService.clearItems() if (this.selectUI == UIEvents.Wall) { stateService.setEventName(LayerEvents.AddWall) } else if (this.selectUI == UIEvents.Table ) { stateService.setEventName(LayerEvents.Table) } else if (this.selectUI == UIEvents.Rectangle ) { stateService.setEventName(LayerEvents.Rectangle) } else if (this.selectUI == UIEvents.Circle ) { stateService.setEventName(LayerEvents.Circle) } else if (this.selectUI == UIEvents.Arrow ) { stateService.setEventName(LayerEvents.Arrow) } else if (this.selectUI == UIEvents.Icon ) { stateService.setEventName(LayerEvents.Icon) } else if (this.selectUI == UIEvents.Tag) { stateService.setEventName(LayerEvents.AddTag) } else if ( this.selectUI == UIEvents.Cigaret || this.selectUI == UIEvents.FirePoint || this.selectUI == UIEvents.FootPrint || this.selectUI == UIEvents.ShoePrint || this.selectUI == UIEvents.FingerPrint || this.selectUI == UIEvents.DeadBody || this.selectUI == UIEvents.BloodStain ) { stateService.setEventName(LayerEvents.AddSign) } } /** * 设置部件属性 * @param {*} type 部件类型 * @param {*} name 属性名称 * @param {*} value 属性值 */ setAttributes(type, name, value) { console.log(name) let item = stateService.getFocusItem() switch (name) { case 'remove': if (type == VectorType.Wall) { floorplanService.deleteWall(item.vectorId) } else if (type == VectorType.Tag) { floorplanService.deleteTag(item.vectorId) let _item = stateService.getDraggingItem() if (_item && _item.vectorId != item.vectorId) { floorplanService.deleteTag(_item.vectorId) } } else if (type == VectorType.WallCorner) { wallService.deleteWallCorner(item.vectorId) } else if (signService.isSign(type)) { floorplanService.deleteSign(item.vectorId) } stateService.clearItems() stateService.clearEventName() this.layer.renderer.autoRedraw() break case 'angle': if (signService.isSign(type)) { let sign = floorplanService.getSign(item.vectorId) sign.angle = value this.layer.renderer.autoRedraw() } break case 'scale': if (signService.isSign(type)) { let sign = floorplanService.getSign(item.vectorId) sign.scale = value this.layer.renderer.autoRedraw() } break case 'split': if (type == VectorType.Wall || type == 'OutWall') { let wall = floorplanService.getWall(item.vectorId) let startPoint = floorplanService.getPoint(wall.start) let endPoint = floorplanService.getPoint(wall.end) let mid = { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, } let point = wallService.createPoint(mid.x, mid.y) const _wallId = wallService.splitWall(item.vectorId, point.vectorId, 'start') wall = floorplanService.getWall(item.vectorId) startPoint = floorplanService.getPoint(wall.start) endPoint = floorplanService.getPoint(wall.end) let _mid = { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, } let focusItem = { vectorId: _wallId, type: type, } if (mid.x < _mid.x) { stateService.setFocusItem(focusItem) } else if (mid.x == _mid.x && mid.y > _mid.y) { stateService.setFocusItem(focusItem) } this.layer.renderer.autoRedraw() } break case 'tag': if (type == VectorType.Tag) { let tag = floorplanService.getTag(item.vectorId) if (value != null && value.trim() != '') { tag.setTitle(value) } else { tag.setTitle(KanKan.Config.i18n('cad.input')) } //this.layer.history.save() this.layer.renderer.autoRedraw() } break } } saveAttributes(type, name) { if (name == 'compass' || (type == 'compass' && name == 'default')) { return } this.layer.history.save() } showAttributes(type) { let item = stateService.getFocusItem() if (item == null) { item = stateService.getDraggingItem() } if (item.type == VectorType.Tag) { let tag = floorplanService.getTag(item.vectorId) this.layer.$xui.currentAttributes = { name: item.type, tag: tag.title, area: tag.des, } } else if (item.type == VectorType.Wall) { let wall = floorplanService.getWall(item.vectorId) this.layer.$xui.currentAttributes = { name: wall.out ? 'OutWall' : item.type, important: wall.important || wall.out, wallType: wall.out ? 'OutWall' : item.type, } } else if (signService.isSign(item.type)) { let sign = floorplanService.getSign(item.vectorId) let scale = sign.scale this.layer.$xui.currentAttributes = { name: item.type, scale: scale, angle: sign.angle, } } } clearUI() { this.selectUI = null } getSignTypeForUI() { if (this.selectUI == UIEvents.Cigaret) { return VectorType.Cigaret } else if (this.selectUI == UIEvents.FirePoint) { return VectorType.FirePoint } else if (this.selectUI == UIEvents.FootPrint) { return VectorType.FootPrint } else if (this.selectUI == UIEvents.ShoePrint) { return VectorType.ShoePrint } else if (this.selectUI == UIEvents.FingerPrint) { return VectorType.FingerPrint } else if (this.selectUI == UIEvents.DeadBody) { return VectorType.DeadBody } else if (this.selectUI == UIEvents.BloodStain) { return VectorType.BloodStain } } //截图 screenShot(styleType) { return new Promise(resolve => { this.menu_flex() stateService.clearItems() let canvas = this.layer.canvas canvas.width = window.innerWidth * Constant.ratio canvas.height = window.innerHeight * Constant.ratio coordinate.width = window.innerWidth coordinate.height = window.innerHeight coordinate.ratio = Constant.ratio let floorplanCamera = this.layer.app.core.get('CameraControls').activeControl.camera coordinate.res = Math.min(window.innerWidth / Math.abs(floorplanCamera.right - floorplanCamera.left), window.innerHeight / Math.abs(floorplanCamera.top - floorplanCamera.bottom)) this.layer.renderer.autoRedrawForDownLoadImg(styleType) setTimeout( function () { let subFloor = floorplanService.getFloorNum() let file = [] if (subFloor == 1) { this.downloadCadImg(canvas, 'floorPlan.jpg') } else { let currentFloor = floorplanService.getCurrentFloor() this.layer.app.store.getValue('floorcad').floors.forEach((floor, index) => { this.currentFloor = index this.layer.renderer.autoRedrawForDownLoadImg(styleType) this.downloadCadImg(canvas, floor.name + '.jpg') }) this.currentFloor = currentFloor } coordinate.updateForCanvas(canvas) coordinate.setRes(floorplanCamera.left, floorplanCamera.right) coordinate.ratio = 1 //this.layer.renderer.autoRedrawForDownLoadImg(styleType) this.layer.renderer.autoRedraw() resolve() }.bind(this), 100 ) }) } downloadCadImg(canvas, filename) { // 图片导出为 jpg 格式 var type = 'jpg' var imgData = canvas.toDataURL(type, 3) // 加工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 } /****************************************************************************针对菜单*******************************************************************************/ execute(name, value) { stateService.clearFocusItem() stateService.clearSelectItem() //this.layer.$xui.hideProps() this.layer.uiControl.currentUI = null switch (name) { case 'recall': //撤销 this.menu_revoke() break case 'recover': //恢复 this.menu_recovery() break case 'default': //恢复默认 this.menu_default() break case 'download': //下载 this.menu_screenShot(value) break case 'texture': //底图 this.showTexture = value this.layer.app.dom.querySelector('.player[name="main"]').style.visibility = this.showTexture ? 'visible' : 'hidden' break case 'clear': //清空 this.menu_clear() break case 'panos': //漫游点 this.menu_panos(value) break case 'rotate': //旋转 this.menu_rotate() break case 'flex': //适应视图 this.menu_flex() break } } //撤销 menu_revoke() { this.layer.revokeHistory() } //恢复 menu_recovery() { this.layer.recoveryHistory() } menu_default() { historyService.clearHistoryRecord() floorplanService.setAngle(0) this.layer.load.loadFloorJson(true).then(() => { }) } //截图 menu_screenShot(styleType) { this.layer.stopAddVector() return this.screenShot(styleType) } //清空 menu_clear() { floorplanService.clear() stateService.clearEventName() addWall.clear() elementService.hideAll() this.layer.history.save() this.layer.renderer.autoRedraw() } menu_rotate() { coordinate.reSet() let angle = floorplanService.getAngle() angle = parseFloat(angle) + Math.PI / 2 if (Math.abs(angle - 2 * Math.PI) < 0.1) { angle = 0 } angle = angle % 360 floorplanService.setAngle(angle) //旋转三维模型 let info = coordinate.getScreenInfoForCAD() info.floorPlanAngle = angle this.layer.app.store.getValue('metadata').floorPlanAngle = angle this.layer.app.core.get('CameraControls').emit('syncCadAnd3DForRotate', info) let floorplanCamera = this.layer.app.core.get('CameraControls').activeControl.camera coordinate.setRes(floorplanCamera.left, floorplanCamera.right) //修改cad数据 coordinate.updateForRotate() this.layer.initPanos(floorplanService.getCurrentFloor()) this.layer.history.save() this.layer.renderer.autoRedraw() } menu_flex(updateCadRes) { if (coordinate.center) { coordinate.reSet() this.layer.renderer.autoRedraw() let info = coordinate.getScreenInfoForCAD() info.floorPlanAngle = floorplanService.getAngle() this.layer.app.core.get('CameraControls').emit('syncCadAnd3D', info) // info = {width:..,height:...,center:...} if (updateCadRes) { let floorplanCamera = this.layer.app.core.get('CameraControls').activeControl.camera coordinate.setRes(floorplanCamera.left, floorplanCamera.right) } } } /******************************************************************************************************************************************************************/ }