anx.framework/ANX.Framework/DrawableGameComponent.cs
SND\AstrorEnales_cp 734679616b - Added ILRepack
- Refactored the AddIn-System to allow merged assemblies (for metro, android, etc. later on)
- Added empty PsVita Sound and Media System modules
- Fixed 2 small missing excludes in the build script
- Cleaned the License headers in the shader files
- Some other refactorings
2012-08-25 17:27:45 +00:00

139 lines
3.7 KiB
C#

#region Using Statements
using System;
using ANX.Framework.Graphics;
#endregion // Using Statements
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
namespace ANX.Framework
{
public class DrawableGameComponent : GameComponent, IDrawable
{
private bool isInitialized;
private IGraphicsDeviceService device;
public GraphicsDevice GraphicsDevice
{
get
{
if (this.device == null)
{
throw new InvalidOperationException("Component is not initialized");
}
return this.device.GraphicsDevice;
}
}
private int drawOrder;
public int DrawOrder
{
get { return drawOrder; }
set
{
if (drawOrder != value)
{
drawOrder = value;
OnDrawOrderChanged(this, EventArgs.Empty);
}
}
}
private bool visible = true;
public bool Visible
{
get { return visible; }
set
{
if (visible != value)
{
visible = value;
OnVisibleChanged(this, EventArgs.Empty);
}
}
}
public event EventHandler<EventArgs> DrawOrderChanged;
public event EventHandler<EventArgs> VisibleChanged;
public DrawableGameComponent(Game game)
: base(game)
{
}
protected virtual void OnDrawOrderChanged(object sender, EventArgs arg)
{
if (DrawOrderChanged != null)
{
DrawOrderChanged(sender, arg);
}
}
protected virtual void OnVisibleChanged(object sender, EventArgs arg)
{
if (VisibleChanged != null)
{
VisibleChanged(sender, arg);
}
}
public override void Initialize()
{
base.Initialize();
if (!isInitialized)
{
this.device = base.Game.Services.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
if (this.device == null)
{
throw new InvalidOperationException("Service not found: IGraphicsDeviceService");
}
this.device.DeviceCreated += OnDeviceCreated;
this.device.DeviceDisposing += OnDeviceDisposing;
}
isInitialized = true;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.UnloadContent();
if (this.device != null)
{
this.device.DeviceCreated -= OnDeviceCreated;
this.device.DeviceDisposing -= OnDeviceDisposing;
}
}
base.Dispose(disposing);
}
protected virtual void LoadContent()
{
}
protected virtual void UnloadContent()
{
}
public virtual void Draw(GameTime gameTime)
{
}
private void OnDeviceCreated(object sender, EventArgs arg)
{
this.LoadContent();
}
private void OnDeviceDisposing(object sender, EventArgs arg)
{
this.UnloadContent();
}
}
}