LineNode.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 System.Collections.Generic;
  5. using Avalonia.Media;
  6. using Avalonia.Platform;
  7. using Avalonia.VisualTree;
  8. namespace Avalonia.Rendering.SceneGraph
  9. {
  10. /// <summary>
  11. /// A node in the scene graph which represents a line draw.
  12. /// </summary>
  13. internal class LineNode : BrushDrawOperation
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="GeometryNode"/> class.
  17. /// </summary>
  18. /// <param name="transform">The transform.</param>
  19. /// <param name="pen">The stroke pen.</param>
  20. /// <param name="p1">The start point of the line.</param>
  21. /// <param name="p2">The end point of the line.</param>
  22. /// <param name="childScenes">Child scenes for drawing visual brushes.</param>
  23. public LineNode(
  24. Matrix transform,
  25. Pen pen,
  26. Point p1,
  27. Point p2,
  28. IDictionary<IVisual, Scene> childScenes = null)
  29. : base(new Rect(p1, p2), transform, pen)
  30. {
  31. Transform = transform;
  32. Pen = pen?.ToImmutable();
  33. P1 = p1;
  34. P2 = p2;
  35. ChildScenes = childScenes;
  36. }
  37. /// <summary>
  38. /// Gets the transform with which the node will be drawn.
  39. /// </summary>
  40. public Matrix Transform { get; }
  41. /// <summary>
  42. /// Gets the stroke pen.
  43. /// </summary>
  44. public Pen Pen { get; }
  45. /// <summary>
  46. /// Gets the start point of the line.
  47. /// </summary>
  48. public Point P1 { get; }
  49. /// <summary>
  50. /// Gets the end point of the line.
  51. /// </summary>
  52. public Point P2 { get; }
  53. /// <inheritdoc/>
  54. public override IDictionary<IVisual, Scene> ChildScenes { get; }
  55. /// <summary>
  56. /// Determines if this draw operation equals another.
  57. /// </summary>
  58. /// <param name="transform">The transform of the other draw operation.</param>
  59. /// <param name="pen">The stroke of the other draw operation.</param>
  60. /// <param name="p1">The start point of the other draw operation.</param>
  61. /// <param name="p2">The end point of the other draw operation.</param>
  62. /// <returns>True if the draw operations are the same, otherwise false.</returns>
  63. /// <remarks>
  64. /// The properties of the other draw operation are passed in as arguments to prevent
  65. /// allocation of a not-yet-constructed draw operation object.
  66. /// </remarks>
  67. public bool Equals(Matrix transform, Pen pen, Point p1, Point p2)
  68. {
  69. return transform == Transform && pen == Pen && p1 == P1 && p2 == P2;
  70. }
  71. public override void Render(IDrawingContextImpl context)
  72. {
  73. context.Transform = Transform;
  74. context.DrawLine(Pen, P1, P2);
  75. }
  76. public override bool HitTest(Point p)
  77. {
  78. // TODO: Implement line hit testing.
  79. return false;
  80. }
  81. }
  82. }