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

202 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-07-16 13:34:03 +08:00
import deviceType from './constant/deviceType';
2019-07-09 19:04:45 +08:00
import Eventful from 'zrender/src/mixin/Eventful';
import * as eventTool from 'zrender/src/core/event';
2019-08-07 10:24:13 +08:00
import store from '@/store';
2019-07-09 19:04:45 +08:00
class EventModel {
2019-07-11 17:58:58 +08:00
constructor(e) {
this.clientX = e.event.clientX;
this.clientY = e.event.clientY;
let view = e.target;
while (view) {
if (Object.values(deviceType).includes(view._type)) {
2019-07-25 15:44:23 +08:00
this.deviceCode = view._code;
this.deviceType = view._type;
2019-07-11 17:58:58 +08:00
break;
}
if (view._subType) {
this.subType = view._subType;
}
if (view._val) {
this.val = view._val;
}
view = view.parent;
}
}
2019-07-09 19:04:45 +08:00
}
class MouseController extends Eventful {
2019-07-11 17:58:58 +08:00
constructor(jmap) {
super();
2019-08-05 18:42:37 +08:00
this.$jmap = jmap;
2019-07-16 09:44:31 +08:00
this.$zr = jmap.getZr();
2019-08-05 18:42:37 +08:00
this.events = jmap.getEvents();
2019-07-19 09:40:22 +08:00
this.initHandler(this.$zr);
2019-07-11 17:58:58 +08:00
}
2019-07-19 09:40:22 +08:00
initHandler(zr) {
2019-07-11 17:58:58 +08:00
if (zr) {
2019-07-21 21:22:49 +08:00
zr.on('click', this.click, this);
zr.on('contextmenu', this.contextmenu, this);
zr.on('mousemove', this.moveEvent, this);
2019-07-11 17:58:58 +08:00
this.enable = function (opts) {
opts = opts || {};
this._moveOnMouseMove = opts.moveOnMouseMove || true;
2019-07-26 17:13:03 +08:00
this._zoomOnMouseWheel = opts.zoomOnMouseWheel || false;
2019-07-11 17:58:58 +08:00
this._preventDefaultMouseMove = opts.preventDefaultMouseMove || true;
this.disable();
2019-07-21 21:22:49 +08:00
zr.on('mousedown', this.mousedown, this);
zr.on('mousemove', this.mousemove, this);
zr.on('mouseup', this.mouseup, this);
zr.on('mousewheel', this.mousewheel, this);
2019-07-11 17:58:58 +08:00
};
this.disable = function () {
2019-07-21 21:22:49 +08:00
zr.off('mousedown', this.mousedown);
zr.off('mousemove', this.mousemove);
zr.off('mouseup', this.mouseup);
zr.off('mousewheel', this.mousewheel);
2019-07-11 17:58:58 +08:00
};
this.dispose = function () {
2019-07-21 21:22:49 +08:00
zr.off('click', this.click);
zr.off('contextmenu', this.contextmenu);
zr.off('mousemove', this.moveEvent);
2019-07-11 17:58:58 +08:00
this.disable();
};
this.isDragging = function () { return this._dragging; };
}
}
2019-07-21 18:59:42 +08:00
mousedown(e) {
if (eventTool.notLeftMouse(e)) {
return;
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
var x = e.offsetX;
var y = e.offsetY;
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
this._x = x;
this._y = y;
this._dragging = true;
2019-07-19 15:17:58 +08:00
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
mousemove(e) {
if (eventTool.notLeftMouse(e) ||
!this._moveOnMouseMove ||
!this._dragging
) {
return;
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
const oldX = this._x;
const oldY = this._y;
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
const dx = e.offsetX - oldX;
const dy = e.offsetY - oldY;
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
this._x = e.offsetX;
this._y = e.offsetY;
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
this._preventDefaultMouseMove && eventTool.stop(e.event);
2019-07-22 09:14:56 +08:00
2019-07-25 18:25:04 +08:00
this.trigger(this.events.__Pan, { dx, dy, oldX, oldY, newX: this._x, newY: this._y });
2019-07-19 15:17:58 +08:00
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
mouseup(e) {
if (!eventTool.notLeftMouse(e)) {
this._dragging = false;
}
2019-07-11 17:58:58 +08:00
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
mousewheel(e) {
const shouldZoom = this._zoomOnMouseWheel;
const wheelDelta = e.wheelDelta;
const originX = e.offsetX;
const originY = e.offsetY;
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
if (wheelDelta === 0 || !shouldZoom) {
return;
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
if (shouldZoom) {
eventTool.stop(e.event);
let scale = 1;
if (wheelDelta > 0) {
scale = 1;
} else if (wheelDelta < 0) {
scale = -1;
}
2019-07-22 09:14:56 +08:00
2019-07-25 18:25:04 +08:00
this.trigger(this.events.__Zoom, {type: 'zoom', scale, originX, originY });
2019-07-19 15:17:58 +08:00
}
2019-07-11 17:58:58 +08:00
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
click(e) {
var em = this.checkEvent(e);
this.trigger(this.events.Selected, em);
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
contextmenu(e) {
var em = this.checkEvent(e);
this.trigger(this.events.Contextmenu, em);
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
moveEvent(e) {
2019-08-07 10:24:13 +08:00
const newEm = new EventModel(e);
const trainDetails = store.state.map.trainDetails;
2019-08-07 11:22:33 +08:00
if (trainDetails) {
if (newEm.deviceType != deviceType.Train || trainDetails.code != newEm.deviceCode) {
var instance = (this.$jmap.getDeviceByCode(trainDetails.code) || {} ).instance;
instance && instance.removeTrainDetail && instance.removeTrainDetail();
2019-08-07 10:24:13 +08:00
}
}
2019-07-21 18:59:42 +08:00
}
2019-07-22 09:14:56 +08:00
2019-07-21 18:59:42 +08:00
checkEvent(e) {
2019-08-05 18:42:37 +08:00
var oldEm = new EventModel(this.$zr.curEvent || { event: {} });
var newEm = new EventModel(e);
if ([1, 3].includes(e.which)) {
// 查找之前和当前鼠标选中的实例
2019-08-07 10:24:13 +08:00
var oldDevice = this.$jmap.getDeviceByCode(oldEm.deviceCode) || {};
var newDevice = this.$jmap.getDeviceByCode(newEm.deviceCode) || {};
var oldInstance = (this.$jmap.getDeviceByCode(oldEm.deviceCode) || {}).instance || {};
var newInstance = (this.$jmap.getDeviceByCode(newEm.deviceCode) || {}).instance || {};
2019-08-05 18:42:37 +08:00
// 如果之前和当前选中的实例不一致
2019-08-07 10:24:13 +08:00
if (oldInstance != newInstance) {
2019-08-05 18:42:37 +08:00
// 如果实例有取消选择函数并且被点击,则执行取消选中函数
2019-08-07 10:24:13 +08:00
if (oldInstance.mouseEvent && oldInstance.mouseEvent.mouseout) {
2019-08-05 18:42:37 +08:00
// 视图数据设置点击标志,同步执行
2019-08-07 10:24:13 +08:00
oldDevice['down'] = false;
oldInstance.mouseEvent['mouseout'](e);
2019-08-05 18:42:37 +08:00
}
// 如果实例有选中函数并且被点击,则执行选中函数
2019-08-07 10:24:13 +08:00
if (e.which == 3 && newInstance.mouseEvent && newInstance.mouseEvent.mouseover) {
newDevice['down'] = true;
newInstance.mouseEvent['mouseover'](e);
2019-08-05 18:42:37 +08:00
}
}
// 保存当前实例到全局
this.$zr.curEvent = e;
}
return newEm;
2019-07-21 18:59:42 +08:00
}
2019-07-25 13:24:40 +08:00
updateDatazoom(zoom) {
this.trigger(this.events.Datazoom, zoom);
}
2019-07-09 19:04:45 +08:00
}
2019-07-11 17:58:58 +08:00
export default MouseController;