2018-11-29 20:41:06 -06:00
using System ;
using System.Collections.Generic ;
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Input ;
2019-12-28 14:35:01 -06:00
using RecrownedAthenaeum.Graphics.Render ;
2018-11-29 20:41:06 -06:00
2018-12-04 07:45:09 -06:00
namespace RecrownedAthenaeum.UI.Modular
2018-11-29 20:41:06 -06:00
{
2019-01-14 00:34:35 -06:00
/// <summary>
/// Contains a group of modules and has its own relative coordinate system.
/// </summary>
2018-11-29 20:41:06 -06:00
public class UIModuleGroup : UIModule
{
List < UIModule > modules = new List < UIModule > ( ) ;
2019-01-30 07:46:58 -06:00
2019-01-14 20:00:11 -06:00
/// <summary>
2019-03-23 19:04:43 -05:00
/// Set this to crop anything that flows out of the boundaries of this group. Will not crop if this is null.
2019-01-14 20:00:11 -06:00
/// </summary>
2019-03-27 02:35:20 -05:00
public BasicScissor basicScissor ;
2018-11-29 20:41:06 -06:00
2019-04-08 23:58:27 -05:00
/// <summary>
/// Instantiates the UI module group.
/// </summary>
/// <param name="basicScissor">Sets the <see cref="basicScissor"/> field.</param>
public UIModuleGroup ( BasicScissor basicScissor = null )
{
this . basicScissor = basicScissor ;
}
2019-01-14 20:00:11 -06:00
/// <summary>
2019-03-27 02:35:20 -05:00
/// 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.
2019-01-14 20:00:11 -06:00
/// </summary>
2019-03-23 19:04:43 -05:00
/// <param name="spriteBatch">Batch used to draw the group.</param>
public override void Draw ( ConsistentSpriteBatch spriteBatch )
2018-11-29 20:41:06 -06:00
{
2019-03-27 02:35:20 -05:00
if ( basicScissor ! = null )
2018-11-29 20:41:06 -06:00
{
2019-03-23 19:04:43 -05:00
spriteBatch . End ( ) ;
2019-03-27 02:35:20 -05:00
basicScissor . Begin ( Boundaries , spriteBatch ) ;
2018-11-29 20:41:06 -06:00
}
2019-03-01 23:24:08 -06:00
2018-11-29 20:41:06 -06:00
foreach ( UIModule module in modules )
{
2019-04-08 23:58:27 -05:00
int offsetX = module . X ;
int offsetY = module . Y ;
module . X = X + offsetX ;
module . Y = Y + offsetY ;
2019-03-23 19:04:43 -05:00
module . Draw ( spriteBatch ) ;
2019-04-08 23:58:27 -05:00
module . X = offsetX ;
module . Y = offsetY ;
2018-11-29 20:41:06 -06:00
}
2019-03-27 02:35:20 -05:00
if ( basicScissor ! = null )
2018-11-29 20:41:06 -06:00
{
2019-03-27 02:35:20 -05:00
basicScissor . End ( ) ;
2019-03-23 19:04:43 -05:00
spriteBatch . Begin ( ) ;
2018-11-29 20:41:06 -06:00
}
}
2019-01-14 00:34:35 -06:00
/// <summary>
/// Updates the group of modules.
/// </summary>
/// <param name="gameTime">Game time used.</param>
2018-11-29 20:41:06 -06:00
public override void Update ( GameTime gameTime )
{
foreach ( UIModule module in modules )
{
module . Update ( gameTime ) ;
}
}
2019-01-13 23:23:03 -06:00
/// <summary>
/// Adds module(s) to this group.
/// </summary>
/// <param name="addModules">The module(s) to add.</param>
2019-04-08 23:58:27 -05:00
public virtual void AddModules ( params UIModule [ ] addModules )
2018-11-29 20:41:06 -06:00
{
foreach ( UIModule module in addModules )
{
if ( modules . Contains ( module ) )
{
throw new InvalidOperationException ( module . ToString ( ) + " already exists in " + this . ToString ( ) ) ;
}
2019-03-10 00:49:25 -06:00
module . parent = this ;
2018-11-29 20:41:06 -06:00
modules . Add ( module ) ;
}
}
2019-01-13 23:23:03 -06:00
/// <summary>
/// Removes given module from group.
/// </summary>
/// <param name="module">module to remove.</param>
2019-04-08 23:58:27 -05:00
public virtual void RemoveModule ( UIModule module )
2018-11-29 20:41:06 -06:00
{
2019-03-10 00:49:25 -06:00
module . parent = null ;
2018-11-29 20:41:06 -06:00
modules . Remove ( module ) ;
}
2019-04-08 23:58:27 -05:00
/// <summary>
/// Obtains an array snapshot of all the modules.
/// </summary>
public UIModule [ ] GetCopyOfModules ( )
{
return modules . ToArray ( ) ;
}
2018-11-29 20:41:06 -06:00
2019-01-14 00:34:35 -06: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>
2018-11-29 20:41:06 -06:00
public override bool KeyboardStateChanged ( KeyboardState state )
{
foreach ( UIModule module in modules )
{
module . KeyboardStateChanged ( state ) ;
}
return base . KeyboardStateChanged ( state ) ;
}
2019-01-14 00:34:35 -06: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>
2018-11-29 20:41:06 -06:00
public override bool MouseStateChanged ( MouseState state )
{
foreach ( UIModule module in modules )
{
module . MouseStateChanged ( state ) ;
}
return base . MouseStateChanged ( state ) ;
}
}
}