diff --git a/src/drawApp/commonApp.ts b/src/drawApp/commonApp.ts index e8f12a8..5900b58 100644 --- a/src/drawApp/commonApp.ts +++ b/src/drawApp/commonApp.ts @@ -7,7 +7,7 @@ import { ContextMenu, MenuItemOptions, KeyListener, - distance2, + calculateDistanceFromPointToLine, getRectangleCenter, } from 'jl-graphic'; import { TrainState } from './graphics/TrainInteraction'; @@ -277,28 +277,25 @@ export function initCommonDrawApp(app: IDrawApp) { }, moveLimitOption: { moveLimitName: 'sectionPointLimit', - moveLimit: () => { + moveLimit: (e) => { + const mousePos = app.toCanvasCoordinates(e.global); const selectSection = app.selectedGraphics[0] as Section; const selectSectionPos = selectSection.localToCanvasPoint( getRectangleCenter(selectSection.lineGraphic.getLocalBounds()) ); - const copySection = graphicCopyPlugin.copys[0] as Section; - const copySectionPos = copySection.localToCanvasPoint( - getRectangleCenter(copySection.lineGraphic.getLocalBounds()) - ); + const selectSectionLengthBound = + selectSection.lineGraphic.getLocalBounds(); const sections = app.queryStore.queryByType
(Section.Type); const minDistanceSection = sections.reduce((prev, cur) => { - return distance2( - prev.localToCanvasPoint( - getRectangleCenter(prev.lineGraphic.getLocalBounds()) - ), - copySectionPos + return calculateDistanceFromPointToLine( + prev.localToCanvasPoint(prev.getStartPoint()), + prev.localToCanvasPoint(prev.getEndPoint()), + mousePos ) > - distance2( - cur.localToCanvasPoint( - getRectangleCenter(cur.lineGraphic.getLocalBounds()) - ), - copySectionPos + calculateDistanceFromPointToLine( + cur.localToCanvasPoint(cur.getStartPoint()), + cur.localToCanvasPoint(cur.getEndPoint()), + mousePos ) ? cur : prev; @@ -309,22 +306,31 @@ export function initCommonDrawApp(app: IDrawApp) { minDistanceSection.lineGraphic.getLocalBounds() ) ); - const minDistanceSectionLength = distance2( - minDistanceSection.datas.points[0], - minDistanceSection.datas.points[ - minDistanceSection.datas.points.length - 1 - ] - ); - if (copySectionPos.x > minDistanceRefSectionsPos.x) { + const minDistanceSectionBound = + minDistanceSection.lineGraphic.getLocalBounds(); + // const changePoints = minDistanceSection.localToCanvasPoints( + // ...minDistanceSection.datas.points + // ); + // const minDistanceSectionLeftPoint = changePoints.reduce( + // (leftmostPoint, currentPoint) => { + // return currentPoint.x < leftmostPoint.x + // ? currentPoint + // : leftmostPoint; + // }, + // changePoints[0] + // ); + if (mousePos.x > minDistanceRefSectionsPos.x) { graphicCopyPlugin.container.position.x = minDistanceRefSectionsPos.x - selectSectionPos.x + - minDistanceSectionLength; + (minDistanceSectionBound.width + selectSectionLengthBound.width) / + 2; } else { graphicCopyPlugin.container.position.x = minDistanceRefSectionsPos.x - selectSectionPos.x - - minDistanceSectionLength; + (minDistanceSectionBound.width + selectSectionLengthBound.width) / + 2; } graphicCopyPlugin.container.position.y = minDistanceRefSectionsPos.y - selectSectionPos.y;