Skip to content

Commit

Permalink
optimize winui GraphicsContainer paint event logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aesalazar committed Sep 18, 2024
1 parent 1dce55b commit 2ab9221
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
19 changes: 4 additions & 15 deletions Asteroids.WinUi3/Classes/GraphicsContainer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -14,9 +13,8 @@ namespace Asteroids.WinUi3.Classes
/// <summary>
/// Canvas Control to paint vectors.
/// </summary>
internal sealed class GraphicsContainer : SKXamlCanvas, IGraphicContainer, IDisposable
internal sealed class GraphicsContainer : SKXamlCanvas, IGraphicContainer
{
private bool _isDisposed;
private IDictionary<DrawColor, SKPaint> _colorCache = new Dictionary<DrawColor, SKPaint>();
private IEnumerable<IGraphicLine> _lastLines = [];
private IEnumerable<IGraphicPolygon> _lastPolygons = [];
Expand All @@ -30,30 +28,21 @@ public Task Initialize(IDictionary<DrawColor, string> drawColorMap)
)
);

PaintSurface += OnPaintSurface;
return Task.CompletedTask;
}

public Task Draw(IEnumerable<IGraphicLine> lines, IEnumerable<IGraphicPolygon> polygons)
{
if (_isDisposed)
return Task.CompletedTask;

_lastLines = lines;
_lastPolygons = polygons;

Invalidate();
return Task.CompletedTask;
}

public void Dispose()
{
PaintSurface -= OnPaintSurface;
_isDisposed = true;
}

private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
{
base.OnPaintSurface(e);
var canvas = e.Surface.Canvas;
canvas.Clear();

Expand Down
2 changes: 1 addition & 1 deletion Asteroids.WinUi3/Helpers/ColorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class ColorHelper
/// <returns>Hydrated <see cref="SKPaint"/> object.</returns>
public static SKPaint ColorHexToPaint(string colorHex)
{
var hex = colorHex.Replace("#", "");
var hex = colorHex.Replace("#", string.Empty);
var length = hex.Length;

var bytes = new byte[length / 2];
Expand Down

0 comments on commit 2ab9221

Please sign in to comment.