This commit is contained in:
Wiesław Šoltés 2021-07-05 21:44:43 +02:00
Родитель 7ab9566750
Коммит 6a08490c61
8 изменённых файлов: 234 добавлений и 0 удалений

8
NuGet.Config Normal file
Просмотреть файл

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="avalonia-all" value="https://nuget.avaloniaui.net/repository/avalonia-all/index.json" />
</packageSources>
</configuration>

16
PerspectiveDemo.sln Normal file
Просмотреть файл

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerspectiveDemo", "PerspectiveDemo\PerspectiveDemo.csproj", "{B17E0F8D-B865-48B7-BE2D-5CFE8FB26289}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B17E0F8D-B865-48B7-BE2D-5CFE8FB26289}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B17E0F8D-B865-48B7-BE2D-5CFE8FB26289}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B17E0F8D-B865-48B7-BE2D-5CFE8FB26289}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B17E0F8D-B865-48B7-BE2D-5CFE8FB26289}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,7 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="PerspectiveDemo.App">
<Application.Styles>
<FluentTheme Mode="Light"/>
</Application.Styles>
</Application>

Просмотреть файл

@ -0,0 +1,24 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
namespace PerspectiveDemo
{
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
}
base.OnFrameworkInitializationCompleted();
}
}
}

Просмотреть файл

@ -0,0 +1,28 @@
<Window xmlns="https://github.com/avaloniaui"
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"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="PerspectiveDemo.MainWindow"
Title="PerspectiveDemo">
<Panel>
<Canvas Name="Canvas" Width="300" Height="300" Background="LightGray" HorizontalAlignment="Center" VerticalAlignment="Center">
<Canvas.Styles>
<Style Selector="Thumb">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-5,-5,0,0" Width="10" Height="10" Fill="Black"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Canvas.Styles>
<Rectangle Name="Rectangle" Canvas.Left="0" Canvas.Top="0" Width="100" Height="100" Fill="Blue"/>
<Thumb Name="UL" Canvas.Left="100" Canvas.Top="100" DragDelta="OnDragDelta" ClipToBounds="False"/>
<Thumb Name="UR" Canvas.Left="200" Canvas.Top="100" DragDelta="OnDragDelta" ClipToBounds="False"/>
<Thumb Name="LL" Canvas.Left="100" Canvas.Top="200" DragDelta="OnDragDelta" ClipToBounds="False"/>
<Thumb Name="LR" Canvas.Left="200" Canvas.Top="200" DragDelta="OnDragDelta" ClipToBounds="False"/>
</Canvas>
</Panel>
</Window>

Просмотреть файл

@ -0,0 +1,117 @@
// https://github.com/AvaloniaUI/Avalonia/pull/6192
// https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/non-affine
// http://www.charlespetzold.com/blog/2007/08/250638.html
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
namespace PerspectiveDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
UpdateTransform();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void UpdateTransform()
{
var canvas = this.FindControl<Canvas>("Canvas");
var rectangle = this.FindControl<Rectangle>("Rectangle");
var width = rectangle.Width;
var height = rectangle.Height;
var ul = this.FindControl<Thumb>("UL");
var ur = this.FindControl<Thumb>("UR");
var ll = this.FindControl<Thumb>("LL");
var lr = this.FindControl<Thumb>("LR");
var ptUL = new Point(Canvas.GetLeft(ul), Canvas.GetTop(ul));
var ptUR = new Point(Canvas.GetLeft(ur), Canvas.GetTop(ur));
var ptLL = new Point(Canvas.GetLeft(ll), Canvas.GetTop(ll));
var ptLR = new Point(Canvas.GetLeft(lr), Canvas.GetTop(lr));
var result = ComputeMatrix(new Size(width, height), ptUL, ptUR, ptLL, ptLR);
rectangle.RenderTransformOrigin = RelativePoint.Center;
rectangle.RenderTransform = new MatrixTransform(result);
}
static Point MapPoint(Matrix matrix, Point point)
{
return new Point(
(point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.M31,
(point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.M32);
}
static Matrix ComputeMatrix(Size size, Point ptUL, Point ptUR, Point ptLL, Point ptLR)
{
// Scale transform
var S = Matrix.CreateScale(1 / size.Width, 1 / size.Height);
// Affine transform
var A = new Matrix(
ptUR.X - ptUL.X,
ptUR.Y - ptUL.Y,
0,
ptLL.X - ptUL.X,
ptLL.Y - ptUL.Y,
0,
ptUL.X,
ptUL.Y,
1);
// Non-Affine transform
//A.TryInvert(out var inverseA);
//var abPoint = MapPoint(inverseA, ptLR);
//var a = abPoint.X;
//var b = abPoint.Y;
double den = A.M11 * A.M22 - A.M12 * A.M21;
double a = (A.M22 * ptLR.X - A.M21 * ptLR.Y + A.M21 * A.M32 - A.M22 * A.M31) / den;
double b = (A.M11 * ptLR.Y - A.M12 * ptLR.X + A.M12 * A.M31 - A.M11 * A.M32) / den;
var scaleX = a / (a + b - 1);
var scaleY = b / (a + b - 1);
var N = new Matrix(
scaleX,
0,
scaleX - 1,
0,
scaleY,
scaleY - 1,
0,
0,
1);
// Multiply S * N * A
var result = S * N * A;
return result;
}
private void OnDragDelta(object? sender, VectorEventArgs e)
{
if (sender is Thumb thumb)
{
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.Vector.X);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.Vector.Y);
UpdateTransform();
}
}
}
}

Просмотреть файл

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.999-cibuild0014847-beta"/>
<PackageReference Include="Avalonia.Desktop" Version="0.10.999-cibuild0014847-beta"/>
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.999-cibuild0014847-beta"/>
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,22 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
namespace PerspectiveDemo
{
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace();
}
}