110 lines
3.4 KiB
C#
110 lines
3.4 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;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using RhythmBullet.Zer01HD.Utilities.Camera;
|
|
using RhythmBullet.Zer01HD.Utilities.Input;
|
|
|
|
namespace RhythmBullet.Zer01HD.UI.Modular
|
|
{
|
|
public class UIModuleGroup : UIModule
|
|
{
|
|
List<UIModule> modules = new List<UIModule>();
|
|
Rectangle scissorBounds;
|
|
RasterizerState scissorRasterizer = new RasterizerState();
|
|
public Camera2D Camera { get; set; }
|
|
|
|
public UIModuleGroup(Camera2D camera = null, bool crop = false)
|
|
{
|
|
Camera = camera;
|
|
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, null, Camera?.TransformMatrix);
|
|
scissorBounds.Width = Bounds.Width;
|
|
scissorBounds.Height = Bounds.Height;
|
|
scissorBounds.X = Bounds.X;
|
|
scissorBounds.Y = Bounds.Y;
|
|
Rectangle scissor = scissorBounds;
|
|
scissorBounds = batch.GraphicsDevice.ScissorRectangle;
|
|
batch.GraphicsDevice.ScissorRectangle = scissor;
|
|
}
|
|
foreach (UIModule module in modules)
|
|
{
|
|
int offsetX = module.Bounds.X;
|
|
int offsetY = module.Bounds.Y;
|
|
module.Bounds.X = Bounds.X + offsetX;
|
|
module.Bounds.Y = Bounds.Y + offsetY;
|
|
module.Draw(batch);
|
|
module.Bounds.X = offsetX;
|
|
module.Bounds.Y = offsetY;
|
|
}
|
|
|
|
if (scissorBounds != null)
|
|
{
|
|
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
|
|
batch.End();
|
|
batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Camera?.TransformMatrix);
|
|
}
|
|
}
|
|
|
|
public override void Update(GameTime gameTime)
|
|
{
|
|
foreach (UIModule module in modules)
|
|
{
|
|
module.Update(gameTime);
|
|
}
|
|
}
|
|
|
|
public void AddModule(params UIModule[] addModules)
|
|
{
|
|
foreach (UIModule 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(UIModule module)
|
|
{
|
|
module.Parent = null;
|
|
modules.Remove(module);
|
|
}
|
|
|
|
public override bool KeyboardStateChanged(KeyboardState state)
|
|
{
|
|
foreach (UIModule module in modules)
|
|
{
|
|
module.KeyboardStateChanged(state);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public override bool MouseStateChanged(MouseState state)
|
|
{
|
|
foreach (UIModule module in modules)
|
|
{
|
|
module.MouseStateChanged(state);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|