-
Notifications
You must be signed in to change notification settings - Fork 8
/
Pen.cs
119 lines (95 loc) · 2.8 KB
/
Pen.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
using System;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Overlay.Extensions;
using SkiaSharp;
namespace MonoGame.Extended.Overlay;
public sealed class Pen : DisposableBase, IPaintProvider
{
public Pen(Color color)
: this(color, 1)
{
}
public Pen(Color color, float strokeWidth)
: this(color, strokeWidth, LineCap.Round, LineJoin.Round, 0)
{
}
public Pen(Color color, float strokeWidth, LineCap lineCap, LineJoin lineJoin, float miter)
{
var paint = new SKPaint();
paint.Color = color.ToSKColor();
paint.IsAntialias = true;
paint.IsStroke = true;
paint.StrokeWidth = strokeWidth;
paint.StrokeCap = Map(lineCap);
paint.StrokeJoin = Map(lineJoin);
paint.StrokeMiter = miter;
Paint = paint;
StrokeWidth = strokeWidth;
LineCap = lineCap;
LineJoin = lineJoin;
MiterLimit = miter;
}
public Pen(Brush brush)
: this(brush, 1)
{
}
public Pen(Brush brush, float strokeWidth)
: this(brush, strokeWidth, LineCap.Round, LineJoin.Round, 0)
{
}
public Pen(Brush brush, float strokeWidth, LineCap lineCap, LineJoin lineJoin, float miter)
{
var paint = brush.Paint.Clone();
paint.IsStroke = true;
paint.StrokeWidth = strokeWidth;
paint.StrokeCap = Map(lineCap);
paint.StrokeJoin = Map(lineJoin);
paint.StrokeMiter = miter;
Paint = paint;
StrokeWidth = strokeWidth;
LineCap = lineCap;
LineJoin = lineJoin;
MiterLimit = miter;
}
public float StrokeWidth { get; }
public LineCap LineCap { get; }
public LineJoin LineJoin { get; }
public float MiterLimit { get; }
internal SKPaint Paint { get; }
protected override void Dispose(bool disposing)
{
if (disposing)
{
Paint.Dispose();
}
}
SKPaint IPaintProvider.Paint => Paint;
private static SKStrokeCap Map(LineCap cap)
{
switch (cap)
{
case LineCap.Round:
return SKStrokeCap.Round;
case LineCap.Square:
return SKStrokeCap.Square;
case LineCap.Butt:
return SKStrokeCap.Butt;
default:
throw new ArgumentOutOfRangeException(nameof(cap), cap, null);
}
}
private static SKStrokeJoin Map(LineJoin join)
{
switch (join)
{
case LineJoin.Round:
return SKStrokeJoin.Round;
case LineJoin.Bevel:
return SKStrokeJoin.Bevel;
case LineJoin.Miter:
return SKStrokeJoin.Miter;
default:
throw new ArgumentOutOfRangeException(nameof(join), join, null);
}
}
}