groundlaying functionality
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user