-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathSKCanvasView.cs
138 lines (113 loc) · 2.8 KB
/
SKCanvasView.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
132
133
134
135
136
137
138
#if !__WATCHOS__
using System;
using System.ComponentModel;
using CoreGraphics;
using Foundation;
using UIKit;
#if __TVOS__
namespace SkiaSharp.Views.tvOS
#elif __IOS__
namespace SkiaSharp.Views.iOS
#endif
{
[Register(nameof(SKCanvasView))]
[DesignTimeVisible(true)]
public class SKCanvasView : UIView, IComponent
{
// for IComponent
#pragma warning disable 67
private event EventHandler DisposedInternal;
#pragma warning restore 67
ISite IComponent.Site { get; set; }
event EventHandler IComponent.Disposed
{
add { DisposedInternal += value; }
remove { DisposedInternal -= value; }
}
private bool designMode;
private SKCGSurfaceFactory drawable;
private bool ignorePixelScaling;
// created in code
public SKCanvasView()
{
Initialize();
}
// created in code
public SKCanvasView(CGRect frame)
: base(frame)
{
Initialize();
}
// created via designer
public SKCanvasView(IntPtr p)
: base(p)
{
}
// created via designer
public override void AwakeFromNib()
{
Initialize();
}
private void Initialize()
{
designMode = ((IComponent)this).Site?.DesignMode == true || !Extensions.IsValidEnvironment;
if (designMode)
return;
drawable = new SKCGSurfaceFactory();
}
public SKSize CanvasSize => drawable?.Info.Size ?? SKSize.Empty;
public bool IgnorePixelScaling
{
get { return ignorePixelScaling; }
set
{
ignorePixelScaling = value;
SetNeedsDisplay();
}
}
public override void Draw(CGRect rect)
{
base.Draw(rect);
if (designMode || drawable == null)
return;
// create the skia context
using (var surface = drawable.CreateSurface(Bounds, IgnorePixelScaling ? 1 : ContentScaleFactor, out var info))
{
if (info.Width == 0 || info.Height == 0)
return;
using (var ctx = UIGraphics.GetCurrentContext())
{
// draw on the image using SKiaSharp
OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info));
#pragma warning disable CS0618 // Type or member is obsolete
DrawInSurface(surface, info);
#pragma warning restore CS0618 // Type or member is obsolete
// draw the surface to the context
drawable.DrawSurface(ctx, Bounds, info, surface);
}
}
}
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
{
PaintSurface?.Invoke(this, e);
}
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete("Use OnPaintSurface(SKPaintSurfaceEventArgs) instead.")]
public virtual void DrawInSurface(SKSurface surface, SKImageInfo info)
{
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
Layer.SetNeedsDisplay();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
drawable?.Dispose();
drawable = null;
}
}
}
#endif