Commit 8cf923ec authored by chili's avatar chili
Browse files

raw mouse input

parent e08532b0
......@@ -34,6 +34,7 @@ void App::DoFrame()
light.SpawnControlWindow();
ShowImguiDemoWindow();
nano.ShowWindow();
ShowRawInputWindow();
// present
wnd.Gfx().EndFrame();
......@@ -48,6 +49,20 @@ void App::ShowImguiDemoWindow()
}
}
void App::ShowRawInputWindow()
{
while( const auto d = wnd.mouse.ReadRawDelta() )
{
x += d->x;
y += d->y;
}
if( ImGui::Begin( "Raw Input" ) )
{
ImGui::Text( "Tally: (%d,%d)",x,y );
}
ImGui::End();
}
App::~App()
{}
......
......@@ -17,7 +17,9 @@ public:
private:
void DoFrame();
void ShowImguiDemoWindow();
void ShowRawInputWindow();
private:
int x = 0,y = 0;
ImguiManager imgui;
Window wnd;
ChiliTimer timer;
......
......@@ -21,11 +21,23 @@
#include "ChiliWin.h"
#include "Mouse.h"
std::pair<int,int> Mouse::GetPos() const noexcept
{
return { x,y };
}
std::optional<Mouse::RawDelta> Mouse::ReadRawDelta() noexcept
{
if( rawDeltaBuffer.empty() )
{
return std::nullopt;
}
const RawDelta d = rawDeltaBuffer.front();
rawDeltaBuffer.pop();
return d;
}
int Mouse::GetPosX() const noexcept
{
return x;
......@@ -90,6 +102,12 @@ void Mouse::OnMouseEnter() noexcept
TrimBuffer();
}
void Mouse::OnRawDelta( int dx,int dy ) noexcept
{
rawDeltaBuffer.push( { dx,dy } );
TrimBuffer();
}
void Mouse::OnLeftPressed( int x,int y ) noexcept
{
leftIsPressed = true;
......@@ -142,6 +160,14 @@ void Mouse::TrimBuffer() noexcept
}
}
void Mouse::TrimRawInputBuffer() noexcept
{
while( rawDeltaBuffer.size() > bufferSize )
{
rawDeltaBuffer.pop();
}
}
void Mouse::OnWheelDelta( int x,int y,int delta ) noexcept
{
wheelDeltaCarry += delta;
......
......@@ -26,6 +26,10 @@ class Mouse
{
friend class Window;
public:
struct RawDelta
{
int x,y;
};
class Event
{
public:
......@@ -86,6 +90,7 @@ public:
Mouse( const Mouse& ) = delete;
Mouse& operator=( const Mouse& ) = delete;
std::pair<int,int> GetPos() const noexcept;
std::optional<RawDelta> ReadRawDelta() noexcept;
int GetPosX() const noexcept;
int GetPosY() const noexcept;
bool IsInWindow() const noexcept;
......@@ -101,6 +106,7 @@ private:
void OnMouseMove( int x,int y ) noexcept;
void OnMouseLeave() noexcept;
void OnMouseEnter() noexcept;
void OnRawDelta( int dx,int dy ) noexcept;
void OnLeftPressed( int x,int y ) noexcept;
void OnLeftReleased( int x,int y ) noexcept;
void OnRightPressed( int x,int y ) noexcept;
......@@ -108,6 +114,7 @@ private:
void OnWheelUp( int x,int y ) noexcept;
void OnWheelDown( int x,int y ) noexcept;
void TrimBuffer() noexcept;
void TrimRawInputBuffer() noexcept;
void OnWheelDelta( int x,int y,int delta ) noexcept;
private:
static constexpr unsigned int bufferSize = 16u;
......@@ -118,4 +125,5 @@ private:
bool isInWindow = false;
int wheelDeltaCarry = 0;
std::queue<Event> buffer;
std::queue<RawDelta> rawDeltaBuffer;
};
\ No newline at end of file
......@@ -103,6 +103,16 @@ Window::Window( int width,int height,const char* name )
ImGui_ImplWin32_Init( hWnd );
// create graphics object
pGfx = std::make_unique<Graphics>( hWnd,width,height );
// register mouse raw input device
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x01; // mouse page
rid.usUsage = 0x02; // mouse usage
rid.dwFlags = 0;
rid.hwndTarget = nullptr;
if( RegisterRawInputDevices( &rid,1,sizeof( rid ) ) == FALSE )
{
throw CHWND_LAST_EXCEPT();
}
}
Window::~Window()
......@@ -416,6 +426,44 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
break;
}
/************** END MOUSE MESSAGES **************/
/************** RAW MOUSE MESSAGES **************/
case WM_INPUT:
{
UINT size;
// first get the size of the input data
if( GetRawInputData(
reinterpret_cast<HRAWINPUT>(lParam),
RID_INPUT,
nullptr,
&size,
sizeof( RAWINPUTHEADER ) ) == -1)
{
// bail msg processing if error
break;
}
rawBuffer.resize( size );
// read in the input data
if( GetRawInputData(
reinterpret_cast<HRAWINPUT>(lParam),
RID_INPUT,
rawBuffer.data(),
&size,
sizeof( RAWINPUTHEADER ) ) != size )
{
// bail msg processing if error
break;
}
// process the raw input data
auto& ri = reinterpret_cast<const RAWINPUT&>(*rawBuffer.data());
if( ri.header.dwType == RIM_TYPEMOUSE &&
(ri.data.mouse.lLastX != 0 || ri.data.mouse.lLastY != 0) )
{
mouse.OnRawDelta( ri.data.mouse.lLastX,ri.data.mouse.lLastY );
}
break;
}
/************** END RAW MOUSE MESSAGES **************/
}
return DefWindowProc( hWnd,msg,wParam,lParam );
......
......@@ -93,9 +93,10 @@ public:
Keyboard kbd;
Mouse mouse;
private:
bool cursorEnabled = false;
bool cursorEnabled = true;
int width;
int height;
HWND hWnd;
std::unique_ptr<Graphics> pGfx;
std::vector<BYTE> rawBuffer;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment