Input - More support for mouse

This commit is contained in:
Dennis Andersen 2014-02-21 21:04:30 +01:00
parent dee6c5f8d1
commit 44aab68bfb
18 changed files with 261 additions and 102 deletions

View File

@ -60,8 +60,8 @@ namespace DanBias
~DanBiasGamePrivateData() ~DanBiasGamePrivateData()
{ {
SafeDeleteInstance( this->sharedStateContent.mouseDevice ); //SafeDeleteInstance( this->sharedStateContent.mouseDevice );
SafeDeleteInstance( this->sharedStateContent.keyboardDevice ); //SafeDeleteInstance( this->sharedStateContent.keyboardDevice );
} }
} data; } data;
} }
@ -273,6 +273,9 @@ LRESULT CALLBACK WindowCallBack(HWND handle, UINT message, WPARAM wParam, LPARAM
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage( 0 ); PostQuitMessage( 0 );
break; break;
case WM_INPUT:
message = 0;
break;
default: break; default: break;
} }

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ShowAllFiles>false</ShowAllFiles> <ShowAllFiles>true</ShowAllFiles>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory> <LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>

View File

@ -161,7 +161,7 @@ void GamingUI::ReadKeyInput()
//send delta mouse movement //send delta mouse movement
{ {
static const float mouseSensitivity = Radian( 1.0f ); static const float mouseSensitivity = Radian( 1.0f );
::Input::Struct::SAIPoint2D deltaPos; ::Input::Struct::SAIPointFloat2D deltaPos;
this->mouseInput->GetDeltaPosition( deltaPos ); this->mouseInput->GetDeltaPosition( deltaPos );
this->camera->PitchDown( deltaPos.y * mouseSensitivity );; this->camera->PitchDown( deltaPos.y * mouseSensitivity );;

View File

@ -92,7 +92,7 @@ GameClientState::ClientState LanMenuState::Update( float deltaTime )
{ {
MouseInput mouseState; MouseInput mouseState;
{ {
::Input::Struct::SAIPoint2D pos; ::Input::Struct::SAIPointInt2D pos;
this->privData->mouseInput->GetPixelPosition( pos ); this->privData->mouseInput->GetPixelPosition( pos );
mouseState.x = pos.x; mouseState.x = pos.x;

View File

@ -63,7 +63,7 @@ GameClientState::ClientState LobbyAdminState::Update( float deltaTime )
{ {
MouseInput mouseState; MouseInput mouseState;
{ {
::Input::Struct::SAIPoint2D pos; ::Input::Struct::SAIPointInt2D pos;
this->privData->mouseInput->GetPixelPosition( pos ); this->privData->mouseInput->GetPixelPosition( pos );
mouseState.x = pos.x; mouseState.x = pos.x;

View File

@ -63,7 +63,7 @@ GameClientState::ClientState LobbyState::Update( float deltaTime )
{ {
MouseInput mouseState; MouseInput mouseState;
{ {
::Input::Struct::SAIPoint2D pos; ::Input::Struct::SAIPointInt2D pos;
this->privData->mouseInput->GetPixelPosition( pos ); this->privData->mouseInput->GetPixelPosition( pos );
mouseState.x = pos.x; mouseState.x = pos.x;

View File

@ -81,8 +81,8 @@ GameClientState::ClientState MainState::Update( float deltaTime )
{ {
MouseInput mouseState; MouseInput mouseState;
{ {
::Input::Struct::SAIPoint2D pos; ::Input::Struct::SAIPointFloat2D pos;
this->privData->mouseInput->GetPixelPosition( pos ); this->privData->mouseInput->GetNormalizedPosition( pos );
this->privData->mousePos.x = mouseState.x = pos.x; this->privData->mousePos.x = mouseState.x = pos.x;
this->privData->mousePos.y = mouseState.y = pos.y; this->privData->mousePos.y = mouseState.y = pos.y;

View File

@ -26,6 +26,11 @@ namespace Input
SAIType_futureExample2, SAIType_futureExample2,
SAIType_futureExample3, SAIType_futureExample3,
}; };
enum InputOptionType
{
InputOptionType_RawInput,
InputOptionType_PlatformDefault,
};
enum ButtonState enum ButtonState
{ {
ButtonState_Press, // When button is pressed (once) ButtonState_Press, // When button is pressed (once)
@ -37,15 +42,24 @@ namespace Input
/*********************************************************************/ /*********************************************************************/
namespace Struct namespace Struct
{ {
struct SAIPoint2D struct SAIPointInt2D
{ {
int x; int x;
int y; int y;
SAIPoint2D() :x(0), y(0) { } SAIPointInt2D() :x(0), y(0) { }
SAIPoint2D(int _x, int _y) :x(_x), y(_y) { } SAIPointInt2D(int _x, int _y) :x(_x), y(_y) { }
int Length() { return (abs(x) + abs(y)); } int Length() { return (abs(x) + abs(y)); }
}; };
struct SAIPointFloat2D
{
float x;
float y;
SAIPointFloat2D() :x(0.0f), y(0.0f) { }
SAIPointFloat2D(float _x, float _y) :x(_x), y(_y) { }
float Length() { return (fabs(x) + fabs(y)); }
};
struct InputData; struct InputData;
} }
/*********************************************************************/ /*********************************************************************/

View File

@ -16,6 +16,8 @@ namespace Input
virtual void Activate () = 0; virtual void Activate () = 0;
virtual void Deactivate () = 0; virtual void Deactivate () = 0;
virtual bool IsActive() = 0; virtual bool IsActive() = 0;
virtual bool IsInputOptionSuported(Enum::InputOptionType options) const = 0;
virtual void SetInputOptionType(Enum::InputOptionType options) = 0;
protected: protected:
InputObject(Enum::SAIType type) { this->type = type; } InputObject(Enum::SAIType type) { this->type = type; }

View File

@ -156,6 +156,8 @@ namespace Input
virtual bool IsKeyUp (Enum::SAKI key) = 0; virtual bool IsKeyUp (Enum::SAKI key) = 0;
virtual bool IsKeyDown (Enum::SAKI key) = 0; virtual bool IsKeyDown (Enum::SAKI key) = 0;
virtual wchar_t* GetAsText(Enum::SAKI key) = 0; virtual wchar_t* GetAsText(Enum::SAKI key) = 0;
virtual void SetInputOptionType(Enum::InputOptionType options) override = 0;
virtual bool IsInputOptionSuported(Enum::InputOptionType options) const override = 0;
public: /* global subscribe callback functions */ public: /* global subscribe callback functions */
void AddOnKeyPressCallback (Typedefs::OnKeyPressCallback func); void AddOnKeyPressCallback (Typedefs::OnKeyPressCallback func);
@ -195,6 +197,7 @@ namespace Input
::std::wstring* textTarget; ::std::wstring* textTarget;
::std::wstring::size_type writePos; ::std::wstring::size_type writePos;
bool active; bool active;
Enum::InputOptionType inputMode;
}; };
} }

View File

@ -47,7 +47,7 @@ namespace Input
typedef void(*OnMousePressCallback)(Enum::SAMI btn, Mouse* sender); typedef void(*OnMousePressCallback)(Enum::SAMI btn, Mouse* sender);
typedef void(*OnMouseDownCallback)(Enum::SAMI btn, Mouse* sender); typedef void(*OnMouseDownCallback)(Enum::SAMI btn, Mouse* sender);
typedef void(*OnMouseReleaseCallback)(Enum::SAMI btn, Mouse* sender); typedef void(*OnMouseReleaseCallback)(Enum::SAMI btn, Mouse* sender);
typedef void(*OnMouseMoveCallback)(Struct::SAIPoint2D cord, Mouse* sender); typedef void(*OnMouseMoveCallback)(Struct::SAIPointInt2D cord, Mouse* sender);
typedef void(*OnMouseScrollCallback)(int delta, Mouse* sender); typedef void(*OnMouseScrollCallback)(int delta, Mouse* sender);
} }
//----------------------------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------------------------
@ -62,21 +62,22 @@ namespace Input
virtual void OnMousePress ( Enum::SAMI key, Mouse* sender ) { } virtual void OnMousePress ( Enum::SAMI key, Mouse* sender ) { }
virtual void OnMouseDown ( Enum::SAMI key, Mouse* sender ) { } virtual void OnMouseDown ( Enum::SAMI key, Mouse* sender ) { }
virtual void OnMouseRelease ( Enum::SAMI key, Mouse* sender ) { } virtual void OnMouseRelease ( Enum::SAMI key, Mouse* sender ) { }
virtual void OnMouseMove ( Struct::SAIPoint2D coordinate, Mouse* sender ) { } virtual void OnMouseMove ( Struct::SAIPointInt2D coordinate, Mouse* sender ) { }
virtual void OnMouseScroll ( int delta, Mouse* sender ) { } virtual void OnMouseScroll ( int delta, Mouse* sender ) { }
}; };
public: /* Manual check functions */ public: /* Manual check functions */
virtual ~Mouse(); virtual bool IsBtnUp(Enum::SAMI key) const = 0;
virtual bool IsBtnDown(Enum::SAMI key) const = 0;
virtual int GetWheelDelta() const = 0;
virtual Struct::SAIPointInt2D& GetPixelPosition(Struct::SAIPointInt2D& targetMem = Struct::SAIPointInt2D()) const = 0;
virtual Struct::SAIPointFloat2D& GetNormalizedPosition(Struct::SAIPointFloat2D& targetMem = Struct::SAIPointFloat2D()) const = 0;
virtual Struct::SAIPointFloat2D& GetDeltaPosition(Struct::SAIPointFloat2D& targetMem = Struct::SAIPointFloat2D()) const = 0;
virtual void SetInputOptionType(Enum::InputOptionType options) override = 0;
virtual bool IsInputOptionSuported(Enum::InputOptionType options) const override = 0;
virtual bool IsBtnUp(Enum::SAMI key) = 0;
virtual bool IsBtnDown(Enum::SAMI key) = 0;
public: /* global subscribe callback functions */ public: /* global subscribe callback functions */
int GetWheelDelta() const;
Struct::SAIPoint2D & GetPixelPosition( Struct::SAIPoint2D &targetMem = Struct::SAIPoint2D() ) const;
Struct::SAIPoint2D & GetDeltaPosition( Struct::SAIPoint2D &targetMem = Struct::SAIPoint2D() ) const;
void AddOnMousePressCallback( Typedefs::OnMousePressCallback func); void AddOnMousePressCallback( Typedefs::OnMousePressCallback func);
void AddOnMouseDownCallback( Typedefs::OnMouseDownCallback func ); void AddOnMouseDownCallback( Typedefs::OnMouseDownCallback func );
void AddOnMouseReleaseCallback( Typedefs::OnMouseReleaseCallback func ); void AddOnMouseReleaseCallback( Typedefs::OnMouseReleaseCallback func );
@ -106,21 +107,25 @@ namespace Input
protected: protected:
Mouse(); Mouse();
virtual ~Mouse();
protected: protected:
void InternalOnBtnPress(Enum::SAMI key); void InternalOnBtnPress(Enum::SAMI key);
void InternalOnBtnDown(Enum::SAMI key); void InternalOnBtnDown(Enum::SAMI key);
void InternalOnBtnRelease(Enum::SAMI key); void InternalOnBtnRelease(Enum::SAMI key);
void InternalOnMove(Struct::SAIPoint2D cord); void InternalOnMove(Struct::SAIPointInt2D cord);
void InternalOnScroll(int delta); void InternalOnScroll(int delta);
protected: protected:
std::vector<MouseEvent*> mouseSubscribers; std::vector<MouseEvent*> mouseSubscribers;
MouseCallbackList* callbackList; MouseCallbackList* callbackList;
Struct::SAIPoint2D pixelPos, deltaPos; Struct::SAIPointInt2D pixelPos;
Struct::SAIPointFloat2D normalPos;
Struct::SAIPointFloat2D deltaPos;
bool isCurorLocked; bool isCurorLocked;
int wheelDelta; int wheelDelta;
bool active; bool active;
Enum::InputOptionType inputMode;
}; };
} }

View File

@ -57,7 +57,6 @@ namespace Input
std::vector<Win32Mouse*> mouse; std::vector<Win32Mouse*> mouse;
bool enabled; bool enabled;
bool exclusive;
HWND targetHwin; HWND targetHwin;
private: private:

View File

@ -13,14 +13,17 @@ namespace Input
class Win32Keyboard :public Keyboard class Win32Keyboard :public Keyboard
{ {
public: public:
Win32Keyboard(); Win32Keyboard(HWND target);
~Win32Keyboard(); ~Win32Keyboard();
bool IsKeyUp (Enum::SAKI key) override; bool IsKeyUp (Enum::SAKI key) override;
bool IsKeyDown (Enum::SAKI key) override; bool IsKeyDown (Enum::SAKI key) override;
wchar_t* GetAsText(Enum::SAKI key) override; wchar_t* GetAsText(Enum::SAKI key) override;
void SetInputOptionType(Enum::InputOptionType options) override;
bool IsInputOptionSuported(Enum::InputOptionType options) const override;
void ProccessKeyboardData (RAWKEYBOARD keyboard); void ProccessKeyboardData (RAWKEYBOARD keyboard);
bool Create( );
private: private:
void MapKey(RAWKEYBOARD& rawKB, Enum::SAKI& out_key, bool& isE0); void MapKey(RAWKEYBOARD& rawKB, Enum::SAKI& out_key, bool& isE0);
@ -31,7 +34,7 @@ namespace Input
bool isDown; bool isDown;
unsigned int makecode; unsigned int makecode;
}; };
RAWINPUTDEVICE device;
static const int MAXKEYS = 256; static const int MAXKEYS = 256;
Keys keys[MAXKEYS]; Keys keys[MAXKEYS];
}; };

View File

@ -12,13 +12,20 @@ namespace Input
class Win32Mouse :public Mouse class Win32Mouse :public Mouse
{ {
public: public:
Win32Mouse(); Win32Mouse(HWND target);
~Win32Mouse(); ~Win32Mouse();
bool IsBtnUp(Enum::SAMI key) override; bool IsBtnUp(Enum::SAMI key) const override;
bool IsBtnDown(Enum::SAMI key) override; bool IsBtnDown(Enum::SAMI key) const override;
int GetWheelDelta() const override;
Struct::SAIPointInt2D& GetPixelPosition(Struct::SAIPointInt2D &targetMem = Struct::SAIPointInt2D()) const override;
Struct::SAIPointFloat2D& GetNormalizedPosition(Struct::SAIPointFloat2D &targetMem = Struct::SAIPointFloat2D()) const override;
Struct::SAIPointFloat2D& GetDeltaPosition(Struct::SAIPointFloat2D& targetMem = Struct::SAIPointFloat2D()) const override;
void SetInputOptionType(Enum::InputOptionType options) override;
bool IsInputOptionSuported(Enum::InputOptionType options) const override;
void ProccessMouseData (RAWMOUSE mouse); void ProccessMouseData (RAWMOUSE mouse);
bool Create( );
private: private:
struct Buttons struct Buttons
@ -26,8 +33,10 @@ namespace Input
unsigned int makeCode; unsigned int makeCode;
bool isDown; bool isDown;
}; };
static const int MAXBUTTONS = 25; static const int MAXBUTTONS =Enum::SAMI_Unknown;
Buttons buttons[25]; Buttons buttons[MAXBUTTONS];
RAWINPUTDEVICE device;
Struct::SAIPointInt2D windowSize;
}; };
} }

View File

@ -165,7 +165,7 @@ void Mouse::InternalOnBtnRelease(Enum::SAMI btn)
w = w->next; w = w->next;
} }
} }
void Mouse::InternalOnMove(Struct::SAIPoint2D cord) void Mouse::InternalOnMove(Struct::SAIPointInt2D cord)
{ {
for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++) for (unsigned int i = 0; i < this->mouseSubscribers.size(); i++)
{ {
@ -200,23 +200,6 @@ void Mouse::InternalOnScroll(int delta)
int Mouse::GetWheelDelta() const
{
return this->wheelDelta;
}
SAIPoint2D & Mouse::GetPixelPosition( Struct::SAIPoint2D &targetMem ) const
{
targetMem.x = this->pixelPos.x;
targetMem.y = this->pixelPos.y;
return targetMem;
}
SAIPoint2D & Mouse::GetDeltaPosition( Struct::SAIPoint2D &targetMem ) const
{
targetMem.x = this->deltaPos.x;
targetMem.y = this->deltaPos.y;
return targetMem;
}
void Mouse::AddOnMousePressCallback( Typedefs::OnMousePressCallback func) void Mouse::AddOnMousePressCallback( Typedefs::OnMousePressCallback func)
{ {
MouseCallbackList::CallbackData d; MouseCallbackList::CallbackData d;

View File

@ -16,7 +16,6 @@ using namespace Input::Enum;
using namespace Input::Struct; using namespace Input::Struct;
using namespace Input::Typedefs; using namespace Input::Typedefs;
Win32Input *Win32Input::instance = 0; Win32Input *Win32Input::instance = 0;
LRESULT Win32Input::RawInputParser(HWND h, LPARAM l) LRESULT Win32Input::RawInputParser(HWND h, LPARAM l)
@ -89,14 +88,6 @@ LRESULT CALLBACK Win32Input::RawWindowCallback(HWND h, UINT m, WPARAM w, LPARAM
return Win32Input::instance->RawInputParser(h, l); return Win32Input::instance->RawInputParser(h, l);
break; break;
case WM_KEYDOWN:
val = 0;
break;
case WM_CHAR:
val = 0;
break;
case WM_ACTIVATE: case WM_ACTIVATE:
Win32Input::instance->WindowActivate((w == TRUE)); Win32Input::instance->WindowActivate((w == TRUE));
break; break;
@ -115,7 +106,7 @@ void Win32Input::WindowActivate(bool activate)
} }
else else
{ {
ShowCursor(0); ShowCursor(1);
} }
} }
@ -151,55 +142,46 @@ InputObject* Win32Input::CreateDevice(const SAIType inputType, Typedefs::WindowH
{ {
if(!this->instance->targetHwin) if(!this->instance->targetHwin)
{ {
this->targetHwin = CreateWindowExW( 0, L"RawInputCallbackFunc" , NULL, NULL, NULL, NULL, NULL, RECT rc;
NULL, (HWND)targetApplication, NULL, (HINSTANCE)GetModuleHandle(0), NULL ); GetWindowRect((HWND)targetApplication, &rc);
AdjustWindowRect(&rc, GetWindowStyle((HWND)targetApplication), FALSE);
this->targetHwin = CreateWindowExW( 0, L"RawInputCallbackFunc" , NULL, NULL, rc.left, rc.top, rc.right, rc.bottom,
(HWND)targetApplication, NULL, (HINSTANCE)GetModuleHandle(0), NULL );
} }
InputObject* val = 0; InputObject* val = 0;
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x01;
rid.hwndTarget = this->instance->targetHwin;
switch (inputType) switch (inputType)
{ {
case SAIType_Keyboard: case SAIType_Keyboard:
{ {
rid.usUsage = RawInput_Usage_keyboard; Win32Keyboard* obj = new Win32Keyboard(this->targetHwin);
rid.dwFlags = RIDEV_NOLEGACY; if(!obj->Create())
if(RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{
Win32Keyboard* obj = new Win32Keyboard();
this->keyboard.push_back(obj);
val = obj;
}
else
{ {
delete obj;
return 0; return 0;
} }
this->keyboard.push_back(obj);
val = obj;
} }
break; break;
case SAIType_Mouse: case SAIType_Mouse:
{ {
rid.usUsage = RawInput_Usage_mouse; Win32Mouse* obj = new Win32Mouse(this->targetHwin);
rid.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE; if(!obj->Create())
if(RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{ {
int i = 0; delete obj;
val = (InputObject*)1; return 0;
Win32Mouse* obj = new Win32Mouse(); }
this->mouse.push_back(obj); this->mouse.push_back(obj);
val = obj; val = obj;
} }
else
{
return 0;
}
}
break; break;
//case SAIType_ApplicationKeyboard:
// //val = new Win32ApplicationKeyboard();
// break;
} }
return val; return val;

View File

@ -1,7 +1,7 @@
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013] // Created by [Dennis Andersen] [2013]
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
#include "..\..\Include\Win32\Win32Keyboard.h" #include "..\..\Include\Win32\Win32Input.h"
#include <algorithm> #include <algorithm>
#pragma warning ( disable : 4172 ) #pragma warning ( disable : 4172 )
@ -10,8 +10,12 @@ using namespace Input::Enum;
using namespace std; using namespace std;
Win32Keyboard::Win32Keyboard() Win32Keyboard::Win32Keyboard(HWND target)
{ {
this->device.usUsagePage = 0x01;
this->device.hwndTarget = target;
this->device.usUsage = RawInput_Usage_keyboard;
this->device.dwFlags = RIDEV_NOLEGACY;
memset(&this->keys[0], 0, sizeof(Win32Keyboard::Keys) * MAXKEYS); memset(&this->keys[0], 0, sizeof(Win32Keyboard::Keys) * MAXKEYS);
} }
Win32Keyboard::~Win32Keyboard() Win32Keyboard::~Win32Keyboard()
@ -37,6 +41,43 @@ wchar_t* Win32Keyboard::GetAsText(Enum::SAKI key)
GetKeyNameTextW((LONG)temp, buff, 16); GetKeyNameTextW((LONG)temp, buff, 16);
return buff; return buff;
} }
void Win32Keyboard::SetInputOptionType(Enum::InputOptionType options)
{
if(! IsInputOptionSuported(options) ) return;
if(this->inputMode == options) return;
this->inputMode = options;
switch (options)
{
case Input::Enum::InputOptionType_RawInput:
{
if(!Create())
printf("Warning! Could not create the specific device");
}
break;
case Input::Enum::InputOptionType_PlatformDefault:
{
RAWINPUTDEVICE d;
d.usUsagePage = 0x01;
d.hwndTarget = 0;
d.usUsage = RawInput_Usage_keyboard;
d.dwFlags = RIDEV_REMOVE;
RegisterRawInputDevices(&d, 1, sizeof(RAWINPUTDEVICE));
}
break;
}
}
bool Win32Keyboard::IsInputOptionSuported(Enum::InputOptionType options) const
{
switch (options)
{
case Input::Enum::InputOptionType_RawInput: return true;
case Input::Enum::InputOptionType_PlatformDefault: return true;
}
return false;
}
void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard) void Win32Keyboard::ProccessKeyboardData (RAWKEYBOARD keyboard)
{ {
if(!this->active) if(!this->active)
@ -639,6 +680,15 @@ void Win32Keyboard::MapKey(RAWKEYBOARD& rawKB, SAKI& out_key, bool& isE0)
break; break;
} }
} }
bool Win32Keyboard::Create()
{
if(RegisterRawInputDevices(&this->device, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{
return true;
}
return false;
}

View File

@ -1,14 +1,14 @@
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013] // Created by [Dennis Andersen] [2013]
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
#include "..\..\Include\Win32\Win32Mouse.h" #include "..\..\Include\Win32\Win32Input.h"
using namespace Input; using namespace Input;
using namespace Input::Enum; using namespace Input::Enum;
using namespace Input::Struct; using namespace Input::Struct;
using namespace Input::Typedefs; using namespace Input::Typedefs;
void MapButton(RAWMOUSE& rawMouse, bool &isUp, Enum::SAMI& btn, int& delta, Struct::SAIPoint2D& vel, unsigned int& mcode) void MapButton(RAWMOUSE& rawMouse, bool &isUp, Enum::SAMI& btn, int& delta, Struct::SAIPointInt2D& vel, unsigned int& mcode)
{ {
if(rawMouse.lLastX != 0 || rawMouse.lLastY != 0) if(rawMouse.lLastX != 0 || rawMouse.lLastY != 0)
{ {
@ -63,41 +63,119 @@ void MapButton(RAWMOUSE& rawMouse, bool &isUp, Enum::SAMI& btn, int& delta, Stru
} }
Win32Mouse::Win32Mouse() Win32Mouse::Win32Mouse(HWND target)
{ {
this->device.usUsagePage = 0x01;
this->device.hwndTarget = target;
this->device.usUsage = RawInput_Usage_mouse;
this->device.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE;
memset(&this->buttons[0], 0, sizeof(Buttons) * MAXBUTTONS); memset(&this->buttons[0], 0, sizeof(Buttons) * MAXBUTTONS);
} }
Win32Mouse::~Win32Mouse() Win32Mouse::~Win32Mouse()
{ { }
}
bool Win32Mouse::IsBtnUp(Enum::SAMI btn) bool Win32Mouse::IsBtnUp(Enum::SAMI btn) const
{ {
if(btn == SAMI_Unknown) return false; if(btn == SAMI_Unknown) return false;
return !this->buttons[btn].isDown; return !this->buttons[btn].isDown;
} }
bool Win32Mouse::IsBtnDown(Enum::SAMI btn) bool Win32Mouse::IsBtnDown(Enum::SAMI btn) const
{ {
if(btn == SAMI_Unknown) return false; if(btn == SAMI_Unknown) return false;
return this->buttons[btn].isDown; return this->buttons[btn].isDown;
} }
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) const
{
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::SetInputOptionType(Enum::InputOptionType options)
{
if(! IsInputOptionSuported(options) ) return;
this->inputMode = options;
switch (options)
{
case Input::Enum::InputOptionType_RawInput:
{
if(!Create())
printf("Warning! Could not create the specific device");
}
break;
case Input::Enum::InputOptionType_PlatformDefault:
{
RAWINPUTDEVICE d;
d.usUsagePage = 0x01;
d.hwndTarget = 0;
d.usUsage = RawInput_Usage_mouse;
d.dwFlags = RIDEV_REMOVE;
RegisterRawInputDevices(&d, 1, sizeof(RAWINPUTDEVICE));
}
break;
}
}
bool Win32Mouse::IsInputOptionSuported(Enum::InputOptionType options) const
{
switch (options)
{
case Input::Enum::InputOptionType_RawInput: return true;
case Input::Enum::InputOptionType_PlatformDefault: return true;
}
return false;
}
void Win32Mouse::ProccessMouseData (RAWMOUSE mouse) void Win32Mouse::ProccessMouseData (RAWMOUSE mouse)
{ {
bool isUp = true; bool isUp = true;
Enum::SAMI btn = Enum::SAMI_Unknown; Enum::SAMI btn = Enum::SAMI_Unknown;
int delta = 0; int delta = 0;
Struct::SAIPoint2D velocity; Struct::SAIPointInt2D velocity;
unsigned int makeCode = 0; unsigned int makeCode = 0;
MapButton(mouse, isUp, btn, delta, velocity, makeCode); MapButton(mouse, isUp, btn, delta, velocity, makeCode);
if(velocity.Length() != 0) if(velocity.Length() != 0)
{ {
this->pixelPos.x += this->deltaPos.x = velocity.x; this->pixelPos.x += velocity.x;
this->pixelPos.y += this->deltaPos.y = velocity.y; this->pixelPos.y += velocity.y;
//Contain
if(this->pixelPos.x < 0) { this->pixelPos.x = 0; }
else if(this->pixelPos.x > this->windowSize.x) { this->pixelPos.x = this->windowSize.x; }
if(this->pixelPos.y < 0) { this->pixelPos.y = 0; }
else if(this->pixelPos.y > this->windowSize.y) { this->pixelPos.y = this->windowSize.y; }
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);
InternalOnMove(this->pixelPos); InternalOnMove(this->pixelPos);
} }
@ -131,3 +209,31 @@ void Win32Mouse::ProccessMouseData (RAWMOUSE mouse)
} }
} }
} }
bool Win32Mouse::Create()
{
if(!this->device.hwndTarget)
{
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
windowSize.x = desktop.right;
windowSize.y = desktop.bottom;
this->device.dwFlags = 0;
}
else
{
RECT re;
GetWindowRect(this->device.hwndTarget, &re);
windowSize.x = re.right;
windowSize.y = re.bottom;
}
if(RegisterRawInputDevices(&this->device, 1, sizeof(RAWINPUTDEVICE)) == TRUE)
{
return true;
}
return false;
}