rt-sim-training-client/src/jmap/painter.js

157 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-07-08 09:38:39 +08:00
import * as zrUtil from 'zrender/src/core/util';
2019-07-03 14:29:09 +08:00
import Group from 'zrender/src/container/Group';
import deviceType from './config/deviceType';
import shapefactory from './shape/factory';
2019-07-09 19:04:45 +08:00
import ZoomHandle from './zoomHandle';
2019-07-03 14:29:09 +08:00
class Painter {
2019-07-10 13:17:12 +08:00
constructor(jmap) {
2019-07-04 18:39:30 +08:00
// 父级实例
2019-07-10 13:17:12 +08:00
this.$jmap = jmap;
// 视图控制器
this.$zoomHandle = new ZoomHandle(this, jmap.getZr());
2019-07-03 14:29:09 +08:00
2019-07-04 18:39:30 +08:00
// 图层数据
this.viewLevelMap = {};
2019-07-03 18:41:00 +08:00
2019-07-04 18:39:30 +08:00
// 视图数据
this.viewInstance = {};
2019-07-03 18:41:00 +08:00
2019-07-04 18:39:30 +08:00
// 父级图层
2019-07-05 00:40:53 +08:00
this.parentLevel = null;
2019-07-09 19:04:45 +08:00
// 初始绘图实例
this.initPainterInstance();
2019-07-04 18:39:30 +08:00
}
2019-07-03 14:29:09 +08:00
2019-07-04 10:59:40 +08:00
/**
2019-07-09 19:04:45 +08:00
* 初始绘图实例
2019-07-04 10:59:40 +08:00
* @param {*} dom
* @param {*} config
2019-07-03 18:41:00 +08:00
*/
2019-07-09 19:04:45 +08:00
initPainterInstance() {
2019-07-04 18:39:30 +08:00
// 添加父级图层
2019-07-05 00:40:53 +08:00
this.parentLevel = new Group({ name: '__parent__' });
2019-07-10 13:17:12 +08:00
this.$jmap.getZr().add(this.parentLevel);
2019-07-04 10:59:40 +08:00
2019-07-04 18:39:30 +08:00
// 添加子级图层
2019-07-08 09:38:39 +08:00
zrUtil.each(Object.values(deviceType), (type) => {
2019-07-04 18:39:30 +08:00
const level = new Group({ name: `__${type}__` });
this.viewLevelMap[type] = level;
this.parentLevel.add(level);
});
}
2019-07-04 10:59:40 +08:00
/**
2019-07-04 18:39:30 +08:00
* 重绘视图
2019-07-04 10:59:40 +08:00
* @param {*} mapDevice
2019-07-03 18:41:00 +08:00
*/
2019-07-05 00:40:53 +08:00
repaint(mapDevice) {
2019-07-04 18:39:30 +08:00
// 清空视图
this.clear();
// 创建视图
Object.values(mapDevice).forEach(device => {
device && this.add(device);
});
}
2019-07-04 10:59:40 +08:00
/**
2019-07-03 14:29:09 +08:00
* 添加视图
2019-07-04 10:59:40 +08:00
* @param {*} device
2019-07-03 14:29:09 +08:00
*/
2019-07-04 18:39:30 +08:00
add(device) {
const type = device._type;
const code = device._code;
2019-07-10 13:17:12 +08:00
const view = shapefactory(type, device, this.$jmap.getStyleDict());
2019-07-04 18:39:30 +08:00
if (view) {
2019-07-10 13:17:12 +08:00
this.$zoomHandle.transformView(view);
2019-07-04 18:39:30 +08:00
this.viewInstance[code] = view;
this.viewLevelMap[type].add(view);
}
}
2019-07-04 10:59:40 +08:00
/**
2019-07-03 14:29:09 +08:00
* 删除视图
2019-07-04 10:59:40 +08:00
* @param {*} device
2019-07-03 14:29:09 +08:00
*/
2019-07-04 18:39:30 +08:00
delete(device) {
const code = device._code;
const type = device._type;
const view = this.viewInstance[code];
if (view) {
this.viewLevelMap[type].remove(view);
delete this.viewInstance[code];
}
}
2019-07-04 10:59:40 +08:00
/**
2019-07-03 14:29:09 +08:00
* 更新视图
2019-07-04 10:59:40 +08:00
* @param {*} device
2019-07-03 14:29:09 +08:00
*/
2019-07-04 18:39:30 +08:00
update(device) {
const code = device._code;
const view = this.viewInstance[code];
if (view) {
view.setState(device);
}
}
2019-07-05 00:40:53 +08:00
/**
* 更新zoom
2019-07-09 09:24:54 +08:00
* @param {*} zoom
2019-07-05 00:40:53 +08:00
*/
2019-07-09 09:24:54 +08:00
updateZoomTransform(zoom) {
2019-07-10 13:17:12 +08:00
this.$zoomHandle.updateTransform(zoom);
2019-07-05 00:40:53 +08:00
}
2019-07-08 09:38:39 +08:00
2019-07-05 00:40:53 +08:00
/**
* 视图是否存在
* @param {*} code
*/
hasViewInstance(code) {
return this.viewInstance[code];
}
2019-07-04 18:39:30 +08:00
/**
* 刷新画布将在下一个渲染帧的时候被刷新
*/
refresh() {
2019-07-10 13:17:12 +08:00
this.$jmap.getZr() && this.$jmap.getZr().refresh();
2019-07-04 18:39:30 +08:00
}
2019-07-04 10:59:40 +08:00
/**
2019-07-03 18:41:00 +08:00
* 清除所有对象和画布
*/
2019-07-04 18:39:30 +08:00
clear() {
// 清空视图
this.viewInstance = {};
// 清空图层
2019-07-08 09:38:39 +08:00
zrUtil.each(Object.values(this.viewLevelMap), (level) => {
2019-07-04 18:39:30 +08:00
level && level.removeAll();
2019-07-08 09:38:39 +08:00
})
2019-07-04 18:39:30 +08:00
this.refresh();
}
2019-07-03 18:41:00 +08:00
2019-07-04 10:59:40 +08:00
/**
2019-07-03 18:41:00 +08:00
* 销毁 ZRender 实例
*/
2019-07-04 18:39:30 +08:00
dispose() {
this.viewInstance = {};
this.viewLevelMap = {};
2019-07-09 19:04:45 +08:00
this.parentLevel = null;
2019-07-04 18:39:30 +08:00
}
2019-07-10 12:02:02 +08:00
/**
* 获取父级图层
*/
getParentLevel() {
return this.parentLevel;
}
2019-07-03 14:29:09 +08:00
}
2019-07-04 10:59:40 +08:00
export default Painter;