123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #region File Description
- //-----------------------------------------------------------------------------
- // MenuScreen.cs
- //
- // XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- 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;
- #endregion
- namespace HoneycombRush
- {
- /// <summary>
- /// 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.
- /// </summary>
- abstract class MenuScreen : GameScreen
- {
- #region Fields
- // the number of pixels to pad above and below menu entries for touch input
- const int menuEntryPadding = 35;
- List<MenuEntry> menuEntries = new List<MenuEntry>();
- int selectedEntry = 0;
- string menuTitle;
- #endregion
- #region Properties
- /// <summary>
- /// Gets the list of menu entries, so derived classes can add
- /// or change the menu contents.
- /// </summary>
- protected IList<MenuEntry> MenuEntries
- {
- get { return menuEntries; }
- }
- #endregion
- #region Initialization
- /// <summary>
- /// Constructor.
- /// </summary>
- 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);
- }
- #endregion
- #region Handle Input
- /// <summary>
- /// Allows the screen to create the hit bounds for a particular menu entry.
- /// </summary>
- protected virtual Rectangle GetMenuEntryHitBounds(MenuEntry entry)
- {
- return entry.Bounds;
- }
- /// <summary>
- /// Responds to user input, changing the selected entry and accepting
- /// or cancelling the menu.
- /// </summary>
- public override void HandleInput(GameTime gameTime, InputState input)
- {
- // we cancel the current menu screen if the user presses the back button
- PlayerIndex player;
- if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
- {
- OnCancel(player);
- }
- // Take care of 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) ||
- input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player) )
- {
- OnSelectEntry(selectedEntry, player);
- }
- // look for any taps that occurred and select any entries that were tapped
- foreach (GestureSample gesture in input.Gestures)
- {
- if (gesture.GestureType == GestureType.Tap)
- {
- // convert the position to a Point that we can test against a Rectangle
- Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
- // iterate the entries to see if any were tapped
- for (int i = 0; i < menuEntries.Count; i++)
- {
- MenuEntry menuEntry = menuEntries[i];
- if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))
- {
- // Select the entry. since gestures are only available on Windows Phone,
- // we can safely pass PlayerIndex.One to all entries since there is only
- // one player on Windows Phone.
- OnSelectEntry(i, PlayerIndex.One);
- }
- }
- }
- }
- }
- /// <summary>
- /// Handler for when the user has chosen a menu entry.
- /// </summary>
- protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
- {
- menuEntries[entryIndex].OnSelectEntry(playerIndex);
- }
- /// <summary>
- /// Handler for when the user has cancelled the menu.
- /// </summary>
- protected virtual void OnCancel(PlayerIndex playerIndex)
- {
- ExitScreen();
- }
- /// <summary>
- /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
- /// </summary>
- protected void OnCancel(object sender, PlayerIndexEventArgs e)
- {
- OnCancel(e.PlayerIndex);
- }
- #endregion
- #region Update and Draw
- /// <summary>
- /// 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.
- /// </summary>
- 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.GraphicsDevice.Viewport.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);
- }
- }
- /// <summary>
- /// Updates the menu.
- /// </summary>
- 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);
- menuEntries[i].Update(this, isSelected, gameTime);
- }
- }
- /// <summary>
- /// Draws the menu.
- /// </summary>
- 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();
- // 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();
- }
- #endregion
- }
- }
|