MoonWorks/src/Input/Mouse.cs

99 lines
2.1 KiB
C#
Raw Permalink Normal View History

2022-04-27 18:37:35 +00:00
using System.Collections.Generic;
using SDL2;
namespace MoonWorks.Input
{
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; }
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; }
public int Wheel { get; internal set; }
2022-07-08 23:47:12 +00:00
public bool AnyPressed { get; private set; }
2022-07-12 23:09:23 +00:00
public MouseButton AnyPressedButton { get; private set; }
public uint ButtonMask { get; private set; }
2022-07-08 23:47:12 +00:00
2022-02-23 05:14:32 +00:00
private bool relativeMode;
public bool RelativeMode
{
get => relativeMode;
set
{
relativeMode = value;
SDL.SDL_SetRelativeMouseMode(
relativeMode ?
SDL.SDL_bool.SDL_TRUE :
SDL.SDL_bool.SDL_FALSE
);
}
}
2022-07-12 23:09:23 +00:00
private readonly Dictionary<MouseButtonCode, MouseButton> CodeToButton;
private IEnumerable<MouseButton> Buttons
{
get
{
yield return LeftButton;
yield return MiddleButton;
yield return RightButton;
}
}
2022-04-27 18:37:35 +00:00
public Mouse()
{
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);
CodeToButton = new Dictionary<MouseButtonCode, MouseButton>
2022-04-27 18:37:35 +00:00
{
{ MouseButtonCode.Left, LeftButton },
{ MouseButtonCode.Right, RightButton },
{ MouseButtonCode.Middle, MiddleButton }
};
}
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-07-12 23:09:23 +00:00
LeftButton.Update();
MiddleButton.Update();
RightButton.Update();
foreach (var button in Buttons)
2022-07-08 23:47:12 +00:00
{
2022-07-12 23:09:23 +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
}
public ButtonState ButtonState(MouseButtonCode buttonCode)
{
return CodeToButton[buttonCode].State;
2022-02-23 05:14:32 +00:00
}
}
}