OpacityMaskNode.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Media;
  4. using Avalonia.Platform;
  5. using Avalonia.VisualTree;
  6. namespace Avalonia.Rendering.SceneGraph
  7. {
  8. /// <summary>
  9. /// A node in the scene graph which represents an opacity mask push or pop.
  10. /// </summary>
  11. internal class OpacityMaskNode : BrushDrawOperation
  12. {
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="OpacityMaskNode"/> class that represents an
  15. /// opacity mask push.
  16. /// </summary>
  17. /// <param name="mask">The opacity mask to push.</param>
  18. /// <param name="bounds">The bounds of the mask.</param>
  19. /// <param name="aux">Auxiliary data required to draw the brush.</param>
  20. public OpacityMaskNode(IBrush mask, Rect bounds, IDisposable? aux = null)
  21. : base(default, Matrix.Identity, aux)
  22. {
  23. Mask = mask.ToImmutable();
  24. MaskBounds = bounds;
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="OpacityMaskNode"/> class that represents an
  28. /// opacity mask pop.
  29. /// </summary>
  30. public OpacityMaskNode()
  31. : base(default, Matrix.Identity, null)
  32. {
  33. }
  34. /// <summary>
  35. /// Gets the mask to be pushed or null if the operation represents a pop.
  36. /// </summary>
  37. public IBrush? Mask { get; }
  38. /// <summary>
  39. /// Gets the bounds of the opacity mask or null if the operation represents a pop.
  40. /// </summary>
  41. public Rect? MaskBounds { get; }
  42. /// <inheritdoc/>
  43. public override bool HitTest(Point p) => false;
  44. /// <summary>
  45. /// Determines if this draw operation equals another.
  46. /// </summary>
  47. /// <param name="mask">The opacity mask of the other draw operation.</param>
  48. /// <param name="bounds">The opacity mask bounds of the other draw operation.</param>
  49. /// <returns>True if the draw operations are the same, otherwise false.</returns>
  50. /// <remarks>
  51. /// The properties of the other draw operation are passed in as arguments to prevent
  52. /// allocation of a not-yet-constructed draw operation object.
  53. /// </remarks>
  54. public bool Equals(IBrush? mask, Rect? bounds) => Mask == mask && MaskBounds == bounds;
  55. /// <inheritdoc/>
  56. public override void Render(IDrawingContextImpl context)
  57. {
  58. if (Mask != null)
  59. {
  60. context.PushOpacityMask(Mask, MaskBounds!.Value);
  61. }
  62. else
  63. {
  64. context.PopOpacityMask();
  65. }
  66. }
  67. }
  68. }