2019-11-29 12:51:58 +08:00
|
|
|
import * as zrUtil from 'zrender/src/core/util';
|
|
|
|
import * as vector from 'zrender/src/core/vector';
|
|
|
|
import Group from 'zrender/src/container/Group';
|
|
|
|
import deviceType from './constant/deviceType';
|
2020-02-11 16:16:04 +08:00
|
|
|
import transitionDeviceStatus from './constant/stateTransition';
|
2019-11-29 12:51:58 +08:00
|
|
|
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.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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 重绘视图
|
|
|
|
* @param {*} mapDevice
|
|
|
|
*/
|
|
|
|
repaint(mapDevice) {
|
|
|
|
// 清空视图
|
|
|
|
this.clear();
|
|
|
|
|
|
|
|
// 创建视图
|
|
|
|
Object.values(mapDevice).forEach(device => {
|
|
|
|
this.add(device);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加视图
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
|
|
|
add(device) {
|
|
|
|
try {
|
|
|
|
const instance = shapefactory(device, this.$jmap);
|
|
|
|
if (instance) {
|
|
|
|
device.instance = instance;
|
|
|
|
this.$transformHandle.transformView(instance);
|
|
|
|
this.mapInstanceLevel[device._type].add(instance);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除视图
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
|
|
|
delete(device) {
|
|
|
|
const instance = device.instance;
|
|
|
|
if (instance) {
|
|
|
|
this.mapInstanceLevel[device._type].remove(instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIntersect(device) {
|
|
|
|
var intersect = false;
|
|
|
|
var befor = device.instance;
|
|
|
|
var train = shapefactory(device, this.$jmap);
|
|
|
|
|
|
|
|
this.mapInstanceLevel[deviceType.Train].eachChild(elem => {
|
|
|
|
if (elem !== befor && elem.getBoundingRect().intersect(train.getBoundingRect())) {
|
|
|
|
intersect = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return intersect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新列车
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
|
|
|
updateTrain(device) {
|
|
|
|
var oldTrainWindowModel = null;
|
|
|
|
var instance = device.instance;
|
|
|
|
var curModel = device;
|
2020-02-14 18:07:54 +08:00
|
|
|
let displayPosition = 'margin';
|
2019-11-29 12:51:58 +08:00
|
|
|
if (instance) {
|
|
|
|
oldTrainWindowModel = device.trainWindowModel;
|
2020-02-14 18:07:54 +08:00
|
|
|
displayPosition = instance.style.Train.common.displayPosition;
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
if (curModel.sectionCode) {
|
|
|
|
curModel.sectionModel = this.$jmap.getDeviceByCode(curModel.sectionCode);
|
2020-02-13 13:12:22 +08:00
|
|
|
if (curModel.sectionModel.trainWindowCode) {
|
|
|
|
curModel.trainWindowCode = curModel.sectionModel.trainWindowCode;
|
2020-02-14 18:07:54 +08:00
|
|
|
} else if (displayPosition === 'center' && curModel.sectionModel.logicSectionCodeList && curModel.sectionModel.logicSectionCodeList.length) {
|
|
|
|
const sec = this.$jmap.getDeviceByCode(curModel.sectionModel.logicSectionCodeList[Math.floor(curModel.sectionModel.logicSectionCodeList.length / 2)]);
|
|
|
|
curModel.trainWindowCode = sec.trainWindowCode;
|
2020-02-13 13:12:22 +08:00
|
|
|
} else if (curModel.right && curModel.sectionModel.logicSectionCodeList && curModel.sectionModel.logicSectionCodeList.length) {
|
|
|
|
const sec = this.$jmap.getDeviceByCode(curModel.sectionModel.logicSectionCodeList[curModel.sectionModel.logicSectionCodeList.length - 1]);
|
|
|
|
curModel.trainWindowCode = sec.trainWindowCode;
|
|
|
|
} else if (!curModel.right && curModel.sectionModel.logicSectionCodeList && curModel.sectionModel.logicSectionCodeList.length) {
|
|
|
|
const sec = this.$jmap.getDeviceByCode(curModel.sectionModel.logicSectionCodeList[0]);
|
|
|
|
curModel.trainWindowCode = sec.trainWindowCode;
|
|
|
|
}
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (curModel.trainWindowCode) {
|
|
|
|
curModel.trainWindowModel = this.$jmap.getDeviceByCode(curModel.trainWindowCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance && oldTrainWindowModel && this.checkIntersect(device)) {
|
|
|
|
device.trainWindowModel = oldTrainWindowModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance && this.mapInstanceLevel[deviceType.Train].remove(instance);
|
|
|
|
this.add(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新视图
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
|
|
|
update(device) {
|
|
|
|
if (device) {
|
|
|
|
try {
|
|
|
|
if (device._dispose) {
|
|
|
|
this.delete(device);
|
2020-03-06 15:27:31 +08:00
|
|
|
} else if (deviceType.Train.toUpperCase() == device.deviceType) {
|
2019-11-29 12:51:58 +08:00
|
|
|
this.updateTrain(device);
|
|
|
|
} else {
|
|
|
|
const instance = device.instance;
|
|
|
|
if (instance) {
|
2020-02-11 16:16:04 +08:00
|
|
|
instance.setState(transitionDeviceStatus(device)); // 改变试图状态
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 15:10:08 +08:00
|
|
|
/**
|
|
|
|
* 更新显示模式
|
|
|
|
*/
|
|
|
|
updateShowMode(device) {
|
|
|
|
if (device && device.instance) {
|
|
|
|
device.instance.setShowMode();
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 12:51:58 +08:00
|
|
|
/**
|
|
|
|
* 更新transform变化
|
|
|
|
* @param {*} opt
|
|
|
|
*/
|
|
|
|
updateTransform(opt) {
|
|
|
|
this.$transformHandle.updateTransform(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新zrender尺寸
|
|
|
|
* @param {*} opt
|
|
|
|
*/
|
|
|
|
updateZrSize(opt) {
|
|
|
|
this.$transformHandle.updateZrSize(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 过去坐标提示位置
|
|
|
|
* @param {*} opts
|
|
|
|
*/
|
|
|
|
getShapeTipPoint(instance, opts) {
|
|
|
|
if (instance) {
|
|
|
|
var point = instance.getShapeTipPoint(opts);
|
|
|
|
if (point) {
|
|
|
|
// 矩阵变换
|
|
|
|
var transform = this.$transformHandle.transform;
|
|
|
|
var transPoint = vector.applyTransform([], [point.x, point.y], transform);
|
|
|
|
return {
|
|
|
|
x: transPoint[0],
|
|
|
|
y: transPoint[1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置图层可见
|
|
|
|
* @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;
|