rhythmbullet/RhythmBullet/Zer01HD/Utilities/UI/Modular/UIModuleGroup.cs

107 lines
3.3 KiB
C#
Raw Normal View History

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;
2018-11-09 05:48:24 +00:00
using Microsoft.Xna.Framework.Input;
2018-11-12 05:05:51 +00:00
using RhythmBullet.Zer01HD.Utilities.Camera;
2018-11-09 05:48:24 +00:00
using RhythmBullet.Zer01HD.Utilities.Input;
namespace RhythmBullet.Zer01HD.UI.Modular
{
2018-11-09 05:48:24 +00:00
public class UIModuleGroup : UIModule
{
2018-11-09 05:48:24 +00:00
List<UIModule> modules = new List<UIModule>();
Rectangle scissorBounds;
RasterizerState scissorRasterizer = new RasterizerState();
2018-11-09 05:48:24 +00:00
public UIModuleGroup(bool crop = false)
{
if (crop)
{
scissorRasterizer.ScissorTestEnable = true;
scissorBounds = new Rectangle();
}
}
2018-11-12 05:05:51 +00:00
public override void Draw(SpriteBatch batch, Camera2D camera)
{
if (scissorBounds != null)
{
batch.End();
2018-11-12 05:05:51 +00:00
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, camera.TransformMatrix);
2018-11-09 05:48:24 +00:00
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;
}
2018-11-09 05:48:24 +00:00
foreach (UIModule module in modules)
{
2018-11-09 05:48:24 +00:00
int offsetX = module.Bounds.X;
int offsetY = module.Bounds.Y;
module.Bounds.X = Bounds.X + offsetX;
module.Bounds.Y = Bounds.Y + offsetY;
2018-11-12 05:05:51 +00:00
module.Draw(batch, camera);
2018-11-09 05:48:24 +00:00
module.Bounds.X = offsetX;
module.Bounds.Y = offsetY;
}
if (scissorBounds != null)
{
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End();
2018-11-12 05:05:51 +00:00
batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix);
}
}
public override void Update(GameTime gameTime)
{
2018-11-09 05:48:24 +00:00
foreach (UIModule module in modules)
{
module.Update(gameTime);
}
}
2018-11-09 05:48:24 +00:00
public void AddModule(params UIModule[] addModules)
{
2018-11-09 05:48:24 +00:00
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);
}
}
2018-11-09 05:48:24 +00:00
public void RemoveModule(UIModule module)
{
module.Parent = null;
modules.Remove(module);
}
2018-11-09 05:48:24 +00:00
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;
}
}
}