Skip to content

Overlay blinking fix #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Pixed.Application/Controls/PaintCanvas.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<Image Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding AvaloniaImageBitmap}"/>
<Image Name="overlay" RenderOptions.BitmapInterpolationMode="None" Source="{Binding AvaloniaOverlayBitmap}"/>
<controls:ImageGrid Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
<controls:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>
<controls:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False" IsVisible="{Binding OverlayVisible}"/>
</Canvas>
</zoom:ZoomBorder>
</Border>
Expand Down
75 changes: 30 additions & 45 deletions Pixed.Application/ViewModels/PaintCanvasViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal class PaintCanvasViewModel : PixedViewModel, IDisposable
private int _prevY = -1;
private bool _gestureZoomEnabled;
private double _zoomValue = 1;
private bool _overlayVisible = false;

private readonly IDisposable _projectModified;
private readonly IDisposable _projectChanged;
Expand Down Expand Up @@ -84,33 +85,32 @@ public int ToolSize
}
}

public SKBitmap Overlay
public PixedImage AvaloniaImageBitmap
{
get => _overlayBitmap;
get => _renderImage;
set
{
_overlayBitmap = value;
_renderImage = value;
OnPropertyChanged();
AvaloniaOverlayBitmap = new PixedImage(_overlayBitmap);
}
}

public PixedImage AvaloniaImageBitmap
public PixedImage AvaloniaOverlayBitmap
{
get => _renderImage;
get => _overlayImage;
set
{
_renderImage = value;
_overlayImage = value;
OnPropertyChanged();
}
}

public PixedImage AvaloniaOverlayBitmap
public bool OverlayVisible
{
get => _overlayImage;
get => _overlayVisible;
set
{
_overlayImage = value;
_overlayVisible = value;
OnPropertyChanged();
}
}
Expand Down Expand Up @@ -245,8 +245,7 @@ public PaintCanvasViewModel(ApplicationData applicationData, ToolSelector toolSe
_frame = f;
_frame.RefreshLayerRenderSources();
ReloadFrameRender();
Overlay.Clear();
AvaloniaOverlayBitmap = new PixedImage(Overlay);
ClearOverlay();
GridWidth = _frame.Width;
GridHeight = _frame.Height;
});
Expand Down Expand Up @@ -274,7 +273,7 @@ public PaintCanvasViewModel(ApplicationData applicationData, ToolSelector toolSe
{
tool.Previous.Reset();
tool.Previous.ResetProperties();
ResetOverlay();
ClearOverlay();
}
});

Expand All @@ -286,6 +285,7 @@ public PaintCanvasViewModel(ApplicationData applicationData, ToolSelector toolSe
_overlayChanged = Subjects.OverlayModified.Subscribe(overlay =>
{
ReloadOverlay();
OverlayVisible = true;
});

_currentLayerRenderModified = Subjects.CurrentLayerRenderModified.Subscribe(pixels =>
Expand Down Expand Up @@ -352,30 +352,29 @@ public void RecalculateFactor(Point windowSize)
{
RefreshGridCanvas();
_lastWindowSize = windowSize;
ResetOverlay();
ClearOverlay();
}

public void ResetOverlay()
public void RefreshZoomText()
{
Overlay ??= new SKBitmap(_frame.Width, _frame.Height, true);

if (Overlay.Width == _frame.Width && Overlay.Height == _frame.Height)
double zoom = ZoomValue * 100d;
bool needRound = zoom % 1 != 0;
ZoomText = "Zoom: " + zoom.ToString(needRound ? "#.0" : "#");
OnPropertyChanged(nameof(ZoomText));
}
public void ClearOverlay()
{
OverlayVisible = false;
if (_overlayBitmap == null || _overlayBitmap.Width != _frame.Width || _overlayBitmap.Height != _frame.Height)
{
Overlay.Clear();
_overlayBitmap?.Dispose();
_overlayBitmap = new SKBitmap(_frame.Width, _frame.Height, true);
}
else
{
Overlay = new SKBitmap(_frame.Width, _frame.Height, true);
_overlayBitmap.Clear();
}
AvaloniaOverlayBitmap = new PixedImage(Overlay);
}

public void RefreshZoomText()
{
double zoom = ZoomValue * 100d;
bool needRound = zoom % 1 != 0;
ZoomText = "Zoom: " + zoom.ToString(needRound ? "#.0" : "#");
OnPropertyChanged(nameof(ZoomText));
AvaloniaOverlayBitmap.UpdateBitmap(_overlayBitmap);
}

public void RefreshGridCanvas()
Expand Down Expand Up @@ -432,7 +431,6 @@ private void LeftMouseDownAction(MouseEvent mouseEvent)

_leftPressed = true;
_toolSelector.ToolSelected?.ApplyTool(mouseEvent.Point, _frame, ref _overlayBitmap, _currentKeyState);
DebugTouchPointer(mouseEvent);
Subjects.FrameModified.OnNext(_frame);
}

Expand All @@ -451,7 +449,6 @@ private void LeftMouseUpAction(MouseEvent mouseEvent)
_applicationData.CurrentModel.AddHistory();
}

DebugTouchPointer(mouseEvent);
Subjects.FrameModified.OnNext(_frame);
}

Expand All @@ -464,7 +461,6 @@ private void RightMouseDownAction(MouseEvent mouseEvent)

_rightPressed = true;
_toolSelector.ToolSelected?.ApplyTool(mouseEvent.Point, _frame, ref _overlayBitmap, _currentKeyState);
DebugTouchPointer(mouseEvent);
}

private void RightMouseUpAction(MouseEvent mouseEvent)
Expand All @@ -482,7 +478,6 @@ private void RightMouseUpAction(MouseEvent mouseEvent)
_applicationData.CurrentModel.AddHistory();
}

DebugTouchPointer(mouseEvent);
Subjects.FrameModified.OnNext(_frame);
}

Expand Down Expand Up @@ -524,7 +519,7 @@ private void MouseLeaveAction()

private void ReloadOverlay()
{
AvaloniaOverlayBitmap = new PixedImage(Overlay);
AvaloniaOverlayBitmap.UpdateBitmap(_overlayBitmap);
}

private void ReloadFrameRender()
Expand All @@ -537,7 +532,7 @@ private void ReloadFrameRender()
}

_frame.RefreshCurrentLayerRenderSource(pixels);
AvaloniaImageBitmap = new PixedImage(_frame.RenderSource.Source);
AvaloniaImageBitmap.UpdateBitmap(_frame.RenderSource.Source);
}

private bool CanProcess(Point point)
Expand Down Expand Up @@ -566,14 +561,4 @@ private ImageBrush GetTransparentBackgroundBrush()
Transform = new ScaleTransform(delta, delta)
};
}

private void DebugTouchPointer(MouseEvent mouseEvent)
{
#if DEBUG
int imageX = (int)(mouseEvent.Point.X);
int imageY = (int)(mouseEvent.Point.Y);
_overlayBitmap.SetPixel(imageX, imageY, UniColor.Black);
Overlay = _overlayBitmap;
#endif
}
}
2 changes: 1 addition & 1 deletion Pixed.Application/ViewModels/ToolsSectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void ToolRadioButton_IsCheckedChanged(object sender, RoutedEventArgs e)
string name = radio.Name;

_toolSelector.ToolSelected = _toolSelector.GetTool(name);
_paintCanvas.ResetOverlay();
_paintCanvas.ClearOverlay();
}
}

Expand Down
Loading