forked from xamarin/mac-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircleView.cs
170 lines (141 loc) · 4.91 KB
/
CircleView.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using AppKit;
using CoreGraphics;
using System.Runtime.InteropServices;
namespace CircleView
{
public partial class CircleView : AppKit.NSView
{
NSTimer Timer;
double LastAnimationTime;
NSTextStorage TextStorage = new NSTextStorage ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis aliquet ipsum non sem volutpat, ut posuere nunc pharetra.");
NSLayoutManager LayoutManager = new NSLayoutManager ();
NSTextContainer TextContainer = new NSTextContainer ();
CGPoint center;
public CGPoint Center
{
get => center;
set { center = value; NeedsDisplay = true; }
}
double radius = 115;
public double Radius
{
get => radius;
set { radius = value; NeedsDisplay = true; }
}
double startingAngle = Math.PI / 2;
public double StartingAngle
{
get => startingAngle;
set { startingAngle = value; NeedsDisplay = true; }
}
double angularVelocity = Math.PI / 2;
public double AngularVelocity
{
get => angularVelocity;
set { angularVelocity = value; NeedsDisplay = true; }
}
public string Text
{
get => TextStorage.Value;
set {
TextStorage.Replace (new NSRange (0, TextStorage.Length), value);
NeedsDisplay = true;
}
}
NSColor textColor;
public NSColor TextColor
{
get => textColor;
set {
textColor = value;
TextStorage.AddAttribute (NSStringAttributeKey.ForegroundColor, value, new NSRange (0, TextStorage.Length));
NeedsDisplay = true;
}
}
public CircleView (IntPtr handle) : base (handle)
{
Initialize ();
}
[Export ("initWithCoder:")]
public CircleView (NSCoder coder) : base (coder)
{
Initialize ();
}
void Initialize ()
{
Center = new CGPoint (180, 135);
LayoutManager.AddTextContainer (TextContainer);
TextStorage.AddLayoutManager (LayoutManager);
TextColor = NSColor.Black;
}
public override void DrawRect (CGRect dirtyRect)
{
base.DrawRect (dirtyRect);
NSColor.White.Set ();
NSGraphics.RectFill (Bounds);
var glyphRange = LayoutManager.GetGlyphRange (TextContainer);
var usedRect = LayoutManager.GetUsedRectForTextContainer (TextContainer);
for (int glyphIndex = (int)glyphRange.Location; glyphIndex < glyphRange.Location + glyphRange.Length; glyphIndex++) {
var context = NSGraphicsContext.CurrentContext;
var lineFramgmentRect = LayoutManager.LineFragmentRectForGlyphAtIndex ((nuint)glyphIndex, IntPtr.Zero);
var layoutLocation = LayoutManager.LocationForGlyphAtIndex (glyphIndex);
NSAffineTransform transform = new NSAffineTransform ();
layoutLocation.X += lineFramgmentRect.X;
layoutLocation.Y += lineFramgmentRect.Y;
var distance = Radius + usedRect.Height - layoutLocation.Y;
nfloat angle = (nfloat)(StartingAngle + layoutLocation.X / distance);
nfloat viewLocationX = (nfloat)(Center.X + distance * Math.Sin (angle));
nfloat viewLocationY = (nfloat)(Center.Y + distance * Math.Cos (angle));
transform.Translate (viewLocationX, viewLocationY);
transform.RotateByRadians (-angle);
context?.SaveGraphicsState ();
transform.Concat ();
LayoutManager.DrawGlyphsForGlyphRange (new NSRange (glyphIndex, 1), new CGPoint (-layoutLocation.X, -layoutLocation.Y));
context?.RestoreGraphicsState ();
}
}
void StartAnimation ()
{
Timer = NSTimer.CreateScheduledTimer (1.0 / 30, true, t => Animate (t));
NSRunLoop.Current.AddTimer (Timer, NSRunLoopMode.ModalPanel);
NSRunLoop.Current.AddTimer (Timer, NSRunLoopMode.EventTracking);
LastAnimationTime = NSDate.Now.SecondsSinceReferenceDate;
}
void StopAnimation ()
{
Timer?.Invalidate ();
Timer = null;
}
void Animate (NSTimer timer)
{
var now = NSDate.Now.SecondsSinceReferenceDate;
StartingAngle = StartingAngle + AngularVelocity * (now - LastAnimationTime);
LastAnimationTime = now;
}
public void ToggleAnimation ()
{
if (Timer == null)
StartAnimation ();
else
StopAnimation ();
}
}
// "lineFragmentRectForGlyphAtIndex:effectiveRange:" binding is missing so work around with manual bindings
// https://github.com/xamarin/xamarin-macios/issues/4740
public static class LayoutManagerExtensions
{
static readonly IntPtr selLineFragmentRectForGlyphAtIndex_EffectiveRange_Handle = ObjCRuntime.Selector.GetHandle ("lineFragmentRectForGlyphAtIndex:effectiveRange:");
[DllImport ("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend_stret")]
public extern static void CGRect_objc_msgSend_stret_nuint_IntPtr (out CGRect retval, IntPtr receiver, IntPtr selector, nuint arg1, IntPtr arg2);
public static CGRect LineFragmentRectForGlyphAtIndex (this NSLayoutManager manager, nuint glyphIndex, IntPtr effectiveGlyphRange)
{
NSApplication.EnsureUIThread ();
CGRect_objc_msgSend_stret_nuint_IntPtr (out CGRect ret, manager.Handle, selLineFragmentRectForGlyphAtIndex_EffectiveRange_Handle, glyphIndex, effectiveGlyphRange);
return ret;
}
}
}