Files
SimpleDraw/ViewModels/MainWindowViewModel.cs
T

115 lines
3.5 KiB
C#

using System;
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()
{
DisplaySplitEvent = new RelayCommand(DisplaySplit);
CloseAppEvent = new RelayCommand(CloseApp);
MaximizeAppEvent = new RelayCommand(MaximizeApp);
MinimizeAppEvent = new RelayCommand(MinimizeApp);
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;
SelectedColor = color.Color.ToString();
}
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;
}
}