//----------------------------------------------------------------------------- // MenuScreen.cs // // XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input.Touch; using Microsoft.Xna.Framework.Input; using Blackjack; using CardsFramework; namespace GameStateManagement { /// /// Base class for screens that contain a menu of options. The user can /// move up and down to select an entry, or cancel to back out of the screen. /// abstract class MenuScreen : GameScreen { // the number of pixels to pad above and below menu entries for touch input const int menuEntryPadding = 35; List menuEntries = new List(); int selectedEntry = 0; string menuTitle; Rectangle bounds; private bool isMouseDown = false; /// /// Gets the list of menu entries, so derived classes can add /// or change the menu contents. /// protected IList MenuEntries { get { return menuEntries; } } /// /// Constructor. /// public MenuScreen(string menuTitle) { // menus generally only need Tap for menu selection EnabledGestures = GestureType.Tap; this.menuTitle = menuTitle; TransitionOnTime = TimeSpan.FromSeconds(0.5); TransitionOffTime = TimeSpan.FromSeconds(0.5); } /// /// Allows the screen to create the hit bounds for a particular menu entry. /// protected virtual Rectangle GetMenuEntryHitBounds(MenuEntry entry) { // the hit bounds are the entire width of the screen, and the height of the entry // with some additional padding above and below. return new Rectangle( 0, (int)entry.Destination.Y - menuEntryPadding, ScreenManager.GraphicsDevice.Viewport.Width, entry.GetHeight(this) + (menuEntryPadding * 2)); } /// /// Responds to user input, changing the selected entry and accepting /// or cancelling the menu. /// public override void HandleInput(InputState input) { // Cancel the current menu screen if the user presses the back button PlayerIndex player; if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player)) { OnCancel(player); } if (UIUtility.IsDesktop) { // Handle keyboard input if (input.IsMenuUp(ControllingPlayer)) { selectedEntry--; if (selectedEntry < 0) selectedEntry = menuEntries.Count - 1; } else if (input.IsMenuDown(ControllingPlayer)) { selectedEntry++; if (selectedEntry >= menuEntries.Count) selectedEntry = 0; } else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) || input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player)) { OnSelectEntry(selectedEntry, player); } // Handle mouse input using InputState if (input.CurrentMouseState.LeftButton == ButtonState.Released) { if (isMouseDown) { isMouseDown = false; Point clickLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y); for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; if (menuEntry.Destination.Contains(clickLocation)) { OnSelectEntry(i, PlayerIndex.One); } } } } else if (input.CurrentMouseState.LeftButton == ButtonState.Pressed) { isMouseDown = true; Point clickLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y); for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; if (menuEntry.Destination.Contains(clickLocation)) selectedEntry = i; } } } else if (UIUtility.IsMobile) { // Handle touch input foreach (GestureSample gesture in input.Gestures) { if (gesture.GestureType == GestureType.Tap) { Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y); for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; if (menuEntry.Destination.Contains(tapLocation)) { OnSelectEntry(i, PlayerIndex.One); } } } } } else { // Handle gamepad input if (input.IsMenuUp(ControllingPlayer)) { selectedEntry--; if (selectedEntry < 0) selectedEntry = menuEntries.Count - 1; } else if (input.IsMenuDown(ControllingPlayer)) { selectedEntry++; if (selectedEntry >= menuEntries.Count) selectedEntry = 0; } else if (input.IsNewButtonPress(Buttons.A, ControllingPlayer, out player)) { OnSelectEntry(selectedEntry, player); } } } /// /// Handler for when the user has chosen a menu entry. /// protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex) { menuEntries[entryIndex].OnSelectEntry(playerIndex); } /// /// Handler for when the user has cancelled the menu. /// protected virtual void OnCancel(PlayerIndex playerIndex) { ExitScreen(); } /// /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler. /// protected void OnCancel(object sender, PlayerIndexEventArgs e) { OnCancel(e.PlayerIndex); } public override void LoadContent() { bounds = ScreenManager.SafeArea; base.LoadContent(); } /// /// Allows the screen the chance to position the menu entries. By default /// all menu entries are lined up in a vertical list, centered on the screen. /// protected virtual void UpdateMenuEntryLocations() { // Make the menu slide into place during transitions, using a // power curve to make things look more interesting (this makes // the movement slow down as it nears the end). float transitionOffset = (float)Math.Pow(TransitionPosition, 2); // start at Y = 475; each X value is generated per entry Vector2 position = new Vector2(0f, ScreenManager.Game.Window.ClientBounds.Height / 2 - (menuEntries[0].GetHeight(this) + (menuEntryPadding * 2) * menuEntries.Count)); // update each menu entry's location in turn for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; // each entry is to be centered horizontally position.X = ScreenManager.BASE_BUFFER_WIDTH / 2 - menuEntry.GetWidth(this) / 2; if (ScreenState == ScreenState.TransitionOn) position.X -= transitionOffset * 256; else position.X += transitionOffset * 512; // set the entry's position //menuEntry.Position = position; // move down for the next entry the size of this entry plus our padding position.Y += menuEntry.GetHeight(this) + (menuEntryPadding * 2); } } /// /// Updates the menu. /// public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); // Update each nested MenuEntry object. for (int i = 0; i < menuEntries.Count; i++) { bool isSelected = IsActive && (i == selectedEntry); UpdateMenuEntryDestination(); menuEntries[i].Update(this, isSelected, gameTime); } } /// /// Draws the menu. /// public override void Draw(GameTime gameTime) { // make sure our entries are in the right place before we draw them //UpdateMenuEntryLocations(); GraphicsDevice graphics = ScreenManager.GraphicsDevice; SpriteBatch spriteBatch = ScreenManager.SpriteBatch; SpriteFont font = ScreenManager.Font; spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation); // Draw each menu entry in turn. for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; bool isSelected = IsActive && (i == selectedEntry); menuEntry.Draw(this, isSelected, gameTime); } // Make the menu slide into place during transitions, using a // power curve to make things look more interesting (this makes // the movement slow down as it nears the end). float transitionOffset = (float)Math.Pow(TransitionPosition, 2); // Draw the menu title centered on the screen Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 375); Vector2 titleOrigin = font.MeasureString(menuTitle) / 2; Color titleColor = new Color(192, 192, 192) * TransitionAlpha; float titleScale = 1.25f; titlePosition.Y -= transitionOffset * 100; spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0, titleOrigin, titleScale, SpriteEffects.None, 0); spriteBatch.End(); } public void UpdateMenuEntryDestination() { Rectangle bounds = ScreenManager.SafeArea; Rectangle textureSize = ScreenManager.ButtonBackground.Bounds; int xStep = bounds.Width / (menuEntries.Count + 2); int maxWidth = 0; for (int i = 0; i < menuEntries.Count; i++) { int width = menuEntries[i].GetWidth(this); if (width > maxWidth) { maxWidth = width; } } maxWidth += 20; for (int i = 0; i < menuEntries.Count; i++) { menuEntries[i].Destination = new Rectangle( bounds.Left + (xStep - textureSize.Width) / 2 + (i + 1) * xStep, bounds.Bottom - textureSize.Height * 2, maxWidth, 50); } } } }