- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/GravityChooser/Screens/GravityChooserMessageBoxScreen.cs

    00001 #region File Description
    00002 //-----------------------------------------------------------------------------
    00003 // GravityChooserMessageBoxScreen.cs
    00004 // (A. Schiffler, 2009-2011)
    00005 //-----------------------------------------------------------------------------
    00006 #endregion
    00007
    00008 namespace GravityChooser
    00009 {
    00010     using System;
    00011     using Microsoft.Xna.Framework;
    00012     using Microsoft.Xna.Framework.Content;
    00013     using Microsoft.Xna.Framework.Graphics;
    00014     using NewGamePhysics.StateManager;
    00015
    00020     class GravityChooserMessageBoxScreen : GameScreen
    00021     {
    00022         #region Fields
    00023
    00027         string message;
    00028
    00032         Texture2D gradientTexture;
    00033
    00034         #endregion
    00035
    00036         #region Events
    00037
    00041         public event EventHandler<PlayerIndexEventArgs> Accepted;
    00042
    00046         public event EventHandler<PlayerIndexEventArgs> Cancelled;
    00047
    00048         #endregion
    00049
    00050         #region Initialization
    00051
    00056         public GravityChooserMessageBoxScreen(string message)
    00057             : this(message, true)
    00058         { }
    00059
    00064         public GravityChooserMessageBoxScreen(string message, bool includeUsageText)
    00065         {
    00066             const string usageText = "\nOK      : (A) button or [Enter]" +
    00067                                      "\nCancel  : (B) button or [Esc]";
    00068
    00069             if (includeUsageText)
    00070             {
    00071                 this.message = message + usageText;
    00072             }
    00073             else
    00074             {
    00075                 this.message = message;
    00076             }
    00077
    00078             IsPopup = true;
    00079
    00080             TransitionOnTime = TimeSpan.FromSeconds(0.2);
    00081             TransitionOffTime = TimeSpan.FromSeconds(0.2);
    00082         }
    00083
    00084
    00088         public override void LoadContent()
    00089         {
    00090             ContentManager content = ScreenManager.Game.Content;
    00091
    00092             gradientTexture = content.Load<Texture2D>(@"Sprites\gradient");
    00093         }
    00094
    00095         #endregion
    00096
    00097         #region Handle Input
    00098
    00102         public override void HandleInput(InputState input)
    00103         {
    00104             PlayerIndex playerIndex;
    00105
    00106             // We pass in our ControllingPlayer, which may either be null (to
    00107             // accept input from any player) or a specific index. If we pass a null
    00108             // controlling player, the InputState helper returns to us which player
    00109             // actually provided the input. We pass that through to our Accepted and
    00110             // Cancelled events, so they can tell which player triggered them.
    00111             if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
    00112             {
    00113                 // Raise the accepted event, then exit the message box.
    00114                 if (Accepted != null)
    00115                     Accepted(this, new PlayerIndexEventArgs(playerIndex));
    00116
    00117                 ExitScreen();
    00118             }
    00119             else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
    00120             {
    00121                 // Raise the cancelled event, then exit the message box.
    00122                 if (Cancelled != null)
    00123                     Cancelled(this, new PlayerIndexEventArgs(playerIndex));
    00124
    00125                 ExitScreen();
    00126             }
    00127         }
    00128
    00129
    00130         #endregion
    00131
    00132         #region Draw
    00133
    00137         public override void Draw(GameTime gameTime)
    00138         {
    00139             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
    00140             SpriteFont font = ScreenManager.Fonts["menu"];
    00141
    00142             // Darken down any other screens that were drawn beneath the popup.
    00143             ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
    00144
    00145             // Center the message text in the viewport.
    00146             Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
    00147             Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
    00148             Vector2 textSize = font.MeasureString(message);
    00149             Vector2 textPosition = (viewportSize - textSize) / 2;
    00150
    00151             // The background includes a border somewhat larger than the text itself.
    00152             const int hPad = 32;
    00153             const int vPad = 16;
    00154
    00155             Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
    00156                                                           (int)textPosition.Y - vPad,
    00157                                                           (int)textSize.X + hPad * 2,
    00158                                                           (int)textSize.Y + vPad * 2);
    00159
    00160             // Fade the popup alpha during transitions.
    00161             Color color = new Color(255, 255, 255, TransitionAlpha);
    00162
    00163             spriteBatch.Begin();
    00164
    00165             // Draw the background rectangle.
    00166             spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
    00167
    00168             // Draw the message box text.
    00169             spriteBatch.DrawString(font, message, textPosition, color);
    00170
    00171             spriteBatch.End();
    00172         }
    00173
    00174         #endregion
    00175     }
    00176 }