Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #region File Description //----------------------------------------------------------------------------- // MessageBoxScreen.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using GameStateManagement; #endregion namespace GameStateManagement { /// <summary> /// A popup message box screen, used to display "are you sure?" /// confirmation messages. /// </summary> class MessageBoxScreen : GameScreen { #region Fields string message; Texture2D gradientTexture; InputAction menuSelect; InputAction menuCancel; #endregion #region Events public event EventHandler<PlayerIndexEventArgs> Accepted; public event EventHandler<PlayerIndexEventArgs> Cancelled; #endregion #region Initialization /// <summary> /// Constructor automatically includes the standard "A=ok, B=cancel" /// usage text prompt. /// </summary> public MessageBoxScreen( string message) : this (message, true ) { } /// <summary> /// Constructor lets the caller specify whether to include the standard /// "A=ok, B=cancel" usage text prompt. /// </summary> public MessageBoxScreen( string message, bool includeUsageText) { const string usageText = "\nA button, Space, Enter = ok" + "\nB button, Esc = cancel" ; if (includeUsageText) this .message = message + usageText; else this .message = message; IsPopup = true ; TransitionOnTime = TimeSpan.FromSeconds(0.2); TransitionOffTime = TimeSpan.FromSeconds(0.2); menuSelect = new InputAction( new Buttons[] { Buttons.A, Buttons.Start }, new Keys[] { Keys.Space, Keys.Enter }, true ); menuCancel = new InputAction( new Buttons[] { Buttons.B, Buttons.Back }, new Keys[] { Keys.Escape, Keys.Back }, true ); } /// <summary> /// Loads graphics content for this screen. This uses the shared ContentManager /// provided by the Game class, so the content will remain loaded forever. /// Whenever a subsequent MessageBoxScreen tries to load this same content, /// it will just get back another reference to the already loaded data. /// </summary> public override void Activate( bool instancePreserved) { if (!instancePreserved) { ContentManager content = ScreenManager.Game.Content; gradientTexture = content.Load<Texture2D>( "gradient" ); } } #endregion #region Handle Input /// <summary> /// Responds to user input, accepting or cancelling the message box. /// </summary> public override void HandleInput(GameTime gameTime, InputState input) { PlayerIndex playerIndex; // We pass in our ControllingPlayer, which may either be null (to // accept input from any player) or a specific index. If we pass a null // controlling player, the InputState helper returns to us which player // actually provided the input. We pass that through to our Accepted and // Cancelled events, so they can tell which player triggered them. if (menuSelect.Evaluate(input, ControllingPlayer, out playerIndex)) { // Raise the accepted event, then exit the message box. if (Accepted != null ) Accepted( this , new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } else if (menuCancel.Evaluate(input, ControllingPlayer, out playerIndex)) { // Raise the cancelled event, then exit the message box. if (Cancelled != null ) Cancelled( this , new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } } #endregion #region Draw /// <summary> /// Draws the message box. /// </summary> public override void Draw(GameTime gameTime) { SpriteBatch spriteBatch = ScreenManager.SpriteBatch; SpriteFont font = ScreenManager.Font; // Darken down any other screens that were drawn beneath the popup. ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3); // Center the message text in the viewport. Viewport viewport = ScreenManager.GraphicsDevice.Viewport; Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height); Vector2 textSize = font.MeasureString(message); Vector2 textPosition = (viewportSize - textSize) / 2; // The background includes a border somewhat larger than the text itself. const int hPad = 32; const int vPad = 16; Rectangle backgroundRectangle = new Rectangle(( int )textPosition.X - hPad, ( int )textPosition.Y - vPad, ( int )textSize.X + hPad * 2, ( int )textSize.Y + vPad * 2); // Fade the popup alpha during transitions. Color color = Color.White * TransitionAlpha; spriteBatch.Begin(); // Draw the background rectangle. spriteBatch.Draw(gradientTexture, backgroundRectangle, color); // Draw the message box text. spriteBatch.DrawString(font, message, textPosition, color); spriteBatch.End(); } #endregion } } |
Source at commit c7acc8b3f8c9 created 12 years 7 months ago. By Nathan Adams, Adding region section for changelog |
---|