Added Input
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
#ifndef Events_GamepadAxis_h__
|
||||
#define Events_GamepadAxis_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Gamepad
|
||||
{
|
||||
/** Gamepad axis type */
|
||||
enum class Axis
|
||||
{
|
||||
LeftX,
|
||||
LeftY,
|
||||
RightX,
|
||||
RightY,
|
||||
LeftTrigger,
|
||||
RightTrigger,
|
||||
LAST = RightTrigger
|
||||
};
|
||||
}
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown when a gamepad axis changes value */
|
||||
struct GamepadAxis : Event
|
||||
{
|
||||
/** ID of the gamepad. */
|
||||
int GamepadID;
|
||||
/** The axis type. */
|
||||
Gamepad::Axis Axis;
|
||||
/** The new value of the axis. */
|
||||
float Value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef Events_GamepadButton_h__
|
||||
#define Events_GamepadButton_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Gamepad
|
||||
{
|
||||
/** Gamepad button type */
|
||||
enum class Button
|
||||
{
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Start,
|
||||
Back,
|
||||
LeftThumb,
|
||||
RightThumb,
|
||||
LeftShoulder,
|
||||
RightShoulder,
|
||||
A,
|
||||
B,
|
||||
X,
|
||||
Y,
|
||||
LAST = Y
|
||||
};
|
||||
}
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown on gamepad button press. */
|
||||
struct GamepadButtonDown : Event
|
||||
{
|
||||
/** ID of the gamepad. */
|
||||
int GamepadID;
|
||||
/** The button that was pressed. */
|
||||
Gamepad::Button Button;
|
||||
};
|
||||
|
||||
/** Thrown on gamepad button release. */
|
||||
struct GamepadButtonUp : Event
|
||||
{
|
||||
/** ID of the gamepad. */
|
||||
int GamepadID;
|
||||
/** The button that was released. */
|
||||
Gamepad::Button Button;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef Events_KeyDown_h__
|
||||
#define Events_KeyDown_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown on key press. */
|
||||
struct KeyDown : Event
|
||||
{
|
||||
/** GLFW key code */
|
||||
int KeyCode;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef Events_KeyUp_h__
|
||||
#define Events_KeyUp_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown on key release. */
|
||||
struct KeyUp : Event
|
||||
{
|
||||
/** GLFW key code */
|
||||
int KeyCode;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef Events_LockMouse_h__
|
||||
#define Events_LockMouse_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Called to lock mouse to the middle of the window. */
|
||||
struct LockMouse : Event { };
|
||||
/** Called to unlock mouse from the middle of the window. */
|
||||
struct UnlockMouse : Event { };
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef Events_MouseMove_h__
|
||||
#define Events_MouseMove_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown on mouse movement */
|
||||
struct MouseMove : Event
|
||||
{
|
||||
/** The new position of the cursor in window coordinates. */
|
||||
double X, Y;
|
||||
/** The delta movement of the mouse. */
|
||||
double DeltaX, DeltaY;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef Events_MousePress_h__
|
||||
#define Events_MousePress_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown on mouse button press. */
|
||||
struct MousePress : Event
|
||||
{
|
||||
/** GLFW mouse button code */
|
||||
int Button;
|
||||
/** The click position in window coordinates. */
|
||||
double X, Y;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef Events_MouseRelease_h__
|
||||
#define Events_MouseRelease_h__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
/** Thrown on mouse button release. */
|
||||
struct MouseRelease : Event
|
||||
{
|
||||
/** GLFW mouse button code */
|
||||
int Button;
|
||||
/** The mouse position in window coordinates. */
|
||||
double X, Y;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef InputController_h__
|
||||
#define InputController_h__
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "EMouseMove.h"
|
||||
#include "Input/EInputCommand.h"
|
||||
|
||||
template <typename EventContext>
|
||||
class InputController
|
||||
{
|
||||
public:
|
||||
InputController(std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
: 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; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<dd::EventBroker> EventBroker;
|
||||
|
||||
private:
|
||||
EventRelay<EventContext, Events::InputCommand> m_EInputCommand;
|
||||
EventRelay<EventContext, Events::MouseMove> m_EMouseMove;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
#ifndef InputManager_h__
|
||||
#define InputManager_h__
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
#include "EKeyDown.h"
|
||||
#include "EKeyUp.h"
|
||||
#include "EMousePress.h"
|
||||
#include "EMouseRelease.h"
|
||||
#include "EMouseMove.h"
|
||||
#include "ELockMouse.h"
|
||||
#include "EGamepadAxis.h"
|
||||
#include "EGamepadButton.h"
|
||||
|
||||
class InputManager
|
||||
{
|
||||
public:
|
||||
InputManager(GLFWwindow* window, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
: m_GLFWWindow(window)
|
||||
, EventBroker(eventBroker)
|
||||
, m_CurrentKeyState()
|
||||
, m_LastKeyState()
|
||||
, m_CurrentMouseState()
|
||||
, m_LastMouseState()
|
||||
, m_CurrentMouseX(0), m_CurrentMouseY(0)
|
||||
, m_LastMouseX(0), m_LastMouseY(0)
|
||||
, m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0)
|
||||
, m_MouseLocked(false)
|
||||
{ Initialize(); }
|
||||
|
||||
void Initialize();
|
||||
|
||||
static const short MAX_GAMEPADS = 4;
|
||||
|
||||
void Update(double dt);
|
||||
|
||||
private:
|
||||
GLFWwindow* m_GLFWWindow;
|
||||
std::shared_ptr<dd::EventBroker> EventBroker;
|
||||
|
||||
EventRelay<InputManager, Events::LockMouse> m_ELockMouse;
|
||||
bool OnLockMouse(const Events::LockMouse &event);
|
||||
EventRelay<InputManager, Events::UnlockMouse> m_EUnlockMouse;
|
||||
bool OnUnlockMouse(const Events::UnlockMouse &event);
|
||||
|
||||
std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
|
||||
std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
|
||||
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_CurrentMouseState;
|
||||
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
|
||||
typedef std::array<float, static_cast<int>(Gamepad::Axis::LAST) + 1> GamepadAxisState;
|
||||
std::array<GamepadAxisState, MAX_GAMEPADS> m_CurrentGamepadAxisState;
|
||||
std::array<GamepadAxisState, MAX_GAMEPADS> m_LastGamepadAxisState;
|
||||
typedef std::array<bool, static_cast<int>(Gamepad::Button::LAST) + 1> GamepadButtonState;
|
||||
std::array<GamepadButtonState, MAX_GAMEPADS> m_CurrentGamepadButtonState;
|
||||
std::array<GamepadButtonState, MAX_GAMEPADS> m_LastGamepadButtonState;
|
||||
|
||||
double m_CurrentMouseX, m_CurrentMouseY;
|
||||
double m_LastMouseX, m_LastMouseY;
|
||||
double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
|
||||
bool m_MouseLocked;
|
||||
|
||||
void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis);
|
||||
void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user