Skip to content

Commit 4d9d1f3

Browse files
authored
Cat clock sample (#556)
* CatClock demo. * Add readme and screenshot.
1 parent b65a591 commit 4d9d1f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+9261
-0
lines changed

9.0/SkiaSharp/CatClock/CatClock.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CatClock", "CatClock\CatClock.csproj", "{C2E5FCA4-EA76-4AEB-A3BB-7D5E02AB265E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C2E5FCA4-EA76-4AEB-A3BB-7D5E02AB265E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C2E5FCA4-EA76-4AEB-A3BB-7D5E02AB265E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C2E5FCA4-EA76-4AEB-A3BB-7D5E02AB265E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C2E5FCA4-EA76-4AEB-A3BB-7D5E02AB265E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:CatClock"
5+
x:Class="CatClock.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace CatClock
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
}
9+
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new Window(new AppShell());
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="CatClock.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:CatClock"
7+
Shell.FlyoutBehavior="Flyout"
8+
Title="CatClock">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CatClock
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SkiaSharp;
2+
using System.Reflection;
3+
4+
namespace CatClock
5+
{
6+
static class BitmapExtensions
7+
{
8+
public static SKBitmap LoadBitmapResource(Type type, string resourceId)
9+
{
10+
Assembly assembly = type.GetTypeInfo().Assembly;
11+
using (Stream? stream = assembly.GetManifestResourceStream(resourceId))
12+
{
13+
return SKBitmap.Decode(stream);
14+
}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
6+
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
7+
<!-- <TargetFrameworks>$(TargetFrameworks);net9.0-tizen</TargetFrameworks> -->
8+
9+
<!-- Note for MacCatalyst:
10+
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
11+
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
12+
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
13+
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
14+
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
15+
16+
<OutputType>Exe</OutputType>
17+
<RootNamespace>CatClock</RootNamespace>
18+
<UseMaui>true</UseMaui>
19+
<SingleProject>true</SingleProject>
20+
<ImplicitUsings>enable</ImplicitUsings>
21+
<Nullable>enable</Nullable>
22+
23+
<!-- Display name -->
24+
<ApplicationTitle>CatClock</ApplicationTitle>
25+
26+
<!-- App Identifier -->
27+
<ApplicationId>com.companyname.catclock</ApplicationId>
28+
29+
<!-- Versions -->
30+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
31+
<ApplicationVersion>1</ApplicationVersion>
32+
33+
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
34+
<WindowsPackageType>None</WindowsPackageType>
35+
36+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
37+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
38+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
39+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
40+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
41+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<!-- App Icon -->
46+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
47+
48+
<!-- Splash Screen -->
49+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
50+
51+
<!-- Images -->
52+
<MauiImage Include="Resources\Images\*" />
53+
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
54+
55+
<!-- Custom Fonts -->
56+
<MauiFont Include="Resources\Fonts\*" />
57+
58+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
59+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
60+
</ItemGroup>
61+
62+
<ItemGroup>
63+
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
64+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
65+
<PackageReference Include="SkiaSharp.Views.Maui.Controls" Version="3.116.1" />
66+
</ItemGroup>
67+
68+
<ItemGroup>
69+
<EmbeddedResource Include="Media\woodgrain.png" />
70+
</ItemGroup>
71+
72+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:skia="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
5+
x:Class="CatClock.MainPage"
6+
Title="Cat Clock">
7+
<skia:SKCanvasView x:Name="canvasView"
8+
PaintSurface="OnCanvasViewPaintSurface" />
9+
</ContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
using SkiaSharp;
2+
using SkiaSharp.Views.Maui;
3+
4+
namespace CatClock
5+
{
6+
public partial class MainPage : ContentPage
7+
{
8+
static readonly SKBitmap bitmap =
9+
BitmapExtensions.LoadBitmapResource(typeof(MainPage), "CatClock.Media.woodgrain.png");
10+
11+
SKPaint blackFillPaint = new SKPaint
12+
{
13+
Style = SKPaintStyle.Fill,
14+
Color = SKColors.Black
15+
};
16+
17+
SKPaint whiteStrokePaint = new SKPaint
18+
{
19+
Style = SKPaintStyle.Stroke,
20+
Color = SKColors.White,
21+
StrokeWidth = 2,
22+
StrokeCap = SKStrokeCap.Round,
23+
IsAntialias = true
24+
};
25+
26+
SKPaint whiteFillPaint = new SKPaint
27+
{
28+
Style = SKPaintStyle.Fill,
29+
Color = SKColors.White
30+
};
31+
32+
SKPaint greenFillPaint = new SKPaint
33+
{
34+
Style = SKPaintStyle.Fill,
35+
Color = SKColors.PaleGreen
36+
};
37+
38+
SKPaint blackStrokePaint = new SKPaint
39+
{
40+
Style = SKPaintStyle.Stroke,
41+
Color = SKColors.Black,
42+
StrokeWidth = 20,
43+
StrokeCap = SKStrokeCap.Round
44+
};
45+
46+
SKPaint grayFillPaint = new SKPaint
47+
{
48+
Style = SKPaintStyle.Fill,
49+
Color = SKColors.Gray
50+
};
51+
52+
SKPaint backgroundFillPaint = new SKPaint
53+
{
54+
Style = SKPaintStyle.Fill
55+
};
56+
57+
SKPath catEarPath = new SKPath();
58+
SKPath catEyePath = new SKPath();
59+
SKPath catPupilPath = new SKPath();
60+
SKPath catTailPath = new SKPath();
61+
62+
SKPath hourHandPath = SKPath.ParseSvgPathData(
63+
"M 0 -60 C 0 -30 20 -30 5 -20 L 5 0 C 5 7.5 -5 7.5 -5 0 L -5 -20 C -20 -30 0 -30 0 -60");
64+
SKPath minuteHandPath = SKPath.ParseSvgPathData(
65+
"M 0 -80 C 0 -75 0 -70 2.5 -60 L 2.5 0 C 2.5 5 -2.5 5 -2.5 0 L -2.5 -60 C 0 -70 0 -75 0 -80");
66+
67+
public MainPage()
68+
{
69+
InitializeComponent();
70+
71+
// Make cat ear path
72+
catEarPath.MoveTo(0, 0);
73+
catEarPath.LineTo(0, 75);
74+
catEarPath.LineTo(100, 75);
75+
catEarPath.Close();
76+
77+
// Make cat eye path
78+
catEyePath.MoveTo(0, 0);
79+
catEyePath.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 50, 0);
80+
catEyePath.ArcTo(50, 50, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 0, 0);
81+
catEyePath.Close();
82+
83+
// Make eye pupil path
84+
catPupilPath.MoveTo(25, -5);
85+
catPupilPath.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, 5);
86+
catPupilPath.ArcTo(6, 6, 0, SKPathArcSize.Small, SKPathDirection.Clockwise, 25, -5);
87+
catPupilPath.Close();
88+
89+
// Make cat tail path
90+
catTailPath.MoveTo(0, 0);
91+
catTailPath.CubicTo(50, 200, 0, 250, -50, 200);
92+
93+
// Create shader
94+
using (SKShader shader = SKShader.CreateBitmap(bitmap, SKShaderTileMode.Mirror, SKShaderTileMode.Mirror))
95+
{
96+
backgroundFillPaint.Shader = shader;
97+
}
98+
99+
Dispatcher.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
100+
{
101+
canvasView.InvalidateSurface();
102+
return true;
103+
});
104+
}
105+
106+
private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs e)
107+
{
108+
SKSurface surface = e.Surface;
109+
SKCanvas canvas = surface.Canvas;
110+
111+
canvas.DrawPaint(backgroundFillPaint);
112+
113+
int width = e.Info.Width;
114+
int height = e.Info.Height;
115+
116+
// Set transforms
117+
canvas.Translate(width / 2, height / 2);
118+
canvas.Scale(Math.Min(width / 210f, height / 520f));
119+
120+
DateTime dateTime = DateTime.Now;
121+
122+
// Head
123+
canvas.DrawCircle(0, -160, 75, blackFillPaint);
124+
125+
// Ears and eyes
126+
for (int i = 0; i < 2; i++)
127+
{
128+
canvas.Save();
129+
canvas.Scale(2 * i - 1, 1);
130+
131+
canvas.Save();
132+
canvas.Translate(-65, -255);
133+
canvas.DrawPath(catEarPath, blackFillPaint);
134+
canvas.Restore();
135+
136+
canvas.Save();
137+
canvas.Translate(10, -170);
138+
canvas.DrawPath(catEyePath, greenFillPaint);
139+
canvas.DrawPath(catPupilPath, blackFillPaint);
140+
canvas.Restore();
141+
142+
// Whiskers
143+
canvas.DrawLine(10, -120, 100, -100, whiteStrokePaint);
144+
canvas.DrawLine(10, -125, 100, -120, whiteStrokePaint);
145+
canvas.DrawLine(10, -130, 100, -140, whiteStrokePaint);
146+
canvas.DrawLine(10, -135, 100, -160, whiteStrokePaint);
147+
148+
canvas.Restore();
149+
}
150+
151+
// Move tail
152+
float t = (float)Math.Sin((dateTime.Second % 2 + dateTime.Millisecond / 1000.0) * Math.PI);
153+
catTailPath.Reset();
154+
catTailPath.MoveTo(0, 100);
155+
SKPoint point1 = new SKPoint(-50 * t, 200);
156+
SKPoint point2 = new SKPoint(0, 250 - Math.Abs(50 * t));
157+
SKPoint point3 = new SKPoint(50 * 5, 250 - Math.Abs(75 * t));
158+
catTailPath.CubicTo(point1, point2, point3);
159+
canvas.DrawPath(catTailPath, blackStrokePaint);
160+
161+
// Clock backgroud
162+
canvas.DrawCircle(0, 0, 100, blackFillPaint);
163+
164+
// Hour and minute marks
165+
for (int angle = 0; angle < 360; angle +=6)
166+
{
167+
canvas.DrawCircle(0, -90, angle % 30 == 0 ? 4 : 2, whiteFillPaint);
168+
canvas.RotateDegrees(6);
169+
}
170+
171+
// Hour hand
172+
canvas.Save();
173+
canvas.RotateDegrees(30 * dateTime.Hour + dateTime.Minute / 2f);
174+
canvas.DrawPath(hourHandPath, grayFillPaint);
175+
canvas.DrawPath(hourHandPath, whiteStrokePaint);
176+
canvas.Restore();
177+
178+
// Minute hand
179+
canvas.Save();
180+
canvas.RotateDegrees(6 * dateTime.Minute + dateTime.Second / 10f);
181+
canvas.DrawPath(minuteHandPath, grayFillPaint);
182+
canvas.DrawPath(minuteHandPath, whiteStrokePaint);
183+
canvas.Restore();
184+
185+
// Second hand
186+
canvas.Save();
187+
float seconds = dateTime.Second + dateTime.Millisecond / 1000f;
188+
canvas.RotateDegrees(6 * seconds);
189+
whiteStrokePaint.StrokeWidth = 2;
190+
canvas.DrawLine(0, 10, 0, -80, whiteStrokePaint);
191+
canvas.Restore();
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)