using Chernobyl.Collections.Generic; using Chernobyl.Collections.Generic.Event; using Chernobyl.ComponentModel; using Chernobyl.Creation; using Chernobyl.Event; using Chernobyl.Graphics.Drawing; using Chernobyl.Graphics.Polygon.Buffers; namespace Chernobyl.Graphics.Xna.Controllers { /// /// Responsible for handling the XNA specific drawing of a /// 's data. /// public class XnaDrawBuffer : Drawable, IDrawBuffer { /// /// Constructor. /// /// The services instance that takes services and /// gives services. /// The buffer whose data is to be rendered. public XnaDrawBuffer(IEventCollection services, IBuffer buffer) { DescendantAdded += XnaDrawBufferDescendantAdded; Buffer = buffer; } /// /// Applies the XnaBuffer so that its data can be properly drawn to the /// screen. /// public override void PreDraw() { _buffer.Apply(); } /// /// The IBuffer whose data is being drawn by this buffer. /// public IBuffer Buffer { get { return _buffer; } set { // the "soul" of the buffer should be the XnaBuffer. BufferDecorator buffer = (BufferDecorator)value; if (buffer != null) _buffer = (IXnaBuffer)buffer.DecoratedBuffer; } } /// /// Returns null since there is not controller for the XNA buffer. /// public IDrawBuffer Controller { get { return null; } } /// /// Gets or sets the name of the instance. /// public string Name { get; set; } /// /// A type that creates instances. /// public class Factory : Builder, IDrawBufferFactory { /// /// Constructs an instance of type . /// /// The method that should be invoked when the /// instance has been created, when its creation was canceled, or when /// an error occurred during its creation. Note that this callback may /// be invoked on another thread. /// The instance that contains the factory arguments. /// contains the instance /// that gives out services for use by this type and takes services /// from this type for use by other systems. /// contains the buffer /// whose data is to be rendered. /// /// The that can be used to control and /// track the creation/configuration of the instance. /// public ITask Create(CreationCompletedEventHandler callback, Pair, IBuffer> args) { ReportCreation(callback, new CreationEventArgs(new XnaDrawBuffer(args.First, args.Second))); return Task.Simple; } } /// /// This method is invoked by the DescendantAdded event. This method /// will look to see if the item added was a Render, if it was it will /// get the XnaRender of that Render and give it the XnaBuffer of this /// class using XnaBuffer.SetRender(XnaRender). /// /// The sender of the argument. /// The arguments to the event. void XnaDrawBufferDescendantAdded(object sender, EventArgs e) { Render render = e.Value as Render; if (render != null) { XnaRender xnaRender = render.Controller as XnaRender; if (xnaRender != null) _buffer.SetRender(xnaRender); } } /// /// The backing field to and the XNA specific buffer. /// IXnaBuffer _buffer; } }