rts-sim-testing-client/src/graphics/axleCountingSection/AxleCountingSection.ts

132 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-07-07 17:15:53 +08:00
import { Graphics, IPointData } from 'pixi.js';
import {
GraphicData,
GraphicRelationParam,
JlGraphic,
JlGraphicTemplate,
VectorText,
} from 'src/jl-graphic';
import { IRelatedRefData, protoPort2Data } from '../CommonGraphics';
import { SectionPort } from '../section/Section';
2023-07-11 15:35:35 +08:00
export interface ITurnoutPosRefData {
get id(): string; //道岔的ID
set id(v: string);
get position(): number; //道岔的正反为0是正位1是反位
set position(v: number);
}
2023-07-07 17:15:53 +08:00
export interface IAxleCountingSectionData extends GraphicData {
get code(): string; // 编号
set code(v: string);
get points(): IPointData[]; // 线坐标点
set points(points: IPointData[]);
2023-07-11 15:35:35 +08:00
get paRef(): IRelatedRefData | undefined; //区段A端关联的设备
2023-07-07 17:15:53 +08:00
set paRef(ref: IRelatedRefData | undefined);
2023-07-11 15:35:35 +08:00
get pbRef(): IRelatedRefData | undefined; //区段B端关联的设备
2023-07-07 17:15:53 +08:00
set pbRef(ref: IRelatedRefData | undefined);
2023-07-11 15:35:35 +08:00
get turnoutPosRef(): ITurnoutPosRefData[]; //关联道岔的正反位
set turnoutPosRef(ref: ITurnoutPosRefData[]);
2023-07-07 17:15:53 +08:00
clone(): IAxleCountingSectionData;
copyFrom(data: IAxleCountingSectionData): void;
eq(other: IAxleCountingSectionData): boolean;
}
export const AxleCountingSectionConsts = {
lineColor: '0xff0000',
lineWidth: 2,
};
export class AxleCountingSection extends JlGraphic {
static Type = 'AxleCountingSection';
lineGraphic: Graphics;
labelGraphic: VectorText;
constructor() {
super(AxleCountingSection.Type);
this.lineGraphic = new Graphics();
this.labelGraphic = new VectorText();
this.labelGraphic.setVectorFontSize(14);
this.labelGraphic.anchor.set(0.5);
this.labelGraphic.style.fill = '#0f0';
this.labelGraphic.transformSave = true;
this.labelGraphic.name = 'label';
this.transformSave = true;
this.addChild(this.lineGraphic);
this.addChild(this.labelGraphic);
}
get datas(): IAxleCountingSectionData {
return this.getDatas<IAxleCountingSectionData>();
}
doRepaint(): void {
if (this.datas.points.length < 2) {
throw new Error('AxleCountingSection坐标数据异常');
}
this.lineGraphic.clear();
this.lineGraphic.lineStyle(
AxleCountingSectionConsts.lineWidth,
AxleCountingSectionConsts.lineColor
);
this.datas.points.forEach((p, i) => {
if (i !== 0) {
this.lineGraphic.lineTo(p.x, p.y);
} else {
this.lineGraphic.moveTo(p.x, p.y);
}
});
this.labelGraphic.text = this.datas.code;
const labelPosition = this.datas.childTransforms?.find(
(t) => t.name === this.labelGraphic.name
)?.transform.position;
if (labelPosition) {
this.labelGraphic.position.set(labelPosition.x, labelPosition.y);
} else {
this.labelGraphic.position.set(
this.datas.points[0].x,
this.datas.points[0].y + 20
);
}
}
get linePoints(): IPointData[] {
return this.datas.points;
}
set linePoints(points: IPointData[]) {
const old = this.datas.clone();
old.points = points;
this.updateData(old);
}
loadRelations() {
if (this.datas?.paRef?.id) {
this.relationManage.addRelation(
new GraphicRelationParam(this, SectionPort.A),
new GraphicRelationParam(
this.queryStore.queryById(this.datas.paRef.id),
protoPort2Data(this.datas.paRef.devicePort)
)
);
}
if (this.datas?.pbRef?.id) {
this.relationManage.addRelation(
new GraphicRelationParam(this, SectionPort.B),
new GraphicRelationParam(
this.queryStore.queryById(this.datas.pbRef.id),
protoPort2Data(this.datas.pbRef.devicePort)
)
);
}
}
}
export class AxleCountingSectionTemplate extends JlGraphicTemplate<AxleCountingSection> {
constructor(dataTemplate: IAxleCountingSectionData) {
super(AxleCountingSection.Type, {
dataTemplate,
});
}
new(): AxleCountingSection {
const axleCountingSection = new AxleCountingSection();
axleCountingSection.loadData(this.datas);
return axleCountingSection;
}
}