- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/StateManager/MenuEntry.cs

    00001 //-----------------------------------------------------------------------------
    00002 // MenuEntry.cs
    00003 // (Based on XNA Community Game Platform Demo, Microsoft Corp., 2008)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.StateManager
    00007 {
    00008     using System;
    00009     using Microsoft.Xna.Framework;
    00010     using Microsoft.Xna.Framework.Graphics;
    00011     using NewGamePhysics.Utilities;
    00012
    00019     public class MenuEntry
    00020     {
    00021         #region Fields
    00022
    00026         private string text;
    00027
    00034         private float selectionFade;
    00035
    00039         private Texture2D paneTexture;
    00040
    00041         #endregion
    00042
    00043         #region Properties
    00044
    00048         public string Text
    00049         {
    00050             get { return text; }
    00051             set { text = value; }
    00052         }
    00053
    00054         #endregion
    00055
    00056         #region Events
    00057
    00061         public event EventHandler<PlayerIndexEventArgs> Selected;
    00062
    00066         protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
    00067         {
    00068             if (Selected != null)
    00069             {
    00070                 Selected(this, new PlayerIndexEventArgs(playerIndex));
    00071             }
    00072         }
    00073
    00074
    00075         #endregion
    00076
    00077         #region Initialization
    00078
    00079
    00083         public MenuEntry(string text)
    00084         {
    00085             this.text = text;
    00086         }
    00087
    00088
    00089         #endregion
    00090
    00091         #region Update and Draw
    00092
    00093
    00097         public virtual void Update(MenuScreen screen, bool isSelected,
    00098                                                       GameTime gameTime)
    00099         {
    00100             // When the menu selection changes, entries gradually fade between
    00101             // their selected and deselected appearance, rather than instantly
    00102             // popping to the new state.
    00103             float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
    00104
    00105             if (isSelected)
    00106             {
    00107                 selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
    00108             }
    00109             else
    00110             {
    00111                 selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
    00112             }
    00113         }
    00114
    00115
    00119         public virtual void Draw(MenuScreen screen, Vector2 position,
    00120                                  bool isSelected, GameTime gameTime)
    00121         {
    00122             // Pulsate the size of the selected menu entry.
    00123             double time = gameTime.TotalGameTime.TotalSeconds;
    00124
    00125             float pulsate = (float)Math.Sin(time * 6) + 1;
    00126
    00127             float scale = 1 + pulsate * 0.05f * selectionFade;
    00128
    00129             // Draw the selected entry in yellow, otherwise white.
    00130             Color textColor = isSelected ? Color.Yellow : Color.White;
    00131             Color paneColor = isSelected ? Color.Yellow : Color.White;
    00132
    00133             // Modify the alpha to fade text out during transitions.
    00134             textColor = new Color(
    00135                 textColor.R,
    00136                 textColor.G,
    00137                 textColor.B,
    00138                 screen.TransitionAlpha);
    00139
    00140             // Modify pane color to be darker
    00141             paneColor = new Color(
    00142                 paneColor.R / 64,
    00143                 paneColor.G / 64,
    00144                 paneColor.B / 64);
    00145
    00146             // Create shadow color
    00147             Color color2 = new Color(
    00148                 Color.DarkSlateBlue.R,
    00149                 Color.DarkSlateBlue.G,
    00150                 Color.DarkSlateBlue.B,
    00151                 screen.TransitionAlpha);
    00152
    00153             // Draw text, centered on the middle of each line.
    00154             ScreenManager screenManager = screen.ScreenManager;
    00155             SpriteBatch spriteBatch = screenManager.SpriteBatch;
    00156             SpriteFont spriteFont = screenManager.Fonts["menu"];
    00157
    00158             Vector2 origin = new Vector2(0, spriteFont.LineSpacing / 2);
    00159
    00160             // Create translucent pane rectangle
    00161             if (this.paneTexture == null)
    00162             {
    00163                 // New background texture
    00164                 this.paneTexture = TextureHelpers.Create(screenManager.GraphicsDevice, new Color(64, 64, 64));
    00165             }
    00166
    00167             Vector2 textSize = spriteFont.MeasureString(text);
    00168             Viewport viewport = screenManager.GraphicsDevice.Viewport;
    00169             Rectangle backgroundPane =
    00170                 new Rectangle(
    00171                     (int)position.X - 15,
    00172                     (int)(position.Y - origin.Y + 2),
    00173                     (int)((viewport.Width / 2) * 1.25f),
    00174                     screenManager.Fonts["menu"].LineSpacing - 2);
    00175             spriteBatch.Draw(this.paneTexture, backgroundPane, new Color(paneColor, 128));
    00176             if (isSelected)
    00177             {
    00178                 backgroundPane.X -= 20;
    00179                 backgroundPane.Width = 20;
    00180                 spriteBatch.Draw(this.paneTexture, backgroundPane, new Color(paneColor, 200));
    00181             }
    00182
    00183             Vector2 shadowPosition;
    00184             Vector2 shadowOffset = new Vector2(2.0f, 2.0f);
    00185             shadowPosition = position + shadowOffset;
    00186
    00187             spriteBatch.DrawString(spriteFont, text, shadowPosition, color2, 0,
    00188                        origin, scale, SpriteEffects.None, 0);
    00189
    00190             spriteBatch.DrawString(spriteFont, text, position, textColor, 0,
    00191                                    origin, scale, SpriteEffects.None, 0);
    00192         }
    00193
    00194
    00198         public virtual int GetHeight(MenuScreen screen)
    00199         {
    00200             return screen.ScreenManager.Fonts["menu"].LineSpacing;
    00201         }
    00202
    00203
    00204         #endregion
    00205     }
    00206 }