Add a way to pan from the preview.
authorGreg Burri <greg.burri@gmail.com>
Wed, 13 Jan 2016 12:30:53 +0000 (13:30 +0100)
committerGreg Burri <greg.burri@gmail.com>
Wed, 13 Jan 2016 12:30:53 +0000 (13:30 +0100)
Parasitemia/Parasitemia/GUI/GUI.fs

index f88bc3c..2132363 100644 (file)
@@ -295,7 +295,32 @@ let run (defaultConfig: Config) =
         let height = srcImg.img.Height * width / srcImg.img.Width
         imgCtrl.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
         stackPreviews.Children.Add(imgCtrl) |> ignore
-        imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args -> setCurrentImage (state.SourceImages |> Seq.find (fun srcImg -> (srcImg :> Object) = imgCtrl.Tag)))
+
+        // Zoom to a mouse position into the control 'imgCtrl'.
+        let zoomTo (mousePos: Point) =
+            let canvasW = canvasCurrentImage.ActualWidth * currentScale
+            let canvasH = canvasCurrentImage.ActualHeight * currentScale
+            let centerX = (mousePos.X - imgCtrl.BorderThickness.Left) / (imgCtrl.ActualWidth - imgCtrl.BorderThickness.Left) * canvasW
+            let centerY = (mousePos.Y - imgCtrl.BorderThickness.Top) / (imgCtrl.ActualHeight - imgCtrl.BorderThickness.Top) * canvasH
+            scrollViewCurrentImage.ScrollToHorizontalOffset(centerX - scrollViewCurrentImage.ViewportWidth / 2. + borderCurrentImage.BorderThickness.Left)
+            scrollViewCurrentImage.ScrollToVerticalOffset(centerY - scrollViewCurrentImage.ViewportHeight / 2. + borderCurrentImage.BorderThickness.Top)
+
+        imgCtrl.MouseLeftButtonDown.AddHandler(fun obj args ->
+            setCurrentImage (state.SourceImages |> Seq.find (fun srcImg -> (srcImg :> Object) = imgCtrl.Tag))
+            imgCtrl.UpdateLayout()
+            zoomTo (args.GetPosition(imgCtrl))
+            imgCtrl.CaptureMouse() |> ignore)
+
+        imgCtrl.MouseMove.AddHandler(fun obj args ->
+            if imgCtrl.IsMouseCaptured
+            then
+                zoomTo (args.GetPosition(imgCtrl)))
+
+        imgCtrl.MouseLeftButtonUp.AddHandler(fun obj args ->
+            if imgCtrl.IsMouseCaptured
+            then
+                imgCtrl.ReleaseMouseCapture())
+
 
     let updatePreviews () =
         stackPreviews.Children.Clear ()