forked from AlexGustafsson/fuse-motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motion.uno
178 lines (155 loc) · 5.11 KB
/
Motion.uno
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
171
172
173
174
175
176
177
178
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.UX;
using Uno.Compiler.ExportTargetInterop;
using Uno;
[Require("Xcode.Framework","CoreMotion.framework")]
[ForeignInclude(Language.ObjC, "Motion.hh")]
[UXGlobalModule]
public class MotionModule : NativeEventEmitterModule
{
static readonly MotionModule _instance;
extern(iOS) ObjC.Object _motion;
//base(false, ...) -> don't cache events that are fired before initialization, ensure up to date, real-time data
public MotionModule() : base (false, "accelerometerChanged", "gyroscopeChanged", "motionChanged", "magnetometerChanged", "activityChanged")
{
if(_instance != null)
return;
_instance = this;
Uno.UX.Resource.SetGlobalKey(_instance, "Motion");
AddMember(new NativeFunction("Subscribe", (NativeCallback)Subscribe));
AddMember(new NativeFunction("Unsubscribe", (NativeCallback)Unsubscribe));
if defined(iOS)
_motion = AllocMotion();
}
[Foreign(Language.ObjC)]
extern(iOS) ObjC.Object AllocMotion()
@{
return [Motion alloc];
@}
[Foreign(Language.ObjC)]
public extern(iOS) bool SubscribeAccelerometer(ObjC.Object motion, double interval, Action<string> callback)
@{
[(Motion *)motion getAccelerometerValues: interval withCallback:callback];
return true;
@}
[Foreign(Language.ObjC)]
public extern(iOS) bool UnsubscribeAccelerometer(ObjC.Object motion)
@{
[(Motion *)motion stopAccelerometer];
return true;
@}
void AccelerometerCallback(string accelerometer)
{
Emit("accelerometerChanged", accelerometer);
}
[Foreign(Language.ObjC)]
public extern(iOS) bool SubscribeGyroscope(ObjC.Object motion, double interval, Action<string> callback)
@{
[(Motion *)motion getGyroValues: interval withCallback:callback];
return true;
@}
[Foreign(Language.ObjC)]
public extern(iOS) bool UnsubscribeGyroscope(ObjC.Object motion)
@{
[(Motion *)motion stopGyroscope];
return true;
@}
void GyroscopeCallback(string gyroscope)
{
Emit("gyroscopeChanged", gyroscope);
}
[Foreign(Language.ObjC)]
public extern(iOS) bool SubscribeMotion(ObjC.Object motion, double interval, Action<string> callback)
@{
[(Motion *)motion getMotionValues: interval withCallback:callback];
return true;
@}
[Foreign(Language.ObjC)]
public extern(iOS) bool UnsubscribeMotion(ObjC.Object motion)
@{
[(Motion *)motion stopMotion];
return true;
@}
void MotionCallback(string motion)
{
Emit("motionChanged", motion);
}
[Foreign(Language.ObjC)]
public extern(iOS) bool SubscribeMagnetometer(ObjC.Object motion, double interval, Action<string> callback)
@{
[(Motion *)motion getMagnetometerValues: interval withCallback:callback];
return true;
@}
[Foreign(Language.ObjC)]
public extern(iOS) bool UnsubscribeMagnetometer(ObjC.Object motion)
@{
[(Motion *)motion stopMagnetometer];
return true;
@}
void MagnetometerCallback(string magnetometer)
{
Emit("magnetometerChanged", magnetometer);
}
[Foreign(Language.ObjC)]
public extern(iOS) bool SubscribeActivity(ObjC.Object motion, double interval, Action<string> callback)
@{
[(Motion *)motion getActivity: interval withCallback:callback];
return true;
@}
[Foreign(Language.ObjC)]
public extern(iOS) bool UnsubscribeActivity(ObjC.Object motion)
@{
[(Motion *)motion stopActivity];
return true;
@}
void ActivityCallback(string activity)
{
Emit("activityChanged", activity);
}
object Subscribe(Context c, object[] args)
{
if defined(iOS){
if(args.Length != 2)
return false;
bool subscribed = false;
if(args[0].ToString().Contains("accelerometer"))
subscribed = SubscribeAccelerometer(_motion, Marshal.ToDouble(args[1]), AccelerometerCallback);
if(args[0].ToString().Contains("gyroscope"))
subscribed = SubscribeGyroscope(_motion, Marshal.ToDouble(args[1]), GyroscopeCallback);
if(args[0].ToString().Contains("motion"))
subscribed = SubscribeMotion(_motion, Marshal.ToDouble(args[1]), MotionCallback);
if(args[0].ToString().Contains("magnetometer"))
subscribed = SubscribeMagnetometer(_motion, Marshal.ToDouble(args[1]), MagnetometerCallback);
if(args[0].ToString().Contains("activity"))
subscribed = SubscribeActivity(_motion, Marshal.ToDouble(args[1]), ActivityCallback);
return subscribed;
} else {
debug_log "Motion is only implemented for iOS";
return false;
}
}
object Unsubscribe(Context c, object[] args)
{
if defined(iOS){
if(args.Length == 0)
return false;
bool unsubscribed = false;
if(args[0].ToString().Contains("accelerometer"))
unsubscribed = UnsubscribeAccelerometer(_motion);
if(args[0].ToString().Contains("gyroscope"))
unsubscribed = UnsubscribeGyroscope(_motion);
if(args[0].ToString().Contains("motion"))
unsubscribed = UnsubscribeMotion(_motion);
if(args[0].ToString().Contains("magnetometer"))
unsubscribed = UnsubscribeMagnetometer(_motion);
if(args[0].ToString().Contains("activity"))
unsubscribed = UnsubscribeActivity(_motion);
return unsubscribed;
} else {
debug_log "Motion is only implemented for iOS";
return false;
}
}
}