Skip to content

Commit

Permalink
update to Avalonia v11 the custom rendering operations and the contro…
Browse files Browse the repository at this point in the history
…l that depends of these
  • Loading branch information
PieroCastillo committed Jan 25, 2024
1 parent 94054a2 commit 194b3e9
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 260 deletions.
2 changes: 1 addition & 1 deletion src/Aura.UI/Controls/Colouring/Arc/Arc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public double SweepAngle

public override void Render(DrawingContext context)
{
context.Custom(new ArcRender(new Rect(0, 0, Bounds.Width, Bounds.Height), null, Stroke, (float)StartAngle, (float)SweepAngle, ArcColor));
context.Custom(new ArcRender(new Rect(0, 0, Bounds.Width, Bounds.Height), Stroke, (float)StartAngle, (float)SweepAngle, ArcColor));
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aura.UI/Controls/Colouring/ColorSquare/ColorSquare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class ColorSquare : Control
{
public override void Render(DrawingContext context)
{
var render = new ColorSquareRender(Bounds, null, ColorToRender, StrokeColor, StrokeWidth);
var render = new ColorSquareRender(Bounds, ColorToRender, StrokeColor, StrokeWidth);
SquareRender = render;
context.Custom(render);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aura.UI/Controls/Colouring/ColorWheel/ColorWheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public override void Render(DrawingContext context)
{
base.Render(context);

context.Custom(new ColorWheelRender(new Rect(Bounds.Size), null, StrokeWidth));
context.Custom(new ColorWheelRender(new Rect(Bounds.Size), StrokeWidth));
}

private float _strokewidth = 20;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static TriangleColorWheel()

public override void Render(DrawingContext context)
{
context.Custom(new TriangleWheelRender(Bounds, Color, null, (float)StrokeWidth));
context.Custom(new TriangleWheelRender(Bounds, Color, (float)StrokeWidth));
base.Render(context);
}

Expand Down
51 changes: 25 additions & 26 deletions src/Aura.UI/Rendering/ArcRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ namespace Aura.UI.Rendering
public class ArcRender : AuraDrawOperationBase
{
public ArcRender(Rect bounds,
IFormattedTextImpl textImpl,
int stroke_w, float angle1,
float angle2,
Color strokeColor) : base(bounds, textImpl)
Color strokeColor) : base(bounds)
{
stroke = stroke_w;
StrokeColor = strokeColor;
Expand All @@ -26,36 +25,36 @@ public ArcRender(Rect bounds,
private float _angle1;
private float _angle2;

public override void Render(IDrawingContextImpl drw_context)
public override void Render(ImmediateDrawingContext drwContext)
{
if (drw_context is I ISkiaDrawingContextImpl context)
{
var canvas = context.SkCanvas;

canvas.Save();
var leaseFeature = drwContext.TryGetFeature<ISkiaSharpApiLeaseFeature>();
if (leaseFeature == null)
return;

var info = new SKImageInfo((int)Bounds.Width, (int)Bounds.Height);
var s_r = stroke / 2;
SKRect rect = new SKRect(s_r, s_r, info.Height - s_r, info.Width - s_r);
using var lease = leaseFeature.Lease();
var canvas = lease.SkCanvas;

using (var paint = new SKPaint())
{
paint.Shader = SKShader.CreateColor(StrokeColor.ToSKColor());
paint.Style = SKPaintStyle.Stroke;
paint.StrokeWidth = stroke;
paint.IsAntialias = true;
paint.Color = StrokeColor.ToSKColor();
paint.StrokeCap = SKStrokeCap.Round;
canvas.DrawArc(rect, _angle1, _angle2, false, paint);
}
canvas.Save();

canvas.Restore();
var info = new SKImageInfo((int)Bounds.Width, (int)Bounds.Height);
var s_r = stroke / 2;
SKRect rect = new SKRect(s_r, s_r, info.Height - s_r, info.Width - s_r);

Debug.WriteLine(StrokeColor.ToString());
Debug.WriteLine("Arc rendered");
using (var paint = new SKPaint())
{
paint.Shader = SKShader.CreateColor(StrokeColor.ToSKColor());
paint.Style = SKPaintStyle.Stroke;
paint.StrokeWidth = stroke;
paint.IsAntialias = true;
paint.Color = StrokeColor.ToSKColor();
paint.StrokeCap = SKStrokeCap.Round;
canvas.DrawArc(rect, _angle1, _angle2, false, paint);
}
}

public override bool HitTest(Point p) => true;
canvas.Restore();

Debug.WriteLine(StrokeColor.ToString());
Debug.WriteLine("Arc rendered");
}
}
}
20 changes: 3 additions & 17 deletions src/Aura.UI/Rendering/AuraDrawOperationBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering.SceneGraph;
using Avalonia.Skia;
Expand All @@ -7,38 +8,23 @@ namespace Aura.UI.Rendering
{
public abstract class AuraDrawOperationBase : ICustomDrawOperation
{
public AuraDrawOperationBase(Rect bounds, IFormattedTextImpl noSkia)
public AuraDrawOperationBase(Rect bounds)
{
Bounds = bounds;
NoSkia = noSkia;
}

public virtual Rect Bounds { get; private set; }
public virtual IFormattedTextImpl NoSkia { get; private set; }

public virtual void Dispose()
{
// do nothing
}

public virtual bool Equals(ICustomDrawOperation other) => false;

public virtual bool HitTest(Point p) => true;

public virtual void Render(IDrawingContextImpl context)
{
}

protected virtual bool CheckSkia(IDrawingContextImpl context)
public virtual void Render(ImmediateDrawingContext context)
{
if (context is ISkiaDrawingContextImpl)
{
return true;
}
else
{
return false;
}
}
}
}
42 changes: 21 additions & 21 deletions src/Aura.UI/Rendering/BlurImageRender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering.SceneGraph;
using Avalonia.Skia;
Expand All @@ -8,9 +9,9 @@

namespace Aura.UI.Rendering
{
public class BlurImageRender : AuraDrawOperationBase, ICustomDrawOperation
public class BlurImageRender : AuraDrawOperationBase
{
public BlurImageRender(MemoryStream data, Rect src, Rect dest, float levelX, float levelY, IFormattedTextImpl noSkia) : base(src, noSkia)
public BlurImageRender(MemoryStream data, Rect src, Rect dest, float levelX, float levelY) : base(src)
{
_data = data;
_levelx = levelX;
Expand All @@ -23,31 +24,30 @@ public BlurImageRender(MemoryStream data, Rect src, Rect dest, float levelX, flo
private float _levelx;
private float _levely;

public override void Render(IDrawingContextImpl drw_context)
public override void Render(ImmediateDrawingContext drwContext)
{
base.Render(drw_context);
var leaseFeature = drwContext.TryGetFeature<ISkiaSharpApiLeaseFeature>();
if (leaseFeature == null)
return;

if (drw_context is ISkiaDrawingContextImpl context)
{
var canvas = context.SkCanvas;

if (_data == null)
return;
using var lease = leaseFeature.Lease();
var canvas = lease.SkCanvas;

if (_data == null)
return;

using (var bitmap = SKBitmap.Decode(_data.ToArray()))
using (var paint = new SKPaint())
using (var bitmap = SKBitmap.Decode(_data.ToArray()))
using (var paint = new SKPaint())
{
if (bitmap == null)
{
if (bitmap == null)
{
Debug.WriteLine("bitmap is null");
return;
}
var img = SKImage.FromBitmap(bitmap);

paint.ImageFilter = SKImageFilter.CreateBlur(_levelx, _levely);
canvas.DrawImage(img, _dest.ToSKRect(), Bounds.ToSKRect(), paint);
Debug.WriteLine("bitmap is null");
return;
}
var img = SKImage.FromBitmap(bitmap);

paint.ImageFilter = SKImageFilter.CreateBlur(_levelx, _levely);
canvas.DrawImage(img, _dest.ToSKRect(), Bounds.ToSKRect(), paint);
}
}
}
Expand Down
88 changes: 0 additions & 88 deletions src/Aura.UI/Rendering/ColorHSVWheelRender.cs

This file was deleted.

Loading

0 comments on commit 194b3e9

Please sign in to comment.