rt-sim-training-client/src/jmapNew/shape/Train/TrainBody.js
2020-12-15 11:18:15 +08:00

492 lines
24 KiB
JavaScript

import Group from 'zrender/src/container/Group';
import TrainBodyBox from './TrainBodyBox';
// import '../StationStand/EDetain';
import ETextName from '../element/ETextName'; // 名称文字 (共有)
import EMouse from './EMouse';
import store from '@/store/index';
import Circle from 'zrender/src/graphic/shape/Circle';
import BoundingRect from 'zrender/src/core/BoundingRect';
import Vue from 'vue';
/** 车身*/
export default class TrainBody extends Group {
constructor(model) {
super();
this.model = model;
this.deviceModel = model.model;
this.style = model.style;
this.nameFormat = model.nameFormat;
this.create();
this.createMouse(); // 鼠标事件
}
createMouse() {
this.mouseEvent = new EMouse(this);
this.add(this.mouseEvent);
if (this.deviceModel.down) {
store.dispatch('map/setTrainDetails', this.deviceModel);
this.mouseEvent.mouseover();
}
this.on('mouseover', () => {
store.dispatch('map/setTrainDetails', this.deviceModel);
this.mouseEvent.mouseover();
});
this.on('mouseout', () => {
store.dispatch('map/setTrainDetails', null);
this.mouseEvent.mouseout();
});
}
create() {
const model = this.model;
const style = this.model.style;
this.nameFormat = model.nameFormat;
const tripNumber = style.Train.trainTarget.tripNumberPrefix + (style.Train.trainTarget.defaultDirectionCode
? (model.directionCode || style.Train.trainTarget.defaultDirectionCode) + (model.tripNumber || style.Train.trainTarget.defaultTripNumber)
: model.tripNumber || style.Train.trainTarget.defaultTripNumber); // 车次号
const groupNumber = style.Train.trainTargetNumber.groupNumberPrefix + (style.Train.trainTargetNumber.lineNumber || '') + (model.groupNumber || style.Train.trainTargetNumber.defaultGroupNumber); // 车组号
let serviceNumber = ''; // 服务号(表号)
if (this.model.type == 'PLAN') { // 计划车
serviceNumber = style.Train.trainServer.serviceNumberPrefix + (model.serviceNumber || model.model.specialServiceNumber || style.Train.trainServer.defaultServiceNumber);
} else if (this.model.type == 'HEAD') { // 头码车
serviceNumber = style.Train.trainServer.serviceNumberPrefix + (model.destinationCode || style.Train.trainNumber.defaultTargetCode);
this.nameFormat = 'serviceNumber:groupNumber';
} else if (this.model.type == 'MANUAL') { // 人工车
serviceNumber = style.Train.trainServer.serviceNumberPrefix + (model.serviceNumber || model.model.specialServiceNumber || style.Train.trainServer.defaultServiceHeadNum);
this.nameFormat = 'serviceNumber:groupNumber';
}
let targetCode = style.Train.trainNumber.targetCodePrefix + (model.destinationCode || style.Train.trainNumber.defaultTargetCode); // 目的地码
if (Vue.prototype.$jlmap.lineCode == '09') { // 暂时先写死 后面有了删掉就好
targetCode = '01';
}
// 服务号
this.textTrainServer = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + style.Train.trainServer.trainServerOffset.x),
y: parseInt(model.point.y + style.Train.trainServer.trainServerOffset.y),
text: serviceNumber.substring(serviceNumber.length - style.Train.trainServer.serviceNumberPrefix.length),
textFill: model.style.Train.trainServer.fontColor || style.trainTextColor,
fontSize: model.style.Train.trainServer.fontSize || model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
// 车次号
this.textTrainTrip = new ETextName({
zlevel: this.model.zlevel,
z: this.model.z + 1,
x: parseInt(model.point.x + model.style.Train.trainTarget.trainTargetOffset.x),
y: parseInt(model.point.y + model.style.Train.trainTarget.trainTargetOffset.y),
text: tripNumber.substring(tripNumber.length - style.Train.trainTarget.tripNumberPrefix.length),
textFill: model.style.Train.trainTarget.fontColor || style.trainTextColor,
fontSize: model.style.Train.trainTarget.fontSize || model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
this.textTrainTarget = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + style.Train.trainNumber.trainNumberOffset.x),
y: parseInt(model.point.y + style.Train.trainNumber.trainNumberOffset.y),
text: targetCode.substring(targetCode.length - style.Train.trainNumber.targetCodePrefix.length),
textFill: model.style.Train.trainNumber.fontColor || style.trainTextColor,
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.style.Train.trainNumber.fontSize || model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
this.textTrainGroup = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + style.Train.trainTargetNumber.trainTargetNumberOffset.x),
y: parseInt(model.point.y + style.Train.trainTargetNumber.trainTargetNumberOffset.y),
text: groupNumber.substring(groupNumber.length - style.Train.trainTargetNumber.groupNumberPrefix.length),
textFill: style.trainTextColor,
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
if (style.Train.trainTargetNumber.maskText) {
this.textTrainGroupMask = new ETextName({
zlevel: model.zlevel,
z: model.z + 2,
x: parseInt(model.point.x + style.Train.trainTargetNumber.trainTargetNumberOffset.x),
y: parseInt(model.point.y + style.Train.trainTargetNumber.trainTargetNumberOffset.y),
text: style.Train.trainTargetNumber.maskText,
textFill: style.trainTextColor,
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
}
this.formatChangePosition(model, style);
this.trainBodyBox = new TrainBodyBox({
zlevel: model.zlevel,
z: model.z,
point: model.point,
speed: model.speed,
style: style
});
const beginX = (model.point.x + style.Train.trainBody.lrPadding);
const beginY = (model.point.y - style.Train.hsda.trainHSDATextFontSize - style.Train.hsda.upPaddingHSDA);
const margin = (style.Train.common.trainWidth - style.Train.hsda.lrPaddingHSDA * 2) / 4;
this.textH = style.Train.common.haveTextHSDA ? new ETextName({
zlevel: model.zlevel,
z: model.z,
x: parseInt(beginX),
y: parseInt(beginY),
text: style.Train.hsda.textHContent,
textFill: style.trainYellowColor,
fontSize: style.Train.hsda.trainHSDATextFontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'middle',
textVerticalAlign: 'top'
}) : '';
this.textS = style.Train.common.haveTextHSDA ? new ETextName({
zlevel: model.zlevel,
z: model.z,
x: parseInt(beginX + margin),
y: parseInt(beginY),
text: style.Train.hsda.textSContent,
textFill: style.trainBlueColor,
fontSize: style.Train.hsda.trainHSDATextFontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'middle',
textVerticalAlign: 'top'
}) : '';
this.textD = style.Train.common.haveTextHSDA ? new ETextName({
zlevel: model.zlevel,
z: model.z,
x: parseInt(beginX + margin * 2),
y: parseInt(beginY),
text: style.Train.hsda.textDContent,
textFill: style.trainGreenColor,
fontSize: style.Train.hsda.trainHSDATextFontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'middle',
textVerticalAlign: 'top'
}) : '';
this.textA = style.Train.common.haveTextHSDA ? new ETextName({
zlevel: model.zlevel,
z: model.z,
x: parseInt(beginX + margin * 3),
y: parseInt(beginY),
text: style.Train.hsda.textAContent,
textFill: style.trainRedColor,
fontSize: style.Train.hsda.trainHSDATextFontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'middle',
textVerticalAlign: 'top'
}) : '';
this.add(this.trainBodyBox);
if (style.Train.common.haveTextHSDA) {
this.add(this.textH);
this.add(this.textS);
this.add(this.textD);
this.add(this.textA);
}
if (style.Train.common.hasAtrStatus) {
this.atrStatus = new Circle({
zlevel: model.zlevel,
z: model.z + 1,
shape: {
cx: parseInt(model.point.x + style.Train.trainAtrStatus.trainAtrStatusOffset.x),
cy: parseInt(model.point.y + style.Train.trainAtrStatus.trainAtrStatusOffset.y),
r: style.Train.trainAtrStatus.r
},
style: {
fill: '#ff0000'
}
});
// this.add(this.atrStatus);
}
if (style.Train.common.hasTravelSigns) {
const travelSignsOffsetX = this.deviceModel.right ? style.Train.travelSigns.trainTravelRightSignsOffset.x + 4 : style.Train.travelSigns.trainTravelLeftSignsOffset.x;
const travelSignsOffsetY = this.deviceModel.right ? style.Train.travelSigns.trainTravelRightSignsOffset.y : style.Train.travelSigns.trainTravelLeftSignsOffset.y;
this.travelSigns = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + travelSignsOffsetX),
y: parseInt(model.point.y + travelSignsOffsetY),
text: this.deviceModel.right ? '>' : '<',
textFill: '#00C300',
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
this.add(this.travelSigns);
}
if (style.Train.common.hasCrewNum) {
this.crewNum = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + style.Train.crewNum.trainCrewNumOffset.x),
y: parseInt(model.point.y + style.Train.crewNum.trainCrewNumOffset.y),
text: 'BBB',
textFill: style.trainTextColor,
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
this.add(this.crewNum);
}
if (style.Train.common.hasTravelNum) {
this.travelNum = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + style.Train.travelNum.trainTravelNumOffset.x),
y: parseInt(model.point.y + style.Train.travelNum.trainTravelNumOffset.y),
text: model.destinationCode,
textFill: '#00C300',
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
this.add(this.travelNum);
}
if (style.Train.common.hasDelayTime) {
this.delayTime = new ETextName({
zlevel: model.zlevel,
z: model.z + 1,
x: parseInt(model.point.x + style.Train.delayTime.trainDelayTimeOffset.x),
y: parseInt(model.point.y + style.Train.delayTime.trainDelayTimeOffset.y),
text: model.dt > 0 ? '+' + model.dt : model.dt,
textFill: '#DADA00',
textStroke: style.trainTextColor,
textStrokeWidth: 0,
fontSize: model.fontSize,
fontFamily: style.Train.common.fontFamily,
textAlign: 'left',
textVerticalAlign: 'top'
});
this.add(this.delayTime);
}
}
removeTrainDetail() {
this.mouseEvent.mouseout();
store.dispatch('map/setTrainDetails', null);
this.deviceModel.down = false;
}
setTrainColor(color) {
this.trainBodyBox && this.trainBodyBox.setColor('fill', color);
// Style('fill', color);
}
setHShow(isShow) {
if (this.textH) {
isShow ? this.textH.show() : this.textH.hide();
}
if (this.style.Train.common.HSDASpliceShow) {
isShow ? this.trainBodyBox.buckleTrain() : this.trainBodyBox.release();
}
}
setSShow(isShow) {
if (this.textS) {
isShow ? this.textS.show() : this.textS.hide();
}
}
setDShow(isShow) {
if (this.textD) {
isShow ? this.textD.show() : this.textD.hide();
}
}
setAShow(isShow) {
if (this.textA) {
isShow ? this.textA.show() : this.textA.hide();
}
}
setBodyBoxShape(key, color) {
this.trainBodyBox && this.trainBodyBox.setColor(key, color);
}
getBoundingRect() {
if (this.trainBodyBox) {
const tempRect = this.trainBodyBox.getBoundingRect().clone();
return tempRect;
} else {
return new BoundingRect(0, 0, 0, 0);
}
}
getBoundingRectOfFont() { // 获取文字高度
if (this.textTrainServer) {
const tempRect = this.textTrainServer.getBoundingRect().clone();
return tempRect;
} else if (this.textTrainTrip) {
const tempRect = this.textTrainTrip.getBoundingRect().clone();
return tempRect;
} else if (this.textTrainTarget) {
const tempRect = this.textTrainTarget.getBoundingRect().clone();
return tempRect;
} else if (this.textTrainGroup) {
const tempRect = this.textTrainGroup.getBoundingRect().clone();
return tempRect;
} else {
return new BoundingRect(0, 0, 0, 0);
}
}
setPlanRoutingTypeColor(planRoutingTypes) {
if (planRoutingTypes === 'BIG') {
this.style.Train.trainTarget.bigColor && this.textTrainTrip && this.textTrainTrip.setStyle({textFill: this.style.Train.trainTarget.bigColor});
} else if (planRoutingTypes === 'SMALL') {
this.style.Train.trainTarget.smallColor && this.textTrainTrip && this.textTrainTrip.setStyle({textFill: this.style.Train.trainTarget.smallColor});
} else if (planRoutingTypes === 'INBOUND') {
this.style.Train.trainTarget.inboundColor && this.textTrainTrip && this.textTrainTrip.setStyle({textFill: this.style.Train.trainTarget.inboundColor});
}
}
setTrainTypeColor(type) {
if (type === 'PLAN') {
this.style.Train.trainTarget.planTypeColor && this.textTrainTrip && this.textTrainTrip.setStyle({textFill: this.style.Train.trainTarget.planTypeColor});
} else if (type === 'MANUAL') {
this.style.Train.trainTargetNumber.manualTypeColor && this.textTrainGroup && this.textTrainGroup.setStyle({textFill: this.style.Train.trainTargetNumber.manualTypeColor});
this.style.Train.trainNumber.manualTypeColor && this.textTrainTarget && this.textTrainTarget.setStyle({textFill: this.style.Train.trainNumber.manualTypeColor});
this.style.Train.trainServer.manualTypeColor && this.textTrainServer && this.textTrainServer.setStyle({textFill: this.style.Train.trainServer.manualTypeColor});
} else if (type === 'HEAD') {
this.style.Train.trainTargetNumber.headTypeColor && this.textTrainGroup && this.textTrainGroup.setStyle({textFill: this.style.Train.trainTargetNumber.headTypeColor});
this.style.Train.trainNumber.headTypeColor && this.textTrainTarget && this.textTrainTarget.setStyle({textFill: this.style.Train.trainNumber.headTypeColor});
}
}
setSoonerOrLater(dt) {
if (this.style.Train.soonerOrLater && this.style.Train.soonerOrLater.level === 5) {
if (dt > 120) {
this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.severeEarlyColor});
this.delayTime && this.delayTime.setStyle({textFill: this.style.Train.soonerOrLater.severeEarlyColor});
} else if (dt >= 15 && dt <= 120) {
this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.earlyColor});
this.delayTime && this.delayTime.setStyle({textFill: this.style.Train.soonerOrLater.earlyColor});
} else if (dt <= -15 && dt >= -120) {
this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.lateColor});
this.delayTime && this.delayTime.setStyle({textFill: this.style.Train.soonerOrLater.lateColor});
} else if (dt < -120) {
this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.severeLateColor});
this.delayTime && this.delayTime.setStyle({textFill: this.style.Train.soonerOrLater.severeLateColor});
}
} else if (this.style.Train.soonerOrLater && this.style.Train.soonerOrLater.level === 3) {
if (dt > 120) {
this.style.Train.soonerOrLater.serviceNumber && this.textTrainServer && this.textTrainServer.setStyle({textFill: this.style.Train.soonerOrLater.earlyColor});
this.style.Train.soonerOrLater.destinationCode && this.textTrainTarget && this.textTrainTarget.setStyle({textFill: this.style.Train.soonerOrLater.earlyColor});
this.style.Train.soonerOrLater.trainBody && this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.earlyColor});
} else if (dt < -120) {
this.style.Train.soonerOrLater.serviceNumber && this.textTrainServer && this.textTrainServer.setStyle({textFill: this.style.Train.soonerOrLater.lateColor});
this.style.Train.soonerOrLater.destinationCode && this.textTrainTarget && this.textTrainTarget.setStyle({textFill: this.style.Train.soonerOrLater.lateColor});
this.style.Train.soonerOrLater.trainBody && this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.lateColor});
}
} else if (this.style.Train.soonerOrLater && this.style.Train.soonerOrLater.level === 6) {
if (dt <= 59 && dt >= -59) {
this.style.Train.soonerOrLater.trainBody && this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.normalColor});
} else if (dt < -59 && dt >= -300) {
this.style.Train.soonerOrLater.trainBody && this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.smallLateColor});
} else if (dt > 300) {
this.style.Train.soonerOrLater.trainBody && this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.bigEarlyColor});
} else if (dt < -300) {
this.style.Train.soonerOrLater.trainBody && this.trainBodyBox && this.trainBodyBox.setColor({stroke: this.style.Train.soonerOrLater.bigLateColor});
}
}
}
formatChangePosition(model, style) {
if (this.nameFormat) {
const arr = this.nameFormat.split(':');
let widthText = 0;
arr.forEach(ele => {
if (ele == 'targetCode') {
!style.Train.common.fixedCoordinates && this.textTrainTarget.setStyle('x', parseInt(model.point.x + widthText + style.Train.common.textOffset) );
widthText += this.textTrainTarget.getBoundingRect().width;
this.add(this.textTrainTarget);
} else if (ele == 'serviceNumber') {
!style.Train.common.fixedCoordinates && this.textTrainServer.setStyle('x', parseInt(model.point.x + widthText + style.Train.common.textOffset));
widthText += this.textTrainServer.getBoundingRect().width;
this.add(this.textTrainServer);
} else if (ele == 'tripNumber') {
!style.Train.common.fixedCoordinates && this.textTrainTrip.setStyle('x', parseInt(model.point.x + widthText + style.Train.common.textOffset));
widthText += this.textTrainTrip.getBoundingRect().width;
this.add(this.textTrainTrip);
} else if (ele == 'groupNumber') {
!style.Train.common.fixedCoordinates && this.textTrainGroup.setStyle('x', parseInt(model.point.x + widthText + style.Train.common.textOffset));
!style.Train.common.fixedCoordinates && this.textTrainGroupMask && this.textTrainGroupMask.setStyle('x', parseInt(model.point.x + widthText + style.Train.common.textOffset));
widthText += this.textTrainGroup.getBoundingRect().width;
this.add(this.textTrainGroup);
this.textTrainGroupMask && this.add(this.textTrainGroupMask);
}
});
if (style.Train.trainBody.changeTrainWidth) {
style.Train.common.trainWidth = widthText + style.Train.common.trainWidthMoreText;
}
}
}
setPositionText(model, right) {
let widthText = 0;
let point = {
x: model.x - 10,
y: model.y
};
if (right == 1) {
point = {
x: model.x - 20,
y: model.y
};
}
if (this.textTrainTarget) {
this.textTrainTarget.attr({
style: {
x: parseInt(point.x + widthText),
y: parseInt(point.y - this.style.Section.line.width),
textVerticalAlign: 'middle'
}
});
widthText += this.textTrainTarget.getBoundingRect().width;
this.textTrainTarget.dirty();
}
if (this.textTrainServer) {
this.textTrainServer.attr({
style: {
x: parseInt(point.x + widthText),
y: parseInt(point.y - this.style.Section.line.width),
textVerticalAlign: 'middle'
}
});
widthText += this.textTrainServer.getBoundingRect().width;
this.textTrainServer.dirty();
}
if (this.textTrainTrip) {
this.textTrainTrip.attr({
style: {
x: parseInt(point.x + widthText),
y: parseInt(point.y - this.style.Section.line.width),
textVerticalAlign: 'middle'
}
});
widthText += this.textTrainTrip.getBoundingRect().width;
this.textTrainTrip.dirty();
}
if (this.textTrainGroup) {
this.textTrainGroup.attr({
style: {
x: parseInt(point.x + widthText),
y: parseInt(point.y - this.style.Section.line.width),
textVerticalAlign: 'middle'
}
});
widthText += this.textTrainGroup.getBoundingRect().width;
this.textTrainGroup.dirty();
}
}
}