DrawOperation.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using Avalonia.Media;
  3. using Avalonia.Platform;
  4. namespace Avalonia.Rendering.SceneGraph
  5. {
  6. /// <summary>
  7. /// Base class for draw operations that have bounds.
  8. /// </summary>
  9. internal abstract class DrawOperation : IDrawOperation
  10. {
  11. public DrawOperation(Rect bounds, Matrix transform)
  12. {
  13. bounds = bounds.Normalize().TransformToAABB(transform);
  14. Bounds = new Rect(
  15. new Point(Math.Floor(bounds.X), Math.Floor(bounds.Y)),
  16. new Point(Math.Ceiling(bounds.Right), Math.Ceiling(bounds.Bottom)));
  17. }
  18. public Rect Bounds { get; }
  19. public abstract bool HitTest(Point p);
  20. public abstract void Render(IDrawingContextImpl context);
  21. public virtual void Dispose()
  22. {
  23. }
  24. }
  25. internal abstract class DrawOperationWithTransform : DrawOperation, IDrawOperationWithTransform
  26. {
  27. protected DrawOperationWithTransform(Rect bounds, Matrix transform) : base(bounds, transform)
  28. {
  29. Transform = transform;
  30. }
  31. public Matrix Transform { get; }
  32. public sealed override bool HitTest(Point p)
  33. {
  34. if (Transform.IsIdentity)
  35. return HitTestTransformed(p);
  36. if (!Transform.HasInverse)
  37. return false;
  38. var transformedPoint = Transform.Invert().Transform(p);
  39. return HitTestTransformed(transformedPoint);
  40. }
  41. public abstract bool HitTestTransformed(Point p);
  42. }
  43. }