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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Media;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using SimpleDraw.Models;
|
||||
|
||||
namespace SimpleDraw.ViewModels;
|
||||
|
||||
public class ViewModelBase : ObservableObject
|
||||
{
|
||||
public ObservableCollection<ColorPickerButton>? ColorPalette { get; set; }
|
||||
public IRelayCommand<PointerPressedEventArgs>? CanvasPointerPressedCommand { get; set; }
|
||||
public IRelayCommand<PointerEventArgs>? CanvasPointerMovedCommand { get; set; }
|
||||
public IRelayCommand<PointerReleasedEventArgs>? CanvasPointerReleasedCommand { get; set; }
|
||||
public ObservableCollection<DrawingPoints> DrawingPoints { get; set; } = new();
|
||||
public DrawingPoints? DrawingPoint { get; set; }
|
||||
|
||||
protected bool IsDrawing { get; set; }
|
||||
|
||||
private Brush _brush = new SolidColorBrush(new Color(a: 255, r: 0, g: 0, b: 0));
|
||||
private double _brushThickness = 2.0;
|
||||
|
||||
protected Brush Brush
|
||||
{
|
||||
get => _brush;
|
||||
set => SetProperty(ref _brush, value);
|
||||
}
|
||||
|
||||
public double BrushThickness
|
||||
{
|
||||
get => _brushThickness;
|
||||
set => SetProperty(ref _brushThickness, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user