-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RoundRectangleGeometry.cs
131 lines (107 loc) · 5.21 KB
/
RoundRectangleGeometry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#nullable disable
using Microsoft.Maui.Devices;
using Microsoft.Maui.Graphics;
using Rect = Microsoft.Maui.Graphics.Rect;
namespace Microsoft.Maui.Controls.Shapes
{
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/RoundRectangleGeometry.xml" path="Type[@FullName='Microsoft.Maui.Controls.Shapes.RoundRectangleGeometry']/Docs/*" />
public class RoundRectangleGeometry : GeometryGroup
{
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/RoundRectangleGeometry.xml" path="//Member[@MemberName='.ctor'][1]/Docs/*" />
public RoundRectangleGeometry()
{
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/RoundRectangleGeometry.xml" path="//Member[@MemberName='.ctor'][2]/Docs/*" />
public RoundRectangleGeometry(CornerRadius cornerRadius, Rect rect)
{
CornerRadius = cornerRadius;
Rect = rect;
}
/// <summary>Bindable property for <see cref="Rect"/>.</summary>
public static readonly BindableProperty RectProperty =
BindableProperty.Create(nameof(Rect), typeof(Rect), typeof(RoundRectangleGeometry), new Rect(),
propertyChanged: OnRectChanged);
static void OnRectChanged(BindableObject bindable, object oldValue, object newValue)
{
(bindable as RoundRectangleGeometry)?.UpdateGeometry();
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/RoundRectangleGeometry.xml" path="//Member[@MemberName='Rect']/Docs/*" />
public Rect Rect
{
set { SetValue(RectProperty, value); }
get { return (Rect)GetValue(RectProperty); }
}
/// <summary>Bindable property for <see cref="CornerRadius"/>.</summary>
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create(nameof(CornerRadius), typeof(CornerRadius), typeof(RoundRectangleGeometry), new CornerRadius(),
propertyChanged: OnCornerRadiusChanged);
static void OnCornerRadiusChanged(BindableObject bindable, object oldValue, object newValue)
{
(bindable as RoundRectangleGeometry)?.UpdateGeometry();
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/RoundRectangleGeometry.xml" path="//Member[@MemberName='CornerRadius']/Docs/*" />
public CornerRadius CornerRadius
{
set { SetValue(CornerRadiusProperty, value); }
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
}
void UpdateGeometry()
{
FillRule = FillRule.Nonzero;
Children.Clear();
Children.Add(GetRoundRectangleGeometry());
}
Geometry GetRoundRectangleGeometry()
{
GeometryGroup roundedRectGeometry = new GeometryGroup
{
FillRule = FillRule.Nonzero
};
if (CornerRadius.TopLeft > 0)
roundedRectGeometry.Children.Add(
new EllipseGeometry(new Point(Rect.Location.X + CornerRadius.TopLeft, Rect.Location.Y + CornerRadius.TopLeft), CornerRadius.TopLeft, CornerRadius.TopLeft));
if (CornerRadius.TopRight > 0)
roundedRectGeometry.Children.Add(
new EllipseGeometry(new Point(Rect.Location.X + Rect.Width - CornerRadius.TopRight, Rect.Location.Y + CornerRadius.TopRight), CornerRadius.TopRight, CornerRadius.TopRight));
if (CornerRadius.BottomRight > 0)
roundedRectGeometry.Children.Add(
new EllipseGeometry(new Point(Rect.Location.X + Rect.Width - CornerRadius.BottomRight, Rect.Location.Y + Rect.Height - CornerRadius.BottomRight), CornerRadius.BottomRight, CornerRadius.BottomRight));
if (CornerRadius.BottomLeft > 0)
roundedRectGeometry.Children.Add(
new EllipseGeometry(new Point(Rect.Location.X + CornerRadius.BottomLeft, Rect.Location.Y + Rect.Height - CornerRadius.BottomLeft), CornerRadius.BottomLeft, CornerRadius.BottomLeft));
PathFigure pathFigure = new PathFigure
{
IsClosed = true,
StartPoint = new Point(Rect.Location.X + CornerRadius.TopLeft, Rect.Location.Y),
Segments = new PathSegmentCollection
{
new LineSegment { Point = new Point(Rect.Location.X + Rect.Width - CornerRadius.TopRight, Rect.Location.Y) },
new LineSegment { Point = new Point(Rect.Location.X + Rect.Width, Rect.Location.Y + CornerRadius.TopRight) },
new LineSegment { Point = new Point(Rect.Location.X + Rect.Width, Rect.Location.Y + Rect.Height - CornerRadius.BottomRight) },
new LineSegment { Point = new Point(Rect.Location.X + Rect.Width - CornerRadius.BottomRight, Rect.Location.Y + Rect.Height) },
new LineSegment { Point = new Point(Rect.Location.X + CornerRadius.BottomLeft, Rect.Location.Y + Rect.Height) },
new LineSegment { Point = new Point(Rect.Location.X, Rect.Location.Y + Rect.Height - CornerRadius.BottomLeft) },
new LineSegment { Point = new Point(Rect.Location.X, Rect.Location.Y + CornerRadius.TopLeft) }
}
};
PathFigureCollection pathFigureCollection = new PathFigureCollection
{
pathFigure
};
roundedRectGeometry.Children.Add(new PathGeometry(pathFigureCollection, FillRule.Nonzero));
return roundedRectGeometry;
}
public override void AppendPath(Graphics.PathF path)
{
float x = (float)Rect.X;
float y = (float)Rect.Y;
float w = (float)Rect.Width;
float h = (float)Rect.Height;
float tl = (float)CornerRadius.TopLeft;
float tr = (float)CornerRadius.TopRight;
float bl = (float)CornerRadius.BottomLeft;
float br = (float)CornerRadius.BottomRight;
path.AppendRoundedRectangle(x, y, w, h, tl, tr, bl, br);
}
}
}