Skip to content

Commit

Permalink
Merge pull request #19 from gosha20777/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gosha20777 authored Jun 27, 2019
2 parents 2ba45e3 + 8e75b4a commit e0477e0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 44 deletions.
46 changes: 46 additions & 0 deletions RescuerLaApp/Models/Zoomer.cs
Original file line number Diff line number Diff line change
@@ -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<KeyEventArgs> KeyDown
{
add => _zoomBorder.KeyDown+=value;
remove => _zoomBorder.KeyDown+=value;
}
}
}
1 change: 1 addition & 0 deletions RescuerLaApp/RescuerLaApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.8.0" />
<PackageReference Include="Avalonia.Controls.PanAndZoom" Version="2.1.0" />
<PackageReference Include="Avalonia.Desktop" Version="0.8.0" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.8.0" />
<PackageReference Include="Avalonia.Skia.Linux.Natives" Version="1.68" />
Expand Down
20 changes: 4 additions & 16 deletions RescuerLaApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<BoundBox>(Frames[SelectedIndex].Rectangles);
}
else
Expand Down
59 changes: 31 additions & 28 deletions RescuerLaApp/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -89,34 +91,35 @@
</StackPanel>
</Grid>
<ScrollViewer Grid.Column="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<!--
</DataTemplate>
-->
<ItemsControl
HorizontalAlignment="Center"
VerticalAlignment="Center"
Items="{Binding BoundBoxes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Name="ShowedImage"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{Binding ImageBrush}"
Height="{Binding CanvasHeight, Mode=TwoWay}"
Width="{Binding CanvasWidth, Mode=TwoWay}"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type models:BoundBox}">
<Polygon
Points="{Binding Points}"
Stroke="Blue" StrokeThickness="2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<paz:ZoomBorder Name="zoomBorder" Stretch="None" ZoomSpeed="1.2"
ClipToBounds="True" Focusable="True"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
>
<ItemsControl
HorizontalAlignment="Center"
VerticalAlignment="Center"
Items="{Binding BoundBoxes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Name="ShowedImage"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{Binding ImageBrush}"
Height="{Binding CanvasHeight, Mode=TwoWay}"
Width="{Binding CanvasWidth, Mode=TwoWay}"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type models:BoundBox}">
<Polygon
Points="{Binding Points}"
Stroke="Blue" StrokeThickness="2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</paz:ZoomBorder>
</ScrollViewer>
</Grid>
<!-- Grid 2 -->
Expand Down
15 changes: 15 additions & 0 deletions RescuerLaApp/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
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<MainWindowViewModel>
{
ZoomBorder z = new ZoomBorder();
public MainWindow()
{
AvaloniaXamlLoader.Load(this);
this.WhenActivated(disposables => { });
#if DEBUG
this.AttachDevTools();
#endif
Zoomer.Init(this.Find<ZoomBorder>("zoomBorder"));
Zoomer.KeyDown += ZoomBorder_KeyDown;
}

private void ZoomBorder_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.R)
{
Zoomer.Reset();
}
}
}
}

0 comments on commit e0477e0

Please sign in to comment.