Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System;


namespace Pixed.Application.Controls;
internal class ImageGrid : OverlayControl
namespace Pixed.Application.Controls.PaintCanvas;
internal class GridOverlay : OverlayControl
{
public const int MinGridSize = 15;
public ImageGrid()
public GridOverlay()
{
ClipToBounds = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Pixed.Application.Zoom;
using SkiaSharp;

namespace Pixed.Application.Controls;
namespace Pixed.Application.Controls.PaintCanvas;
internal abstract class OverlayControl : Control
{
class DrawOperation(Rect bounds, OverlayControl instance) : ICustomDrawOperation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,25 @@
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.Threading;
using Pixed.Core.Models;
using SkiaSharp;

namespace Pixed.Application.Controls;
namespace Pixed.Application.Controls.PaintCanvas;

internal class PaintCanvasImageControl : Control
internal class PaintContainer : Control
{
/// <summary>
/// Defines the <see cref="Source"/> property.
/// </summary>
public static readonly StyledProperty<PixelImage?> SourceProperty =
AvaloniaProperty.Register<PaintCanvasImageControl, PixelImage?>(nameof(Source));
AvaloniaProperty.Register<PaintContainer, PixelImage?>(nameof(Source));

/// <summary>
/// Defines the <see cref="Stretch"/> property.
/// </summary>
public static readonly StyledProperty<Stretch> StretchProperty =
AvaloniaProperty.Register<PaintCanvasImageControl, Stretch>(nameof(Stretch), Stretch.Uniform);

/// <summary>
/// Defines the <see cref="StretchDirection"/> property.
/// </summary>
public static readonly StyledProperty<StretchDirection> StretchDirectionProperty =
AvaloniaProperty.Register<PaintCanvasImageControl, StretchDirection>(
nameof(StretchDirection),
StretchDirection.Both);

static PaintCanvasImageControl()
static PaintContainer()
{
AffectsRender<PaintCanvasImageControl>(SourceProperty, StretchProperty, StretchDirectionProperty);
AffectsMeasure<PaintCanvasImageControl>(SourceProperty, StretchProperty, StretchDirectionProperty);
AutomationProperties.ControlTypeOverrideProperty.OverrideDefaultValue<PaintCanvasImageControl>(AutomationControlType.Image);
AffectsRender<PaintContainer>(SourceProperty);
AffectsMeasure<PaintContainer>(SourceProperty);
AutomationProperties.ControlTypeOverrideProperty.OverrideDefaultValue<PaintContainer>(AutomationControlType.Image);
}

private readonly PixelDrawOperation _image = new();
Expand All @@ -50,24 +37,6 @@ public PixelImage? Source
set => SetValue(SourceProperty, value);
}

/// <summary>
/// Gets or sets a value controlling how the image will be stretched.
/// </summary>
public Stretch Stretch
{
get => GetValue(StretchProperty);
set => SetValue(StretchProperty, value);
}

/// <summary>
/// Gets or sets a value controlling in what direction the image will be stretched.
/// </summary>
public StretchDirection StretchDirection
{
get => GetValue(StretchDirectionProperty);
set => SetValue(StretchDirectionProperty, value);
}

/// <inheritdoc />
protected override bool BypassFlowDirectionPolicies => true;

Expand Down Expand Up @@ -97,8 +66,6 @@ public sealed override void Render(DrawingContext context)
_image.Bounds = sourceRect;
context.DrawImage(_image, sourceRect, sourceRect);
}

Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Input);
}

/// <summary>
Expand All @@ -113,7 +80,7 @@ protected override Size MeasureOverride(Size availableSize)

if (source != null)
{
result = Stretch.CalculateSize(availableSize, new Size(source.Width, source.Height), StretchDirection);
result = Stretch.Uniform.CalculateSize(availableSize, new Size(source.Width, source.Height), StretchDirection.Both);
}

return result;
Expand All @@ -122,17 +89,13 @@ protected override Size MeasureOverride(Size availableSize)
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
var source = Source?.Render();

if (source != null)
if (Source?.Render() is SKBitmap source)
{
var sourceSize = new Size(source.Width, source.Height);
var result = Stretch.CalculateSize(finalSize, sourceSize);
var result = Stretch.Uniform.CalculateSize(finalSize, sourceSize);
return result;
}
else
{
return new Size();
}

return new Size();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<UserControl x:Class="Pixed.Application.Controls.PaintCanvas"
<UserControl x:Class="Pixed.Application.Controls.PaintCanvas.PaintControl"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Pixed.Application.Controls"
xmlns:controls="clr-namespace:Pixed.Application.Controls"
xmlns:paintCanvas="clr-namespace:Pixed.Application.Controls.PaintCanvas"
xmlns:gestures="clr-namespace:Pixed.Application.Controls.Gestures"
xmlns:viewmodels="clr-namespace:Pixed.Application.ViewModels"
xmlns:zoom="clr-namespace:Pixed.Application.Zoom"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
x:DataType="viewmodels:PaintCanvasViewModel">
x:DataType="viewmodels:PaintControlViewModel">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
Expand All @@ -24,10 +24,10 @@
<Border BorderBrush="{StaticResource ButtonBorder}" BorderThickness="2 0 2 2">
<zoom:ZoomControl Name="zoomControl" GesturesEnabled="{Binding GestureZoomEnabled, Mode=TwoWay}" ZoomWidth="{Binding GridWidth, Mode=TwoWay}" ZoomHeight="{Binding GridHeight, Mode=TwoWay}">
<zoom:ZoomControl.ZoomContent>
<controls:TransparentBackground IsHitTestVisible="False" Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="transparentBackground"/>
<controls:PaintCanvasImageControl Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding RenderModel}"/>
<controls:ImageGrid Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
<controls:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>
<paintCanvas:TransparentBackground IsHitTestVisible="False" Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="transparentBackground"/>
<paintCanvas:PaintContainer Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding RenderModel}"/>
<paintCanvas:GridOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
<paintCanvas:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>
</zoom:ZoomControl.ZoomContent>
</zoom:ZoomControl>
</Border>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
using Pixed.Core.Models;
using System;

namespace Pixed.Application.Controls;
namespace Pixed.Application.Controls.PaintCanvas;

internal partial class PaintCanvas : ExtendedControl<PaintCanvasViewModel>
internal partial class PaintControl : ExtendedControl<PaintControlViewModel>
{
private readonly int _scrollBarSize = 18;
private readonly IDisposable _zoomChanged;
public PaintCanvas() : base()
public PaintControl() : base()

Check notice on line 18 in Pixed.Application/Controls/PaintCanvas/PaintControl.axaml.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Pixed.Application/Controls/PaintCanvas/PaintControl.axaml.cs#L18

Remove this redundant 'base()' call.
{
InitializeComponent();
SizeChanged += PaintCanvas_SizeChanged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System;
using System.Collections.Generic;

namespace Pixed.Application.Controls;
namespace Pixed.Application.Controls.PaintCanvas;
internal class SelectionOverlay : OverlayControl
{
private List<Tuple<SKPoint, SKPoint>> _lines = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using Pixed.Core;
using SkiaSharp;

namespace Pixed.Application.Controls;
namespace Pixed.Application.Controls.PaintCanvas;
internal class TransparentBackground : OverlayControl
{
private readonly SKBitmap _transparentBackground;
private readonly SKShader _shader;
private readonly SKPaint _paint;
public TransparentBackground()
{
ClipToBounds = true;
ClipToBounds = false;
_transparentBackground = CreateTransparentBackground();
_shader = SKShader.CreateBitmap(_transparentBackground, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat);
_paint = new SKPaint()
Expand Down
2 changes: 1 addition & 1 deletion Pixed.Application/Controls/PixelImageControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public sealed override void Render(DrawingContext context)
context.DrawImage(_image, sourceRect, destRect);
}

Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Input);
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Register(ref IServiceCollection collection)
collection.AddSingleton<FramesSectionViewModel>();
collection.AddSingleton<LayersSectionViewModel>();
collection.AddSingleton<MainViewModel>();
collection.AddSingleton<PaintCanvasViewModel>();
collection.AddSingleton<PaintControlViewModel>();
collection.AddSingleton<PaletteSectionViewModel>();
collection.AddSingleton<ProjectsSectionViewModel>();
collection.AddSingleton<PropertiesSectionViewModel>();
Expand Down
4 changes: 2 additions & 2 deletions Pixed.Application/Pages/Main.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Pixed"
xmlns:controls="clr-namespace:Pixed.Application.Controls"
xmlns:paintCanvas="clr-namespace:Pixed.Application.Controls.PaintCanvas"
xmlns:mainsections="clr-namespace:Pixed.Application.Controls.MainWindowSections"
xmlns:dd="urn:gong-wpf-dragdrop"
xmlns:viewmodels="clr-namespace:Pixed.Application.ViewModels"
Expand Down Expand Up @@ -35,7 +35,7 @@
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<mainsections:ProjectsSection/>
<controls:PaintCanvas Grid.Row="1" x:Name="paintCanvas"/>
<paintCanvas:PaintControl Grid.Row="1" x:Name="paintCanvas"/>
</Grid>
<mainsections:PropertiesSection Grid.Column="2" Width="242"/>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Media;
using Avalonia.Platform;
using Pixed.Application.Controls;
using Pixed.Application.Controls.PaintCanvas;
using Pixed.Application.Input;
using Pixed.Application.Menu;
using Pixed.Application.Models;
Expand All @@ -20,7 +21,7 @@

namespace Pixed.Application.ViewModels;

internal class PaintCanvasViewModel : ExtendedViewModel, IDisposable
internal class PaintControlViewModel : ExtendedViewModel, IDisposable
{
private readonly ApplicationData _applicationData;
private readonly ToolsManager _toolSelector;
Expand Down Expand Up @@ -191,7 +192,7 @@

public string ZoomText { get; set; }
public ZoomControl ZoomContainer { get; set; }
public ImageGrid GridCanvas { get; set; }
public GridOverlay GridCanvas { get; set; }
public SelectionOverlay SelectionOverlay { get; set; }
public TransparentBackground TransparentBackground { get; set; }
public ImageBrush TransparentBrush
Expand Down Expand Up @@ -234,7 +235,7 @@
}
}

public PaintCanvasViewModel(ApplicationData applicationData, ToolsManager toolSelector, ToolMoveCanvas toolMoveCanvas, SelectionManager selectionManager,
public PaintControlViewModel(ApplicationData applicationData, ToolsManager toolSelector, ToolMoveCanvas toolMoveCanvas, SelectionManager selectionManager,

Check warning on line 238 in Pixed.Application/ViewModels/PaintControlViewModel.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Pixed.Application/ViewModels/PaintControlViewModel.cs#L238

Method PaintControlViewModel::PaintControlViewModel has 138 lines of code (limit is 50)

Check warning on line 238 in Pixed.Application/ViewModels/PaintControlViewModel.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Pixed.Application/ViewModels/PaintControlViewModel.cs#L238

Method PaintControlViewModel::PaintControlViewModel has a cyclomatic complexity of 9 (limit is 8)
FramesSectionViewModel framesSectionViewModel, PropertiesSectionViewModel propertiesSectionViewModel, SelectionMenu selectionMenu)
{
_applicationData = applicationData;
Expand Down
4 changes: 2 additions & 2 deletions Pixed.Application/ViewModels/ToolsSectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
using IPlatformSettings = Pixed.Application.Platform.IPlatformSettings;

namespace Pixed.Application.ViewModels;
internal class ToolsSectionViewModel(ToolsManager toolSelector, PaintCanvasViewModel paintCanvas) : ExtendedViewModel
internal class ToolsSectionViewModel(ToolsManager toolSelector, PaintControlViewModel paintCanvas) : ExtendedViewModel
{
private readonly ToolsManager _toolSelector = toolSelector;
private readonly PaintCanvasViewModel _paintCanvas = paintCanvas;
private readonly PaintControlViewModel _paintCanvas = paintCanvas;
private readonly Dictionary<string, ToolRadioButton> _radios = [];

public void InitializeTools(StackPanel stackPanel)
Expand Down
1 change: 1 addition & 0 deletions Pixed.Application/Zoom/Internals/BaseControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Media.Transformation;
using Avalonia.VisualTree;
using Pixed.Application.Controls;
using Pixed.Application.Controls.PaintCanvas;
using System;
using System.Linq;
using System.Reactive;
Expand Down
1 change: 1 addition & 0 deletions Pixed.Application/Zoom/Internals/ChildrenControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Input;
using Avalonia.Media;
using Pixed.Application.Controls;
using Pixed.Application.Controls.PaintCanvas;
using Pixed.Core;
using System.Collections.Specialized;

Expand Down
1 change: 0 additions & 1 deletion Pixed.Common/Tools/BaseTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public void SetProperty(string name, bool value)
protected static void SetPixels(Frame frame, List<Pixel> pixels)
{
frame.SetPixels(pixels);
Subjects.FrameModified.OnNext(frame);
}

protected static void SetPixels(Layer layer, List<Pixel> pixels)
Expand Down
13 changes: 12 additions & 1 deletion Pixed.Common/Tools/ToolLighten.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Pixed.Core;
using Pixed.Core.Models;
using Pixed.Core.Selection;
using Pixed.Core.Utils;
using SkiaSharp;
using System.Collections.Generic;

Expand All @@ -12,6 +13,8 @@ public class ToolLighten(ApplicationData applicationData) : ToolPenBase(applicat
private const string PROP_DARKEN = "Darken";
private const string PROP_APPLY_ONCE = "Apply once per pixel";

private readonly List<Point> _modified = [];

public override string ImagePath => "avares://Pixed.Application/Resources/fluent-icons/ic_fluent_add_subtract_circle_48_regular.svg";
public override string Name => "Lighten tool";
public override string Id => "tool_lighten";
Expand All @@ -30,10 +33,18 @@ public override void ApplyTool(Point point, Frame frame, ref SKBitmap overlay, K
_prev = point;

var modifiedColor = GetModifierColor(point, frame, ref overlay, shiftPressed, controlPressed);

_modified.AddRange(PaintUtils.GetToolPoints(point, _applicationData.ToolSize));
DrawOnOverlay(modifiedColor, point, frame, ref overlay, selection);
Subjects.OverlayModified.OnNext(overlay);
}

public override void ReleaseTool(Point point, Frame frame, ref SKBitmap overlay, KeyState keyState, BaseSelection? selection)
{
base.ReleaseTool(point, frame, ref overlay, keyState, selection);
_modified.Clear();
}

public override List<ToolProperty> GetToolProperties()
{
return [
Expand All @@ -47,7 +58,7 @@ private int GetModifierColor(Point point, Frame frame, ref SKBitmap overlay, boo
UniColor overlayColor = overlay.GetPixel(point.X, point.Y);
UniColor frameColor = frame.GetPixel(point);

bool isPixelModified = IsPixelModified(point);
bool isPixelModified = _modified.Contains(point);
var pixelColor = isPixelModified ? overlayColor : frameColor;

bool isTransparent = pixelColor == UniColor.Transparent;
Expand Down
Loading
Loading