using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RecrownedGTK.Graphics.Render;
namespace RecrownedGTK.Graphics.UI.Modular
{
///
/// Contains a group of modules and has its own relative coordinate system.
///
public class UIModuleGroup : UIModule
{
List modules = new List();
///
/// Set this to crop anything that flows out of the boundaries of this group. Will not crop if this is null.
///
public BasicScissor basicScissor;
///
/// Instantiates the UI module group.
///
/// Sets the field.
public UIModuleGroup(BasicScissor basicScissor = null)
{
this.basicScissor = basicScissor;
}
///
/// Draws this group of modules. If scissoring, will use the matrix and effect designated in the to begin the batch normally again.
///
/// Batch used to draw the group.
public override void Draw(ConsistentSpriteBatch spriteBatch)
{
if (basicScissor != null)
{
spriteBatch.End();
basicScissor.Begin(Boundaries, spriteBatch);
}
foreach (UIModule module in modules)
{
int offsetX = module.X;
int offsetY = module.Y;
module.X = X + offsetX;
module.Y = Y + offsetY;
module.Draw(spriteBatch);
module.X = offsetX;
module.Y = offsetY;
}
if (basicScissor != null)
{
basicScissor.End();
spriteBatch.Begin();
}
}
///
/// Updates the group of modules.
///
/// Game time used.
public override void Update(GameTime gameTime)
{
foreach (UIModule module in modules)
{
module.Update(gameTime);
}
}
///
/// Adds module(s) to this group.
///
/// The module(s) to add.
public virtual void AddModules(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);
}
}
///
/// Removes given module from group.
///
/// module to remove.
public virtual void RemoveModule(UIModule module)
{
module.parent = null;
modules.Remove(module);
}
///
/// Obtains an array snapshot of all the modules.
///
public UIModule[] GetCopyOfModules()
{
return modules.ToArray();
}
///
/// Updates the keyboard state of the modules in this group.
///
/// The new state.
/// Whether or not to continue updating the other listeners.
public override bool KeyboardStateChanged(KeyboardState state)
{
foreach (UIModule module in modules)
{
module.KeyboardStateChanged(state);
}
return base.KeyboardStateChanged(state);
}
///
/// Updates the moues state of the modules in this group.
///
/// The new state.
/// Whether or not to continue updating other listeners.
public override bool MouseStateChanged(MouseState state)
{
foreach (UIModule module in modules)
{
module.MouseStateChanged(state);
}
return base.MouseStateChanged(state);
}
}
}