136 lines
4.6 KiB
C#
136 lines
4.6 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;
|
|
|
|
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 = new RasterizerState();
|
|
public Camera2D Camera { get; set; }
|
|
|
|
public UIModuleGroup(bool crop = false, Camera2D camera = null)
|
|
{
|
|
Camera = camera;
|
|
if (crop)
|
|
{
|
|
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();
|
|
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.Matrix);
|
|
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 - (int)module.origin.X;
|
|
module.bounds.Y = bounds.Y + offsetY - (int)module.origin.Y;
|
|
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?.Matrix);
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
}
|