Browse Source

share linebouds helper with render demo. remove hacky test code.

pull/3909/head
Dan Walmsley 6 years ago
parent
commit
957eef7dba
  1. 27
      samples/RenderDemo/Controls/LineBoundsDemoControl.cs
  2. 3
      samples/RenderDemo/RenderDemo.csproj
  3. 6
      src/Avalonia.Visuals/Rendering/SceneGraph/LineBoundsHelper.cs

27
samples/RenderDemo/Controls/LineBoundsDemoControl.cs

@ -9,24 +9,34 @@ namespace RenderDemo.Controls
{ {
public class LineBoundsDemoControl : Control public class LineBoundsDemoControl : Control
{ {
private double angle = Math.PI / 8; static LineBoundsDemoControl()
{
AffectsRender<LineBoundsDemoControl>(AngleProperty);
}
public static double CalculateOppSide(double angle, double hyp) public LineBoundsDemoControl()
{ {
return Math.Sin(angle) * hyp; var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1 / 60);
timer.Tick += (sender, e) => Angle += Math.PI / 360;
timer.Start();
} }
public static double CalculateAdjSide(double angle, double hyp) public static readonly StyledProperty<double> AngleProperty =
AvaloniaProperty.Register<LineBoundsDemoControl, double>(nameof(Angle));
public double Angle
{ {
return Math.Cos(angle) * hyp; get => GetValue(AngleProperty);
set => SetValue(AngleProperty, value);
} }
public override void Render(DrawingContext drawingContext) public override void Render(DrawingContext drawingContext)
{ {
var lineLength = Math.Sqrt((100 * 100) + (100 * 100)); var lineLength = Math.Sqrt((100 * 100) + (100 * 100));
var diffX = CalculateAdjSide(angle, lineLength); var diffX = LineBoundsHelper.CalculateAdjSide(Angle, lineLength);
var diffY = CalculateOppSide(angle, lineLength); var diffY = LineBoundsHelper.CalculateOppSide(Angle, lineLength);
var p1 = new Point(200, 200); var p1 = new Point(200, 200);
@ -38,9 +48,6 @@ namespace RenderDemo.Controls
drawingContext.DrawLine(pen, p1, p2); drawingContext.DrawLine(pen, p1, p2);
drawingContext.DrawRectangle(boundPen, LineBoundsHelper.CalculateBounds(p1, p2, pen)); drawingContext.DrawRectangle(boundPen, LineBoundsHelper.CalculateBounds(p1, p2, pen));
angle += Math.PI / 360;
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
} }
} }
} }

3
samples/RenderDemo/RenderDemo.csproj

@ -3,6 +3,9 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Include="..\..\src\Avalonia.Visuals\Rendering\SceneGraph\LineBoundsHelper.cs" Link="LineBoundsHelper.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\Avalonia.Diagnostics\Avalonia.Diagnostics.csproj" /> <ProjectReference Include="..\..\src\Avalonia.Diagnostics\Avalonia.Diagnostics.csproj" />
<ProjectReference Include="..\..\src\Linux\Avalonia.LinuxFramebuffer\Avalonia.LinuxFramebuffer.csproj" /> <ProjectReference Include="..\..\src\Linux\Avalonia.LinuxFramebuffer\Avalonia.LinuxFramebuffer.csproj" />

6
src/Avalonia.Visuals/Rendering/SceneGraph/LineBoundsHelper.cs

@ -3,7 +3,7 @@ using Avalonia.Media;
namespace Avalonia.Rendering.SceneGraph namespace Avalonia.Rendering.SceneGraph
{ {
public static class LineBoundsHelper internal static class LineBoundsHelper
{ {
private static double CalculateAngle(Point p1, Point p2) private static double CalculateAngle(Point p1, Point p2)
{ {
@ -13,12 +13,12 @@ namespace Avalonia.Rendering.SceneGraph
return Math.Atan2(yDiff, xDiff); return Math.Atan2(yDiff, xDiff);
} }
private static double CalculateOppSide(double angle, double hyp) internal static double CalculateOppSide(double angle, double hyp)
{ {
return Math.Sin(angle) * hyp; return Math.Sin(angle) * hyp;
} }
private static double CalculateAdjSide(double angle, double hyp) internal static double CalculateAdjSide(double angle, double hyp)
{ {
return Math.Cos(angle) * hyp; return Math.Cos(angle) * hyp;
} }

Loading…
Cancel
Save