InstructionScreen.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // InstructionScreen.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.Text;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using System.Threading;
  15. using Microsoft.Xna.Framework;
  16. using GameStateManagement;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. #endregion
  20. namespace Blackjack
  21. {
  22. class InstructionScreen : GameplayScreen
  23. {
  24. #region Fields
  25. Texture2D background;
  26. SpriteFont font;
  27. GameplayScreen gameplayScreen;
  28. string theme;
  29. bool isExit = false;
  30. bool isExited = false;
  31. #endregion
  32. #region Initialization
  33. public InstructionScreen(string theme)
  34. : base("")
  35. {
  36. TransitionOnTime = TimeSpan.FromSeconds(0.0);
  37. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  38. this.theme = theme;
  39. #if WINDOWS_PHONE
  40. EnabledGestures = GestureType.Tap;
  41. #endif
  42. }
  43. #endregion
  44. #region Loading
  45. /// <summary>
  46. /// Load the screen resources
  47. /// </summary>
  48. public override void LoadContent()
  49. {
  50. background = Load<Texture2D>(@"Images\instructions");
  51. font = Load<SpriteFont>(@"Fonts\MenuFont");
  52. // Create a new instance of the gameplay screen
  53. gameplayScreen = new GameplayScreen(theme);
  54. }
  55. #endregion
  56. #region Update and Render
  57. /// <summary>
  58. /// Exit the screen after a tap or click
  59. /// </summary>
  60. /// <param name="input"></param>
  61. private void HandleInput(MouseState mouseState, GamePadState padState)
  62. {
  63. if (!isExit)
  64. {
  65. #if WINDOWS_PHONE
  66. if (ScreenManager.input.Gestures.Count > 0 &&
  67. ScreenManager.input.Gestures[0].GestureType == GestureType.Tap)
  68. {
  69. isExit = true;
  70. }
  71. #else
  72. PlayerIndex result;
  73. if (mouseState.LeftButton == ButtonState.Pressed)
  74. {
  75. isExit = true;
  76. }
  77. else if (ScreenManager.input.IsNewButtonPress(Buttons.A, null, out result) ||
  78. ScreenManager.input.IsNewButtonPress(Buttons.Start, null, out result))
  79. {
  80. isExit = true;
  81. }
  82. #endif
  83. }
  84. }
  85. /// <summary>
  86. /// Screen update logic
  87. /// </summary>
  88. /// <param name="gameTime"></param>
  89. /// <param name="otherScreenHasFocus"></param>
  90. /// <param name="coveredByOtherScreen"></param>
  91. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  92. {
  93. if (isExit && !isExited)
  94. {
  95. // Move on to the gameplay screen
  96. foreach (GameScreen screen in ScreenManager.GetScreens())
  97. screen.ExitScreen();
  98. gameplayScreen.ScreenManager = ScreenManager;
  99. ScreenManager.AddScreen(gameplayScreen, null);
  100. isExited = true;
  101. }
  102. HandleInput(Mouse.GetState(), GamePad.GetState(PlayerIndex.One));
  103. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  104. }
  105. /// <summary>
  106. /// Render screen
  107. /// </summary>
  108. /// <param name="gameTime"></param>
  109. public override void Draw(GameTime gameTime)
  110. {
  111. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  112. spriteBatch.Begin();
  113. // Draw Background
  114. spriteBatch.Draw(background, ScreenManager.GraphicsDevice.Viewport.Bounds,
  115. Color.White * TransitionAlpha);
  116. if (isExit)
  117. {
  118. Rectangle safeArea = ScreenManager.SafeArea;
  119. string text = "Loading...";
  120. Vector2 measure = font.MeasureString(text);
  121. Vector2 textPosition = new Vector2(safeArea.Center.X - measure.X / 2,
  122. safeArea.Center.Y - measure.Y / 2);
  123. spriteBatch.DrawString(font, text, textPosition, Color.Black);
  124. }
  125. spriteBatch.End();
  126. base.Draw(gameTime);
  127. }
  128. #endregion
  129. }
  130. }