added zoom feature
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Shapes;
|
using Avalonia.Controls.Shapes;
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
|
using Avalonia.VisualTree;
|
||||||
|
using DelauneyTriangulation.ViewModels;
|
||||||
|
|
||||||
namespace DelauneyTriangulation.Views;
|
namespace DelauneyTriangulation.Views;
|
||||||
|
|
||||||
@@ -12,12 +16,24 @@ public partial class MainWindow : Window
|
|||||||
private bool _isPanning;
|
private bool _isPanning;
|
||||||
private Point _panStart;
|
private Point _panStart;
|
||||||
private Vector _panOffset = new();
|
private Vector _panOffset = new();
|
||||||
|
|
||||||
|
private double _scale = 1.0;
|
||||||
|
private const double MinScale = 0.2;
|
||||||
|
private const double MaxScale = 8.0;
|
||||||
|
|
||||||
private readonly TranslateTransform _panTransform = new();
|
private readonly TranslateTransform _panTransform = new();
|
||||||
|
private readonly ScaleTransform _scaleTransform = new();
|
||||||
|
private readonly TransformGroup _transformGroup = new();
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
SceneContent.RenderTransform = _panTransform;
|
|
||||||
|
_transformGroup.Children.Add(_scaleTransform);
|
||||||
|
_transformGroup.Children.Add(_panTransform);
|
||||||
|
SceneContent.RenderTransform = _transformGroup;
|
||||||
|
|
||||||
|
if (DataContext is MainWindowViewModel vm) vm.View = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTitleBarPointerPressed(object? sender, PointerPressedEventArgs e)
|
private void OnTitleBarPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||||
@@ -64,6 +80,24 @@ public partial class MainWindow : Window
|
|||||||
|
|
||||||
private void Scene_OnWheel(object? sender, PointerWheelEventArgs e)
|
private void Scene_OnWheel(object? sender, PointerWheelEventArgs e)
|
||||||
{
|
{
|
||||||
|
var mouse = e.GetPosition(Scene);
|
||||||
|
|
||||||
|
var oldScale = _scale;
|
||||||
|
var factor = Math.Pow(1.1, e.Delta.Y);
|
||||||
|
var newScale = Math.Clamp(oldScale * factor, MinScale, MaxScale);
|
||||||
|
var applied = newScale / oldScale;
|
||||||
|
|
||||||
|
_scale = newScale;
|
||||||
|
_scaleTransform.ScaleX = _scaleTransform.ScaleY = _scale;
|
||||||
|
|
||||||
|
_panOffset = new Vector(
|
||||||
|
mouse.X - applied * (mouse.X - _panOffset.X),
|
||||||
|
mouse.Y - applied * (mouse.Y - _panOffset.Y)
|
||||||
|
);
|
||||||
|
|
||||||
|
_panTransform.X = _panOffset.X;
|
||||||
|
_panTransform.Y = _panOffset.Y;
|
||||||
|
|
||||||
|
e.Handled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user