#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if !WINDOWS_PHONE
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Nuclex.Graphics.SpecialEffects.Masks {
/// Screen mask that fills the screen with a solid color
public class ColorScreenMask : ScreenMask {
/// Delegate for a factory method that creates this screen mask
///
/// Graphics device the screen mask is rendered with
///
///
/// Content manager the effect was loaded from
///
/// Effect that will be used to render the screen mask
/// A new instance of the solid color screen mask
internal delegate ColorScreenMask CreateDelegate(
GraphicsDevice graphicsDevice, ContentManager contentManager, Effect effect
);
/// Initializes as new solid color screen mask
/// Graphics device the skybox cube lives on
/// Content manager the effect belongs to
/// Effect that will be used to draw the screen mask
protected ColorScreenMask(
GraphicsDevice graphicsDevice, ContentManager contentManager, Effect effect
) :
base(graphicsDevice, effect, vertices) {
this.contentManager = contentManager;
}
/// Immediately releases all resources owned by the instance
public override void Dispose() {
if(this.contentManager != null) {
this.contentManager.Dispose();
this.contentManager = null;
}
base.Dispose();
}
/// Color the mask fills the screen with
public Color Color {
get { return new Color(this.Effect.Parameters[0].GetValueVector4()); }
set { this.Effect.Parameters[0].SetValue(value.ToVector4()); }
}
/// Creates a new solid color screen mask
///
/// Graphics device the screen mask will be draw with
///
/// The newly created solid color screen mask
public static ColorScreenMask Create(GraphicsDevice graphicsDevice) {
return Create(graphicsDevice, CreateDefault);
}
/// Creates a new solid color screen mask
///
/// Graphics device the screen mask will be draw with
///
///
/// Factory method that will be used to instantiate the mask
///
/// The newly created solid color screen mask
internal static ColorScreenMask Create(
GraphicsDevice graphicsDevice, CreateDelegate createDelegate
) {
// Fake up a service provider with a graphics device service so we can
// create a content manager without the huge rat-tail of references
IServiceProvider serviceProvider;
serviceProvider = GraphicsDeviceServiceHelper.MakePrivateServiceProvider(
GraphicsDeviceServiceHelper.MakeDummyGraphicsDeviceService(graphicsDevice)
);
// Create a resource content manager to load the default effect and hand
// everything to the new screen mask instance, which will then be responsible
// for freeing those resources again.
ResourceContentManager contentManager = new ResourceContentManager(
serviceProvider, Resources.ScreenMaskResources.ResourceManager
);
try {
Effect effect = contentManager.Load("ScreenMaskEffect");
try {
return createDelegate(graphicsDevice, contentManager, effect);
}
catch(Exception) {
effect.Dispose();
throw;
}
}
catch(Exception) {
contentManager.Dispose();
throw;
}
}
/// Creates a new instance of the solid color screen mask class
///
/// Graphics device the screen mask is rendered with
///
///
/// Content manager the effect was loaded from
///
/// Effect that will be used to render the screen mask
/// A new instance of the solid color screen mask
private static ColorScreenMask CreateDefault(
GraphicsDevice graphicsDevice, ContentManager contentManager, Effect effect
) {
return new ColorScreenMask(graphicsDevice, contentManager, effect);
}
/// Vertices used to draw the screen mask
private static readonly PositionVertex[] vertices = {
new PositionVertex(new Vector2(-1.0f, +1.0f)),
new PositionVertex(new Vector2(-1.0f, -1.0f)),
new PositionVertex(new Vector2(+1.0f, +1.0f)),
new PositionVertex(new Vector2(+1.0f, -1.0f))
};
/// Content manager the solid color fill effect was loaded from
private ContentManager contentManager;
}
} // namespace Nuclex.Graphics.SpecialEffects.Masks
#endif // !WINDOWS_PHONE