rts-sim-testing-client/src/graphics/CommonGraphics.ts
2023-07-12 13:05:42 +08:00

122 lines
3.4 KiB
TypeScript

import { Graphics } from 'pixi.js';
import { calculateMirrorPoint } from 'src/jl-graphic';
import { graphicData } from 'src/protos/stationLayoutGraphics';
import { Turnout, TurnoutPort } from './turnout/Turnout';
import { Section, SectionPort } from './section/Section';
import { TrainWindow } from './trainWindow/TrainWindow';
import { AxleCounting } from './axleCounting/AxleCounting';
/**
*
* @param polygon
* @param x 箭头顶点x坐标
* @param y 箭头顶点y坐标
* @param length 箭头长度
* @param radius 箭头三角半径
* @param lineWidth 箭头线宽
* @param mirror 是否镜像翻转 (基于箭头顶点)
*/
export function drawArrow(
polygon: Graphics,
x: number,
y: number,
length: number,
radius: number,
lineWidth: number,
mirror: boolean
) {
const trianglAcme = { x, y };
let triangleP1 = {
x: x - radius - Math.sin(Math.PI / 6),
y: y + Math.cos(Math.PI / 6) * radius,
};
let triangleP2 = {
x: x - radius - Math.sin(Math.PI / 6),
y: y - Math.cos(Math.PI / 6) * radius,
};
let lineP1 = {
x: x - radius - Math.sin(Math.PI / 6),
y: y + lineWidth / 2,
};
let lineP2 = {
x: x - length,
y: y + lineWidth / 2,
};
let lineP3 = {
x: x - length,
y: y - lineWidth / 2,
};
let lineP4 = {
x: x - radius - Math.sin(Math.PI / 6),
y: y - lineWidth / 2,
};
if (mirror) {
triangleP1 = calculateMirrorPoint(trianglAcme, triangleP1);
triangleP2 = calculateMirrorPoint(trianglAcme, triangleP2);
lineP1 = calculateMirrorPoint(trianglAcme, lineP1);
lineP2 = calculateMirrorPoint(trianglAcme, lineP2);
lineP3 = calculateMirrorPoint(trianglAcme, lineP3);
lineP4 = calculateMirrorPoint(trianglAcme, lineP4);
}
polygon.drawPolygon(
trianglAcme.x,
trianglAcme.y,
triangleP1.x,
triangleP1.y,
lineP1.x,
lineP1.y,
lineP2.x,
lineP2.y,
lineP3.x,
lineP3.y,
lineP4.x,
lineP4.y,
triangleP2.x,
triangleP2.y
);
}
export function createRelatedRefProto(
type: string,
id: string,
port?: TurnoutPort | SectionPort
) {
const typeMap = new Map([
[Section.Type, graphicData.RelatedRef.DeviceType.Section],
[Turnout.Type, graphicData.RelatedRef.DeviceType.Turnout],
[TrainWindow.Type, graphicData.RelatedRef.DeviceType.TrainWindow],
[AxleCounting.Type, graphicData.RelatedRef.DeviceType.AxleCounting],
]);
const protoDeviceType = typeMap.get(type);
if (protoDeviceType === undefined) throw Error(`输入的type有误: ${type}`);
const protoData = new graphicData.RelatedRef({
deviceType: protoDeviceType,
id,
});
if (port) {
if (port === 'A')
protoData.devicePort = graphicData.RelatedRef.DevicePort.A;
if (port === 'B')
protoData.devicePort = graphicData.RelatedRef.DevicePort.B;
if (port === 'C')
protoData.devicePort = graphicData.RelatedRef.DevicePort.C;
}
return protoData;
}
export function protoPort2Data(port: graphicData.RelatedRef.DevicePort) {
if (port === graphicData.RelatedRef.DevicePort.A) return 'A';
if (port === graphicData.RelatedRef.DevicePort.B) return 'B';
if (port === graphicData.RelatedRef.DevicePort.C) return 'C';
}
export interface IRelatedRefData {
deviceType: graphicData.RelatedRef.DeviceType; //关联的设备类型
id: string; //关联的设备ID
devicePort: graphicData.RelatedRef.DevicePort; //关联的设备端口
}
export interface ISimpleRefData {
deviceType: graphicData.SimpleRef.DeviceType;
id: string;
}