rt-graphic-component/components/Transponder/Transponder.js

89 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-12-25 17:12:58 +08:00
import { Container, Graphics } from 'pixi.js';
2024-01-02 14:36:10 +08:00
import { VectorText, JlGraphic } from 'jl-graphic';
import { getTransponderConsts, getTypePoints, TransponderTypeEnum } from './TransponderConfig.js';
2023-12-25 17:12:58 +08:00
class TransponderCode extends Container {
codeText = new VectorText('');
name = 'transponderCode';
constructor() {
super();
this.addChild(this.codeText);
}
clear() {
this.codeText.text = '';
}
paint(datas) {
this.codeText.text = datas.code;
this.codeText.anchor.set(0.5);
}
}
class Transponder extends JlGraphic {
static Type = 'Transponder';
polygonGraphic = new Graphics();
labelGraphic = new TransponderCode();
2024-01-02 14:36:10 +08:00
constDatas;
constructor(data) {
2023-12-25 17:12:58 +08:00
super(Transponder.Type);
this.addChild(this.polygonGraphic);
this.addChild(this.labelGraphic);
2024-01-02 14:36:10 +08:00
this.constDatas = getTransponderConsts();
if (data) {
Object.assign(this.constDatas, data);
2023-12-25 17:12:58 +08:00
}
}
get datas() {
return this.getDatas();
}
clear() {
this.polygonGraphic.clear();
this.labelGraphic.clear();
}
doRepaint() {
this.clear();
const polygonGraphic = this.polygonGraphic;
2024-01-02 14:36:10 +08:00
const ps = getTypePoints(this.datas.type, this.constDatas);
let lineColor = this.constDatas.lineColor;
if (this.datas.type == TransponderTypeEnum.WB) {
lineColor = this.constDatas.wblineColor;
2023-12-25 17:12:58 +08:00
}
2024-01-02 14:36:10 +08:00
else if (this.datas.type == TransponderTypeEnum.VB) {
lineColor = this.constDatas.vblineColor;
2023-12-25 17:12:58 +08:00
}
2024-01-02 14:36:10 +08:00
else if (this.datas.type == TransponderTypeEnum.IB) {
lineColor = this.constDatas.iblineColor;
2023-12-25 17:12:58 +08:00
}
2024-01-02 14:36:10 +08:00
polygonGraphic.lineStyle(this.constDatas.lineWidth, lineColor);
polygonGraphic.beginFill(this.constDatas.lineColor, 0.00001); // 填充透明色(用于碰撞检测)
2023-12-25 17:12:58 +08:00
const indexArr = [0, 5, 7];
ps.forEach((item, index) => {
if (indexArr.includes(index)) {
polygonGraphic.moveTo(item[0], item[1]);
}
else {
polygonGraphic.lineTo(item[0], item[1]);
}
});
polygonGraphic.endFill;
this.labelGraphic.paint(this.datas);
const style = {
fill: lineColor,
2024-01-02 14:36:10 +08:00
fontSize: this.constDatas.textFontSize,
2023-12-25 17:12:58 +08:00
};
this.labelGraphic.codeText.style = style;
const codeTransform = this.datas?.childTransforms?.find((item) => item.name === 'transponderCode');
if (codeTransform) {
const position = codeTransform?.transform.position;
const rotation = codeTransform?.transform?.rotation;
this.labelGraphic.position.set(position?.x, position?.y);
this.labelGraphic.rotation = rotation || 0;
}
else {
const { height: polygonHeight } = this.polygonGraphic.getLocalBounds();
const { height: textHeight } = this.labelGraphic.getLocalBounds();
2024-01-02 14:36:10 +08:00
this.labelGraphic.position.set(0, polygonHeight / 2 + textHeight / 2 + this.constDatas.textMarginY);
2023-12-25 17:12:58 +08:00
}
}
}
2024-01-02 14:36:10 +08:00
export { Transponder, TransponderCode };