Skip to content

Commit 745a5cd

Browse files
Transparent background fix (#213)
1 parent fe69f06 commit 745a5cd

File tree

7 files changed

+45
-4
lines changed

7 files changed

+45
-4
lines changed

Pixed.Application/Controls/ImageGrid.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public ImageGrid()
1010
{
1111
ClipToBounds = false;
1212
}
13-
public double Zoom { get; set; } = 1;
1413
public double GridWidth { get; set; }
1514
public double GridHeight { get; set; }
1615
public bool GridEnabled { get; set; } = false;

Pixed.Application/Controls/OverlayControl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public void Render(ImmediateDrawingContext context)
4141
}
4242
}
4343

44+
public virtual double Zoom { get; set; } = 1;
45+
4446
public override void Render(DrawingContext context)
4547
{
4648
context.Custom(new DrawOperation(new Rect(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height), this));

Pixed.Application/Controls/PaintCanvas.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
PointerPressed="Canvas_PointerPressed"
3333
PointerReleased="Canvas_PointerReleased"
3434
PointerWheelChanged="Canvas_PointerWheelChanged"
35-
RenderOptions.BitmapInterpolationMode="None"
36-
Background="{Binding TransparentBrush}">
35+
RenderOptions.BitmapInterpolationMode="None">
36+
<controls:TransparentBackground IsHitTestVisible="False" Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="transparentBackground"/>
3737
<controls:PixelImageControl Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding RenderModel}"/>
3838
<controls:ImageGrid Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
3939
<controls:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>

Pixed.Application/Controls/PaintCanvas.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public PaintCanvas() : base()
2222
zoomBorder.GestureZoomEnabled = false;
2323
ViewModel.GridCanvas = gridCanvas;
2424
ViewModel.SelectionOverlay = selectionOverlay;
25+
ViewModel.TransparentBackground = transparentBackground;
2526
ViewModel.ZoomValue = zoomBorder.Zoom;
2627
ViewModel.ZoomOffsetX = zoomBorder.OffsetX;
2728
ViewModel.ZoomOffsetY = zoomBorder.OffsetY;

Pixed.Application/Controls/SelectionOverlay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class SelectionOverlay : OverlayControl
1616
private bool _drawLines = false;
1717
private float[] _pattern = [2f, 2f];
1818

19-
public double Zoom
19+
public override double Zoom
2020
{
2121
get => _zoom;
2222
set
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Pixed.Application.Utils;
2+
using Pixed.Core;
3+
using SkiaSharp;
4+
5+
namespace Pixed.Application.Controls;
6+
internal class TransparentBackground : OverlayControl
7+
{
8+
public TransparentBackground()
9+
{
10+
ClipToBounds = true;
11+
}
12+
public override void Render(SKCanvas canvas)
13+
{
14+
SKPaint paint1 = new() { Color = new UniColor(76, 76, 76), Style = SKPaintStyle.Fill };
15+
SKPaint paint2 = new() { Color = new UniColor(85, 85, 85), Style = SKPaintStyle.Fill };
16+
17+
float right = Bounds.Right.ToFloat();
18+
float bottom = Bounds.Bottom.ToFloat();
19+
int xCount = ((int)right / 32)+1;
20+
int yCount = ((int)bottom / 32)+1;
21+
float size = 32f / Zoom.ToFloat();
22+
23+
for(int x = 0; x < xCount; x++)
24+
{
25+
for(int y = 0; y < yCount; y++)
26+
{
27+
int c = (x + y) % 2;
28+
canvas.DrawRect(x.ToFloat() * size, y.ToFloat() * size, size, size, c == 0 ? paint1 : paint2);
29+
}
30+
}
31+
}
32+
}

Pixed.Application/ViewModels/PaintCanvasViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public double ZoomValue
176176
public ZoomBorder ZoomContainer { get; set; }
177177
public ImageGrid GridCanvas { get; set; }
178178
public SelectionOverlay SelectionOverlay { get; set; }
179+
public TransparentBackground TransparentBackground { get; set; }
179180
public ImageBrush TransparentBrush
180181
{
181182
get => _transparentBrush;
@@ -306,6 +307,12 @@ public PaintCanvasViewModel(ApplicationData applicationData, ToolsManager toolSe
306307
{
307308
SelectionOverlay.Zoom = entry.Zoom;
308309
}
310+
311+
if(TransparentBackground != null)
312+
{
313+
TransparentBackground.Zoom = entry.Zoom;
314+
}
315+
309316
RefreshZoomText();
310317
TransparentBrush = GetTransparentBackgroundBrush();
311318
});

0 commit comments

Comments
 (0)