MainMenuScreen.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // MainMenuScreen.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using GameStateManagement;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. namespace Blackjack
  14. {
  15. class MainMenuScreen : MenuScreen
  16. {
  17. public static string Theme { get; set; } = "Red";
  18. private bool needsRefresh = false;
  19. /// <summary>
  20. /// Initializes a new instance of the screen.
  21. /// </summary>
  22. public MainMenuScreen()
  23. : base("")
  24. {
  25. // Load theme from settings
  26. Theme = GameSettings.Instance.Theme;
  27. }
  28. public override void LoadContent()
  29. {
  30. BuildMenuEntries();
  31. base.LoadContent();
  32. }
  33. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  34. {
  35. // If we were covered and now we're not, refresh the menu entries
  36. // to pick up any language changes from the settings screen
  37. if (!coveredByOtherScreen && needsRefresh)
  38. {
  39. BuildMenuEntries();
  40. needsRefresh = false;
  41. }
  42. else if (coveredByOtherScreen)
  43. {
  44. needsRefresh = true;
  45. }
  46. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  47. }
  48. private void BuildMenuEntries()
  49. {
  50. // Clear existing entries
  51. MenuEntries.Clear();
  52. // Create our menu entries.
  53. MenuEntry startGameMenuEntry = new MenuEntry(Resources.Play);
  54. MenuEntry settingsMenuEntry = new MenuEntry(Resources.Settings);
  55. MenuEntry exitMenuEntry = new MenuEntry(Resources.Exit);
  56. // Hook up menu event handlers.
  57. startGameMenuEntry.Selected += StartGameMenuEntrySelected;
  58. settingsMenuEntry.Selected += SettingsMenuEntrySelected;
  59. exitMenuEntry.Selected += OnCancel;
  60. // Add entries to the menu.
  61. MenuEntries.Add(startGameMenuEntry);
  62. MenuEntries.Add(settingsMenuEntry);
  63. MenuEntries.Add(exitMenuEntry);
  64. }
  65. /// <summary>
  66. /// Respond to "Play" Item Selection
  67. /// </summary>
  68. /// <param name="sender"></param>
  69. /// <param name="e"></param>
  70. void StartGameMenuEntrySelected(object sender, EventArgs e)
  71. {
  72. foreach (GameScreen screen in ScreenManager.GetScreens())
  73. screen.ExitScreen();
  74. // Don't add BackgroundScreen - we don't want the logo on the session browser
  75. ScreenManager.AddScreen(new SessionBrowserScreen(), null);
  76. }
  77. /// <summary>
  78. /// Respond to "Settings" Item Selection
  79. /// </summary>
  80. /// <param name="sender"></param>
  81. /// <param name="e"></param>
  82. void SettingsMenuEntrySelected(object sender, EventArgs e)
  83. {
  84. ScreenManager.AddScreen(new SettingsScreen(), null);
  85. }
  86. /// <summary>
  87. /// Respond to "Exit" Item Selection
  88. /// </summary>
  89. /// <param name="playerIndex"></param>
  90. protected override void OnCancel(PlayerIndex playerIndex)
  91. {
  92. ScreenManager.Game.Exit();
  93. }
  94. }
  95. }