ImageNode.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Platform;
  5. namespace Avalonia.Rendering.SceneGraph
  6. {
  7. /// <summary>
  8. /// A node in the scene graph which represents an image draw.
  9. /// </summary>
  10. internal class ImageNode : DrawOperation
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="ImageNode"/> class.
  14. /// </summary>
  15. /// <param name="transform">The transform.</param>
  16. /// <param name="source">The image to draw.</param>
  17. /// <param name="opacity">The draw opacity.</param>
  18. /// <param name="sourceRect">The source rect.</param>
  19. /// <param name="destRect">The destination rect.</param>
  20. public ImageNode(Matrix transform, IBitmapImpl source, double opacity, Rect sourceRect, Rect destRect)
  21. : base(destRect, transform, null)
  22. {
  23. Transform = transform;
  24. Source = source;
  25. Opacity = opacity;
  26. SourceRect = sourceRect;
  27. DestRect = destRect;
  28. }
  29. /// <summary>
  30. /// Gets the transform with which the node will be drawn.
  31. /// </summary>
  32. public Matrix Transform { get; }
  33. /// <summary>
  34. /// Gets the image to draw.
  35. /// </summary>
  36. public IBitmapImpl Source { get; }
  37. /// <summary>
  38. /// Gets the draw opacity.
  39. /// </summary>
  40. public double Opacity { get; }
  41. /// <summary>
  42. /// Gets the source rect.
  43. /// </summary>
  44. public Rect SourceRect { get; }
  45. /// <summary>
  46. /// Gets the destination rect.
  47. /// </summary>
  48. public Rect DestRect { get; }
  49. /// <summary>
  50. /// Determines if this draw operation equals another.
  51. /// </summary>
  52. /// <param name="transform">The transform of the other draw operation.</param>
  53. /// <param name="source">The image of the other draw operation.</param>
  54. /// <param name="opacity">The opacity of the other draw operation.</param>
  55. /// <param name="sourceRect">The source rect of the other draw operation.</param>
  56. /// <param name="destRect">The dest rect of the other draw operation.</param>
  57. /// <returns>True if the draw operations are the same, otherwise false.</returns>
  58. /// <remarks>
  59. /// The properties of the other draw operation are passed in as arguments to prevent
  60. /// allocation of a not-yet-constructed draw operation object.
  61. /// </remarks>
  62. public bool Equals(Matrix transform, IBitmapImpl source, double opacity, Rect sourceRect, Rect destRect)
  63. {
  64. return transform == Transform &&
  65. Equals(source, Source) &&
  66. opacity == Opacity &&
  67. sourceRect == SourceRect &&
  68. destRect == DestRect;
  69. }
  70. /// <inheritdoc/>
  71. public override void Render(IDrawingContextImpl context)
  72. {
  73. // TODO: Probably need to introduce some kind of locking mechanism in the case of
  74. // WriteableBitmap.
  75. context.Transform = Transform;
  76. context.DrawImage(Source, Opacity, SourceRect, DestRect);
  77. }
  78. /// <inheritdoc/>
  79. public override bool HitTest(Point p) => Bounds.Contains(p);
  80. }
  81. }