Browse Source

Add code review changes:

Fix typos.
Simplify code.
Generalize code.
Remove duplications.
Improve performance.
pull/7402/head
Jan-Peter Zurek 5 years ago
parent
commit
3e7f190ff9
  1. 234
      samples/RenderDemo/Pages/Transform3DPage.axaml
  2. 2
      src/Android/Avalonia.AndroidTestApplication/Resources/Resource.Designer.cs
  3. 124
      src/Avalonia.Visuals/Animation/Transitions/Rotate3DTransition.cs
  4. 12
      src/Avalonia.Visuals/Matrix.cs
  5. 2
      src/Avalonia.Visuals/Media/Rotate3DTransform.cs
  6. 7
      src/Avalonia.Visuals/Point.cs

234
samples/RenderDemo/Pages/Transform3DPage.axaml

@ -21,149 +21,139 @@
</Template>
</Styles.Resources>
</Styles>
<Style Selector="Border.Test">
<Setter Property="Width" Value="200" />
<Setter Property="Height" Value="200" />
<Setter Property="Child" Value="{StaticResource TestContent}" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Grid.ColumnSpan" Value="2" />
</Style>
<Style Selector="TextBlock, Label, Slider">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="10,0,10,0" />
</Style>
<Style Selector="Border TextBlock">
<Setter Property="Foreground" Value="White" />
</Style>
<Style Selector="Border Button">
<Setter Property="Background" Value="White"></Setter>
<Setter Property="Foreground" Value="Black" />
</Style>
<Style Selector="Border#B1">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="0" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="25%">
<Setter Property="Rotate3DTransform.AngleX" Value="90" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="Border#B2">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="90" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="25%">
<Setter Property="Rotate3DTransform.AngleX" Value="180" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="75%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="450" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="Border#B3">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="180" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="50%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="75%">
<Setter Property="Rotate3DTransform.AngleX" Value="450" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="540" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="Border#B4">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="270" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="25%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="50%">
<Setter Property="Rotate3DTransform.AngleX" Value="450" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="630" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</UserControl.Styles>
<Grid ColumnDefinitions="Auto,*,Auto,*" RowDefinitions="*, Auto, Auto, Auto, Auto, Auto, Auto, Auto">
<Grid.Clock>
<Clock />
</Grid.Clock>
<Grid.Styles>
<Style Selector="Border.Test">
<Setter Property="Width" Value="200" />
<Setter Property="Height" Value="200" />
<Setter Property="Child" Value="{StaticResource TestContent}" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Grid.ColumnSpan" Value="2" />
</Style>
<Style Selector="TextBlock, Label, Slider">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="10,0,10,0" />
</Style>
<Style Selector="Border TextBlock">
<Setter Property="Foreground" Value="White" />
</Style>
<Style Selector="Border Button">
<Setter Property="Background" Value="White"></Setter>
<Setter Property="Foreground" Value="Black" />
</Style>
</Grid.Styles>
<Border Background="DarkRed" Classes="Test">
<Border.Styles>
<Style Selector="Border">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="0" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="25%">
<Setter Property="Rotate3DTransform.AngleX" Value="90" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Border.Styles>
<Border Name="B1" Background="DarkRed" Classes="Test">
<Border.RenderTransform>
<Rotate3DTransform CenterZ="-100"
Depth="{Binding Depth}" />
</Border.RenderTransform>
</Border>
<Border Grid.Row="0" Grid.Column="0" Classes="Test" Background="DarkGreen">
<Border.Styles>
<Style Selector="Border">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="90" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="25%">
<Setter Property="Rotate3DTransform.AngleX" Value="180" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="75%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="450" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Border.Styles>
<Border Name="B2" Grid.Row="0" Grid.Column="0" Classes="Test" Background="DarkGreen">
<Border.RenderTransform>
<Rotate3DTransform CenterZ="-100"
Depth="{Binding Depth}" />
</Border.RenderTransform>
</Border>
<Border Grid.Row="0" Grid.Column="0" Classes="Test" Background="DarkBlue">
<Border.Styles>
<Style Selector="Border">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="180" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="50%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="75%">
<Setter Property="Rotate3DTransform.AngleX" Value="450" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="540" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Border.Styles>
<Border Name="B3" Grid.Row="0" Grid.Column="0" Classes="Test" Background="DarkBlue">
<Border.RenderTransform>
<Rotate3DTransform CenterZ="-100"
Depth="{Binding Depth}" />
</Border.RenderTransform>
</Border>
<Border Grid.Row="0" Grid.Column="0" Classes="Test" Background="Orange">
<Border.Styles>
<Style Selector="Border">
<Style.Animations>
<Animation Duration="0:0:10"
IterationCount="Infinite">
<KeyFrame Cue="0%">
<Setter Property="Rotate3DTransform.AngleX" Value="270" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="25%">
<Setter Property="Rotate3DTransform.AngleX" Value="360" />
<Setter Property="ZIndex" Value="4" />
</KeyFrame>
<KeyFrame Cue="50%">
<Setter Property="Rotate3DTransform.AngleX" Value="450" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Rotate3DTransform.AngleX" Value="630" />
<Setter Property="ZIndex" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Border.Styles>
<Border Name="B4" Grid.Row="0" Grid.Column="0" Classes="Test" Background="Orange">
<Border.RenderTransform>
<Rotate3DTransform CenterZ="-100"
Depth="{Binding Depth}" />

2
src/Android/Avalonia.AndroidTestApplication/Resources/Resource.Designer.cs

@ -14,7 +14,7 @@ namespace Avalonia.AndroidTestApplication
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.1.99.62")]
public partial class Resource
{

124
src/Avalonia.Visuals/Animation/Transitions/Rotate3DTransition.cs

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Media;
using Avalonia.Styling;
@ -9,18 +10,26 @@ namespace Avalonia.Animation;
public class Rotate3DTransition: PageSlide
{
/// <summary>
/// Creates a new instance if the <see cref="Rotate3DTransition"/>
/// Creates a new instance of the <see cref="Rotate3DTransition"/>
/// </summary>
/// <param name="duration">How long the rotation should take place</param>
/// <param name="orientation">The orientation of the rotation</param>
public Rotate3DTransition(TimeSpan duration, SlideAxis orientation = SlideAxis.Horizontal)
public Rotate3DTransition(TimeSpan duration, SlideAxis orientation = SlideAxis.Horizontal, double? depth = null)
: base(duration, orientation)
{}
{
Depth = depth;
}
/// <summary>
/// Defines the depth of the 3D Effect. If null, depth will be calculated automatically from the width or height
/// of the common parent of the visual being rotated.
/// </summary>
public double? Depth { get; set; }
/// <summary>
/// Creates a new instance if the <see cref="Rotate3DTransition"/>
/// Creates a new instance of the <see cref="Rotate3DTransition"/>
/// </summary>
public Rotate3DTransition() { }
@ -41,49 +50,32 @@ public class Rotate3DTransition: PageSlide
_ => throw new ArgumentOutOfRangeException()
};
var depthSetter = new Setter {Property = Rotate3DTransform.DepthProperty, Value = center};
var depthSetter = new Setter {Property = Rotate3DTransform.DepthProperty, Value = Depth ?? center};
var centerZSetter = new Setter {Property = Rotate3DTransform.CenterZProperty, Value = -center / 2};
KeyFrame CreateKeyFrame(double cue, double rotation, int zIndex) =>
new() {
Setters =
{
new Setter { Property = rotateProperty, Value = rotation },
new Setter { Property = Visual.ZIndexProperty, Value = zIndex },
centerZSetter,
depthSetter
},
Cue = new Cue(cue)
};
if (from != null)
{
var animation = new Animation
{
Easing = SlideOutEasing,
Duration = Duration,
Children =
{
new KeyFrame
{
Setters =
{
new Setter { Property = rotateProperty, Value = 0d },
new Setter { Property = Visual.ZIndexProperty, Value = 2 },
centerZSetter,
depthSetter,
},
Cue = new Cue(0d)
},
new KeyFrame
{
Setters =
{
new Setter { Property = rotateProperty, Value = 45d * (forward ? -1 : 1) },
new Setter { Property = Visual.ZIndexProperty, Value = 1 },
centerZSetter,
depthSetter
},
Cue = new Cue(0.5d)
},
new KeyFrame
{
Setters =
{
new Setter { Property = rotateProperty, Value = 90d * (forward ? -1 : 1) },
new Setter { Property = Visual.ZIndexProperty, Value = 1 },
centerZSetter,
depthSetter
},
Cue = new Cue(1d)
}
CreateKeyFrame(0d, 0d, 2),
CreateKeyFrame(0.5d, 45d * (forward ? -1 : 1), 1),
CreateKeyFrame(1d, 90d * (forward ? -1 : 1), 1)
}
};
@ -95,42 +87,13 @@ public class Rotate3DTransition: PageSlide
to.IsVisible = true;
var animation = new Animation
{
Easing = SlideInEasing,
Duration = Duration,
Children =
{
new KeyFrame
{
Setters =
{
new Setter { Property = rotateProperty, Value = 90d * (forward ? 1 : -1) },
new Setter { Property = Visual.ZIndexProperty, Value = 1 },
centerZSetter,
depthSetter
},
Cue = new Cue(0d)
},
new KeyFrame
{
Setters =
{
new Setter { Property = Visual.ZIndexProperty, Value = 1 },
new Setter { Property = rotateProperty, Value = 45d * (forward ? 1 : -1) },
centerZSetter,
depthSetter
},
Cue = new Cue(0.5d)
},
new KeyFrame
{
Setters =
{
new Setter { Property = rotateProperty, Value = 0d },
new Setter { Property = Visual.ZIndexProperty, Value = 2 },
centerZSetter,
depthSetter,
},
Cue = new Cue(1d)
}
CreateKeyFrame(0d, 90d * (forward ? 1 : -1), 1),
CreateKeyFrame(0.5d, 45d * (forward ? 1 : -1), 1),
CreateKeyFrame(1d, 0d, 2)
}
};
@ -139,15 +102,18 @@ public class Rotate3DTransition: PageSlide
await Task.WhenAll(tasks);
if (from != null && !cancellationToken.IsCancellationRequested)
if (!cancellationToken.IsCancellationRequested)
{
from.IsVisible = false;
from.ZIndex = 1;
}
if (from != null)
{
from.IsVisible = false;
from.ZIndex = 1;
}
if (to != null && !cancellationToken.IsCancellationRequested)
{
to.ZIndex = 2;
if (to != null)
{
to.ZIndex = 2;
}
}
}
}

12
src/Avalonia.Visuals/Matrix.cs

@ -348,15 +348,9 @@ namespace Avalonia
}
else
{
var x = p.X;
var y = p.Y;
var xAdd = y * M21 + M31;
var yAdd = x * M12 + M32;
x *= M11;
x += xAdd;
y *= M22;
y += yAdd;
transformedResult = new Point(x, y);
return new Point(
(p.X * M11) + (p.Y * M21) + M31,
(p.X * M12) + (p.Y * M22) + M32);
}
return transformedResult;

2
src/Avalonia.Visuals/Media/Rotate3DTransform.cs

@ -5,7 +5,7 @@ using Avalonia.Animation.Animators;
namespace Avalonia.Media;
/// <summary>
/// Non-Affine 3D transformation for rotating an visual around a definable axis
/// Non-Affine 3D transformation for rotating a visual around a definable axis
/// </summary>
public class Rotate3DTransform : Transform
{

7
src/Avalonia.Visuals/Point.cs

@ -169,12 +169,7 @@ namespace Avalonia
/// <param name="point">The point.</param>
/// <param name="matrix">The matrix.</param>
/// <returns>The resulting point.</returns>
public static Point operator *(Point point, Matrix matrix)
{
return new Point(
(point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.M31,
(point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.M32);
}
public static Point operator *(Point point, Matrix matrix) => matrix.Transform(point);
/// <summary>
/// Parses a <see cref="Point"/> string.

Loading…
Cancel
Save