-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAdditionalExtensions.cs
359 lines (310 loc) · 12 KB
/
AdditionalExtensions.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
namespace MapboxMaui;
using Android.Content.Res;
using Android.Util;
using MapboxMapsCameraOptions = Com.Mapbox.Maps.CameraOptions;
using PlatformValue = Com.Mapbox.Bindgen.Value;
using MapboxTerrain = Com.Mapbox.Maps.Extension.Style.Terrain.Generated.Terrain;
using StyleTransitionBuilder = Com.Mapbox.Maps.Extension.Style.Types.StyleTransition.Builder;
using PlatformStyleTransition = Com.Mapbox.Maps.Extension.Style.Types.StyleTransition;
using XTilesetDescriptorOptions = Offline.TilesetDescriptorOptions;
using Com.Mapbox.Maps;
using Microsoft.Maui.Platform;
using MapboxMaui.Styles;
using System.Collections;
using MapboxMaui.Expressions;
using AndroidX.Fragment.App;
static class AdditionalExtensions
{
internal static DisplayMetrics Metrics;
internal static double PixelToPoint(this double pixel)
{
Metrics ??= Resources.System?.DisplayMetrics;
if (Metrics == null) return 0;
return pixel / Metrics.Density;
}
internal static Java.Lang.Boolean ToPlatform(this bool xvalue)
{
return new Java.Lang.Boolean(xvalue);
}
internal static Java.Lang.Double ToPlatform(this double xvalue)
{
return new Java.Lang.Double(xvalue);
}
internal static double[] GetValue(this IList<Java.Lang.Double> xvalue, bool defaultToEmpty = false)
{
if (xvalue == null && defaultToEmpty)
{
return Array.Empty<double>();
}
return xvalue?
.Select(x => x.DoubleValue())
.ToArray();
}
internal static IList<Java.Lang.Double> ToPlatform(this IEnumerable<double> xvalue)
{
return xvalue?
.Select(x => new Java.Lang.Double(x))
.ToList();
}
internal static IList<Java.Lang.String> ToPlatform(this IEnumerable<string> xvalue)
{
return xvalue?
.Select(x => new Java.Lang.String(x))
.ToList();
}
internal static Com.Mapbox.Common.NetworkRestriction ToNative(this Offline.NetworkRestriction xvalue)
{
return xvalue switch
{
Offline.NetworkRestriction.None => Com.Mapbox.Common.NetworkRestriction.None,
Offline.NetworkRestriction.DisallowExpensive => Com.Mapbox.Common.NetworkRestriction.DisallowExpensive,
Offline.NetworkRestriction.DisallowAll => Com.Mapbox.Common.NetworkRestriction.DisallowAll,
_ => null,
};
}
internal static TilesetDescriptorOptions ToNative(this XTilesetDescriptorOptions xoptions)
{
return new TilesetDescriptorOptions.Builder()
.MinZoom(xoptions.MinZoom)
.MaxZoom(xoptions.MaxZoom)
.StyleURI(xoptions.StyleUri)
.PixelRatio(xoptions.PixelRatio)
.StylePackOptions(xoptions.StylePackLoadOptions?.ToNative())
.Build();
}
internal static StylePackLoadOptions ToNative(this Offline.StylePackLoadOptions xoptions)
{
return new StylePackLoadOptions.Builder()
.GlyphsRasterizationMode(xoptions.Mode.HasValue
? GetGlyphsRasterizationMode(xoptions.Mode.Value)
: null
)
.Metadata(xoptions.Metadata.Wrap())
.AcceptExpired(xoptions.AcceptsExpired)
.Build();
}
private static GlyphsRasterizationMode GetGlyphsRasterizationMode(Offline.GlyphsRasterizationMode mode)
{
return mode switch
{
Offline.GlyphsRasterizationMode.NoGlyphsRasterizedLocally => GlyphsRasterizationMode.NoGlyphsRasterizedLocally,
Offline.GlyphsRasterizationMode.IdeographsRasterizedLocally => GlyphsRasterizationMode.IdeographsRasterizedLocally,
Offline.GlyphsRasterizationMode.AllGlyphsRasterizedLocally => GlyphsRasterizationMode.AllGlyphsRasterizedLocally,
_ => null,
};
}
internal static PlatformValue ToPlatformValue(this BaseKVContainer xvalue, bool rgba = false)
{
var properties = new Dictionary<string, PlatformValue>();
foreach (var property in xvalue.Properties)
{
var propertyValue = property.Value.Wrap(rgba);
properties[property.Key] = propertyValue;
}
var result = new PlatformValue(properties);
return result;
}
internal static Com.Mapbox.Maps.LayerPosition ToPlatformValue(this Styles.LayerPosition xvalue)
{
return xvalue.Enum switch
{
LayerPositionEnum.Above => new Com.Mapbox.Maps.LayerPosition(
xvalue.Parameter as string, null, null
),
LayerPositionEnum.At => new Com.Mapbox.Maps.LayerPosition(
null, null, new Java.Lang.Integer((int)xvalue.Parameter)
),
LayerPositionEnum.Below => new Com.Mapbox.Maps.LayerPosition(
null, xvalue.Parameter as string, null
),
_ => null,
};
}
internal static PlatformValue Wrap(this object xvalue, bool rgba = false)
{
var platformValue = xvalue switch
{
bool value => new PlatformValue(value),
byte value => new PlatformValue(value),
short value => new PlatformValue(value),
int value => new PlatformValue(value),
long value => new PlatformValue(value),
ushort value => new PlatformValue(value),
uint value => new PlatformValue(value),
ulong value => new PlatformValue(value),
float value => new PlatformValue(value),
double value => new PlatformValue(value),
string value => new PlatformValue(value),
Color value => rgba
? new PlatformValue(value.ToRgbaString())
: new PlatformValue(value.ToInt()),
INamedString value => new PlatformValue(value.Value),
IPropertyValue value => value.Value is DslExpression expression1
? new PlatformValue(expression1
.ToObjects()
.Select(x => x.Wrap(rgba))
.ToList()
)
: value.Value.Wrap(rgba),
_ => null
};
if (platformValue != null) return platformValue;
if (xvalue is DslExpression expression)
{
return new PlatformValue(expression
.ToObjects()
.Select(x => x.Wrap(rgba))
.ToList()
);
}
if (xvalue is PromoteId promoteId)
{
return string.IsNullOrWhiteSpace(promoteId.StringValue)
? promoteId.KeyValues.Wrap()
: promoteId.StringValue.Wrap();
}
if (xvalue is ResolvedImage resolvedImage)
{
return resolvedImage.Id.Wrap();
}
if (xvalue is IDictionary<string, object> dict)
{
var list = new Dictionary<string, PlatformValue>();
foreach (var item in dict)
{
list[item.Key] = item.Value.Wrap(rgba);
}
return new PlatformValue(list);
}
if (xvalue is IReadOnlyDictionary<string, object> rodict)
{
var list = new Dictionary<string, PlatformValue>();
foreach (var item in rodict)
{
list[item.Key] = item.Value.Wrap(rgba);
}
return new PlatformValue(list);
}
if (xvalue is IEnumerable objects)
{
var list = new List<PlatformValue>();
foreach (var item in objects)
{
list.Add(item.Wrap(rgba));
}
return new PlatformValue(list);
}
throw new NotSupportedException($"Invalue property type: {xvalue?.GetType()} | {xvalue}");
}
internal static MapboxTerrain ToPlatformValue(this Terrain terrain)
{
var result = new MapboxTerrain(terrain.SourceId);
if (terrain.Exaggeration != null)
{
if (terrain.Exaggeration.Expression != null)
{
result.Exaggeration(terrain.Exaggeration.Expression.ToPlatformValue());
}
else
{
result.Exaggeration(terrain.Exaggeration.Constant);
}
}
if (terrain.ExaggerationTransition != null)
{
result.ExaggerationTransition(terrain.ExaggerationTransition.ToPlatformValue());
}
return result;
}
internal static PlatformStyleTransition ToPlatformValue(this StyleTransition xvalue)
{
var styleTransitionBuilder = new StyleTransitionBuilder();
styleTransitionBuilder.InvokeDelay(xvalue.Delay);
styleTransitionBuilder.InvokeDuration(xvalue.Duration);
return styleTransitionBuilder.Build();
}
internal static PlatformValue GetVolatileProperties(this MapboxSource source)
{
var properties = new Dictionary<string, PlatformValue>();
foreach (var property in source.VolatileProperties)
{
var xvalue = property.Value.Wrap();
properties[property.Key] = xvalue;
}
var result = new PlatformValue(properties);
return result;
}
internal static MapView GetMapView(this MapboxViewHandler handler)
=> handler.PlatformView.GetMapView();
internal static MapView GetMapView(this FragmentContainerView container)
{
var mainActivity = (MauiAppCompatActivity)container.Context.GetActivity();
var tag = $"mapbox-maui-{container.Id}";
var fragnent = mainActivity.SupportFragmentManager.FindFragmentByTag(tag);
return (fragnent as MapboxFragment)?.MapView;
}
public static MapDebugOptions ToNative(this DebugOption option)
{
return option switch
{
DebugOption.TileBorders => MapDebugOptions.TileBorders,
DebugOption.ParseStatus => MapDebugOptions.ParseStatus,
DebugOption.Timestamps => MapDebugOptions.Timestamps,
DebugOption.Collision => MapDebugOptions.Collision,
DebugOption.StencilClip => MapDebugOptions.StencilClip,
DebugOption.DepthBuffer => MapDebugOptions.DepthBuffer,
_ => MapDebugOptions.ModelBounds,
};
}
public static IList<MapDebugOptions> ToNative(this IEnumerable<DebugOption> options)
{
return options
.Select(x => x.ToNative())
.ToList();
}
public static string ToNative(this MapboxStyle mapboxStyle)
{
return mapboxStyle.Value;
}
public static MapboxMapsCameraOptions ToNative(this CameraOptions cameraOptions)
{
var cameraOptionsBuilder = new MapboxMapsCameraOptions.Builder();
if (cameraOptions.Center is not null)
{
cameraOptionsBuilder.Center(
Com.Mapbox.Geojson.Point.FromLngLat(
cameraOptions.Center.Longitude,
cameraOptions.Center.Latitude
));
}
if (cameraOptions.Zoom.HasValue)
{
cameraOptionsBuilder.Zoom(new Java.Lang.Double(cameraOptions.Zoom.Value));
}
if (cameraOptions.Bearing.HasValue)
{
cameraOptionsBuilder.Bearing(new Java.Lang.Double(cameraOptions.Bearing.Value));
}
if (cameraOptions.Pitch.HasValue)
{
cameraOptionsBuilder.Pitch(new Java.Lang.Double(cameraOptions.Pitch.Value));
}
if (cameraOptions.Padding.HasValue)
{
cameraOptionsBuilder.Padding(new EdgeInsets(
cameraOptions.Padding.Value.Top,
cameraOptions.Padding.Value.Left,
cameraOptions.Padding.Value.Bottom,
cameraOptions.Padding.Value.Right
));
}
if (cameraOptions.Anchor is not null)
{
cameraOptionsBuilder.Anchor(new ScreenCoordinate(
cameraOptions.Anchor.Value.X,
cameraOptions.Anchor.Value.Y
));
}
return cameraOptionsBuilder.Build();
}
}