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

142 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-07-03 14:29:09 +08:00
import zrender from 'zrender';
import Group from 'zrender/src/container/Group';
import deviceType from './config/deviceType';
import shapefactory from './shape/factory';
2019-07-03 18:41:00 +08:00
import deviceStyle from './config/deviceStyle';
const renderer = 'svg';
const devicePixelRatio = 2;
2019-07-03 14:29:09 +08:00
class Painter {
2019-07-03 18:41:00 +08:00
constructor(jamp, opts) {
// 父级实例
2019-07-03 14:29:09 +08:00
this.$jamp = jamp;
2019-07-03 18:41:00 +08:00
// zrender实例
2019-07-03 14:29:09 +08:00
this.$zr = null;
2019-07-03 18:41:00 +08:00
// 皮肤配置
this.styleMap = {};
// 图层数据
this.viewLevelMap = {};
// 视图数据
2019-07-03 14:29:09 +08:00
this.viewInstance = {};
2019-07-03 18:41:00 +08:00
// 父级图层
this.parentLevel = new Group({ name: '__parent__' });
// 挂载视图
this.mount(opts.dom, Object.assign({ renderer, devicePixelRatio, width: opts.dom.width, height: opts.dom.clientHeight }, opts.config));
2019-07-03 14:29:09 +08:00
}
2019-07-03 18:41:00 +08:00
/**
* 挂载视图
* @param {*} dom
* @param {*} config
*/
2019-07-03 14:29:09 +08:00
mount(dom, config) {
2019-07-03 18:41:00 +08:00
// 挂载页面视图
this.$zr = zrender.init(dom, config);
// 添加父级图层
this.$zr.add(this.parentLevel);
// 添加子级图层
2019-07-03 14:29:09 +08:00
Object.values(deviceType).forEach(type => {
2019-07-03 18:41:00 +08:00
let level = new Group({ name: `__${type}__` })
this.viewLevelMap[type] = level;
this.parentLevel.add(level);
2019-07-03 14:29:09 +08:00
})
}
2019-07-03 18:41:00 +08:00
/**
* 初次渲染视图
* @param {*} mapDevice
*/
render(mapDevice, skinStyle) {
// 清空视图
2019-07-03 14:29:09 +08:00
this.viewInstance = {};
2019-07-03 18:41:00 +08:00
// 加载皮肤
this.styleMap = this.loadStyle(skinStyle);
2019-07-03 14:29:09 +08:00
// 清空图层
2019-07-03 18:41:00 +08:00
Object.values(this.viewLevelMap).forEach(level => {
2019-07-03 14:29:09 +08:00
level && level.removeAll();
})
2019-07-03 18:41:00 +08:00
// 创建视图
2019-07-03 14:29:09 +08:00
Object.values(mapDevice).forEach(device => {
2019-07-03 18:41:00 +08:00
device && this.add(device);
2019-07-03 14:29:09 +08:00
})
}
2019-07-03 18:41:00 +08:00
/**
* 加载皮肤
* @param {*} skinStyle
*/
loadStyle(skinStyle) {
return deviceStyle;
}
2019-07-03 14:29:09 +08:00
/**
* 添加视图
* @param {*} device
*/
add(device) {
2019-07-03 18:41:00 +08:00
let type = device._type;
let code = device._code;
let view = shapefactory(type, device, this.styleMap);
2019-07-03 14:29:09 +08:00
if (view) {
this.viewInstance[code] = view;
2019-07-03 18:41:00 +08:00
this.viewLevelMap[type].add(view);
2019-07-03 14:29:09 +08:00
}
}
/**
* 删除视图
* @param {*} device
*/
delete(device) {
2019-07-03 18:41:00 +08:00
let code = device._code;
let type = device._type;
2019-07-03 14:29:09 +08:00
let view = this.viewInstance[code];
if (view) {
2019-07-03 18:41:00 +08:00
this.viewLevelMap[type].remove(view);
2019-07-03 14:29:09 +08:00
delete this.viewInstance[code];
}
}
/**
* 更新视图
* @param {*} device
*/
update(device) {
2019-07-03 18:41:00 +08:00
let code = device._code;
2019-07-03 14:29:09 +08:00
let view = this.viewInstance[code];
if (view) {
2019-07-03 18:41:00 +08:00
view.setStatus(device);
2019-07-03 14:29:09 +08:00
}
}
2019-07-03 18:41:00 +08:00
/**
* 清除所有对象和画布
*/
clear() {
this.$zr && this.$zr.clear();
}
/**
* 销毁 ZRender 实例
*/
dispose() {
this.$zr && zrender.dispose(this.$zr);
this.$zr = null;
this.viewInstance = {};
this.viewLevelMap = {};
}
2019-07-03 14:29:09 +08:00
}
export default Painter;