import * as zrUtil from 'zrender/src/core/util'; import Group from 'zrender/src/container/Group'; import deviceType from './constant/deviceType'; import shapefactory from './shape/factory'; import TransformHandle from './transformHandle'; class Painter { constructor(jmap) { // 父级实例 this.$jmap = jmap; this.$zr = jmap.getZr(); // 图层数据 this.mapInstanceLevel = {}; // 初始图层 this.initLevels(); // 视图控制器 this.$transformHandle = new TransformHandle(this); } /** * 初始绘图实例 * @param {*} dom * @param {*} config */ initLevels() { // 图层分级策略 this.layerBranch = {}; this.layerBranch['01'] = (type) => { return type == deviceType.Link; }; // 逻辑图层级 this.layerBranch['02'] = (type) => { return type != deviceType.Link; }; // 物理图层级 this.layerBranch['03'] = (type) => { return true; }; // 混合图层级 // 添加父级图层 this.parentLevel = new Group({ name: '__parent__' }); this.$zr.add(this.parentLevel); // 添加子级图层 zrUtil.each(Object.values(deviceType), (type) => { const level = new Group({ name: `__${type}__` }); this.mapInstanceLevel[type] = level; this.parentLevel.add(level); }); // 设置默认显示图级 this.setLayerVisible('02'); } /** * 重绘视图 * @param {*} mapDevice */ repaint(mapDevice) { // 清空视图 this.clear(); // 创建视图 Object.values(mapDevice).forEach(device => { this.add(device); }); } /** * 添加视图 * @param {*} device */ add(device) { const type = device._type; const instance = shapefactory(type, device, this.$jmap); if (instance) { device.instance = instance; this.$transformHandle.transformView(instance); this.mapInstanceLevel[type].add(instance); } } /** * 删除视图 * @param {*} device */ delete(device) { const type = device._type; const instance = device.instance; if (instance) { this.mapInstanceLevel[type].remove(instance); } } checkIntersect(device) { var intersect = false; var befor = this.mapInstanceLevel[device.code]; var train = shapefactory(device._type, device, this.$jmap); for (var code in this.mapInstanceLevel) { const elem = this.mapInstanceLevel[code]; if (elem !== befor && elem.getBoundingRect().intersect(train.getBoundingRect())) { intersect = true; break; } } return intersect; } /** * 更新列车 * @param {*} device */ updateTrain(device) { var oldTrainWindowModel = null; var instance = device.instance; var curModel = device.model = device.state; if (instance) { oldTrainWindowModel = device.model.trainWindowModel; } if (curModel.sectionCode) { curModel.sectionModel = this.$jmap.getDeviceByCode(curModel.sectionCode).model; } if (curModel.trainWindowCode) { curModel.trainWindowModel = this.$jmap.getDeviceByCode(curModel.trainWindowCode).model; } if (instance && oldTrainWindowModel && this.checkIntersect(device)) { device.model.trainWindowModel = oldTrainWindowModel; } this.delete(device); this.add(device); } /** * 更新视图 * @param {*} device */ update(device) { if (deviceType.Train == device._type) { this.updateTrain(device); } else { const instance = device.instance; if (instance) { instance.setState(device.state); } } } /** * 更新transform变化 * @param {*} opt */ updateTransform(opt) { this.$transformHandle.updateTransform(opt); } /** * 更新zrender尺寸 * @param {*} opt */ updateZrSize(opt) { this.$transformHandle.updateZrSize(opt); } /** * 设置逻辑和物理图层 * @param {*} layer */ setLayerVisible(layer) { zrUtil.each(Object.values(deviceType), type => { const level = this.mapInstanceLevel[type]; if (this.layerBranch[layer](type)) { level.show(); } else { level.hide(); } }, this); } /** * 设置图层可见 * @param {*} code */ setLevelVisible(list) { zrUtil.each(Object.values(deviceType), type => { const level = this.mapInstanceLevel[type]; if (list.includes(type)) { level.show(); } else { level.hide(); } }, this); } /** * 刷新图层 */ refresh() { this.$zr.refresh(); } /** * 清除图层 */ clearLevel(type) { const level = this.mapInstanceLevel[type]; if (level) { level.removeAll(); } } /** * 清除canvas */ clear() { zrUtil.each(Object.values(this.mapInstanceLevel), (level) => { level && level.removeAll(); }, this); this.refresh(); } /** * 销毁图层 */ dispose() { this.mapInstanceLevel = {}; this.parentLevel = null; } /** * 父级图层 */ getParentLevel() { return this.parentLevel; } } export default Painter;