2007-08-10 11:27:13 +02:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2007-08-05 12:17:20 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace OpenTK.Input
|
|
|
|
|
{
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The MouseDevice class represents a mouse device and provides methods to query its status.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class MouseDevice : IInputDevice
|
2007-08-05 12:17:20 +02:00
|
|
|
|
{
|
2007-09-21 22:34:07 +02:00
|
|
|
|
private string description;
|
|
|
|
|
private int numButtons, numWheels;
|
|
|
|
|
private IntPtr id;
|
|
|
|
|
private bool[] button = new bool[(int)MouseButton.LastButton];
|
2007-09-26 17:48:11 +02:00
|
|
|
|
private int wheel, x, y; //, wheel_delta , delta_x, delta_y;
|
|
|
|
|
internal int last_x, last_y, last_wheel;
|
2007-09-21 22:34:07 +02:00
|
|
|
|
|
2007-08-05 12:17:20 +02:00
|
|
|
|
#region --- IInputDevice Members ---
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a string describing this MouseDevice.
|
|
|
|
|
/// </summary>
|
2007-08-05 12:17:20 +02:00
|
|
|
|
public string Description
|
|
|
|
|
{
|
2007-09-21 22:34:07 +02:00
|
|
|
|
get { return description; }
|
|
|
|
|
internal set { description = value; }
|
2007-08-05 12:17:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an value indicating the InputDeviceType of this InputDevice.
|
|
|
|
|
/// </summary>
|
2007-08-05 12:17:20 +02:00
|
|
|
|
public InputDeviceType DeviceType
|
|
|
|
|
{
|
|
|
|
|
get { return InputDeviceType.Mouse; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-09-21 22:34:07 +02:00
|
|
|
|
|
2007-09-26 17:48:11 +02:00
|
|
|
|
#region --- Public Methods ---
|
2007-09-21 22:34:07 +02:00
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the number of buttons on this MouseDevice.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public int NumberOfButtons
|
|
|
|
|
{
|
|
|
|
|
get { return numButtons; }
|
|
|
|
|
internal set { numButtons = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the number of wheels on this MouseDevice.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public int NumberOfWheels
|
|
|
|
|
{
|
|
|
|
|
get { return numWheels; }
|
|
|
|
|
internal set { numWheels = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an IntPtr representing a device dependent ID.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public IntPtr DeviceID
|
|
|
|
|
{
|
|
|
|
|
get { return id; }
|
|
|
|
|
internal set { id = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the absolute wheel position.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public int Wheel
|
|
|
|
|
{
|
|
|
|
|
get { return wheel; }
|
2007-09-22 15:13:17 +02:00
|
|
|
|
internal set
|
|
|
|
|
{
|
2007-09-26 17:48:11 +02:00
|
|
|
|
last_wheel = wheel;
|
2007-09-22 15:13:17 +02:00
|
|
|
|
wheel = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the relative wheel movement.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int WheelDelta
|
2007-09-22 15:13:17 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2007-09-26 17:48:11 +02:00
|
|
|
|
return wheel - last_wheel;
|
2007-09-22 15:13:17 +02:00
|
|
|
|
}
|
2007-09-26 17:48:11 +02:00
|
|
|
|
//internal set { wheel_delta = value; }
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the absolute x position of the pointer, in screen pixel coordinates.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public int X
|
|
|
|
|
{
|
|
|
|
|
get { return x; }
|
2007-09-26 17:48:11 +02:00
|
|
|
|
internal set
|
|
|
|
|
{
|
|
|
|
|
last_x = x;
|
|
|
|
|
x = value;
|
|
|
|
|
}
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the absolute y position of the pointer, in screen pixel coordinates.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public int Y
|
|
|
|
|
{
|
|
|
|
|
get { return y; }
|
2007-09-26 17:48:11 +02:00
|
|
|
|
internal set
|
|
|
|
|
{
|
|
|
|
|
last_y = y;
|
|
|
|
|
y = value;
|
|
|
|
|
}
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the relative x movement of the pointer, in pixel coordinates.
|
|
|
|
|
/// </summary>
|
2007-09-22 15:13:17 +02:00
|
|
|
|
public int XDelta
|
2007-09-21 22:34:07 +02:00
|
|
|
|
{
|
2007-09-26 17:48:11 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
//return delta_x;
|
|
|
|
|
return x - last_x;
|
|
|
|
|
}
|
|
|
|
|
//internal set { delta_x = value; }
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the relative y movement of the pointer, in pixel coordinates.
|
|
|
|
|
/// </summary>
|
2007-09-22 15:13:17 +02:00
|
|
|
|
public int YDelta
|
2007-09-21 22:34:07 +02:00
|
|
|
|
{
|
2007-09-26 17:48:11 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
//return delta_y;
|
|
|
|
|
return y - last_y;
|
|
|
|
|
}
|
|
|
|
|
//internal set { delta_y = value; }
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
//public event MouseMoveEvent Move;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a button is pressed.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public event MouseButtonDownEvent ButtonDown;
|
2007-09-26 13:30:18 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a button is released.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public event MouseButtonUpEvent ButtonUp;
|
|
|
|
|
|
|
|
|
|
#region public bool this[MouseButton b]
|
|
|
|
|
|
2007-09-26 17:48:11 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating the status of the specified MouseButton.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">The MouseButton to check.</param>
|
|
|
|
|
/// <returns>True if the MouseButton is pressed, false otherwise.</returns>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public bool this[MouseButton b]
|
|
|
|
|
{
|
2007-09-26 17:48:11 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return button[(int)b];
|
|
|
|
|
}
|
2007-09-21 22:34:07 +02:00
|
|
|
|
internal set
|
|
|
|
|
{
|
2007-09-22 00:39:16 +02:00
|
|
|
|
if (ButtonDown != null && value && !button[(int)b])
|
|
|
|
|
{
|
|
|
|
|
ButtonDown(this, b);
|
|
|
|
|
}
|
|
|
|
|
else if (ButtonUp != null && !value && button[(int)b])
|
|
|
|
|
{
|
|
|
|
|
ButtonUp(this, b);
|
|
|
|
|
}
|
2007-09-21 22:34:07 +02:00
|
|
|
|
button[(int)b] = value;
|
2007-09-26 13:30:18 +02:00
|
|
|
|
//System.Diagnostics.Debug.Print("Mouse button {0} {1}", b, value ? "down" : "up");
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-09-26 13:30:18 +02:00
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
//return base.GetHashCode();
|
|
|
|
|
return (int)(numButtons ^ numWheels ^ id.GetHashCode() ^ description.GetHashCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("ID: {0} ({1}). Buttons: {2}, Wheels: {3}",
|
|
|
|
|
DeviceID, Description, NumberOfButtons, NumberOfWheels);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-09-21 22:34:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
//public delegate void MouseMoveEvent(MouseDevice sender, MouseMoveData key);
|
|
|
|
|
public delegate void MouseButtonDownEvent(MouseDevice sender, MouseButton button);
|
|
|
|
|
public delegate void MouseButtonUpEvent(MouseDevice sender, MouseButton button);
|
|
|
|
|
|
|
|
|
|
#region public enum MouseButton
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enumerates all possible mouse buttons.
|
|
|
|
|
/// </summary>
|
2007-09-21 22:34:07 +02:00
|
|
|
|
public enum MouseButton
|
|
|
|
|
{
|
|
|
|
|
Left = 0,
|
|
|
|
|
Middle,
|
|
|
|
|
Right,
|
|
|
|
|
Button1,
|
|
|
|
|
Button2,
|
|
|
|
|
Button3,
|
|
|
|
|
Button4,
|
|
|
|
|
Button5,
|
|
|
|
|
Button6,
|
|
|
|
|
Button7,
|
|
|
|
|
Button8,
|
|
|
|
|
Button9,
|
|
|
|
|
LastButton
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
#endregion
|
2007-09-21 22:34:07 +02:00
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
#region internal class MouseMoveData
|
2007-09-21 22:34:07 +02:00
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Not used yet.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class MouseMoveData
|
2007-09-21 22:34:07 +02:00
|
|
|
|
{
|
|
|
|
|
private int x;
|
|
|
|
|
private int y;
|
|
|
|
|
private int deltaX;
|
|
|
|
|
private int deltaY;
|
2007-09-26 13:30:18 +02:00
|
|
|
|
private int wheel, deltaWheel;
|
2007-09-21 22:34:07 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the absolute X position of the mouse in screen pixel coordinates.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int X
|
|
|
|
|
{
|
|
|
|
|
get { return x; }
|
|
|
|
|
internal set { x = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the absolute Y position of the mouse in screen pixel coordinates.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Y
|
|
|
|
|
{
|
|
|
|
|
get { return y; }
|
|
|
|
|
internal set { y = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the relative movement of the mouse in the X direction, in pixels.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int DeltaX
|
|
|
|
|
{
|
|
|
|
|
get { return deltaX; }
|
|
|
|
|
internal set { deltaX = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the relative movement of the mouse in the Y direction, in pixels.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int DeltaY
|
|
|
|
|
{
|
|
|
|
|
get { return deltaY; }
|
|
|
|
|
internal set { deltaY = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets data relevant to the mouse wheel.
|
|
|
|
|
/// </summary>
|
2007-09-26 13:30:18 +02:00
|
|
|
|
//public MouseWheel Wheel
|
|
|
|
|
//{
|
|
|
|
|
// get { return wheel; }
|
|
|
|
|
// internal set { wheel = value; }
|
|
|
|
|
//}
|
2007-08-05 12:17:20 +02:00
|
|
|
|
}
|
2007-09-26 13:30:18 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
2007-08-05 12:17:20 +02:00
|
|
|
|
}
|