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

191 lines
7.4 KiB
JavaScript
Raw Normal View History

2023-12-25 17:12:58 +08:00
import { Container, Graphics } from 'pixi.js';
import { VectorText, JlGraphic, JlGraphicTemplate } from 'jl-graphic';
import { TransponderTypeEnum, TransponderConstsMap } from './TransponderConfig.js';
let TransponderConsts = {
height: 12,
lineWidth: 2,
lineColor: '0xFFFFFF',
wblineColor: '0xFF0000',
textFontSize: 12,
textMarginY: 5, // 名称与应答器的距离
vblineColor: '0xFF00FF',
iblineColor: '0x0000FF',
};
const transponderTypePoints = {
[TransponderTypeEnum[TransponderTypeEnum.FB]]: [
[-TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, TransponderConsts.height / 2],
[-TransponderConsts.height / 2, TransponderConsts.height / 2],
[
-TransponderConsts.height / 2,
-TransponderConsts.height / 2 - TransponderConsts.lineWidth / 2,
],
[-TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, TransponderConsts.height / 2],
[TransponderConsts.height / 2, -TransponderConsts.height / 2],
[-TransponderConsts.height / 2, TransponderConsts.height / 2],
],
[TransponderTypeEnum[TransponderTypeEnum.WB]]: [
[-TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, TransponderConsts.height / 2],
[-TransponderConsts.height / 2, TransponderConsts.height / 2],
[
-TransponderConsts.height / 2,
-TransponderConsts.height / 2 - TransponderConsts.lineWidth / 2,
],
[0, -TransponderConsts.height / 2],
[0, TransponderConsts.height / 2],
[TransponderConsts.height / 2, 0],
[-TransponderConsts.height / 2, 0],
],
[TransponderTypeEnum[TransponderTypeEnum.DB]]: [
[-TransponderConsts.height, -TransponderConsts.height / 2],
[TransponderConsts.height, -TransponderConsts.height / 2],
[TransponderConsts.height, TransponderConsts.height / 2],
[-TransponderConsts.height, TransponderConsts.height / 2],
[
-TransponderConsts.height,
-TransponderConsts.height / 2 - TransponderConsts.lineWidth / 2,
],
[-TransponderConsts.height, -TransponderConsts.height / 2],
[TransponderConsts.height, TransponderConsts.height / 2],
[TransponderConsts.height, -TransponderConsts.height / 2],
[-TransponderConsts.height, TransponderConsts.height / 2],
],
[TransponderTypeEnum[TransponderTypeEnum.VB]]: [
[-TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, TransponderConsts.height / 2],
[-TransponderConsts.height / 2, TransponderConsts.height / 2],
[
-TransponderConsts.height / 2,
-TransponderConsts.height / 2 - TransponderConsts.lineWidth / 2,
],
],
[TransponderTypeEnum[TransponderTypeEnum.IB]]: [
[-TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, -TransponderConsts.height / 2],
[TransponderConsts.height / 2, TransponderConsts.height / 2],
[-TransponderConsts.height / 2, TransponderConsts.height / 2],
[
-TransponderConsts.height / 2,
-TransponderConsts.height / 2 - TransponderConsts.lineWidth / 2,
],
[0, -TransponderConsts.height / 2],
[-TransponderConsts.height / 2, 0],
[-TransponderConsts.height / 2, TransponderConsts.height / 2],
[
TransponderConsts.height / 2,
-TransponderConsts.height / 2 + TransponderConsts.lineWidth / 2,
],
[TransponderConsts.height / 2, 0],
[0, TransponderConsts.height / 2],
],
};
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();
categoryType;
constructor(categoryType) {
super(Transponder.Type);
this.addChild(this.polygonGraphic);
this.addChild(this.labelGraphic);
this.categoryType = categoryType;
this.getConstDatas();
}
getConstDatas() {
const constData = TransponderConstsMap.get(this.categoryType);
if (constData) {
TransponderConsts = constData;
}
}
get datas() {
return this.getDatas();
}
clear() {
this.polygonGraphic.clear();
this.labelGraphic.clear();
}
doRepaint() {
this.clear();
const polygonGraphic = this.polygonGraphic;
const type = TransponderTypeEnum[this.datas.type];
const ps = transponderTypePoints[type];
let lineColor = TransponderConsts.lineColor;
if (type == 'WB') {
lineColor = TransponderConsts.wblineColor;
}
else if (type == 'VB') {
lineColor = TransponderConsts.vblineColor;
}
else if (type == 'IB') {
lineColor = TransponderConsts.iblineColor;
}
polygonGraphic.lineStyle(TransponderConsts.lineWidth, lineColor);
polygonGraphic.beginFill(TransponderConsts.lineColor, 0.00001); // 填充透明色(用于碰撞检测)
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,
fontSize: TransponderConsts.textFontSize,
};
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();
this.labelGraphic.position.set(0, polygonHeight / 2 + textHeight / 2 + TransponderConsts.textMarginY);
}
}
}
class TransponderTemplate extends JlGraphicTemplate {
categoryType;
constructor(dataTemplate, gategoryConsts) {
super(Transponder.Type, {
dataTemplate,
});
this.categoryType = gategoryConsts;
}
new() {
const transponder = new Transponder(this.categoryType);
transponder.loadData(this.datas);
return transponder;
}
}
export { Transponder, TransponderCode, TransponderTemplate, transponderTypePoints };