2019-08-24 17:39:35 -07:00
|
|
|
#pragma once
|
2019-04-10 18:44:17 -07:00
|
|
|
|
2021-08-22 11:58:52 -07:00
|
|
|
#include <list>
|
2020-05-07 17:21:56 -07:00
|
|
|
#include <functional>
|
2021-08-19 13:33:08 -07:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "util/Types.h"
|
2021-10-11 12:46:57 -07:00
|
|
|
#include "util/EventEmitter.h"
|
2021-08-19 13:33:08 -07:00
|
|
|
|
2021-09-02 23:48:09 -07:00
|
|
|
class GLFWwindow;
|
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/**
|
|
|
|
* Manages callbacks for key and mouse input, allows toggling mouse locking.
|
|
|
|
*/
|
2021-10-11 12:46:57 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
enum class InEvt {
|
|
|
|
Key,
|
|
|
|
KeyPress,
|
|
|
|
KeyRepeat,
|
|
|
|
KeyRelease,
|
|
|
|
Char,
|
|
|
|
Mouse,
|
|
|
|
MousePress,
|
|
|
|
MouseRelease
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Input : public EventEmitter<
|
|
|
|
Event<InEvt::Key, u32, i32>, Event<InEvt::KeyPress, u32>,
|
|
|
|
Event<InEvt::KeyRepeat, u32>, Event<InEvt::KeyRelease, u32>, Event<InEvt::Char, u32>,
|
|
|
|
Event<InEvt::Mouse, u32, i32>, Event<InEvt::MousePress, u32>, Event<InEvt::MouseRelease, u32>> {
|
2020-05-07 17:21:56 -07:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
public:
|
2021-10-11 12:46:57 -07:00
|
|
|
typedef InEvt Event;
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Activates the input listeners. */
|
|
|
|
void init(GLFWwindow* window);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Updates non-listener based data. */
|
2020-11-08 22:57:34 -08:00
|
|
|
void update();
|
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Returns true if the specified key is down. */
|
|
|
|
bool isKeyDown(u32 key);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Returns true if the specified mouse button is down. */
|
|
|
|
bool isMouseDown(u32 button);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Sets whether or not the mouse is locked and hidden for first person camera use. */
|
|
|
|
void setMouseLocked(bool lock);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/**
|
|
|
|
* Gets the position of the mouse relative to the screen.
|
|
|
|
* Will always report the center of the screen if the mouse is locked.
|
|
|
|
*/
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
ivec2 getMousePos();
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/**
|
|
|
|
* Gets the delta of the last mouse movement.
|
|
|
|
* Only works if the mouse is locked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ivec2 getMouseDelta();
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
private:
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Calls the key callbacks and sets the key state of the key provided. */
|
|
|
|
void updateKey(u32 key, i32 state);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-09-04 00:25:34 -07:00
|
|
|
/** Calls the char callbacks with the codepoint provided. */
|
|
|
|
void updateChar(u32 codepoint);
|
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Calls the button callbacks and sets the button state of the mouse button provided. */
|
|
|
|
void updateMouse(u32 button, i32 state);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the following key callbacks when the user scrolls.
|
|
|
|
* Right: 11, Left: 10, Up: 8, Down: 9
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void scrollCallback(GLFWwindow* window, f64 x, f64 y);
|
|
|
|
|
|
|
|
/** Calls the updateKey function with the proper key and state. */
|
|
|
|
static void keyCallback(GLFWwindow* window, i32 key, i32 code, i32 action, i32 mode);
|
|
|
|
|
2021-09-04 00:25:34 -07:00
|
|
|
/** Calls the updateChar function with the proper codepoint. */
|
|
|
|
static void charCallback(GLFWwindow* window, u32 codepoint);
|
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
/** Calls the updateMouse function with the proper button and state. */
|
|
|
|
static void mouseCallback(GLFWwindow* window, i32 key, i32 action, i32 mods);
|
|
|
|
|
|
|
|
const static ivec2 LOCKED_MOUSE_POS;
|
|
|
|
|
|
|
|
array<bool, 1024> keyState {};
|
|
|
|
array<bool, 3> mouseState {};
|
|
|
|
|
|
|
|
vec2 mouseDelta;
|
|
|
|
bool mouseLocked = false;
|
2020-11-08 22:57:34 -08:00
|
|
|
bool forceMouseUnlocked = false;
|
|
|
|
|
2021-08-19 13:33:08 -07:00
|
|
|
GLFWwindow* window = nullptr;
|
2019-10-15 00:40:16 -07:00
|
|
|
};
|