groundlaying functionality

This commit is contained in:
Ano-sys
2025-02-19 01:05:22 +01:00
commit 6971b05226
14 changed files with 495 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
using Avalonia;
using Avalonia.Input;
using Avalonia.Media;
using CommunityToolkit.Mvvm.Input;
using SimpleDraw.Models;
namespace SimpleDraw.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
#pragma warning disable CA1822 // Mark members as static
public MainWindowViewModel()
{
ColorPalette =
[
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 255, g: 255, b: 255, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 0, g: 0, b: 0, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 128, g: 128, b: 128, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 255, g: 0, b: 0, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 0, g: 255, b: 0, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 0, g: 0, b: 255, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 128, g: 0, b: 128, a: 255)),
},
new ColorPickerButton
{
Color = new SolidColorBrush(new Color(r: 128, g: 128, b: 0, a: 255)),
}
];
foreach (ColorPickerButton colorPickerButton in ColorPalette)
{
var currentColor = colorPickerButton.Color;
colorPickerButton.SelectColorCommand = new RelayCommand(() => ChangeSelectedColor(currentColor));
}
Brush = new SolidColorBrush(Colors.Black);
CanvasPointerPressedCommand = new RelayCommand<PointerPressedEventArgs>(OnCanvasPointerPressed);
CanvasPointerMovedCommand = new RelayCommand<PointerEventArgs>(OnCanvasPointerMoved);
CanvasPointerReleasedCommand = new RelayCommand<PointerReleasedEventArgs>(OnCanvasPointerReleased);
}
#pragma warning restore CA1822 // Mark members as static
private void ChangeSelectedColor(SolidColorBrush color)
{
Brush = color;
}
private void OnCanvasPointerPressed(PointerPressedEventArgs? e)
{
if (e == null) return;
IsDrawing = true;
DrawingPoints.Add(new DrawingPoints(Brush, BrushThickness));
DrawingPoint = DrawingPoints[^1];
// DrawingPoints.DrawingPointsList.Add(DrawingPoints.CurrentDrawingPoints);
if (e.Source is Visual visual)
{
var point = e.GetPosition(visual);
DrawingPoint.CurrentDrawingPoints.Add(point);
}
}
private void OnCanvasPointerMoved(PointerEventArgs? e)
{
if (e == null || !IsDrawing) return;
if (e.Source is Visual visual)
{
var point = e.GetPosition(visual);
DrawingPoint?.CurrentDrawingPoints.Add(point);
}
}
private void OnCanvasPointerReleased(PointerReleasedEventArgs? e)
{
IsDrawing = false;
}
}