RectangleNode.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Media;
  4. using Avalonia.Media.Immutable;
  5. using Avalonia.Platform;
  6. using Avalonia.VisualTree;
  7. namespace Avalonia.Rendering.SceneGraph
  8. {
  9. /// <summary>
  10. /// A node in the scene graph which represents a rectangle draw.
  11. /// </summary>
  12. internal class RectangleNode : BrushDrawOperation
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="RectangleNode"/> class.
  16. /// </summary>
  17. /// <param name="transform">The transform.</param>
  18. /// <param name="brush">The fill brush.</param>
  19. /// <param name="pen">The stroke pen.</param>
  20. /// <param name="rect">The rectangle to draw.</param>
  21. /// <param name="boxShadows">The box shadow parameters</param>
  22. public RectangleNode(
  23. Matrix transform,
  24. IImmutableBrush? brush,
  25. IPen? pen,
  26. RoundedRect rect,
  27. BoxShadows boxShadows)
  28. : base(boxShadows.TransformBounds(rect.Rect).Inflate((pen?.Thickness ?? 0) / 2), transform, brush)
  29. {
  30. Pen = pen?.ToImmutable();
  31. Rect = rect;
  32. BoxShadows = boxShadows;
  33. }
  34. /// <summary>
  35. /// Gets the stroke pen.
  36. /// </summary>
  37. public ImmutablePen? Pen { get; }
  38. /// <summary>
  39. /// Gets the rectangle to draw.
  40. /// </summary>
  41. public RoundedRect Rect { get; }
  42. /// <summary>
  43. /// The parameters for the box-shadow effect
  44. /// </summary>
  45. public BoxShadows BoxShadows { get; }
  46. /// <summary>
  47. /// Determines if this draw operation equals another.
  48. /// </summary>
  49. /// <param name="transform">The transform of the other draw operation.</param>
  50. /// <param name="brush">The fill of the other draw operation.</param>
  51. /// <param name="pen">The stroke of the other draw operation.</param>
  52. /// <param name="rect">The rectangle of the other draw operation.</param>
  53. /// <param name="boxShadows">The box shadow parameters of the other draw operation</param>
  54. /// <returns>True if the draw operations are the same, otherwise false.</returns>
  55. /// <remarks>
  56. /// The properties of the other draw operation are passed in as arguments to prevent
  57. /// allocation of a not-yet-constructed draw operation object.
  58. /// </remarks>
  59. public bool Equals(Matrix transform, IBrush? brush, IPen? pen, RoundedRect rect, BoxShadows boxShadows)
  60. {
  61. return transform == Transform &&
  62. Equals(brush, Brush) &&
  63. Equals(Pen, pen) &&
  64. BoxShadows.Equals(boxShadows) &&
  65. rect.Equals(Rect);
  66. }
  67. /// <inheritdoc/>
  68. public override void Render(IDrawingContextImpl context) => context.DrawRectangle(Brush, Pen, Rect, BoxShadows);
  69. /// <inheritdoc/>
  70. public override bool HitTestTransformed(Point p)
  71. {
  72. if (Brush != null)
  73. {
  74. var rect = Rect.Rect.Inflate((Pen?.Thickness / 2) ?? 0);
  75. return rect.ContainsExclusive(p);
  76. }
  77. else
  78. {
  79. var borderRect = Rect.Rect.Inflate((Pen?.Thickness / 2) ?? 0);
  80. var emptyRect = Rect.Rect.Deflate((Pen?.Thickness / 2) ?? 0);
  81. return borderRect.ContainsExclusive(p) && !emptyRect.ContainsExclusive(p);
  82. }
  83. }
  84. }
  85. }