From 74d28d1f621532f99654cec82f72399570d234cf Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Wed, 12 Feb 2014 15:44:27 +0100 Subject: [PATCH] Misc - Changed to MouseInput struct in EventHandler update. --- Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 13 ++++++++++++- .../GameClientState/Buttons/ButtonEllipse.h | 9 ++------- .../GameClientState/Buttons/ButtonRectangle.h | 10 ++-------- Code/Misc/EventHandler/EventButton.h | 10 ++++++---- Code/Misc/EventHandler/EventButtonCollection.cpp | 4 ++-- Code/Misc/EventHandler/EventButtonCollection.h | 2 +- Code/Misc/EventHandler/EventHandler.cpp | 4 ++-- Code/Misc/EventHandler/EventHandler.h | 2 +- Code/Misc/EventHandler/IEventButton.h | 11 ++++++++++- 9 files changed, 38 insertions(+), 27 deletions(-) diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 0b33e389..c1a893ee 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -142,8 +142,19 @@ namespace DanBias HRESULT DanBiasGame::Update(float deltaTime) { + //Get mouse pos and window size (Temporary) + POINT p; + RECT r; + GetCursorPos(&p); + ScreenToClient(WindowShell::GetHWND(), &p); + GetClientRect(WindowShell::GetHWND(), &r); + //Update menu buttons - EventHandler::Instance().Update(m_data->inputObj); + MouseInput mouseInput; + mouseInput.x = (float)p.x / (float)r.right; + mouseInput.y = (float)p.y / (float)r.bottom; + mouseInput.mouseButtonPressed = m_data->inputObj->IsMousePressed(); + EventHandler::Instance().Update(mouseInput); m_data->inputObj->Update(); diff --git a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h index 4785d630..5570fb2f 100644 --- a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h +++ b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h @@ -37,15 +37,10 @@ namespace DanBias {} //Circle vs point collision - bool Collision(InputClass* inputObject) + bool Collision(Oyster::Event::MouseInput& input) { - POINT p; - RECT r; - GetCursorPos(&p); - ScreenToClient(WindowShell::GetHWND(), &p); - GetClientRect(WindowShell::GetHWND(), &r); //Should come from the InputClass - float xMouse = (float)p.x / (float)r.right, yMouse = (float)p.y / (float)r.bottom; + float xMouse = input.x, yMouse = input.y; double normx = (xMouse - xPos) / width; double normy = (yMouse - yPos) / height; diff --git a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h index a1aac005..711afdf8 100644 --- a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h +++ b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h @@ -37,16 +37,10 @@ namespace DanBias {} //Circle vs point collision - bool Collision(InputClass* inputObject) + bool Collision(Oyster::Event::MouseInput& input) { - POINT p; - RECT r; - GetCursorPos(&p); - ScreenToClient(WindowShell::GetHWND(), &p); - GetClientRect(WindowShell::GetHWND(), &r); - //Should come from the InputClass - float xMouse = (float)p.x / (float)r.right, yMouse = (float)p.y / (float)r.bottom; + float xMouse = input.x, yMouse = input.y; float widthTemp = xPos - width * 0.5f; float widthTemp2 = xPos + width * 0.5f; diff --git a/Code/Misc/EventHandler/EventButton.h b/Code/Misc/EventHandler/EventButton.h index b891889d..0f197ffc 100644 --- a/Code/Misc/EventHandler/EventButton.h +++ b/Code/Misc/EventHandler/EventButton.h @@ -46,7 +46,7 @@ namespace Oyster private: //Implement this in the inherited classes for collision against that shape. - virtual bool Collision(InputClass *input) = 0; + virtual bool Collision(MouseInput& input) = 0; public: EventButton(); @@ -56,7 +56,7 @@ namespace Oyster EventButton(EventFunc func, Owner owner, void* userData); virtual ~EventButton(); - void Update(InputClass *input); + void Update(MouseInput& input); //Send event to callback function void SendEvent(ButtonState state); @@ -135,16 +135,18 @@ namespace Oyster //Checks for collision and template - void EventButton::Update(InputClass *input) + void EventButton::Update(MouseInput& input) { if(this->privData.enabled) { ButtonState currentState = ButtonState_None; static bool outside = false; static bool clicked = false; + + //Check for collision against the button. if(Collision(input)) { - if(input->IsMousePressed()) + if(input.mouseButtonPressed) { //Change state when the mouse button is pressed switch(this->privData.previousState) diff --git a/Code/Misc/EventHandler/EventButtonCollection.cpp b/Code/Misc/EventHandler/EventButtonCollection.cpp index 5c1438c3..9e52805e 100644 --- a/Code/Misc/EventHandler/EventButtonCollection.cpp +++ b/Code/Misc/EventHandler/EventButtonCollection.cpp @@ -32,13 +32,13 @@ EventButtonCollection::~EventButtonCollection() } } -void EventButtonCollection::Update(InputClass* inputObject) +void EventButtonCollection::Update(MouseInput& input) { if(this->collectionState == EventCollectionState_Enabled) { for(int i = 0; i < (int)buttons.size(); i++) { - buttons[i]->Update(inputObject); + buttons[i]->Update(input); } } } diff --git a/Code/Misc/EventHandler/EventButtonCollection.h b/Code/Misc/EventHandler/EventButtonCollection.h index 5bcd044a..3cb3c891 100644 --- a/Code/Misc/EventHandler/EventButtonCollection.h +++ b/Code/Misc/EventHandler/EventButtonCollection.h @@ -36,7 +36,7 @@ namespace Oyster EventButtonCollection(EventCollectionState state = EventCollectionState_Enabled); ~EventButtonCollection(); - void Update(InputClass* inputObject); + void Update(MouseInput& input); void Render(); /*Add a button to the collection when a button is added to the collection you are not allowed to delete it. diff --git a/Code/Misc/EventHandler/EventHandler.cpp b/Code/Misc/EventHandler/EventHandler.cpp index 234a3935..558b776c 100644 --- a/Code/Misc/EventHandler/EventHandler.cpp +++ b/Code/Misc/EventHandler/EventHandler.cpp @@ -27,11 +27,11 @@ void EventHandler::Clean() collections.clear(); } -void EventHandler::Update(InputClass* inputObject) +void EventHandler::Update(MouseInput& input) { for(int i = 0; i < (int)collections.size(); i++) { - collections.at(i)->Update(inputObject); + collections.at(i)->Update(input); } } diff --git a/Code/Misc/EventHandler/EventHandler.h b/Code/Misc/EventHandler/EventHandler.h index a2560f0f..79196afb 100644 --- a/Code/Misc/EventHandler/EventHandler.h +++ b/Code/Misc/EventHandler/EventHandler.h @@ -26,7 +26,7 @@ namespace Oyster void Clean(); - void Update(InputClass* inputObject); + void Update(MouseInput& input); void Render(); /*Add a collection to the EventHandler will only add collections not already present in the list. diff --git a/Code/Misc/EventHandler/IEventButton.h b/Code/Misc/EventHandler/IEventButton.h index 13e6f21b..62044eb2 100644 --- a/Code/Misc/EventHandler/IEventButton.h +++ b/Code/Misc/EventHandler/IEventButton.h @@ -19,6 +19,14 @@ namespace Oyster ButtonState_Down, ButtonState_Released, }; + + //Takes normalized device coordinates + struct MouseInput + { + //Normalized device coordinates + float x, y; + bool mouseButtonPressed; + }; class IEventButton { @@ -26,7 +34,8 @@ namespace Oyster virtual ~IEventButton(){} virtual void Render() = 0; - virtual void Update(InputClass *input) = 0; + + virtual void Update(MouseInput& input) = 0; virtual void SendEvent(ButtonState state) = 0;