rts-sim-testing-client/src/graphics/axleCountingSection/AxleCountingSectionAssistant.ts

262 lines
8.1 KiB
TypeScript
Raw Normal View History

2023-07-07 17:15:53 +08:00
import {
FederatedPointerEvent,
IHitArea,
IPoint,
IPointData,
Point,
} from 'pixi.js';
import {
AbsorbableLine,
AbsorbablePosition,
GraphicDrawAssistant,
GraphicIdGenerator,
GraphicInteractionPlugin,
JlDrawApp,
JlGraphic,
linePoint,
} from 'src/jl-graphic';
import {
IAxleCountingSectionData,
AxleCountingSection,
AxleCountingSectionTemplate,
AxleCountingSectionConsts,
} from './AxleCountingSection';
import { AxleCounting } from '../axleCounting/AxleCounting';
import { Section } from '../section/Section';
import { Turnout } from '../turnout/Turnout';
import { createRelatedRefProto } from '../CommonGraphics';
function hasCommonElements(arr1: string[], arr2: string[]): boolean {
for (let i = 0; i < arr1.length; i++) {
if (arr2.includes(arr1[i])) {
return true;
}
}
return false;
}
function hasSamePosition(point1: IPointData, point2: IPointData): boolean {
if (
Math.abs(point1.x - point2.x) < 20 &&
Math.abs(point1.y - point2.y) < 20
) {
return true;
}
return false;
}
export interface IAxleCountingSectionDrawOptions {
newData: () => IAxleCountingSectionData;
}
export class AxleCountingSectionDraw extends GraphicDrawAssistant<
AxleCountingSectionTemplate,
IAxleCountingSectionData
> {
codeGraph: AxleCountingSection;
constructor(app: JlDrawApp, template: AxleCountingSectionTemplate) {
super(app, template, 'sym_o_circle', '不展示');
this.codeGraph = this.graphicTemplate.new();
this.container.addChild(this.codeGraph);
AxleCountingSectionInteraction.init(app);
}
bind(): void {
super.bind();
this.codeGraph.loadData(this.graphicTemplate.datas);
this.codeGraph.doRepaint();
}
clearCache(): void {
//this.codeGraph.destroy();
}
onLeftDown(e: FederatedPointerEvent): void {
this.container.position.copyFrom(this.toCanvasCoordinates(e.global));
this.createAndStore(true);
}
redraw(p: Point): void {
this.container.position.copyFrom(p);
}
prepareData(data: IAxleCountingSectionData): boolean {
data.transform = this.container.saveTransform();
return true;
}
draw(graphics: AxleCounting[]) {
/* const paRefPs = this.app.queryStore.queryById(
graphics[0].datas.axleCountingRef[0].id
);
const RepbRefPs = this.app.queryStore.queryById(
graphics[1].datas.axleCountingRef[0].id
); */
const axleCountingSection = new AxleCountingSection();
axleCountingSection.loadData(this.graphicTemplate.datas);
axleCountingSection.datas.points = [
graphics[0].position,
graphics[1].position,
];
axleCountingSection.id = GraphicIdGenerator.next();
const paRef = createRelatedRefProto(graphics[0].type, graphics[0].id);
const pbRef = createRelatedRefProto(graphics[1].type, graphics[1].id);
axleCountingSection.datas.paRef = paRef;
axleCountingSection.datas.pbRef = pbRef;
this.storeGraphic(axleCountingSection);
axleCountingSection.loadRelations();
}
oneGenerates() {
const axleCountings = this.app.queryStore.queryByType<AxleCounting>(
AxleCounting.Type
);
const hasfourTurnout: AxleCounting[][] = [];
axleCountings.forEach((axleCounting) => {
const refDeviceTarget = axleCounting.datas.axleCountingRef.map(
(ref) => ref.id
);
for (let i = 0; i < axleCountings.length - 1; i++) {
if (axleCountings[i].id == axleCounting.id) return;
const refDevice = axleCountings[i].datas.axleCountingRef.map(
(ref) => ref.id
);
if (hasCommonElements(refDeviceTarget, refDevice)) {
this.draw([axleCounting, axleCountings[i]]);
}
if (hasSamePosition(axleCounting, axleCountings[i])) {
hasfourTurnout.push([axleCounting, axleCountings[i]]);
}
}
});
const fourTurnout: Turnout[] = [];
hasfourTurnout.forEach((axleCountings) => {
const axleCountingRelations =
axleCountings[0].relationManage.getRelationsOfGraphicAndOtherType(
axleCountings[0],
Turnout.Type
);
axleCountingRelations.forEach((relation) => {
const refDevice = relation.getOtherGraphic<Section>(axleCountings[0]);
fourTurnout;
});
});
/* axleCountings.sort((a, b) => a.x - b.x);
const downAxleCountings = axleCountings.filter((point) => {
return point.y > 350;
});
for (let i = 0; i < downAxleCountings.length - 1; i++) {
const firstRef = downAxleCountings[i].datas.axleCountingRef.map(
(ref) => ref.id
);
const nextRef = downAxleCountings[i + 1].datas.axleCountingRef.map(
(ref) => ref.id
);
let nextNextRef: string[] = [];
if (i + 2 < downAxleCountings.length) {
nextNextRef = downAxleCountings[i + 2].datas.axleCountingRef.map(
(ref) => ref.id
);
}
if (hasCommonElements(firstRef, nextRef)) {
this.draw([downAxleCountings[i], downAxleCountings[i + 1]]);
} else if (
hasSamePosition(
downAxleCountings[i + 1].position,
downAxleCountings[i + 2].position
) &&
hasCommonElements(firstRef, nextNextRef)
) {
this.draw([downAxleCountings[i], downAxleCountings[i + 2]]);
i += 2;
}
} */
}
}
function buildAbsorbablePositions(
axleCountingSection: AxleCountingSection
): AbsorbablePosition[] {
const aps: AbsorbablePosition[] = [];
const axleCountingSections =
axleCountingSection.queryStore.queryByType<AxleCountingSection>(
AxleCountingSection.Type
);
const { width } = axleCountingSection.getGraphicApp().canvas;
axleCountingSections.forEach((other) => {
if (other.id == axleCountingSection.id) {
return;
}
const ps = other.datas.transform.position;
const xs = new AbsorbableLine({ x: 0, y: ps.y }, { x: width, y: ps.y });
aps.push(xs);
});
return aps;
}
class AxleCountingSectionGraphicHitArea implements IHitArea {
axleCountingSection: AxleCountingSection;
constructor(axleCountingSection: AxleCountingSection) {
this.axleCountingSection = axleCountingSection;
}
contains(x: number, y: number): boolean {
for (let i = 1; i < this.axleCountingSection.datas.points.length; i++) {
const p1 = this.axleCountingSection.datas.points[i - 1];
const p2 = this.axleCountingSection.datas.points[i];
if (linePoint(p1, p2, { x, y }, AxleCountingSectionConsts.lineWidth)) {
return true;
}
}
return false;
}
}
export class AxleCountingSectionInteraction extends GraphicInteractionPlugin<AxleCountingSection> {
static Name = 'AxleCountingSection_transform';
constructor(app: JlDrawApp) {
super(AxleCountingSectionInteraction.Name, app);
}
static init(app: JlDrawApp) {
return new AxleCountingSectionInteraction(app);
}
filter(...grahpics: JlGraphic[]): AxleCountingSection[] | undefined {
return grahpics
.filter((g) => g.type === AxleCountingSection.Type)
.map((g) => g as AxleCountingSection);
}
bind(g: AxleCountingSection): void {
g.eventMode = 'static';
g.cursor = 'pointer';
g.scalable = true;
g.transformSave = true;
g.lineGraphic.eventMode = 'static';
g.lineGraphic.cursor = 'pointer';
g.lineGraphic.hitArea = new AxleCountingSectionGraphicHitArea(g);
g.labelGraphic.eventMode = 'static';
g.labelGraphic.cursor = 'pointer';
g.labelGraphic.selectable = true;
g.labelGraphic.draggable = true;
g.on('selected', this.onSelected, this);
}
unbind(g: AxleCountingSection): void {
g.eventMode = 'none';
g.scalable = false;
g.rotatable = false;
g.lineGraphic.eventMode = 'none';
g.lineGraphic.draggable = false;
g.lineGraphic.selectable = false;
g.lineGraphic.transformSave = false;
g.labelGraphic.eventMode = 'none';
g.labelGraphic.draggable = false;
g.labelGraphic.selectable = false;
g.labelGraphic.transformSave = false;
g.off('selected', this.onSelected, this);
}
onSelected(): void {
const AxleCountingSection = this.app
.selectedGraphics[0] as AxleCountingSection;
this.app.setOptions({
absorbablePositions: buildAbsorbablePositions(AxleCountingSection),
});
}
}