Danbias/Code/Misc/Input/Source/Win32/Win32Mouse.cpp

237 lines
5.5 KiB
C++
Raw Normal View History

2014-02-14 15:50:00 +01:00
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
2014-02-24 16:01:06 +01:00
#include "..\..\Include\Win32\Win32Input.h"
2014-02-14 15:50:00 +01:00
using namespace Input;
using namespace Input::Enum;
using namespace Input::Struct;
using namespace Input::Typedefs;
2014-02-24 16:01:06 +01:00
void MapButton(RAWMOUSE& rawMouse, bool &isUp, Enum::SAMI& btn, int& delta, Struct::SAIPointInt2D& vel, unsigned int& mcode)
{
if(rawMouse.lLastX != 0 || rawMouse.lLastY != 0)
{
vel.x = rawMouse.lLastX;
vel.y = rawMouse.lLastY;
}
if( rawMouse.usButtonFlags > 0 )
{
//--------------------------------------------------------------------------------------
//Mouse button pressed
if(rawMouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_DOWN)
{
btn = SAMI_MouseLeftBtn;
isUp = false;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_MIDDLE_BUTTON_DOWN)
{
btn = SAMI_MouseMiddleBtn;
isUp = false;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_DOWN)
{
btn = SAMI_MouseRightBtn;
isUp = false;
}
//--------------------------------------------------------------------------------------
//Mouse button Released
else if(rawMouse.usButtonFlags == RI_MOUSE_LEFT_BUTTON_UP)
{
btn = SAMI_MouseLeftBtn;
isUp = true;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_MIDDLE_BUTTON_UP)
{
btn = SAMI_MouseMiddleBtn;
isUp = true;
}
else if(rawMouse.usButtonFlags == RI_MOUSE_RIGHT_BUTTON_UP)
{
btn = SAMI_MouseRightBtn;
isUp = true;
}
//--------------------------------------------------------------------------------------
else if (rawMouse.usButtonFlags == RI_MOUSE_WHEEL)
{
delta = ((int)rawMouse.usButtonData);
if(delta > 120) delta = -1;
else delta = 1;
}
}
}
2014-02-24 16:01:06 +01:00
void ContainPoint(Struct::SAIPointInt2D& pixelPos, Struct::SAIPointInt2D& windowSize)
{
if(pixelPos.x < 0) { pixelPos.x = 0; }
else if(pixelPos.x > windowSize.x) { pixelPos.x = windowSize.x; }
if(pixelPos.y < 0) { pixelPos.y = 0; }
else if(pixelPos.y > windowSize.y) { pixelPos.y = windowSize.y; }
}
2014-02-14 15:50:00 +01:00
2014-02-24 16:01:06 +01:00
Win32Mouse::Win32Mouse(HWND target)
2014-02-14 15:50:00 +01:00
{
2014-02-24 16:01:06 +01:00
this->isActive = false;
this->device.usUsagePage = 0x01;
this->device.hwndTarget = target;
this->device.usUsage = RawInput_Usage_mouse;
this->device.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE;
2014-02-14 15:50:00 +01:00
memset(&this->buttons[0], 0, sizeof(Buttons) * MAXBUTTONS);
}
Win32Mouse::~Win32Mouse()
2014-02-24 16:01:06 +01:00
{ }
2014-02-14 15:50:00 +01:00
2014-02-24 16:01:06 +01:00
bool Win32Mouse::IsBtnUp(Enum::SAMI btn) const
2014-02-14 15:50:00 +01:00
{
if(btn == SAMI_Unknown) return false;
return !this->buttons[btn].isDown;
}
2014-02-24 16:01:06 +01:00
bool Win32Mouse::IsBtnDown(Enum::SAMI btn) const
2014-02-14 15:50:00 +01:00
{
if(btn == SAMI_Unknown) return false;
return this->buttons[btn].isDown;
}
2014-02-24 16:01:06 +01:00
int Win32Mouse::GetWheelDelta() const
{
return this->wheelDelta;
}
SAIPointInt2D& Win32Mouse::GetPixelPosition( Struct::SAIPointInt2D &targetMem ) const
{
memcpy(&targetMem, &this->pixelPos, sizeof(SAIPointFloat2D));
return targetMem;
}
SAIPointFloat2D& Win32Mouse::GetNormalizedPosition(Struct::SAIPointFloat2D& targetMem)
{
RECT windowVertex;
GetWindowRect( this->device.hwndTarget, &windowVertex );
this->normalPos.x = (float)(pixelPos.x - windowVertex.left);
this->normalPos.x /= (float)(windowVertex.right - windowVertex.left);
this->normalPos.y = (float)(pixelPos.y - windowVertex.top);
this->normalPos.y /= (float)(windowVertex.bottom - windowVertex.top);
memcpy(&targetMem, &this->normalPos, sizeof(SAIPointFloat2D));
return targetMem;
}
SAIPointFloat2D& Win32Mouse::GetDeltaPosition(Struct::SAIPointFloat2D& targetMem) const
{
memcpy(&targetMem, &this->deltaPos, sizeof(SAIPointFloat2D));
return targetMem;
}
void Win32Mouse::Activate ()
{
if(this->isActive) return;
this->Create();
}
void Win32Mouse::Deactivate ()
{
if(!this->isActive) return;
RAWINPUTDEVICE d;
d.dwFlags = RIDEV_REMOVE;
d.hwndTarget = 0;
d.usUsage = RawInput_Usage_mouse;
d.usUsagePage = 0x01;
if(RegisterRawInputDevices(&d, 1, sizeof(RAWINPUTDEVICE)))
{
this->isActive = false;
SetCursorPos(this->winCursPos.x, this->winCursPos.y);
ShowCursor(TRUE);
2014-02-24 16:01:06 +01:00
}
}
2014-02-20 11:27:43 +01:00
void Win32Mouse::ProccessMouseData (RAWMOUSE mouse)
2014-02-14 15:50:00 +01:00
{
bool isUp = true;
Enum::SAMI btn = Enum::SAMI_Unknown;
int delta = 0;
2014-02-24 16:01:06 +01:00
Struct::SAIPointInt2D velocity;
unsigned int makeCode = 0;
MapButton(mouse, isUp, btn, delta, velocity, makeCode);
2014-02-14 15:50:00 +01:00
if(velocity.Length() != 0)
{
2014-02-24 16:01:06 +01:00
this->pixelPos.x += velocity.x;
this->pixelPos.y += velocity.y;
ContainPoint(this->pixelPos, this->windowSize);
InternalOnMove(this->pixelPos, velocity);
2014-02-14 15:50:00 +01:00
}
if(delta != 0)
{
InternalOnScroll(delta);
2014-02-14 15:50:00 +01:00
}
if(btn == SAMI_Unknown) return;
this->buttons[btn].isDown = !isUp;
this->buttons[btn].makeCode = makeCode;
//The btn is released.
if(isUp)
{
InternalOnBtnRelease(btn);
2014-02-14 15:50:00 +01:00
}
//The btn is pressed.
else
{
//The btn is down since last frame
2014-02-14 15:50:00 +01:00
if(this->buttons[btn].isDown)
{
InternalOnBtnDown(btn);
2014-02-14 15:50:00 +01:00
}
else
{
InternalOnBtnPress(btn);
2014-02-14 15:50:00 +01:00
}
}
2014-02-24 16:01:06 +01:00
}
bool Win32Mouse::Create()
{
if(!this->device.hwndTarget)
{
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetClientRect(hDesktop, &desktop);
windowSize.x = desktop.right;
windowSize.y = desktop.bottom;
this->device.dwFlags = 0;
}
else
{
RECT re;
GetClientRect(this->device.hwndTarget, &re);
windowSize.x = re.right - re.left;
windowSize.y = re.bottom - re.top;
}
if(RegisterRawInputDevices(&this->device, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{
this->isActive = true;
POINT p;
GetCursorPos(&p);
this->winCursPos.x = p.x;
this->winCursPos.y = p.y;
ShowCursor(FALSE);
2014-02-24 16:01:06 +01:00
return true;
}
return false;
}