Skip to content

Commit

Permalink
Adding more samples
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jun 20, 2020
1 parent 52bf59f commit ca1dbdd
Show file tree
Hide file tree
Showing 14 changed files with 851 additions and 113 deletions.
5 changes: 2 additions & 3 deletions CSharpMath.Uno.Example/CSharpMath.Uno.Example.Shared/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
x:Class="CSharpMath.Uno.Example.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CSharpMath.Uno.Example">

</Application>
xmlns:local="using:CSharpMath.Uno.Example"
RequestedTheme="Light" />
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,49 @@
<Compile Include="$(MSBuildThisFileDirectory)App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ClockPage.xaml.cs">
<DependentUpon>ClockPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)MoreExamples.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MoreExamplesPage.xaml.cs">
<DependentUpon>MoreExamplesPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)TextPage.xaml.cs">
<DependentUpon>TextPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)TryPage.xaml.cs">
<DependentUpon>TryPage.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)ClockPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)MainPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)MoreExamplesPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)TextPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)TryPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="$(MSBuildThisFileDirectory)Strings\en\Resources.resw" />
</ItemGroup>
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="$(MSBuildThisFileDirectory)Strings\en\Resources.resw" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Page
x:Class="CSharpMath.Uno.Example.Shared.ClockPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:CSharpMath.Uno.Example.Shared"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:skia="using:SkiaSharp.Views.UWP"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<skia:SKXamlCanvas
x:Name="canvasView"
VerticalAlignment="Stretch"
PaintSurface="CanvasView_PaintSurface" />
</Grid>
</Page>
126 changes: 126 additions & 0 deletions CSharpMath.Uno.Example/CSharpMath.Uno.Example.Shared/ClockPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using CSharpMath.SkiaSharp;
using SkiaSharp;
using SkiaSharp.Views.UWP;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using static System.Math;

namespace CSharpMath.Uno.Example.Shared {
public sealed partial class ClockPage : Page {
private readonly DispatcherTimer _timer;

public ClockPage() {
this.InitializeComponent();
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(200);
_timer.Tick += (s, e) => {
canvasView.Invalidate();
};
this.Loaded += ClockPage_Loaded;
this.Unloaded += ClockPage_Unloaded;
}

private void ClockPage_Loaded(object sender, RoutedEventArgs e) {
_timer.Start();
}

private void ClockPage_Unloaded(object sender, RoutedEventArgs e) {
_timer.Stop();
}

readonly SKPaint blackFillPaint = new SKPaint {
Style = SKPaintStyle.Fill,
Color = SKColors.Black
};
readonly SKPaint whiteFillPaint = new SKPaint {
Style = SKPaintStyle.Fill,
Color = SKColors.White
};
readonly SKPaint whiteStrokePaint = new SKPaint {
Style = SKPaintStyle.Stroke,
Color = SKColors.White,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};
readonly SKPaint redStrokePaint = new SKPaint {
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};
readonly string[] labels = {
// Four 4s make 1 to 12 using different operations
@"$\frac{44+4}{4}$",
@"$\frac{44}{44}$",
@"$\frac{4}{4}+\frac{4}{4}$",
@"$\frac{4+4+4}{4}$",
@"$4+\frac{4-4}{4}$",
@"$4+4^{4-4}$",
@"$4+\frac{4+4}{4}$",
@"$\frac{44}{4}-4$",
@"$\sqrt{4}^{4-\frac{4}{4}}$",
@"$\:\:(4-\frac{4}{4})^{\sqrt{4}}$",
@"$\frac{44-4}{4}$",
@"$\frac{4!}{\sqrt{4}}-\frac{4}{4}$"
};
private void CanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
var canvas = e.Surface.Canvas;
canvas.Clear(SKColors.CornflowerBlue);
canvas.Translate(e.Info.Width / 2, e.Info.Height / 2);
canvas.Scale(e.Info.Width / 210f);
canvas.DrawCircle(0, 0, 100, blackFillPaint);
var painter = new TextPainter { FontSize = 8, TextColor = SKColors.White };
for (int i = 0; i < 60; i++) {
// Dots
canvas.Save();
canvas.RotateDegrees(6 * i);
canvas.DrawCircle(0, -90, i % 5 == 0 ? 4 : 2, whiteFillPaint);
canvas.Restore();
// Maths
if (i % 5 == 0) {
painter.LaTeX = labels[i / 5];
if (!(painter.Measure(e.Info.Width) is { } measure))
throw new Structures.InvalidCodePathException("Invalid LaTeX");
var θ = (90 - 6 * i) / 180f * PI;
var sinθ = (float)Sin(θ);
var cosθ = (float)Cos(θ);
painter.Draw(canvas,
new System.Drawing.PointF(75 * cosθ - (float)measure.Width / 2,
-75 * sinθ - (float)measure.Height / 2),
float.PositiveInfinity);
}
}
var dateTime = DateTime.Now;
// H
canvas.Save();
canvas.RotateDegrees(30 * dateTime.Hour + dateTime.Minute / 2f);
whiteStrokePaint.StrokeWidth = 12;
canvas.DrawLine(0, 0, 0, -50, whiteStrokePaint);
canvas.Restore();
// M
canvas.Save();
canvas.RotateDegrees(6 * dateTime.Minute + dateTime.Second / 10f);
whiteStrokePaint.StrokeWidth = 6;
canvas.DrawLine(0, 0, 0, -65, whiteStrokePaint);
canvas.Restore();
// S
canvas.Save();
canvas.RotateDegrees(6f * (dateTime.Second + dateTime.Millisecond / 1000f));
redStrokePaint.StrokeWidth = 2;
canvas.DrawLine(0, 0, 0, -75, redStrokePaint);
canvas.Restore();
}
}
}
16 changes: 12 additions & 4 deletions CSharpMath.Uno.Example/CSharpMath.Uno.Example.Shared/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:CSharpMath.Uno.Example"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:shared="using:CSharpMath.Uno.Example.Shared"
xmlns:skia="using:SkiaSharp.Views.UWP"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<skia:SKXamlCanvas
x:Name="canvasView"
VerticalAlignment="Stretch"
PaintSurface="CanvasView_PaintSurface" />
<Pivot>
<PivotItem Header="Try">
<shared:TryPage />
</PivotItem>
<PivotItem Header="More examples">
<shared:MoreExamplesPage />
</PivotItem>
<PivotItem Header="Clock">
<shared:ClockPage />
</PivotItem>
</Pivot>
</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -24,103 +24,8 @@ namespace CSharpMath.Uno.Example {
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page {
private readonly DispatcherTimer _timer;

public MainPage() {
this.InitializeComponent();
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(200);
_timer.Tick += (s, e) => {
canvasView.Invalidate();
};
}

protected override void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
_timer.Start();
}

readonly SKPaint blackFillPaint = new SKPaint {
Style = SKPaintStyle.Fill,
Color = SKColors.Black
};
readonly SKPaint whiteFillPaint = new SKPaint {
Style = SKPaintStyle.Fill,
Color = SKColors.White
};
readonly SKPaint whiteStrokePaint = new SKPaint {
Style = SKPaintStyle.Stroke,
Color = SKColors.White,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};
readonly SKPaint redStrokePaint = new SKPaint {
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
StrokeCap = SKStrokeCap.Round,
IsAntialias = true
};
readonly string[] labels = {
// Four 4s make 1 to 12 using different operations
@"$\frac{44+4}{4}$",
@"$\frac{44}{44}$",
@"$\frac{4}{4}+\frac{4}{4}$",
@"$\frac{4+4+4}{4}$",
@"$4+\frac{4-4}{4}$",
@"$4+4^{4-4}$",
@"$4+\frac{4+4}{4}$",
@"$\frac{44}{4}-4$",
@"$\sqrt{4}^{4-\frac{4}{4}}$",
@"$\:\:(4-\frac{4}{4})^{\sqrt{4}}$",
@"$\frac{44-4}{4}$",
@"$\frac{4!}{\sqrt{4}}-\frac{4}{4}$"
};
private void CanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e) {
var canvas = e.Surface.Canvas;
canvas.Clear(SKColors.CornflowerBlue);
canvas.Translate(e.Info.Width / 2, e.Info.Height / 2);
canvas.Scale(e.Info.Width / 210f);
canvas.DrawCircle(0, 0, 100, blackFillPaint);
var painter = new TextPainter { FontSize = 8, TextColor = SKColors.White };
for (int i = 0; i < 60; i++) {
// Dots
canvas.Save();
canvas.RotateDegrees(6 * i);
canvas.DrawCircle(0, -90, i % 5 == 0 ? 4 : 2, whiteFillPaint);
canvas.Restore();
// Maths
if (i % 5 == 0) {
painter.LaTeX = labels[i / 5];
if (!(painter.Measure(e.Info.Width) is { } measure))
throw new Structures.InvalidCodePathException("Invalid LaTeX");
var θ = (90 - 6 * i) / 180f * PI;
var sinθ = (float)Sin(θ);
var cosθ = (float)Cos(θ);
painter.Draw(canvas,
new System.Drawing.PointF(75 * cosθ - (float)measure.Width / 2,
-75 * sinθ - (float)measure.Height / 2),
float.PositiveInfinity);
}
}
var dateTime = DateTime.Now;
// H
canvas.Save();
canvas.RotateDegrees(30 * dateTime.Hour + dateTime.Minute / 2f);
whiteStrokePaint.StrokeWidth = 12;
canvas.DrawLine(0, 0, 0, -50, whiteStrokePaint);
canvas.Restore();
// M
canvas.Save();
canvas.RotateDegrees(6 * dateTime.Minute + dateTime.Second / 10f);
whiteStrokePaint.StrokeWidth = 6;
canvas.DrawLine(0, 0, 0, -65, whiteStrokePaint);
canvas.Restore();
// S
canvas.Save();
canvas.RotateDegrees(6f * (dateTime.Second + dateTime.Millisecond / 1000f));
redStrokePaint.StrokeWidth = 2;
canvas.DrawLine(0, 0, 0, -75, redStrokePaint);
canvas.Restore();
this.InitializeComponent();
}
}
}
Loading

0 comments on commit ca1dbdd

Please sign in to comment.