From 42486521231197f4c3c15db3dc804bc0e28d7940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Tue, 12 Dec 2017 21:31:15 +0100 Subject: [PATCH] Added DotMatrixClock Sample --- Samples/DotMatrixClock/DotMatrixClockPage.cs | 170 ++++++++++++++++++ .../DotMatrixClock/DotMatrixClockPage.xaml | 10 ++ Samples/DotMatrixClockSample.cs | 21 +++ Samples/Program.cs | 1 + Samples/Samples.csproj | 4 + 5 files changed, 206 insertions(+) create mode 100644 Samples/DotMatrixClock/DotMatrixClockPage.cs create mode 100644 Samples/DotMatrixClock/DotMatrixClockPage.xaml create mode 100644 Samples/DotMatrixClockSample.cs diff --git a/Samples/DotMatrixClock/DotMatrixClockPage.cs b/Samples/DotMatrixClock/DotMatrixClockPage.cs new file mode 100644 index 0000000..ae08948 --- /dev/null +++ b/Samples/DotMatrixClock/DotMatrixClockPage.cs @@ -0,0 +1,170 @@ +using System; +using Xamarin.Forms; + +namespace DotMatrixClock +{ + public partial class DotMatrixClockPage : ContentPage + { + // Total dots horizontally and vertically. + const int horzDots = 41; + const int vertDots = 7; + + // 5 x 7 dot matrix patterns for 0 through 9. + static readonly int[, ,] numberPatterns = new int[10, 7, 5] + { + { + { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 1, 0, 0, 1, 1}, { 1, 0, 1, 0, 1}, + { 1, 1, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0} + }, + { + { 0, 0, 1, 0, 0}, { 0, 1, 1, 0, 0}, { 0, 0, 1, 0, 0}, { 0, 0, 1, 0, 0}, + { 0, 0, 1, 0, 0}, { 0, 0, 1, 0, 0}, { 0, 1, 1, 1, 0} + }, + { + { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 0, 0, 0, 0, 1}, { 0, 0, 0, 1, 0}, + { 0, 0, 1, 0, 0}, { 0, 1, 0, 0, 0}, { 1, 1, 1, 1, 1} + }, + { + { 1, 1, 1, 1, 1}, { 0, 0, 0, 1, 0}, { 0, 0, 1, 0, 0}, { 0, 0, 0, 1, 0}, + { 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0} + }, + { + { 0, 0, 0, 1, 0}, { 0, 0, 1, 1, 0}, { 0, 1, 0, 1, 0}, { 1, 0, 0, 1, 0}, + { 1, 1, 1, 1, 1}, { 0, 0, 0, 1, 0}, { 0, 0, 0, 1, 0} + }, + { + { 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0}, { 1, 1, 1, 1, 0}, { 0, 0, 0, 0, 1}, + { 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0} + }, + { + { 0, 0, 1, 1, 0}, { 0, 1, 0, 0, 0}, { 1, 0, 0, 0, 0}, { 1, 1, 1, 1, 0}, + { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0} + }, + { + { 1, 1, 1, 1, 1}, { 0, 0, 0, 0, 1}, { 0, 0, 0, 1, 0}, { 0, 0, 1, 0, 0}, + { 0, 1, 0, 0, 0}, { 0, 1, 0, 0, 0}, { 0, 1, 0, 0, 0} + }, + { + { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0}, + { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0} + }, + { + { 0, 1, 1, 1, 0}, { 1, 0, 0, 0, 1}, { 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 1}, + { 0, 0, 0, 0, 1}, { 0, 0, 0, 1, 0}, { 0, 1, 1, 0, 0} + }, + }; + + // Dot matrix pattern for a colon. + static readonly int[,] colonPattern = new int[7, 2] + { + { 0, 0 }, { 1, 1 }, { 1, 1 }, { 0, 0 }, { 1, 1 }, { 1, 1 }, { 0, 0 } + }; + + // BoxView colors for on and off. + static readonly Color colorOn = Color.Red; + static readonly Color colorOff = new Color(0.5, 0.5, 0.5, 0.25); + + // Box views for 6 digits, 7 rows, 5 columns. + BoxView[, ,] digitBoxViews = new BoxView[6, 7, 5]; + + public DotMatrixClockPage() + { + InitializeComponent(); + + // BoxView dot dimensions. + double height = 0.85 / vertDots; + double width = 0.85 / horzDots; + + // Create and assemble the BoxViews. + double xIncrement = 1.0 / (horzDots - 1); + double yIncrement = 1.0 / (vertDots - 1); + double x = 0; + + for (int digit = 0; digit < 6; digit++) + { + for (int col = 0; col < 5; col++) + { + double y = 0; + + for (int row = 0; row < 7; row++) + { + // Create the digit BoxView and add to layout. + BoxView boxView = new BoxView(); + digitBoxViews[digit, row, col] = boxView; + absoluteLayout.Children.Add(boxView, + new Rectangle(x, y, width, height), + AbsoluteLayoutFlags.All); + y += yIncrement; + } + x += xIncrement; + } + x += xIncrement; + + // Colons between the hours, minutes, and seconds. + if (digit == 1 || digit == 3) + { + int colon = digit / 2; + + for (int col = 0; col < 2; col++) + { + double y = 0; + + for (int row = 0; row < 7; row++) + { + // Create the BoxView and set the color. + BoxView boxView = new BoxView + { + Color = colonPattern[row, col] == 1 ? + colorOn : colorOff + }; + absoluteLayout.Children.Add(boxView, + new Rectangle(x, y, width, height), + AbsoluteLayoutFlags.All); + y += yIncrement; + } + x += xIncrement; + } + x += xIncrement; + } + } + + // Set the timer and initialize with a manual call. + Device.StartTimer(TimeSpan.FromSeconds(1), OnTimer); + OnTimer(); + } + + void OnPageSizeChanged(object sender, EventArgs args) + { + // No chance a display will have an aspect ratio > 41:7 + absoluteLayout.HeightRequest = vertDots * Width / horzDots; + } + + bool OnTimer() + { + DateTime dateTime = DateTime.Now; + + // Convert 24-hour clock to 12-hour clock. + int hour = (dateTime.Hour + 11) % 12 + 1; + + // Set the dot colors for each digit separately. + SetDotMatrix(0, hour / 10); + SetDotMatrix(1, hour % 10); + SetDotMatrix(2, dateTime.Minute / 10); + SetDotMatrix(3, dateTime.Minute % 10); + SetDotMatrix(4, dateTime.Second / 10); + SetDotMatrix(5, dateTime.Second % 10); + return true; + } + + void SetDotMatrix(int index, int digit) + { + for (int row = 0; row < 7; row++) + for (int col = 0; col < 5; col++) + { + bool isOn = numberPatterns[digit, row, col] == 1; + Color color = isOn ? colorOn : colorOff; + digitBoxViews[index, row, col].Color = color; + } + } + } +} diff --git a/Samples/DotMatrixClock/DotMatrixClockPage.xaml b/Samples/DotMatrixClock/DotMatrixClockPage.xaml new file mode 100644 index 0000000..41ec329 --- /dev/null +++ b/Samples/DotMatrixClock/DotMatrixClockPage.xaml @@ -0,0 +1,10 @@ + + + + diff --git a/Samples/DotMatrixClockSample.cs b/Samples/DotMatrixClockSample.cs new file mode 100644 index 0000000..b901e86 --- /dev/null +++ b/Samples/DotMatrixClockSample.cs @@ -0,0 +1,21 @@ +using Ooui; +using Xamarin.Forms; + +namespace Samples +{ + public class DotMatrixClockSample : ISample + { + public string Title => "Xamarin.Forms DoMatrixClock"; + + public Ooui.Element CreateElement() + { + var page = new DotMatrixClock.DotMatrixClockPage(); + return page.GetOouiElement(); + } + + public void Publish() + { + UI.Publish("/dotmatrixclock", CreateElement); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index 99b39ad..15c5c98 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -30,6 +30,7 @@ namespace Samples new DrawSample ().Publish (); new FilesSample ().Publish (); new DisplayAlertSample ().Publish (); + new DotMatrixClockSample().Publish(); new EditorSample().Publish(); new TipCalcSample().Publish(); new XuzzleSample().Publish(); diff --git a/Samples/Samples.csproj b/Samples/Samples.csproj index cf5d6b4..d488c4c 100644 --- a/Samples/Samples.csproj +++ b/Samples/Samples.csproj @@ -38,6 +38,9 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:Compile + MSBuild:Compile @@ -49,6 +52,7 @@ +