forked from MoonsideGames/MoonWorks
add mouse input + move inputs to Input namespace
parent
1e77ad2cb4
commit
f2487cd71b
|
@ -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);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace MoonWorks
|
||||
namespace MoonWorks.Input
|
||||
{
|
||||
public enum ButtonState
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using SDL2;
|
||||
|
||||
namespace MoonWorks
|
||||
namespace MoonWorks.Input
|
||||
{
|
||||
public class Gamepad
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace MoonWorks
|
||||
namespace MoonWorks.Input
|
||||
{
|
||||
internal class Key
|
||||
{
|
||||
|
|
|
@ -2,7 +2,7 @@ using System;
|
|||
using System.Runtime.InteropServices;
|
||||
using SDL2;
|
||||
|
||||
namespace MoonWorks
|
||||
namespace MoonWorks.Input
|
||||
{
|
||||
public class Keyboard
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace MoonWorks
|
||||
namespace MoonWorks.Input
|
||||
{
|
||||
// Enum values are equivalent to the SDL Scancode value.
|
||||
public enum Keycode : int
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue