From 0d5bec125e3d6d824faa5a555dc432eb711f8d8b Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Tue, 18 Mar 2025 17:44:21 +0000 Subject: [PATCH] LazMapViewer: Add MarkerDragPlugin. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9687 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/addons/plugins/mvplugins.pas | 88 +++++++++++++++++-- 1 file changed, 81 insertions(+), 7 deletions(-) diff --git a/components/lazmapviewer/source/addons/plugins/mvplugins.pas b/components/lazmapviewer/source/addons/plugins/mvplugins.pas index 86277d7f3..81e1c663d 100644 --- a/components/lazmapviewer/source/addons/plugins/mvplugins.pas +++ b/components/lazmapviewer/source/addons/plugins/mvplugins.pas @@ -144,15 +144,20 @@ type TMarkerClickPlugin = class(TMvMarkerPlugin) private FCursor: TCursor; - FSavedCursor: TCursor; FShift: TShiftState; FOnCanClick: TMarkerCanClickEvent; FOnMarkerClick: TMarkerClickEvent; protected + FMouseDown: Boolean; + FMousePoint: TPoint; + FOrigGpsPoint: TGPSPoint; + FSavedCursor: TCursor; procedure MouseDown(AMapView: TMapView; {%H-}Button: TMouseButton; AShift: TShiftState; X,Y: Integer; var Handled: Boolean); override; procedure MouseMove(AMapView: TMapView; {%H-}AShift: TShiftState; X,Y: Integer; var Handled: Boolean); override; + procedure MouseUp(AMapView: TMapView; {%H-}Button: TMouseButton; + AShift: TShiftState; X,Y: Integer; var Handled: Boolean); override; procedure SetMapView(AValue: TMapView); override; public constructor Create(AOwner: TComponent); override; @@ -164,6 +169,25 @@ type end; + { TMarkerDragPlugin } + + TMarkerDragPlugin = class(TMarkerClickPlugin) + private + FDragCursor: TCursor; + protected + procedure MouseDown(AMapView: TMapView; {%H-}Button: TMouseButton; + AShift: TShiftState; X,Y: Integer; var Handled: Boolean); override; + procedure MouseMove(AMapView: TMapView; {%H-}AShift: TShiftState; + X,Y: Integer; var Handled: Boolean); override; + procedure MouseUp(AMapView: TMapView; {%H-}Button: TMouseButton; + AShift: TShiftState; X,Y: Integer; var Handled: Boolean); override; + public + constructor Create(AOwner: TComponent); override; + published + property DragCursor: TCursor read FDragCursor write FDragCursor default crSizeAll; + end; + + { TMarkerHintPlugin } TMarkerCreateHintWindowEvent = procedure(AMapView: TMapView; @@ -951,23 +975,25 @@ end; procedure TMarkerClickPlugin.MouseDown(AMapView: TMapView; Button: TMouseButton; AShift: TShiftState; X, Y: Integer; var Handled: Boolean); var - gpsPoint: TGPSPoint; canClick: Boolean; begin if Handled then exit; - gpsPoint := FindNearestMarker(AMapView, X, Y); - if Assigned(gpsPoint) and Assigned(FOnMarkerClick) and (AShift = FShift) then + FOrigGPSPoint := FindNearestMarker(AMapView, X, Y); + if Assigned(FOrigGPSPoint) and (AShift = FShift) then begin if Assigned(FOnCanClick) then begin canClick := true; - FOnCanClick(AMapView, gpsPoint, canClick); + FOnCanClick(AMapView, FOrigGPSPoint, canClick); if not canClick then exit; end; - FOnMarkerClick(AMapView, gpsPoint); + if Assigned(FOnMarkerClick) then + FOnMarkerClick(AMapView, FOrigGPSPoint); + FMouseDown := true; + FMousePoint := Point(X, Y); Handled := true; end; end; @@ -991,6 +1017,12 @@ begin AMapView.Cursor := IfThen(canClick, FCursor, FSavedCursor); end; +procedure TMarkerClickPlugin.MouseUp(AMapView: TMapView; Button: TMouseButton; + AShift: TShiftState; X, Y: Integer; var Handled: Boolean); +begin + FMouseDown := false; +end; + { Store the original MapView cursor. Is used when the mouse is not over a clickable point. If no MapView is assigned to the plugin it is assumed that the MapView has the default cursor. } @@ -1004,6 +1036,47 @@ begin end; +{ TMarkerDragPlugin } + +constructor TMarkerDragPlugin.Create(AOwner: TComponent); +begin + inherited; + FDragCursor := crSizeAll; +end; + +procedure TMarkerDragPlugin.MouseDown(AMapView: TMapView; {%H-}Button: TMouseButton; + AShift: TShiftState; X,Y: Integer; var Handled: Boolean); +begin + inherited; + if FMouseDown then + Handled := true; +end; + +procedure TMarkerDragPlugin.MouseMove(AMapView: TMapView; + {%H-}AShift: TShiftState; X,Y: Integer; var Handled: Boolean); +var + P: TRealPoint; +begin + inherited; + + if FMouseDown then + begin + AMapView.Cursor := FDragCursor; + P := AMapView.ScreenToLatLon(Point(X, Y)); + FOrigGPSPoint.MoveTo(P.Lon, P.Lat); + Update; + Handled := true; + end; +end; + +procedure TMarkerDragPlugin.MouseUp(AMapView: TMapView; {%H-}Button: TMouseButton; + AShift: TShiftState; X,Y: Integer; var Handled: Boolean); +begin + inherited; + AMapView.Cursor := FSavedCursor; +end; + + { TMarkerHintPlugin } constructor TMarkerHintPlugin.Create(AOwner: TComponent); @@ -1386,8 +1459,9 @@ initialization RegisterPluginClass(TTileInfoPlugin, 'Tile info'); RegisterPluginClass(TLegalNoticePlugin, 'Legal notice'); RegisterPluginClass(TLinkedMapsPlugin, 'Linked maps'); - RegisterPluginClass(TMarkerClickPlugin, 'Marker click'); RegisterPluginClass(TMarkerHintPlugin, 'Marker hint'); + RegisterPluginClass(TMarkerClickPlugin, 'Marker click'); + RegisterPluginClass(TMarkerDragPlugin, 'Marker drag'); RegisterPluginClass(TDraggableMarkerPlugin, 'Draggable marker'); RegisterPluginClass(TUserDefinedPlugin, 'User-defined');