recrownedgtk/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs
2019-03-10 00:49:25 -06:00

154 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.Render;
using RecrownedAthenaeum.ScreenSystem;
namespace RecrownedAthenaeum.UI.Modular
{
/// <summary>
/// Contains a group of modules and has its own relative coordinate system.
/// </summary>
public class UIModuleGroup : UIModule
{
List<UIModule> modules = new List<UIModule>();
Rectangle scissorBounds;
RasterizerState scissorRasterizer;
SpriteBatchSettings spriteBatchSettings;
/// <summary>
/// Camera used by the module for cropping.
/// </summary>
public Camera2D camera;
/// <summary>
/// Creates a module group.
/// </summary>
/// <param name="crop">Whether or not to crop out of bounds. Default is false.</param>
/// <param name="camera">What camera to use for cropping. Default is null and will use <see cref="Configuration"/>'s camera if crop is enabled.</param>
/// <param name="spriteBatchSettings">The settings to be used that begins the batch.</param>
public UIModuleGroup(bool crop = false, Camera2D camera = null, SpriteBatchSettings? spriteBatchSettings = null)
{
if (spriteBatchSettings == null) spriteBatchSettings = Configuration.spriteBatchSettings;
if (crop && camera == null) camera = Configuration.Camera2D;
this.spriteBatchSettings = spriteBatchSettings.Value;
this.camera = camera;
if (crop)
{
scissorRasterizer = new RasterizerState();
scissorRasterizer.ScissorTestEnable = true;
scissorBounds = new Rectangle();
}
}
/// <summary>
/// Draws this group of modules.
/// </summary>
/// <param name="batch">Batch used to draw the group.</param>
public override void Draw(SpriteBatch batch)
{
if (scissorBounds != null)
{
batch.End();
spriteBatchSettings.BeginSpriteBatch(batch);
scissorBounds.Width = situation.Width;
scissorBounds.Height = situation.Height;
scissorBounds.X = situation.X;
scissorBounds.Y = situation.Y;
Rectangle scissor = scissorBounds;
scissorBounds = batch.GraphicsDevice.ScissorRectangle;
batch.GraphicsDevice.ScissorRectangle = scissor;
}
foreach (UIModule module in modules)
{
int offsetX = module.situation.X;
int offsetY = module.situation.Y;
module.situation.X = situation.X + offsetX;
module.situation.Y = situation.Y + offsetY;
module.Draw(batch);
module.situation.X = offsetX;
module.situation.Y = offsetY;
}
if (scissorBounds != null)
{
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End();
spriteBatchSettings.BeginSpriteBatch(batch);
}
}
/// <summary>
/// Updates the group of modules.
/// </summary>
/// <param name="gameTime">Game time used.</param>
public override void Update(GameTime gameTime)
{
foreach (UIModule module in modules)
{
module.Update(gameTime);
}
}
/// <summary>
/// Adds module(s) to this group.
/// </summary>
/// <param name="addModules">The module(s) to add.</param>
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);
}
}
/// <summary>
/// Removes given module from group.
/// </summary>
/// <param name="module">module to remove.</param>
public void RemoveModule(UIModule module)
{
module.parent = null;
modules.Remove(module);
}
/// <summary>
/// Updates the keyboard state of the modules in this group.
/// </summary>
/// <param name="state">The new state.</param>
/// <returns>Whether or not to continue updating the other listeners.</returns>
public override bool KeyboardStateChanged(KeyboardState state)
{
foreach (UIModule module in modules)
{
module.KeyboardStateChanged(state);
}
return base.KeyboardStateChanged(state);
}
/// <summary>
/// Updates the moues state of the modules in this group.
/// </summary>
/// <param name="state">The new state.</param>
/// <returns>Whether or not to continue updating other listeners.</returns>
public override bool MouseStateChanged(MouseState state)
{
foreach (UIModule module in modules)
{
module.MouseStateChanged(state);
}
return base.MouseStateChanged(state);
}
}
}