Time for action - adding event handlers

  1. Add the following directive to the Game1 class of the Level Editor project:
    using Tile_Engine;
    
  2. Add the following declaration to the declarations area of the Game1 class of the Level Editor project:
    System.Windows.Forms.Control gameForm;
    
  3. Add the following code to the constructor of the Game1 class:
    gameForm =
    System.Windows.Forms.Control.FromHandle(this.Window.Handle);
    gameForm.VisibleChanged +=
    new EventHandler(gameForm_VisibleChanged);
    gameForm.SizeChanged +=
    new EventHandler(pictureBox_SizeChanged);
    
  4. Add the two event handlers to the Game1 class:
    private void gameForm_VisibleChanged(object sender, EventArgs e)
    {
    if (gameForm.Visible == true)
    gameForm.Visible = false;
    }
    void pictureBox_SizeChanged(object sender, EventArgs e)
    {
    if (parentForm.WindowState !=
    System.Windows.Forms.FormWindowState.Minimized)
    {
    graphics.PreferredBackBufferWidth = pictureBox.Width;
    graphics.PreferredBackBufferHeight = pictureBox.Height;
    Camera.ViewPortWidth = pictureBox.Width;
    Camera.ViewPortHeight = pictureBox.Height;
    graphics.ApplyChanges();
    }
    }
    
  5. Execute the application.
  6. To end the application, you will need to return to the Visual Studio interface and select Stop Debugging from the Debug menu.

What just happened?

At first, the gameForm_VisibleChanged() method may look odd. It may seem that it would prevent the game from ever being displayed, as it sets the gameForm.Visible property to false if it ever ends up as true.

Remember though, that the form (or window) that is automatically generated by XNA will always be empty. Our game's display is now being redirected to the PictureBox on our MapEditor form. This means that we really do want to make sure that the game's form is never visible. Whenever its visibility changes, we ensure that Visible is set to false to keep it from appearing.

When the size of the PictureBox changes since it is anchored to the sides of the MapEditor form, resizing the form will resize the PictureBox we want to update the GraphicsDeviceManager with the new size of our display area, and pass those updates along to the Camera class. We need to be careful to check the WindowState of the parent form when processing the resize event. Because the back-buffer width and height must be greater than zero, if we attempt to set them when the form has been minimized, the application will crash.

Clicking on the window close button or pressing Alt + F4 to end the application will no longer work, because the hidden window that Game1 would normally run in will not close automatically.

Filling Out Our Form

Right now, we just have the familiar blue XNA window being displayed on our form. We need to add a number of other controls to the form to build a functional level editor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.218.215