Added MouseInputHandler and firstPersonInputController to aid in creation of first person movement.
This commit is contained in:
@@ -11,25 +11,22 @@ template <typename EventContext>
|
||||
class InputController
|
||||
{
|
||||
public:
|
||||
InputController(std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
: EventBroker(eventBroker)
|
||||
InputController(EventBroker* eventBroker)
|
||||
: m_EventBroker(eventBroker)
|
||||
{ Initialize(); }
|
||||
|
||||
virtual void Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove);
|
||||
}
|
||||
|
||||
virtual bool OnCommand(const Events::InputCommand &event) { return false; }
|
||||
virtual bool OnMouseMove(const Events::MouseMove &event) { return false; }
|
||||
virtual bool OnCommand(const Events::InputCommand& e) { return false; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<dd::EventBroker> EventBroker;
|
||||
EventBroker* m_EventBroker;
|
||||
|
||||
private:
|
||||
EventRelay<EventContext, Events::InputCommand> m_EInputCommand;
|
||||
EventRelay<EventContext, Events::MouseMove> m_EMouseMove;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef FirstPersonInputController_h__
|
||||
#define FirstPersonInputController_h__
|
||||
|
||||
#include "../GLM.h"
|
||||
#include "../Core/InputController.h"
|
||||
|
||||
template <typename EventContext>
|
||||
class FirstPersonInputController : public InputController<EventContext>
|
||||
{
|
||||
public:
|
||||
FirstPersonInputController(EventBroker* eventBroker, unsigned int playerID)
|
||||
: InputController(eventBroker)
|
||||
, m_PlayerID(playerID)
|
||||
{ }
|
||||
|
||||
const glm::quat Orientation() const { return m_Orientation; }
|
||||
|
||||
virtual bool OnCommand(const Events::InputCommand& e) override
|
||||
{
|
||||
if (m_PlayerID != e.PlayerID) {
|
||||
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 (e.Command == "Yaw") {
|
||||
float val = glm::radians(e.Value);
|
||||
m_Orientation = glm::angleAxis<float>(-val, glm::vec3(0, 1, 0)) * m_Orientation;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const unsigned int m_PlayerID;
|
||||
glm::quat m_Orientation;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef MouseInputHandler_h__
|
||||
#define MouseInputHandler_h__
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "InputHandler.h"
|
||||
#include "Core/EMousePress.h"
|
||||
#include "Core/EMouseRelease.h"
|
||||
#include "Core/EMouseMove.h"
|
||||
|
||||
class MouseInputHandler : public InputHandler
|
||||
{
|
||||
public:
|
||||
MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy);
|
||||
|
||||
bool BindOrigin(std::string origin, std::string command, float value) override;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, int> m_OriginCodes;
|
||||
std::unordered_map<std::string, char> m_OriginAxes;
|
||||
std::unordered_map<int, std::tuple<std::string, float>> m_Bindings; // GLFW_MOUSE_BUTTON... -> command string & value
|
||||
std::unordered_map<char, std::tuple<std::string, float>> m_Axes; // Axis -> command string & value
|
||||
|
||||
EventRelay<InputHandler, Events::MousePress> m_EMousePress;
|
||||
bool OnMousePress(const Events::MousePress& e);
|
||||
EventRelay<InputHandler, Events::MouseRelease> m_EMouseRelease;
|
||||
bool OnMouseRelease(const Events::MouseRelease& e);
|
||||
EventRelay<InputHandler, Events::MouseMove> m_EMouseMove;
|
||||
bool OnMouseMove(const Events::MouseMove& e);
|
||||
|
||||
bool hasOrigin(std::string origin);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "Rendering/RenderQueueFactory.h"
|
||||
#include "Input/InputProxy.h"
|
||||
#include "Input/KeyboardInputHandler.h"
|
||||
#include "Input/MouseInputHandler.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EntityXMLFile.h"
|
||||
#include "Core/SystemPipeline.h"
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
[Mouse]
|
||||
Sensitivity=0.5
|
||||
InvertPitch=false
|
||||
|
||||
[Bindings]
|
||||
W=Forward
|
||||
A=Left
|
||||
|
||||
@@ -46,7 +46,7 @@ void InputProxy::Process()
|
||||
for (auto& value : pair.second) {
|
||||
e.Value += value;
|
||||
}
|
||||
e.Value = std::max(-1.f, std::min(e.Value, 1.f));
|
||||
//e.Value = std::max(-1.f, std::min(e.Value, 1.f));
|
||||
m_EventBroker->Publish(e);
|
||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
#include "Input/MouseInputHandler.h"
|
||||
|
||||
MouseInputHandler::MouseInputHandler(EventBroker* eventBroker, InputProxy* inputProxy)
|
||||
: InputHandler(eventBroker, inputProxy)
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &MouseInputHandler::OnMousePress);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &MouseInputHandler::OnMouseRelease);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &MouseInputHandler::OnMouseMove);
|
||||
|
||||
m_OriginCodes["Mouse1"] = GLFW_MOUSE_BUTTON_1;
|
||||
m_OriginCodes["MouseLeft"] = GLFW_MOUSE_BUTTON_LEFT;
|
||||
m_OriginCodes["Mouse2"] = GLFW_MOUSE_BUTTON_2;
|
||||
m_OriginCodes["MouseRight"] = GLFW_MOUSE_BUTTON_RIGHT;
|
||||
m_OriginCodes["Mouse3"] = GLFW_MOUSE_BUTTON_3;
|
||||
m_OriginCodes["MouseMiddle"] = GLFW_MOUSE_BUTTON_MIDDLE;
|
||||
m_OriginCodes["Mouse4"] = GLFW_MOUSE_BUTTON_4;
|
||||
m_OriginCodes["Mouse5"] = GLFW_MOUSE_BUTTON_5;
|
||||
m_OriginCodes["Mouse6"] = GLFW_MOUSE_BUTTON_6;
|
||||
m_OriginCodes["Mouse7"] = GLFW_MOUSE_BUTTON_7;
|
||||
m_OriginCodes["Mouse8"] = GLFW_MOUSE_BUTTON_8;
|
||||
|
||||
m_OriginAxes["MouseX"] = 'X';
|
||||
m_OriginAxes["MouseY"] = 'Y';
|
||||
}
|
||||
|
||||
bool MouseInputHandler::BindOrigin(std::string origin, std::string command, float value)
|
||||
{
|
||||
auto originCode = m_OriginCodes.find(origin);
|
||||
if (originCode != m_OriginCodes.end()) {
|
||||
int code = originCode->second;
|
||||
m_Bindings[code] = std::make_tuple(command, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto originAxis = m_OriginAxes.find(origin);
|
||||
if (originAxis != m_OriginAxes.end()) {
|
||||
char axis = originAxis->second;
|
||||
float multiplier = 1.f;
|
||||
// Sensitivity
|
||||
multiplier *= ResourceManager::Load<ConfigFile>("Input.ini")->Get<float>("Mouse.Sensitivity", 1.f);
|
||||
if (axis == 'Y') {
|
||||
if (ResourceManager::Load<ConfigFile>("Input.ini")->Get<bool>("Mouse.InvertPitch", false)) {
|
||||
multiplier *= -1.f;
|
||||
}
|
||||
}
|
||||
m_Axes[axis] = std::make_tuple(command, value * multiplier);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MouseInputHandler::OnMousePress(const Events::MousePress& e)
|
||||
{
|
||||
auto it = m_Bindings.find(e.Button);
|
||||
if (it == m_Bindings.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
std::tie(ic.Command, ic.Value) = it->second;
|
||||
m_InputProxy->Publish(ic);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MouseInputHandler::OnMouseRelease(const Events::MouseRelease& e)
|
||||
{
|
||||
auto it = m_Bindings.find(e.Button);
|
||||
if (it == m_Bindings.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
std::tie(ic.Command, std::ignore) = it->second;
|
||||
ic.Value = 0;
|
||||
m_InputProxy->Publish(ic);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e)
|
||||
{
|
||||
if (std::abs(e.DeltaX) > 0) {
|
||||
auto it = m_Axes.find('X');
|
||||
if (it != m_Axes.end()) {
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
std::tie(ic.Command, ic.Value) = it->second;
|
||||
ic.Value *= e.DeltaX;
|
||||
m_InputProxy->Publish(ic);
|
||||
}
|
||||
}
|
||||
|
||||
if (std::abs(e.DeltaY) > 0) {
|
||||
auto it = m_Axes.find('Y');
|
||||
if (it != m_Axes.end()) {
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
std::tie(ic.Command, ic.Value) = it->second;
|
||||
ic.Value *= e.DeltaY;
|
||||
m_InputProxy->Publish(ic);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MouseInputHandler::hasOrigin(std::string origin)
|
||||
{
|
||||
if (m_OriginCodes.find(origin) == m_OriginCodes.end()) {
|
||||
return false;
|
||||
}
|
||||
if (m_OriginAxes.find(origin) == m_OriginAxes.end()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ Game::Game(int argc, char* argv[])
|
||||
m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker);
|
||||
m_InputProxy = new InputProxy(m_EventBroker);
|
||||
m_InputProxy->AddHandler<KeyboardInputHandler>();
|
||||
m_InputProxy->AddHandler<MouseInputHandler>();
|
||||
m_InputProxy->LoadBindings("Input.ini");
|
||||
|
||||
// Create the root level GUI frame
|
||||
|
||||
Reference in New Issue
Block a user