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

raw mouse input

parent e08532b0
...@@ -34,6 +34,7 @@ void App::DoFrame() ...@@ -34,6 +34,7 @@ void App::DoFrame()
light.SpawnControlWindow(); light.SpawnControlWindow();
ShowImguiDemoWindow(); ShowImguiDemoWindow();
nano.ShowWindow(); nano.ShowWindow();
ShowRawInputWindow();
// present // present
wnd.Gfx().EndFrame(); wnd.Gfx().EndFrame();
...@@ -48,6 +49,20 @@ void App::ShowImguiDemoWindow() ...@@ -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() App::~App()
{} {}
......
...@@ -17,7 +17,9 @@ public: ...@@ -17,7 +17,9 @@ public:
private: private:
void DoFrame(); void DoFrame();
void ShowImguiDemoWindow(); void ShowImguiDemoWindow();
void ShowRawInputWindow();
private: private:
int x = 0,y = 0;
ImguiManager imgui; ImguiManager imgui;
Window wnd; Window wnd;
ChiliTimer timer; ChiliTimer timer;
......
...@@ -21,11 +21,23 @@ ...@@ -21,11 +21,23 @@
#include "ChiliWin.h" #include "ChiliWin.h"
#include "Mouse.h" #include "Mouse.h"
std::pair<int,int> Mouse::GetPos() const noexcept std::pair<int,int> Mouse::GetPos() const noexcept
{ {
return { x,y }; 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 int Mouse::GetPosX() const noexcept
{ {
return x; return x;
...@@ -90,6 +102,12 @@ void Mouse::OnMouseEnter() noexcept ...@@ -90,6 +102,12 @@ void Mouse::OnMouseEnter() noexcept
TrimBuffer(); TrimBuffer();
} }
void Mouse::OnRawDelta( int dx,int dy ) noexcept
{
rawDeltaBuffer.push( { dx,dy } );
TrimBuffer();
}
void Mouse::OnLeftPressed( int x,int y ) noexcept void Mouse::OnLeftPressed( int x,int y ) noexcept
{ {
leftIsPressed = true; leftIsPressed = true;
...@@ -142,6 +160,14 @@ void Mouse::TrimBuffer() noexcept ...@@ -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 void Mouse::OnWheelDelta( int x,int y,int delta ) noexcept
{ {
wheelDeltaCarry += delta; wheelDeltaCarry += delta;
......
...@@ -26,6 +26,10 @@ class Mouse ...@@ -26,6 +26,10 @@ class Mouse
{ {
friend class Window; friend class Window;
public: public:
struct RawDelta
{
int x,y;
};
class Event class Event
{ {
public: public:
...@@ -86,6 +90,7 @@ public: ...@@ -86,6 +90,7 @@ public:
Mouse( const Mouse& ) = delete; Mouse( const Mouse& ) = delete;
Mouse& operator=( const Mouse& ) = delete; Mouse& operator=( const Mouse& ) = delete;
std::pair<int,int> GetPos() const noexcept; std::pair<int,int> GetPos() const noexcept;
std::optional<RawDelta> ReadRawDelta() noexcept;
int GetPosX() const noexcept; int GetPosX() const noexcept;
int GetPosY() const noexcept; int GetPosY() const noexcept;
bool IsInWindow() const noexcept; bool IsInWindow() const noexcept;
...@@ -101,6 +106,7 @@ private: ...@@ -101,6 +106,7 @@ private:
void OnMouseMove( int x,int y ) noexcept; void OnMouseMove( int x,int y ) noexcept;
void OnMouseLeave() noexcept; void OnMouseLeave() noexcept;
void OnMouseEnter() noexcept; void OnMouseEnter() noexcept;
void OnRawDelta( int dx,int dy ) noexcept;
void OnLeftPressed( int x,int y ) noexcept; void OnLeftPressed( int x,int y ) noexcept;
void OnLeftReleased( int x,int y ) noexcept; void OnLeftReleased( int x,int y ) noexcept;
void OnRightPressed( int x,int y ) noexcept; void OnRightPressed( int x,int y ) noexcept;
...@@ -108,6 +114,7 @@ private: ...@@ -108,6 +114,7 @@ private:
void OnWheelUp( int x,int y ) noexcept; void OnWheelUp( int x,int y ) noexcept;
void OnWheelDown( int x,int y ) noexcept; void OnWheelDown( int x,int y ) noexcept;
void TrimBuffer() noexcept; void TrimBuffer() noexcept;
void TrimRawInputBuffer() noexcept;
void OnWheelDelta( int x,int y,int delta ) noexcept; void OnWheelDelta( int x,int y,int delta ) noexcept;
private: private:
static constexpr unsigned int bufferSize = 16u; static constexpr unsigned int bufferSize = 16u;
...@@ -118,4 +125,5 @@ private: ...@@ -118,4 +125,5 @@ private:
bool isInWindow = false; bool isInWindow = false;
int wheelDeltaCarry = 0; int wheelDeltaCarry = 0;
std::queue<Event> buffer; 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 ) ...@@ -103,6 +103,16 @@ Window::Window( int width,int height,const char* name )
ImGui_ImplWin32_Init( hWnd ); ImGui_ImplWin32_Init( hWnd );
// create graphics object // create graphics object
pGfx = std::make_unique<Graphics>( hWnd,width,height ); 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() Window::~Window()
...@@ -416,6 +426,44 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex ...@@ -416,6 +426,44 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
break; break;
} }
/************** END MOUSE MESSAGES **************/ /************** 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 ); return DefWindowProc( hWnd,msg,wParam,lParam );
......
...@@ -93,9 +93,10 @@ public: ...@@ -93,9 +93,10 @@ public:
Keyboard kbd; Keyboard kbd;
Mouse mouse; Mouse mouse;
private: private:
bool cursorEnabled = false; bool cursorEnabled = true;
int width; int width;
int height; int height;
HWND hWnd; HWND hWnd;
std::unique_ptr<Graphics> pGfx; 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