SelectionObject.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Shapes;
  4. using Avalonia.Media;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace InABox.Avalonia.Components.ImageEditing;
  11. internal class SelectionObject : IImageEditorObject
  12. {
  13. public IBrush? PrimaryBrush { get; set; }
  14. public Point Point1 { get; set; }
  15. public Point Point2 { get; set; }
  16. private Rectangle Control = new();
  17. public Control GetControl() => Control;
  18. public Point GetTopLeft()
  19. {
  20. return new(
  21. Math.Min(Point1.X, Point2.X),
  22. Math.Min(Point1.Y, Point2.Y));
  23. }
  24. public Size GetSize()
  25. {
  26. return new(
  27. Math.Abs(Point2.X - Point1.X),
  28. Math.Abs(Point2.Y - Point1.Y));
  29. }
  30. public void Update()
  31. {
  32. Control.Stroke = GetBrush();
  33. Control.StrokeThickness = 5;
  34. Control.StrokeDashArray = [2, 2];
  35. var topLeft = GetTopLeft();
  36. var size = GetSize();
  37. Canvas.SetLeft(Control, topLeft.X);
  38. Canvas.SetTop(Control, topLeft.Y);
  39. Control.Width = size.Width;
  40. Control.Height = size.Height;
  41. }
  42. private IBrush? GetBrush()
  43. {
  44. var brush = new VisualBrush
  45. {
  46. TileMode = TileMode.Tile,
  47. DestinationRect = new(0, 0, 4, 4, RelativeUnit.Absolute)
  48. };
  49. var canvas = new Canvas
  50. {
  51. Width = 10,
  52. Height = 10
  53. };
  54. var rect1 = new Rectangle { Width = 5, Height = 5 };
  55. var rect2 = new Rectangle { Width = 5, Height = 5 };
  56. var rect3 = new Rectangle { Width = 5, Height = 5 };
  57. var rect4 = new Rectangle { Width = 5, Height = 5 };
  58. Canvas.SetLeft(rect2, 5);
  59. Canvas.SetTop(rect2, 0);
  60. Canvas.SetLeft(rect3, 5);
  61. Canvas.SetTop(rect3, 5);
  62. Canvas.SetLeft(rect4, 0);
  63. Canvas.SetTop(rect4, 5);
  64. rect1.Fill = new SolidColorBrush(Colors.White);
  65. rect2.Fill = new SolidColorBrush(Colors.Black);
  66. rect3.Fill = new SolidColorBrush(Colors.White);
  67. rect4.Fill = new SolidColorBrush(Colors.Black);
  68. canvas.Children.Add(rect1);
  69. canvas.Children.Add(rect2);
  70. canvas.Children.Add(rect3);
  71. brush.Visual = canvas;
  72. return brush;
  73. }
  74. public void SetActive(bool active)
  75. {
  76. }
  77. }