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

161 lines
2.9 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';
2019-07-16 13:34:03 +08:00
import deviceType from './constant/deviceType';
2019-07-03 14:29:09 +08:00
import shapefactory from './shape/factory';
2019-07-19 09:23:56 +08:00
import TransformHandle from './transformHandle';
2019-07-03 14:29:09 +08:00
class Painter {
2019-07-11 17:58:58 +08:00
constructor(jmap) {
// 父级实例
this.$jmap = jmap;
2019-07-16 09:44:31 +08:00
this.$zr = jmap.getZr();
2019-07-10 13:17:12 +08:00
2019-07-11 17:58:58 +08:00
// 视图控制器
2019-07-19 09:23:56 +08:00
this.$transformHandle = new TransformHandle(this, this.$zr);
2019-07-03 14:29:09 +08:00
2019-07-11 17:58:58 +08:00
// 图层数据
2019-07-16 12:01:43 +08:00
this.mapInstanceLevel = {};
2019-07-03 18:41:00 +08:00
2019-07-11 17:58:58 +08:00
// 父级图层
this.parentLevel = null;
2019-07-05 00:40:53 +08:00
2019-07-19 11:11:34 +08:00
// 初始图层
this.initPainterLevels();
2019-07-11 17:58:58 +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-19 11:11:34 +08:00
initPainterLevels() {
2019-07-11 17:58:58 +08:00
// 添加父级图层
this.parentLevel = new Group({ name: '__parent__' });
2019-07-16 09:44:31 +08:00
this.$zr.add(this.parentLevel);
2019-07-11 17:58:58 +08:00
// 添加子级图层
zrUtil.each(Object.values(deviceType), (type) => {
const level = new Group({ name: `__${type}__` });
2019-07-16 12:01:43 +08:00
this.mapInstanceLevel[type] = level;
2019-07-11 17:58:58 +08:00
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-11 17:58:58 +08:00
repaint(mapDevice) {
// 清空视图
this.clear();
2019-07-04 18:39:30 +08:00
2019-07-11 17:58:58 +08:00
// 创建视图
Object.values(mapDevice).forEach(device => {
2019-07-17 12:17:41 +08:00
this.add(device);
2019-07-11 17:58:58 +08:00
});
}
2019-07-04 18:39:30 +08:00
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-11 17:58:58 +08:00
add(device) {
const type = device._type;
2019-07-16 12:01:43 +08:00
const instance = shapefactory(type, device, this.$jmap);
if (instance) {
2019-07-19 09:23:56 +08:00
this.$transformHandle.transformView(instance);
2019-07-16 12:01:43 +08:00
device.instance = instance;
this.mapInstanceLevel[type].add(instance);
2019-07-11 17:58:58 +08:00
}
}
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-11 17:58:58 +08:00
delete(device) {
const type = device._type;
2019-07-16 12:01:43 +08:00
const instance = device.instance;
if (instance) {
this.mapInstanceLevel[type].remove(instance);
2019-07-11 17:58:58 +08:00
}
}
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-11 17:58:58 +08:00
update(device) {
2019-07-16 12:01:43 +08:00
const instance = device.instance;
if (instance) {
instance.setState(device.state);
2019-07-11 17:58:58 +08:00
}
}
/**
2019-07-19 11:11:34 +08:00
* 更新transform变化
* @param {*} opt
2019-07-05 00:40:53 +08:00
*/
2019-07-19 11:11:34 +08:00
updateTransform(opt) {
this.$transformHandle.updateTransform(opt);
}
/**
* 更新zrender尺寸
* @param {*} opt
*/
updateZrSize(opt) {
this.$transformHandle.updateZrSize(opt);
2019-07-11 17:58:58 +08:00
}
2019-07-08 09:38:39 +08:00
2019-07-15 14:14:44 +08:00
/**
2019-07-19 09:40:22 +08:00
* 设置图层可见
2019-07-15 14:14:44 +08:00
* @param {*} code
*/
2019-07-19 09:40:22 +08:00
setLevelVisible(list, show) {
2019-07-15 14:14:44 +08:00
zrUtil.each(Object.values(deviceType), type => {
2019-07-16 12:01:43 +08:00
const level = this.mapInstanceLevel[type];
2019-07-15 14:14:44 +08:00
if (list.includes(type)) {
2019-07-19 09:40:22 +08:00
show ? level.show() : level.hide();
2019-07-15 14:14:44 +08:00
} else {
2019-07-19 09:40:22 +08:00
show ? level.hide() : level.show();
2019-07-15 14:14:44 +08:00
}
});
}
2019-07-11 17:58:58 +08:00
/**
2019-07-16 12:01:43 +08:00
* 刷新图层
2019-07-04 18:39:30 +08:00
*/
2019-07-11 17:58:58 +08:00
refresh() {
2019-07-16 09:44:31 +08:00
this.$zr && this.$zr.refresh();
2019-07-11 17:58:58 +08:00
}
2019-07-04 10:59:40 +08:00
/**
2019-07-16 12:01:43 +08:00
* 清除图层
2019-07-03 18:41:00 +08:00
*/
2019-07-11 17:58:58 +08:00
clear() {
2019-07-16 12:01:43 +08:00
zrUtil.each(Object.values(this.mapInstanceLevel), (level) => {
2019-07-11 17:58:58 +08:00
level && level.removeAll();
});
2019-07-04 18:39:30 +08:00
2019-07-11 17:58:58 +08:00
this.refresh();
}
2019-07-03 18:41:00 +08:00
2019-07-04 10:59:40 +08:00
/**
2019-07-16 12:01:43 +08:00
* 销毁图层
2019-07-03 18:41:00 +08:00
*/
2019-07-11 17:58:58 +08:00
dispose() {
2019-07-16 12:01:43 +08:00
this.mapInstanceLevel = {};
2019-07-11 17:58:58 +08:00
this.parentLevel = null;
}
2019-07-10 12:02:02 +08:00
2019-07-11 17:58:58 +08:00
/**
2019-07-16 12:01:43 +08:00
* 父级图层
2019-07-10 12:02:02 +08:00
*/
2019-07-11 17:58:58 +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;