refactor input.h and related

This commit is contained in:
A-lex-Ra 2024-02-23 13:37:07 +06:00
parent 9c31c6c8b7
commit 8bb1045e34
12 changed files with 236 additions and 254 deletions

View File

@ -90,14 +90,14 @@ void Engine::updateTimers() {
} }
void Engine::updateHotkeys() { void Engine::updateHotkeys() {
if (Events::jpressed(keycode::F2)) { if (Events::jpressed(static_cast<int>(keycode::F2))) {
std::unique_ptr<ImageData> image(Window::takeScreenshot()); std::unique_ptr<ImageData> image(Window::takeScreenshot());
image->flipY(); image->flipY();
fs::path filename = paths->getScreenshotFile("png"); fs::path filename = paths->getScreenshotFile("png");
png::write_image(filename.string(), image.get()); png::write_image(filename.string(), image.get());
std::cout << "saved screenshot as " << filename << std::endl; std::cout << "saved screenshot as " << filename << std::endl;
} }
if (Events::jpressed(keycode::F11)) { if (Events::jpressed(static_cast<int>(keycode::F11))) {
Window::toggleFullscreen(); Window::toggleFullscreen();
} }
} }

View File

@ -205,7 +205,7 @@ bool SlotView::isHighlighted() const {
return highlighted; return highlighted;
} }
void SlotView::clicked(gui::GUI* gui, int button) { void SlotView::clicked(gui::GUI* gui, mousecode button) {
if (bound == nullptr) if (bound == nullptr)
return; return;
@ -260,7 +260,7 @@ void SlotView::clicked(gui::GUI* gui, int button) {
} }
void SlotView::focus(gui::GUI* gui) { void SlotView::focus(gui::GUI* gui) {
clicked(gui, 0); clicked(gui, mousecode::BUTTON_1);
} }
void SlotView::bind( void SlotView::bind(

View File

@ -72,7 +72,7 @@ public:
void setHighlighted(bool flag); void setHighlighted(bool flag);
bool isHighlighted() const; bool isHighlighted() const;
virtual void clicked(gui::GUI*, int) override; virtual void clicked(gui::GUI*, mousecode) override;
virtual void focus(gui::GUI*) override; virtual void focus(gui::GUI*) override;
void bind( void bind(

View File

@ -71,10 +71,10 @@ void GUI::actMouse(float delta) {
pressed = nullptr; pressed = nullptr;
} }
if (hover) { if (hover) {//WTF?! FIXME
for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) { for (int i = static_cast<int>(mousecode::BUTTON_1); i < static_cast<int>(mousecode::BUTTON_1)+12; i++) {
if (Events::jclicked(i)) { if (Events::jclicked(i)) {
hover->clicked(this, i); hover->clicked(this, static_cast<mousecode>(i));
} }
} }
} }
@ -101,7 +101,7 @@ void GUI::act(float delta) {
focus->typed(codepoint); focus->typed(codepoint);
} }
for (auto key : Events::pressedKeys) { for (auto key : Events::pressedKeys) {
focus->keyPressed(key); focus->keyPressed(static_cast<keycode>(key)); // temporary solution
} }
if (!Events::_cursor_locked) { if (!Events::_cursor_locked) {

View File

@ -7,6 +7,7 @@
#include <string> #include <string>
#include <functional> #include <functional>
#include "../../delegates.h" #include "../../delegates.h"
#include "../../window/input.h"
class GfxContext; class GfxContext;
class Assets; class Assets;
@ -85,7 +86,7 @@ namespace gui {
virtual void focus(GUI*) {focused = true;} virtual void focus(GUI*) {focused = true;}
virtual void click(GUI*, int x, int y); virtual void click(GUI*, int x, int y);
virtual void clicked(GUI*, int button) {} virtual void clicked(GUI*, mousecode button) {}
virtual void mouseMove(GUI*, int x, int y) {}; virtual void mouseMove(GUI*, int x, int y) {};
virtual void mouseRelease(GUI*, int x, int y); virtual void mouseRelease(GUI*, int x, int y);
virtual void scrolled(int value); virtual void scrolled(int value);
@ -98,7 +99,7 @@ namespace gui {
virtual bool isFocuskeeper() const {return false;} virtual bool isFocuskeeper() const {return false;}
virtual void typed(unsigned int codepoint) {}; virtual void typed(unsigned int codepoint) {};
virtual void keyPressed(int key) {}; virtual void keyPressed(keycode key) {};
/** Check if screen position is inside of the element /** Check if screen position is inside of the element
* @param pos screen position */ * @param pos screen position */

View File

@ -441,7 +441,7 @@ void TextBox::mouseMove(GUI*, int x, int y) {
extendSelection(index); extendSelection(index);
} }
void TextBox::keyPressed(int key) { void TextBox::keyPressed(keycode key) {
bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT); bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT);
uint previousCaret = caret; uint previousCaret = caret;
if (key == keycode::BACKSPACE) { if (key == keycode::BACKSPACE) {
@ -624,16 +624,14 @@ void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
label->setText(util::str2wstr_utf8(binding.text())); label->setText(util::str2wstr_utf8(binding.text()));
} }
void InputBindBox::clicked(GUI*, int button) { void InputBindBox::clicked(GUI*, mousecode button) {
binding.type = inputtype::mouse; binding.reset(button);
binding.code = button;
defocus(); defocus();
} }
void InputBindBox::keyPressed(int key) { void InputBindBox::keyPressed(keycode key) {
if (key != keycode::ESCAPE) { if (key != keycode::ESCAPE) {
binding.type = inputtype::keyboard; binding.reset(key);
binding.code = key;
} }
defocus(); defocus();
} }

View File

@ -139,7 +139,7 @@ namespace gui {
virtual void draw(const GfxContext* pctx, Assets* assets) override; virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override; virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void typed(unsigned int codepoint) override; virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(int key) override; virtual void keyPressed(keycode key) override;
virtual void setTextSupplier(wstringsupplier supplier); virtual void setTextSupplier(wstringsupplier supplier);
virtual void setTextConsumer(wstringconsumer consumer); virtual void setTextConsumer(wstringconsumer consumer);
virtual void setTextValidator(wstringchecker validator); virtual void setTextValidator(wstringchecker validator);
@ -178,8 +178,8 @@ namespace gui {
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f)); InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f));
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override; virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void clicked(GUI*, int button) override; virtual void clicked(GUI*, mousecode button) override;
virtual void keyPressed(int key) override; virtual void keyPressed(keycode key) override;
virtual bool isFocuskeeper() const override {return true;} virtual bool isFocuskeeper() const override {return true;}
}; };

View File

@ -386,9 +386,9 @@ void Hud::update(bool visible) {
hotbarView->setVisible(visible); hotbarView->setVisible(visible);
if (!gui->isFocusCaught() && !pause) { if (!gui->isFocusCaught() && !pause) {
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) { for (int i = static_cast<int>(keycode::NUM_1); i <= static_cast<int>(keycode::NUM_9); i++) {
if (Events::jpressed(i)) { if (Events::jpressed(i)) {
player->setChosenSlot(i - keycode::NUM_1); player->setChosenSlot(i - static_cast<int>(keycode::NUM_1));
} }
} }
if (Events::jpressed(keycode::NUM_0)) { if (Events::jpressed(keycode::NUM_0)) {

View File

@ -16,36 +16,48 @@ std::vector<uint> Events::codepoints;
std::vector<int> Events::pressedKeys; std::vector<int> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings; std::unordered_map<std::string, Binding> Events::bindings;
// Returns bool repr. of key state bool Events::pressed(keycode keycode) {
return pressed(static_cast<int>(keycode));
}
bool Events::pressed(int keycode) { bool Events::pressed(int keycode) {
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) { if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
fprintf(stderr, "pressed %i\n", keycode); fprintf(stderr, "pressed %i\n", keycode); //FIXME: unreasonable cstdio usage
return false; return false;
} }
return _keys[keycode]; return _keys[keycode];
} }
// Returns bool repr. of key state bool Events::jpressed(keycode keycode) {
bool Events::jpressed(int keycode){ return jpressed(static_cast<int>(keycode));
}
bool Events::jpressed(int keycode) {
return Events::pressed(keycode) && _frames[keycode] == _current; return Events::pressed(keycode) && _frames[keycode] == _current;
} }
// Returns bool repr. of mouse key state bool Events::clicked(mousecode button) {
bool Events::clicked(int button){ return clicked(static_cast<int>(button));
}
bool Events::clicked(int button) {
return Events::pressed(_MOUSE_KEYS_OFFSET + button); return Events::pressed(_MOUSE_KEYS_OFFSET + button);
} }
// Returns bool repr. of mouse key state bool Events::jclicked(mousecode button) {
bool Events::jclicked(int button){ return jclicked(static_cast<int>(button));
}
bool Events::jclicked(int button) {
return Events::jpressed(_MOUSE_KEYS_OFFSET + button); return Events::jpressed(_MOUSE_KEYS_OFFSET + button);
} }
void Events::toggleCursor(){ void Events::toggleCursor() {
_cursor_locked = !_cursor_locked; _cursor_locked = !_cursor_locked;
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
} }
void Events::pollEvents(){ void Events::pollEvents() {
_current++; _current++;
delta.x = 0.f; delta.x = 0.f;
delta.y = 0.f; delta.y = 0.f;
@ -60,8 +72,8 @@ void Events::pollEvents(){
bool newstate = false; bool newstate = false;
switch (binding.type) { switch (binding.type) {
case inputtype::keyboard: newstate = pressed(binding.code); break; case inputtype::keyboard: newstate = pressed(binding.code); break;
case inputtype::mouse: newstate = clicked(binding.code); break; case inputtype::mouse: newstate = clicked(binding.code); break;
} }
if (newstate) { if (newstate) {
@ -69,7 +81,8 @@ void Events::pollEvents(){
binding.state = true; binding.state = true;
binding.justChange = true; binding.justChange = true;
} }
} else { }
else {
if (binding.state) { if (binding.state) {
binding.state = false; binding.state = false;
binding.justChange = true; binding.justChange = true;
@ -78,8 +91,16 @@ void Events::pollEvents(){
} }
} }
void Events::bind(std::string name, inputtype type, keycode code) {
bind(name, type, static_cast<int>(code));
}
void Events::bind(std::string name, inputtype type, mousecode code) {
bind(name, type, static_cast<int>(code));
}
void Events::bind(std::string name, inputtype type, int code) { void Events::bind(std::string name, inputtype type, int code) {
bindings[name] = {type, code, false, false}; bindings[name] = { type, code, false, false };
} }
bool Events::active(std::string name) { bool Events::active(std::string name) {
@ -99,20 +120,22 @@ bool Events::jactive(std::string name) {
} }
void Events::setKey(int key, bool b) { void Events::setKey(int key, bool b) {
Events::_keys[key] = b; Events::_keys[key] = b;
Events::_frames[key] = Events::_current; Events::_frames[key] = Events::_current;
} }
void Events::setButton(int button, bool b) { void Events::setButton(int button, bool b) {
setKey(_MOUSE_KEYS_OFFSET + button, b); setKey(_MOUSE_KEYS_OFFSET + button, b);
} }
void Events::setPosition(float xpos, float ypos) { void Events::setPosition(float xpos, float ypos) {
if (Events::cursor_drag) { if (Events::cursor_drag) {
Events::delta.x += xpos - Events::cursor.x; Events::delta.x += xpos - Events::cursor.x;
Events::delta.y += ypos - Events::cursor.y; Events::delta.y += ypos - Events::cursor.y;
} else }
Events::cursor_drag = true; else {
Events::cursor.x = xpos; Events::cursor_drag = true;
Events::cursor.y = ypos; }
Events::cursor.x = xpos;
Events::cursor.y = ypos;
} }

View File

@ -29,14 +29,20 @@ public:
static void pollEvents(); static void pollEvents();
static bool pressed(keycode keycode);
static bool pressed(int keycode); static bool pressed(int keycode);
static bool jpressed(keycode keycode);
static bool jpressed(int keycode); static bool jpressed(int keycode);
static bool clicked(mousecode button);
static bool clicked(int button); static bool clicked(int button);
static bool jclicked(mousecode button);
static bool jclicked(int button); static bool jclicked(int button);
static void toggleCursor(); static void toggleCursor();
static void bind(std::string name, inputtype type, keycode code);
static void bind(std::string name, inputtype type, mousecode code);
static void bind(std::string name, inputtype type, int code); static void bind(std::string name, inputtype type, int code);
static bool active(std::string name); static bool active(std::string name);
static bool jactive(std::string name); static bool jactive(std::string name);

View File

@ -1,99 +1,35 @@
#include "input.h" #include "input.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
int keycode::ENTER = GLFW_KEY_ENTER;
int keycode::TAB = GLFW_KEY_TAB;
int keycode::SPACE = GLFW_KEY_SPACE;
int keycode::BACKSPACE = GLFW_KEY_BACKSPACE;
int keycode::LEFT_SHIFT = GLFW_KEY_LEFT_SHIFT;
int keycode::LEFT_CONTROL = GLFW_KEY_LEFT_CONTROL;
int keycode::LEFT_ALT = GLFW_KEY_LEFT_ALT;
int keycode::RIGHT_SHIFT = GLFW_KEY_RIGHT_SHIFT;
int keycode::RIGHT_CONTROL = GLFW_KEY_RIGHT_CONTROL;
int keycode::RIGHT_ALT = GLFW_KEY_RIGHT_ALT;
int keycode::ESCAPE = GLFW_KEY_ESCAPE;
int keycode::CAPS_LOCK = GLFW_KEY_CAPS_LOCK;
int keycode::LEFT = GLFW_KEY_LEFT;
int keycode::RIGHT = GLFW_KEY_RIGHT;
int keycode::DOWN = GLFW_KEY_DOWN;
int keycode::UP = GLFW_KEY_UP;
int keycode::F1 = GLFW_KEY_F1;
int keycode::F2 = GLFW_KEY_F2;
int keycode::F3 = GLFW_KEY_F3;
int keycode::F4 = GLFW_KEY_F4;
int keycode::F5 = GLFW_KEY_F5;
int keycode::F6 = GLFW_KEY_F6;
int keycode::F7 = GLFW_KEY_F7;
int keycode::F8 = GLFW_KEY_F8;
int keycode::F9 = GLFW_KEY_F9;
int keycode::F10 = GLFW_KEY_F10;
int keycode::F11 = GLFW_KEY_F11;
int keycode::F12 = GLFW_KEY_F12;
int keycode::A = GLFW_KEY_A;
int keycode::B = GLFW_KEY_B;
int keycode::C = GLFW_KEY_C;
int keycode::D = GLFW_KEY_D;
int keycode::E = GLFW_KEY_E;
int keycode::F = GLFW_KEY_F;
int keycode::G = GLFW_KEY_G;
int keycode::H = GLFW_KEY_H;
int keycode::I = GLFW_KEY_I;
int keycode::J = GLFW_KEY_J;
int keycode::K = GLFW_KEY_K;
int keycode::L = GLFW_KEY_L;
int keycode::M = GLFW_KEY_M;
int keycode::N = GLFW_KEY_N;
int keycode::O = GLFW_KEY_O;
int keycode::P = GLFW_KEY_P;
int keycode::Q = GLFW_KEY_Q;
int keycode::R = GLFW_KEY_R;
int keycode::S = GLFW_KEY_S;
int keycode::T = GLFW_KEY_T;
int keycode::U = GLFW_KEY_U;
int keycode::V = GLFW_KEY_V;
int keycode::W = GLFW_KEY_W;
int keycode::X = GLFW_KEY_X;
int keycode::Y = GLFW_KEY_Y;
int keycode::Z = GLFW_KEY_Z;
int keycode::NUM_0 = GLFW_KEY_0;
int keycode::NUM_1 = GLFW_KEY_1;
int keycode::NUM_2 = GLFW_KEY_2;
int keycode::NUM_3 = GLFW_KEY_3;
int keycode::NUM_4 = GLFW_KEY_4;
int keycode::NUM_5 = GLFW_KEY_5;
int keycode::NUM_6 = GLFW_KEY_6;
int keycode::NUM_7 = GLFW_KEY_7;
int keycode::NUM_8 = GLFW_KEY_8;
int keycode::NUM_9 = GLFW_KEY_9;
int keycode::MENU = GLFW_KEY_MENU;
int keycode::PAUSE = GLFW_KEY_PAUSE;
int keycode::INSERT = GLFW_KEY_INSERT;
int keycode::LEFT_SUPER = GLFW_KEY_LEFT_SUPER;
int keycode::RIGHT_SUPER = GLFW_KEY_RIGHT_SUPER;
int keycode::DELETE = GLFW_KEY_DELETE;
int keycode::PAGE_UP = GLFW_KEY_PAGE_UP;
int keycode::PAGE_DOWN = GLFW_KEY_PAGE_DOWN;
int keycode::HOME = GLFW_KEY_HOME;
int keycode::END = GLFW_KEY_END;
int keycode::PRINT_SCREEN = GLFW_KEY_PRINT_SCREEN;
int keycode::NUM_LOCK = GLFW_KEY_NUM_LOCK;
int keycode::LEFT_BRACKET = GLFW_KEY_LEFT_BRACKET;
int keycode::RIGHT_BRACKET = GLFW_KEY_RIGHT_BRACKET;
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif // _WIN32 #endif // _WIN32
const std::string keycode::name(int code) { void Binding::reset(inputtype type, int code) {
this->type = type;
this->code = code;
}
void Binding::reset(keycode code) {
reset(inputtype::keyboard, static_cast<int>(code));
}
void Binding::reset(mousecode code) {
reset(inputtype::mouse, static_cast<int>(code));
}
namespace input_util {
std::string to_string(keycode code) {
#ifdef _WIN32 #ifdef _WIN32
char name[64]; char name[64];
int result = GetKeyNameTextA(glfwGetKeyScancode(code) << 16, name, 64); int result = GetKeyNameTextA(glfwGetKeyScancode(static_cast<int>(code)) << 16, name, 64);
if (result == NULL) return "Unknown"; if (result == NULL) return "Unknown";
return std::string(name); return std::string(name);
#else #else
const char* name = glfwGetKeyName(code, glfwGetKeyScancode(code)); const char* name = glfwGetKeyName(code, glfwGetKeyScancode(code));
if (name == nullptr) { if (name == nullptr) {
switch (code) { switch (code) {
case GLFW_KEY_TAB: return "Tab"; case GLFW_KEY_TAB: return "Tab";
case GLFW_KEY_LEFT_CONTROL: return "Left Ctrl"; case GLFW_KEY_LEFT_CONTROL: return "Left Ctrl";
case GLFW_KEY_RIGHT_CONTROL: return "Right Ctrl"; case GLFW_KEY_RIGHT_CONTROL: return "Right Ctrl";
@ -136,21 +72,19 @@ const std::string keycode::name(int code) {
case GLFW_KEY_PAUSE: return "Pause"; case GLFW_KEY_PAUSE: return "Pause";
default: default:
return "Unknown"; return "Unknown";
}
} }
} return std::string(name);
return std::string(name);
#endif // _WIN32 #endif // _WIN32
}
int mousecode::BUTTON_1 = GLFW_MOUSE_BUTTON_1;
int mousecode::BUTTON_2 = GLFW_MOUSE_BUTTON_2;
int mousecode::BUTTON_3 = GLFW_MOUSE_BUTTON_3;
const std::string mousecode::name(int code) {
switch (code) {
case GLFW_MOUSE_BUTTON_1: return "LMB";
case GLFW_MOUSE_BUTTON_2: return "RMB";
case GLFW_MOUSE_BUTTON_3: return "MMB";
} }
return "unknown button";
std::string to_string(mousecode code) {
switch (code) {
case mousecode::BUTTON_1: return "LMB";
case mousecode::BUTTON_2: return "RMB";
case mousecode::BUTTON_3: return "MMB";
}
return "unknown button";
}
} }

View File

@ -3,123 +3,143 @@
#include <string> #include <string>
struct keycode { /**
static int ENTER; * @brief Represents glfw3 keycode values.
static int TAB; */
static int SPACE; enum class keycode : int {
static int BACKSPACE; ENTER = 257,
static int LEFT_CONTROL; TAB = 258,
static int LEFT_SHIFT; SPACE = 32,
static int LEFT_ALT; BACKSPACE = 259,
static int RIGHT_CONTROL; LEFT_SHIFT = 340,
static int RIGHT_SHIFT; LEFT_CONTROL = 341,
static int RIGHT_ALT; LEFT_ALT = 342,
static int ESCAPE; RIGHT_SHIFT = 344,
static int CAPS_LOCK; RIGHT_CONTROL = 345,
static int LEFT; RIGHT_ALT = 346,
static int RIGHT; ESCAPE = 256,
static int DOWN; CAPS_LOCK = 280,
static int UP; LEFT = 263,
static int F1; RIGHT = 262,
static int F2; DOWN = 264,
static int F3; UP = 265,
static int F4; F1 = 290,
static int F5; F2 = 291,
static int F6; F3 = 292,
static int F7; F4 = 293,
static int F8; F5 = 294,
static int F9; F6 = 295,
static int F10; F7 = 296,
static int F11; F8 = 297,
static int F12; F9 = 298,
static int A; F10 = 299,
static int B; F11 = 300,
static int C; F12 = 301,
static int D; A = 65,
static int E; B = 66,
static int F; C = 67,
static int G; D = 68,
static int H; E = 69,
static int I; F = 70,
static int J; G = 71,
static int K; H = 72,
static int L; I = 73,
static int M; J = 74,
static int N; K = 75,
static int O; L = 76,
static int P; M = 77,
static int Q; N = 78,
static int R; O = 79,
static int S; P = 80,
static int T; Q = 81,
static int U; R = 82,
static int V; S = 83,
static int W; T = 84,
static int X; U = 85,
static int Y; V = 86,
static int Z; W = 87,
static int NUM_0; X = 88,
static int NUM_1; Y = 89,
static int NUM_2; Z = 90,
static int NUM_3; NUM_0 = 48,
static int NUM_4; NUM_1 = 49,
static int NUM_5; NUM_2 = 50,
static int NUM_6; NUM_3 = 51,
static int NUM_7; NUM_4 = 52,
static int NUM_8; NUM_5 = 53,
static int NUM_9; NUM_6 = 54,
static int MENU; NUM_7 = 55,
static int PAUSE; NUM_8 = 56,
static int INSERT; NUM_9 = 57,
static int LEFT_SUPER; MENU = 348,
static int RIGHT_SUPER; PAUSE = 284,
static int DELETE; INSERT = 260,
static int PAGE_UP; LEFT_SUPER = 343,
static int PAGE_DOWN; RIGHT_SUPER = 347,
static int HOME; DELETE = 261,
static int END; PAGE_UP = 266,
static int PRINT_SCREEN; PAGE_DOWN = 267,
static int NUM_LOCK; HOME = 268,
static int LEFT_BRACKET; END = 269,
static int RIGHT_BRACKET; PRINT_SCREEN = 283,
NUM_LOCK = 282,
static const std::string name(int code); LEFT_BRACKET = 91,
RIGHT_BRACKET = 93,
}; };
struct mousecode {
static int BUTTON_1;
static int BUTTON_2;
static int BUTTON_3;
static const std::string name(int code); /**
* @brief Represents glfw3 mouse button IDs.
* @details There is a subset of glfw3 mouse button IDs.
*/
enum class mousecode : int {
BUTTON_1 = 0, // Left mouse button
BUTTON_2 = 1, // Right mouse button
BUTTON_3 = 2, // Middle mouse button
}; };
namespace input_util {
// @return Key label by keycode
std::string to_string(keycode code);
// @return Mouse button label by keycode
std::string to_string(mousecode code);
}
enum class inputtype { enum class inputtype {
keyboard, keyboard,
mouse, mouse,
}; };
struct Binding { struct Binding {
inputtype type; inputtype type;
int code; int code;
bool state = false; bool state = false;
bool justChange = false; bool justChange = false;
bool active() const { bool active() const {
return state; return state;
} }
bool jactive() const { bool jactive() const {
return state && justChange; return state && justChange;
} }
const std::string text() const { void reset(inputtype, int);
switch (type) { void reset(keycode);
case inputtype::keyboard: return keycode::name(code); void reset(mousecode);
case inputtype::mouse: return mousecode::name(code);
} inline const std::string text() const {
return "<unknown input type>"; switch (type) {
} case inputtype::keyboard: {
return input_util::to_string(static_cast<keycode>(code));
}
case inputtype::mouse: {
return input_util::to_string(static_cast<mousecode>(code));
}
}
return "<unknown input type>";
}
}; };