rhythmbullet/RhythmBullet/Zer01HD/UI/Modular/ModuleGroup.cs

86 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RhythmBullet.Zer01HD.UI.Modular
{
public class ModuleGroup : Module
{
List<Module> modules = new List<Module>();
Rectangle scissorBounds;
RasterizerState scissorRasterizer = new RasterizerState();
public ModuleGroup(bool crop = false)
{
if (crop)
{
scissorRasterizer.ScissorTestEnable = true;
scissorBounds = new Rectangle();
}
}
public override void Draw(SpriteBatch batch)
{
if (scissorBounds != null)
{
batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer);
scissorBounds.Width = (int) Width;
scissorBounds.Height = (int) Height;
scissorBounds.X = (int) Position.X;
scissorBounds.Y = (int) Position.Y;
Rectangle scissor = scissorBounds;
scissorBounds = batch.GraphicsDevice.ScissorRectangle;
batch.GraphicsDevice.ScissorRectangle = scissor;
}
foreach (Module module in modules)
{
float offsetX = module.Position.X;
float offsetY = module.Position.Y;
module.Position.X = Position.X + offsetX;
module.Position.Y = Position.Y + offsetY;
module.Draw(batch);
module.Position.X = offsetX;
module.Position.Y = offsetY;
}
if (scissorBounds != null)
{
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End();
batch.Begin();
}
}
public override void Update(GameTime gameTime)
{
foreach (Module module in modules)
{
module.Update(gameTime);
}
}
public void AddModule(params Module[] addModules)
{
foreach (Module module in addModules)
{
if (modules.Contains(module))
{
throw new InvalidOperationException(module.ToString() + " already exists in " + this.ToString());
}
module.Parent = this;
modules.Add(module);
}
}
public void RemoveModule(Module module)
{
module.Parent = null;
modules.Remove(module);
}
}
}