|
| 1 | +using Avalonia; |
| 2 | +using Avalonia.Media; |
| 3 | +using Avalonia.Platform; |
| 4 | +using Avalonia.Rendering.SceneGraph; |
| 5 | +using Avalonia.Skia; |
| 6 | +using SkiaSharp; |
| 7 | +using System; |
| 8 | + |
| 9 | +namespace MystIVAssetExplorer.Skybox; |
| 10 | + |
| 11 | +partial class SkyboxControl |
| 12 | +{ |
| 13 | + private sealed class CheckerboardDrawOperation : ICustomDrawOperation |
| 14 | + { |
| 15 | + private readonly SkyboxControl owner; |
| 16 | + private readonly SKBitmap checkerboardTileBitmap; |
| 17 | + private readonly SKShader checkerboardShader; |
| 18 | + private readonly SKPaint checkerboardPaint; |
| 19 | + |
| 20 | + public CheckerboardDrawOperation(SkyboxControl owner) |
| 21 | + { |
| 22 | + this.owner = owner; |
| 23 | + |
| 24 | + checkerboardTileBitmap = new SKBitmap(2, 2); |
| 25 | + checkerboardTileBitmap.SetPixel(0, 0, new SKColor(0x55, 0x55, 0x55)); |
| 26 | + checkerboardTileBitmap.SetPixel(1, 0, new SKColor(0x33, 0x33, 0x33)); |
| 27 | + checkerboardTileBitmap.SetPixel(0, 1, new SKColor(0x33, 0x33, 0x33)); |
| 28 | + checkerboardTileBitmap.SetPixel(1, 1, new SKColor(0x55, 0x55, 0x55)); |
| 29 | + |
| 30 | + checkerboardShader = SKShader.CreateBitmap(checkerboardTileBitmap, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, SKMatrix.CreateScale(8, 8)); |
| 31 | + checkerboardPaint = new SKPaint { Shader = checkerboardShader }; |
| 32 | + } |
| 33 | + |
| 34 | + public Rect Bounds => new(owner.Bounds.Size); |
| 35 | + |
| 36 | + public void Render(ImmediateDrawingContext context) |
| 37 | + { |
| 38 | + using var lease = context.TryGetFeature<ISkiaSharpApiLeaseFeature>()!.Lease(); |
| 39 | + var canvas = lease.SkCanvas; |
| 40 | + |
| 41 | + canvas.DrawRect(new SKRect(0, 0, (float)Bounds.Width, (float)Bounds.Height), checkerboardPaint); |
| 42 | + } |
| 43 | + |
| 44 | + public void Dispose() |
| 45 | + { |
| 46 | + checkerboardPaint.Dispose(); |
| 47 | + checkerboardShader.Dispose(); |
| 48 | + checkerboardTileBitmap.Dispose(); |
| 49 | + } |
| 50 | + |
| 51 | + bool IEquatable<ICustomDrawOperation>.Equals(ICustomDrawOperation? other) => other == this; |
| 52 | + |
| 53 | + bool ICustomDrawOperation.HitTest(Point p) => Bounds.Contains(p); |
| 54 | + } |
| 55 | +} |
0 commit comments