#region File Information
//-----------------------------------------------------------------------------
// Container.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Content;
#endregion
namespace DynamicMenu.Controls
{
///
/// Holds a set of controls in positions relative to this container control
///
public class Container : Control
{
#region Fields
private List controls = new List();
private List markedForRemove = new List();
private List markedForAdd = new List();
#endregion
#region Properties
///
/// The controls this container holds
///
public List Controls
{
get { return controls; }
}
///
/// Add a control to the container immediately
///
/// The control to add
public void AddControl(IControl _control)
{
controls.Add(_control);
_control.Parent = this;
}
///
/// Removes a control from the container immediately
///
/// The control to remove
public void RemoveControl(IControl _control)
{
controls.Remove(_control);
}
///
/// Marks a control to be added during the next container update
///
/// The control to add
public void MarkForAdd(IControl _control)
{
markedForAdd.Add(_control);
}
///
/// Marks a control to be removed during the next container update
///
/// The control to remove
public void MarkForRemove(IControl _control)
{
markedForRemove.Add(_control);
}
#endregion
#region Initialization
///
/// Initializes the container and its child controls
///
public override void Initialize()
{
foreach (Control control in controls)
{
control.Parent = this;
control.Initialize();
}
}
///
/// Loads the container and its child controls
///
public override void LoadContent(GraphicsDevice _graphics, ContentManager _content)
{
base.LoadContent(_graphics, _content);
foreach (Control control in controls)
{
control.LoadContent(_graphics, _content);
}
}
#endregion
#region Update
///
/// Updates the container and its visible child controls
///
public override void Update(GameTime gameTime, List gestures)
{
base.Update(gameTime, gestures);
if (Visible)
{
foreach (Control ctrl in controls)
{
if (ctrl.Visible)
{
ctrl.Update(gameTime, gestures);
}
}
}
foreach (IControl ctrl in markedForRemove)
{
RemoveControl(ctrl);
}
markedForRemove.Clear();
foreach (IControl ctrl in markedForAdd)
{
AddControl(ctrl);
}
markedForAdd.Clear();
}
#endregion
#region Draw
///
/// Draws the container and its visible child controls
///
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
base.Draw(gameTime, spriteBatch);
if (Visible)
{
foreach (Control control in controls)
{
if (control.Visible)
{
control.Draw(gameTime, spriteBatch);
}
}
}
}
#endregion
#region Methods
///
/// Gets a child control by the control's name
///
/// The name of the control to find
/// The named control or null if none is found
public IControl FindControlByName(string _name)
{
foreach (IControl ctrl in controls)
{
if (ctrl.Name == _name)
{
return ctrl;
}
}
return null;
}
#endregion
}
}