Files
SimpleDraw/ViewModels/MainWindowViewModel.cs
T
2025-03-23 00:24:19 +01:00

138 lines
4.2 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;
if(UndoDrawingPoints.Count > 0) UndoDrawingPoints.Clear();
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;
}
public void GlobalKeyUpHandler(KeyEventArgs e)
{
if (e.KeyModifiers != KeyModifiers.Control) return;
switch (e.Key)
{
case Key.Z:
if (DrawingPoints.Count > 0)
{
UndoDrawingPoints.Add(DrawingPoints[^1]);
DrawingPoints.Remove(DrawingPoints[^1]);
}
break;
case Key.Y:
if (UndoDrawingPoints.Count > 0)
{
DrawingPoints.Add(UndoDrawingPoints[^1]);
UndoDrawingPoints.Remove(UndoDrawingPoints[^1]);
}
break;
}
}
}