- add raw mouse input

This commit is contained in:
Magnus Norddahl 2019-08-16 06:39:18 +02:00
parent e035ff8113
commit 55c404e689
6 changed files with 204 additions and 2 deletions

View file

@ -19,6 +19,7 @@
using System;
using System.Windows.Forms;
using CodeImp.DoomBuilder.Geometry;
using System.Runtime.InteropServices;
#endregion
@ -89,16 +90,44 @@ namespace CodeImp.DoomBuilder.Actions
{
public RawMouse(System.Windows.Forms.Control control)
{
Handle = RawMouse_New(control.Handle);
if (Handle == IntPtr.Zero)
throw new Exception("VertexDeclaration_New failed");
}
~RawMouse()
{
Dispose();
}
public MouseState Poll()
{
// To do: use WM_RAWINPUT to get data
return new MouseState(0.0f, 0.0f);
return new MouseState(RawMouse_GetX(Handle), RawMouse_GetY(Handle));
}
public bool Disposed { get { return Handle == IntPtr.Zero; } }
public void Dispose()
{
if (!Disposed)
{
RawMouse_Delete(Handle);
Handle = IntPtr.Zero;
}
}
internal IntPtr Handle;
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr RawMouse_New(IntPtr windowHandle);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RawMouse_Delete(IntPtr handle);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern float RawMouse_GetX(IntPtr handle);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern float RawMouse_GetY(IntPtr handle);
}
}

View file

@ -206,6 +206,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="RawMouse.cpp" />
<ClCompile Include="RenderDevice.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="ShaderManager.cpp" />
@ -219,6 +220,7 @@
<ClInclude Include="IndexBuffer.h" />
<ClInclude Include="OpenGLContext.h" />
<ClInclude Include="Precomp.h" />
<ClInclude Include="RawMouse.h" />
<ClInclude Include="RenderDevice.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="ShaderDefault.h" />

View file

@ -13,6 +13,7 @@
<ClCompile Include="Precomp.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="ShaderManager.cpp" />
<ClCompile Include="RawMouse.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="IndexBuffer.h" />
@ -34,6 +35,7 @@
<ClInclude Include="ShaderDefault.h" />
<ClInclude Include="ShaderManager.h" />
<ClInclude Include="ShaderDisplay2D.h" />
<ClInclude Include="RawMouse.h" />
</ItemGroup>
<ItemGroup>
<None Include="exports.def" />

144
Source/Native/RawMouse.cpp Normal file
View file

@ -0,0 +1,144 @@
#include "Precomp.h"
#include "RawMouse.h"
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
#endif
#ifndef HID_USAGE_GENERIC_JOYSTICK
#define HID_USAGE_GENERIC_JOYSTICK ((USHORT) 0x04)
#endif
#ifndef HID_USAGE_GENERIC_GAMEPAD
#define HID_USAGE_GENERIC_GAMEPAD ((USHORT) 0x05)
#endif
#ifndef RIDEV_INPUTSINK
#define RIDEV_INPUTSINK (0x100)
#endif
class RawMouseWindowClass
{
public:
RawMouseWindowClass()
{
WNDCLASSEX windowClassDesc;
memset(&windowClassDesc, 0, sizeof(WNDCLASSEX));
windowClassDesc.cbSize = sizeof(WNDCLASSEX);
windowClassDesc.lpszClassName = ClassName;
windowClassDesc.hInstance = GetModuleHandle(nullptr);
windowClassDesc.lpfnWndProc = &RawMouse::WindowProc;
RegisterClassEx(&windowClassDesc);
}
const TCHAR* ClassName = TEXT("RawMouseWindow");
};
RawMouse::RawMouse(HWND ownerWindow)
{
static RawMouseWindowClass win32class;
handle = CreateWindowEx(0, win32class.ClassName, TEXT(""), WS_POPUP, 0, 0, 100, 100, 0, 0, GetModuleHandle(nullptr), this);
RAWINPUTDEVICE rid;
rid.usUsagePage = HID_USAGE_PAGE_GENERIC;
rid.usUsage = HID_USAGE_GENERIC_MOUSE;
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = handle;
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
}
RawMouse::~RawMouse()
{
if (handle)
DestroyWindow(handle);
}
float RawMouse::GetX()
{
float result = x;
x = 0;
return result;
}
float RawMouse::GetY()
{
float result = y;
y = 0;
return result;
}
LRESULT RawMouse::OnMessage(INT message, WPARAM wparam, LPARAM lparam)
{
if (message == WM_INPUT)
{
HRAWINPUT rawinputHandle = (HRAWINPUT)lparam;
UINT size = 0;
UINT result = GetRawInputData(rawinputHandle, RID_INPUT, 0, &size, sizeof(RAWINPUTHEADER));
if (result == 0 && size > 0)
{
std::vector<uint32_t> buf((size + 3) / 4);
result = GetRawInputData(rawinputHandle, RID_INPUT, buf.data(), &size, sizeof(RAWINPUTHEADER));
if (result >= 0)
{
RAWINPUT* rawinput = (RAWINPUT*)buf.data();
if (rawinput->header.dwType == RIM_TYPEMOUSE)
{
x += rawinput->data.mouse.lLastX;
y += rawinput->data.mouse.lLastY;
}
}
}
return 0;
}
else
{
return DefWindowProc(handle, message, wparam, lparam);
}
}
LRESULT RawMouse::WindowProc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam)
{
if (message == WM_CREATE)
{
CREATESTRUCT* createInfo = (CREATESTRUCT*)lparam;
auto window = reinterpret_cast<RawMouse*>(createInfo->lpCreateParams);
window->handle = handle;
SetWindowLongPtr(handle, GWLP_USERDATA, reinterpret_cast<ULONG_PTR>(window));
return window->OnMessage(message, wparam, lparam);
}
else
{
auto window = reinterpret_cast<RawMouse*>(GetWindowLongPtr(handle, GWLP_USERDATA));
if (window)
return window->OnMessage(message, wparam, lparam);
else
return DefWindowProc(handle, message, wparam, lparam);
}
}
/////////////////////////////////////////////////////////////////////////////
RawMouse* RawMouse_New(HWND hwnd)
{
return new RawMouse(hwnd);
}
void RawMouse_Delete(RawMouse* mouse)
{
delete mouse;
}
float RawMouse_GetX(RawMouse* mouse)
{
return mouse->GetX();
}
float RawMouse_GetY(RawMouse* mouse)
{
return mouse->GetY();
}

21
Source/Native/RawMouse.h Normal file
View file

@ -0,0 +1,21 @@
#pragma once
class RawMouse
{
public:
RawMouse(HWND ownerWindow);
~RawMouse();
float GetX();
float GetY();
private:
LRESULT OnMessage(INT message, WPARAM wparam, LPARAM lparam);
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
HWND handle = 0;
int x = 0;
int y = 0;
friend class RawMouseWindowClass;
};

View file

@ -52,3 +52,7 @@ EXPORTS
Texture_Delete
Texture_Set2DImage
Texture_SetCubeImage
RawMouse_New
RawMouse_Delete
RawMouse_GetX
RawMouse_GetY