- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/StateManager/GameScreen.cs

    00001 //-----------------------------------------------------------------------------
    00002 // GameScreen.cs
    00003 // (Based on XNA Community Game Platform Demo, Microsoft Corp., 2008)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.StateManager
    00007 {
    00008     using System;
    00009     using Microsoft.Xna.Framework;
    00010
    00014     public enum ScreenState
    00015     {
    00019         TransitionOn,
    00020
    00024         Active,
    00025
    00029         TransitionOff,
    00030
    00034         Hidden,
    00035     }
    00036
    00044     public abstract class GameScreen
    00045     {
    00049         private ScreenManager screenManager;
    00050
    00054         PlayerIndex? controllingPlayer;
    00055
    00060         private TimeSpan transitionOnTime = TimeSpan.Zero;
    00061
    00066         private TimeSpan transitionOffTime = TimeSpan.Zero;
    00067
    00071         private bool isPopup = false;
    00072
    00078         private float transitionPosition = 1;
    00079
    00083         private bool otherScreenHasFocus;
    00084
    00088         private ScreenState screenState = ScreenState.TransitionOn;
    00089
    00095         bool isExiting = false;
    00096
    00097         #region Properties
    00098
    00107         public bool IsPopup
    00108         {
    00109             get { return isPopup; }
    00110             protected set { isPopup = value; }
    00111         }
    00112
    00118         public TimeSpan TransitionOnTime
    00119         {
    00120             get { return transitionOnTime; }
    00121             protected set { transitionOnTime = value; }
    00122         }
    00123
    00129         public TimeSpan TransitionOffTime
    00130         {
    00131             get { return this.transitionOffTime; }
    00132             protected set { this.transitionOffTime = value; }
    00133         }
    00134
    00140         public float TransitionPosition
    00141         {
    00142             get { return this.transitionPosition; }
    00143             protected set { this.transitionPosition = value; }
    00144         }
    00145
    00151         public byte TransitionAlpha
    00152         {
    00153             get { return (byte)(255 - this.TransitionPosition * 255); }
    00154         }
    00155
    00159         public ScreenState ScreenState
    00160         {
    00161             get { return this.screenState; }
    00162             protected set { this.screenState = value; }
    00163         }
    00164
    00173         public bool IsExiting
    00174         {
    00175             get { return this.isExiting; }
    00176             protected internal set { this.isExiting = value; }
    00177         }
    00178
    00182         public bool IsActive
    00183         {
    00184             get
    00185             {
    00186                 return !this.otherScreenHasFocus &&
    00187                        (this.screenState == ScreenState.TransitionOn ||
    00188                         this.screenState == ScreenState.Active);
    00189             }
    00190         }
    00191
    00195         public ScreenManager ScreenManager
    00196         {
    00197             get { return this.screenManager; }
    00198             internal set { this.screenManager = value; }
    00199         }
    00200
    00209         public PlayerIndex? ControllingPlayer
    00210         {
    00211             get { return controllingPlayer; }
    00212             internal set { controllingPlayer = value; }
    00213         }
    00214
    00215         #endregion
    00216
    00217         #region Initialization
    00218
    00222         public virtual void LoadContent() { }
    00223
    00227         public virtual void UnloadContent() { }
    00228
    00229         #endregion
    00230
    00231         #region UpdateAndDraw
    00232
    00238         public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
    00239                                                       bool coveredByOtherScreen)
    00240         {
    00241             this.otherScreenHasFocus = otherScreenHasFocus;
    00242
    00243             if (isExiting)
    00244             {
    00245                 // If the screen is going away to die, it should transition off.
    00246                 screenState = ScreenState.TransitionOff;
    00247
    00248                 if (!UpdateTransition(gameTime, transitionOffTime, 1))
    00249                 {
    00250                     // When the transition finishes, remove the screen.
    00251                     ScreenManager.RemoveScreen(this);
    00252                 }
    00253             }
    00254             else if (coveredByOtherScreen)
    00255             {
    00256                 // If the screen is covered by another, it should transition off.
    00257                 if (UpdateTransition(gameTime, transitionOffTime, 1))
    00258                 {
    00259                     // Still busy transitioning.
    00260                     screenState = ScreenState.TransitionOff;
    00261                 }
    00262                 else
    00263                 {
    00264                     // Transition finished!
    00265                     screenState = ScreenState.Hidden;
    00266                 }
    00267             }
    00268             else
    00269             {
    00270                 // Otherwise the screen should transition on and become active.
    00271                 if (UpdateTransition(gameTime, transitionOnTime, -1))
    00272                 {
    00273                     // Still busy transitioning.
    00274                     screenState = ScreenState.TransitionOn;
    00275                 }
    00276                 else
    00277                 {
    00278                     // Transition finished!
    00279                     screenState = ScreenState.Active;
    00280                 }
    00281             }
    00282         }
    00283
    00287         bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
    00288         {
    00289             // How much should we move by?
    00290             float transitionDelta;
    00291
    00292             if (time == TimeSpan.Zero)
    00293             {
    00294                 transitionDelta = 1;
    00295             }
    00296             else
    00297             {
    00298                 transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
    00299                                           time.TotalMilliseconds);
    00300             }
    00301
    00302             // Update the transition position.
    00303             transitionPosition += transitionDelta * direction;
    00304
    00305             // Did we reach the end of the transition?
    00306             if (((direction < 0) && (transitionPosition <= 0)) ||
    00307                 ((direction > 0) && (transitionPosition >= 1)))
    00308             {
    00309                 transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
    00310                 return false;
    00311             }
    00312
    00313             // Otherwise we are still busy transitioning.
    00314             return true;
    00315         }
    00316
    00322         public virtual void HandleInput(InputState input) { }
    00323
    00327         public virtual void Draw(GameTime gameTime) { }
    00328
    00329         #endregion
    00330
    00331         #region Public Methods
    00332
    00338         public void ExitScreen()
    00339         {
    00340             if (TransitionOffTime == TimeSpan.Zero)
    00341             {
    00342                 // If the screen has a zero transition time, remove it immediately.
    00343                 ScreenManager.RemoveScreen(this);
    00344             }
    00345             else
    00346             {
    00347                 // Otherwise flag that it should transition off and then exit.
    00348                 isExiting = true;
    00349             }
    00350         }
    00351
    00352         #endregion
    00353     }
    00354 }