Added Input

This commit is contained in:
sippeangelo
2015-11-24 10:37:50 +01:00
parent 0a1f0d96db
commit 5f9dce4303
22 changed files with 937 additions and 0 deletions
View File
+37
View File
@@ -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
+52
View File
@@ -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
+18
View File
@@ -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
+18
View File
@@ -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
+16
View File
@@ -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
+20
View File
@@ -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
+20
View File
@@ -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
+20
View File
@@ -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
+35
View File
@@ -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
+67
View File
@@ -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
View File
+24
View File
@@ -0,0 +1,24 @@
#ifndef Events_BindGamepadAxis_h__
#define Events_BindGamepadAxis_h__
#include "Core/EventBroker.h"
#include "Core/EGamepadAxis.h"
namespace Events
{
/** Called to bind a gamepad axis to an input command. */
struct BindGamepadAxis : Event
{
/** The axis to bind. */
Gamepad::Axis Axis;
/** The command to send. */
std::string Command;
/** The value to send for positive stimulation.
Multiplied by the 0-1 clamped value of the axis.
*/
float Value;
};
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef Events_BindGamepadButton_h__
#define Events_BindGamepadButton_h__
#include "Core/EventBroker.h"
#include "Core/EGamepadButton.h"
namespace Events
{
/** Called to bind a gamepad button to an input command. */
struct BindGamepadButton : Event
{
/** The gamepad button to bind. */
Gamepad::Button Button;
/** The command to send. */
std::string Command;
/** The value to send for positive stimulation.
Multiplied by the 0-1 clamped value of the button.
*/
float Value;
};
}
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef Events_BindKey_h__
#define Events_BindKey_h__
#include "Core/EventBroker.h"
namespace Events
{
/** Called to bind a keyboard key to an input command. */
struct BindKey : Event
{
/** The GLFW key code to bind. */
int KeyCode;
/** The command to send. */
std::string Command;
/** The value to send for positive stimulation.
Multiplied by the 0-1 clamped value of the key.
*/
float Value;
};
}
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef Events_BindMouseButton_h__
#define Events_BindMouseButton_h__
#include "Core/EventBroker.h"
namespace Events
{
/** Called to bind a mouse button to an input command. */
struct BindMouseButton : Event
{
/** The GLFW mouse button code to bind. */
int Button;
/** The command to send. */
std::string Command;
/** The value to send for positive stimulation.
Multiplied by the 0-1 clamped value of the button.
*/
float Value;
};
}
#endif
+21
View File
@@ -0,0 +1,21 @@
#ifndef Events_InputCommand_h__
#define Events_InputCommand_h__
#include "Core/EventBroker.h"
namespace Events
{
/** Thrown when an input command is sent. */
struct InputCommand : Event
{
/** Numerical ID of the player. */
unsigned int PlayerID;
/** The command that was sent. */
std::string Command;
/** The value of the command. */
float Value;
};
}
#endif
+78
View File
@@ -0,0 +1,78 @@
#ifndef InputSystem_h__
#define InputSystem_h__
#include <array>
#include <unordered_map>
#include "Core/System.h"
#include "Core/EKeyUp.h"
#include "Core/EKeyDown.h"
#include "Core/EMousePress.h"
#include "Core/EMouseRelease.h"
#include "Core/EGamepadAxis.h"
#include "Core/EGamepadButton.h"
#include "Core/Util/EnumClassHash.h"
#include "EBindKey.h"
#include "EBindMouseButton.h"
#include "EBindGamepadAxis.h"
#include "EBindGamepadButton.h"
#include "EInputCommand.h"
namespace Systems
{
class InputSystem : public System
{
public:
InputSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker)
{ }
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
void Update(double dt) override;
private:
std::unordered_map<std::string, std::unordered_map<int, float>> m_CommandKeyboardValues; // command string -> keyboard key value for command
std::unordered_map<std::string, std::unordered_map<int, float>> m_CommandMouseButtonValues; // command string -> mouse button value for command
std::unordered_map<std::string, std::unordered_map<Gamepad::Axis, float, EnumClassHash>> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command
std::unordered_map<std::string, std::unordered_map<Gamepad::Button, float, EnumClassHash>> m_CommandGamepadButtonValues; // command string -> gamepad button value for command
// Input binding tables
std::unordered_multimap<int, std::tuple<std::string, float>> m_KeyBindings; // GLFW_KEY... -> command string & value
std::unordered_multimap<int, std::tuple<std::string, float>> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
std::unordered_multimap<Gamepad::Axis, std::tuple<std::string, float>, EnumClassHash> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value
std::unordered_multimap<Gamepad::Button, std::tuple<std::string, float>, EnumClassHash> m_GamepadButtonBindings; // Gamepad::Button -> command string
// Input events
EventRelay<InputSystem, Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
EventRelay<InputSystem, Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp &event);
EventRelay<InputSystem, Events::MousePress> m_EMousePress;
bool OnMousePress(const Events::MousePress &event);
EventRelay<InputSystem, Events::MouseRelease> m_EMouseRelease;
bool OnMouseRelease(const Events::MouseRelease &event);
EventRelay<InputSystem, Events::GamepadAxis> m_EGamepadAxis;
bool OnGamepadAxis(const Events::GamepadAxis &event);
EventRelay<InputSystem, Events::GamepadButtonDown> m_EGamepadButtonDown;
bool OnGamepadButtonDown(const Events::GamepadButtonDown &event);
EventRelay<InputSystem, Events::GamepadButtonUp> m_EGamepadButtonUp;
bool OnGamepadButtonUp(const Events::GamepadButtonUp &event);
// Input binding events
EventRelay<InputSystem, Events::BindKey> m_EBindKey;
bool OnBindKey(const Events::BindKey &event);
EventRelay<InputSystem, Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event);
EventRelay<InputSystem, Events::BindGamepadAxis> m_EBindGamepadAxis;
bool OnBindGamepadAxis(const Events::BindGamepadAxis &event);
EventRelay<InputSystem, Events::BindGamepadButton> m_EBindGamepadButton;
bool OnBindGamepadButton(const Events::BindGamepadButton &event);
float GetCommandTotalValue(std::string command);
void PublishCommand(int playerID, std::string command, float value);
};
}
#endif