From 69cd13639309ab3a55913eada6edd9f9e1b76cc7 Mon Sep 17 00:00:00 2001 From: joylink_zhaoerwei Date: Mon, 15 Jan 2024 18:07:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=93=E5=B2=94=E4=BB=A3=E7=A0=81=E5=A4=87?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/packages/Section/common/Section.ts | 105 +++++++------- .../Section/common/SectionDrawAssistant.ts | 4 +- src/packages/Turnout/GPTurnout.ts | 8 ++ src/packages/Turnout/Turnout.ts | 53 ------- src/packages/Turnout/common/JlTurnout.ts | 130 ++++++++++++++++++ src/packages/Turnout/common/TurnoutConfig.ts | 48 +++++++ yarn.lock | 6 +- 8 files changed, 247 insertions(+), 109 deletions(-) create mode 100644 src/packages/Turnout/GPTurnout.ts delete mode 100644 src/packages/Turnout/Turnout.ts create mode 100644 src/packages/Turnout/common/JlTurnout.ts create mode 100644 src/packages/Turnout/common/TurnoutConfig.ts diff --git a/package.json b/package.json index 130cac2..04e2687 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test": "node ./test.cjs" }, "dependencies": { - "jl-graphic": "git+https://git.code.tencent.com/jl-framework/graphic-pixi.git#v0.1.3" + "jl-graphic": "git+https://git.code.tencent.com/jl-framework/graphic-pixi.git#v0.1.7" }, "devDependencies": { "@rollup/plugin-alias": "^5.1.0", diff --git a/src/packages/Section/common/Section.ts b/src/packages/Section/common/Section.ts index db1fa09..b558a4e 100644 --- a/src/packages/Section/common/Section.ts +++ b/src/packages/Section/common/Section.ts @@ -13,7 +13,7 @@ import { import { IPointData } from 'pixi.js'; import { SectionGraphic } from './SectionGraphic'; import { DevicePort, DeviceType, IRelatedRef } from 'common/common'; -import { Turnout } from 'src/packages/Turnout/Turnout'; +import { JlTurnout } from 'src/packages/Turnout/common/JlTurnout'; import { AxleCounting } from 'src/packages/AxleCounting/AxleCounting'; const tolerance = 0.01; @@ -151,13 +151,13 @@ export class JlSection extends JlGraphic { (relation) => relation.getRelationParam(this).getParam() === port && (relation.getOtherGraphic(this) instanceof JlSection || - relation.getOtherGraphic(this) instanceof Turnout), + relation.getOtherGraphic(this) instanceof JlTurnout), ); if (!relation) { return; } return { - g: relation?.getOtherGraphic(this) as JlSection | Turnout, + g: relation?.getOtherGraphic(this) as JlSection | JlTurnout, port: relation?.getOtherRelationParam(this).getParam(), }; } @@ -217,52 +217,57 @@ export class JlSection extends JlGraphic { */ buildRelation() { - this.relationManage.deleteRelationOfGraphicAndOtherType(this, JlSection.Type); + this.relationManage.deleteRelationOfGraphicAndOtherType( + this, + JlSection.Type, + ); if (this.datas.sectionType === SectionType.Physical) { - this.queryStore.queryByType(JlSection.Type).forEach((section) => { - if (section.id === this.id) return; + this.queryStore + .queryByType(JlSection.Type) + .forEach((section) => { + if (section.id === this.id) return; - let param: DevicePort[] = []; - if ( - distance2( - this.localToCanvasPoint(this.getStartPoint()), - section.localToCanvasPoint(section.getStartPoint()), - ) <= tolerance - ) { - param = [DevicePort.A, DevicePort.A]; - } - if ( - distance2( - this.localToCanvasPoint(this.getEndPoint()), - section.localToCanvasPoint(section.getStartPoint()), - ) <= tolerance - ) { - param = [DevicePort.B, DevicePort.A]; - } - if ( - distance2( - this.localToCanvasPoint(this.getStartPoint()), - section.localToCanvasPoint(section.getEndPoint()), - ) <= tolerance - ) { - param = [DevicePort.A, DevicePort.B]; - } - if ( - distance2( - this.localToCanvasPoint(this.getEndPoint()), - section.localToCanvasPoint(section.getEndPoint()), - ) <= tolerance - ) { - param = [DevicePort.B, DevicePort.B]; - } - if (param.length) { - this.relationManage.addRelation( - new GraphicRelationParam(this, param[0]), - new GraphicRelationParam(section, param[1]), - ); - } - }); + let param: DevicePort[] = []; + if ( + distance2( + this.localToCanvasPoint(this.getStartPoint()), + section.localToCanvasPoint(section.getStartPoint()), + ) <= tolerance + ) { + param = [DevicePort.A, DevicePort.A]; + } + if ( + distance2( + this.localToCanvasPoint(this.getEndPoint()), + section.localToCanvasPoint(section.getStartPoint()), + ) <= tolerance + ) { + param = [DevicePort.B, DevicePort.A]; + } + if ( + distance2( + this.localToCanvasPoint(this.getStartPoint()), + section.localToCanvasPoint(section.getEndPoint()), + ) <= tolerance + ) { + param = [DevicePort.A, DevicePort.B]; + } + if ( + distance2( + this.localToCanvasPoint(this.getEndPoint()), + section.localToCanvasPoint(section.getEndPoint()), + ) <= tolerance + ) { + param = [DevicePort.B, DevicePort.B]; + } + if (param.length) { + this.relationManage.addRelation( + new GraphicRelationParam(this, param[0]), + new GraphicRelationParam(section, param[1]), + ); + } + }); } } @@ -273,9 +278,9 @@ export class JlSection extends JlGraphic { (relation) => relation.getRelationParam(this).param === DevicePort.A && (relation.getOtherGraphic(this) instanceof JlSection || - relation.getOtherGraphic(this) instanceof Turnout), + relation.getOtherGraphic(this) instanceof JlTurnout), ); - const paDevice = paRelation?.getOtherGraphic(this); + const paDevice = paRelation?.getOtherGraphic(this); if (paDevice) { this.datas.paRef = IRelatedRef.create( paDevice.type, @@ -291,9 +296,9 @@ export class JlSection extends JlGraphic { (relation) => relation.getRelationParam(this).param === DevicePort.B && (relation.getOtherGraphic(this) instanceof JlSection || - relation.getOtherGraphic(this) instanceof Turnout), + relation.getOtherGraphic(this) instanceof JlTurnout), ); - const pbDevice = pbRelation?.getOtherGraphic(this); + const pbDevice = pbRelation?.getOtherGraphic(this); if (pbDevice) { this.datas.pbRef = IRelatedRef.create( pbDevice.type, diff --git a/src/packages/Section/common/SectionDrawAssistant.ts b/src/packages/Section/common/SectionDrawAssistant.ts index a10aa0f..6d8e9bc 100644 --- a/src/packages/Section/common/SectionDrawAssistant.ts +++ b/src/packages/Section/common/SectionDrawAssistant.ts @@ -43,7 +43,7 @@ import { type IPointData, type DisplayObject, } from 'pixi.js'; -import { Turnout } from 'src/packages/Turnout/Turnout'; +import { JlTurnout } from 'src/packages/Turnout/common/JlTurnout'; import { AxleCounting } from 'src/packages/AxleCounting/AxleCounting'; import { DevicePort } from 'common/common'; import { Section } from '../GPSection'; @@ -348,7 +348,7 @@ function buildAbsorbablePositions(section: Section): AbsorbablePosition[] { aps.push(xs, ys, xe, ye); }); - const turnouts = section.queryStore.queryByType(Turnout.Type); + const turnouts = section.queryStore.queryByType(JlTurnout.Type); turnouts.forEach((turnout) => { turnout.getPortPoints().forEach((points) => { turnout.localToCanvasPoints(...points).forEach((p) => { diff --git a/src/packages/Turnout/GPTurnout.ts b/src/packages/Turnout/GPTurnout.ts new file mode 100644 index 0000000..dc3f290 --- /dev/null +++ b/src/packages/Turnout/GPTurnout.ts @@ -0,0 +1,8 @@ +import { JlTurnout } from './common/JlTurnout'; + +export class Turnout extends JlTurnout { + static Type = 'Turnout'; + doRepaint(): void { + super.draw(); + } +} diff --git a/src/packages/Turnout/Turnout.ts b/src/packages/Turnout/Turnout.ts deleted file mode 100644 index 53454f5..0000000 --- a/src/packages/Turnout/Turnout.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { DevicePort, IRelatedRef, KilometerSystem } from 'common/common'; -import { GraphicData, JlGraphic } from 'jl-graphic'; -import { IPointData } from 'pixi.js'; - -export enum SwitchMachineType { - Unknown = 0, - ZDJ9_Single = 1, - ZDJ9_Double = 2, -} - -export interface ITurnoutData extends GraphicData { - code: string; - pointA: IPointData[]; - pointB: IPointData[]; - pointC: IPointData[]; - paRef?: IRelatedRef; - pbRef?: IRelatedRef; - pcRef?: IRelatedRef; - kilometerSystem: KilometerSystem; - paTrackSectionId?: number; - pbTrackSectionId?: number; - pcTrackSectionId?: number; - switchMachineType?: SwitchMachineType; - centralizedStations?: number[]; - clone(): ITurnoutData; - copyFrom(data: ITurnoutData): void; - eq(other: ITurnoutData): boolean; -} - -export class Turnout extends JlGraphic { - static Type = 'Turnout'; - doRepaint(): void { - console.log(111); - } - get datas(): ITurnoutData { - return this.getDatas(); - } - - getGraphicOfPort(port: DevicePort) { - return this.relationManage - .getRelationsOfGraphic(this) - .filter( - (relation) => - relation.getRelationParam(this).getParam() === port, - ) - .map((relation) => { - return relation.getOtherGraphic(this); - }); - } - getPortPoints() { - return [this.datas.pointA, this.datas.pointB, this.datas.pointC]; - } -} diff --git a/src/packages/Turnout/common/JlTurnout.ts b/src/packages/Turnout/common/JlTurnout.ts new file mode 100644 index 0000000..de78985 --- /dev/null +++ b/src/packages/Turnout/common/JlTurnout.ts @@ -0,0 +1,130 @@ +import { JlGraphic, VectorText } from 'jl-graphic'; +import { IPointData, Graphics } from 'pixi.js'; +import { DevicePort } from 'common/common'; +import { ITurnoutData, TurnoutConstsConfig } from './TurnoutConfig'; + +export function getForkPoint(r: number, p: IPointData): IPointData { + if (r === 0) return { x: 0, y: 0 }; + const len = Math.sqrt((-p.x) ** 2 + (-p.y) ** 2); + const scale = r / len; + return { x: scale * p.x, y: scale * p.y }; +} + +export class TurnoutSection extends Graphics { + turnoutConsts: TurnoutConstsConfig; + turnout: JlTurnout; + port: DevicePort; + stateFillColor?: string; + constructor( + turnout: JlTurnout, + port: DevicePort, + turnoutConsts: TurnoutConstsConfig, + ) { + super(); + this.turnoutConsts = turnoutConsts; + this.turnout = turnout; + this.port = port; + } + + paint() { + let pList: IPointData[] = []; + switch (this.port) { + case DevicePort.A: + pList = this.turnout.datas.pointA; + break; + case DevicePort.B: + pList = this.turnout.datas.pointB; + break; + case DevicePort.C: + pList = this.turnout.datas.pointC; + break; + } + const gap = this.port === DevicePort.A ? 0 : this.turnoutConsts.forkLenth; + const fillColor = this.stateFillColor || this.turnoutConsts.lineColor; + const start = getForkPoint(gap, pList[0]); + this.clear() + .lineStyle(this.turnoutConsts.lineWidth, fillColor) + .moveTo(start.x, start.y); + pList.forEach((p) => { + const { x, y } = p; + this.lineTo(x, y); + }); + } +} + +class ForkGraphic extends Graphics { + turnoutConsts: TurnoutConstsConfig; + turnout: JlTurnout; + stateFillColor?: string; + constructor(turnout: JlTurnout, turnoutConsts: TurnoutConstsConfig) { + super(); + this.turnoutConsts = turnoutConsts; + this.turnout = turnout; + } + + paint(p: IPointData) { + const fillColor = this.stateFillColor || this.turnoutConsts.lineColor; + const target = getForkPoint(this.turnoutConsts.forkLenth, p); + this.clear() + .lineStyle(this.turnoutConsts.lineWidth, fillColor) + .moveTo(0, 0) + .lineTo(target.x, target.y); + } +} + +export abstract class JlTurnout extends JlGraphic { + static Type = 'Turnout'; + graphics: { + fork: ForkGraphic; + sections: [TurnoutSection, TurnoutSection, TurnoutSection]; + label: VectorText; + }; + + constructor(turnoutConsts: TurnoutConstsConfig) { + super(JlTurnout.Type); + this.name = 'turnout'; + this.graphics = { + fork: new ForkGraphic(this, turnoutConsts), + sections: [ + new TurnoutSection(this, DevicePort.A, turnoutConsts), + new TurnoutSection(this, DevicePort.B, turnoutConsts), + new TurnoutSection(this, DevicePort.C, turnoutConsts), + ], + label: new VectorText(), + }; + this.addChild(this.graphics.fork); + this.addChild(this.graphics.sections[0]); + this.addChild(this.graphics.sections[1]); + this.addChild(this.graphics.sections[2]); + this.graphics.label.anchor.set(0.5); + this.graphics.label.style.fill = '#0f0'; + this.graphics.label.setVectorFontSize(turnoutConsts.labelFontSize); + this.graphics.label.position.set(20, 20); + this.graphics.label.transformSave = true; + this.graphics.label.name = 'label'; + this.addChild(this.graphics.label); + } + draw(): void { + this.graphics.label.text = this.datas.code; + this.graphics.sections.forEach((sectionGraphic) => sectionGraphic.paint()); + } + get datas(): ITurnoutData { + return this.getDatas(); + } + + getPortPoints() { + return [this.datas.pointA, this.datas.pointB, this.datas.pointC]; + } + + getGraphicOfPort(port: DevicePort) { + return this.relationManage + .getRelationsOfGraphic(this) + .filter( + (relation) => + relation.getRelationParam(this).getParam() === port, + ) + .map((relation) => { + return relation.getOtherGraphic(this); + }); + } +} diff --git a/src/packages/Turnout/common/TurnoutConfig.ts b/src/packages/Turnout/common/TurnoutConfig.ts new file mode 100644 index 0000000..323a798 --- /dev/null +++ b/src/packages/Turnout/common/TurnoutConfig.ts @@ -0,0 +1,48 @@ +import { IRelatedRef, KilometerSystem } from 'common/common'; +import { GraphicData } from 'jl-graphic'; +import { IPointData } from 'pixi.js'; + +export interface TurnoutConstsConfig { + lineColor: string; + occupiedColor: string; + lineWidth: number; + forkLenth: number; + labelFontSize: number; + normalLabelColor: string; + reverseLabelColor: string; +} + +export const GPConsts = { + lineColor: '#5578b6', + occupiedColor: '#f00', + lineWidth: 5, + forkLenth: 20, + labelFontSize: 12, + normalLabelColor: '#0f0', + reverseLabelColor: '#ff0', +}; + +export enum SwitchMachineType { + Unknown = 0, + ZDJ9_Single = 1, + ZDJ9_Double = 2, +} + +export interface ITurnoutData extends GraphicData { + code: string; + pointA: IPointData[]; //A端点列表(从岔心向外) + pointB: IPointData[]; + pointC: IPointData[]; + paRef?: IRelatedRef; // 道岔A端关联的设备 + pbRef?: IRelatedRef; + pcRef?: IRelatedRef; + kilometerSystem: KilometerSystem; // 道岔公里标 + paTrackSectionId?: number; // A端轨道区段id + pbTrackSectionId?: number; + pcTrackSectionId?: number; + switchMachineType?: SwitchMachineType; // 转辙机类型 + centralizedStations?: number[]; // 集中站Id列表--北京 + clone(): ITurnoutData; + copyFrom(data: ITurnoutData): void; + eq(other: ITurnoutData): boolean; +} diff --git a/yarn.lock b/yarn.lock index 3855e4c..c1dae9a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1182,9 +1182,9 @@ ismobilejs@^1.1.0: resolved "https://registry.npmmirror.com/ismobilejs/-/ismobilejs-1.1.1.tgz#c56ca0ae8e52b24ca0f22ba5ef3215a2ddbbaa0e" integrity sha512-VaFW53yt8QO61k2WJui0dHf4SlL8lxBofUuUmwBo0ljPk0Drz2TiuDW4jo3wDcv41qy/SxrJ+VAzJ/qYqsmzRw== -"jl-graphic@git+https://git.code.tencent.com/jl-framework/graphic-pixi.git#v0.1.3": - version "0.1.3" - resolved "git+https://git.code.tencent.com/jl-framework/graphic-pixi.git#100ddafc75ffa2fc646ad26359682e0f083511e3" +"jl-graphic@git+https://git.code.tencent.com/jl-framework/graphic-pixi.git#v0.1.7": + version "0.1.6" + resolved "git+https://git.code.tencent.com/jl-framework/graphic-pixi.git#9a4a307efa10f55708f5f1d518f815882df567a3" dependencies: "@pixi/graphics-extras" "^7.3.2" "@pixi/utils" "^7.3.2"