2DLightPathSimulator/LensSimulatorWPF/MainWindow.xaml.cs

144 lines
4.6 KiB
C#
Raw Normal View History

2023-07-12 12:27:45 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace LensSimulatorWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Point _lastDragPoint;
private Matrix _currentMatrix = Matrix.Identity;
private const double ScaleRate = 1.1;
private const double MinScale = 0.5;
private const double MaxScale = 10.0;
private Point lastZoomPos = new Point(0, 0);
public MainWindow()
{
InitializeComponent();
DrawGrid();
this.MouseWheel += MainWindow_MouseWheel;
this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
this.MouseLeftButtonUp += MainWindow_MouseLeftButtonUp;
this.MouseMove += MainWindow_MouseMove;
}
private void MainWindow_MouseWheel(object sender, MouseWheelEventArgs e)
{
var scale = e.Delta > 0 ? ScaleRate : 1 / ScaleRate;
// Clamp the scale in between the min and max scale
if (_currentMatrix.M11 * scale < MinScale || _currentMatrix.M11 * scale > MaxScale)
{
return;
}
// Get the mouse position in screen space (relative to canvas)
var screenMousePos = e.GetPosition(canvas);
// Convert the screen mouse position to the canvas space
var matrix = _currentMatrix;
matrix.Invert();
var targetPointInCanvas = matrix.Transform(screenMousePos);
lastZoomPos = targetPointInCanvas;
// Perform the scale operation at the target point
_currentMatrix.ScaleAtPrepend(scale, scale, canvas.ActualWidth/2, canvas.ActualHeight/ 2);
// Apply the transformation to the canvas
canvas.RenderTransform = new MatrixTransform(_currentMatrix);
DrawGrid();
}
private void DrawGrid()
{
canvas.Children.Clear();
double step = 50; // The M11 component of the matrix represents the scale in X
double width = canvas.ActualWidth;
double height = canvas.ActualHeight;
for (double i = step; i < width; i += step)
{
canvas.Children.Add(new Line
{
Stroke = Brushes.LightGray,
X1 = i,
Y1 = 0,
X2 = i,
Y2 = height
});
}
for (double i = step; i < height; i += step)
{
canvas.Children.Add(new Line
{
Stroke = Brushes.LightGray,
X1 = 0,
Y1 = i,
X2 = width,
Y2 = i
});
}
var ellipse = new Ellipse
{
Fill = Brushes.Red,
Width = 10,
Height = 10
};
Canvas.SetLeft(ellipse, lastZoomPos.X - ellipse.Width / 2);
Canvas.SetTop(ellipse, lastZoomPos.Y - ellipse.Height / 2);
canvas.Children.Add(ellipse);
}
private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var mousePos = e.GetPosition(this);
_lastDragPoint = new Point(mousePos.X - _currentMatrix.OffsetX, mousePos.Y - _currentMatrix.OffsetY);
Mouse.Capture(this);
}
private void MainWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
}
private void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var posNow = e.GetPosition(this);
var deltaX = posNow.X - _lastDragPoint.X - _currentMatrix.OffsetX;
var deltaY = posNow.Y - _lastDragPoint.Y - _currentMatrix.OffsetY;
_currentMatrix.Translate(deltaX, deltaY);
canvas.RenderTransform = new MatrixTransform(_currentMatrix);
_lastDragPoint = new Point(posNow.X - _currentMatrix.OffsetX, posNow.Y - _currentMatrix.OffsetY);
}
}
}
}