mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Refactored and completed WindowsInput implementation
This commit is contained in:
@@ -7,60 +7,79 @@ using static System.Windows;
|
|||||||
|
|
||||||
namespace GlitchyEngine
|
namespace GlitchyEngine
|
||||||
{
|
{
|
||||||
|
|
||||||
|
#if GE_WINDOWS
|
||||||
|
/// Windows (WinApi) specific implementation of the Input-class
|
||||||
extension Input
|
extension Input
|
||||||
{
|
{
|
||||||
static int8* LastKeyStates = new int8[256]* ~ delete _;
|
/// Represents the state of the input devices on the windows plattform (WinApi that is).
|
||||||
static int8* CurrentkeyStates = new int8[256]* ~ delete _;
|
struct WindowsInputState
|
||||||
|
{
|
||||||
|
public int8[256] KeyStates;
|
||||||
|
public Point CursorPosition;
|
||||||
|
public Point CursorPositionDifference;
|
||||||
|
}
|
||||||
|
|
||||||
[CLink, CallingConvention(.Stdcall)]
|
static WindowsInputState* CurrentState = new WindowsInputState() ~ delete _;
|
||||||
static extern int16 GetKeyState(int32 keycode);
|
static WindowsInputState* LastState = new WindowsInputState() ~ delete _;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Current Keyboard state
|
||||||
|
//
|
||||||
public override static bool IsKeyPressed(Key keycode)
|
public override static bool IsKeyPressed(Key keycode)
|
||||||
{
|
{
|
||||||
//int16 state = GetKeyState((.)keycode);
|
int8 state = CurrentState.KeyStates[(int)keycode];
|
||||||
int8 state = CurrentkeyStates[(int)keycode];
|
|
||||||
return state < 0;
|
return state < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override static bool IsKeyReleased(Key keycode)
|
public override static bool IsKeyReleased(Key keycode)
|
||||||
{
|
{
|
||||||
//int16 state = GetKeyState((.)keycode);
|
int8 state = CurrentState.KeyStates[(int)keycode];
|
||||||
int8 state = CurrentkeyStates[(int)keycode];
|
|
||||||
return state >= 0;
|
return state >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override static bool IsKeyToggled(Key keycode)
|
public override static bool IsKeyToggled(Key keycode)
|
||||||
{
|
{
|
||||||
//int16 state = GetKeyState((.)keycode);
|
int8 state = CurrentState.KeyStates[(int)keycode];
|
||||||
int8 state = CurrentkeyStates[(int)keycode];
|
|
||||||
return (state & 0x1) == 1;
|
return (state & 0x1) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Last Keyboard state
|
||||||
|
//
|
||||||
public override static bool WasKeyPressed(Key keycode)
|
public override static bool WasKeyPressed(Key keycode)
|
||||||
{
|
{
|
||||||
int8 state = LastKeyStates[(int)keycode];
|
int8 state = CurrentState.KeyStates[(int)keycode];
|
||||||
return state < 0;
|
return state < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override static bool WasKeyReleased(Key keycode)
|
public override static bool WasKeyReleased(Key keycode)
|
||||||
{
|
{
|
||||||
int8 state = LastKeyStates[(int)keycode];
|
int8 state = CurrentState.KeyStates[(int)keycode];
|
||||||
return state >= 0;
|
return state >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override static bool WasKeyToggled(Key keycode)
|
public override static bool WasKeyToggled(Key keycode)
|
||||||
{
|
{
|
||||||
int8 state = LastKeyStates[(int)keycode];
|
int8 state = LastState.KeyStates[(int)keycode];
|
||||||
return (state & 0x1) == 1;
|
return (state & 0x1) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Keyboard state transition
|
||||||
|
//
|
||||||
public override static bool IsKeyPressing(Key keycode)
|
public override static bool IsKeyPressing(Key keycode)
|
||||||
{
|
{
|
||||||
return default;
|
return IsKeyPressed(keycode) && WasKeyReleased(keycode);
|
||||||
//return IsKeyPressed(keycode) && WasKeyReleased(keycode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static mixin MouseButtonToKeyCode(MouseButton button)
|
public override static bool IsKeyReleasing(Key keycode)
|
||||||
|
{
|
||||||
|
return IsKeyReleased(keycode) && WasKeyPressed(keycode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// translates the MouseButton-enum to WinApi virtual keycodes
|
||||||
|
static mixin MouseButtonToKeyCode(MouseButton button)
|
||||||
{
|
{
|
||||||
int32 keycode;
|
int32 keycode;
|
||||||
switch(button)
|
switch(button)
|
||||||
@@ -82,71 +101,98 @@ namespace GlitchyEngine
|
|||||||
keycode
|
keycode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Current Mouse state
|
||||||
|
//
|
||||||
public override static bool IsMouseButtonPressed(MouseButton button)
|
public override static bool IsMouseButtonPressed(MouseButton button)
|
||||||
{
|
{
|
||||||
//int16 state = GetKeyState(MouseButtonToKeyCode!(button));
|
int8 state = CurrentState.KeyStates[MouseButtonToKeyCode!(button)];
|
||||||
int8 state = CurrentkeyStates[MouseButtonToKeyCode!(button)];
|
|
||||||
return state < 0;
|
return state < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override static bool IsMouseButtonReleased(MouseButton button)
|
public override static bool IsMouseButtonReleased(MouseButton button)
|
||||||
{
|
{
|
||||||
//int16 state = GetKeyState(MouseButtonToKeyCode!(button));
|
int8 state = CurrentState.KeyStates[MouseButtonToKeyCode!(button)];
|
||||||
int8 state = CurrentkeyStates[MouseButtonToKeyCode!(button)];
|
|
||||||
return state >= 0;
|
return state >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override static Point GetMousePosition() => CurrentState.CursorPosition;
|
||||||
|
|
||||||
|
public override static int32 GetMouseX() => CurrentState.CursorPosition.X;
|
||||||
|
|
||||||
|
public override static int32 GetMouseY() => CurrentState.CursorPosition.Y;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Last Mouse state
|
||||||
|
//
|
||||||
|
public override static bool WasMouseButtonPressed(MouseButton button)
|
||||||
|
{
|
||||||
|
int8 state = LastState.KeyStates[MouseButtonToKeyCode!(button)];
|
||||||
|
return state < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override static bool WasMouseButtonReleased(MouseButton button)
|
||||||
|
{
|
||||||
|
int8 state = LastState.KeyStates[MouseButtonToKeyCode!(button)];
|
||||||
|
return state >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override static Point GetLastMousePosition() => LastState.CursorPosition;
|
||||||
|
|
||||||
|
public override static int32 GetLastMouseX() => LastState.CursorPosition.X;
|
||||||
|
|
||||||
|
public override static int32 GetLastMouseY() => LastState.CursorPosition.Y;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Mouse state transition
|
||||||
|
//
|
||||||
|
public override static bool IsMouseButtonPressing(MouseButton button) => IsMouseButtonPressed(button) && WasMouseButtonReleased(button);
|
||||||
|
|
||||||
|
public override static bool IsMouseButtonReleasiong(MouseButton button) => IsMouseButtonReleased(button) && WasMouseButtonPressed(button);
|
||||||
|
|
||||||
|
public override static Point GetMouseMovement() => CurrentState.CursorPositionDifference;
|
||||||
|
|
||||||
|
|
||||||
|
//[CLink, CallingConvention(.Stdcall)]
|
||||||
|
//static extern int16 GetKeyState(int32 keycode);
|
||||||
[CLink, CallingConvention(.Stdcall)]
|
[CLink, CallingConvention(.Stdcall)]
|
||||||
static extern IntBool GetCursorPos(out Point p);
|
static extern IntBool GetCursorPos(out Point p);
|
||||||
[CLink, CallingConvention(.Stdcall)]
|
[CLink, CallingConvention(.Stdcall)]
|
||||||
static extern IntBool ScreenToClient(HWnd hWnd, ref Point p);
|
static extern IntBool ScreenToClient(HWnd hWnd, ref Point p);
|
||||||
|
|
||||||
static mixin GetMousePos()
|
|
||||||
{
|
|
||||||
Point point;
|
|
||||||
if (GetCursorPos(out point))
|
|
||||||
{
|
|
||||||
HWnd windowHandle = (HWnd)(int)Application.Get().Window.NativeWindow;
|
|
||||||
if (ScreenToClient(windowHandle, ref point))
|
|
||||||
{
|
|
||||||
// Todo: error handling?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
point
|
|
||||||
}
|
|
||||||
|
|
||||||
public override static Point GetMousePosition()
|
|
||||||
{
|
|
||||||
return GetMousePos!();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override static int32 GetMouseX()
|
|
||||||
{
|
|
||||||
return GetMousePos!().X;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override static int32 GetMouseY()
|
|
||||||
{
|
|
||||||
return GetMousePos!().Y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override static void NewFrame()
|
public override static void NewFrame()
|
||||||
{
|
{
|
||||||
// Switch last and current keyStates
|
// Switch last and current states
|
||||||
int8* buffer = LastKeyStates;
|
var buffer = LastState;
|
||||||
LastKeyStates = CurrentkeyStates;
|
LastState = CurrentState;
|
||||||
CurrentkeyStates = buffer;
|
CurrentState = buffer;
|
||||||
|
|
||||||
// Get current keyboard state
|
// Get current keyboard state
|
||||||
var result = DirectX.Windows.Winuser.GetKeyboardState((uint8*)CurrentkeyStates);
|
if(DirectX.Windows.Winuser.GetKeyboardState((uint8*)&CurrentState.KeyStates) == 0)
|
||||||
|
|
||||||
if(result == 0)
|
|
||||||
{
|
{
|
||||||
DirectX.Common.HResult res = (.)GetLastError();
|
DirectX.Common.HResult errorCode = (.)GetLastError();
|
||||||
|
Log.EngineLogger.Error("Failed to get keyboard state. Message({}){}:", (int32)errorCode, errorCode);
|
||||||
Log.EngineLogger.Error("Failed to get keyboard state Message({}){}:", (int32)res, res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the current mouse position
|
||||||
|
if (GetCursorPos(out CurrentState.CursorPosition) != 0)
|
||||||
|
{
|
||||||
|
HWnd windowHandle = (HWnd)(int)Application.Get().Window.NativeWindow;
|
||||||
|
if (ScreenToClient(windowHandle, ref CurrentState.CursorPosition) == 0)
|
||||||
|
{
|
||||||
|
DirectX.Common.HResult errorCode = (.)GetLastError();
|
||||||
|
Log.EngineLogger.Error("Failed to convert coordinates from screen to client space. Message({}){}:", (int32)errorCode, errorCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DirectX.Common.HResult errorCode = (.)GetLastError();
|
||||||
|
Log.EngineLogger.Error("Failed to get mouse position. Message({}){}:", (int32)errorCode, errorCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate cursor movement
|
||||||
|
CurrentState.CursorPositionDifference = CurrentState.CursorPosition - LastState.CursorPosition;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user