/*
* Util.MouseUtility.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 System.Drawing;
using System.Windows.Forms;
namespace Util
{
///
/// A basic utility for holding the mouse within the window.
///
public static class MouseUtility
{
#region Fields
// The area in which the mouse is trapped within
private static readonly Rectangle DefaultClip = Cursor.Clip;
// Reference to the current game
private static Microsoft.Xna.Framework.Game Game;
#endregion
#region Mouse Trap
///
/// Trap the mouse cursor within the window of this game.
///
/// Reference to the current game screen
public static void SetTrap(Microsoft.Xna.Framework.Game game)
{
Cursor.Clip = new Rectangle(game.Window.ClientBounds.X, game.Window.ClientBounds.Y, game.Window.ClientBounds.Width, game.Window.ClientBounds.Height);
}
///
/// Release the mouse cursor to it's default bounds.
///
public static void ReleaseTrap()
{
Cursor.Clip = DefaultClip;
}
///
/// Automatically trap the mouse within the window of this game, while the game has focus,
/// and release it if the game loses focus (such as via alt+tab).
///
/// Reference to the current game screen
public static void AutoTrap(Microsoft.Xna.Framework.Game game)
{
Game = game;
Game.Activated += game_Activated;
Game.Deactivated += game_Deactivated;
SetTrap(Game);
}
#endregion
#region Activation/Deactivation
///
/// Sets what happens when the mouse utility is activated
///
/// The sender of the activated event
/// The associated arguments with the deactivated event
static void game_Activated(object sender, System.EventArgs e)
{
SetTrap(Game);
}
///
/// Sets what happens when the mouse utility is deactived
///
/// The sender of the deactived event
/// The associated arguments with the deactived event
static void game_Deactivated(object sender, System.EventArgs e)
{
ReleaseTrap();
}
#endregion
}
}