From 9fbadf1f645f1763e2b851b5dfdbae27593e9320 Mon Sep 17 00:00:00 2001 From: gosha20777 Date: Thu, 27 Jun 2019 17:24:29 +0300 Subject: [PATCH] feat: zoom function --- RescuerLaApp/Models/Zoomer.cs | 46 +++++++++++++++ RescuerLaApp/RescuerLaApp.csproj | 1 + .../ViewModels/MainWindowViewModel.cs | 20 ++----- RescuerLaApp/Views/MainWindow.xaml | 59 ++++++++++--------- RescuerLaApp/Views/MainWindow.xaml.cs | 15 +++++ 5 files changed, 97 insertions(+), 44 deletions(-) create mode 100644 RescuerLaApp/Models/Zoomer.cs diff --git a/RescuerLaApp/Models/Zoomer.cs b/RescuerLaApp/Models/Zoomer.cs new file mode 100644 index 0000000..de9217d --- /dev/null +++ b/RescuerLaApp/Models/Zoomer.cs @@ -0,0 +1,46 @@ +using System; +using Avalonia.Controls.PanAndZoom; +using Avalonia.Input; + +namespace RescuerLaApp.Models +{ + public class Zoomer + { + private static ZoomBorder _zoomBorder = null; + + public static void Init(ZoomBorder zoomBorder) + { + _zoomBorder = zoomBorder; + } + + public static void Zoom(double scale) + { + _zoomBorder?.ZoomTo(scale, 0, 0); + } + + public static void MoveTo(double x, double y) + { + _zoomBorder?.PanTo(0.1,0); + } + + public static double GetZoomX() + { + return _zoomBorder?.ZoomX ?? 1; + } + public static double GetZoomY() + { + return _zoomBorder?.ZoomY ?? 1; + } + + public static void Reset() + { + _zoomBorder?.Reset(); + } + + public static event EventHandler KeyDown + { + add => _zoomBorder.KeyDown+=value; + remove => _zoomBorder.KeyDown+=value; + } + } +} \ No newline at end of file diff --git a/RescuerLaApp/RescuerLaApp.csproj b/RescuerLaApp/RescuerLaApp.csproj index ac7510c..36bc32a 100644 --- a/RescuerLaApp/RescuerLaApp.csproj +++ b/RescuerLaApp/RescuerLaApp.csproj @@ -14,6 +14,7 @@ + diff --git a/RescuerLaApp/ViewModels/MainWindowViewModel.cs b/RescuerLaApp/ViewModels/MainWindowViewModel.cs index 35fc8de..e2996b4 100644 --- a/RescuerLaApp/ViewModels/MainWindowViewModel.cs +++ b/RescuerLaApp/ViewModels/MainWindowViewModel.cs @@ -148,16 +148,12 @@ private async void PredictAll() private void ShrinkCanvas() { - CanvasWidth -= CanvasWidth * 0.25; - CanvasHeight -= CanvasHeight * 0.25; - UpdateUi(); + Zoomer.Zoom(0.8); } private void IncreaseCanvas() { - CanvasWidth += CanvasWidth * 0.25; - CanvasHeight += CanvasHeight * 0.25; - UpdateUi(); + Zoomer.Zoom(1.2); } private async void OpenFile() @@ -287,18 +283,10 @@ private void UpdateUi() { /*TODO: Вынести сюда все функции обновления UI*/ ImageBrush.Source = new Bitmap(Frames[SelectedIndex].Patch); //replace to frame.load(...) - CanvasHeight = CanvasWidth * ImageBrush.Source.PixelSize.Height / ImageBrush.Source.PixelSize.Width; - //Frames[SelectedIndex].Resize(CanvasWidth, CanvasHeight); + CanvasHeight = ImageBrush.Source.PixelSize.Height; + CanvasWidth = ImageBrush.Source.PixelSize.Width; if (Frames[SelectedIndex].Rectangles != null && Frames[SelectedIndex].Rectangles.Count > 0) { - var scaleX = CanvasWidth / ImageBrush.Source.PixelSize.Width; - var scaleY = CanvasHeight / ImageBrush.Source.PixelSize.Height; - Console.WriteLine($"{ImageBrush.Source.PixelSize.Width} x {ImageBrush.Source.PixelSize.Height}"); - Console.WriteLine($"{CanvasWidth} x {CanvasHeight}"); - foreach (var box in Frames[SelectedIndex].Rectangles) - { - box.Update(scaleX, scaleY); - } BoundBoxes = new List(Frames[SelectedIndex].Rectangles); } else diff --git a/RescuerLaApp/Views/MainWindow.xaml b/RescuerLaApp/Views/MainWindow.xaml index e864b69..a9a0101 100644 --- a/RescuerLaApp/Views/MainWindow.xaml +++ b/RescuerLaApp/Views/MainWindow.xaml @@ -2,6 +2,8 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:RescuerLaApp.ViewModels;assembly=RescuerLaApp" xmlns:models="clr-namespace:RescuerLaApp.Models" + xmlns:paz="clr-namespace:Avalonia.Controls.PanAndZoom;assembly=Avalonia.Controls.PanAndZoom" + UseLayoutRounding="True" Icon="resm:RescuerLaApp.Assets.avalonia-logo.ico" Title="RescuerLaApp" Width = "800" @@ -89,34 +91,35 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + diff --git a/RescuerLaApp/Views/MainWindow.xaml.cs b/RescuerLaApp/Views/MainWindow.xaml.cs index 8004786..7a1a8fa 100644 --- a/RescuerLaApp/Views/MainWindow.xaml.cs +++ b/RescuerLaApp/Views/MainWindow.xaml.cs @@ -1,12 +1,17 @@ using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.PanAndZoom; +using Avalonia.Input; using Avalonia.Markup.Xaml; using RescuerLaApp.ViewModels; using ReactiveUI; +using RescuerLaApp.Models; namespace RescuerLaApp.Views { public sealed class MainWindow : ReactiveWindow { + ZoomBorder z = new ZoomBorder(); public MainWindow() { AvaloniaXamlLoader.Load(this); @@ -14,6 +19,16 @@ public MainWindow() #if DEBUG this.AttachDevTools(); #endif + Zoomer.Init(this.Find("zoomBorder")); + Zoomer.KeyDown += ZoomBorder_KeyDown; + } + + private void ZoomBorder_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.R) + { + Zoomer.Reset(); + } } } }