diff --git a/GlitchyEngine/src/Input.bf b/GlitchyEngine/src/Input.bf new file mode 100644 index 0000000..fbf6484 --- /dev/null +++ b/GlitchyEngine/src/Input.bf @@ -0,0 +1,21 @@ +using System; +using GlitchyEngine.Events; +using GlitchyEngine.Math; + +namespace GlitchyEngine +{ + public static class Input + { + public static extern bool IsKeyPressed(int32 keycode); + public static extern bool IsKeyReleased(int32 keycode); + public static extern bool IsKeyToggled(int32 keycode); + + public static extern bool IsMouseButtonPressed(MouseButton button); + public static extern bool IsMouseButtonReleased(MouseButton button); + + public static extern int32 GetMouseX(); + public static extern int32 GetMouseY(); + + public static extern Point GetMousePosition(); + } +} diff --git a/GlitchyEngine/src/Platform/Windows/WindowsInput.bf b/GlitchyEngine/src/Platform/Windows/WindowsInput.bf new file mode 100644 index 0000000..740b20d --- /dev/null +++ b/GlitchyEngine/src/Platform/Windows/WindowsInput.bf @@ -0,0 +1,102 @@ +using System; +using GlitchyEngine.Events; +using DirectX.Windows.VirtualKeyCodes; +using DirectX.Windows; +using GlitchyEngine.Math; +using static System.Windows; + +namespace GlitchyEngine +{ + extension Input + { + [CLink, CallingConvention(.Stdcall)] + static extern int16 GetKeyState(int32 keycode); + + public override static bool IsKeyPressed(int32 keycode) + { + int16 state = GetKeyState(keycode); + return state < 0; + } + + public override static bool IsKeyReleased(int32 keycode) + { + int16 state = GetKeyState(keycode); + return state >= 0; + } + + public override static bool IsKeyToggled(int32 keycode) + { + int16 state = GetKeyState(keycode); + return (state & 0x1) == 1; + } + + private static mixin MouseButtonToKeyCode(MouseButton button) + { + int32 keycode; + switch(button) + { + case .LeftButton: + keycode = VK_LBUTTON; + case .RightButton: + keycode = VK_RBUTTON; + case .MiddleButton: + keycode = VK_MBUTTON; + case .XButton1: + keycode = VK_XBUTTON1; + case .XButton2: + keycode = VK_XBUTTON2; + case default: + return false; + } + + keycode + } + + public override static bool IsMouseButtonPressed(MouseButton button) + { + int16 state = GetKeyState(MouseButtonToKeyCode!(button)); + return state < 0; + } + + public override static bool IsMouseButtonReleased(MouseButton button) + { + int16 state = GetKeyState(MouseButtonToKeyCode!(button)); + return state >= 0; + } + + [CLink, CallingConvention(.Stdcall)] + static extern IntBool GetCursorPos(out Point p); + [CLink, CallingConvention(.Stdcall)] + 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; + } + } +}