Maslov/Views/MainWindow.axaml.cs

156 lines
5.1 KiB
C#
Raw Permalink Normal View History

2024-12-16 09:55:24 +00:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Media;
using belonging.Models;
using System;
namespace belonging.Views;
public partial class MainWindow : Window
{
Random rnd = new Random();
private readonly ShapeManager _shapeManager;
public MainWindow()
{
InitializeComponent();
_shapeManager = new ShapeManager(DrawingCanvas);
// Установка обработчиков событий в коде позади
DrawingCanvas.PointerPressed += _shapeManager.OnPointerPressed;
DrawingCanvas.PointerMoved += _shapeManager.OnPointerMoved;
DrawingCanvas.PointerReleased += _shapeManager.OnPointerReleased;
DrawingCanvas.Loaded += OnCanvasLoaded;
}
private void OnCanvasLoaded(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
// Удаляем обработчик события, чтобы он не вызывался повторно
DrawingCanvas.Loaded -= OnCanvasLoaded;
// Добавляем фигуры в случайных местах при запуске
AddRandomShape(CreateSquare());
AddRandomShape(CreatePentagon());
AddRandomShape(CreateHexagon());
}
private void AddRandomShape(Shape shape)
{
var randomPosition = _shapeManager.GenerateRandomPosition(shape);
Canvas.SetLeft(shape, randomPosition.X);
Canvas.SetTop(shape, randomPosition.Y);
_shapeManager.AddShape(shape);
}
private Rectangle CreateSquare()
{
return new Rectangle
{
Width = 100,
Height = 100,
Fill = Brushes.Black
};
}
private Polygon CreatePentagon()
{
return new Polygon
{
Points = new Avalonia.Collections.AvaloniaList<Point>
{
new Point(50, 0), // Верхняя вершина
new Point(100, 38), // Правая верхняя
new Point(81, 100), // Правая нижняя
new Point(19, 100), // Левая нижняя
new Point(0, 38) // Левая верхняя
},
Fill = Brushes.Black
};
}
private Polygon CreateHexagon()
{
return new Polygon
{
Points = new Avalonia.Collections.AvaloniaList<Point>
{
new Point(50, 0), // Верхняя центральная
new Point(100, 25), // Правая верхняя
new Point(100, 75), // Правая нижняя
new Point(50, 100), // Нижняя центральная
new Point(0, 75), // Левая нижняя
new Point(0, 25) // Левая верхняя
},
Fill = Brushes.Black
};
}
// Обработчики событий для кнопок
private void OnDrawSquareClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var square = new Rectangle
{
Width = 100,
Height = 100,
Fill = Brushes.Black
};
Canvas.SetLeft(square, rnd.NextInt64((int)Width));
Canvas.SetTop(square, rnd.NextInt64((int)Height));
_shapeManager.AddShape(square);
}
public void OnClearAllClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
_shapeManager.ClearShapes()
}
private void OnDrawPentagonClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var pentagon = new Polygon
{
Points = new Avalonia.Collections.AvaloniaList<Point>
{
new Point(50, 0), // Верхняя вершина
new Point(100, 38), // Правая верхняя
new Point(81, 100), // Правая нижняя
new Point(19, 100), // Левая нижняя
new Point(0, 38) // Левая верхняя
},
Fill = Brushes.Black
};
Canvas.SetLeft(pentagon, rnd.NextInt64((int)Width));
Canvas.SetTop(pentagon, rnd.NextInt64((int)Height));
_shapeManager.AddShape(pentagon);
}
private void OnDrawHexagonClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var hexagon = new Polygon
{
Points = new Avalonia.Collections.AvaloniaList<Point>
{
new Point(50, 0), // Верхняя центральная
new Point(100, 25), // Правая верхняя
new Point(100, 75), // Правая нижняя
new Point(50, 100), // Нижняя центральная
new Point(0, 75), // Левая нижняя
new Point(0, 25) // Левая верхняя
},
Fill = Brushes.Black
};
Canvas.SetLeft(hexagon, rnd.NextInt64((int)Width));
Canvas.SetTop(hexagon, rnd.NextInt64((int)Height));
_shapeManager.AddShape(hexagon);
}
}