add mouse input + move inputs to Input namespace

pull/14/head
cosmonaut 2021-01-22 00:20:07 -08:00
parent 1e77ad2cb4
commit f2487cd71b
8 changed files with 80 additions and 11 deletions

View File

@ -2,6 +2,7 @@
using SDL2;
using MoonWorks.Audio;
using MoonWorks.Graphics;
using MoonWorks.Input;
namespace MoonWorks
{
@ -16,7 +17,7 @@ namespace MoonWorks
public Window Window { get; }
public GraphicsDevice GraphicsDevice { get; }
public AudioDevice AudioDevice { get; }
public Input Input { get; }
public Inputs Inputs { get; }
private Dictionary<PresentMode, RefreshCS.Refresh.PresentMode> moonWorksToRefreshPresentMode = new Dictionary<PresentMode, RefreshCS.Refresh.PresentMode>
{
@ -42,7 +43,7 @@ namespace MoonWorks
Logger.Initialize();
Input = new Input();
Inputs = new Inputs();
Window = new Window(windowCreateInfo);
@ -81,7 +82,7 @@ namespace MoonWorks
{
HandleSDLEvents();
Input.Update();
Inputs.Update();
AudioDevice.Update();
Update(timestep);

View File

@ -1,4 +1,4 @@
namespace MoonWorks
namespace MoonWorks.Input
{
public enum ButtonState
{

View File

@ -1,7 +1,7 @@
using System;
using SDL2;
namespace MoonWorks
namespace MoonWorks.Input
{
public class Gamepad
{

View File

@ -1,17 +1,19 @@
using SDL2;
using System.Collections.Generic;
namespace MoonWorks
namespace MoonWorks.Input
{
public class Input
public class Inputs
{
public Keyboard Keyboard { get; }
public Mouse Mouse { get; }
List<Gamepad> gamepads = new List<Gamepad>();
internal Input()
internal Inputs()
{
Keyboard = new Keyboard();
Mouse = new Mouse();
for (int i = 0; i < SDL.SDL_NumJoysticks(); i++)
{
@ -26,6 +28,7 @@ namespace MoonWorks
internal void Update()
{
Keyboard.Update();
Mouse.Update();
foreach (var gamepad in gamepads)
{

View File

@ -1,4 +1,4 @@
namespace MoonWorks
namespace MoonWorks.Input
{
internal class Key
{

View File

@ -2,7 +2,7 @@ using System;
using System.Runtime.InteropServices;
using SDL2;
namespace MoonWorks
namespace MoonWorks.Input
{
public class Keyboard
{

View File

@ -1,4 +1,4 @@
namespace MoonWorks
namespace MoonWorks.Input
{
// Enum values are equivalent to the SDL Scancode value.
public enum Keycode : int

65
src/Input/Mouse.cs Normal file
View File

@ -0,0 +1,65 @@
using SDL2;
namespace MoonWorks.Input
{
public class Mouse
{
public ButtonState LeftButton { get; private set; }
public ButtonState MiddleButton { get; private set; }
public ButtonState RightButton { get; private set; }
public int X { get; private set; }
public int Y { get; private set; }
public int DeltaX { get; private set; }
public int DeltaY { get; private set; }
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
);
}
}
internal void Update()
{
var buttons = SDL.SDL_GetMouseState(out var x, out var y);
var _ = SDL.SDL_GetRelativeMouseState(out var deltaX, out var deltaY);
X = x;
Y = y;
DeltaX = deltaX;
DeltaY = deltaY;
LeftButton = UpdateState(LeftButton, buttons, SDL.SDL_BUTTON_LEFT);
MiddleButton = UpdateState(MiddleButton, buttons, SDL.SDL_BUTTON_MIDDLE);
RightButton = UpdateState(RightButton, buttons, SDL.SDL_BUTTON_RIGHT);
}
private ButtonState UpdateState(ButtonState state, uint buttonMask, uint buttonFlag)
{
var isPressed = buttonMask & buttonFlag;
if (isPressed != 0)
{
if (state == ButtonState.Pressed)
{
return ButtonState.Held;
}
else if (state == ButtonState.Released)
{
return ButtonState.Pressed;
}
}
return ButtonState.Released;
}
}
}