Transition.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // Transitions.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;
  15. using DynamicMenu.Controls;
  16. #endregion
  17. namespace DynamicMenu.Transitions
  18. {
  19. /// <summary>
  20. /// Transitions are used to apply effects to controls. Transitions can be used to resize
  21. /// controls, adjust their position, or change their coloring for an adjustable period of
  22. /// time.
  23. /// To use a transition, instantiate one directly or use one of the creation methods, then
  24. /// use the Control's ApplyTransition method to start it. If you wish to be informed when
  25. /// the transition is complete, register a callback for the TransitionComplete event before
  26. /// using ApplyTransition.
  27. /// </summary>
  28. public class Transition
  29. {
  30. #region Fields
  31. private bool startPositionSet = false;
  32. private bool endPositionSet = false;
  33. private bool startSizeSet = false;
  34. private bool endSizeSet = false;
  35. private bool startHueSet = false;
  36. private bool endHueSet = false;
  37. private Point startPosition = new Point();
  38. private Point endPosition = new Point();
  39. private Point startSize = new Point();
  40. private Point endSize = new Point();
  41. private Color startHue = new Color();
  42. private Color endHue = new Color();
  43. private IControl control = null;
  44. private float transitionLength = 1.0f;
  45. private bool transitionActive = false;
  46. private double transitionStartTime = 0;
  47. #endregion
  48. #region Events
  49. /// <summary>
  50. /// Occurs when a transition is complete
  51. /// </summary>
  52. public event EventHandler TransitionComplete;
  53. #endregion
  54. #region Properties
  55. /// <summary>
  56. /// The control this transition is being applied to
  57. /// </summary>
  58. public IControl Control
  59. {
  60. get { return control; }
  61. set { control = value; }
  62. }
  63. /// <summary>
  64. /// The starting position for the top left corner of the control. This is an optional parameter.
  65. /// </summary>
  66. public Point StartPosition
  67. {
  68. get { return startPosition; }
  69. set
  70. {
  71. startPositionSet = true;
  72. startPosition = value;
  73. }
  74. }
  75. /// <summary>
  76. /// The ending position for the top left corner of the control. This is an optional parameter.
  77. /// </summary>
  78. public Point EndPosition
  79. {
  80. get { return endPosition; }
  81. set
  82. {
  83. endPositionSet = true;
  84. endPosition = value;
  85. }
  86. }
  87. /// <summary>
  88. /// The starting width and height of the control. This is an optional parameter.
  89. /// </summary>
  90. public Point StartSize
  91. {
  92. get { return startSize; }
  93. set
  94. {
  95. startSizeSet = true;
  96. startSize = value;
  97. }
  98. }
  99. /// <summary>
  100. /// The ending width and height of the control. This is an optional parameter.
  101. /// </summary>
  102. public Point EndSize
  103. {
  104. get { return endSize; }
  105. set
  106. {
  107. endSizeSet = true;
  108. endSize = value;
  109. }
  110. }
  111. /// <summary>
  112. /// The starting color of the control. This is an optional parameter.
  113. /// </summary>
  114. public Color StartColor
  115. {
  116. get { return startHue; }
  117. set
  118. {
  119. startHueSet = true;
  120. startHue = value;
  121. }
  122. }
  123. /// <summary>
  124. /// The ending color of the control. This is an optional parameter.
  125. /// </summary>
  126. public Color EndColor
  127. {
  128. get { return endHue; }
  129. set
  130. {
  131. endHueSet = true;
  132. endHue = value;
  133. }
  134. }
  135. /// <summary>
  136. /// The length of time in seconds to play the transition over.
  137. /// </summary>
  138. public float TransitionLength
  139. {
  140. get { return transitionLength; }
  141. set { transitionLength = value; }
  142. }
  143. /// <summary>
  144. /// Whether the transition is currently being played.
  145. /// </summary>
  146. public bool TransitionActive
  147. {
  148. get { return transitionActive; }
  149. }
  150. #endregion
  151. #region Methods
  152. /// <summary>
  153. /// Begin applying the transition to the target control
  154. /// </summary>
  155. public void StartTranstion()
  156. {
  157. transitionActive = true;
  158. transitionStartTime = 0;
  159. if (startPositionSet)
  160. {
  161. control.Left = startPosition.X;
  162. control.Top = startPosition.Y;
  163. }
  164. else
  165. {
  166. startPosition.X = control.Left;
  167. startPosition.Y = control.Top;
  168. }
  169. if (startSizeSet)
  170. {
  171. control.Width = startSize.X;
  172. control.Height = startSize.Y;
  173. }
  174. else
  175. {
  176. startSize.X = control.Width;
  177. startSize.Y = control.Height;
  178. }
  179. if (startHueSet)
  180. {
  181. control.Hue = startHue;
  182. }
  183. else
  184. {
  185. startHue = control.Hue;
  186. }
  187. if (!endPositionSet)
  188. {
  189. endPosition.X = control.Left;
  190. endPosition.Y = control.Top;
  191. }
  192. if (!endSizeSet)
  193. {
  194. endSize.X = control.Width;
  195. endSize.Y = control.Height;
  196. }
  197. if (!endHueSet)
  198. {
  199. endHue = control.Hue;
  200. }
  201. }
  202. /// <summary>
  203. /// Create a transition with the specified starting and ending parameters. Each of these parameters
  204. /// is optional, with a null indicating that that parameter will not be changed with this transition.
  205. /// </summary>
  206. /// <param name="startPosition">The starting position for the top left corner of the control</param>
  207. /// <param name="endPosition">The ending position for the top left corner of the control</param>
  208. /// <param name="startSize">The starting width and height of the control</param>
  209. /// <param name="endSize">The ending width and height of the control</param>
  210. /// <param name="startColor">The starting color of the control</param>
  211. /// <param name="endColor">The ending color of the control</param>
  212. public Transition(Point? startPosition, Point? endPosition, Point? startSize, Point? endSize,
  213. Color? startColor, Color? endColor)
  214. {
  215. if (startPosition.HasValue) StartPosition = startPosition.Value;
  216. if (endPosition.HasValue) EndPosition = endPosition.Value;
  217. if (startSize.HasValue) StartSize = startSize.Value;
  218. if (endSize.HasValue) EndSize = endSize.Value;
  219. if (startColor.HasValue) StartColor = startColor.Value;
  220. if (endColor.HasValue) EndColor = endColor.Value;
  221. }
  222. /// <summary>
  223. /// Increment the trasition between the starting and ending state of the control.
  224. /// </summary>
  225. public void Update(GameTime gameTime)
  226. {
  227. if (transitionActive)
  228. {
  229. // Set the start time if it hasn't been set yet
  230. if (transitionStartTime == 0)
  231. {
  232. transitionStartTime = gameTime.TotalGameTime.TotalSeconds;
  233. }
  234. float timeSinceStart = (float)(gameTime.TotalGameTime.TotalSeconds - transitionStartTime);
  235. float percentComplete = timeSinceStart / transitionLength;
  236. // We've reached the end
  237. if (percentComplete > 1.0)
  238. {
  239. control.Left = endPosition.X;
  240. control.Top = endPosition.Y;
  241. control.Width = endSize.X;
  242. control.Height = endSize.Y;
  243. control.Hue = endHue;
  244. transitionStartTime = 0;
  245. transitionActive = false;
  246. if (TransitionComplete != null)
  247. {
  248. TransitionComplete(this, new EventArgs());
  249. }
  250. }
  251. else
  252. {
  253. control.Left = (int)(startPosition.X + (endPosition.X - startPosition.X) * percentComplete);
  254. control.Top = (int)(startPosition.Y + (endPosition.Y - startPosition.Y) * percentComplete);
  255. control.Width = (int)(startSize.X + (endSize.X - startSize.X) * percentComplete);
  256. control.Height = (int)(startSize.Y + (endSize.Y - startSize.Y) * percentComplete);
  257. Vector4 curHue = startHue.ToVector4() + (endHue.ToVector4() - startHue.ToVector4()) * percentComplete;
  258. control.Hue = new Color(curHue);
  259. }
  260. }
  261. }
  262. #endregion
  263. #region Transition creation methods
  264. /// <summary>
  265. /// Create a transition which fades in a control over the specified period of time.
  266. /// </summary>
  267. /// <param name="control">The control to apply the transition to</param>
  268. /// <param name="length">Amount of time to play this transition over.
  269. /// This is an optional parameter.</param>
  270. /// <returns>The created transition</returns>
  271. public static Transition CreateFadeIn(IControl control, float? length)
  272. {
  273. Color startHue = control.Hue;
  274. Color endHue = control.Hue;
  275. startHue.A = 0;
  276. Transition transition = new Transition(null, null, null, null, startHue, endHue);
  277. transition.Control = control;
  278. if (length.HasValue)
  279. {
  280. transition.TransitionLength = length.Value;
  281. }
  282. return transition;
  283. }
  284. /// <summary>
  285. /// Create a transition which fades out a control over the specified period of time.
  286. /// </summary>
  287. /// <param name="control">The control to apply the transition to</param>
  288. /// <param name="length">Amount of time to play this transition over.
  289. /// This is an optional parameter.</param>
  290. /// <returns>The created transition</returns>
  291. public static Transition CreateFadeOut(IControl control, float? length)
  292. {
  293. Color startHue = control.Hue;
  294. Color endHue = control.Hue;
  295. endHue.A = 0;
  296. Transition transition = new Transition(null, null, null, null, startHue, endHue);
  297. transition.Control = control;
  298. if (length.HasValue)
  299. {
  300. transition.TransitionLength = length.Value;
  301. }
  302. return transition;
  303. }
  304. /// <summary>
  305. /// Creates a transition which causes the control to fly into view from a specified starting point.
  306. /// </summary>
  307. /// <param name="control">The control to apply the transition to</param>
  308. /// <param name="startPos">The position of the top left corner at the start of the transition</param>
  309. /// <param name="length">Amount of time to play this transition over.
  310. /// This is an optional parameter.</param>
  311. /// <returns>The created transition</returns>
  312. public static Transition CreateFlyIn(IControl control, Point startPos, float? length)
  313. {
  314. Transition transition = new Transition(startPos, null, null, null, null, null);
  315. transition.Control = control;
  316. if (length.HasValue)
  317. {
  318. transition.TransitionLength = length.Value;
  319. }
  320. return transition;
  321. }
  322. /// <summary>
  323. /// Creates a transition which causes the control to fly out of view to a specified ending point.
  324. /// </summary>
  325. /// <param name="control">The control to apply the transition to</param>
  326. /// <param name="startPos">The position of the top left corner at the end of the transition</param>
  327. /// <param name="length">Amount of time to play this transition over.
  328. /// This is an optional parameter.</param>
  329. /// <returns>The created transition</returns>
  330. public static Transition CreateFlyOut(IControl control, Point endPos, float? length)
  331. {
  332. Transition transition = new Transition(null, endPos, null, null, null, null);
  333. transition.Control = control;
  334. if (length.HasValue)
  335. {
  336. transition.TransitionLength = length.Value;
  337. }
  338. return transition;
  339. }
  340. #endregion
  341. }
  342. }