This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
/
ArrayParameter.cs
937 lines (801 loc) · 29.8 KB
/
ArrayParameter.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/*
* Copyright © 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
using Microsoft.Devices;
using Microsoft.Phone.Info;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Windows.Phone.Media.Capture;
namespace CameraExplorer
{
/// <summary>
/// Enumerator to enumerate through ArrayParameterOptions in an ArrayParameter.
/// </summary>
public class ArrayParameterEnumerator : IEnumerator<ArrayParameterOption>
{
private ArrayParameter _arrayParameter;
private int _count;
private int _index = -1;
public ArrayParameterEnumerator(ArrayParameter arrayParameter, int count)
{
_arrayParameter = arrayParameter;
_count = count;
}
/// <summary>
/// Current option.
/// </summary>
public object Current
{
get
{
return _arrayParameter.Option(_index);
}
}
/// <summary>
/// Current option.
/// </summary>
ArrayParameterOption IEnumerator<ArrayParameterOption>.Current
{
get
{
return (ArrayParameterOption)Current;
}
}
/// <summary>
/// Step forward one item.
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (_index < _count - 1)
{
_index++;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Reset to the beginning.
/// </summary>
public void Reset()
{
_index = -1;
}
public void Dispose()
{
}
}
/// <summary>
/// ArrayParameterOption is a single item in an ArrayParameter.
/// </summary>
public class ArrayParameterOption
{
private dynamic _value;
private string _name;
private string _overlaySource;
public ArrayParameterOption(dynamic value, string name, string overlaySource = null)
{
_value = value;
_name = name;
_overlaySource = overlaySource;
}
/// <summary>
/// Camera property value related to this option.
/// </summary>
public dynamic Value
{
get
{
return _value;
}
}
/// <summary>
/// Name of this option.
/// </summary>
public string Name
{
get
{
return _name;
}
}
/// <summary>
/// Overlay icon path for this option.
/// </summary>
public string OverlaySource
{
get
{
return _overlaySource;
}
}
}
/// <summary>
/// Abstract parameter base class for array type parameters.
/// </summary>
public abstract class ArrayParameter : Parameter, IReadOnlyCollection<ArrayParameterOption>
{
private List<ArrayParameterOption> _options = new List<ArrayParameterOption>();
private ArrayParameterOption _selectedOption;
private Guid _propertyId;
private bool _refreshing = false;
public ArrayParameter(PhotoCaptureDevice device, string name)
: base(device, name)
{
}
public ArrayParameter(PhotoCaptureDevice device, Guid propertyId, string name)
: base(device, name)
{
_propertyId = propertyId;
}
/// <summary>
/// Read parameter options and current value from Parameter.Device. Previous options
/// are discarded and new options shall be created in the abstract void PopulateOptions()
/// method.
/// </summary>
public override void Refresh()
{
_refreshing = true;
_options.Clear();
_selectedOption = null;
try
{
PopulateOptions();
Supported = _options.Count > 0;
}
catch (Exception)
{
Supported = false;
System.Diagnostics.Debug.WriteLine("Getting " + Name.ToLower() + " failed");
}
Modifiable = Supported && _options.Count > 1;
if (Supported)
{
NotifyPropertyChanged("Count");
NotifyPropertyChanged("SelectedOption");
NotifyPropertyChanged("OverlaySource");
}
_refreshing = false;
}
/// <summary>
/// Get option for index.
/// </summary>
/// <param name="index">Option index</param>
/// <returns>Option</returns>
public ArrayParameterOption Option(int index)
{
return _options[index];
}
/// <summary>
/// Current option.
/// </summary>
public ArrayParameterOption SelectedOption
{
get
{
return _selectedOption;
}
set
{
if (value == null) return; // null check to avoid http://stackoverflow.com/questions/3446102
// value should not be saved when initialing the array
bool save = _selectedOption != null;
if (_selectedOption != value)
{
if (!(_refreshing && _selectedOption == null))
{
SetOption(value);
}
_selectedOption = value;
OverlaySource = _selectedOption.OverlaySource;
if (!(_refreshing && _selectedOption == null))
{
NotifyPropertyChanged("SelectedOption");
NotifyPropertyChanged("OverlaySource");
}
if (save)
{
Save();
}
}
}
}
/// <summary>
/// Amount of options in this ArrayParameter.
/// </summary>
public int Count
{
get
{
return _options.Count;
}
}
/// <summary>
/// Get an enumerator to this ArrayParameter.
/// </summary>
/// <returns>Enumerator</returns>
public IEnumerator<ArrayParameterOption> GetEnumerator()
{
return new ArrayParameterEnumerator(this, _options.Count);
}
/// <summary>
/// Get an enumerator to this ArrayParameter.
/// </summary>
/// <returns>Enumerator</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return new ArrayParameterEnumerator(this, _options.Count);
}
/// <summary>
/// Camera Guid that this ArrayParameter acts on.
/// </summary>
protected Guid PropertyId
{
get
{
return _propertyId;
}
}
/// <summary>
/// List of options for this ArrayParameter.
/// </summary>
protected List<ArrayParameterOption> Options
{
get
{
return _options;
}
}
/// <summary>
/// Abstract method to read the supported properties from the Parameter.Device.
/// This method must populate the Options parameter with all the supported
/// ArrayParameterOptions for the parameter in question. SelectedOption must be
/// set as well.
/// </summary>
protected abstract void PopulateOptions();
/// <summary>
/// Abstract method to set a option as the current option. This method must set the
/// Value from the ArrayParameterOption in an appropriate way to the Parameter.Device.
/// </summary>
/// <param name="option">Option to set</param>
protected abstract void SetOption(ArrayParameterOption option);
/// <summary>
/// Set saved value if exists, otherwise set to default.
/// </summary>
public override void SetSavedOrDefault()
{
System.Diagnostics.Debug.WriteLine("Setting saved or default value to setting " + Name);
if (!Load())
{
SetDefault();
}
}
/// <summary>
/// Save parameter to application settings.
/// </summary>
public override void Save()
{
if (SelectedOption == null || SelectedOption.Name == null || SelectedOption.Name.Length <= 0) return;
if (DataContext.Settings.Contains(ParameterSettingName))
{
if ((string)DataContext.Settings[ParameterSettingName] != SelectedOption.Name)
{
DataContext.Settings[ParameterSettingName] = SelectedOption.Name;
}
}
else
{
DataContext.Settings.Add(ParameterSettingName, SelectedOption.Name);
}
}
/// <summary>
/// Load parameter from application settings.
/// </summary>
/// <returns>true if setting was loaded successfully, otherwise false.</returns>
public override bool Load()
{
bool ret = false;
if (DataContext.Settings.Contains(ParameterSettingName))
{
string name = DataContext.Settings[ParameterSettingName].ToString();
for (int i = 0; i < Options.Count; i++)
{
if (Options[i].Name.Equals(name))
{
ret = true;
SelectedOption = Options[i];
}
}
}
return ret;
}
}
/// <summary>
/// Parameter to handle reading supported capture resolutions and changing the currently
/// active capture resolution.
/// </summary>
public class CaptureResolutionParameter : ArrayParameter
{
public CaptureResolutionParameter(PhotoCaptureDevice device)
: base(device, "Capture resolution")
{
}
/// <summary>
/// Reads supported capture resolutions from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
/// </summary>
protected override void PopulateOptions()
{
IReadOnlyList<Windows.Foundation.Size> supportedValues = PhotoCaptureDevice.GetAvailableCaptureResolutions(Device.SensorLocation);
Windows.Foundation.Size value = Device.CaptureResolution;
ArrayParameterOption option = null;
foreach (Windows.Foundation.Size i in supportedValues)
{
option = new ArrayParameterOption(i, i.Width + " x " + i.Height);
Options.Add(option);
if (i.Equals(value))
{
SelectedOption = option;
}
}
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected async override void SetOption(ArrayParameterOption option)
{
if (Modifiable)
{
Modifiable = false;
await Device.SetCaptureResolutionAsync((Windows.Foundation.Size)option.Value);
Modifiable = true;
}
}
/// <summary>
/// Default option for capture resolution is the first supported resolution.
/// </summary>
public override void SetDefault()
{
SelectedOption = Options.Count > 0 ? Options.First() : null;
}
}
/// <summary>
/// Parameter to handle reading supported exposure time values and changing the currently
/// active exposure time.
/// </summary>
public class ExposureTimeParameter : ArrayParameter
{
public ExposureTimeParameter(PhotoCaptureDevice device)
: base(device, KnownCameraPhotoProperties.ExposureTime, "Exposure time")
{
}
/// <summary>
/// Reads supported exposure time values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
///
/// Exposure time auto value is set by setting the value in PhotoCaptureDevice API to
/// null, therefore the separate handling for option "Auto".
/// </summary>
protected override void PopulateOptions()
{
ArrayParameterOption option = new ArrayParameterOption(null, "Auto", "Assets/Icons/overlay.exposuretime.auto.png");
ArrayParameterOption selectedOption = option;
Options.Add(option);
CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, KnownCameraPhotoProperties.ExposureTime);
object value = Device.GetProperty(PropertyId);
UInt32[] standardValues = { /* 16000, 8000, 4000,*/ 2000, 1000, 500, 250, 125, 60, 30, 15, 8, 4, 2 };
UInt32 min = (UInt32)range.Min;
UInt32 max = (UInt32)range.Max;
foreach (UInt32 i in standardValues)
{
UInt32 usecs = 1000000 / i;
if (usecs >= min && usecs <= max)
{
option = new ArrayParameterOption(usecs, "1 / " + i.ToString() + " s", "Assets/Icons/overlay.exposuretime." + i.ToString() + ".png");
Options.Add(option);
if (selectedOption == null && usecs.Equals(value))
{
selectedOption = option;
}
}
}
// Expsoure times of 1 second and over are possible in some devices.
UInt32 microseconds = 1000000; // second in microseconds
while (microseconds <= max)
{
UInt32 usecs = microseconds / 1000000;
option = new ArrayParameterOption(microseconds, usecs.ToString() + " s", "Assets/Icons/overlay.exposuretime." + usecs.ToString() + "s.png");
Options.Add(option);
if (selectedOption == null && usecs.Equals(value))
{
selectedOption = option;
}
microseconds *= 2;
}
SelectedOption = selectedOption;
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, option.Value);
}
/// <summary>
/// Default option for exposure time is the first supported value.
/// </summary>
public override void SetDefault()
{
SelectedOption = Options.Count > 0 ? Options.First() : null;
}
}
/// <summary>
/// Parameter to handle reading supported ISO values and changing the currently
/// active ISO setting.
/// </summary>
public class IsoParameter : ArrayParameter
{
public IsoParameter(PhotoCaptureDevice device)
: base(device, KnownCameraPhotoProperties.Iso, "ISO")
{
}
/// <summary>
/// Reads supported ISO values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
///
/// ISO auto value is set by setting the value in PhotoCaptureDevice API to
/// null, therefore the separate handling for option "Auto".
/// </summary>
protected override void PopulateOptions()
{
ArrayParameterOption option = new ArrayParameterOption(null, "Auto", "Assets/Icons/overlay.iso.auto.png");
ArrayParameterOption selectedOption = option;
Options.Add(option);
CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, PropertyId);
object value = Device.GetProperty(PropertyId);
UInt32[] standardValues = { 100, 200, 400, 800, 1600, 3200 };
UInt32 min = (UInt32)range.Min;
UInt32 max = (UInt32)range.Max;
foreach (UInt32 i in standardValues)
{
if (i >= min && i <= max)
{
option = new ArrayParameterOption(i, "ISO " + i.ToString(), "Assets/Icons/overlay.iso." + i.ToString() + ".png");
Options.Add(option);
if (i.Equals(value))
{
selectedOption = option;
}
}
}
SelectedOption = selectedOption;
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, option.Value);
}
/// <summary>
/// Default option for ISO is the first supported value.
/// </summary>
public override void SetDefault()
{
SelectedOption = Options.Count > 0 ? Options.First() : null;
}
}
/// <summary>
/// Parameter to handle reading supported scene mode values and changing the currently
/// active scene mode.
/// </summary>
public class SceneModeParameter : ArrayParameter
{
public SceneModeParameter(PhotoCaptureDevice device)
: base(device, KnownCameraPhotoProperties.SceneMode, "Scene mode")
{
}
/// <summary>
/// Reads supported scene mode values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
/// </summary>
protected override void PopulateOptions()
{
IReadOnlyList<object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
object value = Device.GetProperty(PropertyId);
foreach (dynamic i in supportedValues)
{
CameraSceneMode csm = (CameraSceneMode)i;
ArrayParameterOption option = new ArrayParameterOption(csm, csm.EnumerationToParameterName<CameraSceneMode>(), "Assets/Icons/overlay.scenemode." + csm.ToString().ToLower() + ".png");
Options.Add(option);
if (i.Equals(value))
{
SelectedOption = option;
}
}
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, option.Value);
}
/// <summary>
/// Default option for scene mode is either Auto if it is supported, or the last supported value.
/// </summary>
public override void SetDefault()
{
bool found = false;
foreach (ArrayParameterOption i in Options)
{
if ((CameraSceneMode)i.Value == CameraSceneMode.Auto || i == Options.Last())
{
SelectedOption = i;
found = true;
break;
}
}
if (!found)
{
SelectedOption = null;
}
}
}
/// <summary>
/// Parameter to handle reading supported flash mode values and changing the currently
/// active scene mode.
/// </summary>
public class FlashModeParameter : ArrayParameter
{
public FlashModeParameter(PhotoCaptureDevice device)
: base(device, KnownCameraPhotoProperties.FlashMode, "Flash mode")
{
}
/// <summary>
/// Reads supported flash mode values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
/// </summary>
protected override void PopulateOptions()
{
IReadOnlyList<object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
object value = Device.GetProperty(PropertyId);
foreach (dynamic i in supportedValues)
{
FlashState fm = (FlashState)i;
ArrayParameterOption option = new ArrayParameterOption(fm, fm.EnumerationToParameterName<FlashState>(), "Assets/Icons/overlay.flashmode." + fm.ToString().ToLower() + ".png");
Options.Add(option);
if (i.Equals(value))
{
SelectedOption = option;
}
}
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, (FlashState)option.Value);
}
/// <summary>
/// Default option for flash mode is either Auto if it is supported, or none.
/// </summary>
public override void SetDefault()
{
bool found = false;
foreach (ArrayParameterOption i in Options)
{
if (i.Value == FlashState.Auto)
{
SelectedOption = i;
found = true;
break;
}
}
if (!found)
{
SelectedOption = null;
}
}
}
/// <summary>
/// Parameter to handle reading supported focus illumination mode values and changing the currently
/// active focus illumination.
/// </summary>
public class FocusIlluminationModeParameter : ArrayParameter
{
public FocusIlluminationModeParameter(PhotoCaptureDevice device)
: base(device, KnownCameraPhotoProperties.FocusIlluminationMode, "Focus illumination mode")
{
}
/// <summary>
/// Reads supported focus illumination mode values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
/// </summary>
protected override void PopulateOptions()
{
IReadOnlyList<object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
object value = Device.GetProperty(PropertyId);
foreach (dynamic i in supportedValues)
{
FocusIlluminationMode fim = (FocusIlluminationMode)i;
ArrayParameterOption option = new ArrayParameterOption(fim, fim.EnumerationToParameterName<FocusIlluminationMode>());
Options.Add(option);
if (i.Equals(value))
{
SelectedOption = option;
}
}
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, option.Value);
}
/// <summary>
/// Default option for focus illumination mode is either Auto if it is supported, or none.
/// </summary>
public override void SetDefault()
{
bool found = false;
foreach (ArrayParameterOption i in Options)
{
if (i.Value == FocusIlluminationMode.Auto)
{
SelectedOption = i;
found = true;
break;
}
}
if (!found)
{
SelectedOption = null;
}
}
}
/// <summary>
/// Parameter to handle reading supported white balance preset values and changing the currently
/// active white balance preset.
/// </summary>
public class WhiteBalancePresetParameter : ArrayParameter
{
public WhiteBalancePresetParameter(PhotoCaptureDevice device)
: base(device, KnownCameraPhotoProperties.WhiteBalancePreset, "White balance preset")
{
}
/// <summary>
/// Reads supported white balance preset values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
///
/// White balace preset auto value is set by setting the value in PhotoCaptureDevice API to
/// null, therefore the separate handling for option "Auto".
/// </summary>
protected override void PopulateOptions()
{
ArrayParameterOption option = new ArrayParameterOption(null, "Auto");
ArrayParameterOption selectedOption = option;
Options.Add(option);
IReadOnlyList<object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
object value = Device.GetProperty(PropertyId);
foreach (dynamic i in supportedValues)
{
WhiteBalancePreset wbp = (WhiteBalancePreset)i;
option = new ArrayParameterOption(wbp, wbp.EnumerationToParameterName<WhiteBalancePreset>());
Options.Add(option);
if (i.Equals(value))
{
selectedOption = option;
}
}
SelectedOption = selectedOption;
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, option.Value);
}
/// <summary>
/// Default option for white balance preset is the first supported value.
/// </summary>
public override void SetDefault()
{
SelectedOption = Options.Count > 0 ? Options.First() : null;
}
}
/// <summary>
/// Parameter to handle reading supported autofocus range values and changing the currently
/// active white balance preset.
/// </summary>
public class AutoFocusRangeParameter : ArrayParameter
{
public AutoFocusRangeParameter(PhotoCaptureDevice device)
: base(device, KnownCameraGeneralProperties.AutoFocusRange, "Auto focus range")
{
}
/// <summary>
/// Reads supported autofocus range values from Parameter.Device and populates
/// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
/// </summary>
protected override void PopulateOptions()
{
IReadOnlyList<object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
object value = Device.GetProperty(PropertyId);
foreach (dynamic i in supportedValues)
{
AutoFocusRange afr = (AutoFocusRange)i;
ArrayParameterOption option = new ArrayParameterOption(afr, afr.EnumerationToParameterName<AutoFocusRange>());
Options.Add(option);
if (i.Equals(value))
{
SelectedOption = option;
}
}
}
/// <summary>
/// The API response on querying whether the AutoFocusRange parameter is supported
/// cannot be trusted on HTC devices at least, see http://bit.ly/11Midmq
/// Until further notice we call the base Refresh implementation only for Nokia devices
/// and asume the feature not to be supported on the other.
/// </summary>
public override void Refresh()
{
if (CultureInfo.InvariantCulture.CompareInfo.IndexOf(DeviceStatus.DeviceManufacturer, "Nokia", CompareOptions.IgnoreCase) == -1)
{
Supported = false;
Modifiable = false;
return;
}
base.Refresh();
}
/// <summary>
/// Handles setting the given option as currently active one.
/// </summary>
/// <param name="option">Option to set as current value</param>
protected override void SetOption(ArrayParameterOption option)
{
Device.SetProperty(PropertyId, option.Value);
}
/// <summary>
/// Default option for autofocus range is either Normal if it is supported, or null.
/// </summary>
public override void SetDefault()
{
bool found = false;
foreach (ArrayParameterOption i in Options)
{
if (i.Value == AutoFocusRange.Normal)
{
SelectedOption = i;
found = true;
break;
}
}
if (!found)
{
SelectedOption = null;
}
}
}
}