MoonWorks/src/Input/Mouse.cs

136 lines
3.5 KiB
C#
Raw Normal View History

2022-04-27 18:37:35 +00:00
using System.Collections.Generic;
using SDL2;
namespace MoonWorks.Input
{
/// <summary>
/// The mouse input device abstraction.
/// </summary>
2022-02-23 05:14:32 +00:00
public class Mouse
{
2022-07-12 23:09:23 +00:00
public MouseButton LeftButton { get; }
public MouseButton MiddleButton { get; }
public MouseButton RightButton { get; }
public MouseButton X1Button { get; }
public MouseButton X2Button { get; }
2022-02-23 05:14:32 +00:00
public int X { get; private set; }
public int Y { get; private set; }
public int DeltaX { get; private set; }
public int DeltaY { get; private set; }
2022-10-21 18:39:06 +00:00
// note that this is a delta value
public int Wheel { get; private set; }
internal int WheelRaw;
private int previousWheelRaw = 0;
2022-02-23 05:14:32 +00:00
/// <summary>
/// True if any button on the keyboard is active. Useful for input remapping.
/// </summary>
2022-07-08 23:47:12 +00:00
public bool AnyPressed { get; private set; }
/// <summary>
/// Contains a reference to an arbitrary MouseButton that was pressed this frame. Useful for input remapping.
/// </summary>
2022-07-12 23:09:23 +00:00
public MouseButton AnyPressedButton { get; private set; }
internal uint ButtonMask { get; private set; }
2022-07-08 23:47:12 +00:00
2022-02-23 05:14:32 +00:00
private bool relativeMode;
/// <summary>
/// If set to true, the cursor is hidden, the mouse position is constrained to the window,
/// and relative mouse motion will be reported even if the mouse is at the edge of the window.
/// </summary>
2022-02-23 05:14:32 +00:00
public bool RelativeMode
{
get => relativeMode;
set
{
relativeMode = value;
SDL.SDL_SetRelativeMouseMode(
relativeMode ?
SDL.SDL_bool.SDL_TRUE :
SDL.SDL_bool.SDL_FALSE
);
}
}
2023-03-01 21:47:25 +00:00
private bool hidden;
/// <summary>
/// If set to true, the OS cursor will not be shown in your application window.
/// </summary>
2023-03-01 21:47:25 +00:00
public bool Hidden
{
get => hidden;
set
{
hidden = value;
SDL.SDL_ShowCursor(hidden ? SDL.SDL_DISABLE : SDL.SDL_ENABLE);
}
}
2022-07-12 23:09:23 +00:00
private readonly Dictionary<MouseButtonCode, MouseButton> CodeToButton;
internal Mouse()
2022-04-27 18:37:35 +00:00
{
2022-07-12 23:09:23 +00:00
LeftButton = new MouseButton(this, MouseButtonCode.Left, SDL.SDL_BUTTON_LMASK);
MiddleButton = new MouseButton(this, MouseButtonCode.Middle, SDL.SDL_BUTTON_MMASK);
RightButton = new MouseButton(this, MouseButtonCode.Right, SDL.SDL_BUTTON_RMASK);
X1Button = new MouseButton(this, MouseButtonCode.X1, SDL.SDL_BUTTON_X1MASK);
X2Button = new MouseButton(this, MouseButtonCode.X2, SDL.SDL_BUTTON_X2MASK);
2022-07-12 23:09:23 +00:00
CodeToButton = new Dictionary<MouseButtonCode, MouseButton>
2022-04-27 18:37:35 +00:00
{
{ MouseButtonCode.Left, LeftButton },
{ MouseButtonCode.Right, RightButton },
{ MouseButtonCode.Middle, MiddleButton },
{ MouseButtonCode.X1, X1Button },
{ MouseButtonCode.X2, X2Button }
2022-04-27 18:37:35 +00:00
};
}
2022-02-23 05:14:32 +00:00
internal void Update()
{
2022-07-08 23:47:12 +00:00
AnyPressed = false;
2022-07-12 23:09:23 +00:00
ButtonMask = SDL.SDL_GetMouseState(out var x, out var y);
2022-02-23 05:14:32 +00:00
var _ = SDL.SDL_GetRelativeMouseState(out var deltaX, out var deltaY);
X = x;
Y = y;
DeltaX = deltaX;
DeltaY = deltaY;
2022-10-21 18:39:06 +00:00
Wheel = WheelRaw - previousWheelRaw;
previousWheelRaw = WheelRaw;
2022-12-13 23:13:43 +00:00
foreach (var button in CodeToButton.Values)
2022-07-08 23:47:12 +00:00
{
button.Update();
2022-07-08 23:47:12 +00:00
if (button.IsPressed)
{
AnyPressed = true;
2022-07-12 23:09:23 +00:00
AnyPressedButton = button;
2022-07-08 23:47:12 +00:00
}
}
2022-04-27 18:37:35 +00:00
}
/// <summary>
/// Gets a button from the mouse given a MouseButtonCode.
/// </summary>
2023-06-10 01:15:15 +00:00
public MouseButton Button(MouseButtonCode buttonCode)
{
return CodeToButton[buttonCode];
}
/// <summary>
/// Gets a button state from a mouse button corresponding to the given MouseButtonCode.
/// </summary>
2022-04-27 18:37:35 +00:00
public ButtonState ButtonState(MouseButtonCode buttonCode)
{
return CodeToButton[buttonCode].State;
2022-02-23 05:14:32 +00:00
}
}
}