Этап 7
This commit is contained in:
43
Minint.Core/Services/Impl/DrawingService.cs
Normal file
43
Minint.Core/Services/Impl/DrawingService.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Minint.Core.Models;
|
||||
|
||||
namespace Minint.Core.Services.Impl;
|
||||
|
||||
public sealed class DrawingService : IDrawingService
|
||||
{
|
||||
public void ApplyBrush(MinintLayer layer, int cx, int cy, int radius, int colorIndex, int width, int height)
|
||||
{
|
||||
foreach (var (x, y) in GetBrushMask(cx, cy, radius, width, height))
|
||||
layer.Pixels[y * width + x] = colorIndex;
|
||||
}
|
||||
|
||||
public void ApplyEraser(MinintLayer layer, int cx, int cy, int radius, int width, int height)
|
||||
{
|
||||
foreach (var (x, y) in GetBrushMask(cx, cy, radius, width, height))
|
||||
layer.Pixels[y * width + x] = 0;
|
||||
}
|
||||
|
||||
public List<(int X, int Y)> GetBrushMask(int cx, int cy, int radius, int width, int height)
|
||||
{
|
||||
var mask = new List<(int, int)>();
|
||||
int r = Math.Max(radius, 0);
|
||||
int r2 = r * r;
|
||||
|
||||
int xMin = Math.Max(0, cx - r);
|
||||
int xMax = Math.Min(width - 1, cx + r);
|
||||
int yMin = Math.Max(0, cy - r);
|
||||
int yMax = Math.Min(height - 1, cy + r);
|
||||
|
||||
for (int py = yMin; py <= yMax; py++)
|
||||
{
|
||||
int dy = py - cy;
|
||||
for (int px = xMin; px <= xMax; px++)
|
||||
{
|
||||
int dx = px - cx;
|
||||
if (dx * dx + dy * dy <= r2)
|
||||
mask.Add((px, py));
|
||||
}
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
}
|
||||
47
Minint.Core/Services/Impl/FloodFillService.cs
Normal file
47
Minint.Core/Services/Impl/FloodFillService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Minint.Core.Models;
|
||||
|
||||
namespace Minint.Core.Services.Impl;
|
||||
|
||||
public sealed class FloodFillService : IFloodFillService
|
||||
{
|
||||
public void Fill(MinintLayer layer, int x, int y, int newColorIndex, int width, int height)
|
||||
{
|
||||
if (x < 0 || x >= width || y < 0 || y >= height)
|
||||
return;
|
||||
|
||||
var pixels = layer.Pixels;
|
||||
int targetIndex = pixels[y * width + x];
|
||||
|
||||
if (targetIndex == newColorIndex)
|
||||
return;
|
||||
|
||||
var queue = new Queue<(int X, int Y)>();
|
||||
var visited = new bool[width * height];
|
||||
|
||||
queue.Enqueue((x, y));
|
||||
visited[y * width + x] = true;
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var (cx, cy) = queue.Dequeue();
|
||||
pixels[cy * width + cx] = newColorIndex;
|
||||
|
||||
Span<(int, int)> neighbors =
|
||||
[
|
||||
(cx - 1, cy), (cx + 1, cy),
|
||||
(cx, cy - 1), (cx, cy + 1)
|
||||
];
|
||||
|
||||
foreach (var (nx, ny) in neighbors)
|
||||
{
|
||||
if (nx < 0 || nx >= width || ny < 0 || ny >= height)
|
||||
continue;
|
||||
int ni = ny * width + nx;
|
||||
if (visited[ni] || pixels[ni] != targetIndex)
|
||||
continue;
|
||||
visited[ni] = true;
|
||||
queue.Enqueue((nx, ny));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,6 @@
|
||||
|
||||
<Application.Styles>
|
||||
<FluentTheme />
|
||||
<StyleInclude Source="avares://Avalonia.Controls.ColorPicker/Themes/Fluent/Fluent.xaml"/>
|
||||
</Application.Styles>
|
||||
</Application>
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
@@ -9,18 +10,6 @@ using Avalonia.Threading;
|
||||
|
||||
namespace Minint.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Custom control that renders a WriteableBitmap through a Viewport (pan/zoom).
|
||||
/// Supports nearest-neighbor scaling, pixel grid overlay, scrollbars, and mouse interaction.
|
||||
/// <para>Input model:</para>
|
||||
/// <list type="bullet">
|
||||
/// <item>Ctrl+Wheel: zoom at cursor</item>
|
||||
/// <item>Wheel: scroll vertically</item>
|
||||
/// <item>Shift+Wheel: scroll horizontally</item>
|
||||
/// <item>Touchpad two-finger scroll: free pan (Delta.X + Delta.Y)</item>
|
||||
/// <item>Middle mouse drag: pan</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
public class PixelCanvas : Control
|
||||
{
|
||||
#region Styled Properties
|
||||
@@ -45,11 +34,29 @@ public class PixelCanvas : Control
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events for tool interaction
|
||||
|
||||
/// <summary>Fires when the user presses the left mouse button at an image pixel.</summary>
|
||||
public event Action<int, int>? ToolDown;
|
||||
|
||||
/// <summary>Fires when the user drags with left button held at an image pixel.</summary>
|
||||
public event Action<int, int>? ToolDrag;
|
||||
|
||||
/// <summary>Fires when the cursor moves over the image (pixel coords, or null if outside).</summary>
|
||||
public event Action<(int X, int Y)?>? CursorPixelChanged;
|
||||
|
||||
/// <summary>Set by the host to provide preview mask pixels for overlay.</summary>
|
||||
public Func<List<(int X, int Y)>?>? GetPreviewMask { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
private readonly Viewport _viewport = new();
|
||||
private bool _isPanning;
|
||||
private bool _isDrawing;
|
||||
private Point _panStart;
|
||||
private double _panStartOffsetX, _panStartOffsetY;
|
||||
private bool _viewportInitialized;
|
||||
private (int X, int Y)? _lastCursorPixel;
|
||||
|
||||
private ScrollBar? _hScrollBar;
|
||||
private ScrollBar? _vScrollBar;
|
||||
@@ -70,19 +77,12 @@ public class PixelCanvas : Control
|
||||
ClipToBounds = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects external ScrollBar controls. Call once after the UI is built.
|
||||
/// </summary>
|
||||
public void AttachScrollBars(ScrollBar horizontal, ScrollBar vertical)
|
||||
{
|
||||
if (_hScrollBar is not null)
|
||||
_hScrollBar.ValueChanged -= OnHScrollChanged;
|
||||
if (_vScrollBar is not null)
|
||||
_vScrollBar.ValueChanged -= OnVScrollChanged;
|
||||
|
||||
if (_hScrollBar is not null) _hScrollBar.ValueChanged -= OnHScrollChanged;
|
||||
if (_vScrollBar is not null) _vScrollBar.ValueChanged -= OnVScrollChanged;
|
||||
_hScrollBar = horizontal;
|
||||
_vScrollBar = vertical;
|
||||
|
||||
_hScrollBar.ValueChanged += OnHScrollChanged;
|
||||
_vScrollBar.ValueChanged += OnVScrollChanged;
|
||||
}
|
||||
@@ -95,8 +95,7 @@ public class PixelCanvas : Control
|
||||
context.FillRectangle(Brushes.Transparent, new Rect(Bounds.Size));
|
||||
|
||||
var bmp = SourceBitmap;
|
||||
if (bmp is null)
|
||||
return;
|
||||
if (bmp is null) return;
|
||||
|
||||
int imgW = bmp.PixelSize.Width;
|
||||
int imgH = bmp.PixelSize.Height;
|
||||
@@ -117,7 +116,8 @@ public class PixelCanvas : Control
|
||||
if (ShowGrid && _viewport.Zoom >= 4)
|
||||
DrawPixelGrid(context, imgW, imgH);
|
||||
|
||||
// Defer scrollbar sync — updating layout properties during Render is forbidden
|
||||
DrawToolPreview(context, imgW, imgH);
|
||||
|
||||
int w = imgW, h = imgH;
|
||||
Dispatcher.UIThread.Post(() => SyncScrollBars(w, h), DispatcherPriority.Render);
|
||||
}
|
||||
@@ -136,10 +136,8 @@ public class PixelCanvas : Control
|
||||
using (context.PushClip(visible))
|
||||
{
|
||||
context.FillRectangle(light, visible);
|
||||
|
||||
double startX = visible.X - ((visible.X - rect.X) % (checkerSize * 2));
|
||||
double startY = visible.Y - ((visible.Y - rect.Y) % (checkerSize * 2));
|
||||
|
||||
for (double y = startY; y < visible.Bottom; y += checkerSize)
|
||||
{
|
||||
for (double x = startX; x < visible.Right; x += checkerSize)
|
||||
@@ -175,7 +173,6 @@ public class PixelCanvas : Control
|
||||
var (sx, _) = _viewport.PixelToScreen(px, 0);
|
||||
context.DrawLine(pen, new Point(sx, visible.Top), new Point(sx, visible.Bottom));
|
||||
}
|
||||
|
||||
for (int py = startPy; py <= endPy; py++)
|
||||
{
|
||||
var (_, sy) = _viewport.PixelToScreen(0, py);
|
||||
@@ -184,6 +181,44 @@ public class PixelCanvas : Control
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawToolPreview(DrawingContext context, int imgW, int imgH)
|
||||
{
|
||||
var mask = GetPreviewMask?.Invoke();
|
||||
if (mask is null || mask.Count == 0) return;
|
||||
|
||||
double zoom = _viewport.Zoom;
|
||||
var previewBrush = new SolidColorBrush(Color.FromArgb(80, 255, 255, 255));
|
||||
var outlinePen = new Pen(new SolidColorBrush(Color.FromArgb(160, 255, 255, 255)), 1);
|
||||
|
||||
var clip = new Rect(0, 0, Bounds.Width, Bounds.Height);
|
||||
using (context.PushClip(clip))
|
||||
{
|
||||
foreach (var (px, py) in mask)
|
||||
{
|
||||
var (sx, sy) = _viewport.PixelToScreen(px, py);
|
||||
var r = new Rect(sx, sy, zoom, zoom);
|
||||
context.FillRectangle(previewBrush, r);
|
||||
}
|
||||
|
||||
// Outline around the mask bounding box
|
||||
if (mask.Count > 0)
|
||||
{
|
||||
int minX = mask[0].X, maxX = mask[0].X;
|
||||
int minY = mask[0].Y, maxY = mask[0].Y;
|
||||
foreach (var (px, py) in mask)
|
||||
{
|
||||
if (px < minX) minX = px;
|
||||
if (px > maxX) maxX = px;
|
||||
if (py < minY) minY = py;
|
||||
if (py > maxY) maxY = py;
|
||||
}
|
||||
var (ox, oy) = _viewport.PixelToScreen(minX, minY);
|
||||
var outlineRect = new Rect(ox, oy, (maxX - minX + 1) * zoom, (maxY - minY + 1) * zoom);
|
||||
context.DrawRectangle(outlinePen, outlineRect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Scrollbar Sync
|
||||
@@ -191,10 +226,8 @@ public class PixelCanvas : Control
|
||||
private void SyncScrollBars(int imgW, int imgH)
|
||||
{
|
||||
if (_hScrollBar is null || _vScrollBar is null) return;
|
||||
|
||||
_suppressScrollSync = true;
|
||||
|
||||
// Scrollbar value is negated offset: increasing value = scroll right = offset decreases
|
||||
var (hMin, hMax, hVal, hView) = _viewport.GetScrollInfo(imgW, Bounds.Width, _viewport.OffsetX);
|
||||
_hScrollBar.Minimum = -hMax;
|
||||
_hScrollBar.Maximum = -hMin;
|
||||
@@ -214,8 +247,7 @@ public class PixelCanvas : Control
|
||||
{
|
||||
if (_suppressScrollSync) return;
|
||||
var (imgW, imgH) = GetImageSize();
|
||||
_viewport.SetOffset(-e.NewValue, _viewport.OffsetY,
|
||||
imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
_viewport.SetOffset(-e.NewValue, _viewport.OffsetY, imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
InvalidateVisual();
|
||||
}
|
||||
|
||||
@@ -223,8 +255,7 @@ public class PixelCanvas : Control
|
||||
{
|
||||
if (_suppressScrollSync) return;
|
||||
var (imgW, imgH) = GetImageSize();
|
||||
_viewport.SetOffset(_viewport.OffsetX, -e.NewValue,
|
||||
imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
_viewport.SetOffset(_viewport.OffsetX, -e.NewValue, imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
InvalidateVisual();
|
||||
}
|
||||
|
||||
@@ -238,6 +269,16 @@ public class PixelCanvas : Control
|
||||
return bmp is not null ? (bmp.PixelSize.Width, bmp.PixelSize.Height) : (0, 0);
|
||||
}
|
||||
|
||||
private (int X, int Y)? ScreenToPixelClamped(Point pos)
|
||||
{
|
||||
var (imgW, imgH) = GetImageSize();
|
||||
if (imgW == 0) return null;
|
||||
var (px, py) = _viewport.ScreenToPixel(pos.X, pos.Y);
|
||||
if (px < 0 || px >= imgW || py < 0 || py >= imgH)
|
||||
return null;
|
||||
return (px, py);
|
||||
}
|
||||
|
||||
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
|
||||
{
|
||||
base.OnPointerWheelChanged(e);
|
||||
@@ -250,20 +291,17 @@ public class PixelCanvas : Control
|
||||
if (ctrl)
|
||||
{
|
||||
var pos = e.GetPosition(this);
|
||||
_viewport.ZoomAtPoint(pos.X, pos.Y, e.Delta.Y,
|
||||
imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
_viewport.ZoomAtPoint(pos.X, pos.Y, e.Delta.Y, imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
}
|
||||
else
|
||||
{
|
||||
double dx = e.Delta.X * ScrollPixelsPerTick;
|
||||
double dy = e.Delta.Y * ScrollPixelsPerTick;
|
||||
|
||||
if (shift && Math.Abs(e.Delta.X) < 0.001)
|
||||
{
|
||||
dx = dy;
|
||||
dy = 0;
|
||||
}
|
||||
|
||||
_viewport.Pan(dx, dy, imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
}
|
||||
|
||||
@@ -274,8 +312,9 @@ public class PixelCanvas : Control
|
||||
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||
{
|
||||
base.OnPointerPressed(e);
|
||||
var props = e.GetCurrentPoint(this).Properties;
|
||||
|
||||
if (e.GetCurrentPoint(this).Properties.IsMiddleButtonPressed)
|
||||
if (props.IsMiddleButtonPressed)
|
||||
{
|
||||
_isPanning = true;
|
||||
_panStart = e.GetPosition(this);
|
||||
@@ -283,15 +322,25 @@ public class PixelCanvas : Control
|
||||
_panStartOffsetY = _viewport.OffsetY;
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (props.IsLeftButtonPressed && !_isPanning)
|
||||
{
|
||||
var pixel = ScreenToPixelClamped(e.GetPosition(this));
|
||||
if (pixel is not null)
|
||||
{
|
||||
_isDrawing = true;
|
||||
ToolDown?.Invoke(pixel.Value.X, pixel.Value.Y);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPointerMoved(PointerEventArgs e)
|
||||
{
|
||||
base.OnPointerMoved(e);
|
||||
var pos = e.GetPosition(this);
|
||||
|
||||
if (_isPanning)
|
||||
{
|
||||
var pos = e.GetPosition(this);
|
||||
var (imgW, imgH) = GetImageSize();
|
||||
_viewport.SetOffset(
|
||||
_panStartOffsetX + (pos.X - _panStart.X),
|
||||
@@ -299,6 +348,22 @@ public class PixelCanvas : Control
|
||||
imgW, imgH, Bounds.Width, Bounds.Height);
|
||||
InvalidateVisual();
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Update preview cursor position
|
||||
var pixel = ScreenToPixelClamped(pos);
|
||||
if (pixel != _lastCursorPixel)
|
||||
{
|
||||
_lastCursorPixel = pixel;
|
||||
CursorPixelChanged?.Invoke(pixel);
|
||||
InvalidateVisual();
|
||||
}
|
||||
|
||||
if (_isDrawing && pixel is not null)
|
||||
{
|
||||
ToolDrag?.Invoke(pixel.Value.X, pixel.Value.Y);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,6 +376,22 @@ public class PixelCanvas : Control
|
||||
_isPanning = false;
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (_isDrawing && e.InitialPressMouseButton == MouseButton.Left)
|
||||
{
|
||||
_isDrawing = false;
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPointerExited(PointerEventArgs e)
|
||||
{
|
||||
base.OnPointerExited(e);
|
||||
if (_lastCursorPixel is not null)
|
||||
{
|
||||
_lastCursorPixel = null;
|
||||
CursorPixelChanged?.Invoke(null);
|
||||
InvalidateVisual();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -318,7 +399,6 @@ public class PixelCanvas : Control
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
||||
{
|
||||
base.OnPropertyChanged(change);
|
||||
|
||||
if (change.Property == SourceBitmapProperty)
|
||||
_viewportInitialized = false;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
|
||||
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Avalonia.Controls.ColorPicker" Version="11.3.8" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using Avalonia;
|
||||
using Avalonia.Media.Imaging;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Minint.Core.Models;
|
||||
using Minint.Core.Services;
|
||||
using Minint.Core.Services.Impl;
|
||||
@@ -13,6 +15,8 @@ public partial class EditorViewModel : ViewModelBase
|
||||
{
|
||||
private readonly ICompositor _compositor = new Compositor();
|
||||
private readonly IPaletteService _paletteService = new PaletteService();
|
||||
private readonly IDrawingService _drawingService = new DrawingService();
|
||||
private readonly IFloodFillService _floodFillService = new FloodFillService();
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(HasContainer))]
|
||||
@@ -33,9 +37,38 @@ public partial class EditorViewModel : ViewModelBase
|
||||
[ObservableProperty]
|
||||
private bool _showGrid;
|
||||
|
||||
// Tool state
|
||||
[ObservableProperty]
|
||||
private ToolType _activeTool = ToolType.Brush;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _brushRadius = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Path of the currently open file, or null for unsaved new containers.
|
||||
/// Pixel coordinates of current brush/eraser preview center, or null if cursor is outside image.
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private (int X, int Y)? _previewCenter;
|
||||
|
||||
private Avalonia.Media.Color _previewColor = Avalonia.Media.Color.FromArgb(255, 0, 0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Avalonia Color bound two-way to the ColorPicker in the toolbar.
|
||||
/// </summary>
|
||||
public Avalonia.Media.Color PreviewColor
|
||||
{
|
||||
get => _previewColor;
|
||||
set
|
||||
{
|
||||
if (_previewColor == value) return;
|
||||
_previewColor = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(SelectedColor));
|
||||
}
|
||||
}
|
||||
|
||||
public RgbaColor SelectedColor => new(_previewColor.R, _previewColor.G, _previewColor.B, _previewColor.A);
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(Title))]
|
||||
private string? _filePath;
|
||||
@@ -51,6 +84,8 @@ public partial class EditorViewModel : ViewModelBase
|
||||
public ObservableCollection<MinintDocument> Documents { get; } = [];
|
||||
public ObservableCollection<MinintLayer> Layers { get; } = [];
|
||||
|
||||
#region Container / Document management
|
||||
|
||||
public void NewContainer(int width, int height)
|
||||
{
|
||||
var c = new MinintContainer(width, height);
|
||||
@@ -70,9 +105,6 @@ public partial class EditorViewModel : ViewModelBase
|
||||
SelectDocument(container.Documents.Count > 0 ? container.Documents[0] : null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by CommunityToolkit when ActiveDocument property changes (e.g. from ListBox binding).
|
||||
/// </summary>
|
||||
partial void OnActiveDocumentChanged(MinintDocument? value)
|
||||
{
|
||||
if (_suppressDocumentSync) return;
|
||||
@@ -84,7 +116,6 @@ public partial class EditorViewModel : ViewModelBase
|
||||
_suppressDocumentSync = true;
|
||||
ActiveDocument = doc;
|
||||
_suppressDocumentSync = false;
|
||||
|
||||
SyncLayersAndCanvas(doc);
|
||||
}
|
||||
|
||||
@@ -101,10 +132,84 @@ public partial class EditorViewModel : ViewModelBase
|
||||
{
|
||||
ActiveLayer = null;
|
||||
}
|
||||
RefreshCanvas();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Drawing
|
||||
|
||||
/// <summary>
|
||||
/// Called by PixelCanvas on left-click at the given image pixel coordinate.
|
||||
/// </summary>
|
||||
public void OnToolDown(int px, int py)
|
||||
{
|
||||
if (Container is null || ActiveDocument is null || ActiveLayer is null)
|
||||
return;
|
||||
|
||||
int w = Container.Width, h = Container.Height;
|
||||
if (px < 0 || px >= w || py < 0 || py >= h)
|
||||
return;
|
||||
|
||||
switch (ActiveTool)
|
||||
{
|
||||
case ToolType.Brush:
|
||||
{
|
||||
int colorIdx = ActiveDocument.EnsureColorCached(SelectedColor);
|
||||
_drawingService.ApplyBrush(ActiveLayer, px, py, BrushRadius, colorIdx, w, h);
|
||||
break;
|
||||
}
|
||||
case ToolType.Eraser:
|
||||
_drawingService.ApplyEraser(ActiveLayer, px, py, BrushRadius, w, h);
|
||||
break;
|
||||
case ToolType.Fill:
|
||||
{
|
||||
int colorIdx = ActiveDocument.EnsureColorCached(SelectedColor);
|
||||
_floodFillService.Fill(ActiveLayer, px, py, colorIdx, w, h);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
RefreshCanvas();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by PixelCanvas on left-drag at the given image pixel coordinate.
|
||||
/// Same as OnToolDown for brush/eraser, no-op for fill.
|
||||
/// </summary>
|
||||
public void OnToolDrag(int px, int py)
|
||||
{
|
||||
if (ActiveTool == ToolType.Fill) return;
|
||||
OnToolDown(px, py);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns brush mask pixels for tool preview overlay.
|
||||
/// </summary>
|
||||
public List<(int X, int Y)>? GetPreviewMask()
|
||||
{
|
||||
if (PreviewCenter is null || Container is null)
|
||||
return null;
|
||||
if (ActiveTool == ToolType.Fill)
|
||||
return null;
|
||||
|
||||
var (cx, cy) = PreviewCenter.Value;
|
||||
return _drawingService.GetBrushMask(cx, cy, BrushRadius, Container.Width, Container.Height);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void SelectBrush() => ActiveTool = ToolType.Brush;
|
||||
|
||||
[RelayCommand]
|
||||
private void SelectEraser() => ActiveTool = ToolType.Eraser;
|
||||
|
||||
[RelayCommand]
|
||||
private void SelectFill() => ActiveTool = ToolType.Fill;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Canvas rendering
|
||||
|
||||
public void RefreshCanvas()
|
||||
{
|
||||
if (Container is null || ActiveDocument is null)
|
||||
@@ -129,7 +234,6 @@ public partial class EditorViewModel : ViewModelBase
|
||||
var dst = new Span<uint>((void*)fb.Address, w * h);
|
||||
for (int i = 0; i < argb.Length; i++)
|
||||
{
|
||||
// argb[i] is 0xAARRGGBB, need premultiplied BGRA for the bitmap
|
||||
uint px = argb[i];
|
||||
byte a = (byte)(px >> 24);
|
||||
byte r = (byte)((px >> 16) & 0xFF);
|
||||
@@ -138,7 +242,7 @@ public partial class EditorViewModel : ViewModelBase
|
||||
|
||||
if (a == 255)
|
||||
{
|
||||
dst[i] = px; // ARGB layout == BGRA in LE memory, alpha=255 → no premul needed
|
||||
dst[i] = px;
|
||||
}
|
||||
else if (a == 0)
|
||||
{
|
||||
@@ -158,6 +262,9 @@ public partial class EditorViewModel : ViewModelBase
|
||||
CanvasBitmap = bmp;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public ICompositor Compositor => _compositor;
|
||||
public IPaletteService PaletteService => _paletteService;
|
||||
public IDrawingService DrawingService => _drawingService;
|
||||
}
|
||||
|
||||
8
Minint/ViewModels/ToolType.cs
Normal file
8
Minint/ViewModels/ToolType.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Minint.ViewModels;
|
||||
|
||||
public enum ToolType
|
||||
{
|
||||
Brush,
|
||||
Eraser,
|
||||
Fill
|
||||
}
|
||||
25
Minint/ViewModels/ToolTypeConverters.cs
Normal file
25
Minint/ViewModels/ToolTypeConverters.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace Minint.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// Static IValueConverter instances for binding RadioButton.IsChecked to ToolType.
|
||||
/// These are one-way (read-only) — the RadioButton Command sets the actual value.
|
||||
/// </summary>
|
||||
public static class ToolTypeConverters
|
||||
{
|
||||
public static readonly IValueConverter IsBrush = new ToolTypeConverter(ToolType.Brush);
|
||||
public static readonly IValueConverter IsEraser = new ToolTypeConverter(ToolType.Eraser);
|
||||
public static readonly IValueConverter IsFill = new ToolTypeConverter(ToolType.Fill);
|
||||
|
||||
private sealed class ToolTypeConverter(ToolType target) : IValueConverter
|
||||
{
|
||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
=> value is ToolType t && t == target;
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
=> target;
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,40 @@
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
|
||||
<!-- Toolbar -->
|
||||
<Border DockPanel.Dock="Top"
|
||||
BorderBrush="{DynamicResource SystemControlForegroundBaseMediumLowBrush}"
|
||||
BorderThickness="0,0,0,1" Padding="6,4">
|
||||
<StackPanel Orientation="Horizontal" Spacing="10">
|
||||
<RadioButton GroupName="Tool" Content="Brush"
|
||||
IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsBrush}}"
|
||||
Command="{Binding Editor.SelectBrushCommand}"/>
|
||||
<RadioButton GroupName="Tool" Content="Eraser"
|
||||
IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsEraser}}"
|
||||
Command="{Binding Editor.SelectEraserCommand}"/>
|
||||
<RadioButton GroupName="Tool" Content="Fill"
|
||||
IsChecked="{Binding Editor.ActiveTool, Converter={x:Static vm:ToolTypeConverters.IsFill}}"
|
||||
Command="{Binding Editor.SelectFillCommand}"/>
|
||||
|
||||
<Separator/>
|
||||
|
||||
<TextBlock Text="Size:" VerticalAlignment="Center"/>
|
||||
<Slider Value="{Binding Editor.BrushRadius}" Minimum="0" Maximum="64"
|
||||
TickFrequency="1" IsSnapToTickEnabled="True" Width="120"
|
||||
VerticalAlignment="Center"/>
|
||||
<TextBlock Text="{Binding Editor.BrushRadius}" VerticalAlignment="Center"
|
||||
Width="20" TextAlignment="Center"/>
|
||||
|
||||
<Separator/>
|
||||
|
||||
<TextBlock Text="Color:" VerticalAlignment="Center"/>
|
||||
<ColorPicker x:Name="ToolColorPicker"
|
||||
Color="{Binding Editor.PreviewColor, Mode=TwoWay}"
|
||||
IsAlphaVisible="False"
|
||||
VerticalAlignment="Center"/>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
|
||||
<!-- Status bar -->
|
||||
<Border DockPanel.Dock="Bottom" Background="{DynamicResource SystemControlBackgroundChromeMediumLowBrush}"
|
||||
Padding="8,2">
|
||||
|
||||
@@ -23,6 +23,9 @@ public partial class MainWindow : Window
|
||||
|
||||
if (canvas is not null && hScroll is not null && vScroll is not null)
|
||||
canvas.AttachScrollBars(hScroll, vScroll);
|
||||
|
||||
if (canvas is not null && DataContext is MainWindowViewModel vm)
|
||||
WireCanvasEvents(canvas, vm.Editor);
|
||||
}
|
||||
|
||||
protected override void OnDataContextChanged(EventArgs e)
|
||||
@@ -31,4 +34,12 @@ public partial class MainWindow : Window
|
||||
if (DataContext is MainWindowViewModel vm)
|
||||
vm.Owner = this;
|
||||
}
|
||||
|
||||
private static void WireCanvasEvents(PixelCanvas canvas, EditorViewModel editor)
|
||||
{
|
||||
canvas.ToolDown += (px, py) => editor.OnToolDown(px, py);
|
||||
canvas.ToolDrag += (px, py) => editor.OnToolDrag(px, py);
|
||||
canvas.CursorPixelChanged += pixel => editor.PreviewCenter = pixel;
|
||||
canvas.GetPreviewMask = () => editor.GetPreviewMask();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user