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() {
if (Events::jpressed(keycode::F2)) {
if (Events::jpressed(static_cast<int>(keycode::F2))) {
std::unique_ptr<ImageData> image(Window::takeScreenshot());
image->flipY();
fs::path filename = paths->getScreenshotFile("png");
png::write_image(filename.string(), image.get());
std::cout << "saved screenshot as " << filename << std::endl;
}
if (Events::jpressed(keycode::F11)) {
if (Events::jpressed(static_cast<int>(keycode::F11))) {
Window::toggleFullscreen();
}
}

View File

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

View File

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

View File

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

View File

@ -7,6 +7,7 @@
#include <string>
#include <functional>
#include "../../delegates.h"
#include "../../window/input.h"
class GfxContext;
class Assets;
@ -85,7 +86,7 @@ namespace gui {
virtual void focus(GUI*) {focused = true;}
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 mouseRelease(GUI*, int x, int y);
virtual void scrolled(int value);
@ -98,7 +99,7 @@ namespace gui {
virtual bool isFocuskeeper() const {return false;}
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
* @param pos screen position */

View File

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

View File

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

View File

@ -386,9 +386,9 @@ void Hud::update(bool visible) {
hotbarView->setVisible(visible);
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)) {
player->setChosenSlot(i - keycode::NUM_1);
player->setChosenSlot(i - static_cast<int>(keycode::NUM_1));
}
}
if (Events::jpressed(keycode::NUM_0)) {

View File

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

View File

@ -29,14 +29,20 @@ public:
static void pollEvents();
static bool pressed(keycode keycode);
static bool pressed(int keycode);
static bool jpressed(keycode keycode);
static bool jpressed(int keycode);
static bool clicked(mousecode button);
static bool clicked(int button);
static bool jclicked(mousecode button);
static bool jclicked(int button);
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 bool active(std::string name);
static bool jactive(std::string name);

View File

@ -1,99 +1,35 @@
#include "input.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
#include <windows.h>
#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
char name[64];
int result = GetKeyNameTextA(glfwGetKeyScancode(code) << 16, name, 64);
if (result == NULL) return "Unknown";
return std::string(name);
char name[64];
int result = GetKeyNameTextA(glfwGetKeyScancode(static_cast<int>(code)) << 16, name, 64);
if (result == NULL) return "Unknown";
return std::string(name);
#else
const char* name = glfwGetKeyName(code, glfwGetKeyScancode(code));
if (name == nullptr) {
switch (code) {
const char* name = glfwGetKeyName(code, glfwGetKeyScancode(code));
if (name == nullptr) {
switch (code) {
case GLFW_KEY_TAB: return "Tab";
case GLFW_KEY_LEFT_CONTROL: return "Left 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";
default:
return "Unknown";
}
}
}
return std::string(name);
return std::string(name);
#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>
struct keycode {
static int ENTER;
static int TAB;
static int SPACE;
static int BACKSPACE;
static int LEFT_CONTROL;
static int LEFT_SHIFT;
static int LEFT_ALT;
static int RIGHT_CONTROL;
static int RIGHT_SHIFT;
static int RIGHT_ALT;
static int ESCAPE;
static int CAPS_LOCK;
static int LEFT;
static int RIGHT;
static int DOWN;
static int UP;
static int F1;
static int F2;
static int F3;
static int F4;
static int F5;
static int F6;
static int F7;
static int F8;
static int F9;
static int F10;
static int F11;
static int F12;
static int A;
static int B;
static int C;
static int D;
static int E;
static int F;
static int G;
static int H;
static int I;
static int J;
static int K;
static int L;
static int M;
static int N;
static int O;
static int P;
static int Q;
static int R;
static int S;
static int T;
static int U;
static int V;
static int W;
static int X;
static int Y;
static int Z;
static int NUM_0;
static int NUM_1;
static int NUM_2;
static int NUM_3;
static int NUM_4;
static int NUM_5;
static int NUM_6;
static int NUM_7;
static int NUM_8;
static int NUM_9;
static int MENU;
static int PAUSE;
static int INSERT;
static int LEFT_SUPER;
static int RIGHT_SUPER;
static int DELETE;
static int PAGE_UP;
static int PAGE_DOWN;
static int HOME;
static int END;
static int PRINT_SCREEN;
static int NUM_LOCK;
static int LEFT_BRACKET;
static int RIGHT_BRACKET;
static const std::string name(int code);
/**
* @brief Represents glfw3 keycode values.
*/
enum class keycode : int {
ENTER = 257,
TAB = 258,
SPACE = 32,
BACKSPACE = 259,
LEFT_SHIFT = 340,
LEFT_CONTROL = 341,
LEFT_ALT = 342,
RIGHT_SHIFT = 344,
RIGHT_CONTROL = 345,
RIGHT_ALT = 346,
ESCAPE = 256,
CAPS_LOCK = 280,
LEFT = 263,
RIGHT = 262,
DOWN = 264,
UP = 265,
F1 = 290,
F2 = 291,
F3 = 292,
F4 = 293,
F5 = 294,
F6 = 295,
F7 = 296,
F8 = 297,
F9 = 298,
F10 = 299,
F11 = 300,
F12 = 301,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
NUM_0 = 48,
NUM_1 = 49,
NUM_2 = 50,
NUM_3 = 51,
NUM_4 = 52,
NUM_5 = 53,
NUM_6 = 54,
NUM_7 = 55,
NUM_8 = 56,
NUM_9 = 57,
MENU = 348,
PAUSE = 284,
INSERT = 260,
LEFT_SUPER = 343,
RIGHT_SUPER = 347,
DELETE = 261,
PAGE_UP = 266,
PAGE_DOWN = 267,
HOME = 268,
END = 269,
PRINT_SCREEN = 283,
NUM_LOCK = 282,
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 {
keyboard,
mouse,
keyboard,
mouse,
};
struct Binding {
inputtype type;
int code;
bool state = false;
bool justChange = false;
inputtype type;
int code;
bool state = false;
bool justChange = false;
bool active() const {
return state;
}
bool active() const {
return state;
}
bool jactive() const {
return state && justChange;
}
bool jactive() const {
return state && justChange;
}
void reset(inputtype, int);
void reset(keycode);
void reset(mousecode);
const std::string text() const {
switch (type) {
case inputtype::keyboard: return keycode::name(code);
case inputtype::mouse: return mousecode::name(code);
}
return "<unknown input type>";
}
inline const std::string text() const {
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>";
}
};