GameMenuScreen.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameMenuScreen.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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using RobotGameData.GameObject;
  15. #endregion
  16. namespace RobotGameData.Screen
  17. {
  18. /// <summary>
  19. /// Base class for screens that contatain a menu.
  20. /// The user can move up and down to select an entry,
  21. /// or cancel to back out of the screen.
  22. /// </summary>
  23. public class GameMenuScreen : GameScreen
  24. {
  25. #region Fields
  26. List<Sprite2DObject> menuEntries = new List<Sprite2DObject>();
  27. int[] selectedVerticalEntryIndex = new int[4];
  28. int[] selectedHorizontalEntryIndex = new int[4];
  29. #endregion
  30. #region Properties
  31. /// <summary>
  32. /// Gets the list of menu entry strings, so derived classes can add
  33. /// or change the menu contents.
  34. /// </summary>
  35. public IList<Sprite2DObject> MenuEntries
  36. {
  37. get { return menuEntries; }
  38. }
  39. public static int InputCount
  40. {
  41. get { return FrameworkCore.ScreenManager.ScreenInput.Length; }
  42. }
  43. #endregion
  44. /// <summary>
  45. /// Constructor.
  46. /// </summary>
  47. public GameMenuScreen()
  48. {
  49. for( int i=0; i<4; i++)
  50. {
  51. selectedVerticalEntryIndex[i] = 0;
  52. selectedHorizontalEntryIndex[i] = 0;
  53. }
  54. menuEntries.Clear();
  55. }
  56. #region Handle Input
  57. /// <summary>
  58. /// Responds to user input, changing the selected entry and accepting
  59. /// or cancelling the menu.
  60. /// </summary>
  61. public override void HandleInput(GameTime gameTime)
  62. {
  63. for (int i = 0; i < FrameworkCore.ScreenManager.InputCount; i++)
  64. {
  65. GameScreenInput input = FrameworkCore.ScreenManager.ScreenInput[i];
  66. // Move to the previous menu entry?
  67. if (input.MenuUp)
  68. {
  69. selectedVerticalEntryIndex[i]--;
  70. OnFocusEntry(i, selectedVerticalEntryIndex[i],
  71. selectedHorizontalEntryIndex[i]);
  72. }
  73. // Move to the next menu entry?
  74. if (input.MenuDown)
  75. {
  76. selectedVerticalEntryIndex[i]++;
  77. OnFocusEntry(i, selectedVerticalEntryIndex[i],
  78. selectedHorizontalEntryIndex[i]);
  79. }
  80. // Move to the previous menu entry?
  81. if (input.MenuLeft)
  82. {
  83. selectedHorizontalEntryIndex[i]--;
  84. OnFocusEntry(i, selectedVerticalEntryIndex[i],
  85. selectedHorizontalEntryIndex[i]);
  86. }
  87. // Move to the next menu entry?
  88. if (input.MenuRight)
  89. {
  90. selectedHorizontalEntryIndex[i]++;
  91. OnFocusEntry(i, selectedVerticalEntryIndex[i],
  92. selectedHorizontalEntryIndex[i]);
  93. }
  94. // Accept or cancel the menu?
  95. if (input.MenuSelect)
  96. {
  97. OnSelectedEntry(i, selectedVerticalEntryIndex[i],
  98. selectedHorizontalEntryIndex[i]);
  99. }
  100. else
  101. {
  102. if (input.MenuCancel)
  103. {
  104. OnCancel(i);
  105. }
  106. if (input.MenuExit)
  107. {
  108. OnExit(i);
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Notifies derived classes that a menu entry has been chosen.
  115. /// </summary>
  116. public virtual void OnSelectedEntry(int inputIndex,
  117. int verticalEntryIndex,
  118. int horizontalEntryIndex) { }
  119. /// <summary>
  120. /// Notifies derived classes that a menu entry has been focused.
  121. /// </summary>
  122. public virtual void OnFocusEntry( int inputIndex,
  123. int verticalEntryIndex,
  124. int horizontalEntryIndex) { }
  125. /// <summary>
  126. /// Notifies derived classes that the menu has been cancelled.
  127. /// </summary>
  128. public virtual void OnCancel(int inputIndex) { }
  129. /// <summary>
  130. /// Notifies derived classes that a menu entry has been exited.
  131. /// </summary>
  132. public virtual void OnExit(int inputIndex) { }
  133. /// <summary>
  134. /// Notifies derived classes that a menu entry has been updated.
  135. /// </summary>
  136. public virtual void OnUpdateEntry(GameTime gameTime,
  137. int[] verticalEntryIndex,
  138. int[] horizontalEntryIndex) { }
  139. #endregion
  140. #region Update & Draw
  141. /// <summary>
  142. /// always calls the update function for the menu entry.
  143. /// </summary>
  144. /// <param name="gameTime"></param>
  145. /// <param name="otherScreenHasFocus"></param>
  146. /// <param name="coveredByOtherScreen"></param>
  147. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  148. bool coveredByOtherScreen)
  149. {
  150. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  151. if (MenuEntries.Count > 0)
  152. OnUpdateEntry(gameTime, selectedVerticalEntryIndex,
  153. selectedHorizontalEntryIndex);
  154. }
  155. public override void Draw(GameTime gameTime)
  156. {
  157. throw new NotImplementedException(
  158. "The method or operation is not implemented.");
  159. }
  160. #endregion
  161. public void SetVerticalEntryIndex(int index, int value)
  162. {
  163. selectedVerticalEntryIndex[index] = value;
  164. }
  165. public void SetHorizontalEntryIndex(int index, int value)
  166. {
  167. selectedHorizontalEntryIndex[index] = value;
  168. }
  169. public void AddMenuEntry(Sprite2DObject item)
  170. {
  171. MenuEntries.Add(item);
  172. }
  173. public void RemoveMenuEntry(int index)
  174. {
  175. MenuEntries.RemoveAt(index);
  176. }
  177. public void RemoveMenuEntry(Sprite2DObject item)
  178. {
  179. MenuEntries.Remove(item);
  180. }
  181. public void RemoveAllMenuEntry()
  182. {
  183. MenuEntries.Clear();
  184. }
  185. }
  186. }