Container.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // Container.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Xml.Serialization;
  15. using Microsoft.Xna.Framework;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input.Touch;
  18. using Microsoft.Xna.Framework.Content;
  19. #endregion
  20. namespace DynamicMenu.Controls
  21. {
  22. /// <summary>
  23. /// Holds a set of controls in positions relative to this container control
  24. /// </summary>
  25. public class Container : Control
  26. {
  27. #region Fields
  28. private List<IControl> controls = new List<IControl>();
  29. private List<IControl> markedForRemove = new List<IControl>();
  30. private List<IControl> markedForAdd = new List<IControl>();
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// The controls this container holds
  35. /// </summary>
  36. public List<IControl> Controls
  37. {
  38. get { return controls; }
  39. }
  40. /// <summary>
  41. /// Add a control to the container immediately
  42. /// </summary>
  43. /// <param name="_control">The control to add</param>
  44. public void AddControl(IControl _control)
  45. {
  46. controls.Add(_control);
  47. _control.Parent = this;
  48. }
  49. /// <summary>
  50. /// Removes a control from the container immediately
  51. /// </summary>
  52. /// <param name="_control">The control to remove</param>
  53. public void RemoveControl(IControl _control)
  54. {
  55. controls.Remove(_control);
  56. }
  57. /// <summary>
  58. /// Marks a control to be added during the next container update
  59. /// </summary>
  60. /// <param name="_control">The control to add</param>
  61. public void MarkForAdd(IControl _control)
  62. {
  63. markedForAdd.Add(_control);
  64. }
  65. /// <summary>
  66. /// Marks a control to be removed during the next container update
  67. /// </summary>
  68. /// <param name="_control">The control to remove</param>
  69. public void MarkForRemove(IControl _control)
  70. {
  71. markedForRemove.Add(_control);
  72. }
  73. #endregion
  74. #region Initialization
  75. /// <summary>
  76. /// Initializes the container and its child controls
  77. /// </summary>
  78. public override void Initialize()
  79. {
  80. foreach (Control control in controls)
  81. {
  82. control.Parent = this;
  83. control.Initialize();
  84. }
  85. }
  86. /// <summary>
  87. /// Loads the container and its child controls
  88. /// </summary>
  89. public override void LoadContent(GraphicsDevice _graphics, ContentManager _content)
  90. {
  91. base.LoadContent(_graphics, _content);
  92. foreach (Control control in controls)
  93. {
  94. control.LoadContent(_graphics, _content);
  95. }
  96. }
  97. #endregion
  98. #region Update
  99. /// <summary>
  100. /// Updates the container and its visible child controls
  101. /// </summary>
  102. public override void Update(GameTime gameTime, List<GestureSample> gestures)
  103. {
  104. base.Update(gameTime, gestures);
  105. if (Visible)
  106. {
  107. foreach (Control ctrl in controls)
  108. {
  109. if (ctrl.Visible)
  110. {
  111. ctrl.Update(gameTime, gestures);
  112. }
  113. }
  114. }
  115. foreach (IControl ctrl in markedForRemove)
  116. {
  117. RemoveControl(ctrl);
  118. }
  119. markedForRemove.Clear();
  120. foreach (IControl ctrl in markedForAdd)
  121. {
  122. AddControl(ctrl);
  123. }
  124. markedForAdd.Clear();
  125. }
  126. #endregion
  127. #region Draw
  128. /// <summary>
  129. /// Draws the container and its visible child controls
  130. /// </summary>
  131. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  132. {
  133. base.Draw(gameTime, spriteBatch);
  134. if (Visible)
  135. {
  136. foreach (Control control in controls)
  137. {
  138. if (control.Visible)
  139. {
  140. control.Draw(gameTime, spriteBatch);
  141. }
  142. }
  143. }
  144. }
  145. #endregion
  146. #region Methods
  147. /// <summary>
  148. /// Gets a child control by the control's name
  149. /// </summary>
  150. /// <param name="_name">The name of the control to find</param>
  151. /// <returns>The named control or null if none is found</returns>
  152. public IControl FindControlByName(string _name)
  153. {
  154. foreach (IControl ctrl in controls)
  155. {
  156. if (ctrl.Name == _name)
  157. {
  158. return ctrl;
  159. }
  160. }
  161. return null;
  162. }
  163. #endregion
  164. }
  165. }