/*
* Util.MenuScreen.cs
* Authors: Adam Nabinger
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Util
{
internal enum MenuEventCode
{
MenuSelect = 1001,
MenuCancel = 1002,
}
///
/// A generic menu screen.
///
public class MenuScreen : GameScreen
{
#region Fields
///
/// Reference to the current sprite batch used for rendering
///
protected SpriteBatch m_SpriteBatch;
///
/// The menu screens handler for any input
///
protected InputHandler inputHandler;
///
/// Reference to the game screen which created this
///
protected readonly GameScreen m_Game;
///
/// The texture used as the background of this screen
///
protected Texture2D mBackground;
///
/// The location and dimensions of this screen
///
protected Rectangle mBackgroundLocation;
///
/// Array of buttons that can be clicked on in the menu screen
///
protected MenuButton[] mButtons;
///
/// The image for the mouse pointer
///
protected Texture2D mMousePointer;
///
/// The location and dimensions of the mouse pointer
///
protected Rectangle mMouseLocation;
#endregion
#region Creation
///
/// Constructor
///
/// Reference to the current game screen
public MenuScreen(GameScreen game)
{
m_Game = game;
mButtons = new MenuButton[0];
}
#endregion
#region Initialization
///
/// Initialize.
///
public override void Initialize()
{
inputHandler = GetInputHandler();
inputHandler.Initialize();
inputHandler.BindButton(MouseButton.Left, Trigger.Activated, (int)MenuEventCode.MenuSelect);
inputHandler.BindButton(Microsoft.Xna.Framework.Input.Keys.Escape, Trigger.Activated, (int)MenuEventCode.MenuCancel);
base.Initialize();
}
///
/// The menuscreen should not be drawn over.
///
public override void Reinitialize() { }
#endregion
#region Management
///
/// Loads in all necessary information for all content dependent objects
///
protected override void LoadContent()
{
m_SpriteBatch = new SpriteBatch(GraphicsDevice);
foreach (MenuButton b in mButtons)
{
b.LoadContent(Content);
}
base.LoadContent();
}
///
/// Disposes of any unmanged objects
///
/// Whether or not this is currently being disposed
protected override void Dispose(bool disposing)
{
if (disposing)
{
m_SpriteBatch.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region Update
///
/// Updates the menu screen
///
/// Time that has passed in game
public override void Update(GameTime gameTime)
{
Point tempLocation = inputHandler.MouseLocation;
mMouseLocation.X = tempLocation.X;
mMouseLocation.Y = tempLocation.Y;
foreach (MenuButton mb in mButtons)
{
MenuButtonHover mbh = mb as MenuButtonHover;
if (mbh != null)
{
mbh.Update(mbh.Location.Contains(mMouseLocation));
}
}
while (inputHandler.HasEvent())
{
InputEvent e = inputHandler.GetNextEvent();
switch ((MenuEventCode)e.EventCode)
{
case MenuEventCode.MenuSelect:
MouseInputEvent me = e as MouseInputEvent;
if (me == null)
{
break;
}
foreach (MenuButton b in mButtons)
{
if (b.Location.Contains(me.Location))
{
Enabled = false;
Visible = false;
inputHandler.Disable();
b.Execute(m_Game);
return;
}
}
break;
case MenuEventCode.MenuCancel:
Done();
return;
}
}
base.Update(gameTime);
}
#endregion
#region Render
///
/// Renders the menu screen
///
/// Time that has passed in game
public override void Draw(GameTime gameTime)
{
m_SpriteBatch.Begin();
if (mBackground != null)
{
m_SpriteBatch.Draw(mBackground, mBackgroundLocation, Color.White);
}
foreach (MenuButton b in mButtons)
{
b.Draw(m_SpriteBatch);
}
if (mMousePointer != null)
{
m_SpriteBatch.Draw(mMousePointer, mMouseLocation, Color.White);
}
m_SpriteBatch.End();
base.Draw(gameTime);
}
#endregion
}
}