Skip to content

Added a SkiaSharp.Extended.Controls project #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions SkiaSharp.Extended.sln
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Extended.Tests",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "deprectated", "deprectated", "{7BB41E64-A9E4-4675-8CE6-98360BF6AB0C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Extended.Controls", "source\SkiaSharp.Extended.Controls\SkiaSharp.Extended.Controls.csproj", "{E2E70F07-6401-469B-8979-839D78B6F50E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -537,6 +539,30 @@ Global
{B5A95CCE-FF80-4ACA-AA49-F150C23C65D5}.Release|x64.Build.0 = Release|Any CPU
{B5A95CCE-FF80-4ACA-AA49-F150C23C65D5}.Release|x86.ActiveCfg = Release|Any CPU
{B5A95CCE-FF80-4ACA-AA49-F150C23C65D5}.Release|x86.Build.0 = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|ARM.ActiveCfg = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|ARM.Build.0 = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|iPhone.Build.0 = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|x64.ActiveCfg = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|x64.Build.0 = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|x86.ActiveCfg = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Debug|x86.Build.0 = Debug|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|Any CPU.Build.0 = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|ARM.ActiveCfg = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|ARM.Build.0 = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|iPhone.ActiveCfg = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|iPhone.Build.0 = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|x64.ActiveCfg = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|x64.Build.0 = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|x86.ActiveCfg = Release|Any CPU
{E2E70F07-6401-469B-8979-839D78B6F50E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
27 changes: 27 additions & 0 deletions samples/SkiaSharpDemo/Demos/DynamicSurfacePage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:controls="clr-namespace:SkiaSharp.Extended.Controls;assembly=SkiaSharp.Extended.Controls"
mc:Ignorable="d"
x:Class="SkiaSharpDemo.Demos.DynamicSurfacePage"
Title="Dynamic Surface">

<Grid>

<controls:SKDynamicSurfaceView x:Name="dynamicSurface"
PaintSurface="OnPainting"
IsHardwareAccelerated="{Binding UseHardware}"
EnableTouchEvents="True"
Touch="OnTouching" />

<StackLayout Padding="10" Margin="0" Spacing="5"
HorizontalOptions="Start" VerticalOptions="Start" Orientation="Horizontal"
BackgroundColor="#CCFFFFFF">
<Label Text="Use Hardware Acceleration:" VerticalOptions="Center" />
<Switch IsToggled="{Binding UseHardware}" VerticalOptions="Center" />
</StackLayout>

</Grid>

</ContentPage>
148 changes: 148 additions & 0 deletions samples/SkiaSharpDemo/Demos/DynamicSurfacePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using SkiaSharp;
using SkiaSharp.Extended.Controls;
using SkiaSharp.Views.Forms;
using Xamarin.Forms;

namespace SkiaSharpDemo.Demos
{
public partial class DynamicSurfacePage : ContentPage
{
private bool useHardware;
private float elevation;
private bool isCurrent;
private SK3dView rotationView = new SK3dView();
private Dictionary<long, SKPath> points = new Dictionary<long, SKPath>();

public DynamicSurfacePage()
{
InitializeComponent();

BindingContext = this;
}

protected override void OnAppearing()
{
base.OnAppearing();

isCurrent = true;

rotationView.RotateYDegrees(30);

// set up a nice render loop
Device.StartTimer(TimeSpan.FromSeconds(1.0 / 60.0), () =>
{
rotationView.RotateYDegrees(1);
elevation += 0.05f;

dynamicSurface?.InvalidateSurface();
return isCurrent;
});
}

protected override void OnDisappearing()
{
isCurrent = false;

base.OnDisappearing();
}

public bool UseHardware
{
get => useHardware;
set
{
useHardware = value;
OnPropertyChanged();
}
}

private void OnPainting(object sender, SKPaintDynamicSurfaceEventArgs e)
{
var canvas = e.Surface.Canvas;
var width = e.Info.Width;
var height = e.Info.Height;
var sine = (float)Math.Abs(Math.Sin(elevation)) * 100;

// get the 2D equivalent of the 3D matrix
var rotationMatrix = rotationView.Matrix;

// get the properties of the rectangle
var length = Math.Min(width / 6, height / 6);
var rect = new SKRect(-length, -length - sine, length, length - sine);
var side = rotationMatrix.MapPoint(new SKPoint(1, 0)).X > 0;

canvas.Clear(SKColors.CornflowerBlue);

// first do 2D translation to the center of the screen
canvas.Translate(width / 2, height / 2);

// then apply the 3D rotation
canvas.Concat(ref rotationMatrix);

// draw main block
var paint = new SKPaint
{
Color = side ? SKColors.Purple : SKColors.Green,
Style = SKPaintStyle.Fill,
IsAntialias = true
};
canvas.DrawRoundRect(rect, 30, 30, paint);

// draw shadow
var shadow = SKShader.CreateLinearGradient(
new SKPoint(0, 0), new SKPoint(0, length * 2),
new[] { paint.Color.WithAlpha(127), paint.Color.WithAlpha(0) },
null,
SKShaderTileMode.Clamp);
paint = new SKPaint
{
Shader = shadow,
Style = SKPaintStyle.Fill,
IsAntialias = true
};
rect.Offset(0, length * 2 + 5 + sine + sine);
canvas.DrawRoundRect(rect, 30, 30, paint);

// prepare to draw the touch lines
canvas.ResetMatrix();

// draw the lines
paint = new SKPaint
{
Color = SKColors.Orange,
Style = SKPaintStyle.Stroke,
IsAntialias = true,
StrokeCap = SKStrokeCap.Round,
StrokeJoin = SKStrokeJoin.Round,
StrokeWidth = 20
};
foreach (var line in points)
{
canvas.DrawPath(line.Value, paint);
}
}

private void OnTouching(object sender, SKTouchEventArgs e)
{
switch (e.ActionType)
{
case SKTouchAction.Pressed:
points[e.Id] = new SKPath();
points[e.Id].MoveTo(e.Location);
break;
case SKTouchAction.Moved:
if (points.TryGetValue(e.Id, out var path))
path.LineTo(e.Location);
break;
case SKTouchAction.Released:
case SKTouchAction.Cancelled:
points.Remove(e.Id);
break;
}

e.Handled = true;
}
}
}
23 changes: 23 additions & 0 deletions samples/SkiaSharpDemo/Demos/GestureSurfacePage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:controls="clr-namespace:SkiaSharp.Extended.Controls;assembly=SkiaSharp.Extended.Controls"
mc:Ignorable="d"
x:Class="SkiaSharpDemo.Demos.GestureSurfacePage"
Title="Gesture Surface">
<Grid>

<controls:SKGestureSurfaceView x:Name="gestureSurface"
PaintSurface="OnPainting"
IsHardwareAccelerated="{Binding UseHardware}" />

<StackLayout Padding="10" Margin="0" Spacing="5"
HorizontalOptions="Start" VerticalOptions="Start" Orientation="Horizontal"
BackgroundColor="#CCFFFFFF">
<Label Text="Use Hardware Acceleration:" VerticalOptions="Center" />
<Switch IsToggled="{Binding UseHardware}" VerticalOptions="Center" />
</StackLayout>

</Grid>
</ContentPage>
Loading