This commit is contained in:
Joseph Roy 2023-07-12 13:27:45 +01:00
parent f85d3a9d04
commit 4a6d128216
8 changed files with 232 additions and 1 deletions

View File

@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImplementationTest", "Imple
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LensSimulatorWindows", "LensSimulatorWindows\LensSimulatorWindows.csproj", "{ACDB5AA7-0C3D-4923-9843-E850C3807041}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LensSimulatorWindows", "LensSimulatorWindows\LensSimulatorWindows.csproj", "{ACDB5AA7-0C3D-4923-9843-E850C3807041}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LensSimulator-WinForms", "LensSimulator-WinForms\LensSimulator-WinForms.csproj", "{F16F2F39-B341-4591-8D1E-A0BAC484248E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LensSimulator-WinForms", "LensSimulator-WinForms\LensSimulator-WinForms.csproj", "{F16F2F39-B341-4591-8D1E-A0BAC484248E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LensSimulatorWPF", "LensSimulatorWPF\LensSimulatorWPF.csproj", "{0C57DE67-4CA1-4F8D-9D9F-9DF8FD1C87E1}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,6 +35,10 @@ Global
{F16F2F39-B341-4591-8D1E-A0BAC484248E}.Debug|Any CPU.Build.0 = Debug|Any CPU {F16F2F39-B341-4591-8D1E-A0BAC484248E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F16F2F39-B341-4591-8D1E-A0BAC484248E}.Release|Any CPU.ActiveCfg = Release|Any CPU {F16F2F39-B341-4591-8D1E-A0BAC484248E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F16F2F39-B341-4591-8D1E-A0BAC484248E}.Release|Any CPU.Build.0 = Release|Any CPU {F16F2F39-B341-4591-8D1E-A0BAC484248E}.Release|Any CPU.Build.0 = Release|Any CPU
{0C57DE67-4CA1-4F8D-9D9F-9DF8FD1C87E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C57DE67-4CA1-4F8D-9D9F-9DF8FD1C87E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C57DE67-4CA1-4F8D-9D9F-9DF8FD1C87E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C57DE67-4CA1-4F8D-9D9F-9DF8FD1C87E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,9 @@
<Application x:Class="LensSimulatorWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LensSimulatorWPF"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace LensSimulatorWPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<ItemGroup>
<ApplicationDefinition Update="App.xaml">
<SubType>Designer</SubType>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>

View File

@ -0,0 +1,21 @@
<Window x:Class="LensSimulatorWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LensSimulatorWPF"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="500">
<Grid Background="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="15*"/>
</Grid.RowDefinitions>
<Grid Background="Wheat ">
<Canvas Name="canvas" Background="White" Width="1000" Height="1000"/>
</Grid>
</Grid>
</Grid>
</Window>

View File

@ -0,0 +1,144 @@
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);
}
}
}
}