Skip to content

Commit f9cc8e1

Browse files
committed
Fix picker watch face
1 parent 2f0f3c3 commit f9cc8e1

File tree

4 files changed

+470
-12
lines changed

4 files changed

+470
-12
lines changed

BionicCode.Net/BionicCode.Controls.Net.Wpf/AnalogClockFace.cs

Lines changed: 218 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,92 @@ public double Radius
300300

301301
#endregion Radius read-only dependecy property
302302

303+
#region HourHandRadius dependency property
304+
305+
public static readonly DependencyProperty HourHandRadiusProperty = DependencyProperty.Register(
306+
"HourHandRadius",
307+
typeof(double),
308+
typeof(AnalogClockFace),
309+
new PropertyMetadata(default(double), AnalogClockFace.OnHourHandRadiusChanged));
310+
311+
public double HourHandRadius { get => (double) GetValue(AnalogClockFace.HourHandRadiusProperty); set => SetValue(AnalogClockFace.HourHandRadiusProperty, value); }
312+
313+
#endregion HourHandRadius dependency property
314+
315+
#region MinuteHandRadius dependency property
316+
317+
public static readonly DependencyProperty MinuteHandRadiusProperty = DependencyProperty.Register(
318+
"MinuteHandRadius",
319+
typeof(double),
320+
typeof(AnalogClockFace),
321+
new PropertyMetadata(default(double), AnalogClockFace.OnMinuteHandRadiusChanged));
322+
323+
public double MinuteHandRadius { get => (double) GetValue(AnalogClockFace.MinuteHandRadiusProperty); set => SetValue(AnalogClockFace.MinuteHandRadiusProperty, value); }
324+
325+
#endregion MinuteHandRadius dependency property
326+
327+
#region SecondHandRadius dependency property
328+
329+
public static readonly DependencyProperty SecondHandRadiusProperty = DependencyProperty.Register(
330+
"SecondHandRadius",
331+
typeof(double),
332+
typeof(AnalogClockFace),
333+
new PropertyMetadata(default(double), AnalogClockFace.OnSecondHandRadiusChanged));
334+
335+
public double SecondHandRadius { get => (double) GetValue(AnalogClockFace.SecondHandRadiusProperty); set => SetValue(AnalogClockFace.SecondHandRadiusProperty, value); }
336+
337+
#endregion SecondHandRadius dependency property
338+
339+
#region HourHandDiameter read-only dependency property
340+
protected static readonly DependencyPropertyKey HourHandDiameterPropertyKey = DependencyProperty.RegisterReadOnly(
341+
"HourHandDiameter",
342+
typeof(double),
343+
typeof(AnalogClockFace),
344+
new PropertyMetadata(default(double)));
345+
346+
public static readonly DependencyProperty HourHandDiameterProperty = AnalogClockFace.HourHandDiameterPropertyKey.DependencyProperty;
347+
348+
public double HourHandDiameter
349+
{
350+
get => (double) GetValue(AnalogClockFace.HourHandDiameterProperty);
351+
private set => SetValue(AnalogClockFace.HourHandDiameterPropertyKey, value);
352+
}
353+
#endregion HourHandDiameter read-only dependency property
354+
355+
#region MinuteHandDiameter read-only dependency property
356+
protected static readonly DependencyPropertyKey MinuteHandDiameterPropertyKey = DependencyProperty.RegisterReadOnly(
357+
"MinuteHandDiameter",
358+
typeof(double),
359+
typeof(AnalogClockFace),
360+
new PropertyMetadata(default(double)));
361+
362+
public static readonly DependencyProperty MinuteHandDiameterProperty = AnalogClockFace.MinuteHandDiameterPropertyKey.DependencyProperty;
363+
364+
public double MinuteHandDiameter
365+
{
366+
get => (double) GetValue(AnalogClockFace.MinuteHandDiameterProperty);
367+
private set => SetValue(AnalogClockFace.MinuteHandDiameterPropertyKey, value);
368+
}
369+
370+
#endregion MinuteHandDiameter read-only dependency property
371+
372+
#region SecondHandDiameter read-only dependency property
373+
protected static readonly DependencyPropertyKey SecondHandDiameterPropertyKey = DependencyProperty.RegisterReadOnly(
374+
"SecondHandDiameter",
375+
typeof(double),
376+
typeof(AnalogClockFace),
377+
new PropertyMetadata(default(double)));
378+
379+
public static readonly DependencyProperty SecondHandDiameterProperty = AnalogClockFace.SecondHandDiameterPropertyKey.DependencyProperty;
380+
381+
public double SecondHandDiameter
382+
{
383+
get => (double) GetValue(AnalogClockFace.SecondHandDiameterProperty);
384+
private set => SetValue(AnalogClockFace.SecondHandDiameterPropertyKey, value);
385+
}
386+
387+
#endregion SecondHandDiameter read-only dependency property
388+
303389
#region Is24HModeEnabled dependency property
304390

305391
public static readonly DependencyProperty Is24HModeEnabledProperty = DependencyProperty.Register(
@@ -345,9 +431,9 @@ private void InitializeClockFaceCanvas()
345431
private void InitializeClockRotateTransforms()
346432
{
347433
this.IntervalElementTransform = new RotateTransform(0, this.Radius, this.Radius);
348-
this.HourHandTransform = new RotateTransform();
349-
this.MinuteHandTransform = new RotateTransform(0, this.Radius, this.Radius);
350-
this.SecondHandTransform = new RotateTransform(0, this.Radius, this.Radius);
434+
this.HourHandTransform = new RotateTransform(GetClockAngle(this.SelectedHour, this.Is24HModeEnabled ? 24 : 12), this.Radius, this.Radius);
435+
this.MinuteHandTransform = new RotateTransform(GetClockAngle(this.SelectedMinute, 60), this.Radius, this.Radius);
436+
this.SecondHandTransform = new RotateTransform(GetClockAngle(this.SelectedSecond, 60), this.Radius, this.Radius);
351437
var radiusBinding = new Binding(nameof(this.Radius)) { Source = this };
352438
BindingOperations.SetBinding(this.IntervalElementTransform, RotateTransform.CenterXProperty, radiusBinding);
353439
BindingOperations.SetBinding(this.IntervalElementTransform, RotateTransform.CenterYProperty, radiusBinding);
@@ -366,7 +452,15 @@ private void InitializeClockRotateTransforms()
366452
protected override Size MeasureOverride(Size constraint)
367453
{
368454
constraint = base.MeasureOverride(constraint);
369-
DrawAnalogClock();
455+
456+
if (this.Is24HModeEnabled)
457+
{
458+
DrawAnalog24Clock();
459+
}
460+
else
461+
{
462+
DrawAnalogClock();
463+
}
370464
return constraint;
371465
}
372466

@@ -419,6 +513,24 @@ private static void OnSelectedTimeChanged(DependencyObject d, DependencyProperty
419513
this_.OnSelectedTimeChanged((DateTime) e.OldValue, (DateTime) e.NewValue);
420514
}
421515

516+
private static void OnHourHandRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
517+
{
518+
var this_ = d as AnalogClockFace;
519+
this_.HourHandDiameter = (double) e.NewValue * 2;
520+
}
521+
522+
private static void OnMinuteHandRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
523+
{
524+
var this_ = d as AnalogClockFace;
525+
this_.MinuteHandDiameter = (double)e.NewValue * 2;
526+
}
527+
528+
private static void OnSecondHandRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
529+
{
530+
var this_ = d as AnalogClockFace;
531+
this_.SecondHandDiameter = (double)e.NewValue * 2;
532+
}
533+
422534
protected virtual void OnSelectedTimeChanged(DateTime oldValue, DateTime newValue)
423535
{
424536
if (this.IsUpdatingSelectedTimeComponent)
@@ -501,7 +613,7 @@ private static object CoerceSeconds(DependencyObject d, object basevalue)
501613

502614
protected virtual void OnSelectedHourChanged(double oldValue, double newValue)
503615
{
504-
double angle = GetClockAngle(newValue, 12);
616+
double angle = GetClockAngle(newValue, this.Is24HModeEnabled ? 24 : 12);
505617
this.HourHandTransform.Angle = angle;
506618

507619
if (this.IsUpdatingSelectedTime)
@@ -759,7 +871,7 @@ protected virtual void DrawAnalogClock()
759871
UIElement intervalMarker = CreateIntervalMarker(step);
760872
if (intervalMarkerCenterPositionRadius.Equals(-1))
761873
{
762-
InitializeRadiusOfIntervalMarkers(out intervalMarkerCenterPositionRadius, intervalMarker);
874+
InitializeRadiusOfIntervalMarkers(out intervalMarkerCenterPositionRadius, intervalMarker, this.Radius);
763875
}
764876

765877
DrawIntervalMarker(degreesOfCurrentStep, intervalMarkerCenterPositionRadius, intervalMarker);
@@ -777,6 +889,93 @@ protected virtual void DrawAnalogClock()
777889
OnClockFaceLoaded();
778890
}
779891

892+
protected virtual void DrawAnalog24Clock()
893+
{
894+
this.ClockFaceCanvas.Children.Clear();
895+
896+
if (this.Diameter == 0)
897+
{
898+
return;
899+
}
900+
901+
double steps = 60.0;
902+
double degreeOfStep = 360.0 / steps;
903+
double intervalMarkerCenterPositionRadius = -1;
904+
for (int step = 0; step < steps; step++)
905+
{
906+
double degreesOfCurrentStep = step * degreeOfStep;
907+
908+
UIElement intervalMarker = CreateIntervalMarker(step);
909+
if (intervalMarkerCenterPositionRadius.Equals(-1))
910+
{
911+
InitializeRadiusOfIntervalMarkers(out intervalMarkerCenterPositionRadius, intervalMarker, this.MinuteHandRadius);
912+
}
913+
914+
DrawIntervalMarker(degreesOfCurrentStep, intervalMarkerCenterPositionRadius, intervalMarker);
915+
916+
if (step % 5 == 0)
917+
{
918+
UIElement intervalMarkerLabel = CreateIntervalLabel(step);
919+
if (intervalMarkerLabel != null)
920+
{
921+
DrawIntervalLabel(intervalMarkerCenterPositionRadius, degreesOfCurrentStep, intervalMarkerLabel);
922+
}
923+
}
924+
}
925+
926+
steps = 24.0;
927+
degreeOfStep = 360.0 / steps;
928+
intervalMarkerCenterPositionRadius = -1;
929+
for (int step = 0; step < steps; step++)
930+
{
931+
double degreesOfCurrentStep = step * degreeOfStep;
932+
933+
UIElement intervalMarker = CreateIntervalMarker(step);
934+
if (intervalMarkerCenterPositionRadius.Equals(-1))
935+
{
936+
InitializeRadiusOfIntervalMarkers(out intervalMarkerCenterPositionRadius, intervalMarker, this.HourHandRadius);
937+
}
938+
939+
DrawIntervalMarker(degreesOfCurrentStep, intervalMarkerCenterPositionRadius, intervalMarker);
940+
941+
UIElement intervalMarkerLabel = CreateIntervalLabel(step);
942+
if (intervalMarkerLabel != null)
943+
{
944+
DrawIntervalLabel(intervalMarkerCenterPositionRadius, degreesOfCurrentStep, intervalMarkerLabel);
945+
}
946+
}
947+
948+
if (this.MinuteHandRadius != this.SecondHandRadius)
949+
{
950+
steps = 60.0;
951+
degreeOfStep = 360.0 / steps;
952+
intervalMarkerCenterPositionRadius = -1;
953+
for (int step = 0; step < steps; step++)
954+
{
955+
double degreesOfCurrentStep = step * degreeOfStep;
956+
957+
UIElement intervalMarker = CreateIntervalMarker(step);
958+
if (intervalMarkerCenterPositionRadius.Equals(-1))
959+
{
960+
InitializeRadiusOfIntervalMarkers(out intervalMarkerCenterPositionRadius, intervalMarker, this.SecondHandRadius);
961+
}
962+
963+
DrawIntervalMarker(degreesOfCurrentStep, intervalMarkerCenterPositionRadius, intervalMarker);
964+
965+
//UIElement intervalMarkerLabel = CreateIntervalLabel(step);
966+
//if (intervalMarkerLabel != null)
967+
//{
968+
// DrawIntervalLabel(intervalMarkerCenterPositionRadius, degreesOfCurrentStep, intervalMarkerLabel);
969+
//}
970+
}
971+
}
972+
973+
Draw24ClockFaceBackground(intervalMarkerCenterPositionRadius);
974+
975+
AddClockHands();
976+
OnClockFaceLoaded();
977+
}
978+
780979
private void DrawClockFaceBackground(double intervalMarkerCenterPositionRadius)
781980
{
782981
double deltaToMiddleRadius = (this.Radius - intervalMarkerCenterPositionRadius) * 2;
@@ -791,6 +990,17 @@ private void DrawClockFaceBackground(double intervalMarkerCenterPositionRadius)
791990
0);
792991
}
793992

993+
private void Draw24ClockFaceBackground(double intervalMarkerCenterPositionRadius)
994+
{
995+
AddElementToClockFace(
996+
new Ellipse()
997+
{
998+
Height = this.Diameter, Width = this.Diameter, Fill = this.Background
999+
},
1000+
new Point(),
1001+
0);
1002+
}
1003+
7941004
private void DrawIntervalLabel(
7951005
double intervalMarkerCenterPositionRadius,
7961006
double degreesOfCurrentStep,
@@ -900,10 +1110,10 @@ private UIElement CreateIntervalLabel(int step)
9001110
return intervalMarkerLabel;
9011111
}
9021112

903-
private void InitializeRadiusOfIntervalMarkers(out double intervalMarkerCenterPositionRadius, UIElement intervalMarker)
1113+
private void InitializeRadiusOfIntervalMarkers(out double intervalMarkerCenterPositionRadius, UIElement intervalMarker, double radius)
9041114
{
9051115
double radiusOffset = CalculateIntervalMarkerCenterRadiusOffset(intervalMarker);
906-
intervalMarkerCenterPositionRadius = this.Radius + radiusOffset;
1116+
intervalMarkerCenterPositionRadius = radius + radiusOffset;
9071117
}
9081118

9091119
private void AlignElementCenterPointToRadius(ref Point cartesianPoint, UIElement element)

BionicCode.Net/BionicCode.Controls.Net.Wpf/AnalogTimePicker.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System.Windows;
99
using System.Windows.Controls;
10+
using System.Windows.Input;
1011
using System.Windows.Markup;
1112
using System.Windows.Shapes;
1213

@@ -51,6 +52,16 @@ public AnalogTimePicker()
5152
//this.AnalogClockFace = new AnalogClockFace() {Background = Brushes.PaleVioletRed};
5253
}
5354

55+
#region Overrides of UIElement
56+
57+
/// <inheritdoc />
58+
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
59+
{
60+
base.OnPreviewMouseLeftButtonDown(e);
61+
}
62+
63+
#endregion
64+
5465
#region Overrides of AnalogClockFace
5566

5667
//protected virtual void OnClockFaceLoaded(object sender, RoutedEventArgs routedEventArgs)

0 commit comments

Comments
 (0)