RendererBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) The Perspex 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.Linq;
  5. using Perspex.Media;
  6. using Perspex.Platform;
  7. namespace Perspex.Rendering
  8. {
  9. /// <summary>
  10. /// Base class for standard renderers.
  11. /// </summary>
  12. /// <remarks>
  13. /// This class provides implements the platform-independent parts of <see cref="IRenderingViewport"/>.
  14. /// </remarks>
  15. public static class RendererMixin
  16. {
  17. /// <summary>
  18. /// Renders the specified visual.
  19. /// </summary>
  20. /// <param name="viewport">IRenderer instance</param>
  21. /// <param name="visual">The visual to render.</param>
  22. /// <param name="target">An optional platform-specific handle.</param>
  23. public static void Render(this IRenderingViewport viewport, IVisual visual, IPlatformHandle target)
  24. {
  25. using (var ctx = viewport.CreateDrawingContext(target))
  26. ctx.Render(visual);
  27. }
  28. /// <summary>
  29. /// Renders the specified visual.
  30. /// </summary>
  31. /// <param name="viewport">IRenderer instance</param>
  32. /// <param name="target">An optional platform-specific handle.</param>
  33. /// <param name="visual">The visual to render.</param>
  34. /// <param name="translation">The current translation.</param>
  35. /// <param name="transform">The current transform.</param>
  36. public static void Render(this IRenderingViewport viewport, IVisual visual, IPlatformHandle target, Matrix translation, Matrix transform)
  37. {
  38. using (var ctx = viewport.CreateDrawingContext(target))
  39. ctx.Render(visual, translation, transform);
  40. }
  41. /// <summary>
  42. /// Renders the specified visual with the specified transform and clip.
  43. /// </summary>
  44. /// <param name="viewport">IRenderer instance</param>
  45. /// <param name="target">An optional platform-specific handle.</param>
  46. /// <param name="visual">The visual to render.</param>
  47. /// <param name="transform">The transform.</param>
  48. /// <param name="clip">An optional clip rectangle.</param>
  49. public static void Render(this IRenderingViewport viewport, IVisual visual, IPlatformHandle target, Matrix transform, Rect? clip = null)
  50. {
  51. using (var context = viewport.CreateDrawingContext(target))
  52. context.Render(visual, transform, clip);
  53. }
  54. /// <summary>
  55. /// Renders the specified visual.
  56. /// </summary>
  57. /// <param name="visual">The visual to render.</param>
  58. ///
  59. /// <param name="context">The drawing context.</param>
  60. public static void Render(this IDrawingContext context, IVisual visual)
  61. {
  62. context.Render(visual, Matrix.Identity);
  63. }
  64. /// <summary>
  65. /// Renders the specified visual with the specified transform and clip.
  66. /// </summary>
  67. /// <param name="visual">The visual to render.</param>
  68. /// <param name="context">The drawing context.</param>
  69. /// <param name="transform">The transform.</param>
  70. /// <param name="clip">An optional clip rectangle.</param>
  71. public static void Render(this IDrawingContext context, IVisual visual, Matrix transform, Rect? clip = null)
  72. {
  73. using (clip.HasValue ? context.PushClip(clip.Value) : null)
  74. {
  75. context.Render(visual, Matrix.Identity, transform);
  76. }
  77. }
  78. /// <summary>
  79. /// Renders the specified visual.
  80. /// </summary>
  81. /// <param name="visual">The visual to render.</param>
  82. /// <param name="context">The drawing context.</param>
  83. /// <param name="translation">The current translation.</param>
  84. /// <param name="transform">The current transform.</param>
  85. public static void Render(this IDrawingContext context, IVisual visual, Matrix translation, Matrix transform)
  86. {
  87. var opacity = visual.Opacity;
  88. if (visual.IsVisible && opacity > 0)
  89. {
  90. // Translate any existing transform into this controls coordinate system.
  91. Matrix offset = Matrix.CreateTranslation(visual.Bounds.Position);
  92. transform = offset * transform * -offset;
  93. // Update the current offset.
  94. translation *= Matrix.CreateTranslation(visual.Bounds.Position);
  95. // Apply the control's render transform, if any.
  96. if (visual.RenderTransform != null)
  97. {
  98. offset = Matrix.CreateTranslation(visual.TransformOrigin.ToPixels(visual.Bounds.Size));
  99. transform *= -offset * visual.RenderTransform.Value * offset;
  100. }
  101. // Draw the control and its children.
  102. var m = transform * translation;
  103. var d = context.PushTransform(m);
  104. using (context.PushOpacity(opacity))
  105. using (visual.ClipToBounds ? context.PushClip(visual.Bounds) : null)
  106. {
  107. visual.Render(context);
  108. d.Dispose();
  109. foreach (var child in visual.VisualChildren.OrderBy(x => x.ZIndex))
  110. {
  111. context.Render(child, translation, transform);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }