MouseInputHandler
This commit is contained in:
@@ -66,6 +66,27 @@ void InputProxy::Process()
|
||||
m_LastCommandValues[command] = currentValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Accumulate the input values of all unique commands published by input handlers
|
||||
for (auto& pair : m_CommandQueue) {
|
||||
Events::InputCommand e;
|
||||
e.PlayerID = pair.first.first;
|
||||
e.Command = pair.first.second;
|
||||
e.Value = 0;
|
||||
for (auto& value : pair.second) {
|
||||
e.Value += value;
|
||||
}
|
||||
//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);
|
||||
}
|
||||
m_CommandQueue.clear();
|
||||
}
|
||||
|
||||
void InputProxy::Publish(const Events::InputCommand& e)
|
||||
{
|
||||
auto key = std::make_pair(e.PlayerID, e.Command);
|
||||
m_CommandQueue[key].push_back(e.Value);
|
||||
}
|
||||
|
||||
bool InputProxy::OnBindOrigin(const Events::BindOrigin& e)
|
||||
|
||||
@@ -50,6 +50,16 @@ bool MouseInputHandler::BindOrigin(std::string origin, std::string command, floa
|
||||
return false;
|
||||
}
|
||||
|
||||
float MouseInputHandler::GetCommandValue(std::string command)
|
||||
{
|
||||
auto it = m_CommandValues.find(command);
|
||||
if (it != m_CommandValues.end()) {
|
||||
return m_CommandValues[command];
|
||||
} else {
|
||||
return 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
bool MouseInputHandler::OnMousePress(const Events::MousePress& e)
|
||||
{
|
||||
auto it = m_Bindings.find(e.Button);
|
||||
@@ -57,10 +67,10 @@ bool MouseInputHandler::OnMousePress(const Events::MousePress& e)
|
||||
return false;
|
||||
}
|
||||
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
std::tie(ic.Command, ic.Value) = it->second;
|
||||
m_InputProxy->Publish(ic);
|
||||
std::string command;
|
||||
float value;
|
||||
std::tie(command, value) = it->second;
|
||||
m_CommandValues[command] += value;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -72,11 +82,10 @@ bool MouseInputHandler::OnMouseRelease(const Events::MouseRelease& e)
|
||||
return false;
|
||||
}
|
||||
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
std::tie(ic.Command, std::ignore) = it->second;
|
||||
ic.Value = 0;
|
||||
m_InputProxy->Publish(ic);
|
||||
std::string command;
|
||||
float value;
|
||||
std::tie(command, value) = it->second;
|
||||
m_CommandValues[command] -= value;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -87,7 +96,7 @@ bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e)
|
||||
auto it = m_Axes.find('X');
|
||||
if (it != m_Axes.end()) {
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
ic.PlayerID = -1;
|
||||
std::tie(ic.Command, ic.Value) = it->second;
|
||||
ic.Value *= e.DeltaX;
|
||||
m_InputProxy->Publish(ic);
|
||||
@@ -98,7 +107,7 @@ bool MouseInputHandler::OnMouseMove(const Events::MouseMove& e)
|
||||
auto it = m_Axes.find('Y');
|
||||
if (it != m_Axes.end()) {
|
||||
Events::InputCommand ic;
|
||||
ic.PlayerID = 0;
|
||||
ic.PlayerID = -1;
|
||||
std::tie(ic.Command, ic.Value) = it->second;
|
||||
ic.Value *= e.DeltaY;
|
||||
m_InputProxy->Publish(ic);
|
||||
@@ -117,5 +126,4 @@ bool MouseInputHandler::hasOrigin(std::string origin)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "Rendering/Renderer.h"
|
||||
#include "Rendering/DebugCameraInputController.h"
|
||||
|
||||
void Renderer::Initialize()
|
||||
{
|
||||
@@ -83,6 +84,8 @@ void Renderer::InitializeShaders()
|
||||
|
||||
void Renderer::InputUpdate(double dt)
|
||||
{
|
||||
static DebugCameraInputController<Renderer> firstPersonInputController(m_EventBroker, -1);
|
||||
|
||||
glm::vec3 m_Position = m_Camera->Position();
|
||||
if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS)
|
||||
{
|
||||
@@ -112,37 +115,15 @@ void Renderer::InputUpdate(double dt)
|
||||
m_CameraMoveSpeed = 0.5f;
|
||||
}
|
||||
|
||||
|
||||
static double mousePosX, mousePosY;
|
||||
glfwGetCursorPos(m_Window, &mousePosX, &mousePosY);
|
||||
|
||||
if (glfwGetKey(m_Window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
|
||||
|
||||
double deltaX, deltaY;
|
||||
deltaX = mousePosX - (float)Resolution().Width / 2;
|
||||
deltaY = mousePosY - (float)Resolution().Height / 2;
|
||||
|
||||
float rotationY = -deltaY / 300.f;
|
||||
float rotationX = -deltaX / 300.f;
|
||||
glm::quat orientation = m_Camera->Orientation();
|
||||
|
||||
|
||||
orientation = orientation * glm::angleAxis<float>(rotationY, glm::vec3(1, 0, 0));
|
||||
orientation = glm::angleAxis<float>(rotationX, glm::vec3(0, 1, 0)) * orientation;
|
||||
|
||||
m_Camera->SetOrientation(orientation);
|
||||
|
||||
glfwSetCursorPos(m_Window, Resolution().Width / 2, Resolution().Height / 2);
|
||||
}
|
||||
m_Camera->SetPosition(m_Position);
|
||||
firstPersonInputController.Update(dt);
|
||||
m_Camera->SetOrientation(firstPersonInputController.Orientation());
|
||||
m_Camera->SetPosition(firstPersonInputController.Position());
|
||||
}
|
||||
|
||||
void Renderer::Update(double dt)
|
||||
{
|
||||
m_EventBroker->Process<Renderer>();
|
||||
InputUpdate(dt);
|
||||
|
||||
}
|
||||
|
||||
void Renderer::Draw(RenderQueueCollection& rq)
|
||||
|
||||
Reference in New Issue
Block a user