Merge with axios-newgsm

master
nathan@daedalus 2012-04-28 22:04:40 -05:00
commit d68b7d5ad3
35 changed files with 3361 additions and 624 deletions

View File

@ -190,18 +190,32 @@
<Compile Include="PrimitiveBatch.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenSystem\BackgroundScreen.cs" />
<Compile Include="ScreenSystem\Button.cs" />
<Compile Include="ScreenSystem\Camera2D.cs" />
<Compile Include="ScreenSystem\ConvertUnits.cs" />
<Compile Include="ScreenSystem\FramerateCounterComponent.cs" />
<Compile Include="ScreenSystem\GameplayScreen.cs" />
<Compile Include="ScreenSystem\GameScreen.cs" />
<Compile Include="ScreenSystem\IDemoScreen.cs" />
<Compile Include="ScreenSystem\InputAction.cs" />
<Compile Include="ScreenSystem\InputHelper.cs" />
<Compile Include="ScreenSystem\InputState.cs" />
<Compile Include="ScreenSystem\IScreenFactory.cs" />
<Compile Include="ScreenSystem\LoadingScreen.cs" />
<Compile Include="ScreenSystem\LogoScreen.cs" />
<Compile Include="ScreenSystem\MainMenuScreen.cs" />
<Compile Include="ScreenSystem\MenuButton.cs" />
<Compile Include="ScreenSystem\MenuEntry.cs" />
<Compile Include="ScreenSystem\MenuScreen.cs" />
<Compile Include="ScreenSystem\MessageBoxScreen.cs" />
<Compile Include="ScreenSystem\OptionsMenuScreen.cs" />
<Compile Include="ScreenSystem\PauseMenuScreen.cs" />
<Compile Include="ScreenSystem\PhoneMainMenuScreen.cs" />
<Compile Include="ScreenSystem\PhoneMenuScreen.cs" />
<Compile Include="ScreenSystem\PhonePauseScreen.cs" />
<Compile Include="ScreenSystem\PhysicsGameScreen.cs" />
<Compile Include="ScreenSystem\PlayerIndexEventArgs.cs" />
<Compile Include="ScreenSystem\ScreenManager.cs" />
<Compile Include="ScreenSystem\ScreenManagerComponent.cs" />
<Compile Include="ScreenSystem\SpriteFonts.cs" />
<Compile Include="ScreenSystem\VirtualButton.cs" />
@ -221,6 +235,7 @@
<Reference Include="System.Core" />
<Reference Include="system.data.linq, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e, processorArchitecture=MSIL" />
<Reference Include="System.Net" />
<Reference Include="System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, processorArchitecture=MSIL" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
@ -229,7 +244,7 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" />
<PropertyGroup>
<PostBuildEvent>if not exist "$(TargetPath)" ..\..\Combined mkdir "$(TargetPath)" ..\..\Combined
<PostBuildEvent>if not exist "$(TargetDir)"..\..\Combined mkdir "$(TargetDir)"..\..\Combined
copy "$(TargetPath)" ..\..\Combined</PostBuildEvent>
</PropertyGroup>
<!--

View File

@ -234,19 +234,32 @@
<Compile Include="PrimitiveBatch.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenSystem\BackgroundScreen.cs" />
<Compile Include="ScreenSystem\Button.cs" />
<Compile Include="ScreenSystem\Camera2D.cs" />
<Compile Include="ScreenSystem\ConvertUnits.cs" />
<Compile Include="ScreenSystem\FramerateCounterComponent.cs" />
<Compile Include="ScreenSystem\GameplayScreen.cs" />
<Compile Include="ScreenSystem\GameScreen.cs" />
<Compile Include="ScreenSystem\IDemoScreen.cs" />
<Compile Include="ScreenSystem\InputHelper.cs" />
<Compile Include="ScreenSystem\InputAction.cs" />
<Compile Include="Engine\AxiosGameScreen.cs" />
<Compile Include="ScreenSystem\InputState.cs" />
<Compile Include="ScreenSystem\IScreenFactory.cs" />
<Compile Include="ScreenSystem\LoadingScreen.cs" />
<Compile Include="ScreenSystem\LogoScreen.cs" />
<Compile Include="ScreenSystem\MainMenuScreen.cs" />
<Compile Include="ScreenSystem\MenuButton.cs" />
<Compile Include="ScreenSystem\MenuEntry.cs" />
<Compile Include="ScreenSystem\MenuScreen.cs" />
<Compile Include="ScreenSystem\MessageBoxScreen.cs" />
<Compile Include="ScreenSystem\OptionsMenuScreen.cs" />
<Compile Include="ScreenSystem\PauseMenuScreen.cs" />
<Compile Include="ScreenSystem\PhoneMainMenuScreen.cs" />
<Compile Include="ScreenSystem\PhoneMenuScreen.cs" />
<Compile Include="ScreenSystem\PhonePauseScreen.cs" />
<Compile Include="ScreenSystem\PhysicsGameScreen.cs" />
<Compile Include="ScreenSystem\PlayerIndexEventArgs.cs" />
<Compile Include="ScreenSystem\ScreenManager.cs" />
<Compile Include="ScreenSystem\ScreenManagerComponent.cs" />
<Compile Include="ScreenSystem\SpriteFonts.cs" />
<Compile Include="ScreenSystem\VirtualButton.cs" />
@ -260,7 +273,7 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" />
<PropertyGroup>
<PostBuildEvent>if not exist "$(TargetPath)" ..\..\Combined mkdir "$(TargetPath)" ..\..\Combined
<PostBuildEvent>if not exist "$(TargeDir)"..\..\Combined mkdir "$(TargetDir)"..\..\Combined
copy "$(TargetPath)" ..\..\Combined</PostBuildEvent>
</PropertyGroup>
<!--

View File

@ -183,18 +183,31 @@
<Compile Include="PrimitiveBatch.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScreenSystem\BackgroundScreen.cs" />
<Compile Include="ScreenSystem\Button.cs" />
<Compile Include="ScreenSystem\Camera2D.cs" />
<Compile Include="ScreenSystem\ConvertUnits.cs" />
<Compile Include="ScreenSystem\FramerateCounterComponent.cs" />
<Compile Include="ScreenSystem\GameplayScreen.cs" />
<Compile Include="ScreenSystem\GameScreen.cs" />
<Compile Include="ScreenSystem\IDemoScreen.cs" />
<Compile Include="ScreenSystem\InputHelper.cs" />
<Compile Include="ScreenSystem\InputAction.cs" />
<Compile Include="ScreenSystem\InputState.cs" />
<Compile Include="ScreenSystem\IScreenFactory.cs" />
<Compile Include="ScreenSystem\LoadingScreen.cs" />
<Compile Include="ScreenSystem\LogoScreen.cs" />
<Compile Include="ScreenSystem\MainMenuScreen.cs" />
<Compile Include="ScreenSystem\MenuButton.cs" />
<Compile Include="ScreenSystem\MenuEntry.cs" />
<Compile Include="ScreenSystem\MenuScreen.cs" />
<Compile Include="ScreenSystem\MessageBoxScreen.cs" />
<Compile Include="ScreenSystem\OptionsMenuScreen.cs" />
<Compile Include="ScreenSystem\PauseMenuScreen.cs" />
<Compile Include="ScreenSystem\PhoneMainMenuScreen.cs" />
<Compile Include="ScreenSystem\PhoneMenuScreen.cs" />
<Compile Include="ScreenSystem\PhonePauseScreen.cs" />
<Compile Include="ScreenSystem\PhysicsGameScreen.cs" />
<Compile Include="ScreenSystem\PlayerIndexEventArgs.cs" />
<Compile Include="ScreenSystem\ScreenManager.cs" />
<Compile Include="ScreenSystem\ScreenManagerComponent.cs" />
<Compile Include="ScreenSystem\SpriteFonts.cs" />
<Compile Include="ScreenSystem\VirtualButton.cs" />
@ -224,7 +237,7 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" />
<PropertyGroup>
<PostBuildEvent>if not exist "$(TargetPath)" ..\..\Combined mkdir "$(TargetPath)" ..\..\Combined
<PostBuildEvent>if not exist "$(TargetDir)"..\..\Combined mkdir "$(TargetDir)"..\..\Combined
copy "$(TargetPath)" ..\..\Combined</PostBuildEvent>
</PropertyGroup>
<!--

View File

@ -1,5 +1,6 @@
using System;
using FarseerPhysics.SamplesFramework;
using GameStateManagement;
namespace Axios.Engine
{
@ -19,45 +20,45 @@ namespace Axios.Engine
}
}
public delegate void AxiosHandler(object sender, AxiosGameScreen gameScreen, InputHelper input);
public delegate void AxiosHandler(object sender, AxiosGameScreen gameScreen, InputState input);
public delegate void AxiosGameObjectHandler(AxiosGameObject sender);
#region GameObjectEventMethods
public virtual void OnFocusEnter(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnFocusEnter(AxiosGameScreen gameScreen, InputState input)
{
this.HasFocus = true;
this.OnEvent(FocusEnter, gameScreen, input);
}
public virtual void OnFocusLeave(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnFocusLeave(AxiosGameScreen gameScreen, InputState input)
{
this.HasFocus = false;
this.OnEvent(FocusLeave, gameScreen, input);
}
public virtual void OnMouseHover(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnMouseHover(AxiosGameScreen gameScreen, InputState input)
{
this.OnEvent(MouseHover, gameScreen, input);
}
public virtual void OnMouseLeave(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnMouseLeave(AxiosGameScreen gameScreen, InputState input)
{
this.OnEvent(MouseLeave, gameScreen, input);
}
public virtual void OnValueChange(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnValueChange(AxiosGameScreen gameScreen, InputState input)
{
this.OnEvent(ValueChange, gameScreen, input);
}
public virtual void OnMouseDown(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnMouseDown(AxiosGameScreen gameScreen, InputState input)
{
this.OnEvent(MouseDown, gameScreen, input);
}
public virtual void OnMouseUp(AxiosGameScreen gameScreen, InputHelper input)
public virtual void OnMouseUp(AxiosGameScreen gameScreen, InputState input)
{
this.OnEvent(MouseUp, gameScreen, input);
}
@ -68,7 +69,7 @@ namespace Axios.Engine
this.ScaleChanged(gameObject);
}
private void OnEvent(AxiosHandler e, AxiosGameScreen gameScreen, InputHelper input)
private void OnEvent(AxiosHandler e, AxiosGameScreen gameScreen, InputState input)
{
AxiosHandler handle = e;
if (handle != null)

View File

@ -2,6 +2,7 @@
using FarseerPhysics.Dynamics;
using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework;
using GameStateManagement;
namespace Axios.Engine
{
@ -46,12 +47,12 @@ namespace Axios.Engine
}
public virtual void HandleInput(AxiosGameScreen gameScreen, InputHelper input, GameTime gameTime)
public virtual void HandleInput(AxiosGameScreen gameScreen, InputState input, GameTime gameTime)
{
}
public virtual void HandleCursor(AxiosGameScreen gameScreen, InputHelper input)
public virtual void HandleCursor(AxiosGameScreen gameScreen, InputState input)
{
}

View File

@ -8,7 +8,9 @@ using Axios.Engine.UI;
using FarseerPhysics.Dynamics;
using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagement;
namespace Axios.Engine
{
@ -158,19 +160,16 @@ namespace Axios.Engine
AxiosLog.Instance.AddLine("Memory usage after cleanup: ", LoggingFlag.DEBUG);
}
public override void ExitScreen()
public override void Activate(bool instancePreserved)
{
base.ExitScreen();
}
public override void LoadContent()
{
base.LoadContent();
base.Activate(instancePreserved);
#if DEBUG
if (!Axios.Settings.ScreenSaver)
this.DebugSpriteFont = this.ScreenManager.Content.Load<SpriteFont>(this.DebugTextFont);
{
ContentManager man = new ContentManager(ScreenManager.Game.Services, "Content");
this.DebugSpriteFont = man.Load<SpriteFont>(this.DebugTextFont);
}
#endif
}
@ -211,7 +210,7 @@ namespace Axios.Engine
}
public override void HandleCursor(InputHelper input)
public override void HandleCursor(InputState input)
{
base.HandleCursor(input);
HandleMouseEvents(input);
@ -220,7 +219,7 @@ namespace Axios.Engine
g.HandleCursor(this, input);
}
private void HandleMouseEvents(InputHelper input)
private void HandleMouseEvents(InputState input)
{
Vector2 position = this.Camera.ConvertScreenToWorld(input.Cursor);
Fixture fix = this.World.TestPoint(position);
@ -335,9 +334,9 @@ namespace Axios.Engine
}
}
public override void HandleInput(InputHelper input, GameTime gameTime)
public override void HandleInput(GameTime gameTime, InputState input)
{
base.HandleInput(input, gameTime);
base.HandleInput(gameTime, input);
foreach (AxiosGameObject g in _gameObjects.ToList())
g.HandleInput(this, input, gameTime);
@ -346,9 +345,9 @@ namespace Axios.Engine
g.HandleInput(this, input, gameTime);
}
public override void UnloadContent()
public override void Deactivate()
{
base.UnloadContent();
base.Deactivate();
//AxiosLog.Instance.AddLine("Memory usage before cleanup: " + GC.GetTotalMemory(true).ToString(), LoggingFlag.DEBUG);
foreach (AxiosGameObject g in _gameObjects)
g.UnloadContent(this);

View File

@ -2,6 +2,7 @@
using Axios.Engine.Interfaces;
using Microsoft.Xna.Framework;
using GameStateManagement;
namespace Axios.Engine
{
@ -66,12 +67,12 @@ namespace Axios.Engine
}
public override void HandleInput(AxiosGameScreen gameScreen, FarseerPhysics.SamplesFramework.InputHelper input, Microsoft.Xna.Framework.GameTime gameTime)
public override void HandleInput(AxiosGameScreen gameScreen, InputState input, Microsoft.Xna.Framework.GameTime gameTime)
{
}
public override void HandleCursor(AxiosGameScreen gameScreen, FarseerPhysics.SamplesFramework.InputHelper input)
public override void HandleCursor(AxiosGameScreen gameScreen, InputState input)
{
}

View File

@ -1,5 +1,6 @@
using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework;
using GameStateManagement;
namespace Axios.Engine.Interfaces
@ -8,8 +9,8 @@ namespace Axios.Engine.Interfaces
{
void Update(AxiosGameScreen gameScreen, GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen);
void LoadContent(AxiosGameScreen gameScreen);
void HandleInput(AxiosGameScreen gameScreen, InputHelper input, GameTime gameTime);
void HandleCursor(AxiosGameScreen gameScreen, InputHelper input);
void HandleInput(AxiosGameScreen gameScreen, InputState input, GameTime gameTime);
void HandleCursor(AxiosGameScreen gameScreen, InputState input);
void UnloadContent(AxiosGameScreen gameScreen);
}
}

View File

@ -1,6 +1,7 @@

using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagement;
namespace Axios.Engine.UI
{
@ -48,7 +49,7 @@ namespace Axios.Engine.UI
}
public override void OnMouseHover(AxiosGameScreen gameScreen, InputHelper input)
public override void OnMouseHover(AxiosGameScreen gameScreen, InputState input)
{
base.OnMouseHover(gameScreen, input);
@ -56,21 +57,21 @@ namespace Axios.Engine.UI
}
public override void OnMouseLeave(AxiosGameScreen gameScreen, InputHelper input)
public override void OnMouseLeave(AxiosGameScreen gameScreen, InputState input)
{
base.OnMouseLeave(gameScreen, input);
this.Texture = _normaltexture;
}
public override void OnMouseDown(AxiosGameScreen gameScreen, InputHelper input)
public override void OnMouseDown(AxiosGameScreen gameScreen, InputState input)
{
base.OnMouseDown(gameScreen, input);
this.Texture = _clicktexture;
}
public override void OnMouseUp(AxiosGameScreen gameScreen, InputHelper input)
public override void OnMouseUp(AxiosGameScreen gameScreen, InputState input)
{
base.OnMouseUp(gameScreen, input);
@ -79,9 +80,9 @@ namespace Axios.Engine.UI
}
public override void HandleCursor(AxiosGameScreen gameScreen, InputHelper input)
public override void HandleCursor(AxiosGameScreen gameScreen, InputState input)
{
base.HandleCursor(gameScreen, input);

View File

@ -1,8 +1,21 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#region File Description
//-----------------------------------------------------------------------------
// BackgroundScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
namespace FarseerPhysics.SamplesFramework
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagement;
#endregion
namespace GameStateManagement
{
/// <summary>
/// The background screen sits behind all the other menu screens.
@ -11,14 +24,15 @@ namespace FarseerPhysics.SamplesFramework
/// </summary>
public class BackgroundScreen : GameScreen
{
private const float LogoScreenHeightRatio = 0.25f;
private const float LogoScreenBorderRatio = 0.0375f;
private const float LogoWidthHeightRatio = 1.4f;
#region Fields
ContentManager content;
Texture2D backgroundTexture;
#endregion
#region Initialization
private Texture2D _backgroundTexture;
private Rectangle _logoDestination;
//private Texture2D _logoTexture;
private Rectangle _viewport;
/// <summary>
/// Constructor.
@ -29,24 +43,40 @@ namespace FarseerPhysics.SamplesFramework
TransitionOffTime = TimeSpan.FromSeconds(0.5);
}
public override void LoadContent()
/// <summary>
/// Loads graphics content for this screen. The background texture is quite
/// big, so we use our own local ContentManager to load it. This allows us
/// to unload before going from the menus into the game itself, wheras if we
/// used the shared ContentManager provided by the Game class, the content
/// would remain loaded forever.
/// </summary>
public override void Activate(bool instancePreserved)
{
//_logoTexture = ScreenManager.Content.Load<Texture2D>("Common/logo");
_backgroundTexture = ScreenManager.Content.Load<Texture2D>("Common/gradient");
if (!instancePreserved)
{
if (content == null)
content = new ContentManager(ScreenManager.Game.Services, "Content");
Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
Vector2 logoSize = new Vector2();
logoSize.Y = viewport.Height * LogoScreenHeightRatio;
logoSize.X = logoSize.Y * LogoWidthHeightRatio;
float border = viewport.Height * LogoScreenBorderRatio;
Vector2 logoPosition = new Vector2(viewport.Width - border - logoSize.X,
viewport.Height - border - logoSize.Y);
_logoDestination = new Rectangle((int)logoPosition.X, (int)logoPosition.Y, (int)logoSize.X,
(int)logoSize.Y);
_viewport = viewport.Bounds;
backgroundTexture = content.Load<Texture2D>("background");
}
}
/// <summary>
/// Unloads graphics content for this screen.
/// </summary>
public override void Unload()
{
content.Unload();
}
#endregion
#region Update and Draw
/// <summary>
/// Updates the background screen. Unlike most screens, this should not
/// transition off even if it has been covered by another screen: it is
@ -55,20 +85,30 @@ namespace FarseerPhysics.SamplesFramework
/// Update method wanting to transition off.
/// </summary>
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, false);
}
/// <summary>
/// Draws the background screen.
/// </summary>
public override void Draw(GameTime gameTime)
{
ScreenManager.SpriteBatch.Begin();
ScreenManager.SpriteBatch.Draw(_backgroundTexture, _viewport, Color.White);
//ScreenManager.SpriteBatch.Draw(_logoTexture, _logoDestination, Color.White * 0.6f);
ScreenManager.SpriteBatch.End();
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, fullscreen,
new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));
spriteBatch.End();
}
#endregion
}
}
}

View File

@ -0,0 +1,191 @@
#region File Description
//-----------------------------------------------------------------------------
// Button.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GameStateManagement
{
/// <summary>
/// A special button that handles toggling between "On" and "Off"
/// </summary>
class BooleanButton : Button
{
private string option;
private bool value;
/// <summary>
/// Creates a new BooleanButton.
/// </summary>
/// <param name="option">The string text to display for the option.</param>
/// <param name="value">The initial value of the button.</param>
public BooleanButton(string option, bool value)
: base(option)
{
this.option = option;
this.value = value;
GenerateText();
}
protected override void OnTapped()
{
// When tapped we need to toggle the value and regenerate the text
value = !value;
GenerateText();
base.OnTapped();
}
/// <summary>
/// Helper that generates the actual Text value the base class uses for drawing.
/// </summary>
private void GenerateText()
{
Text = string.Format("{0}: {1}", option, value ? "On" : "Off");
}
}
/// <summary>
/// Represents a touchable button.
/// </summary>
class Button
{
/// <summary>
/// The text displayed in the button.
/// </summary>
public string Text = "Button";
/// <summary>
/// The position of the top-left corner of the button.
/// </summary>
public Vector2 Position = Vector2.Zero;
/// <summary>
/// The size of the button.
/// </summary>
public Vector2 Size = new Vector2(250, 75);
/// <summary>
/// The thickness of the border drawn for the button.
/// </summary>
public int BorderThickness = 4;
/// <summary>
/// The color of the button border.
/// </summary>
public Color BorderColor = new Color(200, 200, 200);
/// <summary>
/// The color of the button background.
/// </summary>
public Color FillColor = new Color(100, 100, 100) * .75f;
/// <summary>
/// The color of the text.
/// </summary>
public Color TextColor = Color.White;
/// <summary>
/// The opacity of the button.
/// </summary>
public float Alpha = 0f;
/// <summary>
/// Invoked when the button is tapped.
/// </summary>
public event EventHandler<EventArgs> Tapped;
/// <summary>
/// Creates a new Button.
/// </summary>
/// <param name="text">The text to display in the button.</param>
public Button(string text)
{
Text = text;
}
/// <summary>
/// Invokes the Tapped event and allows subclasses to perform actions when tapped.
/// </summary>
protected virtual void OnTapped()
{
if (Tapped != null)
Tapped(this, EventArgs.Empty);
}
/// <summary>
/// Passes a tap location to the button for handling.
/// </summary>
/// <param name="tap">The location of the tap.</param>
/// <returns>True if the button was tapped, false otherwise.</returns>
public bool HandleTap(Vector2 tap)
{
if (tap.X >= Position.X &&
tap.Y >= Position.Y &&
tap.X <= Position.X + Size.X &&
tap.Y <= Position.Y + Size.Y)
{
OnTapped();
return true;
}
return false;
}
/// <summary>
/// Draws the button
/// </summary>
/// <param name="screen">The screen drawing the button</param>
public void Draw(GameScreen screen)
{
// Grab some common items from the ScreenManager
SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
SpriteFont font = screen.ScreenManager.Font;
Texture2D blank = screen.ScreenManager.BlankTexture;
// Compute the button's rectangle
Rectangle r = new Rectangle(
(int)Position.X,
(int)Position.Y,
(int)Size.X,
(int)Size.Y);
// Fill the button
spriteBatch.Draw(blank, r, FillColor * Alpha);
// Draw the border
spriteBatch.Draw(
blank,
new Rectangle(r.Left, r.Top, r.Width, BorderThickness),
BorderColor * Alpha);
spriteBatch.Draw(
blank,
new Rectangle(r.Left, r.Top, BorderThickness, r.Height),
BorderColor * Alpha);
spriteBatch.Draw(
blank,
new Rectangle(r.Right - BorderThickness, r.Top, BorderThickness, r.Height),
BorderColor * Alpha);
spriteBatch.Draw(
blank,
new Rectangle(r.Left, r.Bottom - BorderThickness, r.Width, BorderThickness),
BorderColor * Alpha);
// Draw the text centered in the button
Vector2 textSize = font.MeasureString(Text);
Vector2 textPosition = new Vector2(r.Center.X, r.Center.Y) - textSize / 2f;
textPosition.X = (int)textPosition.X;
textPosition.Y = (int)textPosition.Y;
spriteBatch.DrawString(font, Text, textPosition, TextColor * Alpha);
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Globalization;
using Microsoft.Xna.Framework;
using GameStateManagement;
namespace FarseerPhysics.SamplesFramework
{

View File

@ -1,19 +1,18 @@
#region File Description
//-----------------------------------------------------------------------------
// PlayerIndexEventArgs.cs
// GameScreen.cs
//
// XNA Community Game Platform
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
/// <summary>
/// Enum describes the screen transition state.
@ -26,6 +25,7 @@ namespace FarseerPhysics.SamplesFramework
Hidden,
}
/// <summary>
/// A screen is a single layer that has update and draw logic, and which
/// can be combined with other layers to build up a complex menu system.
@ -35,23 +35,13 @@ namespace FarseerPhysics.SamplesFramework
/// </summary>
public abstract class GameScreen
{
private GestureType _enabledGestures = GestureType.None;
private bool _otherScreenHasFocus;
public GameScreen()
{
ScreenState = ScreenState.TransitionOn;
TransitionPosition = 1;
TransitionOffTime = TimeSpan.Zero;
TransitionOnTime = TimeSpan.Zero;
HasCursor = false;
HasVirtualStick = false;
HasCursor = false;
}
public bool HasCursor { get; set; }
public bool HasVirtualStick { get; set; }
public bool HasCursor = false;
/// <summary>
/// Normally when one screen is brought up over the top of another,
/// the first screen will transition off to make room for the new
@ -59,26 +49,54 @@ namespace FarseerPhysics.SamplesFramework
/// popup, in which case screens underneath it do not need to bother
/// transitioning off.
/// </summary>
public bool IsPopup { get; protected set; }
public bool IsPopup
{
get { return isPopup; }
protected set { isPopup = value; }
}
bool isPopup = false;
public bool HasVirtualStick { get; set; }
/// <summary>
/// Indicates how long the screen takes to
/// transition on when it is activated.
/// </summary>
public TimeSpan TransitionOnTime { get; protected set; }
public TimeSpan TransitionOnTime
{
get { return transitionOnTime; }
protected set { transitionOnTime = value; }
}
TimeSpan transitionOnTime = TimeSpan.Zero;
/// <summary>
/// Indicates how long the screen takes to
/// transition off when it is deactivated.
/// </summary>
public TimeSpan TransitionOffTime { get; protected set; }
public TimeSpan TransitionOffTime
{
get { return transitionOffTime; }
protected set { transitionOffTime = value; }
}
TimeSpan transitionOffTime = TimeSpan.Zero;
/// <summary>
/// Gets the current position of the screen transition, ranging
/// from zero (fully active, no transition) to one (transitioned
/// fully off to nothing).
/// </summary>
public float TransitionPosition { get; protected set; }
public float TransitionPosition
{
get { return transitionPosition; }
protected set { transitionPosition = value; }
}
float transitionPosition = 1;
/// <summary>
/// Gets the current alpha of the screen transition, ranging
@ -90,10 +108,18 @@ namespace FarseerPhysics.SamplesFramework
get { return 1f - TransitionPosition; }
}
/// <summary>
/// Gets the current screen transition state.
/// </summary>
public ScreenState ScreenState { get; protected set; }
public ScreenState ScreenState
{
get { return screenState; }
protected set { screenState = value; }
}
ScreenState screenState = ScreenState.TransitionOn;
/// <summary>
/// There are two possible reasons why a screen might be transitioning
@ -103,9 +129,14 @@ namespace FarseerPhysics.SamplesFramework
/// if set, the screen will automatically remove itself as soon as the
/// transition finishes.
/// </summary>
public bool IsExiting { get; protected internal set; }
public bool IsExiting
{
get { return isExiting; }
protected internal set { isExiting = value; }
}
bool isExiting = false;
public bool AlwaysHasFocus = false;
/// <summary>
/// Checks whether this screen is active and can respond to user input.
@ -114,16 +145,43 @@ namespace FarseerPhysics.SamplesFramework
{
get
{
return !_otherScreenHasFocus &&
(ScreenState == ScreenState.TransitionOn ||
ScreenState == ScreenState.Active);
return !otherScreenHasFocus &&
(screenState == ScreenState.TransitionOn ||
screenState == ScreenState.Active);
}
}
bool otherScreenHasFocus;
/// <summary>
/// Gets the manager that this screen belongs to.
/// </summary>
public ScreenManager ScreenManager { get; internal set; }
public ScreenManager ScreenManager
{
get { return screenManager; }
internal set { screenManager = value; }
}
ScreenManager screenManager;
/// <summary>
/// Gets the index of the player who is currently controlling this screen,
/// or null if it is accepting input from any player. This is used to lock
/// the game to a specific player profile. The main menu responds to input
/// from any connected gamepad, but whichever player makes a selection from
/// this menu is given control over all subsequent screens, so other gamepads
/// are inactive until the controlling player returns to the main menu.
/// </summary>
public PlayerIndex? ControllingPlayer
{
get { return controllingPlayer; }
internal set { controllingPlayer = value; }
}
PlayerIndex? controllingPlayer;
/// <summary>
/// Gets the gestures the screen is interested in. Screens should be as specific
@ -134,10 +192,10 @@ namespace FarseerPhysics.SamplesFramework
/// </summary>
public GestureType EnabledGestures
{
get { return _enabledGestures; }
get { return enabledGestures; }
protected set
{
_enabledGestures = value;
enabledGestures = value;
// the screen manager handles this during screen changes, but
// if this screen is active and the gesture types are changing,
@ -149,36 +207,62 @@ namespace FarseerPhysics.SamplesFramework
}
}
/// <summary>
/// Load graphics content for the screen.
/// </summary>
public virtual void LoadContent()
{
}
GestureType enabledGestures = GestureType.None;
/// <summary>
/// Unload content for the screen.
/// Gets whether or not this screen is serializable. If this is true,
/// the screen will be recorded into the screen manager's state and
/// its Serialize and Deserialize methods will be called as appropriate.
/// If this is false, the screen will be ignored during serialization.
/// By default, all screens are assumed to be serializable.
/// </summary>
public virtual void UnloadContent()
public bool IsSerializable
{
get { return isSerializable; }
protected set { isSerializable = value; }
}
bool isSerializable = true;
/// <summary>
/// Activates the screen. Called when the screen is added to the screen manager or if the game resumes
/// from being paused or tombstoned.
/// </summary>
/// <param name="instancePreserved">
/// True if the game was preserved during deactivation, false if the screen is just being added or if the game was tombstoned.
/// On Xbox and Windows this will always be false.
/// </param>
public virtual void Activate(bool instancePreserved) { }
/// <summary>
/// Deactivates the screen. Called when the game is being deactivated due to pausing or tombstoning.
/// </summary>
public virtual void Deactivate() { }
/// <summary>
/// Unload content for the screen. Called when the screen is removed from the screen manager.
/// </summary>
public virtual void Unload() { }
/// <summary>
/// Allows the screen to run logic, such as updating the transition position.
/// Unlike HandleInput, this method is called regardless of whether the screen
/// is active, hidden, or in the middle of a transition.
/// </summary>
public virtual void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
public virtual void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
{
_otherScreenHasFocus = otherScreenHasFocus;
this.otherScreenHasFocus = otherScreenHasFocus;
if (IsExiting)
if (isExiting)
{
// If the screen is going away to die, it should transition off.
ScreenState = ScreenState.TransitionOff;
screenState = ScreenState.TransitionOff;
if (!UpdateTransition(gameTime, TransitionOffTime, 1))
if (!UpdateTransition(gameTime, transitionOffTime, 1))
{
// When the transition finishes, remove the screen.
ScreenManager.RemoveScreen(this);
@ -187,59 +271,55 @@ namespace FarseerPhysics.SamplesFramework
else if (coveredByOtherScreen)
{
// If the screen is covered by another, it should transition off.
if (UpdateTransition(gameTime, TransitionOffTime, 1))
if (UpdateTransition(gameTime, transitionOffTime, 1))
{
// Still busy transitioning.
ScreenState = ScreenState.TransitionOff;
screenState = ScreenState.TransitionOff;
}
else
{
// Transition finished!
ScreenState = ScreenState.Hidden;
screenState = ScreenState.Hidden;
}
}
else
{
// Otherwise the screen should transition on and become active.
if (UpdateTransition(gameTime, TransitionOnTime, -1))
if (UpdateTransition(gameTime, transitionOnTime, -1))
{
// Still busy transitioning.
ScreenState = ScreenState.TransitionOn;
screenState = ScreenState.TransitionOn;
}
else
{
// Transition finished!
ScreenState = ScreenState.Active;
screenState = ScreenState.Active;
}
}
}
/// <summary>
/// Helper for updating the screen transition position.
/// </summary>
private bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
bool UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
{
// How much should we move by?
float transitionDelta;
if (time == TimeSpan.Zero)
{
transitionDelta = 1f;
}
transitionDelta = 1;
else
{
transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds /
time.TotalMilliseconds);
}
transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / time.TotalMilliseconds);
// Update the transition position.
TransitionPosition += transitionDelta * direction;
transitionPosition += transitionDelta * direction;
// Did we reach the end of the transition?
if (((direction < 0) && (TransitionPosition <= 0)) ||
((direction > 0) && (TransitionPosition >= 1)))
if (((direction < 0) && (transitionPosition <= 0)) ||
((direction > 0) && (transitionPosition >= 1)))
{
TransitionPosition = MathHelper.Clamp(TransitionPosition, 0, 1);
transitionPosition = MathHelper.Clamp(transitionPosition, 0, 1);
return false;
}
@ -247,30 +327,29 @@ namespace FarseerPhysics.SamplesFramework
return true;
}
/// <summary>
/// Allows the screen to handle user input. Unlike Update, this method
/// is only called when the screen is active, and not when some other
/// screen has taken the focus.
/// </summary>
public virtual void HandleInput(InputHelper input, GameTime gameTime)
{
}
public virtual void HandleInput(GameTime gameTime, InputState input) { }
/// <summary>
/// This is called when the screen should draw itself.
/// </summary>
public virtual void Draw(GameTime gameTime)
{
}
public virtual void Draw(GameTime gameTime) { }
/// <summary>
/// Tells the screen to go away. Unlike ScreenManager.RemoveScreen, which
/// instantly kills the screen, this method respects the transition timings
/// and will give the screen a chance to gradually transition off.
/// </summary>
public virtual void ExitScreen()
public void ExitScreen()
{
if (TransitionOffTime == TimeSpan.Zero && this.TransitionPosition == 0 && this.TransitionAlpha == 1)
if (TransitionOffTime == TimeSpan.Zero)
{
// If the screen has a zero transition time, remove it immediately.
ScreenManager.RemoveScreen(this);
@ -278,8 +357,8 @@ namespace FarseerPhysics.SamplesFramework
else
{
// Otherwise flag that it should transition off and then exit.
IsExiting = true;
isExiting = true;
}
}
}
}
}

View File

@ -0,0 +1,265 @@
#region File Description
//-----------------------------------------------------------------------------
// GameplayScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
#endregion
namespace GameStateManagementSample
{
/// <summary>
/// This screen implements the actual game logic. It is just a
/// placeholder to get the idea across: you'll probably want to
/// put some more interesting gameplay in here!
/// </summary>
class GameplayScreen : GameScreen
{
#region Fields
ContentManager content;
SpriteFont gameFont;
Vector2 playerPosition = new Vector2(100, 100);
Vector2 enemyPosition = new Vector2(100, 100);
Random random = new Random();
float pauseAlpha;
InputAction pauseAction;
#endregion
#region Initialization
/// <summary>
/// Constructor.
/// </summary>
public GameplayScreen()
{
TransitionOnTime = TimeSpan.FromSeconds(1.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
pauseAction = new InputAction(
new Buttons[] { Buttons.Start, Buttons.Back },
new Keys[] { Keys.Escape },
true);
}
/// <summary>
/// Load graphics content for the game.
/// </summary>
public override void Activate(bool instancePreserved)
{
if (!instancePreserved)
{
if (content == null)
content = new ContentManager(ScreenManager.Game.Services, "Content");
gameFont = content.Load<SpriteFont>("gamefont");
// A real game would probably have more content than this sample, so
// it would take longer to load. We simulate that by delaying for a
// while, giving you a chance to admire the beautiful loading screen.
Thread.Sleep(1000);
// once the load has finished, we use ResetElapsedTime to tell the game's
// timing mechanism that we have just finished a very long frame, and that
// it should not try to catch up.
ScreenManager.Game.ResetElapsedTime();
}
#if WINDOWS_PHONE
if (Microsoft.Phone.Shell.PhoneApplicationService.Current.State.ContainsKey("PlayerPosition"))
{
playerPosition = (Vector2)Microsoft.Phone.Shell.PhoneApplicationService.Current.State["PlayerPosition"];
enemyPosition = (Vector2)Microsoft.Phone.Shell.PhoneApplicationService.Current.State["EnemyPosition"];
}
#endif
}
public override void Deactivate()
{
#if WINDOWS_PHONE
Microsoft.Phone.Shell.PhoneApplicationService.Current.State["PlayerPosition"] = playerPosition;
Microsoft.Phone.Shell.PhoneApplicationService.Current.State["EnemyPosition"] = enemyPosition;
#endif
base.Deactivate();
}
/// <summary>
/// Unload graphics content used by the game.
/// </summary>
public override void Unload()
{
content.Unload();
#if WINDOWS_PHONE
Microsoft.Phone.Shell.PhoneApplicationService.Current.State.Remove("PlayerPosition");
Microsoft.Phone.Shell.PhoneApplicationService.Current.State.Remove("EnemyPosition");
#endif
}
#endregion
#region Update and Draw
/// <summary>
/// Updates the state of the game. This method checks the GameScreen.IsActive
/// property, so the game will stop updating when the pause menu is active,
/// or if you tab away to a different application.
/// </summary>
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, false);
// Gradually fade in or out depending on whether we are covered by the pause screen.
if (coveredByOtherScreen)
pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
else
pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
if (IsActive)
{
// Apply some random jitter to make the enemy move around.
const float randomization = 10;
enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;
// Apply a stabilizing force to stop the enemy moving off the screen.
Vector2 targetPosition = new Vector2(
ScreenManager.GraphicsDevice.Viewport.Width / 2 - gameFont.MeasureString("Insert Gameplay Here").X / 2,
200);
enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
// TODO: this game isn't very fun! You could probably improve
// it by inserting something more interesting in this space :-)
}
}
/// <summary>
/// Lets the game respond to player input. Unlike the Update method,
/// this will only be called when the gameplay screen is active.
/// </summary>
public override void HandleInput(GameTime gameTime, InputState input)
{
if (input == null)
throw new ArgumentNullException("input");
// Look up inputs for the active player profile.
int playerIndex = (int)ControllingPlayer.Value;
KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
// The game pauses either if the user presses the pause button, or if
// they unplug the active gamepad. This requires us to keep track of
// whether a gamepad was ever plugged in, because we don't want to pause
// on PC if they are playing with a keyboard and have no gamepad at all!
bool gamePadDisconnected = !gamePadState.IsConnected &&
input.GamePadWasConnected[playerIndex];
PlayerIndex player;
if (pauseAction.Evaluate(input, ControllingPlayer, out player) || gamePadDisconnected)
{
#if WINDOWS_PHONE
ScreenManager.AddScreen(new PhonePauseScreen(), ControllingPlayer);
#else
ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
#endif
}
else
{
// Otherwise move the player position.
Vector2 movement = Vector2.Zero;
if (keyboardState.IsKeyDown(Keys.Left))
movement.X--;
if (keyboardState.IsKeyDown(Keys.Right))
movement.X++;
if (keyboardState.IsKeyDown(Keys.Up))
movement.Y--;
if (keyboardState.IsKeyDown(Keys.Down))
movement.Y++;
Vector2 thumbstick = gamePadState.ThumbSticks.Left;
movement.X += thumbstick.X;
movement.Y -= thumbstick.Y;
if (input.TouchState.Count > 0)
{
Vector2 touchPosition = input.TouchState[0].Position;
Vector2 direction = touchPosition - playerPosition;
direction.Normalize();
movement += direction;
}
if (movement.Length() > 1)
movement.Normalize();
playerPosition += movement * 8f;
}
}
/// <summary>
/// Draws the gameplay screen.
/// </summary>
public override void Draw(GameTime gameTime)
{
// This game has a blue background. Why? Because!
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
Color.CornflowerBlue, 0, 0);
// Our player and enemy are both actually just text strings.
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
spriteBatch.Begin();
spriteBatch.DrawString(gameFont, "// TODO", playerPosition, Color.Green);
spriteBatch.DrawString(gameFont, "Insert Gameplay Here",
enemyPosition, Color.DarkRed);
spriteBatch.End();
// If the game is transitioning on or off, fade it out to black.
if (TransitionPosition > 0 || pauseAlpha > 0)
{
float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
ScreenManager.FadeBackBufferToBlack(alpha);
}
}
#endregion
}
}

View File

@ -0,0 +1,49 @@
#region File Description
//-----------------------------------------------------------------------------
// IScreenFactory.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
namespace GameStateManagement
{
/// <summary>
/// Defines an object that can create a screen when given its type.
///
/// The ScreenManager attempts to handle tombstoning on Windows Phone by creating an XML
/// document that has a list of the screens currently in the manager. When the game is
/// reactivated, the ScreenManager needs to create instances of those screens. However
/// since there is no restriction that a particular GameScreen subclass has a parameterless
/// constructor, there is no way the ScreenManager alone could create those instances.
///
/// IScreenFactory fills this gap by providing an interface the game should implement to
/// act as a translation from type to instance. The ScreenManager locates the IScreenFactory
/// from the Game.Services collection and passes each screen type to the factory, expecting
/// to get the correct GameScreen out.
///
/// If your game screens all have parameterless constructors, the minimal implementation of
/// this interface would look like this:
///
/// return Activator.CreateInstance(screenType) as GameScreen;
///
/// If you have screens with constructors that take arguments, you will need to ensure that
/// you can read these arguments from storage or generate new ones, then construct the screen
/// based on the type.
///
/// The ScreenFactory type in the sample game has the minimal implementation along with some
/// extra comments showing a potentially more complex example of how to implement IScreenFactory.
/// </summary>
public interface IScreenFactory
{
/// <summary>
/// Creates a GameScreen from the given type.
/// </summary>
/// <param name="screenType">The type of screen to create.</param>
/// <returns>The newly created screen.</returns>
GameScreen CreateScreen(Type screenType);
}
}

View File

@ -0,0 +1,96 @@
#region File Description
//-----------------------------------------------------------------------------
// InputAction.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace GameStateManagement
{
/// <summary>
/// Defines an action that is designated by some set of buttons and/or keys.
///
/// The way actions work is that you define a set of buttons and keys that trigger the action. You can
/// then evaluate the action against an InputState which will test to see if any of the buttons or keys
/// are pressed by a player. You can also set a flag that indicates if the action only occurs once when
/// the buttons/keys are first pressed or whether the action should occur each frame.
///
/// Using this InputAction class means that you can configure new actions based on keys and buttons
/// without having to directly modify the InputState type. This means more customization by your games
/// without having to change the core classes of Game State Management.
/// </summary>
public class InputAction
{
private readonly Buttons[] buttons;
private readonly Keys[] keys;
private readonly bool newPressOnly;
// These delegate types map to the methods on InputState. We use these to simplify the evalute method
// by allowing us to map the appropriate delegates and invoke them, rather than having two separate code paths.
private delegate bool ButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex player);
private delegate bool KeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex player);
/// <summary>
/// Initializes a new InputAction.
/// </summary>
/// <param name="buttons">An array of buttons that can trigger the action.</param>
/// <param name="keys">An array of keys that can trigger the action.</param>
/// <param name="newPressOnly">Whether the action only occurs on the first press of one of the buttons/keys,
/// false if it occurs each frame one of the buttons/keys is down.</param>
public InputAction(Buttons[] buttons, Keys[] keys, bool newPressOnly)
{
// Store the buttons and keys. If the arrays are null, we create a 0 length array so we don't
// have to do null checks in the Evaluate method
this.buttons = buttons != null ? buttons.Clone() as Buttons[] : new Buttons[0];
this.keys = keys != null ? keys.Clone() as Keys[] : new Keys[0];
this.newPressOnly = newPressOnly;
}
/// <summary>
/// Evaluates the action against a given InputState.
/// </summary>
/// <param name="state">The InputState to test for the action.</param>
/// <param name="controllingPlayer">The player to test, or null to allow any player.</param>
/// <param name="player">If controllingPlayer is null, this is the player that performed the action.</param>
/// <returns>True if the action occurred, false otherwise.</returns>
public bool Evaluate(InputState state, PlayerIndex? controllingPlayer, out PlayerIndex player)
{
// Figure out which delegate methods to map from the state which takes care of our "newPressOnly" logic
ButtonPress buttonTest;
KeyPress keyTest;
if (newPressOnly)
{
buttonTest = state.IsNewButtonPress;
keyTest = state.IsNewKeyPress;
}
else
{
buttonTest = state.IsButtonPressed;
keyTest = state.IsKeyPressed;
}
// Now we simply need to invoke the appropriate methods for each button and key in our collections
foreach (Buttons button in buttons)
{
if (buttonTest(button, controllingPlayer, out player))
return true;
}
foreach (Keys key in keys)
{
if (keyTest(key, controllingPlayer, out player))
return true;
}
// If we got here, the action is not matched
player = PlayerIndex.One;
return false;
}
}
}

View File

@ -1,10 +1,13 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using FarseerPhysics.SamplesFramework;
using GameStateManagement;
namespace FarseerPhysics.SamplesFramework
namespace FarseerPhysics.SamplesFramework2
{
/// <summary>
/// an enum of all available mouse buttons.
@ -145,13 +148,14 @@ namespace FarseerPhysics.SamplesFramework
public void LoadContent()
{
_cursorSprite = new Sprite(_manager.Content.Load<Texture2D>("Common/cursor"));
_cursorSprite = new Sprite(_manager.Game.Content.Load<Texture2D>("Common/cursor"));
#if WINDOWS_PHONE
// virtual stick content
_phoneStick = new VirtualStick(_manager.Content.Load<Texture2D>("Common/socket"),
_manager.Content.Load<Texture2D>("Common/stick"), new Vector2(80f, 400f));
_phoneStick = new VirtualStick(_manager.Game.Content.Load<Texture2D>("Common/socket"),
_manager.Game.Content.Load<Texture2D>("Common/stick"), new Vector2(80f, 400f));
Texture2D temp = _manager.Content.Load<Texture2D>("Common/buttons");
Texture2D temp = _manager.Game.Content.Load<Texture2D>("Common/buttons");
_phoneA = new VirtualButton(temp, new Vector2(695f, 380f), new Rectangle(0, 0, 40, 40), new Rectangle(0, 40, 40, 40));
_phoneB = new VirtualButton(temp, new Vector2(745f, 360f), new Rectangle(40, 0, 40, 40), new Rectangle(40, 40, 40, 40));
#endif

View File

@ -0,0 +1,609 @@
#region File Description
//-----------------------------------------------------------------------------
// InputState.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace GameStateManagement
{
/// <summary>
/// an enum of all available mouse buttons.
/// </summary>
public enum MouseButtons
{
LeftButton,
MiddleButton,
RightButton,
ExtraButton1,
ExtraButton2
}
/// <summary>
/// Helper for reading input from keyboard, gamepad, and touch input. This class
/// tracks both the current and previous state of the input devices, and implements
/// query methods for high level input actions such as "move up through the menu"
/// or "pause the game".
/// </summary>
public class InputState
{
public const int MaxInputs = 4;
public readonly KeyboardState[] CurrentKeyboardStates;
public readonly GamePadState[] CurrentGamePadStates;
public readonly KeyboardState[] LastKeyboardStates;
public readonly GamePadState[] LastGamePadStates;
public readonly bool[] GamePadWasConnected;
/*
* Needed for virtual stick on WP7
* -- Nathan Adams [adamsna@datanethost.net] - 4/12/2012
*/
private GamePadState _currentVirtualState;
private GamePadState _lastVirtualState;
private bool _handleVirtualStick;
/*
* I didn't create an array for the virtual stick because there will only be one
* -- Nathan Adams [adamsna@datanethost.net] - 4/12/2012
*/
/*
* Adding variables for the cursor
* -- Nathan Adams [adamsna@datanethost.net] - 4/15/2012
*
*/
private MouseState _currentMouseState;
private MouseState _lastMouseState;
private Vector2 _cursor;
private bool _cursorIsValid;
private bool _cursorIsVisible;
private bool _cursorMoved;
private Sprite _cursorSprite;
#if WINDOWS_PHONE
private VirtualStick _phoneStick;
private VirtualButton _phoneA;
private VirtualButton _phoneB;
#endif
public TouchCollection TouchState;
public readonly List<GestureSample> Gestures = new List<GestureSample>();
private ScreenManager _manager;
private Viewport _viewport;
/// <summary>
/// Constructs a new input state.
/// </summary>
public InputState(ScreenManager manager)
{
_manager = manager;
CurrentKeyboardStates = new KeyboardState[MaxInputs];
CurrentGamePadStates = new GamePadState[MaxInputs];
LastKeyboardStates = new KeyboardState[MaxInputs];
LastGamePadStates = new GamePadState[MaxInputs];
GamePadWasConnected = new bool[MaxInputs];
_currentVirtualState = new GamePadState();
_lastVirtualState = new GamePadState();
_cursorIsVisible = false;
_cursorMoved = false;
#if WINDOWS_PHONE
_cursorIsValid = false;
#else
_cursorIsValid = true;
#endif
_cursor = Vector2.Zero;
_handleVirtualStick = false;
}
public MouseState MouseState
{
get { return _currentMouseState; }
}
public GamePadState VirtualState
{
get { return _currentVirtualState; }
}
public MouseState PreviousMouseState
{
get { return _lastMouseState; }
}
public GamePadState PreviousVirtualState
{
get { return _lastVirtualState; }
}
public bool ShowCursor
{
get { return _cursorIsVisible && _cursorIsValid; }
set { _cursorIsVisible = value; }
}
public bool EnableVirtualStick
{
get { return _handleVirtualStick; }
set { _handleVirtualStick = value; }
}
public Vector2 Cursor
{
get { return _cursor; }
}
public bool IsCursorMoved
{
get { return _cursorMoved; }
}
public bool IsCursorValid
{
get { return _cursorIsValid; }
}
public void LoadContent()
{
ContentManager man = new ContentManager(_manager.Game.Services, "Content");
_cursorSprite = new Sprite(man.Load<Texture2D>("Common/cursor"));
#if WINDOWS_PHONE
// virtual stick content
_phoneStick = new VirtualStick(man.Load<Texture2D>("Common/socket"),
man.Load<Texture2D>("Common/stick"), new Vector2(80f, 400f));
Texture2D temp = man.Load<Texture2D>("Common/buttons");
_phoneA = new VirtualButton(temp, new Vector2(695f, 380f), new Rectangle(0, 0, 40, 40), new Rectangle(0, 40, 40, 40));
_phoneB = new VirtualButton(temp, new Vector2(745f, 360f), new Rectangle(40, 0, 40, 40), new Rectangle(40, 40, 40, 40));
#endif
_viewport = _manager.GraphicsDevice.Viewport;
}
private GamePadState HandleVirtualStickWin()
{
Vector2 _leftStick = Vector2.Zero;
List<Buttons> _buttons = new List<Buttons>();
PlayerIndex pout;
if (IsNewKeyPress(Keys.A, PlayerIndex.One, out pout))
{
_leftStick.X -= 1f;
}
if (IsNewKeyPress(Keys.S, PlayerIndex.One, out pout))
{
_leftStick.Y -= 1f;
}
if (IsNewKeyPress(Keys.D, PlayerIndex.One, out pout))
{
_leftStick.X += 1f;
}
if (IsNewKeyPress(Keys.W, PlayerIndex.One, out pout))
{
_leftStick.Y += 1f;
}
if (IsNewKeyPress(Keys.Space, PlayerIndex.One, out pout))
{
_buttons.Add(Buttons.A);
}
if (IsNewKeyPress(Keys.LeftControl, PlayerIndex.One, out pout))
{
_buttons.Add(Buttons.B);
}
if (_leftStick != Vector2.Zero)
{
_leftStick.Normalize();
}
return new GamePadState(_leftStick, Vector2.Zero, 0f, 0f, _buttons.ToArray());
}
private GamePadState HandleVirtualStickWP7()
{
List<Buttons> _buttons = new List<Buttons>();
Vector2 _stick = Vector2.Zero;
#if WINDOWS_PHONE
_phoneA.Pressed = false;
_phoneB.Pressed = false;
TouchCollection touchLocations = TouchPanel.GetState();
foreach (TouchLocation touchLocation in touchLocations)
{
_phoneA.Update(touchLocation);
_phoneB.Update(touchLocation);
_phoneStick.Update(touchLocation);
}
if (_phoneA.Pressed)
{
_buttons.Add(Buttons.A);
}
if (_phoneB.Pressed)
{
_buttons.Add(Buttons.B);
}
_stick = _phoneStick.StickPosition;
#endif
return new GamePadState(_stick, Vector2.Zero, 0f, 0f, _buttons.ToArray());
}
public void Draw()
{
if (_cursorIsVisible && _cursorIsValid)
{
_manager.SpriteBatch.Begin();
_manager.SpriteBatch.Draw(_cursorSprite.Texture, _cursor, null, Color.White, 0f, _cursorSprite.Origin, 1f, SpriteEffects.None, 0f);
_manager.SpriteBatch.End();
}
#if WINDOWS_PHONE
if (_handleVirtualStick)
{
_manager.SpriteBatch.Begin();
_phoneA.Draw(_manager.SpriteBatch);
_phoneB.Draw(_manager.SpriteBatch);
_phoneStick.Draw(_manager.SpriteBatch);
_manager.SpriteBatch.End();
}
#endif
}
/// <summary>
/// Reads the latest state user input.
/// </summary>
public void Update(GameTime gameTime)
{
PlayerIndex p;
_lastMouseState = _currentMouseState;
if (_handleVirtualStick)
{
_lastVirtualState = _currentVirtualState;
}
_currentMouseState = Mouse.GetState();
if (_handleVirtualStick)
{
#if XBOX
_currentVirtualState= GamePad.GetState(PlayerIndex.One);
#elif WINDOWS
if (GamePad.GetState(PlayerIndex.One).IsConnected)
{
_currentVirtualState = GamePad.GetState(PlayerIndex.One);
}
else
{
_currentVirtualState = HandleVirtualStickWin();
}
#elif WINDOWS_PHONE
_currentVirtualState = HandleVirtualStickWP7();
#endif
}
for (int i = 0; i < MaxInputs; i++)
{
LastKeyboardStates[i] = CurrentKeyboardStates[i];
LastGamePadStates[i] = CurrentGamePadStates[i];
CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
// Keep track of whether a gamepad has ever been
// connected, so we can detect if it is unplugged.
if (CurrentGamePadStates[i].IsConnected)
{
GamePadWasConnected[i] = true;
}
}
// Get the raw touch state from the TouchPanel
TouchState = TouchPanel.GetState();
// Read in any detected gestures into our list for the screens to later process
Gestures.Clear();
while (TouchPanel.IsGestureAvailable)
{
//System.Diagnostics.Debugger.Break();
Gestures.Add(TouchPanel.ReadGesture());
}
//System.Diagnostics.Debugger.Break();
// Update cursor
Vector2 oldCursor = _cursor;
if (CurrentGamePadStates[0].IsConnected && CurrentGamePadStates[0].ThumbSticks.Left != Vector2.Zero)
{
Vector2 temp = CurrentGamePadStates[0].ThumbSticks.Left;
_cursor += temp * new Vector2(300f, -300f) * (float)gameTime.ElapsedGameTime.TotalSeconds;
Mouse.SetPosition((int)_cursor.X, (int)_cursor.Y);
}
else
{
_cursor.X = _currentMouseState.X;
_cursor.Y = _currentMouseState.Y;
}
if (this.IsNewKeyPress(Keys.P, PlayerIndex.One, out p))
Console.WriteLine(_cursor.ToString());
_cursor.X = MathHelper.Clamp(_cursor.X, 0f, _viewport.Width);
_cursor.Y = MathHelper.Clamp(_cursor.Y, 0f, _viewport.Height);
if (this.IsNewKeyPress(Keys.P, PlayerIndex.One, out p))
Console.WriteLine(_cursor.ToString());
if (_cursorIsValid && oldCursor != _cursor)
{
_cursorMoved = true;
}
else
{
_cursorMoved = false;
}
#if WINDOWS
if (_viewport.Bounds.Contains(_currentMouseState.X, _currentMouseState.Y))
{
_cursorIsValid = true;
}
else
{
_cursorIsValid = false;
}
#elif WINDOWS_PHONE
if (_currentMouseState.LeftButton == ButtonState.Pressed)
{
_cursorIsValid = true;
}
else
{
_cursorIsValid = false;
}
#endif
if (this.IsNewKeyPress(Keys.P, PlayerIndex.One, out p))
Console.WriteLine(_viewport.ToString());
}
/// <summary>
/// Helper for checking if a key was pressed during this update. The
/// controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a keypress
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsKeyPressed(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return CurrentKeyboardStates[i].IsKeyDown(key);
}
else
{
// Accept input from any player.
return (IsKeyPressed(key, PlayerIndex.One, out playerIndex) ||
IsKeyPressed(key, PlayerIndex.Two, out playerIndex) ||
IsKeyPressed(key, PlayerIndex.Three, out playerIndex) ||
IsKeyPressed(key, PlayerIndex.Four, out playerIndex));
}
}
/// <summary>
/// Helper for checking if a button was pressed during this update.
/// The controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a button press
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsButtonPressed(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return CurrentGamePadStates[i].IsButtonDown(button);
}
else
{
// Accept input from any player.
return (IsButtonPressed(button, PlayerIndex.One, out playerIndex) ||
IsButtonPressed(button, PlayerIndex.Two, out playerIndex) ||
IsButtonPressed(button, PlayerIndex.Three, out playerIndex) ||
IsButtonPressed(button, PlayerIndex.Four, out playerIndex));
}
}
/// <summary>
/// Helper for checking if a key was newly pressed during this update. The
/// controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a keypress
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return (CurrentKeyboardStates[i].IsKeyDown(key) &&
LastKeyboardStates[i].IsKeyUp(key));
}
else
{
// Accept input from any player.
return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
}
}
public bool IsNewKeyRelease(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return (CurrentKeyboardStates[i].IsKeyUp(key) &&
LastKeyboardStates[i].IsKeyDown(key));
}
else
{
// Accept input from any player.
return (IsNewKeyRelease(key, PlayerIndex.One, out playerIndex) ||
IsNewKeyRelease(key, PlayerIndex.Two, out playerIndex) ||
IsNewKeyRelease(key, PlayerIndex.Three, out playerIndex) ||
IsNewKeyRelease(key, PlayerIndex.Four, out playerIndex));
}
}
/// <summary>
/// Helper for checking if a button was newly pressed during this update.
/// The controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a button press
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return (CurrentGamePadStates[i].IsButtonDown(button) &&
LastGamePadStates[i].IsButtonUp(button));
}
else
{
// Accept input from any player.
return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) ||
IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) ||
IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) ||
IsNewButtonPress(button, PlayerIndex.Four, out playerIndex));
}
}
public bool IsNewButtonRelease(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
return (CurrentGamePadStates[i].IsButtonUp(button) &&
LastGamePadStates[i].IsButtonDown(button));
}
else
{
// Accept input from any player.
return (IsNewButtonRelease(button, PlayerIndex.One, out playerIndex) ||
IsNewButtonRelease(button, PlayerIndex.Two, out playerIndex) ||
IsNewButtonRelease(button, PlayerIndex.Three, out playerIndex) ||
IsNewButtonRelease(button, PlayerIndex.Four, out playerIndex));
}
}
public bool IsNewVirtualButtonPress(Buttons button)
{
return (_lastVirtualState.IsButtonUp(button) &&
_currentVirtualState.IsButtonDown(button));
}
public bool IsNewVirtualButtonRelease(Buttons button)
{
return (_lastVirtualState.IsButtonDown(button) &&
_currentVirtualState.IsButtonUp(button));
}
/// <summary>
/// Helper for checking if a mouse button was newly pressed during this update.
/// </summary>
public bool IsNewMouseButtonPress(MouseButtons button)
{
switch (button)
{
case MouseButtons.LeftButton:
return (_currentMouseState.LeftButton == ButtonState.Pressed &&
_lastMouseState.LeftButton == ButtonState.Released);
case MouseButtons.RightButton:
return (_currentMouseState.RightButton == ButtonState.Pressed &&
_lastMouseState.RightButton == ButtonState.Released);
case MouseButtons.MiddleButton:
return (_currentMouseState.MiddleButton == ButtonState.Pressed &&
_lastMouseState.MiddleButton == ButtonState.Released);
case MouseButtons.ExtraButton1:
return (_currentMouseState.XButton1 == ButtonState.Pressed &&
_lastMouseState.XButton1 == ButtonState.Released);
case MouseButtons.ExtraButton2:
return (_currentMouseState.XButton2 == ButtonState.Pressed &&
_lastMouseState.XButton2 == ButtonState.Released);
default:
return false;
}
}
/// <summary>
/// Checks if the requested mouse button is released.
/// </summary>
/// <param name="button">The button.</param>
public bool IsNewMouseButtonRelease(MouseButtons button)
{
switch (button)
{
case MouseButtons.LeftButton:
return (_lastMouseState.LeftButton == ButtonState.Pressed &&
_currentMouseState.LeftButton == ButtonState.Released);
case MouseButtons.RightButton:
return (_lastMouseState.RightButton == ButtonState.Pressed &&
_currentMouseState.RightButton == ButtonState.Released);
case MouseButtons.MiddleButton:
return (_lastMouseState.MiddleButton == ButtonState.Pressed &&
_currentMouseState.MiddleButton == ButtonState.Released);
case MouseButtons.ExtraButton1:
return (_lastMouseState.XButton1 == ButtonState.Pressed &&
_currentMouseState.XButton1 == ButtonState.Released);
case MouseButtons.ExtraButton2:
return (_lastMouseState.XButton2 == ButtonState.Pressed &&
_currentMouseState.XButton2 == ButtonState.Released);
default:
return false;
}
}
}
}

View File

@ -0,0 +1,163 @@
#region File Description
//-----------------------------------------------------------------------------
// LoadingScreen.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.Graphics;
using GameStateManagement;
#endregion
namespace GameStateManagement
{
/// <summary>
/// The loading screen coordinates transitions between the menu system and the
/// game itself. Normally one screen will transition off at the same time as
/// the next screen is transitioning on, but for larger transitions that can
/// take a longer time to load their data, we want the menu system to be entirely
/// gone before we start loading the game. This is done as follows:
///
/// - Tell all the existing screens to transition off.
/// - Activate a loading screen, which will transition on at the same time.
/// - The loading screen watches the state of the previous screens.
/// - When it sees they have finished transitioning off, it activates the real
/// next screen, which may take a long time to load its data. The loading
/// screen will be the only thing displayed while this load is taking place.
/// </summary>
class LoadingScreen : GameScreen
{
#region Fields
bool loadingIsSlow;
bool otherScreensAreGone;
GameScreen[] screensToLoad;
#endregion
#region Initialization
/// <summary>
/// The constructor is private: loading screens should
/// be activated via the static Load method instead.
/// </summary>
private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow,
GameScreen[] screensToLoad)
{
this.loadingIsSlow = loadingIsSlow;
this.screensToLoad = screensToLoad;
TransitionOnTime = TimeSpan.FromSeconds(0.5);
}
/// <summary>
/// Activates the loading screen.
/// </summary>
public static void Load(ScreenManager screenManager, bool loadingIsSlow,
PlayerIndex? controllingPlayer,
params GameScreen[] screensToLoad)
{
// Tell all the current screens to transition off.
foreach (GameScreen screen in screenManager.GetScreens())
screen.ExitScreen();
// Create and activate the loading screen.
LoadingScreen loadingScreen = new LoadingScreen(screenManager,
loadingIsSlow,
screensToLoad);
screenManager.AddScreen(loadingScreen, controllingPlayer);
}
#endregion
#region Update and Draw
/// <summary>
/// Updates the loading screen.
/// </summary>
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
// If all the previous screens have finished transitioning
// off, it is time to actually perform the load.
if (otherScreensAreGone)
{
ScreenManager.RemoveScreen(this);
foreach (GameScreen screen in screensToLoad)
{
if (screen != null)
{
ScreenManager.AddScreen(screen, ControllingPlayer);
}
}
// Once the load has finished, we use ResetElapsedTime to tell
// the game timing mechanism that we have just finished a very
// long frame, and that it should not try to catch up.
ScreenManager.Game.ResetElapsedTime();
}
}
/// <summary>
/// Draws the loading screen.
/// </summary>
public override void Draw(GameTime gameTime)
{
// If we are the only active screen, that means all the previous screens
// must have finished transitioning off. We check for this in the Draw
// method, rather than in Update, because it isn't enough just for the
// screens to be gone: in order for the transition to look good we must
// have actually drawn a frame without them before we perform the load.
if ((ScreenState == ScreenState.Active) &&
(ScreenManager.GetScreens().Length == 1))
{
otherScreensAreGone = true;
}
// The gameplay screen takes a while to load, so we display a loading
// message while that is going on, but the menus load very quickly, and
// it would look silly if we flashed this up for just a fraction of a
// second while returning from the game to the menus. This parameter
// tells us how long the loading is going to take, so we know whether
// to bother drawing the message.
if (loadingIsSlow)
{
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
SpriteFont font = ScreenManager.Font;
const string message = "Loading...";
// Center the 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;
Color color = Color.White * TransitionAlpha;
// Draw the text.
spriteBatch.Begin();
spriteBatch.DrawString(font, message, textPosition, color);
spriteBatch.End();
}
}
#endregion
}
}

View File

@ -3,8 +3,9 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.SamplesFramework;
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
public class LogoScreen : GameScreen
{
@ -29,36 +30,40 @@ namespace FarseerPhysics.SamplesFramework
/// used the shared ContentManager provided by the Game class, the content
/// would remain loaded forever.
/// </summary>
public override void LoadContent()
public override void Activate(bool instancePreserved)
{
if (_content == null)
if (!instancePreserved)
{
_content = new ContentManager(ScreenManager.Game.Services, "Content");
if (_content == null)
{
_content = new ContentManager(ScreenManager.Game.Services, "Content");
}
_farseerLogoTexture = _content.Load<Texture2D>("Common/logo");
Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
int rectHeight = (int)(viewport.Height * LogoScreenHeightRatio);
int rectWidth = (int)(rectHeight * LogoWidthHeightRatio);
int posX = viewport.Bounds.Center.X - rectWidth / 2;
int posY = viewport.Bounds.Center.Y - rectHeight / 2;
_destination = new Rectangle(posX, posY, rectWidth, rectHeight);
}
_farseerLogoTexture = _content.Load<Texture2D>("Common/logo");
Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
int rectHeight = (int)(viewport.Height * LogoScreenHeightRatio);
int rectWidth = (int)(rectHeight * LogoWidthHeightRatio);
int posX = viewport.Bounds.Center.X - rectWidth / 2;
int posY = viewport.Bounds.Center.Y - rectHeight / 2;
_destination = new Rectangle(posX, posY, rectWidth, rectHeight);
}
/// <summary>
/// Unloads graphics content for this screen.
/// </summary>
public override void UnloadContent()
public override void Unload()
{
_content.Unload();
}
public override void HandleInput(InputHelper input, GameTime gameTime)
public override void HandleInput(GameTime gameTime, InputState input)
{
if (input.KeyboardState.GetPressedKeys().Length > 0 ||
input.GamePadState.IsButtonDown(Buttons.A | Buttons.Start | Buttons.Back) ||
//input.
if (input.CurrentKeyboardStates[0].GetPressedKeys().Length > 0 ||
input.CurrentGamePadStates[0].IsButtonDown(Buttons.A | Buttons.Start | Buttons.Back) ||
input.MouseState.LeftButton == ButtonState.Pressed)
{
_duration = TimeSpan.Zero;

View File

@ -0,0 +1,99 @@
#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using Microsoft.Xna.Framework;
using GameStateManagementSample;
#endregion
namespace GameStateManagement
{
/// <summary>
/// The main menu screen is the first thing displayed when the game starts up.
/// </summary>
class MainMenuScreen : MenuScreen
{
#region Initialization
/// <summary>
/// Constructor fills in the menu contents.
/// </summary>
public MainMenuScreen()
: base("Main Menu")
{
// Create our menu entries.
MenuEntry playGameMenuEntry = new MenuEntry("Play Game");
MenuEntry optionsMenuEntry = new MenuEntry("Options");
MenuEntry exitMenuEntry = new MenuEntry("Exit");
// Hook up menu event handlers.
playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
optionsMenuEntry.Selected += OptionsMenuEntrySelected;
exitMenuEntry.Selected += OnCancel;
// Add entries to the menu.
MenuEntries.Add(playGameMenuEntry);
MenuEntries.Add(optionsMenuEntry);
MenuEntries.Add(exitMenuEntry);
}
#endregion
#region Handle Input
/// <summary>
/// Event handler for when the Play Game menu entry is selected.
/// </summary>
void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
new GameplayScreen());
}
/// <summary>
/// Event handler for when the Options menu entry is selected.
/// </summary>
void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
ScreenManager.AddScreen(new OptionsMenuScreen(), e.PlayerIndex);
}
/// <summary>
/// When the user cancels the main menu, ask if they want to exit the sample.
/// </summary>
protected override void OnCancel(PlayerIndex playerIndex)
{
const string message = "Are you sure you want to exit this sample?";
MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);
confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;
ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
}
/// <summary>
/// Event handler for when the user selects ok on the "are you sure
/// you want to exit" message box.
/// </summary>
void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
{
ScreenManager.Game.Exit();
}
#endregion
}
}

View File

@ -2,7 +2,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
/// <summary>
/// Helper class represents a single entry in a MenuScreen. By default this

View File

@ -1,39 +1,35 @@
using System;
#region File Description
//-----------------------------------------------------------------------------
// MenuEntry.cs
//
// 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.Graphics;
using GameStateManagement;
#endregion
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
public enum EntryType
{
Screen,
Separator,
ExitItem,
BackItem
}
/// <summary>
/// Helper class represents a single entry in a MenuScreen. By default this
/// just draws the entry text string, but it can be customized to display menu
/// entries in different ways. This also provides an event that will be raised
/// when the menu entry is selected.
/// </summary>
public sealed class MenuEntry
public class MenuEntry
{
private float _alpha;
private Vector2 _baseOrigin;
private float _height;
private MenuScreen _menu;
#region Fields
/// <summary>
/// The position at which the entry is drawn. This is set by the MenuScreen
/// each frame in Update.
/// The text rendered for this entry.
/// </summary>
private Vector2 _position;
private float _scale;
private GameScreen _screen;
string text;
/// <summary>
/// Tracks a fading selection effect on the entry.
@ -41,28 +37,17 @@ namespace FarseerPhysics.SamplesFramework
/// <remarks>
/// The entries transition out of the selection effect when they are deselected.
/// </remarks>
private float _selectionFade;
float selectionFade;
/// <summary>
/// The text rendered for this entry.
/// The position at which the entry is drawn. This is set by the MenuScreen
/// each frame in Update.
/// </summary>
private string _text;
Vector2 position;
private EntryType _type;
private float _width;
#endregion
/// <summary>
/// Constructs a new menu entry with the specified text.
/// </summary>
public MenuEntry(MenuScreen menu, string text, EntryType type, GameScreen screen)
{
_text = text;
_screen = screen;
_type = type;
_menu = menu;
_scale = 0.9f;
_alpha = 1.0f;
}
#region Properties
/// <summary>
@ -70,124 +55,138 @@ namespace FarseerPhysics.SamplesFramework
/// </summary>
public string Text
{
get { return _text; }
set { _text = value; }
get { return text; }
set { text = value; }
}
/// <summary>
/// Gets or sets the position at which to draw this menu entry.
/// </summary>
public Vector2 Position
{
get { return _position; }
set { _position = value; }
get { return position; }
set { position = value; }
}
public float Alpha
#endregion
#region Events
/// <summary>
/// Event raised when the menu entry is selected.
/// </summary>
public event EventHandler<PlayerIndexEventArgs> Selected;
/// <summary>
/// Method for raising the Selected event.
/// </summary>
protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
{
get { return _alpha; }
set { _alpha = value; }
if (Selected != null)
Selected(this, new PlayerIndexEventArgs(playerIndex));
}
public GameScreen Screen
#endregion
#region Initialization
/// <summary>
/// Constructs a new menu entry with the specified text.
/// </summary>
public MenuEntry(string text)
{
get { return _screen; }
this.text = text;
}
public void Initialize()
{
SpriteFont font = _menu.ScreenManager.Fonts.MenuSpriteFont;
_baseOrigin = new Vector2(font.MeasureString(Text).X, font.MeasureString("M").Y) * 0.5f;
#endregion
_width = font.MeasureString(Text).X * 0.8f;
_height = font.MeasureString("M").Y * 0.8f;
}
#region Update and Draw
public bool IsExitItem()
{
return _type == EntryType.ExitItem;
}
public bool IsSelectable()
{
return _type != EntryType.Separator;
}
public bool IsBackItem()
{
return _type == EntryType.BackItem;
}
/// <summary>
/// Updates the menu entry.
/// </summary>
public void Update(bool isSelected, GameTime gameTime)
public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
{
// there is no such thing as a selected item on Windows Phone, so we always
// force isSelected to be false
#if WINDOWS_PHONE
isSelected = false;
#endif
// When the menu selection changes, entries gradually fade between
// their selected and deselected appearance, rather than instantly
// popping to the new state.
if (_type != EntryType.Separator)
{
float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
if (isSelected)
{
_selectionFade = Math.Min(_selectionFade + fadeSpeed, 1f);
}
else
{
_selectionFade = Math.Max(_selectionFade - fadeSpeed, 0f);
}
_scale = 0.7f + 0.1f * _selectionFade;
}
float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
if (isSelected)
selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
else
selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
}
/// <summary>
/// Draws the menu entry. This can be overridden to customize the appearance.
/// </summary>
public void Draw()
public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
{
SpriteFont font = _menu.ScreenManager.Fonts.MenuSpriteFont;
SpriteBatch batch = _menu.ScreenManager.SpriteBatch;
// there is no such thing as a selected item on Windows Phone, so we always
// force isSelected to be false
#if WINDOWS_PHONE
isSelected = false;
#endif
Color color;
if (_type == EntryType.Separator)
{
color = Color.DarkOrange;
}
else
{
// Draw the selected entry in yellow, otherwise white
color = Color.Lerp(Color.White, new Color(255, 210, 0), _selectionFade);
}
color *= _alpha;
// Draw the selected entry in yellow, otherwise white.
Color color = isSelected ? Color.Yellow : Color.White;
// Pulsate the size of the selected menu entry.
double time = gameTime.TotalGameTime.TotalSeconds;
float pulsate = (float)Math.Sin(time * 6) + 1;
float scale = 1 + pulsate * 0.05f * selectionFade;
// Modify the alpha to fade text out during transitions.
color *= screen.TransitionAlpha;
// Draw text, centered on the middle of each line.
batch.DrawString(font, _text, _position - _baseOrigin * _scale + Vector2.One,
Color.DarkSlateGray * _alpha * _alpha, 0, Vector2.Zero, _scale, SpriteEffects.None, 0);
batch.DrawString(font, _text, _position - _baseOrigin * _scale, color, 0, Vector2.Zero, _scale,
SpriteEffects.None, 0);
ScreenManager screenManager = screen.ScreenManager;
SpriteBatch spriteBatch = screenManager.SpriteBatch;
SpriteFont font = screenManager.Font;
Vector2 origin = new Vector2(0, font.LineSpacing / 2);
spriteBatch.DrawString(font, text, position, color, 0,
origin, scale, SpriteEffects.None, 0);
}
/// <summary>
/// Queries how much space this menu entry requires.
/// </summary>
public int GetHeight()
public virtual int GetHeight(MenuScreen screen)
{
return (int)_height;
return screen.ScreenManager.Font.LineSpacing;
}
/// <summary>
/// Queries how wide the entry is, used for centering on the screen.
/// </summary>
public int GetWidth()
public virtual int GetWidth(MenuScreen screen)
{
return (int)_width;
return (int)screen.ScreenManager.Font.MeasureString(Text).X;
}
#endregion
}
}
}

View File

@ -1,9 +1,23 @@
#region File Description
//-----------------------------------------------------------------------------
// MenuScreen.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.SamplesFramework;
#endregion
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
/// <summary>
/// Base class for screens that contain a menu of options. The user can
@ -11,225 +25,207 @@ namespace FarseerPhysics.SamplesFramework
/// </summary>
public class MenuScreen : GameScreen
{
#if WINDOWS || XBOX
protected const float NumEntries = 15;
#elif WINDOWS_PHONE
protected const float NumEntries = 9;
#endif
protected List<MenuEntry> _menuEntries = new List<MenuEntry>();
protected string _menuTitle;
protected Vector2 _titlePosition;
protected Vector2 _titleOrigin;
protected int _selectedEntry;
protected float _menuBorderTop;
protected float _menuBorderBottom;
protected float _menuBorderMargin;
protected float _menuOffset;
protected float _maxOffset;
#region Fields
protected Texture2D _texScrollButton;
protected Texture2D _texSlider;
// the number of pixels to pad above and below menu entries for touch input
const int menuEntryPadding = 10;
private List<MenuEntry> menuEntries = new List<MenuEntry>();
int selectedEntry = 0;
string menuTitle;
InputAction menuUp;
InputAction menuDown;
InputAction menuSelect;
InputAction menuCancel;
#endregion
#region Properties
/// <summary>
/// Gets the list of menu entries, so derived classes can add
/// or change the menu contents.
/// </summary>
protected IList<MenuEntry> MenuEntries
{
get { return menuEntries; }
}
#endregion
#region Initialization
protected MenuButton _scrollUp;
protected MenuButton _scrollDown;
protected MenuButton _scrollSlider;
protected bool _scrollLock;
/// <summary>
/// Constructor.
/// </summary>
public MenuScreen(string menuTitle)
{
_menuTitle = menuTitle;
this.menuTitle = menuTitle;
// menus generally only need Tap for menu selection
EnabledGestures = GestureType.Tap;
TransitionOnTime = TimeSpan.FromSeconds(0.7);
TransitionOffTime = TimeSpan.FromSeconds(0.7);
HasCursor = true;
TransitionOnTime = TimeSpan.FromSeconds(0.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
menuUp = new InputAction(
new Buttons[] { Buttons.DPadUp, Buttons.LeftThumbstickUp },
new Keys[] { Keys.Up },
true);
menuDown = new InputAction(
new Buttons[] { Buttons.DPadDown, Buttons.LeftThumbstickDown },
new Keys[] { Keys.Down },
true);
menuSelect = new InputAction(
new Buttons[] { Buttons.A, Buttons.Start },
new Keys[] { Keys.Enter, Keys.Space },
true);
menuCancel = new InputAction(
new Buttons[] { Buttons.B, Buttons.Back },
new Keys[] { Keys.Escape },
true);
}
public void AddMenuItem(string name, EntryType type, GameScreen screen)
#endregion
public void AddMenuItem(string name)
{
MenuEntry entry = new MenuEntry(this, name, type, screen);
_menuEntries.Add(entry);
menuEntries.Add(new MenuEntry(name));
}
public void AddMenuItem(MenuEntry me)
{
_menuEntries.Add(me);
}
public override void LoadContent()
{
base.LoadContent();
_texScrollButton = ScreenManager.Content.Load<Texture2D>("Common/arrow");
_texSlider = ScreenManager.Content.Load<Texture2D>("Common/slider");
//Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
//float scrollBarPos = viewport.Width / 2f;
//scrollBarPos -= _texScrollButton.Width + 2f;
InitMenu();
}
public void InitMenu()
{
Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
SpriteFont font = ScreenManager.Fonts.MenuSpriteFont;
float scrollBarPos = viewport.Width / 2f;
for (int i = 0; i < _menuEntries.Count; ++i)
{
_menuEntries[i].Initialize();
scrollBarPos = Math.Min(scrollBarPos,
(viewport.Width - _menuEntries[i].GetWidth()) / 2f);
}
_titleOrigin = font.MeasureString(_menuTitle) / 2f;
_titlePosition = new Vector2(viewport.Width / 2f, font.MeasureString("M").Y / 2f + 10f);
_menuBorderMargin = font.MeasureString("M").Y * 0.8f;
_menuBorderTop = (viewport.Height - _menuBorderMargin * (NumEntries - 1)) / 2f;
_menuBorderBottom = (viewport.Height + _menuBorderMargin * (NumEntries - 1)) / 2f;
_menuOffset = 0f;
_maxOffset = Math.Max(0f, (_menuEntries.Count - NumEntries) * _menuBorderMargin);
_scrollUp = new MenuButton(_texScrollButton, false,
new Vector2(scrollBarPos, _menuBorderTop - _texScrollButton.Height), this);
_scrollDown = new MenuButton(_texScrollButton, true,
new Vector2(scrollBarPos, _menuBorderBottom + _texScrollButton.Height), this);
_scrollSlider = new MenuButton(_texSlider, false, new Vector2(scrollBarPos, _menuBorderTop), this);
_scrollLock = false;
}
#region Handle Input
/// <summary>
/// Returns the index of the menu entry at the position of the given mouse state.
/// Allows the screen to create the hit bounds for a particular menu entry.
/// </summary>
/// <returns>Index of menu entry if valid, -1 otherwise</returns>
private int GetMenuEntryAt(Vector2 position)
protected virtual Rectangle GetMenuEntryHitBounds(MenuEntry entry)
{
if (this.TransitionPosition == 0f && this.ScreenState == SamplesFramework.ScreenState.Active)
{
int index = 0;
foreach (MenuEntry entry in _menuEntries)
{
float width = entry.GetWidth();
float height = entry.GetHeight();
Rectangle rect = new Rectangle((int)(entry.Position.X - width / 2f),
(int)(entry.Position.Y - height / 2f),
(int)width, (int)height);
if (rect.Contains((int)position.X, (int)position.Y) && entry.Alpha > 0.1f)
{
return index;
}
++index;
}
}
return -1;
// the hit bounds are the entire width of the screen, and the height of the entry
// with some additional padding above and below.
return new Rectangle(
0,
(int)entry.Position.Y - menuEntryPadding,
ScreenManager.GraphicsDevice.Viewport.Width,
entry.GetHeight(this) + (menuEntryPadding * 2));
}
/// <summary>
/// Responds to user input, changing the selected entry and accepting
/// or cancelling the menu.
/// </summary>
public override void HandleInput(InputHelper input, GameTime gameTime)
public override void HandleInput(GameTime gameTime, InputState input)
{
// Mouse or touch on a menu item
int hoverIndex = GetMenuEntryAt(input.Cursor);
if (hoverIndex > -1 && _menuEntries[hoverIndex].IsSelectable() && !_scrollLock)
// For input tests 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
// OnSelectEntry and OnCancel, so they can tell which player triggered them.
#if WINDOWS || XBOX360
PlayerIndex playerIndex;
// Move to the previous menu entry?
if (menuUp.Evaluate(input, ControllingPlayer, out playerIndex))
{
_selectedEntry = hoverIndex;
}
else
{
_selectedEntry = -1;
selectedEntry--;
if (selectedEntry < 0)
selectedEntry = menuEntries.Count - 1;
}
_scrollSlider.Hover = false;
if (input.IsCursorValid)
// Move to the next menu entry?
if (menuDown.Evaluate(input, ControllingPlayer, out playerIndex))
{
_scrollUp.Collide(input.Cursor);
_scrollDown.Collide(input.Cursor);
_scrollSlider.Collide(input.Cursor);
}
else
{
_scrollUp.Hover = false;
_scrollDown.Hover = false;
_scrollLock = false;
selectedEntry++;
if (selectedEntry >= menuEntries.Count)
selectedEntry = 0;
}
// Accept or cancel the menu?
if (input.IsMenuSelect() && _selectedEntry != -1)
if (menuSelect.Evaluate(input, ControllingPlayer, out playerIndex))
{
if (_menuEntries[_selectedEntry].IsExitItem())
OnSelectEntry(selectedEntry, playerIndex);
}
else if (menuCancel.Evaluate(input, ControllingPlayer, out playerIndex))
{
OnCancel(playerIndex);
}
#endif
#if WINDOWS_PHONE
//selectedEntry = 1;
PlayerIndex player;
if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
{
OnCancel(player);
}
// look for any taps that occurred and select any entries that were tapped
foreach (GestureSample gesture in input.Gestures)
{
//System.Diagnostics.Debugger.Break();
if (gesture.GestureType == GestureType.Tap)
{
ScreenManager.Game.Exit();
}
else if (_menuEntries[_selectedEntry].IsBackItem())
{
this.ExitScreen();
}
else if (_menuEntries[_selectedEntry].Screen != null)
{
ScreenManager.AddScreen(_menuEntries[_selectedEntry].Screen);
if (_menuEntries[_selectedEntry].Screen is IDemoScreen)
// convert the position to a Point that we can test against a Rectangle
Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
// iterate the entries to see if any were tapped
for (int i = 0; i < menuEntries.Count; i++)
{
ScreenManager.AddScreen(
new MessageBoxScreen((_menuEntries[_selectedEntry].Screen as IDemoScreen).GetDetails()));
MenuEntry menuEntry = menuEntries[i];
if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation))
{
// select the entry. since gestures are only available on Windows Phone,
// we can safely pass PlayerIndex.One to all entries since there is only
// one player on Windows Phone.
OnSelectEntry(i, PlayerIndex.One);
}
}
}
}
else if (input.IsMenuCancel())
{
if (this.ScreenState == SamplesFramework.ScreenState.Active && this.TransitionPosition == 0 && this.TransitionAlpha == 1)
{
//GameScreen[] screens = ScreenManager.GetScreens();
//if (screens[screens.Length - 1] is BackgroundScreen ||| screens.Length )
// ScreenManager.Game.Exit();
//if (ScreenManager.GetScreens().Length == 2)
// ScreenManager.Game.Exit();
//else
this.ExitScreen();
}
//ScreenManager.Game.Exit();
}
if (input.IsMenuPressed())
{
if (_scrollUp.Hover)
{
_menuOffset = Math.Max(_menuOffset - 200f * (float)gameTime.ElapsedGameTime.TotalSeconds, 0f);
_scrollLock = false;
}
if (_scrollDown.Hover)
{
_menuOffset = Math.Min(_menuOffset + 200f * (float)gameTime.ElapsedGameTime.TotalSeconds, _maxOffset);
_scrollLock = false;
}
if (_scrollSlider.Hover)
{
_scrollLock = true;
}
}
if (input.IsMenuReleased())
{
_scrollLock = false;
}
if (_scrollLock)
{
_scrollSlider.Hover = true;
_menuOffset = Math.Max(Math.Min(((input.Cursor.Y - _menuBorderTop) / (_menuBorderBottom - _menuBorderTop)) * _maxOffset, _maxOffset), 0f);
}
#endif
}
/// <summary>
/// Handler for when the user has chosen a menu entry.
/// </summary>
protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
{
menuEntries[entryIndex].OnSelectEntry(playerIndex);
}
/// <summary>
/// Handler for when the user has cancelled the menu.
/// </summary>
protected virtual void OnCancel(PlayerIndex playerIndex)
{
ExitScreen();
}
/// <summary>
/// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
/// </summary>
protected void OnCancel(object sender, PlayerIndexEventArgs e)
{
OnCancel(e.PlayerIndex);
}
#endregion
#region Update and Draw
/// <summary>
/// Allows the screen the chance to position the menu entries. By default
/// all menu entries are lined up in a vertical list, centered on the screen.
@ -241,69 +237,49 @@ namespace FarseerPhysics.SamplesFramework
// the movement slow down as it nears the end).
float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
Vector2 position = Vector2.Zero;
position.Y = _menuBorderTop - _menuOffset;
// start at Y = 175; each X value is generated per entry
Vector2 position = new Vector2(0f, 175f);
// update each menu entry's location in turn
for (int i = 0; i < _menuEntries.Count; ++i)
for (int i = 0; i < menuEntries.Count; i++)
{
position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2f;
MenuEntry menuEntry = menuEntries[i];
// each entry is to be centered horizontally
position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;
if (ScreenState == ScreenState.TransitionOn)
{
position.X -= transitionOffset * 256;
}
else
{
position.X += transitionOffset * 256;
}
position.X += transitionOffset * 512;
// set the entry's position
_menuEntries[i].Position = position;
if (position.Y < _menuBorderTop)
{
_menuEntries[i].Alpha = 1f -
Math.Min(_menuBorderTop - position.Y, _menuBorderMargin) / _menuBorderMargin;
}
else if (position.Y > _menuBorderBottom)
{
_menuEntries[i].Alpha = 1f -
Math.Min(position.Y - _menuBorderBottom, _menuBorderMargin) /
_menuBorderMargin;
}
else
{
_menuEntries[i].Alpha = 1f;
}
menuEntry.Position = position;
// move down for the next entry the size of this entry
position.Y += _menuEntries[i].GetHeight();
position.Y += menuEntry.GetHeight(this);
}
Vector2 scrollPos = _scrollSlider.Position;
scrollPos.Y = MathHelper.Lerp(_menuBorderTop, _menuBorderBottom, _menuOffset / _maxOffset);
_scrollSlider.Position = scrollPos;
}
/// <summary>
/// Updates the menu.
/// </summary>
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
// Update each nested MenuEntry object.
for (int i = 0; i < _menuEntries.Count; ++i)
for (int i = 0; i < menuEntries.Count; i++)
{
bool isSelected = IsActive && (i == _selectedEntry);
_menuEntries[i].Update(isSelected, gameTime);
}
bool isSelected = IsActive && (i == selectedEntry);
_scrollUp.Update(gameTime);
_scrollDown.Update(gameTime);
_scrollSlider.Update(gameTime);
menuEntries[i].Update(this, isSelected, gameTime);
}
}
/// <summary>
/// Draws the menu.
/// </summary>
@ -312,30 +288,42 @@ namespace FarseerPhysics.SamplesFramework
// make sure our entries are in the right place before we draw them
UpdateMenuEntryLocations();
GraphicsDevice graphics = ScreenManager.GraphicsDevice;
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
SpriteFont font = ScreenManager.Fonts.MenuSpriteFont;
SpriteFont font = ScreenManager.Font;
spriteBatch.Begin();
// Draw each menu entry in turn.
for (int i = 0; i < _menuEntries.Count; ++i)
for (int i = 0; i < menuEntries.Count; i++)
{
bool isSelected = IsActive && (i == _selectedEntry);
_menuEntries[i].Draw();
MenuEntry menuEntry = menuEntries[i];
bool isSelected = IsActive && (i == selectedEntry);
menuEntry.Draw(this, isSelected, gameTime);
}
// Make the menu slide into place during transitions, using a
// power curve to make things look more interesting (this makes
// the movement slow down as it nears the end).
Vector2 transitionOffset = new Vector2(0f, (float)Math.Pow(TransitionPosition, 2) * 100f);
float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
// Draw the menu title centered on the screen
Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
float titleScale = 1.25f;
titlePosition.Y -= transitionOffset * 100;
spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
titleOrigin, titleScale, SpriteEffects.None, 0);
spriteBatch.DrawString(font, _menuTitle, _titlePosition - transitionOffset + Vector2.One * 2f, Color.Black, 0,
_titleOrigin, 1f, SpriteEffects.None, 0);
spriteBatch.DrawString(font, _menuTitle, _titlePosition - transitionOffset, new Color(255, 210, 0), 0,
_titleOrigin, 1f, SpriteEffects.None, 0);
_scrollUp.Draw();
_scrollSlider.Draw();
_scrollDown.Draw();
spriteBatch.End();
}
#endregion
}
}
}

View File

@ -1,36 +1,87 @@
using System;
#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 FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
/// <summary>
/// A popup message box screen, used to display "are you sure?"
/// confirmation messages.
/// </summary>
public class MessageBoxScreen : GameScreen
class MessageBoxScreen : GameScreen
{
protected Rectangle _backgroundRectangle;
protected Texture2D _gradientTexture;
protected string _message;
protected Vector2 _textPosition;
#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)
{
_message = message;
const string usageText = "\nA button, Space, Enter = ok" +
"\nB button, Esc = cancel";
if (includeUsageText)
this.message = message + usageText;
else
this.message = message;
IsPopup = true;
HasCursor = true;
TransitionOnTime = TimeSpan.FromSeconds(0.4);
TransitionOffTime = TimeSpan.FromSeconds(0.4);
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);
}
public MessageBoxScreen()
{
IsPopup = true;
}
/// <summary>
/// Loads graphics content for this screen. This uses the shared ContentManager
@ -38,62 +89,98 @@ namespace FarseerPhysics.SamplesFramework
/// 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 LoadContent()
public override void Activate(bool instancePreserved)
{
SpriteFont font = ScreenManager.Fonts.DetailsFont;
ContentManager content = ScreenManager.Game.Content;
_gradientTexture = content.Load<Texture2D>("Common/popup");
// 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);
_textPosition = (viewportSize - textSize) / 2;
// The background includes a border somewhat larger than the text itself.
const int hPad = 32;
const int vPad = 16;
_backgroundRectangle = new Rectangle((int)_textPosition.X - hPad,
(int)_textPosition.Y - vPad,
(int)textSize.X + hPad * 2,
(int)textSize.Y + vPad * 2);
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(InputHelper input, GameTime gameTime)
public override void HandleInput(GameTime gameTime, InputState input)
{
PlayerIndex playerIndex;
if (input.IsMenuSelect() || input.IsMenuCancel() ||
input.IsNewMouseButtonPress(MouseButtons.LeftButton))
// 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.Fonts.DetailsFont;
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 * (2f / 3f);
Color color = Color.White * TransitionAlpha;
spriteBatch.Begin();
// Draw the background rectangle.
spriteBatch.Draw(_gradientTexture, _backgroundRectangle, color);
spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
// Draw the message box text.
spriteBatch.DrawString(font, _message, _textPosition + Vector2.One, Color.Black);
spriteBatch.DrawString(font, _message, _textPosition, Color.White);
spriteBatch.DrawString(font, message, textPosition, color);
spriteBatch.End();
}
#endregion
}
}
}

View File

@ -0,0 +1,149 @@
#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using Microsoft.Xna.Framework;
#endregion
namespace GameStateManagement
{
/// <summary>
/// The options screen is brought up over the top of the main menu
/// screen, and gives the user a chance to configure the game
/// in various hopefully useful ways.
/// </summary>
class OptionsMenuScreen : MenuScreen
{
#region Fields
MenuEntry ungulateMenuEntry;
MenuEntry languageMenuEntry;
MenuEntry frobnicateMenuEntry;
MenuEntry elfMenuEntry;
enum Ungulate
{
BactrianCamel,
Dromedary,
Llama,
}
static Ungulate currentUngulate = Ungulate.Dromedary;
static string[] languages = { "C#", "French", "Deoxyribonucleic acid" };
static int currentLanguage = 0;
static bool frobnicate = true;
static int elf = 23;
#endregion
#region Initialization
/// <summary>
/// Constructor.
/// </summary>
public OptionsMenuScreen()
: base("Options")
{
// Create our menu entries.
ungulateMenuEntry = new MenuEntry(string.Empty);
languageMenuEntry = new MenuEntry(string.Empty);
frobnicateMenuEntry = new MenuEntry(string.Empty);
elfMenuEntry = new MenuEntry(string.Empty);
SetMenuEntryText();
MenuEntry back = new MenuEntry("Back");
// Hook up menu event handlers.
ungulateMenuEntry.Selected += UngulateMenuEntrySelected;
languageMenuEntry.Selected += LanguageMenuEntrySelected;
frobnicateMenuEntry.Selected += FrobnicateMenuEntrySelected;
elfMenuEntry.Selected += ElfMenuEntrySelected;
back.Selected += OnCancel;
// Add entries to the menu.
MenuEntries.Add(ungulateMenuEntry);
MenuEntries.Add(languageMenuEntry);
MenuEntries.Add(frobnicateMenuEntry);
MenuEntries.Add(elfMenuEntry);
MenuEntries.Add(back);
}
/// <summary>
/// Fills in the latest values for the options screen menu text.
/// </summary>
void SetMenuEntryText()
{
ungulateMenuEntry.Text = "Preferred ungulate: " + currentUngulate;
languageMenuEntry.Text = "Language: " + languages[currentLanguage];
frobnicateMenuEntry.Text = "Frobnicate: " + (frobnicate ? "on" : "off");
elfMenuEntry.Text = "elf: " + elf;
}
#endregion
#region Handle Input
/// <summary>
/// Event handler for when the Ungulate menu entry is selected.
/// </summary>
void UngulateMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
currentUngulate++;
if (currentUngulate > Ungulate.Llama)
currentUngulate = 0;
SetMenuEntryText();
}
/// <summary>
/// Event handler for when the Language menu entry is selected.
/// </summary>
void LanguageMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
currentLanguage = (currentLanguage + 1) % languages.Length;
SetMenuEntryText();
}
/// <summary>
/// Event handler for when the Frobnicate menu entry is selected.
/// </summary>
void FrobnicateMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
frobnicate = !frobnicate;
SetMenuEntryText();
}
/// <summary>
/// Event handler for when the Elf menu entry is selected.
/// </summary>
void ElfMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
elf++;
SetMenuEntryText();
}
#endregion
}
}

View File

@ -0,0 +1,79 @@
#region File Description
//-----------------------------------------------------------------------------
// PauseMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using Microsoft.Xna.Framework;
#endregion
namespace GameStateManagement
{
/// <summary>
/// The pause menu comes up over the top of the game,
/// giving the player options to resume or quit.
/// </summary>
class PauseMenuScreen : MenuScreen
{
#region Initialization
/// <summary>
/// Constructor.
/// </summary>
public PauseMenuScreen()
: base("Paused")
{
// Create our menu entries.
MenuEntry resumeGameMenuEntry = new MenuEntry("Resume Game");
MenuEntry quitGameMenuEntry = new MenuEntry("Quit Game");
// Hook up menu event handlers.
resumeGameMenuEntry.Selected += OnCancel;
quitGameMenuEntry.Selected += QuitGameMenuEntrySelected;
// Add entries to the menu.
MenuEntries.Add(resumeGameMenuEntry);
MenuEntries.Add(quitGameMenuEntry);
}
#endregion
#region Handle Input
/// <summary>
/// Event handler for when the Quit Game menu entry is selected.
/// </summary>
void QuitGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
{
const string message = "Are you sure you want to quit this game?";
MessageBoxScreen confirmQuitMessageBox = new MessageBoxScreen(message);
confirmQuitMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;
ScreenManager.AddScreen(confirmQuitMessageBox, ControllingPlayer);
}
/// <summary>
/// Event handler for when the user selects ok on the "are you sure
/// you want to quit" message box. This uses the loading screen to
/// transition from the game back to the main menu screen.
/// </summary>
void ConfirmQuitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
{
LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
new MainMenuScreen());
}
#endregion
}
}

View File

@ -0,0 +1,66 @@
#region File Description
//-----------------------------------------------------------------------------
// PhoneMainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using GameStateManagement;
using GameStateManagementSample;
using Microsoft.Xna.Framework;
namespace GameStateManagement
{
class PhoneMainMenuScreen : PhoneMenuScreen
{
public PhoneMainMenuScreen()
: base("Main Menu")
{
// Create a button to start the game
Button playButton = new Button("Play");
playButton.Tapped += playButton_Tapped;
MenuButtons.Add(playButton);
// Create two buttons to toggle sound effects and music. This sample just shows one way
// of making and using these buttons; it doesn't actually have sound effects or music
BooleanButton sfxButton = new BooleanButton("Sound Effects", true);
sfxButton.Tapped += sfxButton_Tapped;
MenuButtons.Add(sfxButton);
BooleanButton musicButton = new BooleanButton("Music", true);
musicButton.Tapped += musicButton_Tapped;
MenuButtons.Add(musicButton);
}
void playButton_Tapped(object sender, EventArgs e)
{
// When the "Play" button is tapped, we load the GameplayScreen
LoadingScreen.Load(ScreenManager, true, PlayerIndex.One, new GameplayScreen());
}
void sfxButton_Tapped(object sender, EventArgs e)
{
BooleanButton button = sender as BooleanButton;
// In a real game, you'd want to store away the value of
// the button to turn off sounds here. :)
}
void musicButton_Tapped(object sender, EventArgs e)
{
BooleanButton button = sender as BooleanButton;
// In a real game, you'd want to store away the value of
// the button to turn off music here. :)
}
protected override void OnCancel()
{
ScreenManager.Game.Exit();
base.OnCancel();
}
}
}

View File

@ -0,0 +1,149 @@
#region File Description
//-----------------------------------------------------------------------------
// PhoneMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using System.Collections.Generic;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
namespace GameStateManagement
{
/// <summary>
/// Provides a basic base screen for menus on Windows Phone leveraging the Button class.
/// </summary>
class PhoneMenuScreen : GameScreen
{
List<Button> menuButtons = new List<Button>();
string menuTitle;
InputAction menuCancel;
/// <summary>
/// Gets the list of buttons, so derived classes can add or change the menu contents.
/// </summary>
protected IList<Button> MenuButtons
{
get { return menuButtons; }
}
/// <summary>
/// Creates the PhoneMenuScreen with a particular title.
/// </summary>
/// <param name="title">The title of the screen</param>
public PhoneMenuScreen(string title)
{
menuTitle = title;
TransitionOnTime = TimeSpan.FromSeconds(0.5);
TransitionOffTime = TimeSpan.FromSeconds(0.5);
// Create the menuCancel action
menuCancel = new InputAction(new Buttons[] { Buttons.Back }, null, true);
// We need tap gestures to hit the buttons
EnabledGestures = GestureType.Tap;
}
public override void Activate(bool instancePreserved)
{
// When the screen is activated, we have a valid ScreenManager so we can arrange
// our buttons on the screen
float y = 140f;
float center = ScreenManager.GraphicsDevice.Viewport.Bounds.Center.X;
for (int i = 0; i < MenuButtons.Count; i++)
{
Button b = MenuButtons[i];
b.Position = new Vector2(center - b.Size.X / 2, y);
y += b.Size.Y * 1.5f;
}
base.Activate(instancePreserved);
}
public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
{
// Update opacity of the buttons
foreach (Button b in menuButtons)
{
b.Alpha = TransitionAlpha;
}
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
}
/// <summary>
/// An overrideable method called whenever the menuCancel action is triggered
/// </summary>
protected virtual void OnCancel() { }
public override void HandleInput(GameTime gameTime, InputState input)
{
// Test for the menuCancel action
PlayerIndex player;
if (menuCancel.Evaluate(input, ControllingPlayer, out player))
{
OnCancel();
}
// Read in our gestures
foreach (GestureSample gesture in input.Gestures)
{
// If we have a tap
if (gesture.GestureType == GestureType.Tap)
{
// Test the tap against the buttons until one of the buttons handles the tap
foreach (Button b in menuButtons)
{
if (b.HandleTap(gesture.Position))
break;
}
}
}
base.HandleInput(gameTime, input);
}
public override void Draw(GameTime gameTime)
{
GraphicsDevice graphics = ScreenManager.GraphicsDevice;
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
SpriteFont font = ScreenManager.Font;
spriteBatch.Begin();
// Draw all of the buttons
foreach (Button b in menuButtons)
b.Draw(this);
// Make the menu slide into place during transitions, using a
// power curve to make things look more interesting (this makes
// the movement slow down as it nears the end).
float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
// Draw the menu title centered on the screen
Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
float titleScale = 1.25f;
titlePosition.Y -= transitionOffset * 100;
spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
titleOrigin, titleScale, SpriteEffects.None, 0);
spriteBatch.End();
base.Draw(gameTime);
}
}
}

View File

@ -0,0 +1,57 @@
#region File Description
//-----------------------------------------------------------------------------
// PhonePauseScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
namespace GameStateManagement
{
/// <summary>
/// A basic pause screen for Windows Phone
/// </summary>
class PhonePauseScreen : PhoneMenuScreen
{
public PhonePauseScreen()
: base("Paused")
{
// Create the "Resume" and "Exit" buttons for the screen
Button resumeButton = new Button("Resume");
resumeButton.Tapped += resumeButton_Tapped;
MenuButtons.Add(resumeButton);
Button exitButton = new Button("Exit");
exitButton.Tapped += exitButton_Tapped;
MenuButtons.Add(exitButton);
}
/// <summary>
/// The "Resume" button handler just calls the OnCancel method so that
/// pressing the "Resume" button is the same as pressing the hardware back button.
/// </summary>
void resumeButton_Tapped(object sender, EventArgs e)
{
OnCancel();
}
/// <summary>
/// The "Exit" button handler uses the LoadingScreen to take the user out to the main menu.
/// </summary>
void exitButton_Tapped(object sender, EventArgs e)
{
LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
new PhoneMainMenuScreen());
}
protected override void OnCancel()
{
ExitScreen();
base.OnCancel();
}
}
}

View File

@ -6,9 +6,10 @@ using FarseerPhysics.Dynamics.Joints;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.SamplesFramework;
using Axios.Engine;
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
public class PhysicsGameScreen : GameScreen
{
@ -47,48 +48,52 @@ namespace FarseerPhysics.SamplesFramework
_agentTorque = torque;
}
public override void LoadContent()
public override void Activate(bool instancePreserved)
{
base.LoadContent();
//We enable diagnostics to show get values for our performance counters.
Settings.EnableDiagnostics = true;
if (World == null)
if (!instancePreserved)
{
World = new World(Vector2.Zero);
}
else
{
World.Clear();
}
base.Activate(instancePreserved);
if (DebugView == null)
{
if (!Axios.Settings.ScreenSaver)
//We enable diagnostics to show get values for our performance counters.
Settings.EnableDiagnostics = true;
if (World == null)
{
DebugView = new DebugViewXNA(World);
DebugView.RemoveFlags(DebugViewFlags.Shape);
DebugView.RemoveFlags(DebugViewFlags.Joint);
DebugView.DefaultShapeColor = Color.White;
DebugView.SleepingShapeColor = Color.LightGray;
DebugView.LoadContent(ScreenManager.GraphicsDevice, ScreenManager.Content);
World = new World(Vector2.Zero);
}
else
{
World.Clear();
}
}
if (Camera == null)
{
Camera = new Camera2D(ScreenManager.GraphicsDevice);
}
else
{
Camera.ResetCamera();
}
if (DebugView == null)
{
if (!Axios.Settings.ScreenSaver)
{
ContentManager man = new ContentManager(this.ScreenManager.Game.Services, "Content");
DebugView = new DebugViewXNA(World);
DebugView.RemoveFlags(DebugViewFlags.Shape);
DebugView.RemoveFlags(DebugViewFlags.Joint);
DebugView.DefaultShapeColor = Color.White;
DebugView.SleepingShapeColor = Color.LightGray;
DebugView.LoadContent(ScreenManager.GraphicsDevice, man);
}
}
// Loading may take a while... so prevent the game from "catching up" once we finished loading
ScreenManager.Game.ResetElapsedTime();
if (Camera == null)
{
Camera = new Camera2D(ScreenManager.GraphicsDevice);
}
else
{
Camera.ResetCamera();
}
// Loading may take a while... so prevent the game from "catching up" once we finished loading
ScreenManager.Game.ResetElapsedTime();
}
}
public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
@ -110,12 +115,13 @@ namespace FarseerPhysics.SamplesFramework
{
}
public override void HandleInput(InputHelper input, GameTime gameTime)
public override void HandleInput(GameTime gameTime, InputState input)
{
#if DEBUG
// Control debug view
if (input.IsNewButtonPress(Buttons.Start))
PlayerIndex player;
if (input.IsNewButtonPress(Buttons.Start, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.Shape);
EnableOrDisableFlag(DebugViewFlags.DebugPanel);
@ -126,37 +132,37 @@ namespace FarseerPhysics.SamplesFramework
EnableOrDisableFlag(DebugViewFlags.Controllers);
}
if (input.IsNewKeyPress(Keys.F1))
if (input.IsNewKeyPress(Keys.F1, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.Shape);
}
if (input.IsNewKeyPress(Keys.F2))
if (input.IsNewKeyPress(Keys.F2, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.DebugPanel);
EnableOrDisableFlag(DebugViewFlags.PerformanceGraph);
}
if (input.IsNewKeyPress(Keys.F3))
if (input.IsNewKeyPress(Keys.F3, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.Joint);
}
if (input.IsNewKeyPress(Keys.F4))
if (input.IsNewKeyPress(Keys.F4, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.ContactPoints);
EnableOrDisableFlag(DebugViewFlags.ContactNormals);
}
if (input.IsNewKeyPress(Keys.F5))
if (input.IsNewKeyPress(Keys.F5, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.PolygonPoints);
}
if (input.IsNewKeyPress(Keys.F6))
if (input.IsNewKeyPress(Keys.F6, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.Controllers);
}
if (input.IsNewKeyPress(Keys.F7))
if (input.IsNewKeyPress(Keys.F7, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.CenterOfMass);
}
if (input.IsNewKeyPress(Keys.F8))
if (input.IsNewKeyPress(Keys.F8, ControllingPlayer.Value, out player))
{
EnableOrDisableFlag(DebugViewFlags.AABB);
}
@ -179,9 +185,10 @@ namespace FarseerPhysics.SamplesFramework
HandleCursor(input);
}
if (input.IsNewButtonPress(Buttons.Back) || input.IsNewKeyPress(Keys.Escape))
PlayerIndex i;
if (input.IsNewButtonPress(Buttons.Back, PlayerIndex.One, out i) || input.IsNewKeyPress(Keys.Escape, PlayerIndex.One, out i))
{
if (this.ScreenState == SamplesFramework.ScreenState.Active && this.TransitionPosition == 0 && this.TransitionAlpha == 1)
if (this.ScreenState == GameStateManagement.ScreenState.Active && this.TransitionPosition == 0 && this.TransitionAlpha == 1)
{ //Give the screens a chance to transition
CleanUp();
@ -189,14 +196,15 @@ namespace FarseerPhysics.SamplesFramework
}
}
base.HandleInput(input, gameTime);
base.HandleInput(gameTime, input);
}
public virtual void HandleCursor(InputHelper input)
public virtual void HandleCursor(InputState input)
{
PlayerIndex player;
Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);
if ((input.IsNewButtonPress(Buttons.A) ||
if ((input.IsNewButtonPress(Buttons.A, PlayerIndex.One, out player) ||
input.IsNewMouseButtonPress(MouseButtons.LeftButton)) &&
_fixedMouseJoint == null)
{
@ -211,7 +219,8 @@ namespace FarseerPhysics.SamplesFramework
}
}
if ((input.IsNewButtonRelease(Buttons.A) ||
if ((input.IsNewButtonRelease(Buttons.A, ControllingPlayer.Value, out player) ||
input.IsNewMouseButtonRelease(MouseButtons.LeftButton)) &&
_fixedMouseJoint != null)
{
@ -226,32 +235,32 @@ namespace FarseerPhysics.SamplesFramework
}
private void HandleCamera(InputHelper input, GameTime gameTime)
private void HandleCamera(InputState input, GameTime gameTime)
{
Vector2 camMove = Vector2.Zero;
if (input.KeyboardState.IsKeyDown(Keys.Up))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Up))
{
camMove.Y -= 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (input.KeyboardState.IsKeyDown(Keys.Down))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Down))
{
camMove.Y += 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (input.KeyboardState.IsKeyDown(Keys.Left))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Left))
{
camMove.X -= 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (input.KeyboardState.IsKeyDown(Keys.Right))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Right))
{
camMove.X += 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (input.KeyboardState.IsKeyDown(Keys.PageUp))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.PageUp))
{
Camera.Zoom += 5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Camera.Zoom / 20f;
}
if (input.KeyboardState.IsKeyDown(Keys.PageDown))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.PageDown))
{
Camera.Zoom -= 5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Camera.Zoom / 20f;
}
@ -259,19 +268,20 @@ namespace FarseerPhysics.SamplesFramework
{
Camera.MoveCamera(camMove);
}
if (input.IsNewKeyPress(Keys.Home))
PlayerIndex i;
if (input.IsNewKeyPress(Keys.Home, PlayerIndex.One, out i))
{
Camera.ResetCamera();
}
}
private void HandleUserAgent(InputHelper input)
private void HandleUserAgent(InputState input)
{
Vector2 force = _agentForce * new Vector2(input.GamePadState.ThumbSticks.Right.X,
-input.GamePadState.ThumbSticks.Right.Y);
float torque = _agentTorque * (input.GamePadState.Triggers.Right - input.GamePadState.Triggers.Left);
Vector2 force = _agentForce * new Vector2(input.CurrentGamePadStates[0].ThumbSticks.Right.X,
-input.CurrentGamePadStates[0].ThumbSticks.Right.Y);
float torque = _agentTorque * (input.CurrentGamePadStates[0].Triggers.Right - input.CurrentGamePadStates[0].Triggers.Left);
_userAgent.ApplyForce(force);
_userAgent.ApplyTorque(torque);
@ -281,27 +291,27 @@ namespace FarseerPhysics.SamplesFramework
force = Vector2.Zero;
torque = 0;
if (input.KeyboardState.IsKeyDown(Keys.A))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.A))
{
force += new Vector2(-forceAmount, 0);
}
if (input.KeyboardState.IsKeyDown(Keys.S))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.S))
{
force += new Vector2(0, forceAmount);
}
if (input.KeyboardState.IsKeyDown(Keys.D))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.D))
{
force += new Vector2(forceAmount, 0);
}
if (input.KeyboardState.IsKeyDown(Keys.W))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.W))
{
force += new Vector2(0, -forceAmount);
}
if (input.KeyboardState.IsKeyDown(Keys.Q))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Q))
{
torque -= _agentTorque;
}
if (input.KeyboardState.IsKeyDown(Keys.E))
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.E))
{
torque += _agentTorque;
}

View File

@ -0,0 +1,42 @@
#region File Description
//-----------------------------------------------------------------------------
// PlayerIndexEventArgs.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
#endregion
namespace GameStateManagement
{
/// <summary>
/// Custom event argument which includes the index of the player who
/// triggered the event. This is used by the MenuEntry.Selected event.
/// </summary>
public class PlayerIndexEventArgs : EventArgs
{
/// <summary>
/// Constructor.
/// </summary>
public PlayerIndexEventArgs(PlayerIndex playerIndex)
{
this.playerIndex = playerIndex;
}
/// <summary>
/// Gets the index of the player who triggered this event.
/// </summary>
public PlayerIndex PlayerIndex
{
get { return playerIndex; }
}
PlayerIndex playerIndex;
}
}

View File

@ -0,0 +1,463 @@
#region File Description
//-----------------------------------------------------------------------------
// ScreenManager.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using FarseerPhysics.SamplesFramework;
#endregion
namespace GameStateManagement
{
/// <summary>
/// The screen manager is a component which manages one or more GameScreen
/// instances. It maintains a stack of screens, calls their Update and Draw
/// methods at the appropriate times, and automatically routes input to the
/// topmost active screen.
/// </summary>
public class ScreenManager : DrawableGameComponent
{
#region Fields
private const string StateFilename = "ScreenManagerState.xml";
List<GameScreen> screens = new List<GameScreen>();
List<GameScreen> tempScreensList = new List<GameScreen>();
InputState input;
SpriteBatch spriteBatch;
SpriteFont font;
Texture2D blankTexture;
bool isInitialized;
bool traceEnabled;
/// <summary>
/// Contains all the fonts avaliable for use.
/// </summary>
private SpriteFonts _spriteFonts;
#endregion
#region Properties
public SpriteFonts Fonts
{
get { return _spriteFonts; }
}
/// <summary>
/// A default SpriteBatch shared by all the screens. This saves
/// each screen having to bother creating their own local instance.
/// </summary>
public SpriteBatch SpriteBatch
{
get { return spriteBatch; }
}
/// <summary>
/// A default font shared by all the screens. This saves
/// each screen having to bother loading their own local copy.
/// </summary>
public SpriteFont Font
{
get { return font; }
}
/// <summary>
/// If true, the manager prints out a list of all the screens
/// each time it is updated. This can be useful for making sure
/// everything is being added and removed at the right times.
/// </summary>
public bool TraceEnabled
{
get { return traceEnabled; }
set { traceEnabled = value; }
}
/// <summary>
/// Gets a blank texture that can be used by the screens.
/// </summary>
public Texture2D BlankTexture
{
get { return blankTexture; }
}
#endregion
#region Initialization
/// <summary>
/// Constructs a new screen manager component.
/// </summary>
public ScreenManager(Game game)
: base(game)
{
// we must set EnabledGestures before we can query for them, but
// we don't assume the game wants to read them.
TouchPanel.EnabledGestures = GestureType.None;
this.input = new InputState(this);
}
/// <summary>
/// Initializes the screen manager component.
/// </summary>
public override void Initialize()
{
base.Initialize();
isInitialized = true;
}
/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent()
{
// Load content belonging to the screen manager.
ContentManager content = new ContentManager(this.Game.Services, "Content/Fonts");
_spriteFonts = new SpriteFonts(content);
spriteBatch = new SpriteBatch(GraphicsDevice);
font = content.Load<SpriteFont>("menufont");
blankTexture = Game.Content.Load<Texture2D>("Materials/blank");
input.LoadContent();
// Tell each of the screens to load their content.
foreach (GameScreen screen in screens)
{
screen.Activate(false);
}
}
/// <summary>
/// Unload your graphics content.
/// </summary>
protected override void UnloadContent()
{
// Tell each of the screens to unload their content.
foreach (GameScreen screen in screens)
{
screen.Unload();
}
}
#endregion
#region Update and Draw
/// <summary>
/// Allows each screen to run logic.
/// </summary>
public override void Update(GameTime gameTime)
{
// Read the keyboard and gamepad.
input.Update(gameTime);
// Make a copy of the master screen list, to avoid confusion if
// the process of updating one screen adds or removes others.
tempScreensList.Clear();
foreach (GameScreen screen in screens)
tempScreensList.Add(screen);
bool otherScreenHasFocus = !Game.IsActive;
bool coveredByOtherScreen = false;
// Loop as long as there are screens waiting to be updated.
while (tempScreensList.Count > 0)
{
// Pop the topmost screen off the waiting list.
GameScreen screen = tempScreensList[tempScreensList.Count - 1];
tempScreensList.RemoveAt(tempScreensList.Count - 1);
// Update the screen.
screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
if (screen.ScreenState == ScreenState.TransitionOn ||
screen.ScreenState == ScreenState.Active)
{
// If this is the first active screen we came across,
// give it a chance to handle input.
if (!otherScreenHasFocus)
{
input.ShowCursor = screen.HasCursor;
input.EnableVirtualStick = screen.HasVirtualStick;
screen.HandleInput(gameTime, input);
otherScreenHasFocus = true;
}
// If this is an active non-popup, inform any subsequent
// screens that they are covered by it.
if (!screen.IsPopup)
coveredByOtherScreen = true;
}
}
// Print debug trace?
if (traceEnabled)
TraceScreens();
}
/// <summary>
/// Prints a list of all the screens, for debugging.
/// </summary>
void TraceScreens()
{
List<string> screenNames = new List<string>();
foreach (GameScreen screen in screens)
screenNames.Add(screen.GetType().Name);
Debug.WriteLine(string.Join(", ", screenNames.ToArray()));
}
/// <summary>
/// Tells each screen to draw itself.
/// </summary>
public override void Draw(GameTime gameTime)
{
foreach (GameScreen screen in screens)
{
if (screen.ScreenState == ScreenState.Hidden)
continue;
screen.Draw(gameTime);
}
input.Draw();
}
#endregion
#region Public Methods
/// <summary>
/// Adds a new screen to the screen manager.
/// </summary>
public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
{
screen.ControllingPlayer = controllingPlayer;
screen.ScreenManager = this;
screen.IsExiting = false;
// If we have a graphics device, tell the screen to load content.
if (isInitialized)
{
screen.Activate(false);
}
screens.Add(screen);
// update the TouchPanel to respond to gestures this screen is interested in
TouchPanel.EnabledGestures = screen.EnabledGestures;
}
/// <summary>
/// Removes a screen from the screen manager. You should normally
/// use GameScreen.ExitScreen instead of calling this directly, so
/// the screen can gradually transition off rather than just being
/// instantly removed.
/// </summary>
public void RemoveScreen(GameScreen screen)
{
// If we have a graphics device, tell the screen to unload content.
if (isInitialized)
{
screen.Unload();
}
screens.Remove(screen);
tempScreensList.Remove(screen);
// if there is a screen still in the manager, update TouchPanel
// to respond to gestures that screen is interested in.
if (screens.Count > 0)
{
TouchPanel.EnabledGestures = screens[screens.Count - 1].EnabledGestures;
}
}
/// <summary>
/// Expose an array holding all the screens. We return a copy rather
/// than the real master list, because screens should only ever be added
/// or removed using the AddScreen and RemoveScreen methods.
/// </summary>
public GameScreen[] GetScreens()
{
return screens.ToArray();
}
/// <summary>
/// Helper draws a translucent black fullscreen sprite, used for fading
/// screens in and out, and for darkening the background behind popups.
/// </summary>
public void FadeBackBufferToBlack(float alpha)
{
spriteBatch.Begin();
spriteBatch.Draw(blankTexture, GraphicsDevice.Viewport.Bounds, Color.Black * alpha);
spriteBatch.End();
}
/// <summary>
/// Informs the screen manager to serialize its state to disk.
/// </summary>
public void Deactivate()
{
#if !WINDOWS_PHONE
return;
#else
// Open up isolated storage
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
// Create an XML document to hold the list of screen types currently in the stack
XDocument doc = new XDocument();
XElement root = new XElement("ScreenManager");
doc.Add(root);
// Make a copy of the master screen list, to avoid confusion if
// the process of deactivating one screen adds or removes others.
tempScreensList.Clear();
foreach (GameScreen screen in screens)
tempScreensList.Add(screen);
// Iterate the screens to store in our XML file and deactivate them
foreach (GameScreen screen in tempScreensList)
{
// Only add the screen to our XML if it is serializable
if (screen.IsSerializable)
{
// We store the screen's controlling player so we can rehydrate that value
string playerValue = screen.ControllingPlayer.HasValue
? screen.ControllingPlayer.Value.ToString()
: "";
root.Add(new XElement(
"GameScreen",
new XAttribute("Type", screen.GetType().AssemblyQualifiedName),
new XAttribute("ControllingPlayer", playerValue)));
}
// Deactivate the screen regardless of whether we serialized it
screen.Deactivate();
}
// Save the document
using (IsolatedStorageFileStream stream = storage.CreateFile(StateFilename))
{
doc.Save(stream);
}
}
#endif
}
public bool Activate(bool instancePreserved)
{
#if !WINDOWS_PHONE
return false;
#else
// If the game instance was preserved, the game wasn't dehydrated so our screens still exist.
// We just need to activate them and we're ready to go.
if (instancePreserved)
{
// Make a copy of the master screen list, to avoid confusion if
// the process of activating one screen adds or removes others.
tempScreensList.Clear();
foreach (GameScreen screen in screens)
tempScreensList.Add(screen);
foreach (GameScreen screen in tempScreensList)
screen.Activate(true);
}
// Otherwise we need to refer to our saved file and reconstruct the screens that were present
// when the game was deactivated.
else
{
// Try to get the screen factory from the services, which is required to recreate the screens
IScreenFactory screenFactory = Game.Services.GetService(typeof(IScreenFactory)) as IScreenFactory;
if (screenFactory == null)
{
throw new InvalidOperationException(
"Game.Services must contain an IScreenFactory in order to activate the ScreenManager.");
}
// Open up isolated storage
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
// Check for the file; if it doesn't exist we can't restore state
if (!storage.FileExists(StateFilename))
return false;
// Read the state file so we can build up our screens
using (IsolatedStorageFileStream stream = storage.OpenFile(StateFilename, FileMode.Open))
{
XDocument doc = XDocument.Load(stream);
// Iterate the document to recreate the screen stack
foreach (XElement screenElem in doc.Root.Elements("GameScreen"))
{
// Use the factory to create the screen
Type screenType = Type.GetType(screenElem.Attribute("Type").Value);
GameScreen screen = screenFactory.CreateScreen(screenType);
// Rehydrate the controlling player for the screen
PlayerIndex? controllingPlayer = screenElem.Attribute("ControllingPlayer").Value != ""
? (PlayerIndex)Enum.Parse(typeof(PlayerIndex), screenElem.Attribute("ControllingPlayer").Value, true)
: (PlayerIndex?)null;
screen.ControllingPlayer = controllingPlayer;
// Add the screen to the screens list and activate the screen
screen.ScreenManager = this;
screens.Add(screen);
screen.Activate(false);
// update the TouchPanel to respond to gestures this screen is interested in
TouchPanel.EnabledGestures = screen.EnabledGestures;
}
}
}
}
return true;
#endif
}
#endregion
}
}

View File

@ -3,9 +3,11 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using FarseerPhysics.SamplesFramework;
namespace FarseerPhysics.SamplesFramework
namespace GameStateManagement
{
/*
/// <summary>
/// The screen manager is a component which manages one or more GameScreen
/// instances. It maintains a stack of screens, calls their Update and Draw
@ -299,5 +301,5 @@ namespace FarseerPhysics.SamplesFramework
{
return _screens.ToArray();
}
}
}*/
}

View File

@ -11,9 +11,9 @@ namespace FarseerPhysics.SamplesFramework
public SpriteFonts(ContentManager contentManager)
{
MenuSpriteFont = contentManager.Load<SpriteFont>("Fonts/menuFont");
FrameRateCounterFont = contentManager.Load<SpriteFont>("Fonts/frameRateCounterFont");
DetailsFont = contentManager.Load<SpriteFont>("Fonts/detailsFont");
MenuSpriteFont = contentManager.Load<SpriteFont>("menuFont");
FrameRateCounterFont = contentManager.Load<SpriteFont>("frameRateCounterFont");
DetailsFont = contentManager.Load<SpriteFont>("detailsFont");
}
}
}