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 { /// /// Contains a group of modules and has its own relative coordinate system. /// public class UIModuleGroup : UIModule { List modules = new List(); Rectangle scissorBounds; RasterizerState scissorRasterizer; /// /// Camera used by the module for cropping. /// public Camera2D camera; /// /// Creates a module group. /// /// Whether or not to crop out of bounds. Default is false. /// What camera to use for cropping. Default is null and will use 's camera if crop is enabled. public UIModuleGroup(bool crop = false, Camera2D camera = null) { if (crop && camera == null) camera = Configuration.Camera2D; this.camera = camera; if (crop) { scissorRasterizer = new RasterizerState(); scissorRasterizer.ScissorTestEnable = true; scissorBounds = new Rectangle(); } } /// /// Draws this group of modules. /// /// Batch used to draw the group. public override void Draw(SpriteBatch batch) { if (scissorBounds != null) { batch.End(); batch.Begin(effect: camera?.BasicEffect); 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; module.bounds.Y = bounds.Y + offsetY; module.Draw(batch); module.bounds.X = offsetX; module.bounds.Y = offsetY; } if (scissorBounds != null) { batch.GraphicsDevice.ScissorRectangle = scissorBounds; batch.End(); batch.Begin(effect: camera?.BasicEffect); } } /// /// 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 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); } } /// /// Removes given module from group. /// /// module to remove. public void RemoveModule(UIModule module) { module.Parent = null; modules.Remove(module); } /// /// 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); } } }