// ----------------------------------------------------------------------- // // Copyright 2013 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Windows { using System; using SharpDX; using SharpDX.Direct2D1; using DwFactory = SharpDX.DirectWrite.Factory; using Matrix = Perspex.Media.Matrix; /// /// Renders a . /// public class Renderer { /// /// The render target. /// private WindowRenderTarget renderTarget; /// /// Initializes a new instance of the class. /// /// The window handle. /// The width of the window. /// The height of the window. public Renderer(IntPtr hwnd, int width, int height) { this.Direct2DFactory = new Factory(); this.DirectWriteFactory = new DwFactory(); RenderTargetProperties renderTargetProperties = new RenderTargetProperties { }; HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties { Hwnd = hwnd, PixelSize = new Size2(width, height), }; this.renderTarget = new WindowRenderTarget( this.Direct2DFactory, renderTargetProperties, hwndProperties); } /// /// Gets the Direct2D factory. /// public Factory Direct2DFactory { get; private set; } /// /// Gets the DirectWrite factory. /// public DwFactory DirectWriteFactory { get; private set; } /// /// Renders the specified visual. /// /// The visual to render. public void Render(Visual visual) { using (DrawingContext context = new DrawingContext(this.renderTarget, this.DirectWriteFactory)) { this.Render(visual, context); } } /// /// Resizes the renderer. /// /// The new width. /// The new height. public void Resize(int width, int height) { this.renderTarget.Resize(new Size2(width, height)); } /// /// Renders the specified visual. /// /// The visual to render. /// The drawing context. private void Render(Visual visual, DrawingContext context) { visual.Render(context); foreach (Visual child in visual.VisualChildren) { Matrix translate = Matrix.Translation(child.Bounds.X, child.Bounds.Y); using (context.PushTransform(translate)) { this.Render(child, context); } } } } }