Skip to content

Commit 16cc30f

Browse files
knocteaarani
andcommitted
[GTK] Some Shapes API support (1st part)
Supported Shapes: - Ellipse - Rectangle - Path Supported Path Segments: - Arc - Line Supported Brushes: - SolidColorBrush Upstream PR: xamarin#14235 Co-authored-by: Afshin Arani <[email protected]>
1 parent 28f50d9 commit 16cc30f

File tree

10 files changed

+405
-0
lines changed

10 files changed

+405
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Cairo;
3+
using Xamarin.Forms.Platform.GTK.Extensions;
4+
5+
namespace Xamarin.Forms.Platform.GTK.Controls
6+
{
7+
public class EllipseView : ShapeView
8+
{
9+
protected override void Draw(Gdk.Rectangle area, Context cr)
10+
{
11+
double width = _width;
12+
double height = _height;
13+
14+
cr.Translate(width / 2, height / 2);
15+
cr.Scale(width / 2, height / 2);
16+
cr.Arc(0, 0, 1, 0, 2 * Math.PI);
17+
18+
base.Draw(area, cr);
19+
}
20+
}
21+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Cairo;
4+
using Gdk;
5+
using Xamarin.Forms.Platform.GTK.Extensions;
6+
using Xamarin.Forms.Shapes;
7+
8+
namespace Xamarin.Forms.Platform.GTK.Controls
9+
{
10+
public class PathView : ShapeView
11+
{
12+
private PathGeometry _geometry;
13+
14+
public void UpdateGeometry(PathGeometry geometry)
15+
{
16+
_geometry = geometry;
17+
QueueDraw();
18+
}
19+
20+
protected override void Draw(Gdk.Rectangle area, Context cr)
21+
{
22+
cr.FillRule = _geometry.FillRule == Shapes.FillRule.EvenOdd ? Cairo.FillRule.EvenOdd : Cairo.FillRule.Winding;
23+
24+
foreach (var figure in _geometry.Figures)
25+
{
26+
cr.MoveTo(figure.StartPoint.X, figure.StartPoint.Y);
27+
28+
Point lastPoint = figure.StartPoint;
29+
30+
foreach (var segment in figure.Segments)
31+
{
32+
if (segment is LineSegment lineSegment)
33+
{
34+
cr.LineTo(lineSegment.Point.X, lineSegment.Point.Y);
35+
36+
lastPoint = lineSegment.Point;
37+
}
38+
else if (segment is ArcSegment arcSegment)
39+
{
40+
List<Point> points = new List<Point>();
41+
42+
GeometryHelper.FlattenArc(points,
43+
lastPoint,
44+
arcSegment.Point,
45+
arcSegment.Size.Width,
46+
arcSegment.Size.Height,
47+
arcSegment.RotationAngle,
48+
arcSegment.IsLargeArc,
49+
arcSegment.SweepDirection == SweepDirection.CounterClockwise,
50+
1);
51+
52+
for (int i = 0; i < points.Count; i++)
53+
{
54+
cr.LineTo(points[i].X, points[i].Y);
55+
}
56+
57+
if (points.Count > 0)
58+
lastPoint = points[points.Count - 1];
59+
60+
}
61+
}
62+
63+
if (figure.IsClosed)
64+
cr.ClosePath();
65+
}
66+
67+
base.Draw(area, cr);
68+
}
69+
70+
}
71+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using Cairo;
3+
using Xamarin.Forms.Platform.GTK.Extensions;
4+
5+
namespace Xamarin.Forms.Platform.GTK.Controls
6+
{
7+
public class RectangleView : ShapeView
8+
{
9+
protected override void Draw(Gdk.Rectangle area, Context cr)
10+
{
11+
cr.Rectangle(0, 0, _width, _height);
12+
13+
base.Draw(area, cr);
14+
}
15+
}
16+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using Cairo;
3+
using Xamarin.Forms.Platform.GTK.Extensions;
4+
5+
namespace Xamarin.Forms.Platform.GTK.Controls
6+
{
7+
public class ShapeView : GtkFormsContainer
8+
{
9+
protected Brush _fill, _stroke;
10+
protected int _height, _width;
11+
double? _strokeThickness;
12+
float? _strokeDashOffset, _strokeMiterLimit;
13+
double[] _dashArray;
14+
LineCap? _strokeCap;
15+
LineJoin? _strokeJoin;
16+
17+
public void UpdateFill(Brush brush)
18+
{
19+
_fill = brush;
20+
QueueDraw();
21+
}
22+
23+
public void UpdateStroke(Brush brush)
24+
{
25+
_stroke = brush;
26+
}
27+
28+
public void UpdateSize(int height, int width)
29+
{
30+
_height = height;
31+
_width = width;
32+
QueueDraw();
33+
}
34+
35+
public void UpdateStrokeThickness(double strokeThickness)
36+
{
37+
_strokeThickness = strokeThickness;
38+
}
39+
40+
public void UpdateStrokeDashArray(double[] dashArray)
41+
{
42+
_dashArray = dashArray;
43+
}
44+
45+
public void UpdateStrokeDashOffset(float strokeDashOffset)
46+
{
47+
_strokeDashOffset = strokeDashOffset;
48+
}
49+
50+
public void UpdateStrokeLineCap(LineCap strokeCap)
51+
{
52+
_strokeCap = strokeCap;
53+
}
54+
55+
public void UpdateStrokeLineJoin(LineJoin strokeJoin)
56+
{
57+
_strokeJoin = strokeJoin;
58+
}
59+
60+
public void UpdateStrokeMiterLimit(float strokeMiterLimit)
61+
{
62+
_strokeMiterLimit = strokeMiterLimit;
63+
QueueDraw();
64+
}
65+
66+
protected override void Draw(Gdk.Rectangle area, Context cr)
67+
{
68+
if (_fill is SolidColorBrush fillBrush)
69+
{
70+
cr.SetSourceRGBA(fillBrush.Color.R, fillBrush.Color.G, fillBrush.Color.B, fillBrush.Color.A);
71+
cr.FillPreserve();
72+
}
73+
else if (_fill != null)
74+
throw new NotImplementedException("Brushes other than SolidColorBrush are not implemented yet");
75+
}
76+
}
77+
}

Xamarin.Forms.Platform.GTK/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@
4848
[assembly: ExportCell(typeof(Xamarin.Forms.ImageCell), typeof(ImageCellRenderer))]
4949
[assembly: ExportCell(typeof(Xamarin.Forms.SwitchCell), typeof(SwitchCellRenderer))]
5050
[assembly: ExportCell(typeof(Xamarin.Forms.ViewCell), typeof(ViewCellRenderer))]
51+
52+
[assembly: ExportRenderer(typeof(Xamarin.Forms.Shapes.Rectangle), typeof(RectangleRenderer))]
53+
[assembly: ExportRenderer(typeof(Xamarin.Forms.Shapes.Ellipse), typeof(EllipseRenderer))]
54+
[assembly: ExportRenderer(typeof(Xamarin.Forms.Shapes.Path), typeof(PathRenderer))]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.ComponentModel;
3+
using Xamarin.Forms.Platform.GTK.Controls;
4+
using Xamarin.Forms.Platform.GTK.Extensions;
5+
using Xamarin.Forms.PlatformConfiguration.GTKSpecific;
6+
using Xamarin.Forms.Shapes;
7+
8+
namespace Xamarin.Forms.Platform.GTK.Renderers
9+
{
10+
public class EllipseRenderer : ShapeRenderer<Ellipse, EllipseView>
11+
{
12+
}
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.ComponentModel;
3+
using Xamarin.Forms.Platform.GTK.Controls;
4+
using Xamarin.Forms.Platform.GTK.Extensions;
5+
using Xamarin.Forms.PlatformConfiguration.GTKSpecific;
6+
using Xamarin.Forms.Shapes;
7+
8+
namespace Xamarin.Forms.Platform.GTK.Renderers
9+
{
10+
public class PathRenderer : ShapeRenderer<Path, PathView>
11+
{
12+
protected override void OnElementChanged(ElementChangedEventArgs<Path> e)
13+
{
14+
base.OnElementChanged(e);
15+
16+
if (e.NewElement != null)
17+
{
18+
SetData(Element.Data);
19+
}
20+
}
21+
22+
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
23+
{
24+
base.OnElementPropertyChanged(sender, e);
25+
26+
if (e.PropertyName == Shapes.Path.DataProperty.PropertyName)
27+
SetData(Element.Data);
28+
}
29+
30+
void SetData(Geometry data)
31+
{
32+
Control.UpdateGeometry(data as PathGeometry);
33+
}
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.ComponentModel;
3+
using Xamarin.Forms.Platform.GTK.Controls;
4+
using Xamarin.Forms.Platform.GTK.Extensions;
5+
using Xamarin.Forms.PlatformConfiguration.GTKSpecific;
6+
using FormsRectangle = Xamarin.Forms.Shapes.Rectangle;
7+
8+
namespace Xamarin.Forms.Platform.GTK.Renderers
9+
{
10+
public class RectangleRenderer : ShapeRenderer<FormsRectangle, RectangleView>
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)