Control.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // Controls.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 Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework;
  17. using Microsoft.Xna.Framework.Input.Touch;
  18. using DynamicMenu.Transitions;
  19. #endregion
  20. namespace DynamicMenu.Controls
  21. {
  22. /// <summary>
  23. /// The base class for all controls
  24. /// </summary>
  25. public abstract class Control : IControl
  26. {
  27. #region Fields
  28. private List<Transition> activeTransitions = new List<Transition>();
  29. #endregion
  30. #region Properties
  31. /// <summary>
  32. /// The left position of this control relative from its parent
  33. /// </summary>
  34. public int Left { get; set; }
  35. /// <summary>
  36. /// The top position of this control relative from its parent
  37. /// </summary>
  38. public int Top { get; set; }
  39. /// <summary>
  40. /// The width of this control
  41. /// </summary>
  42. public int Width { get; set; }
  43. /// <summary>
  44. /// The height of this control
  45. /// </summary>
  46. public int Height { get; set; }
  47. /// <summary>
  48. /// The bottom position of this control relative from its parent
  49. /// </summary>
  50. [ContentSerializerIgnore]
  51. public int Bottom
  52. {
  53. get { return Top + Height; }
  54. }
  55. /// <summary>
  56. /// The right position of this control relative from its parent
  57. /// </summary>
  58. [ContentSerializerIgnore]
  59. public int Right
  60. {
  61. get { return Left + Width; }
  62. }
  63. /// <summary>
  64. /// The name of this control
  65. /// </summary>
  66. [ContentSerializer(Optional = true)]
  67. public string Name { get; set; }
  68. /// <summary>
  69. /// The name of the background texture for this control
  70. /// </summary>
  71. [ContentSerializer(Optional = true)]
  72. public string BackTextureName { get; set; }
  73. /// <summary>
  74. /// The Background texture for this control
  75. /// </summary>
  76. [ContentSerializerIgnore]
  77. public Texture2D BackTexture { get; set; }
  78. /// <summary>
  79. /// Whether this control is currently visible and enabled
  80. /// </summary>
  81. [ContentSerializer(Optional = true)]
  82. public bool Visible { get; set; }
  83. /// <summary>
  84. /// The hugh applied to this control
  85. /// </summary>
  86. [ContentSerializer(Optional = true)]
  87. public Color Hue { get; set; }
  88. /// <summary>
  89. /// The parent of this control
  90. /// </summary>
  91. [ContentSerializerIgnore]
  92. public IControl Parent { get; set; }
  93. /// <summary>
  94. /// This can be used to store information related to this control
  95. /// </summary>
  96. [ContentSerializerIgnore]
  97. public object Tag { get; set; }
  98. #endregion
  99. #region Initialization
  100. /// <summary>
  101. /// Public constructor
  102. /// </summary>
  103. public Control()
  104. {
  105. Visible = true;
  106. Hue = Color.White;
  107. }
  108. /// <summary>
  109. /// Control initialization
  110. /// </summary>
  111. virtual public void Initialize()
  112. {
  113. // Do nothing here
  114. }
  115. /// <summary>
  116. /// Loads the content for this control
  117. /// </summary>
  118. virtual public void LoadContent(GraphicsDevice _graphics, ContentManager _content)
  119. {
  120. if (!string.IsNullOrEmpty(BackTextureName))
  121. {
  122. BackTexture = _content.Load<Texture2D>(BackTextureName);
  123. }
  124. }
  125. #endregion
  126. #region Update
  127. /// <summary>
  128. /// Updates this control, called once per game frame
  129. /// </summary>
  130. /// <param name="gameTime">The current game time</param>
  131. /// <param name="gestures">The list of recorded gestures for this frame</param>
  132. virtual public void Update(GameTime gameTime, List<GestureSample> gestures)
  133. {
  134. List<Transition> toRemove = new List<Transition>();
  135. List<Transition> curTransitions = new List<Transition>();
  136. curTransitions.AddRange(activeTransitions);
  137. // Update any applied transitions
  138. foreach (Transition transition in curTransitions)
  139. {
  140. transition.Update(gameTime);
  141. if (!transition.TransitionActive)
  142. {
  143. toRemove.Add(transition);
  144. }
  145. }
  146. // Remove any completed transitions
  147. foreach (Transition transition in toRemove)
  148. {
  149. activeTransitions.Remove(transition);
  150. }
  151. }
  152. #endregion
  153. #region Draw
  154. /// <summary>
  155. /// Draws the control, called once per frame
  156. /// </summary>
  157. /// <param name="gameTime">The current game time</param>
  158. /// <param name="spriteBatch">The sprite batch to draw with</param>
  159. virtual public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  160. {
  161. Texture2D currTexture = GetCurrTexture();
  162. if (currTexture != null)
  163. {
  164. Rectangle rect = GetAbsoluteRect();
  165. spriteBatch.Draw(currTexture, rect, null, Hue, 0f, new Vector2(), SpriteEffects.None, 0f);
  166. }
  167. }
  168. #endregion
  169. #region Helper Functions
  170. /// <summary>
  171. /// Overridable by decendants. This returns the current texture being used as the background image.
  172. /// </summary>
  173. /// <returns>The current texture</returns>
  174. virtual public Texture2D GetCurrTexture()
  175. {
  176. return BackTexture;
  177. }
  178. /// <summary>
  179. /// Gets the top left position of this control in screen coordinates.
  180. /// </summary>
  181. /// <returns>The top left point</returns>
  182. public Point GetAbsoluteTopLeft()
  183. {
  184. Point absoluteTopLeft = new Point(Left, Top);
  185. if (Parent != null)
  186. {
  187. Point parentTopLeft = Parent.GetAbsoluteTopLeft();
  188. absoluteTopLeft.X += parentTopLeft.X;
  189. absoluteTopLeft.Y += parentTopLeft.Y;
  190. }
  191. return absoluteTopLeft;
  192. }
  193. /// <summary>
  194. /// Gets the boundaries for this control in screen coordinates.
  195. /// </summary>
  196. /// <returns>The rect boundaries</returns>
  197. public Rectangle GetAbsoluteRect()
  198. {
  199. Point topLeft = GetAbsoluteTopLeft();
  200. return new Rectangle(topLeft.X, topLeft.Y, Width, Height);
  201. }
  202. /// <summary>
  203. /// Starts the passed in transition on this control.
  204. /// </summary>
  205. /// <param name="transition">The transition to apply</param>
  206. public void ApplyTransition(Transition transition)
  207. {
  208. activeTransitions.Add(transition);
  209. transition.Control = this;
  210. transition.StartTranstion();
  211. }
  212. /// <summary>
  213. /// Draws the specified text in the center of the control.
  214. /// </summary>
  215. /// <param name="_spriteBatch">The sprite batch to draw with</param>
  216. /// <param name="_font">The font to use for the text</param>
  217. /// <param name="_rect">The boundaries to center the text within</param>
  218. /// <param name="_text">The text to draw</param>
  219. /// <param name="_color">The hue to use for the text</param>
  220. protected void DrawCenteredText(SpriteBatch _spriteBatch, SpriteFont _font, Rectangle _rect, string _text, Color _color)
  221. {
  222. if (_font == null || string.IsNullOrEmpty(_text)) return;
  223. // Center the text in the rect
  224. Vector2 midPoint = new Vector2(_rect.X + _rect.Width / 2, _rect.Y + _rect.Height / 2);
  225. Vector2 stringSize = _font.MeasureString(_text);
  226. Vector2 fontPos = new Vector2(midPoint.X - stringSize.X * .5f, midPoint.Y - stringSize.Y * .5f);
  227. _spriteBatch.DrawString(_font, _text, fontPos, _color);
  228. }
  229. /// <summary>
  230. /// Indicates whether the specified position falls within the control
  231. /// </summary>
  232. /// <param name="pos">The position to check</param>
  233. /// <returns>Whether the position is contained within the control</returns>
  234. protected bool ContainsPos(Vector2 pos)
  235. {
  236. Rectangle rect = GetAbsoluteRect();
  237. return rect.Contains((int)pos.X, (int)pos.Y);
  238. }
  239. #endregion
  240. }
  241. }