-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoCsharpWrapperTest.cs
More file actions
604 lines (466 loc) · 18.6 KB
/
GeoCsharpWrapperTest.cs
File metadata and controls
604 lines (466 loc) · 18.6 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
using Godot;
using System;
using System.Threading.Tasks;
public class GeoCsharpWrapperTest : Control
{
private HFlowContainer _testButtonContainer = null!;
private RichTextLabel _locationDataOutput = null!;
private RichTextLabel _errorOutput = null!;
private Button _clearErrorOutput = null!;
private Button _clearLocationOutput = null!;
private TabContainer _tabContainer = null!;
// private MenuButton _tabSelectMenuButton = null!;
// private PopupMenu _tabSelectPopup = null!;
private OptionButton _tabOptionButton = null!;
public Location? CurrentLocation;
public Location? StartLocation;
public Location? EndLocation;
public Geolocation GeolocationAPI = null!;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_tabContainer = GetNode<TabContainer>("%TestTabContainer");
//_tabSelectMenuButton = GetNode<MenuButton>("%TabSelectMenuButton");
//_tabSelectPopup = _tabSelectMenuButton.GetPopup();
_tabOptionButton = GetNode<OptionButton>("%TabOptionButton");
_testButtonContainer = GetNode<HFlowContainer>("%TestButtonContainer");
_locationDataOutput = GetNode<RichTextLabel>("%LocationDataOutput");
_errorOutput = GetNode<RichTextLabel>("%ErrorOutput");
_clearErrorOutput = GetNode<Button>("%ClearErrorOutput");
_clearLocationOutput = GetNode<Button>("%ClearLocationOutput");
_clearErrorOutput.Connect("button_up", this, nameof(OnClearErrorOutput));
_clearLocationOutput.Connect("button_up", this, nameof(OnClearLocationOutput));
OnClearErrorOutput();
Log("Ready");
GeolocationAPI = GetNode<Geolocation>("/root/Geolocation");
if (GeolocationAPI is null)
{
Log("Geolocation is null... node not found!");
return;
}
if (GeolocationAPI.Supported)
{
Log("Geolocation supported");
// Geolocation Settings
GeolocationAPI.SetDebugLogSignal(true);
GeolocationAPI.SetFailureTimeout(30);
//GeolocationAPI.SetAutoCheckLocationCapability(true);
//
}
else
{
Log("Geolocation NOT supported!");
}
GeolocationAPI.Log += OnLogEvent;
GeolocationAPI.AuthorizationChanged += OnAuthorizationChangedEvent;
//GeolocationAPI.LocationUpdate += OnLocationUpdateEvent;
GeolocationAPI.Error += OnErrorEvent;
//GeolocationAPI.HeadingUpdate += OnHeadingUpdate;
InitUIAndWindowScaling();
CreateTestTabsAndButtons();
return;
// calculate distance (test)
//CreateDebugButton("Set Start", nameof(OnSetStartLocation));
//CreateDebugButton("Set End", nameof(OnSetEndLocation));
//CreateDebugButton("Calc Distance", nameof(OnShowDistance));
}
private void OnTabSelectPopupSelected(int id)
{
//Log("Selected Tab id: " + id);
_tabContainer.CurrentTab = id;
}
private HFlowContainer CreateOneTab(string name)
{
var theTab = new HFlowContainer();
theTab.Name = name;
_tabContainer.AddChild(theTab);
_tabOptionButton.AddItem(name);
return theTab;
}
private void CreateTestTabsAndButtons()
{
_tabOptionButton.Connect("item_selected", this, nameof(OnTabSelectPopupSelected));
Log("Creating Test Tabs");
// permissions and authorization
var permissionTab = CreateOneTab("Permissions");
// request_permissions
permissionTab.AddChild(ReturnButton("Request Permission Async", nameof(OnRequestPermissionAsync)));
// location
var locationTab = CreateOneTab("Location");
// request_location
locationTab.AddChild(ReturnButton("Request Location Async", nameof(OnRequestLocationAsync)));
// start_updating_location
locationTab.AddChild(ReturnButton("Start Updating Async", nameof(OnGetLocationUpdater)));
// stop_updating_location
locationTab.AddChild(ReturnButton("Stop Updating Async", nameof(OnStopLocationUpdater)));
// heading
var headingTab = CreateOneTab("Heading");
// TODO make async!!!
// start_updating_heading
headingTab.AddChild(ReturnButton("Start heading", nameof(OnStartHeading)));
// stop_updating_heading
headingTab.AddChild(ReturnButton("Stop heading", nameof(OnStopHeading)));
// options
var optionsTab = CreateOneTab("Options");
// set_distance_filter
optionsTab.AddChild(ReturnButton("DistanceFilter 0", nameof(OnSetDistanceFilter), new Godot.Collections.Array { 0 }));
optionsTab.AddChild(ReturnButton("DF 10", nameof(OnSetDistanceFilter), new Godot.Collections.Array { 10 }));
optionsTab.AddChild(ReturnButton("DF 50", nameof(OnSetDistanceFilter), new Godot.Collections.Array { 50 }));
// set_desired_accuracy
optionsTab.AddChild(ReturnButton("Accuracy Best", nameof(OnSetDesiredAccuracyBest)));
optionsTab.AddChild(ReturnButton("Ac 100m", nameof(OnSetDesiredAccuracyHundredMeters)));
// set_update_interval
optionsTab.AddChild(ReturnButton("Interval 1s", nameof(OnSetUpdateIntervalAndroid), new Godot.Collections.Array { 1 }));
optionsTab.AddChild(ReturnButton("Interval 5s", nameof(OnSetUpdateIntervalAndroid), new Godot.Collections.Array { 5 }));
// set_max_wait_time
optionsTab.AddChild(ReturnButton("MaxWait 1s", nameof(OnSetMaxWaitTimeAndroid), new Godot.Collections.Array { 1 }));
optionsTab.AddChild(ReturnButton("MaxWait 10s", nameof(OnSetMaxWaitTimeAndroid), new Godot.Collections.Array { 10 }));
// set_return_string_coordinates
// ignore... make private
// set_failure_timeout
optionsTab.AddChild(ReturnButton("Timeout 2s", nameof(OnSetFailureTimeout), new Godot.Collections.Array { 2 }));
optionsTab.AddChild(ReturnButton("Timeout 20s", nameof(OnSetFailureTimeout), new Godot.Collections.Array { 20 }));
// set_debug_log_signal
// ignore...
// status
var statusTab = CreateOneTab("Status");
// authorization_status
statusTab.AddChild(ReturnButton("Auth. Status", nameof(OnGetAuthorizationStatus)));
// allows_full_accuracy
statusTab.AddChild(ReturnButton("Full Accuracy", nameof(OnCheckFullAccuracy)));
// can_request_permissions
statusTab.AddChild(ReturnButton("Can Request Perm.", nameof(OnCanRequestPermissions)));
// is_updating_location
statusTab.AddChild(ReturnButton("Is Updating Loc.", nameof(OnCheckIsUpdatingLocation)));
// is_updating_heading
statusTab.AddChild(ReturnButton("Is Updating Heading", nameof(OnCheckIsUpdatingHeading)));
// should_show_permission_requirement_explanation
statusTab.AddChild(ReturnButton("Should Explain", nameof(OnShouldShowPermissionRequirementExplanation)));
// request_location_capabilty
statusTab.AddChild(ReturnButton("Location Capability", nameof(OnCheckLocCap)));
// Support checks
var supportsTab = CreateOneTab("Supports");
// supports_heading
supportsTab.AddChild(ReturnButton("Heading", nameof(OnSupportsHeading)));
// supports_update_interval
supportsTab.AddChild(ReturnButton("Update Interval", nameof(OnSupportsUpdateInterval)));
// supports_max_wait_time
supportsTab.AddChild(ReturnButton("Max Wait Time", nameof(OnSupportsMaxWaitTime)));
// supports_should_show_permission_requirement_explanation
supportsTab.AddChild(ReturnButton("Should Explain", nameof(OnSupportsShouldExplain)));
// supports_request_permissions
supportsTab.AddChild(ReturnButton("Request Permisson", nameof(OnSupportsRequestPermission)));
// Distance Calculator
var utilityTab = CreateOneTab("Utility");
utilityTab.AddChild(ReturnButton("Set Start", nameof(OnSetStartLocation)));
utilityTab.AddChild(ReturnButton("Set End", nameof(OnSetEndLocation)));
utilityTab.AddChild(ReturnButton("Show Distance", nameof(OnShowDistance)));
_tabOptionButton.Selected = 0;
}
private void InitUIAndWindowScaling()
{
var currentOS = OS.GetName();
Log(currentOS);
var screenDpi = OS.GetScreenDpi();
Log("System DPI: " + screenDpi);
float finalScaleFactor = 1;
if(screenDpi > 140) finalScaleFactor = 1.51f;
if(screenDpi > 250)
{
var scaleFactor = screenDpi / 150.0f;
Log("raw scale factor: " + scaleFactor);
finalScaleFactor = Mathf.FloorToInt(scaleFactor);
}
Log("ScaleFacor " + finalScaleFactor.ToString());
GetTree().SetScreenStretch(SceneTree.StretchMode.Disabled, SceneTree.StretchAspect.Ignore, new Vector2(0, 0), finalScaleFactor);
if (currentOS == "iOS" || currentOS == "Android") return; // don't scale window
var currentSize = OS.WindowSize;
OS.WindowSize = currentSize * finalScaleFactor;
var screenSize = OS.GetScreenSize();
OS.WindowPosition = (screenSize * 0.5f - OS.WindowSize * 0.5f);
var colors = this.Theme.GetColorList("Button");
GD.Print("Button colors:");
Log("Button colors:");
foreach (var color in colors)
{
GD.Print(color);
Log(color);
}
}
private void OnSupportsHeading() => Log("Supports heading " + GeolocationAPI.Supports("start_updating_heading"));
private void OnSupportsUpdateInterval() => Log("Supports Update Interval " + GeolocationAPI.Supports("set_update_interval"));
private void OnSupportsMaxWaitTime() => Log("Supports Max wait time " + GeolocationAPI.Supports("set_max_wait_time"));
private void OnSupportsShouldExplain() => Log("Supports should explain " + GeolocationAPI.Supports("should_show_permission_requirement_explanation"));
private void OnSupportsRequestPermission() => Log("Supports request poermisson " + GeolocationAPI.Supports("request_permission"));
private async void OnRequestPermissionAsync()
{
var authorization = await GeolocationAPI.RequestPermissionsAsync();
Log("OnRequestPermissionAsync " + authorization);
}
private async void OnCheckLocCap()
{
var capable = await GeolocationAPI.LocationCapabilityAsync();
Log("OnCheckLocCap " + capable);
}
private async void OnRequestLocationAsync()
{
//Log("-- try auto location --");
var loc = await GeolocationAPI.RequestLocationAsync();
//Log("-- after await location --");
if (loc != null)
{
Log("-- autolocation incoming --");
LogLocation(loc);
CurrentLocation = loc;
}
else
{
Log("Auto location null");
Log("Last Error (might not be correct): " + GeolocationAPI.LastError);
}
}
private Geolocation.LocationUpdater? _locationUpdater = null!;
private Geolocation.HeadingUpdater? _headingUpdater = null!;
private async void OnGetLocationUpdater()
{
if (_locationUpdater != null)
{
Log("## !!! stop old _locationUpdater");
_locationUpdater.Stop();
}
_locationUpdater = await GeolocationAPI.GetLocationUpdater();
if (_locationUpdater is null)
{
Log("## _locationUpdater null! don't loop");
return;
}
Log("## entering new while loop");
Location? loc;
while ((loc = await _locationUpdater.LocationUpdateAsync()) != null)
{
LogLocation(loc);
CurrentLocation = loc;
}
Log("#### YEAH after loop new"); // we should _always_ arrive here after updates stop
if(_locationUpdater.HasError)
{
Log("Exited because of error: " + _locationUpdater.LastError.ToString());
}
}
private void OnStopLocationUpdater()
{
if (_locationUpdater is null) return;
_locationUpdater.Stop();
}
private void OnCanRequestPermissions()
{
Log("Can Request permissions: " + GeolocationAPI.CanRequestPermissions);
}
private void OnIsWatching()
{
Log("is watching: " + GeolocationAPI.IsUpdatingLocation);
}
private void OnLogEvent(object sender, string message)
{
Log(message);
}
private void OnAuthorizationChangedEvent(object sender, Geolocation.GeolocationAuthorizationStatus status)
{
Log("Auth Status changed to: " + status);
}
private void OnLocationUpdateEvent(object sender, Location locationData)
{
LogLocation(locationData);
CurrentLocation = locationData;
}
private void OnErrorEvent(object sender, Geolocation.GeolocationErrorCodes error)
{
Log("ERROR: " + error.ToString());
}
///////////////////////////////////// old ///////////////////////////////////////////////
// is_updating_location
private void OnCheckIsUpdatingLocation()
{
var isUpdating = GeolocationAPI.IsUpdatingLocation;
Log("Location Updating: " + isUpdating);
}
private void OnCheckIsUpdatingHeading()
{
var isUpdating = GeolocationAPI.IsUpdatingHeading;
Log("Heading Updating: " + isUpdating);
}
// should_show_permission_requirement_explanation
private void OnShouldShowPermissionRequirementExplanation()
{
var shouldShow = GeolocationAPI.ShouldShowPermissionRequirementExplanation;
Log("Should show Explanation: " + shouldShow);
}
private void OnSetFailureTimeout(int seconds)
{
GeolocationAPI.SetFailureTimeout(seconds);
}
private void OnSetMaxWaitTimeAndroid(int waitSeconds)
{
GeolocationAPI.SetMaxWaitTime(waitSeconds);
}
private void OnSetUpdateIntervalAndroid(int interval)
{
GeolocationAPI.SetUpdateInterval(interval);
}
private async void OnStartHeading()
{
/*
if (GeolocationAPI.SupportsHeading)
{
GeolocationAPI.StartUpdatingHeading();
}
else
{
Log("Heading not supported!");
}
*/
if (_headingUpdater != null)
{
Log("## !!! stop old _headingUpdater");
_headingUpdater.Stop();
}
_headingUpdater = GeolocationAPI.GetHeadingUpdater();
if (_headingUpdater is null)
{
Log("## _headingUpdater null! don't loop");
return;
}
Log("## entering new while loop");
Heading? heading;
while ((heading = await _headingUpdater.HeadingUpdateAsync()) != null)
{
LogHeading(heading);
}
Log("#### YEAH after heading loop new"); // we should _always_ arrive here after updates stop
}
private void OnStopHeading()
{
//GeolocationAPI.StopUpdatingHeading();
if (_headingUpdater != null)
{
Log("#### Stopping heading updates");
_headingUpdater.Stop();
}
}
private void OnSetDesiredAccuracyBest()
{
GeolocationAPI.SetDesiredAccuracy(Geolocation.GeolocationDesiredAccuracyConstants.ACCURACY_BEST);
}
private void OnSetDesiredAccuracyHundredMeters()
{
GeolocationAPI.SetDesiredAccuracy(Geolocation.GeolocationDesiredAccuracyConstants.ACCURACY_HUNDRED_METERS);
}
private void OnSetDistanceFilter(float distance)
{
Log("try setting distance filter to: " + distance);
GeolocationAPI.SetDistanceFilter(distance);
}
private void OnCheckFullAccuracy()
{
Log("Full accuracy is: " + GeolocationAPI.AllowsFullAccuracy);
}
private async void OnSetStartLocation()
{
StartLocation = await GeolocationAPI.RequestLocationAsync();
if (StartLocation is null)
{
Log("Could not set Start Location!");
return;
}
LogLocation(StartLocation, "Start Location");
}
private async void OnSetEndLocation()
{
EndLocation = await GeolocationAPI.RequestLocationAsync();
if (EndLocation is null)
{
Log("Could not set End Location!");
return;
}
LogLocation(EndLocation, "End Location");
}
private void OnShowDistance()
{
if (StartLocation is null || EndLocation is null)
{
Log("Start or end missing!");
return;
}
Log("Distance in m: " + StartLocation.DistanceToMeters(EndLocation));
}
private void OnRequestLocation()
{
GeolocationAPI.RequestLocation();
}
private void OnGetAuthorizationStatus()
{
Log("Auth Status: " + GeolocationAPI.AuthorizationStatus);
}
private void LogLocation(Location location, string title = null!)
{
_locationDataOutput.Text = "";
if (title != null) _locationDataOutput.Text += $"# {title} #\n";
_locationDataOutput.Text += $"{location.ToString()}\n";
}
private void LogHeading(Heading heading, string title = null!)
{
_locationDataOutput.Text = "";
if (title != null) _locationDataOutput.Text += $"# {title} #\n";
_locationDataOutput.Text += $"{heading.ToString()}\n";
}
/*
private void OnHeadingUpdate(object sender, Heading headingData)
{
//Log("got new heading");
LogHeading(headingData);
}
*/
private void OnStartWatch()
{
Log("Start");
GeolocationAPI.StartUpdatingLocation();
}
private void OnStopWatch()
{
Log("Stop");
GeolocationAPI.StopUpdatingLocation();
}
private void OnRequestPermission()
{
Log("Requesting Permisson");
GeolocationAPI.RequestPermissions();
}
private void OnClearErrorOutput()
{
_errorOutput.Text = "";
}
private void OnClearLocationOutput()
{
_locationDataOutput.Text = "";
}
private void Log(string message)
{
_errorOutput.Text = message + "\n" + _errorOutput.Text;
}
private void CreateDebugButton(string title, string nameOfMethod, Godot.Collections.Array paramArray = null!)
{
var target = this;
var newBtn = new Button();
newBtn.Text = title;
newBtn.Connect("button_up", target, nameOfMethod, paramArray);
_testButtonContainer.AddChild(newBtn);
}
private Button ReturnButton(string title, string nameOfMethod, Godot.Collections.Array paramArray = null!)
{
var target = this;
var newBtn = new Button();
newBtn.Text = title;
newBtn.Connect("button_up", target, nameOfMethod, paramArray);
return newBtn;
}
}