Browse Source

Implemented Polygon, Polyline, PolylineGeometry.

Added unit-tests with data and demo code in TestApplication.
pull/398/head
Ivan Kochurkin 11 years ago
parent
commit
5c969c40b8
  1. 37
      samples/TestApplicationShared/MainWindow.cs
  2. 2
      src/Perspex.Controls/Perspex.Controls.csproj
  3. 28
      src/Perspex.Controls/Shapes/Polygon.cs
  4. 33
      src/Perspex.Controls/Shapes/Polyline.cs
  5. 50
      src/Perspex.SceneGraph/Media/PolylineGeometry.cs
  6. 2
      src/Perspex.SceneGraph/Perspex.SceneGraph.csproj
  7. 2
      tests/Perspex.RenderTests/Perspex.Cairo.RenderTests.csproj
  8. 2
      tests/Perspex.RenderTests/Perspex.Direct2D1.RenderTests.csproj
  9. 2
      tests/Perspex.RenderTests/Perspex.Skia.RenderTests.csproj
  10. 24
      tests/Perspex.RenderTests/Shapes/PolygonTests.cs
  11. 52
      tests/Perspex.RenderTests/Shapes/PolylineTests.cs
  12. BIN
      tests/TestFiles/Cairo/Shapes/Polygon/Polygon_1px_Stroke.expected.png
  13. BIN
      tests/TestFiles/Cairo/Shapes/Polyline/Polyline_1px_Stroke.expected.png
  14. BIN
      tests/TestFiles/Direct2D1/Shapes/Polygon/Polygon_1px_Stroke.expected.png
  15. BIN
      tests/TestFiles/Direct2D1/Shapes/Polyline/Polyline_1px_Stroke.expected.png
  16. BIN
      tests/TestFiles/Skia/Shapes/Polygon/Polygon_1px_Stroke.expected.png
  17. BIN
      tests/TestFiles/Skia/Shapes/Polyline/Polyline_1px_Stroke.expected.png

37
samples/TestApplicationShared/MainWindow.cs

@ -542,6 +542,17 @@ namespace TestApplication
private static TabItem LayoutTab()
{
var polylinePoints = new Point[] { new Point(0, 0), new Point(5, 0), new Point(6, -2), new Point(7, 3), new Point(8, -3),
new Point(9, 1), new Point(10, 0), new Point(15, 0) };
var polygonPoints = new Point[] { new Point(5, 0), new Point(8, 8), new Point(0, 3), new Point(10, 3), new Point(2, 8) };
for (int i = 0; i < polylinePoints.Length; i++)
{
polylinePoints[i] = polylinePoints[i] * 13;
}
for (int i = 0; i < polygonPoints.Length; i++)
{
polygonPoints[i] = polygonPoints[i] * 15;
}
return new TabItem
{
Header = "Layout",
@ -690,13 +701,31 @@ namespace TestApplication
},
new Line
{
Width = 90,
Height = 70,
Stroke = Brushes.Red,
StrokeThickness = 2,
PointPair = new PointPair(120, 185, 30, 115)
},
new Perspex.Controls.Shapes.Path
{
Fill = Brushes.Orange,
Data = StreamGeometry.Parse("M 30,250 c 50,0 50,-50 c 50,0 50,50 h -50 v 50 l -50,-50 Z"),
},
new Polygon
{
Stroke = Brushes.DarkBlue,
Fill = Brushes.Violet,
Points = polygonPoints,
StrokeThickness = 1,
[Canvas.LeftProperty] = 150,
[Canvas.TopProperty] = 180,
},
new Polyline
{
Stroke = Brushes.Brown,
Points = polylinePoints,
[Canvas.LeftProperty] = 30,
[Canvas.TopProperty] = 120
}
[Canvas.TopProperty] = 350,
},
}
},
}

2
src/Perspex.Controls/Perspex.Controls.csproj

@ -48,6 +48,8 @@
<Compile Include="SelectionMode.cs" />
<Compile Include="Separator.cs" />
<Compile Include="Shapes\Line.cs" />
<Compile Include="Shapes\Polygon.cs" />
<Compile Include="Shapes\Polyline.cs" />
<Compile Include="SystemDialog.cs" />
<Compile Include="Generators\ITreeItemContainerGenerator.cs" />
<Compile Include="Generators\ItemContainers.cs" />

28
src/Perspex.Controls/Shapes/Polygon.cs

@ -0,0 +1,28 @@
using Perspex.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perspex.Controls.Shapes
{
public class Polygon : Shape
{
public static readonly PerspexProperty<IList<Point>> PointsProperty =
PerspexProperty.Register<Polygon, IList<Point>>("Points");
public IList<Point> Points
{
get { return GetValue(PointsProperty); }
set { SetValue(PointsProperty, value); }
}
public override Geometry DefiningGeometry => new PolylineGeometry(Points, true);
protected override Size MeasureOverride(Size availableSize)
{
return new Size(StrokeThickness, StrokeThickness);
}
}
}

33
src/Perspex.Controls/Shapes/Polyline.cs

@ -0,0 +1,33 @@
using Perspex.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perspex.Controls.Shapes
{
public class Polyline: Shape
{
public static readonly PerspexProperty<IList<Point>> PointsProperty =
PerspexProperty.Register<Polyline, IList<Point>>("Points");
public Polyline()
{
StrokeThickness = 1; // Default Thickness
}
public IList<Point> Points
{
get { return GetValue(PointsProperty); }
set { SetValue(PointsProperty, value); }
}
public override Geometry DefiningGeometry => new PolylineGeometry(Points, false);
protected override Size MeasureOverride(Size availableSize)
{
return new Size(StrokeThickness, StrokeThickness);
}
}
}

50
src/Perspex.SceneGraph/Media/PolylineGeometry.cs

@ -0,0 +1,50 @@
using Perspex.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perspex.Media
{
/// <summary>
/// Represents the geometry of an polyline or polygon.
/// </summary>
public class PolylineGeometry : Geometry
{
private IList<Point> _points;
private bool _isFilled;
public PolylineGeometry(IList<Point> points, bool isFilled)
{
_points = points;
_isFilled = isFilled;
IPlatformRenderInterface factory = PerspexLocator.Current.GetService<IPlatformRenderInterface>();
IStreamGeometryImpl impl = factory.CreateStreamGeometry();
using (IStreamGeometryContextImpl context = impl.Open())
{
if (points.Count > 0)
{
context.BeginFigure(points[0], isFilled);
for (int i = 1; i < points.Count; i++)
{
context.LineTo(points[i]);
}
context.EndFigure(isFilled);
}
}
PlatformImpl = impl;
}
/// <inheritdoc/>
public override Rect Bounds => PlatformImpl.Bounds;
/// <inheritdoc/>
public override Geometry Clone()
{
return new PolylineGeometry(new List<Point>(_points), _isFilled);
}
}
}

2
src/Perspex.SceneGraph/Perspex.SceneGraph.csproj

@ -71,6 +71,7 @@
<Compile Include="Media\GradientSpreadMethod.cs" />
<Compile Include="Media\GradientStop.cs" />
<Compile Include="Media\LineGeometry.cs" />
<Compile Include="Media\PolylineGeometry.cs" />
<Compile Include="Media\RadialGradientBrush.cs" />
<Compile Include="Media\LinearGradientBrush.cs" />
<Compile Include="Media\MediaExtensions.cs" />
@ -107,6 +108,7 @@
<Compile Include="NameScopeEventArgs.cs" />
<Compile Include="NameScopeExtensions.cs" />
<Compile Include="NameScope.cs" />
<Compile Include="PointPair.cs" />
<Compile Include="RelativePoint.cs" />
<Compile Include="Platform\IFormattedTextImpl.cs" />
<Compile Include="Platform\IBitmapImpl.cs" />

2
tests/Perspex.RenderTests/Perspex.Cairo.RenderTests.csproj

@ -74,6 +74,8 @@
<Compile Include="Shapes\LineTests.cs" />
<Compile Include="Shapes\PathTests.cs" />
<Compile Include="Shapes\EllipseTests.cs" />
<Compile Include="Shapes\PolygonTests.cs" />
<Compile Include="Shapes\PolylineTests.cs" />
<Compile Include="Shapes\RectangleTests.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

2
tests/Perspex.RenderTests/Perspex.Direct2D1.RenderTests.csproj

@ -79,6 +79,8 @@
<Compile Include="Shapes\LineTests.cs" />
<Compile Include="Shapes\PathTests.cs" />
<Compile Include="Shapes\EllipseTests.cs" />
<Compile Include="Shapes\PolygonTests.cs" />
<Compile Include="Shapes\PolylineTests.cs" />
<Compile Include="Shapes\RectangleTests.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

2
tests/Perspex.RenderTests/Perspex.Skia.RenderTests.csproj

@ -70,6 +70,8 @@
<Compile Include="Shapes\EllipseTests.cs" />
<Compile Include="Shapes\LineTests.cs" />
<Compile Include="Shapes\PathTests.cs" />
<Compile Include="Shapes\PolygonTests.cs" />
<Compile Include="Shapes\PolylineTests.cs" />
<Compile Include="Shapes\RectangleTests.cs" />
<Compile Include="TestBase.cs" />
</ItemGroup>

24
tests/Perspex.RenderTests/Shapes/PolygonTests.cs

@ -0,0 +1,24 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using Perspex.Controls;
using Perspex.Controls.Shapes;
using Perspex.Media;
using Xunit;
#if PERSPEX_CAIRO
namespace Perspex.Cairo.RenderTests.Shapes
#elif PERSPEX_SKIA
namespace Perspex.Skia.RenderTests
#else
namespace Perspex.Direct2D1.RenderTests.Shapes
#endif
{
public class PolygonTests : TestBase
{
public PolygonTests()
: base(@"Shapes\Polygon")
{
}
}
}

52
tests/Perspex.RenderTests/Shapes/PolylineTests.cs

@ -0,0 +1,52 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using Perspex.Controls;
using Perspex.Controls.Shapes;
using Perspex.Media;
using Xunit;
#if PERSPEX_CAIRO
namespace Perspex.Cairo.RenderTests.Shapes
#elif PERSPEX_SKIA
namespace Perspex.Skia.RenderTests
#else
namespace Perspex.Direct2D1.RenderTests.Shapes
#endif
{
public class PolylineTests : TestBase
{
public PolylineTests()
: base(@"Shapes\Polyline")
{
}
[Fact]
public void Polyline_1px_Stroke()
{
var polylinePoints = new Point[] { new Point(0, 0), new Point(5, 0), new Point(6, -2), new Point(7, 3), new Point(8, -3),
new Point(9, 1), new Point(10, 0), new Point(15, 0) };
var offsetPoint = new Point(0, 50);
for (int i = 0; i < polylinePoints.Length; i++)
{
polylinePoints[i] = polylinePoints[i] * 13 + offsetPoint;
}
Decorator target = new Decorator
{
Padding = new Thickness(8),
Width = 230,
Height = 130,
Child = new Polyline
{
Stroke = Brushes.Brown,
Points = polylinePoints,
StrokeThickness = 1
}
};
RenderToFile(target);
CompareImages();
}
}
}

BIN
tests/TestFiles/Cairo/Shapes/Polygon/Polygon_1px_Stroke.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
tests/TestFiles/Cairo/Shapes/Polyline/Polyline_1px_Stroke.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
tests/TestFiles/Direct2D1/Shapes/Polygon/Polygon_1px_Stroke.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
tests/TestFiles/Direct2D1/Shapes/Polyline/Polyline_1px_Stroke.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
tests/TestFiles/Skia/Shapes/Polygon/Polygon_1px_Stroke.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
tests/TestFiles/Skia/Shapes/Polyline/Polyline_1px_Stroke.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Loading…
Cancel
Save