recrownedathenaeum/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs

120 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
2019-03-10 06:49:25 +00:00
using RecrownedAthenaeum.Render;
namespace RecrownedAthenaeum.UI.Modular
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// Contains a group of modules and has its own relative coordinate system.
/// </summary>
public class UIModuleGroup : UIModule
{
List<UIModule> modules = new List<UIModule>();
/// <summary>
/// Set this to crop anything that flows out of the boundaries of this group. Will not crop if this is null.
/// </summary>
public BasicScissor basicScissor;
/// <summary>
/// Draws this group of modules. If scissoring, will use the matrix and effect designated in the <see cref="BasicScissor"/> to begin the batch normally again.
/// </summary>
/// <param name="spriteBatch">Batch used to draw the group.</param>
public override void Draw(ConsistentSpriteBatch spriteBatch)
{
if (basicScissor != null)
{
spriteBatch.End();
basicScissor.Begin(Boundaries, spriteBatch);
}
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(spriteBatch);
module.situation.X = offsetX;
module.situation.Y = offsetY;
}
if (basicScissor != null)
{
basicScissor.End();
spriteBatch.Begin();
}
}
2019-01-14 06:34:35 +00:00
/// <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);
}
}
2019-01-14 05:23:03 +00:00
/// <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());
}
2019-03-10 06:49:25 +00:00
module.parent = this;
modules.Add(module);
}
}
2019-01-14 05:23:03 +00:00
/// <summary>
/// Removes given module from group.
/// </summary>
/// <param name="module">module to remove.</param>
public void RemoveModule(UIModule module)
{
2019-03-10 06:49:25 +00:00
module.parent = null;
modules.Remove(module);
}
2019-01-14 06:34:35 +00:00
/// <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);
}
2019-01-14 06:34:35 +00:00
/// <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);
}
}
}