From 53dc42ffc4f445dcab03417e1272e81fd8d0f7a1 Mon Sep 17 00:00:00 2001 From: dean11 Date: Thu, 20 Feb 2014 11:27:43 +0100 Subject: [PATCH] Input - Added normalized cordinates --- Code/Game/GameClient/GameClient.vcxproj.user | 2 +- Code/Misc/Input/Include/Common.h | 14 ++++-- Code/Misc/Input/Include/Mouse.h | 16 +++--- Code/Misc/Input/Include/Win32/Win32Mouse.h | 8 ++- Code/Misc/Input/Source/Mouse.cpp | 3 ++ Code/Misc/Input/Source/Win32/Win32Input.cpp | 21 +++----- Code/Misc/Input/Source/Win32/Win32Mouse.cpp | 51 ++++++++++++++++++-- 7 files changed, 85 insertions(+), 30 deletions(-) diff --git a/Code/Game/GameClient/GameClient.vcxproj.user b/Code/Game/GameClient/GameClient.vcxproj.user index 4b847ee6..2e28d6f7 100644 --- a/Code/Game/GameClient/GameClient.vcxproj.user +++ b/Code/Game/GameClient/GameClient.vcxproj.user @@ -1,7 +1,7 @@  - false + true $(OutDir) diff --git a/Code/Misc/Input/Include/Common.h b/Code/Misc/Input/Include/Common.h index 18999b41..9bb9f2cf 100644 --- a/Code/Misc/Input/Include/Common.h +++ b/Code/Misc/Input/Include/Common.h @@ -36,14 +36,22 @@ namespace Input /*********************************************************************/ namespace Struct { - struct SAIPoint2D + struct SAIPointInt2D { int x; int y; - SAIPoint2D() :x(0), y(0) { } - SAIPoint2D(int _x, int _y) :x(_x), y(_y) { } + SAIPointInt2D() :x(0), y(0) { } + SAIPointInt2D(int _x, int _y) :x(_x), y(_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; } diff --git a/Code/Misc/Input/Include/Mouse.h b/Code/Misc/Input/Include/Mouse.h index b657604b..6c58ec97 100644 --- a/Code/Misc/Input/Include/Mouse.h +++ b/Code/Misc/Input/Include/Mouse.h @@ -47,7 +47,7 @@ namespace Input typedef void(*OnMousePressCallback)(Enum::SAMI btn, Mouse* sender); typedef void(*OnMouseDownCallback)(Enum::SAMI btn, Mouse* sender); typedef void(*OnMouseReleaseCallback)(Enum::SAMI btn, Mouse* sender); - typedef void(*OnMouseMoveCallback)(Struct::SAIPoint2D, Mouse* sender); + typedef void(*OnMouseMoveCallback)(Struct::SAIPointInt2D, Mouse* sender); typedef void(*OnMouseScrollCallback)(int delta, Mouse* sender); } //----------------------------------------------------------------------------------------------------------------------------- @@ -61,15 +61,16 @@ namespace Input virtual void OnMousePress ( Enum::SAMI key, Mouse* sender ) { } virtual void OnMouseDown ( 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 ) { } }; public: - virtual bool IsBtnUp(Enum::SAMI key) = 0; - virtual bool IsBtnDown(Enum::SAMI key) = 0; - virtual int GetWheelDelta() = 0; - virtual Struct::SAIPoint2D GetPixelPosition(Struct::SAIPoint2D targetMem = Struct::SAIPoint2D()) = 0; + virtual bool IsBtnUp(Enum::SAMI key) = 0; + virtual bool IsBtnDown(Enum::SAMI key) = 0; + virtual int GetWheelDelta() = 0; + virtual Struct::SAIPointInt2D GetPixelPosition(Struct::SAIPointInt2D targetMem = Struct::SAIPointInt2D()) = 0; + virtual Struct::SAIPointFloat2D GetNormalizedPosition(Struct::SAIPointFloat2D targetMem = Struct::SAIPointFloat2D()) = 0; public: void AddOnMousePressCallback( Typedefs::OnMousePressCallback func); @@ -135,7 +136,8 @@ namespace Input protected: std::vector mouseSubscribers; MouseCallbackList* callbackList; - Struct::SAIPoint2D pixelPos; + Struct::SAIPointInt2D pixelPos; + Struct::SAIPointFloat2D normalPos; bool isCurorLocked; int wheelDelta; }; diff --git a/Code/Misc/Input/Include/Win32/Win32Mouse.h b/Code/Misc/Input/Include/Win32/Win32Mouse.h index 10c04a28..278a76a4 100644 --- a/Code/Misc/Input/Include/Win32/Win32Mouse.h +++ b/Code/Misc/Input/Include/Win32/Win32Mouse.h @@ -18,9 +18,12 @@ namespace Input bool IsBtnUp(Enum::SAMI key) override; bool IsBtnDown(Enum::SAMI key) override; int GetWheelDelta() override; - Struct::SAIPoint2D GetPixelPosition(Struct::SAIPoint2D targetMem = Struct::SAIPoint2D()) override; + Struct::SAIPointInt2D GetPixelPosition(Struct::SAIPointInt2D targetMem = Struct::SAIPointInt2D()) override; + Struct::SAIPointFloat2D GetNormalizedPosition(Struct::SAIPointFloat2D targetMem = Struct::SAIPointFloat2D()) override; - void ProccessMouseData (bool isDown, Enum::SAMI btn, int delta, Struct::SAIPoint2D velocity, unsigned int makeCode); + void ProccessMouseData (bool isDown, Enum::SAMI btn, int delta, Struct::SAIPointInt2D velocity, unsigned int makeCode); + + bool Create(HWND target); private: struct Buttons @@ -30,6 +33,7 @@ namespace Input }; static const int MAXBUTTONS = 25; Buttons buttons[25]; + RAWINPUTDEVICE device; }; } diff --git a/Code/Misc/Input/Source/Mouse.cpp b/Code/Misc/Input/Source/Mouse.cpp index 36bd8330..de1cf26f 100644 --- a/Code/Misc/Input/Source/Mouse.cpp +++ b/Code/Misc/Input/Source/Mouse.cpp @@ -81,6 +81,7 @@ Mouse::Mouse() , wheelDelta(0) , isCurorLocked(0) , pixelPos() + , normalPos() { } Mouse::~Mouse() @@ -149,6 +150,8 @@ void Mouse::SetPixelPos(int x, int y) { this->pixelPos.x = x; this->pixelPos.y = y; + + // TODO: Update normalized position } void Mouse::ToggleCursor(bool toggler) { diff --git a/Code/Misc/Input/Source/Win32/Win32Input.cpp b/Code/Misc/Input/Source/Win32/Win32Input.cpp index c6be3c9d..edf85729 100644 --- a/Code/Misc/Input/Source/Win32/Win32Input.cpp +++ b/Code/Misc/Input/Source/Win32/Win32Input.cpp @@ -556,7 +556,7 @@ void MapKey(RAWKEYBOARD& rawKB, bool& out_isUp, SAKI& out_key, unsigned int& sCo rawKB.MakeCode = scanCode; sCode = scanCode; } -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) { @@ -647,7 +647,7 @@ void Win32Input::RawInputParser(HWND h, LPARAM l) bool isUp = true; Enum::SAMI btn = Enum::SAMI_Unknown; int delta = 0; - Struct::SAIPoint2D vel; + Struct::SAIPointInt2D vel; unsigned int mcode = 0; MapButton(raw->data.mouse, isUp, btn, delta, vel, mcode); @@ -759,20 +759,15 @@ InputObject* Win32Input::CreateDevice(const SAIType inputType, Typedefs::WindowH case SAIType_Mouse: { - rid.usUsage = RawInput_Usage_mouse; - rid.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE; - if(RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)) == TRUE) - { - int i = 0; - val = (InputObject*)1; - Win32Mouse* obj = new Win32Mouse(); - this->mouse.push_back(obj); - val = obj; - } - else + Win32Mouse* obj = new Win32Mouse(); + if(!obj->Create(this->targetHwin)) { + delete obj; return 0; } + + this->mouse.push_back(obj); + val = obj; } break; } diff --git a/Code/Misc/Input/Source/Win32/Win32Mouse.cpp b/Code/Misc/Input/Source/Win32/Win32Mouse.cpp index ef6d9edf..50d55a91 100644 --- a/Code/Misc/Input/Source/Win32/Win32Mouse.cpp +++ b/Code/Misc/Input/Source/Win32/Win32Mouse.cpp @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// -#include "..\..\Include\Win32\Win32Mouse.h" +#include "..\..\Include\Win32\Win32Input.h" using namespace Input; using namespace Input::Enum; @@ -11,6 +11,7 @@ using namespace Input::Typedefs; Win32Mouse::Win32Mouse() { + memset(&this->device, 0, sizeof(RAWINPUTDEVICE)); memset(&this->buttons[0], 0, sizeof(Buttons) * MAXBUTTONS); } Win32Mouse::~Win32Mouse() @@ -34,13 +35,29 @@ int Win32Mouse::GetWheelDelta() { return this->wheelDelta; } -Struct::SAIPoint2D Win32Mouse::GetPixelPosition(Struct::SAIPoint2D targetMem) +Struct::SAIPointInt2D Win32Mouse::GetPixelPosition(Struct::SAIPointInt2D targetMem) { targetMem = this->pixelPos; return targetMem; } +Struct::SAIPointFloat2D Win32Mouse::GetNormalizedPosition(Struct::SAIPointFloat2D targetMem) +{ + POINT mousePos; + GetCursorPos( &mousePos ); + + RECT windowVertex; + GetWindowRect( this->device.hwndTarget, &windowVertex ); + + this->normalPos.x = (float)(mousePos.x - windowVertex.left); + this->normalPos.x /= (float)(windowVertex.right - windowVertex.left); + + this->normalPos.y = (float)(mousePos.y - windowVertex.top); + this->normalPos.y /= (float)(windowVertex.bottom - windowVertex.top); -void Win32Mouse::ProccessMouseData (bool isUp, Enum::SAMI btn, int delta, Struct::SAIPoint2D velocity, unsigned int makeCode) + return targetMem; +} + +void Win32Mouse::ProccessMouseData (bool isUp, Enum::SAMI btn, int delta, Struct::SAIPointInt2D velocity, unsigned int makeCode) { if(velocity.Length() != 0) { @@ -138,4 +155,30 @@ void Win32Mouse::ProccessMouseData (bool isUp, Enum::SAMI btn, int delta, Struct } } } -} \ No newline at end of file + + +} + +bool Win32Mouse::Create(HWND target) +{ + this->device.usUsagePage = 0x01; + this->device.hwndTarget = target; + this->device.usUsage = Input::RawInput_Usage_mouse; + this->device.dwFlags = RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE; + + if(RegisterRawInputDevices(&this->device, 1, sizeof(RAWINPUTDEVICE)) == TRUE) + { + return true; + } + + memset(&this->device, 0, sizeof(RAWINPUTDEVICE)); + + return false; +} + + + + + + +