rts-sim-testing-client/src/graphics/axleCountingSection/AxleCountingSection.ts
2023-07-11 16:18:02 +08:00

134 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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';
export interface ITurnoutPosRefData {
get id(): string; //道岔的ID
set id(v: string);
get position(): number; //道岔的正反为0是正位1是反位
set position(v: number);
}
export interface IAxleCountingSectionData extends GraphicData {
get code(): string; // 编号
set code(v: string);
get points(): IPointData[]; // 线坐标点
set points(points: IPointData[]);
get paRef(): IRelatedRefData | undefined; //区段A端关联的设备
set paRef(ref: IRelatedRefData | undefined);
get pbRef(): IRelatedRefData | undefined; //区段B端关联的设备
set pbRef(ref: IRelatedRefData | undefined);
get turnoutPosRef(): ITurnoutPosRefData[]; //关联道岔的正反位
set turnoutPosRef(ref: ITurnoutPosRefData[]);
get indexNumber(): number; // 索引编号
set indexNumber(v: number);
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;
}
}