From 73300543e1531782da545c9fdd67fcf25754fdf3 Mon Sep 17 00:00:00 2001 From: joylink_zhaoerwei Date: Tue, 16 Jan 2024 16:11:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BA=E6=AE=B5A=E9=94=AE=E7=9B=98=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drawApp/commonApp.ts | 50 ++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/drawApp/commonApp.ts b/src/drawApp/commonApp.ts index 040a140..6411f7b 100644 --- a/src/drawApp/commonApp.ts +++ b/src/drawApp/commonApp.ts @@ -8,6 +8,7 @@ import { MenuItemOptions, KeyListener, distance2, + getRectangleCenter, } from 'jl-graphic'; import { TrainState } from './graphics/TrainInteraction'; import { @@ -260,18 +261,49 @@ export function initCommonDrawApp(app: IDrawApp) { value: 'KeyA', global: true, onPress: () => { - const copySection = graphicCopyPlugin.copys[0] as Section; - const copySectionLength = distance2( - copySection.datas.points[0], - copySection.datas.points[1] + const selectSection = app.selectedGraphics[0] as Section; + const selectSectionPos = selectSection.localToCanvasPoint( + getRectangleCenter(selectSection.getLocalBounds()) ); - if (graphicCopyPlugin.container.position.x > 0) { - graphicCopyPlugin.copys[0].position.x += copySectionLength; + const copySection = graphicCopyPlugin.copys[0] as Section; + const copySectionPos = copySection.localToCanvasPoint( + getRectangleCenter(copySection.getLocalBounds()) + ); + const sections = app.queryStore.queryByType
(Section.Type); + const minDistanceSection = sections.reduce((prev, cur) => { + return distance2( + prev.localToCanvasPoint(getRectangleCenter(prev.getLocalBounds())), + copySectionPos + ) > + distance2( + cur.localToCanvasPoint(getRectangleCenter(cur.getLocalBounds())), + copySectionPos + ) + ? cur + : prev; + }); + const minDistanceRefSectionsPos = minDistanceSection.localToCanvasPoint( + getRectangleCenter(minDistanceSection.getLocalBounds()) + ); + const minDistanceSectionLength = distance2( + minDistanceSection.datas.points[0], + minDistanceSection.datas.points[ + minDistanceSection.datas.points.length - 1 + ] + ); + if (copySectionPos.x > minDistanceRefSectionsPos.x) { + graphicCopyPlugin.container.position.x = + minDistanceRefSectionsPos.x - + selectSectionPos.x + + minDistanceSectionLength; } else { - graphicCopyPlugin.copys[0].position.x -= copySectionLength; + graphicCopyPlugin.container.position.x = + minDistanceRefSectionsPos.x - + selectSectionPos.x - + minDistanceSectionLength; } - graphicCopyPlugin.saveCopyGraphic(); - graphicCopyPlugin.clear(); + graphicCopyPlugin.container.position.y = + minDistanceRefSectionsPos.y - selectSectionPos.y; }, }); graphicCopyPlugin.addGraphicControlers([