Commit 9bde6218 authored by chili's avatar chili
Browse files

basic mouse input

parent 7640b5df
/******************************************************************************************
* Chili DirectX Framework Version 16.07.20 *
* Mouse.cpp *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "Mouse.h"
std::pair<int,int> Mouse::GetPos() const noexcept
{
return { x,y };
}
int Mouse::GetPosX() const noexcept
{
return x;
}
int Mouse::GetPosY() const noexcept
{
return y;
}
bool Mouse::LeftIsPressed() const noexcept
{
return leftIsPressed;
}
bool Mouse::RightIsPressed() const noexcept
{
return rightIsPressed;
}
Mouse::Event Mouse::Read() noexcept
{
if( buffer.size() > 0u )
{
Mouse::Event e = buffer.front();
buffer.pop();
return e;
}
else
{
return Mouse::Event();
}
}
void Mouse::Flush() noexcept
{
buffer = std::queue<Event>();
}
void Mouse::OnMouseMove( int newx,int newy ) noexcept
{
x = newx;
y = newy;
buffer.push( Mouse::Event( Mouse::Event::Type::Move,*this ) );
TrimBuffer();
}
void Mouse::OnLeftPressed( int x,int y ) noexcept
{
leftIsPressed = true;
buffer.push( Mouse::Event( Mouse::Event::Type::LPress,*this ) );
TrimBuffer();
}
void Mouse::OnLeftReleased( int x,int y ) noexcept
{
leftIsPressed = false;
buffer.push( Mouse::Event( Mouse::Event::Type::LRelease,*this ) );
TrimBuffer();
}
void Mouse::OnRightPressed( int x,int y ) noexcept
{
rightIsPressed = true;
buffer.push( Mouse::Event( Mouse::Event::Type::RPress,*this ) );
TrimBuffer();
}
void Mouse::OnRightReleased( int x,int y ) noexcept
{
rightIsPressed = false;
buffer.push( Mouse::Event( Mouse::Event::Type::RRelease,*this ) );
TrimBuffer();
}
void Mouse::OnWheelUp( int x,int y ) noexcept
{
buffer.push( Mouse::Event( Mouse::Event::Type::WheelUp,*this ) );
TrimBuffer();
}
void Mouse::OnWheelDown( int x,int y ) noexcept
{
buffer.push( Mouse::Event( Mouse::Event::Type::WheelDown,*this ) );
TrimBuffer();
}
void Mouse::TrimBuffer() noexcept
{
while( buffer.size() > bufferSize )
{
buffer.pop();
}
}
\ No newline at end of file
/******************************************************************************************
* Chili DirectX Framework Version 16.07.20 *
* Mouse.h *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include <queue>
class Mouse
{
friend class Window;
public:
class Event
{
public:
enum class Type
{
LPress,
LRelease,
RPress,
RRelease,
WheelUp,
WheelDown,
Move,
Invalid
};
private:
Type type;
bool leftIsPressed;
bool rightIsPressed;
int x;
int y;
public:
Event() noexcept
:
type( Type::Invalid ),
leftIsPressed( false ),
rightIsPressed( false ),
x( 0 ),
y( 0 )
{}
Event( Type type,const Mouse& parent ) noexcept
:
type( type ),
leftIsPressed( parent.leftIsPressed ),
rightIsPressed( parent.rightIsPressed ),
x( parent.x ),
y( parent.y )
{}
bool IsValid() const noexcept
{
return type != Type::Invalid;
}
Type GetType() const noexcept
{
return type;
}
std::pair<int,int> GetPos() const noexcept
{
return{ x,y };
}
int GetPosX() const noexcept
{
return x;
}
int GetPosY() const noexcept
{
return y;
}
bool LeftIsPressed() const noexcept
{
return leftIsPressed;
}
bool RightIsPressed() const noexcept
{
return rightIsPressed;
}
};
public:
Mouse() = default;
Mouse( const Mouse& ) = delete;
Mouse& operator=( const Mouse& ) = delete;
std::pair<int,int> GetPos() const noexcept;
int GetPosX() const noexcept;
int GetPosY() const noexcept;
bool LeftIsPressed() const noexcept;
bool RightIsPressed() const noexcept;
Mouse::Event Read() noexcept;
bool IsEmpty() const noexcept
{
return buffer.empty();
}
void Flush() noexcept;
private:
void OnMouseMove( int x,int y ) noexcept;
void OnLeftPressed( int x,int y ) noexcept;
void OnLeftReleased( int x,int y ) noexcept;
void OnRightPressed( int x,int y ) noexcept;
void OnRightReleased( int x,int y ) noexcept;
void OnWheelUp( int x,int y ) noexcept;
void OnWheelDown( int x,int y ) noexcept;
void TrimBuffer() noexcept;
private:
static constexpr unsigned int bufferSize = 16u;
int x;
int y;
bool leftIsPressed = false;
bool rightIsPressed = false;
std::queue<Event> buffer;
};
\ No newline at end of file
......@@ -159,6 +159,51 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
kbd.OnChar( static_cast<unsigned char>(wParam) );
break;
/*********** END KEYBOARD MESSAGES ***********/
/************* MOUSE MESSAGES ****************/
case WM_MOUSEMOVE:
{
POINTS pt = MAKEPOINTS( lParam );
mouse.OnMouseMove( pt.x,pt.y );
}
case WM_LBUTTONDOWN:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnLeftPressed( pt.x,pt.y );
break;
}
case WM_RBUTTONDOWN:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnRightPressed( pt.x,pt.y );
break;
}
case WM_LBUTTONUP:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnLeftReleased( pt.x,pt.y );
break;
}
case WM_RBUTTONUP:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnRightReleased( pt.x,pt.y );
break;
}
case WM_MOUSEWHEEL:
{
const POINTS pt = MAKEPOINTS( lParam );
if( GET_WHEEL_DELTA_WPARAM( wParam ) > 0 )
{
mouse.OnWheelUp( pt.x,pt.y );
}
else if( GET_WHEEL_DELTA_WPARAM( wParam ) < 0 )
{
mouse.OnWheelDown( pt.x,pt.y );
}
break;
}
/************** END MOUSE MESSAGES **************/
}
return DefWindowProc( hWnd,msg,wParam,lParam );
......
......@@ -21,6 +21,7 @@
#include "ChiliWin.h"
#include "ChiliException.h"
#include "Keyboard.h"
#include "Mouse.h"
class Window
......@@ -65,6 +66,7 @@ private:
LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
public:
Keyboard kbd;
Mouse mouse;
private:
int width;
int height;
......
......@@ -147,6 +147,7 @@
<ItemGroup>
<ClCompile Include="ChiliException.cpp" />
<ClCompile Include="Keyboard.cpp" />
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="Window.cpp" />
<ClCompile Include="WindowsMessageMap.cpp" />
<ClCompile Include="WinMain.cpp" />
......@@ -155,6 +156,7 @@
<ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliWin.h" />
<ClInclude Include="Keyboard.h" />
<ClInclude Include="Mouse.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="Window.h" />
<ClInclude Include="WindowsMessageMap.h" />
......
......@@ -30,6 +30,9 @@
<ClCompile Include="Keyboard.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Mouse.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WindowsMessageMap.h">
......@@ -50,6 +53,9 @@
<ClInclude Include="Keyboard.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Mouse.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hw3d.rc">
......
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