Implementing the Quit Screen


You've already designed two user interface screens in earlier chapters, so this part will be a cakewalk to you, but you must implement this last screen. While the game is being played, you want the user to be asked whether she is sure that she wants to quit if she presses the Escape key. If the user accidentally presses the key, you won't lose all the hard work on the level. Go back to your UiScreen.cs code file and add the class in Listing 9.9.

Listing 9.9. The Quit User Screen
 public class QuitScreen : UiScreen, IDisposable {     private Texture buttonTextures = null;     private Texture messageTexture = null;     private UiButton yesButton = null;     private UiButton noButton = null;     // Events     public event EventHandler QuitGame;     public event EventHandler Cancel;     /// <summary>     /// Create the quit screen     /// </summary>     public QuitScreen(Device device, Texture buttons, int width, int height)         : base(device, width, height)     {         // Create the textures for the buttons         buttonTextures = buttons;         // Create the texture for the background         messageTexture = TextureLoader.FromFile(device, GameEngine.MediaPath                                                 + "QuitConfirm.png");         StoreTexture(messageTexture, width, height, true);         // Calculate the spacing for the buttons         float horizSpacing = (float)(width - (SmallButtonWidth * 3))             / 2.0f;         // Create the yes/no buttons         yesButton = new UiButton(renderSprite, buttonTextures, buttonTextures,             new Rectangle(SmallButtonWidth,SmallButtonHeight * 3,             SmallButtonWidth,SmallButtonHeight),             new Rectangle(0,SmallButtonHeight * 3,             SmallButtonWidth,SmallButtonHeight),             new Point((int)horizSpacing,             (int)(centerUpper.Y + backgroundSource.Height)             - (SmallButtonHeight * 2)));         yesButton.Click += new EventHandler(OnYesButton);         noButton = new UiButton(renderSprite, buttonTextures, buttonTextures,             new Rectangle(SmallButtonWidth,SmallButtonHeight * 2,             SmallButtonWidth,SmallButtonHeight),             new Rectangle(0,SmallButtonHeight * 2,             SmallButtonWidth,SmallButtonHeight),             new Point(width - ((int)horizSpacing + SmallButtonWidth),             (int)(centerUpper.Y + backgroundSource.Height)             - (SmallButtonHeight * 2)));         noButton.Click += new EventHandler(OnNoButton);     } } 

The variables needed here are similar to what you used in the Select Loopy screen, with one button less. You also have events for each of the buttons that can be clicked. In the constructor, the texture for the buttons is stored, and a new texture for the background is created and stored. You create the buttons and calculate the position to center the buttons inside the background texture. The event handlers used to handle the button clicks simply fire the associated event for this class:

 private void OnYesButton(object sender, EventArgs e) {     if (QuitGame != null)         QuitGame(this, EventArgs.Empty); } private void OnNoButton(object sender, EventArgs e) {     if (Cancel != null)         Cancel(this, EventArgs.Empty); } 

Obviously, you still need to add the cleanup code for the quit screen, which appears in Listing 9.10.

Listing 9.10. Cleaning Up the Quit User Screen
 /// <summary> /// Clean up in case dispose isn't called /// </summary> ~QuitScreen() {     Dispose(); } /// <summary> /// Clean up any resources /// </summary> public override void Dispose() {     GC.SuppressFinalize(this);     if (messageTexture != null)     {         messageTexture.Dispose();     }     messageTexture = null;     buttonTextures = null;     base.Dispose(); } 

This code is the same as that for the Select Loopy screen. The last thing you need for your quit screen is the rendering method and the user input handlers. You find these methods in Listing 9.11.

Listing 9.11. Finishing Up the Quit User Screen
 public override void Draw() {     // Start rendering sprites     base.BeginSprite();     // Draw the background     base.Draw();     // Now the buttons     yesButton.Draw();     noButton.Draw();     // You're done rendering sprites     base.EndSprite(); } public void OnMouseMove(int x, int y) {     yesButton.OnMouseMove(x, y);     noButton.OnMouseMove(x, y); } public void OnMouseClick(int x, int y) {     yesButton.OnMouseClick(x, y);     noButton.OnMouseClick(x, y); } public void OnKeyPress(System.Windows.Forms.Keys key) {     switch(key)     {         case Keys.Escape:         case Keys.N:             // Escape will dismiss the dialog             OnNoButton(this, EventArgs.Empty);             break;         case Keys.Enter:         case Keys.Y:             // Same as pressing the Yes Button             OnYesButton(this, EventArgs.Empty);             break;     } } 

Rendering is accomplished as it was in the other user screens: the sprite is begun (with alpha blending because the background of the dialog is transparent), the background is drawn, the buttons are drawn, and the sprite is ended. The mouse input is simply passed directly to the buttons, and the keyboard input is mapped into the appropriate button.

You can see the quit screen in action in Figure 9.1.

Figure 9.1. Do you really want to quit?




Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net