- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/PendulumGame/Screens/PendulumMessageBoxScreen.cs

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