Refactored InputProxy to gracefully handle cases where multiple origins send the same command
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "../GLM.h"
|
||||
#include "../Core/InputController.h"
|
||||
#include "../Core/ELockMouse.h"
|
||||
|
||||
template <typename EventContext>
|
||||
class FirstPersonInputController : public InputController<EventContext>
|
||||
@@ -11,9 +12,26 @@ public:
|
||||
FirstPersonInputController(EventBroker* eventBroker, unsigned int playerID)
|
||||
: InputController(eventBroker)
|
||||
, m_PlayerID(playerID)
|
||||
{ }
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &FirstPersonInputController::OnLockMouse);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &FirstPersonInputController::OnUnlockMouse);
|
||||
}
|
||||
|
||||
const glm::quat Orientation() const { return m_Orientation; }
|
||||
|
||||
void LockMouse()
|
||||
{
|
||||
Events::LockMouse e;
|
||||
m_EventBroker->Publish(e);
|
||||
m_MouseLocked = true;
|
||||
}
|
||||
|
||||
void UnlockMouse()
|
||||
{
|
||||
Events::UnlockMouse e;
|
||||
m_EventBroker->Publish(e);
|
||||
m_MouseLocked = false;
|
||||
}
|
||||
|
||||
virtual bool OnCommand(const Events::InputCommand& e) override
|
||||
{
|
||||
@@ -21,20 +39,32 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (e.Command == "Pitch") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation = m_Orientation * glm::angleAxis<float>(-val, glm::vec3(1, 0, 0));
|
||||
if (m_MouseLocked) {
|
||||
if (e.Command == "Pitch") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation = m_Orientation * glm::angleAxis<float>(-val, glm::vec3(1, 0, 0));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (e.Command == "Yaw") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation = glm::angleAxis<float>(-val, glm::vec3(0, 1, 0)) * m_Orientation;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.Command == "Yaw") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation = glm::angleAxis<float>(-val, glm::vec3(0, 1, 0)) * m_Orientation;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
const unsigned int m_PlayerID;
|
||||
glm::quat m_Orientation;
|
||||
bool m_MouseLocked = false;
|
||||
|
||||
EventRelay<EventContext, Events::LockMouse> m_ELockMouse;
|
||||
bool OnLockMouse(const Events::LockMouse& e) { m_MouseLocked = true; return true; }
|
||||
EventRelay<EventContext, Events::UnlockMouse> m_EUnlockMouse;
|
||||
bool OnUnlockMouse(const Events::UnlockMouse& e) { m_MouseLocked = false; return true; }
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -13,8 +13,9 @@ public:
|
||||
, m_InputProxy(inputProxy)
|
||||
{ }
|
||||
|
||||
virtual void Update(double dt) { }
|
||||
virtual bool BindOrigin(std::string origin, std::string command, float value) = 0;
|
||||
virtual void Update(double dt) { }
|
||||
virtual float GetCommandValue(std::string command) = 0;
|
||||
|
||||
protected:
|
||||
EventBroker* m_EventBroker;
|
||||
|
||||
@@ -20,13 +20,17 @@ public:
|
||||
void Process();
|
||||
template <typename T>
|
||||
void AddHandler();
|
||||
void Publish(const Events::InputCommand& e);
|
||||
|
||||
protected:
|
||||
EventBroker* m_EventBroker;
|
||||
std::vector<InputHandler*> m_Handlers;
|
||||
std::map<std::string, std::set<InputHandler*>> m_CommandHandlers;
|
||||
|
||||
// Represents every unique command (has of PlayerID & Command) and all values reported for that command this frame
|
||||
std::map<std::pair<unsigned int, std::string>, std::vector<float>> m_CommandQueue;
|
||||
//std::map<std::pair<unsigned int, std::string>, std::vector<float>> m_CommandQueue;
|
||||
|
||||
std::map<std::string, float> m_CurrentCommandValues;
|
||||
std::map<std::string, float> m_LastCommandValues;
|
||||
|
||||
EventRelay<InputProxy, Events::BindOrigin> m_EBindOrigin;
|
||||
bool OnBindOrigin(const Events::BindOrigin& e);
|
||||
|
||||
@@ -12,10 +12,12 @@ public:
|
||||
KeyboardInputHandler(EventBroker* eventBroker, InputProxy* inputProxy);
|
||||
|
||||
bool BindOrigin(std::string origin, std::string command, float value) override;
|
||||
virtual float GetCommandValue(std::string command) override;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, int> m_OriginKeyCodes;
|
||||
std::unordered_map<int, std::tuple<std::string, float>> m_KeyBindings; // GLFW_KEY... -> command string & value
|
||||
std::unordered_map<std::string, float> m_CommandValues;
|
||||
|
||||
EventRelay<InputHandler, Events::KeyDown> m_EKeyDown;
|
||||
bool OnKeyDown(const Events::KeyDown& e);
|
||||
|
||||
Reference in New Issue
Block a user