141 lines
5.0 KiB
C#
141 lines
5.0 KiB
C#
|
using Avalonia;
|
|||
|
using Avalonia.Controls;
|
|||
|
using Avalonia.Controls.Shapes;
|
|||
|
using Avalonia.Input;
|
|||
|
using Avalonia.Media;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace belonging.Models
|
|||
|
{
|
|||
|
public class ShapeManager
|
|||
|
{
|
|||
|
private readonly Canvas _canvas;
|
|||
|
private readonly List<Shape> _shapes = new List<Shape>();
|
|||
|
private Shape? _draggedShape;
|
|||
|
private Point _dragStart;
|
|||
|
private TextBlock? _messageTextBlock; // Ссылка на текущее сообщение
|
|||
|
|
|||
|
// Статический объект Random для генерации случайных чисел
|
|||
|
private static readonly Random _random = new Random();
|
|||
|
|
|||
|
public ShapeManager(Canvas canvas)
|
|||
|
{
|
|||
|
_canvas = canvas;
|
|||
|
}
|
|||
|
|
|||
|
public void AddShape(Shape shape)
|
|||
|
{
|
|||
|
_shapes.Add(shape);
|
|||
|
_canvas.Children.Add(shape);
|
|||
|
}
|
|||
|
|
|||
|
public void ClearShapes()
|
|||
|
{
|
|||
|
_shapes.Clear();
|
|||
|
_canvas.Children.Clear();
|
|||
|
}
|
|||
|
|
|||
|
public void OnPointerPressed(object? sender, PointerPressedEventArgs e)
|
|||
|
{
|
|||
|
var point = e.GetPosition(_canvas);
|
|||
|
_draggedShape = null;
|
|||
|
|
|||
|
foreach (var shape in _shapes)
|
|||
|
{
|
|||
|
if (shape.Bounds.Contains(point))
|
|||
|
{
|
|||
|
_draggedShape = shape;
|
|||
|
_dragStart = point;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (_draggedShape == null)
|
|||
|
{
|
|||
|
ShowMessage("Не попал");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// Определяем тип фигуры и показываем соответствующее сообщение
|
|||
|
string shapeName = _draggedShape is Rectangle ? "квадрат" :
|
|||
|
_draggedShape is Polygon polygon ? (polygon.Points.Count == 5 ? "пятиугольник" : "шестиугольник") :
|
|||
|
"фигуру";
|
|||
|
|
|||
|
ShowMessage($"Ты попал в {shapeName}!!!");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void OnPointerMoved(object? sender, PointerEventArgs e)
|
|||
|
{
|
|||
|
if (_draggedShape != null)
|
|||
|
{
|
|||
|
var currentPoint = e.GetPosition(_canvas);
|
|||
|
var delta = currentPoint - _dragStart;
|
|||
|
|
|||
|
var newLeft = Canvas.GetLeft(_draggedShape) + delta.X;
|
|||
|
var newTop = Canvas.GetTop(_draggedShape) + delta.Y;
|
|||
|
|
|||
|
// Проверка границ
|
|||
|
if (newLeft < 0) newLeft = 0;
|
|||
|
if (newTop < 0) newTop = 0;
|
|||
|
if (newLeft + _draggedShape.Bounds.Width > _canvas.Bounds.Width) newLeft = _canvas.Bounds.Width - _draggedShape.Bounds.Width;
|
|||
|
if (newTop + _draggedShape.Bounds.Height > _canvas.Bounds.Height) newTop = _canvas.Bounds.Height - _draggedShape.Bounds.Height;
|
|||
|
|
|||
|
Canvas.SetLeft(_draggedShape, newLeft);
|
|||
|
Canvas.SetTop(_draggedShape, newTop);
|
|||
|
|
|||
|
_dragStart = currentPoint;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void OnPointerReleased(object? sender, PointerReleasedEventArgs e)
|
|||
|
{
|
|||
|
_draggedShape = null;
|
|||
|
}
|
|||
|
|
|||
|
private async void ShowMessage(string message)
|
|||
|
{
|
|||
|
// Удаляем старое сообщение, если оно есть
|
|||
|
if (_messageTextBlock != null)
|
|||
|
{
|
|||
|
_canvas.Children.Remove(_messageTextBlock);
|
|||
|
_messageTextBlock = null;
|
|||
|
}
|
|||
|
|
|||
|
// Создаем новое сообщение
|
|||
|
_messageTextBlock = new TextBlock
|
|||
|
{
|
|||
|
Text = message,
|
|||
|
FontSize = 40,
|
|||
|
Foreground = Brushes.Black,
|
|||
|
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
|
|||
|
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top,
|
|||
|
TextAlignment = Avalonia.Media.TextAlignment.Center,
|
|||
|
Margin = new Thickness(0, 30, 0, 0) // Отступ сверху
|
|||
|
};
|
|||
|
|
|||
|
// Фиксированная позиция по центру сверху
|
|||
|
Canvas.SetLeft(_messageTextBlock, (_canvas.Bounds.Width - _messageTextBlock.Bounds.Width) / 2);
|
|||
|
Canvas.SetTop(_messageTextBlock, 0);
|
|||
|
|
|||
|
_canvas.Children.Add(_messageTextBlock);
|
|||
|
|
|||
|
// Удаляем сообщение через 2 секунды
|
|||
|
await Task.Delay(3000);
|
|||
|
if (_messageTextBlock != null)
|
|||
|
{
|
|||
|
_canvas.Children.Remove(_messageTextBlock);
|
|||
|
_messageTextBlock = null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public Point GenerateRandomPosition(Shape shape)
|
|||
|
{
|
|||
|
double x = _random.NextDouble() * (_canvas.Bounds.Width - shape.Bounds.Width);
|
|||
|
double y = _random.NextDouble() * (_canvas.Bounds.Height - shape.Bounds.Height);
|
|||
|
return new Point(x, y);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|