2009-02-22 10:43:35 +00:00
#define COMPAT_REV1519 // Keeps compatibility with revision 1519
2009-09-03 21:15:12 +00:00
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
2009-02-22 10:43:35 +00:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
2011-12-08 00:03:14 +00:00
#if ! MINIMAL
using System.Drawing ;
#endif
using System.Text ;
2009-02-22 10:43:35 +00:00
namespace OpenTK.Input
{
/// <summary>
/// Represents a mouse device and provides methods to query its status.
/// </summary>
public sealed class MouseDevice : IInputDevice
{
#region - - - Fields - - -
string description ;
IntPtr id ;
int numButtons , numWheels ;
2014-05-04 06:33:20 +00:00
MouseState state ;
2009-02-22 10:43:35 +00:00
#if COMPAT_REV1519
int wheel_last_accessed = 0 ;
Point pos_last_accessed = new Point ( ) ;
#endif
#endregion
#region - - - IInputDevice Members - - -
#region public string Description
/// <summary>
/// Gets a string describing this MouseDevice.
/// </summary>
public string Description
{
get { return description ; }
internal set { description = value ; }
}
#endregion
#region public InputDeviceType DeviceType
/// <summary>
/// Gets a value indicating the InputDeviceType of this InputDevice.
/// </summary>
public InputDeviceType DeviceType
{
get { return InputDeviceType . Mouse ; }
}
#endregion
#endregion
#region - - - Public Members - - -
2014-09-10 23:03:24 +00:00
/// <summary>
/// Retrieves the combined hardware <see cref="OpenTK.Input.MouseState"/> for all specified mouse devices.
/// This method is equivalent to <see cref="OpenTK.Input.Mouse.GetState()"/>.
/// </summary>
/// <returns>A <see cref="OpenTK.Input.MouseState"/> structure representing the state for the specified mouse device.</returns>
/// <seealso cref="OpenTK.Input.Mouse.GetState()"/>
public MouseState GetState ( )
{
return Mouse . GetState ( ) ;
}
/// <summary>
/// Retrieves the hardware <see cref="OpenTK.Input.MouseState"/> for the specified mouse device.
/// This method is equivalent to <see cref="OpenTK.Input.Mouse.GetState(int)"/>.
/// </summary>
/// <param name="index">The index of the mouse device.</param>
/// <returns>A <see cref="OpenTK.Input.MouseState"/> structure representing the state for the specified mouse device.</returns>
/// <seealso cref="OpenTK.Input.Mouse.GetState(int)"/>
public MouseState GetState ( int index )
{
return Mouse . GetState ( index ) ;
}
/// <summary>
/// Retreves the <see cref="OpenTK.Input.MouseState"/> for the mouse cursor.
/// This method is equivalent to <see cref="OpenTK.Input.Mouse.GetCursorState"/>.
/// </summary>
/// <returns>A <see cref="OpenTK.Input.MouseState"/> structure representing the state of the mouse cursor.</returns>
/// <seealso cref="OpenTK.Input.Mouse.GetCursorState()"/>
public MouseState GetCursorState ( )
{
return Mouse . GetCursorState ( ) ;
}
2009-02-22 10:43:35 +00:00
#region public int NumberOfButtons
/// <summary>
/// Gets an integer representing the number of buttons on this MouseDevice.
/// </summary>
public int NumberOfButtons
{
get { return numButtons ; }
internal set { numButtons = value ; }
}
#endregion
#region public int NumberOfWheels
/// <summary>
/// Gets an integer representing the number of wheels on this MouseDevice.
/// </summary>
public int NumberOfWheels
{
get { return numWheels ; }
internal set { numWheels = value ; }
}
#endregion
#region public IntPtr DeviceID
/// <summary>
/// Gets an IntPtr representing a device dependent ID.
/// </summary>
public IntPtr DeviceID
{
get { return id ; }
internal set { id = value ; }
}
#endregion
#region public int Wheel
/// <summary>
2009-11-16 12:15:35 +00:00
/// Gets the absolute wheel position in integer units.
/// To support high-precision mice, it is recommended to use <see cref="WheelPrecise"/> instead.
2009-02-22 10:43:35 +00:00
/// </summary>
public int Wheel
2009-11-16 12:15:35 +00:00
{
2014-05-04 06:33:20 +00:00
get { return state . Wheel ; }
2009-11-16 12:15:35 +00:00
}
/// <summary>
/// Gets the absolute wheel position in floating-point units.
/// </summary>
public float WheelPrecise
2009-02-22 10:43:35 +00:00
{
2014-05-04 06:33:20 +00:00
get { return state . WheelPrecise ; }
2009-02-22 10:43:35 +00:00
}
#endregion
#region public int X
/// <summary>
/// Gets an integer representing the absolute x position of the pointer, in window pixel coordinates.
/// </summary>
public int X
{
2014-05-04 06:33:20 +00:00
get { return state . X ; }
2009-02-22 10:43:35 +00:00
}
#endregion
#region public int Y
/// <summary>
/// Gets an integer representing the absolute y position of the pointer, in window pixel coordinates.
/// </summary>
public int Y
{
2014-05-04 06:33:20 +00:00
get { return state . Y ; }
2009-02-22 10:43:35 +00:00
}
#endregion
#region public bool this [ MouseButton b ]
/// <summary>
/// Gets a System.Boolean indicating the state of the specified MouseButton.
/// </summary>
2009-03-25 21:53:12 +00:00
/// <param name="button">The MouseButton to check.</param>
2009-02-22 10:43:35 +00:00
/// <returns>True if the MouseButton is pressed, false otherwise.</returns>
public bool this [ MouseButton button ]
{
get
{
2014-05-04 06:33:20 +00:00
return state [ button ] ;
2009-02-22 10:43:35 +00:00
}
internal set
{
2014-05-04 06:33:20 +00:00
state [ button ] = value ;
2009-02-22 10:43:35 +00:00
}
}
#endregion
#endregion
#region - - - Internal Members - - -
2014-05-04 06:33:20 +00:00
internal void HandleMouseDown ( object sender , MouseButtonEventArgs e )
2009-02-22 10:43:35 +00:00
{
2014-05-04 14:58:24 +00:00
state = e . Mouse ;
2014-05-04 06:33:20 +00:00
ButtonDown ( this , e ) ;
2009-02-22 10:43:35 +00:00
}
2014-05-04 06:33:20 +00:00
internal void HandleMouseUp ( object sender , MouseButtonEventArgs e )
{
2014-05-04 14:58:24 +00:00
state = e . Mouse ;
2014-05-04 06:33:20 +00:00
ButtonUp ( this , e ) ;
}
internal void HandleMouseMove ( object sender , MouseMoveEventArgs e )
{
2014-05-04 14:58:24 +00:00
state = e . Mouse ;
2014-05-04 06:33:20 +00:00
Move ( this , e ) ;
}
internal void HandleMouseWheel ( object sender , MouseWheelEventArgs e )
{
2014-05-04 14:58:24 +00:00
state = e . Mouse ;
2014-05-04 06:33:20 +00:00
WheelChanged ( this , e ) ;
}
2009-02-22 10:43:35 +00:00
#endregion
#region - - - Events - - -
/// <summary>
/// Occurs when the mouse's position is moved.
/// </summary>
2009-09-04 23:13:24 +00:00
public event EventHandler < MouseMoveEventArgs > Move = delegate { } ;
2009-02-22 10:43:35 +00:00
/// <summary>
/// Occurs when a button is pressed.
/// </summary>
2009-09-04 23:13:24 +00:00
public event EventHandler < MouseButtonEventArgs > ButtonDown = delegate { } ;
2009-02-22 10:43:35 +00:00
/// <summary>
/// Occurs when a button is released.
/// </summary>
2009-09-04 23:13:24 +00:00
public event EventHandler < MouseButtonEventArgs > ButtonUp = delegate { } ;
2009-02-22 10:43:35 +00:00
/// <summary>
/// Occurs when one of the mouse wheels is moved.
/// </summary>
2009-09-04 23:13:24 +00:00
public event EventHandler < MouseWheelEventArgs > WheelChanged = delegate { } ;
2009-02-22 10:43:35 +00:00
#region - - - Overrides - - -
2009-09-04 23:13:24 +00:00
/// <summary>
/// Calculates the hash code for this instance.
/// </summary>
/// <returns></returns>
2009-02-22 10:43:35 +00:00
public override int GetHashCode ( )
{
return ( int ) ( numButtons ^ numWheels ^ id . GetHashCode ( ) ^ description . GetHashCode ( ) ) ;
}
2009-09-04 23:13:24 +00:00
/// <summary>
/// Returns a <see cref="System.String"/> that describes this instance.
/// </summary>
/// <returns>A <see cref="System.String"/> that describes this instance.</returns>
2009-02-22 10:43:35 +00:00
public override string ToString ( )
{
return String . Format ( "ID: {0} ({1}). Buttons: {2}, Wheels: {3}" ,
DeviceID , Description , NumberOfButtons , NumberOfWheels ) ;
}
#endregion
#endregion
#region COMPAT_REV1519
#if COMPAT_REV1519
#region public int WheelDelta
/// <summary>
/// Gets an integer representing the relative wheel movement.
/// </summary>
[Obsolete("WheelDelta is only defined for a single WheelChanged event. Use the OpenTK.Input.MouseWheelEventArgs::Delta property with the OpenTK.Input.MouseDevice::WheelChanged event.", false)]
public int WheelDelta
{
get
{
2014-05-04 06:33:20 +00:00
int result = ( int ) Math . Round ( state . WheelPrecise - wheel_last_accessed , MidpointRounding . AwayFromZero ) ;
wheel_last_accessed = state . Wheel ;
2009-02-22 10:43:35 +00:00
return result ;
}
}
#endregion
#region public int XDelta
/// <summary>
/// Gets an integer representing the relative x movement of the pointer, in pixel coordinates.
/// </summary>
[Obsolete("XDelta is only defined for a single Move event. Use the OpenTK.Input.MouseMoveEventArgs::Delta property with the OpenTK.Input.MouseDevice::Move event.", false)]
public int XDelta
{
get
{
2014-05-04 06:33:20 +00:00
int result = state . X - pos_last_accessed . X ;
pos_last_accessed . X = state . X ;
2009-02-22 10:43:35 +00:00
return result ;
}
}
#endregion
#region public int YDelta
/// <summary>
/// Gets an integer representing the relative y movement of the pointer, in pixel coordinates.
/// </summary>
[Obsolete("YDelta is only defined for a single Move event. Use the OpenTK.Input.MouseMoveEventArgs::Delta property with the OpenTK.Input.MouseDevice::Move event.", false)]
public int YDelta
{
get
{
2014-05-04 06:33:20 +00:00
int result = state . Y - pos_last_accessed . Y ;
pos_last_accessed . Y = state . Y ;
2009-02-22 10:43:35 +00:00
return result ;
}
}
#endregion
#endif
#endregion
}
}