The samples can now be scaled and panned. Closes #173

This commit is contained in:
Matthew Leibowitz 2016-11-11 06:48:59 +02:00
Родитель 2926f3391f
Коммит e1ca4c05ae
3 изменённых файлов: 91 добавлений и 0 удалений

Просмотреть файл

@ -7,11 +7,15 @@
<views:SKCanvasView x:Name="canvas" PaintSurface="OnPaintSample">
<views:SKCanvasView.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapSample" />
<PanGestureRecognizer PanUpdated="OnPanSample" />
<PinchGestureRecognizer PinchUpdated="OnPinchSample" />
</views:SKCanvasView.GestureRecognizers>
</views:SKCanvasView>
<views:SKGLView x:Name="glview" PaintSurface="OnPaintGLSample" IsVisible="False" HasRenderLoop="False">
<views:SKGLView.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapSample" />
<PanGestureRecognizer PanUpdated="OnPanSample" />
<PinchGestureRecognizer PinchUpdated="OnPinchSample" />
</views:SKGLView.GestureRecognizers>
</views:SKGLView>
</Grid>

Просмотреть файл

@ -58,6 +58,31 @@ namespace SkiaSharpSample.FormsSample
Sample?.Tap();
}
private void OnPanSample(object sender, PanUpdatedEventArgs e)
{
var scale = canvas.CanvasSize.Width / (float)canvas.Width;
if (glview.IsVisible)
scale = glview.CanvasSize.Width / (float)glview.Width;
Sample?.Pan(
(GestureState)(int)e.StatusType,
new SKPoint((float)e.TotalX * scale, (float)e.TotalY * scale));
RefreshSamples();
}
private void OnPinchSample(object sender, PinchGestureUpdatedEventArgs e)
{
var size = canvas.CanvasSize;
if (glview.IsVisible)
size = glview.CanvasSize;
Sample?.Pinch(
(GestureState)(int)e.Status,
(float)e.Scale,
new SKPoint((float)e.ScaleOrigin.X * size.Width, (float)e.ScaleOrigin.Y * size.Height));
RefreshSamples();
}
private void OnPaintSample(object sender, SKPaintSurfaceEventArgs e)
{
Sample?.DrawSample(e.Surface.Canvas, e.Info.Width, e.Info.Height);

Просмотреть файл

@ -6,6 +6,13 @@ namespace SkiaSharpSample
{
public abstract class SampleBase
{
protected SKMatrix Matrix = SKMatrix.MakeIdentity();
private SKMatrix startPanMatrix = SKMatrix.MakeIdentity();
private SKMatrix startPinchMatrix = SKMatrix.MakeIdentity();
private SKPoint startPinchOrigin = SKPoint.Empty;
private float totalPinchScale = 1f;
public abstract string Title { get; }
public virtual string Description { get; } = string.Empty;
@ -22,6 +29,7 @@ namespace SkiaSharpSample
{
if (IsInitialized)
{
canvas.SetMatrix(Matrix);
OnDrawSample(canvas, width, height);
}
}
@ -30,6 +38,9 @@ namespace SkiaSharpSample
public async void Init(Action callback = null)
{
// reset the matrix for the new sample
Matrix = SKMatrix.MakeIdentity();
if (!IsInitialized)
{
await OnInit();
@ -57,6 +68,49 @@ namespace SkiaSharpSample
{
}
public void Pan(GestureState state, SKPoint translation)
{
switch (state)
{
case GestureState.Started:
startPanMatrix = Matrix;
break;
case GestureState.Running:
var canvasTranslation = SKMatrix.MakeTranslation(translation.X, translation.Y);
SKMatrix.Concat(ref Matrix, ref canvasTranslation, ref startPanMatrix);
break;
default:
startPanMatrix = SKMatrix.MakeIdentity();
break;
}
}
public void Pinch(GestureState state, float scale, SKPoint origin)
{
switch (state)
{
case GestureState.Started:
startPinchMatrix = Matrix;
startPinchOrigin = origin;
totalPinchScale = 1f;
break;
case GestureState.Running:
totalPinchScale *= scale;
var pinchTranslation = origin - startPinchOrigin;
var canvasTranslation = SKMatrix.MakeTranslation(pinchTranslation.X, pinchTranslation.Y);
var canvasScaling = SKMatrix.MakeScale(totalPinchScale, totalPinchScale, origin.X, origin.Y);
var canvasCombined = SKMatrix.MakeIdentity();
SKMatrix.Concat(ref canvasCombined, ref canvasScaling, ref canvasTranslation);
SKMatrix.Concat(ref Matrix, ref canvasCombined, ref startPinchMatrix);
break;
default:
startPinchMatrix = SKMatrix.MakeIdentity();
startPinchOrigin = SKPoint.Empty;
totalPinchScale = 1f;
break;
}
}
public virtual bool MatchesFilter(string searchText)
{
if (string.IsNullOrWhiteSpace(searchText))
@ -67,4 +121,12 @@ namespace SkiaSharpSample
Description.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) != -1;
}
}
public enum GestureState
{
Started,
Running,
Completed,
Canceled
}
}